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_node::create_empty (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::get_create (tree decl
)
154 varpool_node
*node
= varpool_node::get (decl
);
155 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
159 node
= varpool_node::create_empty ();
161 node
->register_symbol ();
165 /* Remove variable from symbol table. */
168 varpool_node::remove (void)
170 varpool_call_node_removal_hooks (this);
173 /* When streaming we can have multiple nodes associated with decl. */
174 if (cgraph_state
== CGRAPH_LTO_STREAMING
)
176 /* Keep constructor when it may be used for folding. We remove
177 references to external variables before final compilation. */
178 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
179 && !ctor_useable_for_folding_p ())
180 remove_initializer ();
184 /* Remove node initializer when it is no longer needed. */
186 varpool_node::remove_initializer (void)
188 if (DECL_INITIAL (decl
)
189 && !DECL_IN_CONSTANT_POOL (decl
)
190 /* Keep vtables for BINFO folding. */
191 && !DECL_VIRTUAL_P (decl
)
192 /* FIXME: http://gcc.gnu.org/PR55395 */
193 && debug_info_level
== DINFO_LEVEL_NONE
194 /* When doing declaration merging we have duplicate
195 entries for given decl. Do not attempt to remove
196 the boides, or we will end up remiving
198 && cgraph_state
!= CGRAPH_LTO_STREAMING
)
199 DECL_INITIAL (decl
) = error_mark_node
;
202 /* Dump given varpool node to F. */
204 varpool_node::dump (FILE *f
)
207 fprintf (f
, " Availability: %s\n",
208 cgraph_function_flags_ready
209 ? cgraph_availability_names
[get_availability ()]
211 fprintf (f
, " Varpool flags:");
212 if (DECL_INITIAL (decl
))
213 fprintf (f
, " initialized");
215 fprintf (f
, " output");
216 if (used_by_single_function
)
217 fprintf (f
, " used-by-single-function");
218 if (TREE_READONLY (decl
))
219 fprintf (f
, " read-only");
220 if (ctor_useable_for_folding_p ())
221 fprintf (f
, " const-value-known");
223 fprintf (f
, " write-only");
225 fprintf (f
, " %s", tls_model_names
[tls_model
]);
230 /* Dump given varpool node to stderr. */
231 void varpool_node::debug (void)
233 varpool_node::dump (stderr
);
236 /* Dump the variable pool to F. */
238 varpool_node::dump_varpool (FILE *f
)
242 fprintf (f
, "variable pool:\n\n");
243 FOR_EACH_VARIABLE (node
)
247 /* Dump the variable pool to stderr. */
250 varpool_node::debug_varpool (void)
252 dump_varpool (stderr
);
255 /* Given an assembler name, lookup node. */
257 varpool_node::get_for_asmname (tree asmname
)
259 if (symtab_node
*node
= symtab_node_for_asm (asmname
))
260 return dyn_cast
<varpool_node
*> (node
);
265 /* When doing LTO, read variable's constructor from disk if
266 it is not already present. */
269 varpool_node::get_constructor (void)
271 struct lto_file_decl_data
*file_data
;
272 const char *data
, *name
;
275 if (DECL_INITIAL (decl
) != error_mark_node
277 return DECL_INITIAL (decl
);
279 timevar_push (TV_IPA_LTO_CTORS_IN
);
281 file_data
= lto_file_data
;
282 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
284 /* We may have renamed the declaration, e.g., a static function. */
285 name
= lto_get_decl_name_mapping (file_data
, name
);
287 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
290 fatal_error ("%s: section %s is missing",
291 file_data
->file_name
,
294 lto_input_variable_constructor (file_data
, this, data
);
295 lto_stats
.num_function_bodies
++;
296 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
298 lto_free_function_in_decl_state_for_node (this);
299 timevar_pop (TV_IPA_LTO_CTORS_IN
);
300 return DECL_INITIAL (decl
);
303 /* Return true if variable has constructor that can be used for folding. */
306 varpool_node::ctor_useable_for_folding_p (void)
308 varpool_node
*real_node
= this;
310 if (real_node
->alias
&& real_node
->definition
)
311 real_node
= ultimate_alias_target ();
313 if (TREE_CODE (decl
) == CONST_DECL
314 || DECL_IN_CONSTANT_POOL (decl
))
316 if (TREE_THIS_VOLATILE (decl
))
319 /* If we do not have a constructor, we can't use it. */
320 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
321 && !real_node
->lto_file_data
)
324 /* Vtables are defined by their types and must match no matter of interposition
326 if (DECL_VIRTUAL_P (decl
))
328 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
329 classes not defined in the current TU so that it can refer
330 to them from typeinfo objects. Avoid returning NULL_TREE. */
331 return DECL_INITIAL (real_node
->decl
) != NULL
;
334 /* Alias of readonly variable is also readonly, since the variable is stored
335 in readonly memory. We also accept readonly aliases of non-readonly
336 locations assuming that user knows what he is asking for. */
337 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
340 /* Variables declared 'const' without an initializer
341 have zero as the initializer if they may not be
342 overridden at link or run time. */
343 if (!DECL_INITIAL (real_node
->decl
)
344 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
347 /* Variables declared `const' with an initializer are considered
348 to not be overwritable with different initializer by default.
350 ??? Previously we behaved so for scalar variables but not for array
355 /* If DECLARATION is constant variable and its initial value is known
356 (so we can do constant folding), return its constructor (DECL_INITIAL).
357 This may be an expression or NULL when DECL is initialized to 0.
358 Return ERROR_MARK_NODE otherwise.
360 In LTO this may actually trigger reading the constructor from disk.
361 For this reason varpool_ctor_useable_for_folding_p should be used when
362 the actual constructor value is not needed. */
365 ctor_for_folding (tree decl
)
367 varpool_node
*node
, *real_node
;
370 if (TREE_CODE (decl
) != VAR_DECL
371 && TREE_CODE (decl
) != CONST_DECL
)
372 return error_mark_node
;
374 if (TREE_CODE (decl
) == CONST_DECL
375 || DECL_IN_CONSTANT_POOL (decl
))
376 return DECL_INITIAL (decl
);
378 if (TREE_THIS_VOLATILE (decl
))
379 return error_mark_node
;
381 /* Do not care about automatic variables. Those are never initialized
382 anyway, because gimplifier exapnds the code. */
383 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
385 gcc_assert (!TREE_PUBLIC (decl
));
386 return error_mark_node
;
389 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
391 real_node
= node
= varpool_node::get (decl
);
394 real_node
= node
->ultimate_alias_target ();
395 real_decl
= real_node
->decl
;
400 /* See if we are dealing with alias.
401 In most cases alias is just alternative symbol pointing to a given
402 constructor. This allows us to use interposition rules of DECL
403 constructor of REAL_NODE. However weakrefs are special by being just
404 alternative name of their target (if defined). */
405 if (decl
!= real_decl
)
407 gcc_assert (!DECL_INITIAL (decl
)
408 || DECL_INITIAL (decl
) == error_mark_node
);
411 node
= node
->get_alias_target ();
416 if ((!DECL_VIRTUAL_P (real_decl
)
417 || DECL_INITIAL (real_decl
) == error_mark_node
418 || !DECL_INITIAL (real_decl
))
419 && (!node
|| !node
->ctor_useable_for_folding_p ()))
420 return error_mark_node
;
422 /* OK, we can return constructor. See if we need to fetch it from disk
424 if (DECL_INITIAL (real_decl
) != error_mark_node
426 return DECL_INITIAL (real_decl
);
427 return real_node
->get_constructor ();
430 /* Add the variable DECL to the varpool.
431 Unlike varpool_finalize_decl function is intended to be used
432 by middle end and allows insertion of new variable at arbitrary point
435 varpool_add_new_variable (tree decl
)
438 varpool_node::finalize_decl (decl
);
439 node
= varpool_node::get_create (decl
);
440 varpool_call_variable_insertion_hooks (node
);
441 if (node
->externally_visible_p ())
442 node
->externally_visible
= true;
445 /* Return variable availability. See cgraph.h for description of individual
448 varpool_node::get_availability (void)
451 return AVAIL_NOT_AVAILABLE
;
452 if (!TREE_PUBLIC (decl
))
453 return AVAIL_AVAILABLE
;
454 if (DECL_IN_CONSTANT_POOL (decl
)
455 || DECL_VIRTUAL_P (decl
))
456 return AVAIL_AVAILABLE
;
457 if (alias
&& weakref
)
459 enum availability avail
;
461 ultimate_alias_target (&avail
)->get_availability ();
464 /* If the variable can be overwritten, return OVERWRITABLE. Takes
465 care of at least one notable extension - the COMDAT variables
466 used to share template instantiations in C++. */
467 if (decl_replaceable_p (decl
)
468 || DECL_EXTERNAL (decl
))
469 return AVAIL_INTERPOSABLE
;
470 return AVAIL_AVAILABLE
;
474 varpool_node::analyze (void)
476 /* When reading back varpool at LTO time, we re-construct the queue in order
477 to have "needed" list right by inserting all needed nodes into varpool.
478 We however don't want to re-analyze already analyzed nodes. */
481 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
482 /* Compute the alignment early so function body expanders are
483 already informed about increased alignment. */
484 align_variable (decl
, 0);
487 resolve_alias (varpool_node::get (alias_target
));
488 else if (DECL_INITIAL (decl
))
489 record_references_in_initializer (decl
, analyzed
);
493 /* Assemble thunks and aliases associated to varpool node. */
496 varpool_node::assemble_aliases (void)
500 FOR_EACH_ALIAS (this, ref
)
502 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
503 do_assemble_alias (alias
->decl
,
504 DECL_ASSEMBLER_NAME (decl
));
505 alias
->assemble_aliases ();
509 /* Output one variable, if necessary. Return whether we output it. */
512 varpool_node::assemble_decl (void)
514 /* Aliases are outout when their target is produced or by
519 /* Constant pool is output from RTL land when the reference
520 survive till this level. */
521 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
524 /* Decls with VALUE_EXPR should not be in the varpool at all. They
525 are not real variables, but just info for debugging and codegen.
526 Unfortunately at the moment emutls is not updating varpool correctly
527 after turning real vars into value_expr vars. */
528 if (DECL_HAS_VALUE_EXPR_P (decl
)
529 && !targetm
.have_tls
)
532 /* Hard register vars do not need to be output. */
533 if (DECL_HARD_REGISTER (decl
))
536 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
537 && TREE_CODE (decl
) == VAR_DECL
538 && !DECL_HAS_VALUE_EXPR_P (decl
));
540 if (!in_other_partition
541 && !DECL_EXTERNAL (decl
))
544 assemble_variable (decl
, 0, 1, 0);
545 gcc_assert (TREE_ASM_WRITTEN (decl
));
546 gcc_assert (definition
);
554 /* Add NODE to queue starting at FIRST.
555 The queue is linked via AUX pointers and terminated by pointer to 1. */
558 enqueue_node (varpool_node
*node
, varpool_node
**first
)
562 gcc_checking_assert (*first
);
567 /* Optimization of function bodies might've rendered some variables as
568 unnecessary so we want to avoid these from being compiled. Re-do
569 reachability starting from variables that are either externally visible
570 or was referred from the asm output routines. */
573 varpool_remove_unreferenced_decls (void)
575 varpool_node
*next
, *node
;
576 varpool_node
*first
= (varpool_node
*)(void *)1;
578 struct ipa_ref
*ref
= NULL
;
579 struct pointer_set_t
*referenced
= pointer_set_create ();
584 if (cgraph_dump_file
)
585 fprintf (cgraph_dump_file
, "Trivially needed variables:");
586 FOR_EACH_DEFINED_VARIABLE (node
)
589 && (!node
->can_remove_if_no_refs_p ()
590 /* We just expanded all function bodies. See if any of
591 them needed the variable. */
592 || DECL_RTL_SET_P (node
->decl
)))
594 enqueue_node (node
, &first
);
595 if (cgraph_dump_file
)
596 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
599 while (first
!= (varpool_node
*)(void *)1)
602 first
= (varpool_node
*)first
->aux
;
604 if (node
->same_comdat_group
)
607 for (next
= node
->same_comdat_group
;
609 next
= next
->same_comdat_group
)
611 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
612 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
613 enqueue_node (vnext
, &first
);
616 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
618 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
620 && !vnode
->in_other_partition
621 && (!DECL_EXTERNAL (ref
->referred
->decl
)
624 enqueue_node (vnode
, &first
);
626 pointer_set_insert (referenced
, node
);
629 if (cgraph_dump_file
)
630 fprintf (cgraph_dump_file
, "\nRemoving variables:");
631 for (node
= varpool_first_defined_variable (); node
; node
= next
)
633 next
= varpool_next_defined_variable (node
);
636 if (cgraph_dump_file
)
637 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
638 if (pointer_set_contains (referenced
, node
))
639 node
->remove_initializer ();
644 pointer_set_destroy (referenced
);
645 if (cgraph_dump_file
)
646 fprintf (cgraph_dump_file
, "\n");
649 /* For variables in named sections make sure get_variable_section
650 is called before we switch to those sections. Then section
651 conflicts between read-only and read-only requiring relocations
652 sections can be resolved. */
654 varpool_node::finalize_named_section_flags (void)
656 if (!TREE_ASM_WRITTEN (decl
)
658 && !in_other_partition
659 && !DECL_EXTERNAL (decl
)
660 && TREE_CODE (decl
) == VAR_DECL
661 && !DECL_HAS_VALUE_EXPR_P (decl
)
663 get_variable_section (decl
, false);
666 /* Output all variables enqueued to be assembled. */
668 varpool_node::output_variables (void)
670 bool changed
= false;
676 varpool_remove_unreferenced_decls ();
678 timevar_push (TV_VAROUT
);
680 FOR_EACH_DEFINED_VARIABLE (node
)
681 node
->finalize_named_section_flags ();
683 FOR_EACH_DEFINED_VARIABLE (node
)
684 if (node
->assemble_decl ())
686 timevar_pop (TV_VAROUT
);
690 /* Create a new global variable of type TYPE. */
692 add_new_static_var (tree type
)
695 varpool_node
*new_node
;
697 new_decl
= create_tmp_var_raw (type
, NULL
);
698 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
699 TREE_READONLY (new_decl
) = 0;
700 TREE_STATIC (new_decl
) = 1;
701 TREE_USED (new_decl
) = 1;
702 DECL_CONTEXT (new_decl
) = NULL_TREE
;
703 DECL_ABSTRACT (new_decl
) = 0;
704 lang_hooks
.dup_lang_specific_decl (new_decl
);
705 new_node
= varpool_node::get_create (new_decl
);
706 varpool_node::finalize_decl (new_decl
);
708 return new_node
->decl
;
711 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
712 Extra name aliases are output whenever DECL is output. */
715 varpool_node::create_alias (tree alias
, tree decl
)
717 varpool_node
*alias_node
;
719 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
720 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
721 alias_node
= varpool_node::get_create (alias
);
722 alias_node
->alias
= true;
723 alias_node
->definition
= true;
724 alias_node
->alias_target
= decl
;
725 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
726 alias_node
->weakref
= true;
730 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
731 Extra name aliases are output whenever DECL is output. */
734 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
736 varpool_node
*alias_node
;
738 #ifndef ASM_OUTPUT_DEF
739 /* If aliases aren't supported by the assembler, fail. */
742 alias_node
= varpool_node::create_alias (alias
, decl
);
743 alias_node
->cpp_implicit_alias
= true;
745 /* Extra name alias mechanizm creates aliases really late
746 via DECL_ASSEMBLER_NAME mechanizm.
747 This is unfortunate because they are not going through the
748 standard channels. Ensure they get output. */
749 if (cpp_implicit_aliases_done
)
750 alias_node
->resolve_alias (varpool_node::get_create (decl
));
754 /* Call calback on varpool symbol and aliases associated to varpool symbol.
755 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
759 varpool_node::call_for_node_and_aliases (bool (*callback
) (varpool_node
*,
762 bool include_overwritable
)
766 if (callback (this, data
))
769 FOR_EACH_ALIAS (this, ref
)
771 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
772 if (include_overwritable
773 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
774 if (alias
->call_for_node_and_aliases (callback
, data
,
775 include_overwritable
))