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"
55 const char * const tls_model_names
[]={"none", "tls-emulated", "tls-real",
56 "tls-global-dynamic", "tls-local-dynamic",
57 "tls-initial-exec", "tls-local-exec"};
59 /* List of hooks triggered on varpool_node events. */
60 struct varpool_node_hook_list
{
61 varpool_node_hook hook
;
63 struct varpool_node_hook_list
*next
;
66 /* Register HOOK to be called with DATA on each removed node. */
67 varpool_node_hook_list
*
68 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
70 varpool_node_hook_list
*entry
;
71 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
73 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
83 /* Remove ENTRY from the list of hooks called on removing nodes. */
85 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
87 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
95 /* Call all node removal hooks. */
97 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
99 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
102 entry
->hook (node
, entry
->data
);
107 /* Register HOOK to be called with DATA on each inserted node. */
108 varpool_node_hook_list
*
109 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
111 varpool_node_hook_list
*entry
;
112 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
114 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
124 /* Remove ENTRY from the list of hooks called on inserted nodes. */
126 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
128 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
130 while (*ptr
!= entry
)
136 /* Call all node insertion hooks. */
138 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
140 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
143 entry
->hook (node
, entry
->data
);
148 /* Allocate new callgraph node and insert it into basic data structures. */
151 varpool_node::create_empty (void)
153 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
154 node
->type
= SYMTAB_VARIABLE
;
158 /* Return varpool node assigned to DECL. Create new one when needed. */
160 varpool_node::get_create (tree decl
)
162 varpool_node
*node
= varpool_node::get (decl
);
163 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
167 node
= varpool_node::create_empty ();
171 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
)))
173 node
->offloadable
= 1;
174 g
->have_offload
= true;
176 vec_safe_push (offload_vars
, decl
);
179 node
->register_symbol ();
183 /* Remove variable from symbol table. */
186 varpool_node::remove (void)
188 symtab
->call_varpool_removal_hooks (this);
191 /* When streaming we can have multiple nodes associated with decl. */
192 if (symtab
->state
== LTO_STREAMING
)
194 /* Keep constructor when it may be used for folding. We remove
195 references to external variables before final compilation. */
196 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
197 && !ctor_useable_for_folding_p ())
198 remove_initializer ();
202 /* Remove node initializer when it is no longer needed. */
204 varpool_node::remove_initializer (void)
206 if (DECL_INITIAL (decl
)
207 && !DECL_IN_CONSTANT_POOL (decl
)
208 /* Keep vtables for BINFO folding. */
209 && !DECL_VIRTUAL_P (decl
)
210 /* FIXME: http://gcc.gnu.org/PR55395 */
211 && debug_info_level
== DINFO_LEVEL_NONE
212 /* When doing declaration merging we have duplicate
213 entries for given decl. Do not attempt to remove
214 the boides, or we will end up remiving
216 && symtab
->state
!= LTO_STREAMING
)
217 DECL_INITIAL (decl
) = error_mark_node
;
220 /* Dump given varpool node to F. */
222 varpool_node::dump (FILE *f
)
225 fprintf (f
, " Availability: %s\n",
226 symtab
->function_flags_ready
227 ? cgraph_availability_names
[get_availability ()]
229 fprintf (f
, " Varpool flags:");
230 if (DECL_INITIAL (decl
))
231 fprintf (f
, " initialized");
233 fprintf (f
, " output");
234 if (used_by_single_function
)
235 fprintf (f
, " used-by-single-function");
236 if (need_bounds_init
)
237 fprintf (f
, " need-bounds-init");
238 if (TREE_READONLY (decl
))
239 fprintf (f
, " read-only");
240 if (ctor_useable_for_folding_p ())
241 fprintf (f
, " const-value-known");
243 fprintf (f
, " write-only");
245 fprintf (f
, " %s", tls_model_names
[tls_model
]);
250 /* Dump given varpool node to stderr. */
251 void varpool_node::debug (void)
253 varpool_node::dump (stderr
);
256 /* Dump the variable pool to F. */
258 varpool_node::dump_varpool (FILE *f
)
262 fprintf (f
, "variable pool:\n\n");
263 FOR_EACH_VARIABLE (node
)
267 /* Dump the variable pool to stderr. */
270 varpool_node::debug_varpool (void)
272 dump_varpool (stderr
);
275 /* Given an assembler name, lookup node. */
277 varpool_node::get_for_asmname (tree asmname
)
279 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
280 return dyn_cast
<varpool_node
*> (node
);
285 /* When doing LTO, read variable's constructor from disk if
286 it is not already present. */
289 varpool_node::get_constructor (void)
291 lto_file_decl_data
*file_data
;
292 const char *data
, *name
;
295 if (DECL_INITIAL (decl
) != error_mark_node
297 return DECL_INITIAL (decl
);
299 timevar_push (TV_IPA_LTO_CTORS_IN
);
301 file_data
= lto_file_data
;
302 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
304 /* We may have renamed the declaration, e.g., a static function. */
305 name
= lto_get_decl_name_mapping (file_data
, name
);
307 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
310 fatal_error ("%s: section %s is missing",
311 file_data
->file_name
,
314 lto_input_variable_constructor (file_data
, this, data
);
315 lto_stats
.num_function_bodies
++;
316 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
318 lto_free_function_in_decl_state_for_node (this);
319 timevar_pop (TV_IPA_LTO_CTORS_IN
);
320 return DECL_INITIAL (decl
);
323 /* Return true if variable has constructor that can be used for folding. */
326 varpool_node::ctor_useable_for_folding_p (void)
328 varpool_node
*real_node
= this;
330 if (real_node
->alias
&& real_node
->definition
)
331 real_node
= ultimate_alias_target ();
333 if (TREE_CODE (decl
) == CONST_DECL
334 || DECL_IN_CONSTANT_POOL (decl
))
336 if (TREE_THIS_VOLATILE (decl
))
339 /* If we do not have a constructor, we can't use it. */
340 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
341 && !real_node
->lto_file_data
)
344 /* Avoid attempts to load constructors that was not streamed. */
345 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
346 && real_node
->body_removed
)
349 /* Vtables are defined by their types and must match no matter of interposition
351 if (DECL_VIRTUAL_P (decl
))
353 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
354 classes not defined in the current TU so that it can refer
355 to them from typeinfo objects. Avoid returning NULL_TREE. */
356 return DECL_INITIAL (real_node
->decl
) != NULL
;
359 /* Alias of readonly variable is also readonly, since the variable is stored
360 in readonly memory. We also accept readonly aliases of non-readonly
361 locations assuming that user knows what he is asking for. */
362 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
365 /* Variables declared 'const' without an initializer
366 have zero as the initializer if they may not be
367 overridden at link or run time.
369 It is actually requirement for C++ compiler to optimize const variables
370 consistently. As a GNU extension, do not enfore this rule for user defined
371 weak variables, so we support interposition on:
372 static const int dummy = 0;
373 extern const int foo __attribute__((__weak__, __alias__("dummy")));
375 if ((!DECL_INITIAL (real_node
->decl
)
376 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
377 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
380 /* Variables declared `const' with an initializer are considered
381 to not be overwritable with different initializer by default.
383 ??? Previously we behaved so for scalar variables but not for array
388 /* If DECLARATION is constant variable and its initial value is known
389 (so we can do constant folding), return its constructor (DECL_INITIAL).
390 This may be an expression or NULL when DECL is initialized to 0.
391 Return ERROR_MARK_NODE otherwise.
393 In LTO this may actually trigger reading the constructor from disk.
394 For this reason varpool_ctor_useable_for_folding_p should be used when
395 the actual constructor value is not needed. */
398 ctor_for_folding (tree decl
)
400 varpool_node
*node
, *real_node
;
403 if (TREE_CODE (decl
) != VAR_DECL
404 && TREE_CODE (decl
) != CONST_DECL
)
405 return error_mark_node
;
407 /* Static constant bounds are created to be
408 used instead of constants and therefore
409 do not let folding it. */
410 if (POINTER_BOUNDS_P (decl
))
411 return error_mark_node
;
413 if (TREE_CODE (decl
) == CONST_DECL
414 || DECL_IN_CONSTANT_POOL (decl
))
415 return DECL_INITIAL (decl
);
417 if (TREE_THIS_VOLATILE (decl
))
418 return error_mark_node
;
420 /* Do not care about automatic variables. Those are never initialized
421 anyway, because gimplifier exapnds the code. */
422 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
424 gcc_assert (!TREE_PUBLIC (decl
));
425 return error_mark_node
;
428 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
430 real_node
= node
= varpool_node::get (decl
);
433 real_node
= node
->ultimate_alias_target ();
434 real_decl
= real_node
->decl
;
439 /* See if we are dealing with alias.
440 In most cases alias is just alternative symbol pointing to a given
441 constructor. This allows us to use interposition rules of DECL
442 constructor of REAL_NODE. However weakrefs are special by being just
443 alternative name of their target (if defined). */
444 if (decl
!= real_decl
)
446 gcc_assert (!DECL_INITIAL (decl
)
447 || (node
->alias
&& node
->get_alias_target () == real_node
)
448 || DECL_INITIAL (decl
) == error_mark_node
);
451 node
= node
->get_alias_target ();
456 if ((!DECL_VIRTUAL_P (real_decl
)
457 || DECL_INITIAL (real_decl
) == error_mark_node
458 || !DECL_INITIAL (real_decl
))
459 && (!node
|| !node
->ctor_useable_for_folding_p ()))
460 return error_mark_node
;
462 /* OK, we can return constructor. See if we need to fetch it from disk
464 if (DECL_INITIAL (real_decl
) != error_mark_node
466 return DECL_INITIAL (real_decl
);
467 return real_node
->get_constructor ();
470 /* Add the variable DECL to the varpool.
471 Unlike finalize_decl function is intended to be used
472 by middle end and allows insertion of new variable at arbitrary point
475 varpool_node::add (tree decl
)
478 varpool_node::finalize_decl (decl
);
479 node
= varpool_node::get_create (decl
);
480 symtab
->call_varpool_insertion_hooks (node
);
481 if (node
->externally_visible_p ())
482 node
->externally_visible
= true;
483 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
484 node
->no_reorder
= 1;
487 /* Return variable availability. See cgraph.h for description of individual
490 varpool_node::get_availability (void)
493 return AVAIL_NOT_AVAILABLE
;
494 if (!TREE_PUBLIC (decl
))
495 return AVAIL_AVAILABLE
;
496 if (DECL_IN_CONSTANT_POOL (decl
)
497 || DECL_VIRTUAL_P (decl
))
498 return AVAIL_AVAILABLE
;
499 if (alias
&& weakref
)
501 enum availability avail
;
503 ultimate_alias_target (&avail
)->get_availability ();
506 /* If the variable can be overwritten, return OVERWRITABLE. Takes
507 care of at least one notable extension - the COMDAT variables
508 used to share template instantiations in C++. */
509 if (decl_replaceable_p (decl
)
510 || DECL_EXTERNAL (decl
))
511 return AVAIL_INTERPOSABLE
;
512 return AVAIL_AVAILABLE
;
516 varpool_node::analyze (void)
518 /* When reading back varpool at LTO time, we re-construct the queue in order
519 to have "needed" list right by inserting all needed nodes into varpool.
520 We however don't want to re-analyze already analyzed nodes. */
523 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
524 /* Compute the alignment early so function body expanders are
525 already informed about increased alignment. */
526 align_variable (decl
, 0);
529 resolve_alias (varpool_node::get (alias_target
));
530 else if (DECL_INITIAL (decl
))
531 record_references_in_initializer (decl
, analyzed
);
535 /* Assemble thunks and aliases associated to varpool node. */
538 varpool_node::assemble_aliases (void)
542 FOR_EACH_ALIAS (this, ref
)
544 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
545 do_assemble_alias (alias
->decl
,
546 DECL_ASSEMBLER_NAME (decl
));
547 alias
->assemble_aliases ();
551 /* Output one variable, if necessary. Return whether we output it. */
554 varpool_node::assemble_decl (void)
556 /* Aliases are outout when their target is produced or by
561 /* Constant pool is output from RTL land when the reference
562 survive till this level. */
563 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
566 /* Decls with VALUE_EXPR should not be in the varpool at all. They
567 are not real variables, but just info for debugging and codegen.
568 Unfortunately at the moment emutls is not updating varpool correctly
569 after turning real vars into value_expr vars. */
570 if (DECL_HAS_VALUE_EXPR_P (decl
)
571 && !targetm
.have_tls
)
574 /* Hard register vars do not need to be output. */
575 if (DECL_HARD_REGISTER (decl
))
578 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
579 && TREE_CODE (decl
) == VAR_DECL
580 && !DECL_HAS_VALUE_EXPR_P (decl
));
582 if (!in_other_partition
583 && !DECL_EXTERNAL (decl
))
586 assemble_variable (decl
, 0, 1, 0);
587 gcc_assert (TREE_ASM_WRITTEN (decl
));
588 gcc_assert (definition
);
596 /* Add NODE to queue starting at FIRST.
597 The queue is linked via AUX pointers and terminated by pointer to 1. */
600 enqueue_node (varpool_node
*node
, varpool_node
**first
)
604 gcc_checking_assert (*first
);
609 /* Optimization of function bodies might've rendered some variables as
610 unnecessary so we want to avoid these from being compiled. Re-do
611 reachability starting from variables that are either externally visible
612 or was referred from the asm output routines. */
615 symbol_table::remove_unreferenced_decls (void)
617 varpool_node
*next
, *node
;
618 varpool_node
*first
= (varpool_node
*)(void *)1;
621 hash_set
<varpool_node
*> referenced
;
627 fprintf (dump_file
, "Trivially needed variables:");
628 FOR_EACH_DEFINED_VARIABLE (node
)
631 && (!node
->can_remove_if_no_refs_p ()
632 /* We just expanded all function bodies. See if any of
633 them needed the variable. */
634 || DECL_RTL_SET_P (node
->decl
)))
636 enqueue_node (node
, &first
);
638 fprintf (dump_file
, " %s", node
->asm_name ());
641 while (first
!= (varpool_node
*)(void *)1)
644 first
= (varpool_node
*)first
->aux
;
646 if (node
->same_comdat_group
)
649 for (next
= node
->same_comdat_group
;
651 next
= next
->same_comdat_group
)
653 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
654 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
655 enqueue_node (vnext
, &first
);
658 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
660 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
662 && !vnode
->in_other_partition
663 && (!DECL_EXTERNAL (ref
->referred
->decl
)
666 enqueue_node (vnode
, &first
);
668 referenced
.add (node
);
672 fprintf (dump_file
, "\nRemoving variables:");
673 for (node
= first_defined_variable (); node
; node
= next
)
675 next
= next_defined_variable (node
);
676 if (!node
->aux
&& !node
->no_reorder
)
679 fprintf (dump_file
, " %s", node
->asm_name ());
680 if (referenced
.contains(node
))
681 node
->remove_initializer ();
688 fprintf (dump_file
, "\n");
691 /* For variables in named sections make sure get_variable_section
692 is called before we switch to those sections. Then section
693 conflicts between read-only and read-only requiring relocations
694 sections can be resolved. */
696 varpool_node::finalize_named_section_flags (void)
698 if (!TREE_ASM_WRITTEN (decl
)
700 && !in_other_partition
701 && !DECL_EXTERNAL (decl
)
702 && TREE_CODE (decl
) == VAR_DECL
703 && !DECL_HAS_VALUE_EXPR_P (decl
)
705 get_variable_section (decl
, false);
708 /* Output all variables enqueued to be assembled. */
710 symbol_table::output_variables (void)
712 bool changed
= false;
718 remove_unreferenced_decls ();
720 timevar_push (TV_VAROUT
);
722 FOR_EACH_VARIABLE (node
)
723 if (!node
->definition
)
724 assemble_undefined_decl (node
->decl
);
725 FOR_EACH_DEFINED_VARIABLE (node
)
727 /* Handled in output_in_order. */
728 if (node
->no_reorder
)
731 node
->finalize_named_section_flags ();
734 FOR_EACH_DEFINED_VARIABLE (node
)
736 /* Handled in output_in_order. */
737 if (node
->no_reorder
)
739 if (node
->assemble_decl ())
742 timevar_pop (TV_VAROUT
);
746 /* Create a new global variable of type TYPE. */
748 add_new_static_var (tree type
)
751 varpool_node
*new_node
;
753 new_decl
= create_tmp_var_raw (type
, NULL
);
754 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
755 TREE_READONLY (new_decl
) = 0;
756 TREE_STATIC (new_decl
) = 1;
757 TREE_USED (new_decl
) = 1;
758 DECL_CONTEXT (new_decl
) = NULL_TREE
;
759 DECL_ABSTRACT_P (new_decl
) = false;
760 lang_hooks
.dup_lang_specific_decl (new_decl
);
761 new_node
= varpool_node::get_create (new_decl
);
762 varpool_node::finalize_decl (new_decl
);
764 return new_node
->decl
;
767 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
768 Extra name aliases are output whenever DECL is output. */
771 varpool_node::create_alias (tree alias
, tree decl
)
773 varpool_node
*alias_node
;
775 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
776 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
777 alias_node
= varpool_node::get_create (alias
);
778 alias_node
->alias
= true;
779 alias_node
->definition
= true;
780 alias_node
->alias_target
= decl
;
781 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
782 alias_node
->weakref
= true;
786 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
787 Extra name aliases are output whenever DECL is output. */
790 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
792 varpool_node
*alias_node
;
794 #ifndef ASM_OUTPUT_DEF
795 /* If aliases aren't supported by the assembler, fail. */
798 alias_node
= varpool_node::create_alias (alias
, decl
);
799 alias_node
->cpp_implicit_alias
= true;
801 /* Extra name alias mechanizm creates aliases really late
802 via DECL_ASSEMBLER_NAME mechanizm.
803 This is unfortunate because they are not going through the
804 standard channels. Ensure they get output. */
805 if (symtab
->cpp_implicit_aliases_done
)
806 alias_node
->resolve_alias (varpool_node::get_create (decl
));
810 /* Call calback on varpool symbol and aliases associated to varpool symbol.
811 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
815 varpool_node::call_for_node_and_aliases (bool (*callback
) (varpool_node
*,
818 bool include_overwritable
)
822 if (callback (this, data
))
825 FOR_EACH_ALIAS (this, ref
)
827 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
828 if (include_overwritable
829 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
830 if (alias
->call_for_node_and_aliases (callback
, data
,
831 include_overwritable
))