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"
38 #include "tree-ssa-alias.h"
40 #include "lto-streamer.h"
42 const char * const tls_model_names
[]={"none", "tls-emulated", "tls-real",
43 "tls-global-dynamic", "tls-local-dynamic",
44 "tls-initial-exec", "tls-local-exec"};
46 /* List of hooks triggered on varpool_node events. */
47 struct varpool_node_hook_list
{
48 varpool_node_hook hook
;
50 struct varpool_node_hook_list
*next
;
53 /* List of hooks triggered when a node is removed. */
54 struct varpool_node_hook_list
*first_varpool_node_removal_hook
;
55 /* List of hooks triggered when an variable is inserted. */
56 struct varpool_node_hook_list
*first_varpool_variable_insertion_hook
;
58 /* Register HOOK to be called with DATA on each removed node. */
59 struct varpool_node_hook_list
*
60 varpool_add_node_removal_hook (varpool_node_hook hook
, void *data
)
62 struct varpool_node_hook_list
*entry
;
63 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
65 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
75 /* Remove ENTRY from the list of hooks called on removing nodes. */
77 varpool_remove_node_removal_hook (struct varpool_node_hook_list
*entry
)
79 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
87 /* Call all node removal hooks. */
89 varpool_call_node_removal_hooks (varpool_node
*node
)
91 struct varpool_node_hook_list
*entry
= first_varpool_node_removal_hook
;
94 entry
->hook (node
, entry
->data
);
99 /* Register HOOK to be called with DATA on each inserted node. */
100 struct varpool_node_hook_list
*
101 varpool_add_variable_insertion_hook (varpool_node_hook hook
, void *data
)
103 struct varpool_node_hook_list
*entry
;
104 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
106 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
116 /* Remove ENTRY from the list of hooks called on inserted nodes. */
118 varpool_remove_variable_insertion_hook (struct varpool_node_hook_list
*entry
)
120 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
122 while (*ptr
!= entry
)
128 /* Call all node insertion hooks. */
130 varpool_call_variable_insertion_hooks (varpool_node
*node
)
132 struct varpool_node_hook_list
*entry
= first_varpool_variable_insertion_hook
;
135 entry
->hook (node
, entry
->data
);
140 /* Allocate new callgraph node and insert it into basic data structures. */
143 varpool_create_empty_node (void)
145 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
146 node
->type
= SYMTAB_VARIABLE
;
150 /* Return varpool node assigned to DECL. Create new one when needed. */
152 varpool_node_for_decl (tree decl
)
154 varpool_node
*node
= varpool_get_node (decl
);
155 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
159 node
= varpool_create_empty_node ();
161 symtab_register_node (node
);
165 /* Remove node from the varpool. */
167 varpool_remove_node (varpool_node
*node
)
169 varpool_call_node_removal_hooks (node
);
170 symtab_unregister_node (node
);
172 /* When streaming we can have multiple nodes associated with decl. */
173 if (cgraph_state
== CGRAPH_LTO_STREAMING
)
175 /* Keep constructor when it may be used for folding. We remove
176 references to external variables before final compilation. */
177 else if (DECL_INITIAL (node
->decl
) && DECL_INITIAL (node
->decl
) != error_mark_node
178 && !varpool_ctor_useable_for_folding_p (node
))
179 varpool_remove_initializer (node
);
183 /* Renove node initializer when it is no longer needed. */
185 varpool_remove_initializer (varpool_node
*node
)
187 if (DECL_INITIAL (node
->decl
)
188 && !DECL_IN_CONSTANT_POOL (node
->decl
)
189 /* Keep vtables for BINFO folding. */
190 && !DECL_VIRTUAL_P (node
->decl
)
191 /* FIXME: http://gcc.gnu.org/PR55395 */
192 && debug_info_level
== DINFO_LEVEL_NONE
193 /* When doing declaration merging we have duplicate
194 entries for given decl. Do not attempt to remove
195 the boides, or we will end up remiving
197 && cgraph_state
!= CGRAPH_LTO_STREAMING
)
198 DECL_INITIAL (node
->decl
) = error_mark_node
;
201 /* Dump given cgraph node. */
203 dump_varpool_node (FILE *f
, varpool_node
*node
)
205 dump_symtab_base (f
, node
);
206 fprintf (f
, " Availability: %s\n",
207 cgraph_function_flags_ready
208 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
210 fprintf (f
, " Varpool flags:");
211 if (DECL_INITIAL (node
->decl
))
212 fprintf (f
, " initialized");
214 fprintf (f
, " output");
215 if (node
->used_by_single_function
)
216 fprintf (f
, " used-by-single-function");
217 if (TREE_READONLY (node
->decl
))
218 fprintf (f
, " read-only");
219 if (varpool_ctor_useable_for_folding_p (node
))
220 fprintf (f
, " const-value-known");
222 fprintf (f
, " write-only");
224 fprintf (f
, " %s", tls_model_names
[node
->tls_model
]);
228 /* Dump the variable pool. */
230 dump_varpool (FILE *f
)
234 fprintf (f
, "variable pool:\n\n");
235 FOR_EACH_VARIABLE (node
)
236 dump_varpool_node (f
, node
);
239 /* Dump the variable pool to stderr. */
244 dump_varpool (stderr
);
247 /* Given an assembler name, lookup node. */
249 varpool_node_for_asm (tree asmname
)
251 if (symtab_node
*node
= symtab_node_for_asm (asmname
))
252 return dyn_cast
<varpool_node
*> (node
);
257 /* When doing LTO, read NODE's constructor from disk if it is not already present. */
260 varpool_get_constructor (struct varpool_node
*node
)
262 struct lto_file_decl_data
*file_data
;
263 const char *data
, *name
;
265 tree decl
= node
->decl
;
267 if (DECL_INITIAL (node
->decl
) != error_mark_node
269 return DECL_INITIAL (node
->decl
);
271 timevar_push (TV_IPA_LTO_CTORS_IN
);
273 file_data
= node
->lto_file_data
;
274 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
276 /* We may have renamed the declaration, e.g., a static function. */
277 name
= lto_get_decl_name_mapping (file_data
, name
);
279 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
282 fatal_error ("%s: section %s is missing",
283 file_data
->file_name
,
286 lto_input_variable_constructor (file_data
, node
, data
);
287 lto_stats
.num_function_bodies
++;
288 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
290 lto_free_function_in_decl_state_for_node (node
);
291 timevar_pop (TV_IPA_LTO_CTORS_IN
);
292 return DECL_INITIAL (node
->decl
);
295 /* Return ture if NODE has constructor that can be used for folding. */
298 varpool_ctor_useable_for_folding_p (varpool_node
*node
)
300 varpool_node
*real_node
= node
;
302 if (real_node
->alias
&& real_node
->definition
)
303 real_node
= varpool_variable_node (node
);
305 if (TREE_CODE (node
->decl
) == CONST_DECL
306 || DECL_IN_CONSTANT_POOL (node
->decl
))
308 if (TREE_THIS_VOLATILE (node
->decl
))
311 /* If we do not have a constructor, we can't use it. */
312 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
313 && !real_node
->lto_file_data
)
316 /* Vtables are defined by their types and must match no matter of interposition
318 if (DECL_VIRTUAL_P (node
->decl
))
320 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
321 classes not defined in the current TU so that it can refer
322 to them from typeinfo objects. Avoid returning NULL_TREE. */
323 return DECL_INITIAL (real_node
->decl
) != NULL
;
326 /* Alias of readonly variable is also readonly, since the variable is stored
327 in readonly memory. We also accept readonly aliases of non-readonly
328 locations assuming that user knows what he is asking for. */
329 if (!TREE_READONLY (node
->decl
) && !TREE_READONLY (real_node
->decl
))
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_node
->decl
)
336 && (DECL_EXTERNAL (node
->decl
) || decl_replaceable_p (node
->decl
)))
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
347 /* If DECL is constant variable and its initial value is known (so we can
348 do constant folding), return its constructor (DECL_INITIAL). This may
349 be an expression or NULL when DECL is initialized to 0.
350 Return ERROR_MARK_NODE otherwise.
352 In LTO this may actually trigger reading the constructor from disk.
353 For this reason varpool_ctor_useable_for_folding_p should be used when
354 the actual constructor value is not needed. */
357 ctor_for_folding (tree decl
)
359 varpool_node
*node
, *real_node
;
362 if (TREE_CODE (decl
) != VAR_DECL
363 && TREE_CODE (decl
) != CONST_DECL
)
364 return error_mark_node
;
366 if (TREE_CODE (decl
) == CONST_DECL
367 || DECL_IN_CONSTANT_POOL (decl
))
368 return DECL_INITIAL (decl
);
370 if (TREE_THIS_VOLATILE (decl
))
371 return error_mark_node
;
373 /* Do not care about automatic variables. Those are never initialized
374 anyway, because gimplifier exapnds the code. */
375 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
377 gcc_assert (!TREE_PUBLIC (decl
));
378 return error_mark_node
;
381 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
383 real_node
= node
= varpool_get_node (decl
);
386 real_node
= varpool_variable_node (node
);
387 real_decl
= real_node
->decl
;
392 /* See if we are dealing with alias.
393 In most cases alias is just alternative symbol pointing to a given
394 constructor. This allows us to use interposition rules of DECL
395 constructor of REAL_NODE. However weakrefs are special by being just
396 alternative name of their target (if defined). */
397 if (decl
!= real_decl
)
399 gcc_assert (!DECL_INITIAL (decl
)
400 || DECL_INITIAL (decl
) == error_mark_node
);
403 node
= varpool_alias_target (node
);
408 if ((!DECL_VIRTUAL_P (real_decl
)
409 || DECL_INITIAL (real_decl
) == error_mark_node
410 || !DECL_INITIAL (real_decl
))
411 && (!node
|| !varpool_ctor_useable_for_folding_p (node
)))
412 return error_mark_node
;
414 /* OK, we can return constructor. See if we need to fetch it from disk
416 if (DECL_INITIAL (real_decl
) != error_mark_node
418 return DECL_INITIAL (real_decl
);
419 return varpool_get_constructor (real_node
);
422 /* Add the variable DECL to the varpool.
423 Unlike varpool_finalize_decl function is intended to be used
424 by middle end and allows insertion of new variable at arbitrary point
427 varpool_add_new_variable (tree decl
)
430 varpool_finalize_decl (decl
);
431 node
= varpool_node_for_decl (decl
);
432 varpool_call_variable_insertion_hooks (node
);
433 if (varpool_externally_visible_p (node
))
434 node
->externally_visible
= true;
437 /* Return variable availability. See cgraph.h for description of individual
440 cgraph_variable_initializer_availability (varpool_node
*node
)
442 if (!node
->definition
)
443 return AVAIL_NOT_AVAILABLE
;
444 if (!TREE_PUBLIC (node
->decl
))
445 return AVAIL_AVAILABLE
;
446 if (DECL_IN_CONSTANT_POOL (node
->decl
)
447 || DECL_VIRTUAL_P (node
->decl
))
448 return AVAIL_AVAILABLE
;
449 if (node
->alias
&& node
->weakref
)
451 enum availability avail
;
453 cgraph_variable_initializer_availability
454 (varpool_variable_node (node
, &avail
));
457 /* If the variable can be overwritten, return OVERWRITABLE. Takes
458 care of at least one notable extension - the COMDAT variables
459 used to share template instantiations in C++. */
460 if (decl_replaceable_p (node
->decl
)
461 || DECL_EXTERNAL (node
->decl
))
462 return AVAIL_OVERWRITABLE
;
463 return AVAIL_AVAILABLE
;
467 varpool_analyze_node (varpool_node
*node
)
469 tree decl
= node
->decl
;
471 /* When reading back varpool at LTO time, we re-construct the queue in order
472 to have "needed" list right by inserting all needed nodes into varpool.
473 We however don't want to re-analyze already analyzed nodes. */
476 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
477 /* Compute the alignment early so function body expanders are
478 already informed about increased alignment. */
479 align_variable (decl
, 0);
483 (node
, varpool_get_node (node
->alias_target
));
484 else if (DECL_INITIAL (decl
))
485 record_references_in_initializer (decl
, node
->analyzed
);
486 node
->analyzed
= true;
489 /* Assemble thunks and aliases associated to NODE. */
492 assemble_aliases (varpool_node
*node
)
496 FOR_EACH_ALIAS (node
, ref
)
498 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
499 do_assemble_alias (alias
->decl
,
500 DECL_ASSEMBLER_NAME (node
->decl
));
501 assemble_aliases (alias
);
505 /* Output one variable, if necessary. Return whether we output it. */
508 varpool_assemble_decl (varpool_node
*node
)
510 tree decl
= node
->decl
;
512 /* Aliases are outout when their target is produced or by
517 /* Constant pool is output from RTL land when the reference
518 survive till this level. */
519 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
522 /* Decls with VALUE_EXPR should not be in the varpool at all. They
523 are not real variables, but just info for debugging and codegen.
524 Unfortunately at the moment emutls is not updating varpool correctly
525 after turning real vars into value_expr vars. */
526 if (DECL_HAS_VALUE_EXPR_P (decl
)
527 && !targetm
.have_tls
)
530 /* Hard register vars do not need to be output. */
531 if (DECL_HARD_REGISTER (decl
))
534 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
535 && TREE_CODE (decl
) == VAR_DECL
536 && !DECL_HAS_VALUE_EXPR_P (decl
));
538 if (!node
->in_other_partition
539 && !DECL_EXTERNAL (decl
))
541 varpool_get_constructor (node
);
542 assemble_variable (decl
, 0, 1, 0);
543 gcc_assert (TREE_ASM_WRITTEN (decl
));
544 gcc_assert (node
->definition
);
545 assemble_aliases (node
);
552 /* Add NODE to queue starting at FIRST.
553 The queue is linked via AUX pointers and terminated by pointer to 1. */
556 enqueue_node (varpool_node
*node
, varpool_node
**first
)
560 gcc_checking_assert (*first
);
565 /* Optimization of function bodies might've rendered some variables as
566 unnecessary so we want to avoid these from being compiled. Re-do
567 reachability starting from variables that are either externally visible
568 or was referred from the asm output routines. */
571 varpool_remove_unreferenced_decls (void)
573 varpool_node
*next
, *node
;
574 varpool_node
*first
= (varpool_node
*)(void *)1;
576 struct ipa_ref
*ref
= NULL
;
577 struct pointer_set_t
*referenced
= pointer_set_create ();
582 if (cgraph_dump_file
)
583 fprintf (cgraph_dump_file
, "Trivially needed variables:");
584 FOR_EACH_DEFINED_VARIABLE (node
)
587 && (!varpool_can_remove_if_no_refs (node
)
588 /* We just expanded all function bodies. See if any of
589 them needed the variable. */
590 || DECL_RTL_SET_P (node
->decl
)))
592 enqueue_node (node
, &first
);
593 if (cgraph_dump_file
)
594 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
597 while (first
!= (varpool_node
*)(void *)1)
600 first
= (varpool_node
*)first
->aux
;
602 if (node
->same_comdat_group
)
605 for (next
= node
->same_comdat_group
;
607 next
= next
->same_comdat_group
)
609 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
610 if (vnext
&& vnext
->analyzed
&& !symtab_comdat_local_p (next
))
611 enqueue_node (vnext
, &first
);
614 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
616 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
618 && !vnode
->in_other_partition
619 && (!DECL_EXTERNAL (ref
->referred
->decl
)
622 enqueue_node (vnode
, &first
);
624 pointer_set_insert (referenced
, node
);
627 if (cgraph_dump_file
)
628 fprintf (cgraph_dump_file
, "\nRemoving variables:");
629 for (node
= varpool_first_defined_variable (); node
; node
= next
)
631 next
= varpool_next_defined_variable (node
);
634 if (cgraph_dump_file
)
635 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
636 if (pointer_set_contains (referenced
, node
))
637 varpool_remove_initializer (node
);
639 varpool_remove_node (node
);
642 pointer_set_destroy (referenced
);
643 if (cgraph_dump_file
)
644 fprintf (cgraph_dump_file
, "\n");
647 /* For variables in named sections make sure get_variable_section
648 is called before we switch to those sections. Then section
649 conflicts between read-only and read-only requiring relocations
650 sections can be resolved. */
652 varpool_finalize_named_section_flags (varpool_node
*node
)
654 if (!TREE_ASM_WRITTEN (node
->decl
)
656 && !node
->in_other_partition
657 && !DECL_EXTERNAL (node
->decl
)
658 && TREE_CODE (node
->decl
) == VAR_DECL
659 && !DECL_HAS_VALUE_EXPR_P (node
->decl
)
660 && node
->get_section ())
661 get_variable_section (node
->decl
, false);
664 /* Output all variables enqueued to be assembled. */
666 varpool_output_variables (void)
668 bool changed
= false;
674 varpool_remove_unreferenced_decls ();
676 timevar_push (TV_VAROUT
);
678 FOR_EACH_DEFINED_VARIABLE (node
)
679 varpool_finalize_named_section_flags (node
);
681 FOR_EACH_DEFINED_VARIABLE (node
)
682 if (varpool_assemble_decl (node
))
684 timevar_pop (TV_VAROUT
);
688 /* Create a new global variable of type TYPE. */
690 add_new_static_var (tree type
)
693 varpool_node
*new_node
;
695 new_decl
= create_tmp_var_raw (type
, NULL
);
696 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
697 TREE_READONLY (new_decl
) = 0;
698 TREE_STATIC (new_decl
) = 1;
699 TREE_USED (new_decl
) = 1;
700 DECL_CONTEXT (new_decl
) = NULL_TREE
;
701 DECL_ABSTRACT (new_decl
) = 0;
702 lang_hooks
.dup_lang_specific_decl (new_decl
);
703 new_node
= varpool_node_for_decl (new_decl
);
704 varpool_finalize_decl (new_decl
);
706 return new_node
->decl
;
709 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
710 Extra name aliases are output whenever DECL is output. */
713 varpool_create_variable_alias (tree alias
, tree decl
)
715 varpool_node
*alias_node
;
717 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
718 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
719 alias_node
= varpool_node_for_decl (alias
);
720 alias_node
->alias
= true;
721 alias_node
->definition
= true;
722 alias_node
->alias_target
= decl
;
723 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
724 alias_node
->weakref
= true;
728 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
729 Extra name aliases are output whenever DECL is output. */
732 varpool_extra_name_alias (tree alias
, tree decl
)
734 varpool_node
*alias_node
;
736 #ifndef ASM_OUTPUT_DEF
737 /* If aliases aren't supported by the assembler, fail. */
740 alias_node
= varpool_create_variable_alias (alias
, decl
);
741 alias_node
->cpp_implicit_alias
= true;
743 /* Extra name alias mechanizm creates aliases really late
744 via DECL_ASSEMBLER_NAME mechanizm.
745 This is unfortunate because they are not going through the
746 standard channels. Ensure they get output. */
747 if (cpp_implicit_aliases_done
)
748 symtab_resolve_alias (alias_node
,
749 varpool_node_for_decl (decl
));
753 /* Call calback on NODE and aliases associated to NODE.
754 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
758 varpool_for_node_and_aliases (varpool_node
*node
,
759 bool (*callback
) (varpool_node
*, void *),
761 bool include_overwritable
)
765 if (callback (node
, data
))
768 FOR_EACH_ALIAS (node
, ref
)
770 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
771 if (include_overwritable
772 || cgraph_variable_initializer_availability (alias
) > AVAIL_OVERWRITABLE
)
773 if (varpool_for_node_and_aliases (alias
, callback
, data
,
774 include_overwritable
))