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"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
35 #include "gimple-expr.h"
38 /* List of hooks triggered on varpool_node events. */
39 struct varpool_node_hook_list
{
40 varpool_node_hook hook
;
42 struct varpool_node_hook_list
*next
;
45 /* List of hooks triggered when a node is removed. */
46 struct varpool_node_hook_list
*first_varpool_node_removal_hook
;
47 /* List of hooks triggered when an variable is inserted. */
48 struct varpool_node_hook_list
*first_varpool_variable_insertion_hook
;
50 /* Register HOOK to be called with DATA on each removed node. */
51 struct varpool_node_hook_list
*
52 varpool_add_node_removal_hook (varpool_node_hook hook
, void *data
)
54 struct varpool_node_hook_list
*entry
;
55 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
57 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
67 /* Remove ENTRY from the list of hooks called on removing nodes. */
69 varpool_remove_node_removal_hook (struct varpool_node_hook_list
*entry
)
71 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
79 /* Call all node removal hooks. */
81 varpool_call_node_removal_hooks (struct varpool_node
*node
)
83 struct varpool_node_hook_list
*entry
= first_varpool_node_removal_hook
;
86 entry
->hook (node
, entry
->data
);
91 /* Register HOOK to be called with DATA on each inserted node. */
92 struct varpool_node_hook_list
*
93 varpool_add_variable_insertion_hook (varpool_node_hook hook
, void *data
)
95 struct varpool_node_hook_list
*entry
;
96 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
98 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
108 /* Remove ENTRY from the list of hooks called on inserted nodes. */
110 varpool_remove_variable_insertion_hook (struct varpool_node_hook_list
*entry
)
112 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
114 while (*ptr
!= entry
)
120 /* Call all node insertion hooks. */
122 varpool_call_variable_insertion_hooks (struct varpool_node
*node
)
124 struct varpool_node_hook_list
*entry
= first_varpool_variable_insertion_hook
;
127 entry
->hook (node
, entry
->data
);
132 /* Allocate new callgraph node and insert it into basic data structures. */
134 struct varpool_node
*
135 varpool_create_empty_node (void)
137 struct varpool_node
*node
= ggc_alloc_cleared_varpool_node ();
138 node
->type
= SYMTAB_VARIABLE
;
142 /* Return varpool node assigned to DECL. Create new one when needed. */
143 struct varpool_node
*
144 varpool_node_for_decl (tree decl
)
146 struct varpool_node
*node
= varpool_get_node (decl
);
147 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
151 node
= varpool_create_empty_node ();
153 symtab_register_node (node
);
157 /* Remove node from the varpool. */
159 varpool_remove_node (struct varpool_node
*node
)
162 varpool_call_node_removal_hooks (node
);
163 symtab_unregister_node (node
);
165 /* Because we remove references from external functions before final compilation,
166 we may end up removing useful constructors.
167 FIXME: We probably want to trace boundaries better. */
168 if ((init
= ctor_for_folding (node
->decl
)) == error_mark_node
)
169 varpool_remove_initializer (node
);
171 DECL_INITIAL (node
->decl
) = init
;
175 /* Renove node initializer when it is no longer needed. */
177 varpool_remove_initializer (struct varpool_node
*node
)
179 if (DECL_INITIAL (node
->decl
)
180 && !DECL_IN_CONSTANT_POOL (node
->decl
)
181 /* Keep vtables for BINFO folding. */
182 && !DECL_VIRTUAL_P (node
->decl
)
183 /* FIXME: http://gcc.gnu.org/PR55395 */
184 && debug_info_level
== DINFO_LEVEL_NONE
185 /* When doing declaration merging we have duplicate
186 entries for given decl. Do not attempt to remove
187 the boides, or we will end up remiving
189 && cgraph_state
!= CGRAPH_LTO_STREAMING
)
190 DECL_INITIAL (node
->decl
) = error_mark_node
;
193 /* Dump given cgraph node. */
195 dump_varpool_node (FILE *f
, struct varpool_node
*node
)
197 dump_symtab_base (f
, node
);
198 fprintf (f
, " Availability: %s\n",
199 cgraph_function_flags_ready
200 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
202 fprintf (f
, " Varpool flags:");
203 if (DECL_INITIAL (node
->decl
))
204 fprintf (f
, " initialized");
206 fprintf (f
, " output");
207 if (node
->need_bounds_init
)
208 fprintf (f
, " need-bounds-init");
209 if (TREE_READONLY (node
->decl
))
210 fprintf (f
, " read-only");
211 if (ctor_for_folding (node
->decl
) != error_mark_node
)
212 fprintf (f
, " const-value-known");
216 /* Dump the variable pool. */
218 dump_varpool (FILE *f
)
220 struct varpool_node
*node
;
222 fprintf (f
, "variable pool:\n\n");
223 FOR_EACH_VARIABLE (node
)
224 dump_varpool_node (f
, node
);
227 /* Dump the variable pool to stderr. */
232 dump_varpool (stderr
);
235 /* Given an assembler name, lookup node. */
236 struct varpool_node
*
237 varpool_node_for_asm (tree asmname
)
239 if (symtab_node
*node
= symtab_node_for_asm (asmname
))
240 return dyn_cast
<varpool_node
> (node
);
245 /* Return if DECL is constant and its initial value is known (so we can do
246 constant folding using DECL_INITIAL (decl)).
247 Return ERROR_MARK_NODE when value is unknown. */
250 ctor_for_folding (tree decl
)
252 struct varpool_node
*node
, *real_node
;
255 if (TREE_CODE (decl
) != VAR_DECL
256 && TREE_CODE (decl
) != CONST_DECL
)
257 return error_mark_node
;
259 if (TREE_CODE (decl
) == CONST_DECL
260 || DECL_IN_CONSTANT_POOL (decl
))
261 return DECL_INITIAL (decl
);
263 if (TREE_THIS_VOLATILE (decl
))
264 return error_mark_node
;
266 /* Do not care about automatic variables. Those are never initialized
267 anyway, because gimplifier exapnds the code*/
268 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
270 gcc_assert (!TREE_PUBLIC (decl
));
271 return error_mark_node
;
274 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
276 node
= varpool_get_node (decl
);
279 real_node
= varpool_variable_node (node
);
280 real_decl
= real_node
->decl
;
285 /* See if we are dealing with alias.
286 In most cases alias is just alternative symbol pointing to a given
287 constructor. This allows us to use interposition rules of DECL
288 constructor of REAL_NODE. However weakrefs are special by being just
289 alternative name of their target (if defined). */
290 if (decl
!= real_decl
)
292 gcc_assert (!DECL_INITIAL (decl
)
293 || DECL_INITIAL (decl
) == error_mark_node
);
294 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
)))
296 node
= varpool_alias_target (node
);
301 /* Vtables are defined by their types and must match no matter of interposition
303 if (DECL_VIRTUAL_P (real_decl
))
305 gcc_checking_assert (TREE_READONLY (real_decl
));
306 return DECL_INITIAL (real_decl
);
309 /* If there is no constructor, we have nothing to do. */
310 if (DECL_INITIAL (real_decl
) == error_mark_node
)
311 return error_mark_node
;
313 /* Non-readonly alias of readonly variable is also de-facto readonly,
314 because the variable itself is in readonly section.
315 We also honnor READONLY flag on alias assuming that user knows
317 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_decl
))
318 return error_mark_node
;
320 /* Variables declared 'const' without an initializer
321 have zero as the initializer if they may not be
322 overridden at link or run time. */
323 if (!DECL_INITIAL (real_decl
)
324 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
325 return error_mark_node
;
327 /* Variables declared `const' with an initializer are considered
328 to not be overwritable with different initializer by default.
330 ??? Previously we behaved so for scalar variables but not for array
332 return DECL_INITIAL (real_decl
);
335 /* Add the variable DECL to the varpool.
336 Unlike varpool_finalize_decl function is intended to be used
337 by middle end and allows insertion of new variable at arbitrary point
340 varpool_add_new_variable (tree decl
)
342 struct varpool_node
*node
;
343 varpool_finalize_decl (decl
);
344 node
= varpool_node_for_decl (decl
);
345 varpool_call_variable_insertion_hooks (node
);
346 if (varpool_externally_visible_p (node
))
347 node
->externally_visible
= true;
350 /* Return variable availability. See cgraph.h for description of individual
353 cgraph_variable_initializer_availability (struct varpool_node
*node
)
355 gcc_assert (cgraph_function_flags_ready
);
356 if (!node
->definition
)
357 return AVAIL_NOT_AVAILABLE
;
358 if (!TREE_PUBLIC (node
->decl
))
359 return AVAIL_AVAILABLE
;
360 if (DECL_IN_CONSTANT_POOL (node
->decl
)
361 || DECL_VIRTUAL_P (node
->decl
))
362 return AVAIL_AVAILABLE
;
363 if (node
->alias
&& node
->weakref
)
365 enum availability avail
;
367 cgraph_variable_initializer_availability
368 (varpool_variable_node (node
, &avail
));
371 /* If the variable can be overwritten, return OVERWRITABLE. Takes
372 care of at least one notable extension - the COMDAT variables
373 used to share template instantiations in C++. */
374 if (decl_replaceable_p (node
->decl
)
375 || DECL_EXTERNAL (node
->decl
))
376 return AVAIL_OVERWRITABLE
;
377 return AVAIL_AVAILABLE
;
381 varpool_analyze_node (struct varpool_node
*node
)
383 tree decl
= node
->decl
;
385 /* When reading back varpool at LTO time, we re-construct the queue in order
386 to have "needed" list right by inserting all needed nodes into varpool.
387 We however don't want to re-analyze already analyzed nodes. */
390 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
391 /* Compute the alignment early so function body expanders are
392 already informed about increased alignment. */
393 align_variable (decl
, 0);
397 (node
, varpool_get_node (node
->alias_target
));
398 else if (DECL_INITIAL (decl
))
399 record_references_in_initializer (decl
, node
->analyzed
);
400 node
->analyzed
= true;
403 /* Assemble thunks and aliases associated to NODE. */
406 assemble_aliases (struct varpool_node
*node
)
410 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
411 if (ref
->use
== IPA_REF_ALIAS
)
413 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
414 do_assemble_alias (alias
->decl
,
415 DECL_ASSEMBLER_NAME (node
->decl
));
416 assemble_aliases (alias
);
420 /* Output one variable, if necessary. Return whether we output it. */
423 varpool_assemble_decl (struct varpool_node
*node
)
425 tree decl
= node
->decl
;
427 /* Aliases are outout when their target is produced or by
432 /* Constant pool is output from RTL land when the reference
433 survive till this level. */
434 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
437 /* Decls with VALUE_EXPR should not be in the varpool at all. They
438 are not real variables, but just info for debugging and codegen.
439 Unfortunately at the moment emutls is not updating varpool correctly
440 after turning real vars into value_expr vars. */
441 if (DECL_HAS_VALUE_EXPR_P (decl
)
442 && !targetm
.have_tls
)
445 /* Hard register vars do not need to be output. */
446 if (DECL_HARD_REGISTER (decl
))
449 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
450 && TREE_CODE (decl
) == VAR_DECL
451 && !DECL_HAS_VALUE_EXPR_P (decl
));
453 if (!node
->in_other_partition
454 && !DECL_EXTERNAL (decl
))
456 assemble_variable (decl
, 0, 1, 0);
457 gcc_assert (TREE_ASM_WRITTEN (decl
));
458 node
->definition
= true;
459 assemble_aliases (node
);
466 /* Add NODE to queue starting at FIRST.
467 The queue is linked via AUX pointers and terminated by pointer to 1. */
470 enqueue_node (struct varpool_node
*node
, struct varpool_node
**first
)
474 gcc_checking_assert (*first
);
479 /* Optimization of function bodies might've rendered some variables as
480 unnecessary so we want to avoid these from being compiled. Re-do
481 reachability starting from variables that are either externally visible
482 or was referred from the asm output routines. */
485 varpool_remove_unreferenced_decls (void)
487 struct varpool_node
*next
, *node
;
488 struct varpool_node
*first
= (struct varpool_node
*)(void *)1;
495 if (cgraph_dump_file
)
496 fprintf (cgraph_dump_file
, "Trivially needed variables:");
497 FOR_EACH_DEFINED_VARIABLE (node
)
500 && (!varpool_can_remove_if_no_refs (node
)
501 /* We just expanded all function bodies. See if any of
502 them needed the variable. */
503 || DECL_RTL_SET_P (node
->decl
)))
505 enqueue_node (node
, &first
);
506 if (cgraph_dump_file
)
507 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
510 while (first
!= (struct varpool_node
*)(void *)1)
513 first
= (struct varpool_node
*)first
->aux
;
515 if (node
->same_comdat_group
)
518 for (next
= node
->same_comdat_group
;
520 next
= next
->same_comdat_group
)
522 varpool_node
*vnext
= dyn_cast
<varpool_node
> (next
);
523 if (vnext
&& vnext
->analyzed
)
524 enqueue_node (vnext
, &first
);
527 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
); i
++)
529 varpool_node
*vnode
= dyn_cast
<varpool_node
> (ref
->referred
);
531 && (!DECL_EXTERNAL (ref
->referred
->decl
)
534 enqueue_node (vnode
, &first
);
537 if (cgraph_dump_file
)
538 fprintf (cgraph_dump_file
, "\nRemoving variables:");
539 for (node
= varpool_first_defined_variable (); node
; node
= next
)
541 next
= varpool_next_defined_variable (node
);
544 if (cgraph_dump_file
)
545 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
546 varpool_remove_node (node
);
549 if (cgraph_dump_file
)
550 fprintf (cgraph_dump_file
, "\n");
553 /* For variables in named sections make sure get_variable_section
554 is called before we switch to those sections. Then section
555 conflicts between read-only and read-only requiring relocations
556 sections can be resolved. */
558 varpool_finalize_named_section_flags (struct varpool_node
*node
)
560 if (!TREE_ASM_WRITTEN (node
->decl
)
562 && !node
->in_other_partition
563 && !DECL_EXTERNAL (node
->decl
)
564 && TREE_CODE (node
->decl
) == VAR_DECL
565 && !DECL_HAS_VALUE_EXPR_P (node
->decl
)
566 && DECL_SECTION_NAME (node
->decl
))
567 get_variable_section (node
->decl
, false);
570 /* Output all variables enqueued to be assembled. */
572 varpool_output_variables (void)
574 bool changed
= false;
575 struct varpool_node
*node
;
580 varpool_remove_unreferenced_decls ();
582 timevar_push (TV_VAROUT
);
584 FOR_EACH_DEFINED_VARIABLE (node
)
585 varpool_finalize_named_section_flags (node
);
587 FOR_EACH_DEFINED_VARIABLE (node
)
588 if (varpool_assemble_decl (node
))
590 timevar_pop (TV_VAROUT
);
594 /* Create a new global variable of type TYPE. */
596 add_new_static_var (tree type
)
599 struct varpool_node
*new_node
;
601 new_decl
= create_tmp_var_raw (type
, NULL
);
602 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
603 TREE_READONLY (new_decl
) = 0;
604 TREE_STATIC (new_decl
) = 1;
605 TREE_USED (new_decl
) = 1;
606 DECL_CONTEXT (new_decl
) = NULL_TREE
;
607 DECL_ABSTRACT (new_decl
) = 0;
608 lang_hooks
.dup_lang_specific_decl (new_decl
);
609 new_node
= varpool_node_for_decl (new_decl
);
610 varpool_finalize_decl (new_decl
);
612 return new_node
->decl
;
615 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
616 Extra name aliases are output whenever DECL is output. */
618 struct varpool_node
*
619 varpool_create_variable_alias (tree alias
, tree decl
)
621 struct varpool_node
*alias_node
;
623 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
624 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
625 alias_node
= varpool_node_for_decl (alias
);
626 alias_node
->alias
= true;
627 alias_node
->definition
= true;
628 alias_node
->alias_target
= decl
;
629 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
630 alias_node
->weakref
= true;
634 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
635 Extra name aliases are output whenever DECL is output. */
637 struct varpool_node
*
638 varpool_extra_name_alias (tree alias
, tree decl
)
640 struct varpool_node
*alias_node
;
642 #ifndef ASM_OUTPUT_DEF
643 /* If aliases aren't supported by the assembler, fail. */
646 alias_node
= varpool_create_variable_alias (alias
, decl
);
647 alias_node
->cpp_implicit_alias
= true;
649 /* Extra name alias mechanizm creates aliases really late
650 via DECL_ASSEMBLER_NAME mechanizm.
651 This is unfortunate because they are not going through the
652 standard channels. Ensure they get output. */
653 if (cpp_implicit_aliases_done
)
654 symtab_resolve_alias (alias_node
,
655 varpool_node_for_decl (decl
));
659 /* Call calback on NODE and aliases associated to NODE.
660 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
664 varpool_for_node_and_aliases (struct varpool_node
*node
,
665 bool (*callback
) (struct varpool_node
*, void *),
667 bool include_overwritable
)
672 if (callback (node
, data
))
674 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
675 if (ref
->use
== IPA_REF_ALIAS
)
677 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
678 if (include_overwritable
679 || cgraph_variable_initializer_availability (alias
) > AVAIL_OVERWRITABLE
)
680 if (varpool_for_node_and_aliases (alias
, callback
, data
,
681 include_overwritable
))