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 "basic-block.h"
31 #include "plugin-api.h"
36 #include "hard-reg-set.h"
41 #include "langhooks.h"
42 #include "diagnostic-core.h"
47 #include "gimple-expr.h"
49 #include "tree-ssa-alias.h"
51 #include "lto-streamer.h"
53 const char * const tls_model_names
[]={"none", "tls-emulated", "tls-real",
54 "tls-global-dynamic", "tls-local-dynamic",
55 "tls-initial-exec", "tls-local-exec"};
57 /* List of hooks triggered on varpool_node events. */
58 struct varpool_node_hook_list
{
59 varpool_node_hook hook
;
61 struct varpool_node_hook_list
*next
;
64 /* Register HOOK to be called with DATA on each removed node. */
65 varpool_node_hook_list
*
66 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
68 varpool_node_hook_list
*entry
;
69 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
71 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
81 /* Remove ENTRY from the list of hooks called on removing nodes. */
83 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
85 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
93 /* Call all node removal hooks. */
95 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
97 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
100 entry
->hook (node
, entry
->data
);
105 /* Register HOOK to be called with DATA on each inserted node. */
106 varpool_node_hook_list
*
107 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
109 varpool_node_hook_list
*entry
;
110 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
112 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
122 /* Remove ENTRY from the list of hooks called on inserted nodes. */
124 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
126 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
128 while (*ptr
!= entry
)
134 /* Call all node insertion hooks. */
136 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
138 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
141 entry
->hook (node
, entry
->data
);
146 /* Allocate new callgraph node and insert it into basic data structures. */
149 varpool_node::create_empty (void)
151 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
152 node
->type
= SYMTAB_VARIABLE
;
156 /* Return varpool node assigned to DECL. Create new one when needed. */
158 varpool_node::get_create (tree decl
)
160 varpool_node
*node
= varpool_node::get (decl
);
161 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
165 node
= varpool_node::create_empty ();
167 node
->register_symbol ();
171 /* Remove variable from symbol table. */
174 varpool_node::remove (void)
176 symtab
->call_varpool_removal_hooks (this);
179 /* When streaming we can have multiple nodes associated with decl. */
180 if (symtab
->state
== LTO_STREAMING
)
182 /* Keep constructor when it may be used for folding. We remove
183 references to external variables before final compilation. */
184 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
185 && !ctor_useable_for_folding_p ())
186 remove_initializer ();
190 /* Remove node initializer when it is no longer needed. */
192 varpool_node::remove_initializer (void)
194 if (DECL_INITIAL (decl
)
195 && !DECL_IN_CONSTANT_POOL (decl
)
196 /* Keep vtables for BINFO folding. */
197 && !DECL_VIRTUAL_P (decl
)
198 /* FIXME: http://gcc.gnu.org/PR55395 */
199 && debug_info_level
== DINFO_LEVEL_NONE
200 /* When doing declaration merging we have duplicate
201 entries for given decl. Do not attempt to remove
202 the boides, or we will end up remiving
204 && symtab
->state
!= LTO_STREAMING
)
205 DECL_INITIAL (decl
) = error_mark_node
;
208 /* Dump given varpool node to F. */
210 varpool_node::dump (FILE *f
)
213 fprintf (f
, " Availability: %s\n",
214 symtab
->function_flags_ready
215 ? cgraph_availability_names
[get_availability ()]
217 fprintf (f
, " Varpool flags:");
218 if (DECL_INITIAL (decl
))
219 fprintf (f
, " initialized");
221 fprintf (f
, " output");
222 if (used_by_single_function
)
223 fprintf (f
, " used-by-single-function");
224 if (need_bounds_init
)
225 fprintf (f
, " need-bounds-init");
226 if (TREE_READONLY (decl
))
227 fprintf (f
, " read-only");
228 if (ctor_useable_for_folding_p ())
229 fprintf (f
, " const-value-known");
231 fprintf (f
, " write-only");
233 fprintf (f
, " %s", tls_model_names
[tls_model
]);
238 /* Dump given varpool node to stderr. */
239 void varpool_node::debug (void)
241 varpool_node::dump (stderr
);
244 /* Dump the variable pool to F. */
246 varpool_node::dump_varpool (FILE *f
)
250 fprintf (f
, "variable pool:\n\n");
251 FOR_EACH_VARIABLE (node
)
255 /* Dump the variable pool to stderr. */
258 varpool_node::debug_varpool (void)
260 dump_varpool (stderr
);
263 /* Given an assembler name, lookup node. */
265 varpool_node::get_for_asmname (tree asmname
)
267 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
268 return dyn_cast
<varpool_node
*> (node
);
273 /* When doing LTO, read variable's constructor from disk if
274 it is not already present. */
277 varpool_node::get_constructor (void)
279 lto_file_decl_data
*file_data
;
280 const char *data
, *name
;
283 if (DECL_INITIAL (decl
) != error_mark_node
285 return DECL_INITIAL (decl
);
287 timevar_push (TV_IPA_LTO_CTORS_IN
);
289 file_data
= lto_file_data
;
290 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
292 /* We may have renamed the declaration, e.g., a static function. */
293 name
= lto_get_decl_name_mapping (file_data
, name
);
295 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
298 fatal_error ("%s: section %s is missing",
299 file_data
->file_name
,
302 lto_input_variable_constructor (file_data
, this, data
);
303 lto_stats
.num_function_bodies
++;
304 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
306 lto_free_function_in_decl_state_for_node (this);
307 timevar_pop (TV_IPA_LTO_CTORS_IN
);
308 return DECL_INITIAL (decl
);
311 /* Return true if variable has constructor that can be used for folding. */
314 varpool_node::ctor_useable_for_folding_p (void)
316 varpool_node
*real_node
= this;
318 if (real_node
->alias
&& real_node
->definition
)
319 real_node
= ultimate_alias_target ();
321 if (TREE_CODE (decl
) == CONST_DECL
322 || DECL_IN_CONSTANT_POOL (decl
))
324 if (TREE_THIS_VOLATILE (decl
))
327 /* If we do not have a constructor, we can't use it. */
328 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
329 && !real_node
->lto_file_data
)
332 /* Avoid attempts to load constructors that was not streamed. */
333 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
334 && real_node
->body_removed
)
337 /* Vtables are defined by their types and must match no matter of interposition
339 if (DECL_VIRTUAL_P (decl
))
341 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
342 classes not defined in the current TU so that it can refer
343 to them from typeinfo objects. Avoid returning NULL_TREE. */
344 return DECL_INITIAL (real_node
->decl
) != NULL
;
347 /* Alias of readonly variable is also readonly, since the variable is stored
348 in readonly memory. We also accept readonly aliases of non-readonly
349 locations assuming that user knows what he is asking for. */
350 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
353 /* Variables declared 'const' without an initializer
354 have zero as the initializer if they may not be
355 overridden at link or run time.
357 It is actually requirement for C++ compiler to optimize const variables
358 consistently. As a GNU extension, do not enfore this rule for user defined
359 weak variables, so we support interposition on:
360 static const int dummy = 0;
361 extern const int foo __attribute__((__weak__, __alias__("dummy")));
363 if ((!DECL_INITIAL (real_node
->decl
)
364 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
365 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
368 /* Variables declared `const' with an initializer are considered
369 to not be overwritable with different initializer by default.
371 ??? Previously we behaved so for scalar variables but not for array
376 /* If DECLARATION is constant variable and its initial value is known
377 (so we can do constant folding), return its constructor (DECL_INITIAL).
378 This may be an expression or NULL when DECL is initialized to 0.
379 Return ERROR_MARK_NODE otherwise.
381 In LTO this may actually trigger reading the constructor from disk.
382 For this reason varpool_ctor_useable_for_folding_p should be used when
383 the actual constructor value is not needed. */
386 ctor_for_folding (tree decl
)
388 varpool_node
*node
, *real_node
;
391 if (TREE_CODE (decl
) != VAR_DECL
392 && TREE_CODE (decl
) != CONST_DECL
)
393 return error_mark_node
;
395 /* Static constant bounds are created to be
396 used instead of constants and therefore
397 do not let folding it. */
398 if (POINTER_BOUNDS_P (decl
))
399 return error_mark_node
;
401 if (TREE_CODE (decl
) == CONST_DECL
402 || DECL_IN_CONSTANT_POOL (decl
))
403 return DECL_INITIAL (decl
);
405 if (TREE_THIS_VOLATILE (decl
))
406 return error_mark_node
;
408 /* Do not care about automatic variables. Those are never initialized
409 anyway, because gimplifier exapnds the code. */
410 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
412 gcc_assert (!TREE_PUBLIC (decl
));
413 return error_mark_node
;
416 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
418 real_node
= node
= varpool_node::get (decl
);
421 real_node
= node
->ultimate_alias_target ();
422 real_decl
= real_node
->decl
;
427 /* See if we are dealing with alias.
428 In most cases alias is just alternative symbol pointing to a given
429 constructor. This allows us to use interposition rules of DECL
430 constructor of REAL_NODE. However weakrefs are special by being just
431 alternative name of their target (if defined). */
432 if (decl
!= real_decl
)
434 gcc_assert (!DECL_INITIAL (decl
)
435 || (node
->alias
&& node
->get_alias_target () == real_node
)
436 || DECL_INITIAL (decl
) == error_mark_node
);
439 node
= node
->get_alias_target ();
444 if ((!DECL_VIRTUAL_P (real_decl
)
445 || DECL_INITIAL (real_decl
) == error_mark_node
446 || !DECL_INITIAL (real_decl
))
447 && (!node
|| !node
->ctor_useable_for_folding_p ()))
448 return error_mark_node
;
450 /* OK, we can return constructor. See if we need to fetch it from disk
452 if (DECL_INITIAL (real_decl
) != error_mark_node
454 return DECL_INITIAL (real_decl
);
455 return real_node
->get_constructor ();
458 /* Add the variable DECL to the varpool.
459 Unlike finalize_decl function is intended to be used
460 by middle end and allows insertion of new variable at arbitrary point
463 varpool_node::add (tree decl
)
466 varpool_node::finalize_decl (decl
);
467 node
= varpool_node::get_create (decl
);
468 symtab
->call_varpool_insertion_hooks (node
);
469 if (node
->externally_visible_p ())
470 node
->externally_visible
= true;
471 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
472 node
->no_reorder
= 1;
475 /* Return variable availability. See cgraph.h for description of individual
478 varpool_node::get_availability (void)
481 return AVAIL_NOT_AVAILABLE
;
482 if (!TREE_PUBLIC (decl
))
483 return AVAIL_AVAILABLE
;
484 if (DECL_IN_CONSTANT_POOL (decl
)
485 || DECL_VIRTUAL_P (decl
))
486 return AVAIL_AVAILABLE
;
487 if (alias
&& weakref
)
489 enum availability avail
;
491 ultimate_alias_target (&avail
)->get_availability ();
494 /* If the variable can be overwritten, return OVERWRITABLE. Takes
495 care of at least one notable extension - the COMDAT variables
496 used to share template instantiations in C++. */
497 if (decl_replaceable_p (decl
)
498 || DECL_EXTERNAL (decl
))
499 return AVAIL_INTERPOSABLE
;
500 return AVAIL_AVAILABLE
;
504 varpool_node::analyze (void)
506 /* When reading back varpool at LTO time, we re-construct the queue in order
507 to have "needed" list right by inserting all needed nodes into varpool.
508 We however don't want to re-analyze already analyzed nodes. */
511 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
512 /* Compute the alignment early so function body expanders are
513 already informed about increased alignment. */
514 align_variable (decl
, 0);
517 resolve_alias (varpool_node::get (alias_target
));
518 else if (DECL_INITIAL (decl
))
519 record_references_in_initializer (decl
, analyzed
);
523 /* Assemble thunks and aliases associated to varpool node. */
526 varpool_node::assemble_aliases (void)
530 FOR_EACH_ALIAS (this, ref
)
532 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
533 do_assemble_alias (alias
->decl
,
534 DECL_ASSEMBLER_NAME (decl
));
535 alias
->assemble_aliases ();
539 /* Output one variable, if necessary. Return whether we output it. */
542 varpool_node::assemble_decl (void)
544 /* Aliases are outout when their target is produced or by
549 /* Constant pool is output from RTL land when the reference
550 survive till this level. */
551 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
554 /* Decls with VALUE_EXPR should not be in the varpool at all. They
555 are not real variables, but just info for debugging and codegen.
556 Unfortunately at the moment emutls is not updating varpool correctly
557 after turning real vars into value_expr vars. */
558 if (DECL_HAS_VALUE_EXPR_P (decl
)
559 && !targetm
.have_tls
)
562 /* Hard register vars do not need to be output. */
563 if (DECL_HARD_REGISTER (decl
))
566 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
567 && TREE_CODE (decl
) == VAR_DECL
568 && !DECL_HAS_VALUE_EXPR_P (decl
));
570 if (!in_other_partition
571 && !DECL_EXTERNAL (decl
))
574 assemble_variable (decl
, 0, 1, 0);
575 gcc_assert (TREE_ASM_WRITTEN (decl
));
576 gcc_assert (definition
);
584 /* Add NODE to queue starting at FIRST.
585 The queue is linked via AUX pointers and terminated by pointer to 1. */
588 enqueue_node (varpool_node
*node
, varpool_node
**first
)
592 gcc_checking_assert (*first
);
597 /* Optimization of function bodies might've rendered some variables as
598 unnecessary so we want to avoid these from being compiled. Re-do
599 reachability starting from variables that are either externally visible
600 or was referred from the asm output routines. */
603 symbol_table::remove_unreferenced_decls (void)
605 varpool_node
*next
, *node
;
606 varpool_node
*first
= (varpool_node
*)(void *)1;
609 hash_set
<varpool_node
*> referenced
;
615 fprintf (dump_file
, "Trivially needed variables:");
616 FOR_EACH_DEFINED_VARIABLE (node
)
619 && (!node
->can_remove_if_no_refs_p ()
620 /* We just expanded all function bodies. See if any of
621 them needed the variable. */
622 || DECL_RTL_SET_P (node
->decl
)))
624 enqueue_node (node
, &first
);
626 fprintf (dump_file
, " %s", node
->asm_name ());
629 while (first
!= (varpool_node
*)(void *)1)
632 first
= (varpool_node
*)first
->aux
;
634 if (node
->same_comdat_group
)
637 for (next
= node
->same_comdat_group
;
639 next
= next
->same_comdat_group
)
641 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
642 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
643 enqueue_node (vnext
, &first
);
646 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
648 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
650 && !vnode
->in_other_partition
651 && (!DECL_EXTERNAL (ref
->referred
->decl
)
654 enqueue_node (vnode
, &first
);
656 referenced
.add (node
);
660 fprintf (dump_file
, "\nRemoving variables:");
661 for (node
= first_defined_variable (); node
; node
= next
)
663 next
= next_defined_variable (node
);
664 if (!node
->aux
&& !node
->no_reorder
)
667 fprintf (dump_file
, " %s", node
->asm_name ());
668 if (referenced
.contains(node
))
669 node
->remove_initializer ();
676 fprintf (dump_file
, "\n");
679 /* For variables in named sections make sure get_variable_section
680 is called before we switch to those sections. Then section
681 conflicts between read-only and read-only requiring relocations
682 sections can be resolved. */
684 varpool_node::finalize_named_section_flags (void)
686 if (!TREE_ASM_WRITTEN (decl
)
688 && !in_other_partition
689 && !DECL_EXTERNAL (decl
)
690 && TREE_CODE (decl
) == VAR_DECL
691 && !DECL_HAS_VALUE_EXPR_P (decl
)
693 get_variable_section (decl
, false);
696 /* Output all variables enqueued to be assembled. */
698 symbol_table::output_variables (void)
700 bool changed
= false;
706 remove_unreferenced_decls ();
708 timevar_push (TV_VAROUT
);
710 FOR_EACH_VARIABLE (node
)
711 if (!node
->definition
)
712 assemble_undefined_decl (node
->decl
);
713 FOR_EACH_DEFINED_VARIABLE (node
)
715 /* Handled in output_in_order. */
716 if (node
->no_reorder
)
719 node
->finalize_named_section_flags ();
722 FOR_EACH_DEFINED_VARIABLE (node
)
724 /* Handled in output_in_order. */
725 if (node
->no_reorder
)
727 if (node
->assemble_decl ())
730 timevar_pop (TV_VAROUT
);
734 /* Create a new global variable of type TYPE. */
736 add_new_static_var (tree type
)
739 varpool_node
*new_node
;
741 new_decl
= create_tmp_var_raw (type
, NULL
);
742 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
743 TREE_READONLY (new_decl
) = 0;
744 TREE_STATIC (new_decl
) = 1;
745 TREE_USED (new_decl
) = 1;
746 DECL_CONTEXT (new_decl
) = NULL_TREE
;
747 DECL_ABSTRACT_P (new_decl
) = false;
748 lang_hooks
.dup_lang_specific_decl (new_decl
);
749 new_node
= varpool_node::get_create (new_decl
);
750 varpool_node::finalize_decl (new_decl
);
752 return new_node
->decl
;
755 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
756 Extra name aliases are output whenever DECL is output. */
759 varpool_node::create_alias (tree alias
, tree decl
)
761 varpool_node
*alias_node
;
763 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
764 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
765 alias_node
= varpool_node::get_create (alias
);
766 alias_node
->alias
= true;
767 alias_node
->definition
= true;
768 alias_node
->alias_target
= decl
;
769 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
770 alias_node
->weakref
= true;
774 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
775 Extra name aliases are output whenever DECL is output. */
778 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
780 varpool_node
*alias_node
;
782 #ifndef ASM_OUTPUT_DEF
783 /* If aliases aren't supported by the assembler, fail. */
786 alias_node
= varpool_node::create_alias (alias
, decl
);
787 alias_node
->cpp_implicit_alias
= true;
789 /* Extra name alias mechanizm creates aliases really late
790 via DECL_ASSEMBLER_NAME mechanizm.
791 This is unfortunate because they are not going through the
792 standard channels. Ensure they get output. */
793 if (symtab
->cpp_implicit_aliases_done
)
794 alias_node
->resolve_alias (varpool_node::get_create (decl
));
798 /* Call calback on varpool symbol and aliases associated to varpool symbol.
799 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
803 varpool_node::call_for_node_and_aliases (bool (*callback
) (varpool_node
*,
806 bool include_overwritable
)
810 if (callback (this, data
))
813 FOR_EACH_ALIAS (this, ref
)
815 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
816 if (include_overwritable
817 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
818 if (alias
->call_for_node_and_aliases (callback
, data
,
819 include_overwritable
))