1 /* Callgraph handling code.
2 Copyright (C) 2003-2015 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"
29 #include "fold-const.h"
32 #include "basic-block.h"
34 #include "plugin-api.h"
35 #include "hard-reg-set.h"
40 #include "langhooks.h"
41 #include "diagnostic-core.h"
46 #include "gimple-expr.h"
48 #include "tree-ssa-alias.h"
50 #include "lto-streamer.h"
54 const char * const tls_model_names
[]={"none", "emulated",
55 "global-dynamic", "local-dynamic",
56 "initial-exec", "local-exec"};
58 /* List of hooks triggered on varpool_node events. */
59 struct varpool_node_hook_list
{
60 varpool_node_hook hook
;
62 struct varpool_node_hook_list
*next
;
65 /* Register HOOK to be called with DATA on each removed node. */
66 varpool_node_hook_list
*
67 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
69 varpool_node_hook_list
*entry
;
70 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
72 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
82 /* Remove ENTRY from the list of hooks called on removing nodes. */
84 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
86 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
94 /* Call all node removal hooks. */
96 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
98 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
101 entry
->hook (node
, entry
->data
);
106 /* Register HOOK to be called with DATA on each inserted node. */
107 varpool_node_hook_list
*
108 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
110 varpool_node_hook_list
*entry
;
111 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
113 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
123 /* Remove ENTRY from the list of hooks called on inserted nodes. */
125 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
127 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
129 while (*ptr
!= entry
)
135 /* Call all node insertion hooks. */
137 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
139 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
142 entry
->hook (node
, entry
->data
);
147 /* Allocate new callgraph node and insert it into basic data structures. */
150 varpool_node::create_empty (void)
152 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
153 node
->type
= SYMTAB_VARIABLE
;
157 /* Return varpool node assigned to DECL. Create new one when needed. */
159 varpool_node::get_create (tree decl
)
161 varpool_node
*node
= varpool_node::get (decl
);
162 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
166 node
= varpool_node::create_empty ();
169 if ((flag_openacc
|| flag_openmp
) && !DECL_EXTERNAL (decl
)
170 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
)))
172 node
->offloadable
= 1;
173 #ifdef ENABLE_OFFLOADING
174 g
->have_offload
= true;
176 vec_safe_push (offload_vars
, decl
);
177 node
->force_output
= 1;
181 node
->register_symbol ();
185 /* Remove variable from symbol table. */
188 varpool_node::remove (void)
190 symtab
->call_varpool_removal_hooks (this);
193 lto_free_function_in_decl_state_for_node (this);
194 lto_file_data
= NULL
;
197 /* When streaming we can have multiple nodes associated with decl. */
198 if (symtab
->state
== LTO_STREAMING
)
200 /* Keep constructor when it may be used for folding. We remove
201 references to external variables before final compilation. */
202 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
203 && !ctor_useable_for_folding_p ())
204 remove_initializer ();
210 /* Remove node initializer when it is no longer needed. */
212 varpool_node::remove_initializer (void)
214 if (DECL_INITIAL (decl
)
215 && !DECL_IN_CONSTANT_POOL (decl
)
216 /* Keep vtables for BINFO folding. */
217 && !DECL_VIRTUAL_P (decl
)
218 /* FIXME: http://gcc.gnu.org/PR55395 */
219 && debug_info_level
== DINFO_LEVEL_NONE
220 /* When doing declaration merging we have duplicate
221 entries for given decl. Do not attempt to remove
222 the boides, or we will end up remiving
224 && symtab
->state
!= LTO_STREAMING
)
225 DECL_INITIAL (decl
) = error_mark_node
;
228 /* Dump given varpool node to F. */
230 varpool_node::dump (FILE *f
)
233 fprintf (f
, " Availability: %s\n",
234 symtab
->function_flags_ready
235 ? cgraph_availability_names
[get_availability ()]
237 fprintf (f
, " Varpool flags:");
238 if (DECL_INITIAL (decl
))
239 fprintf (f
, " initialized");
241 fprintf (f
, " output");
242 if (used_by_single_function
)
243 fprintf (f
, " used-by-single-function");
244 if (need_bounds_init
)
245 fprintf (f
, " need-bounds-init");
246 if (TREE_READONLY (decl
))
247 fprintf (f
, " read-only");
248 if (ctor_useable_for_folding_p ())
249 fprintf (f
, " const-value-known");
251 fprintf (f
, " write-only");
253 fprintf (f
, " tls-%s", tls_model_names
[tls_model
]);
258 /* Dump given varpool node to stderr. */
259 void varpool_node::debug (void)
261 varpool_node::dump (stderr
);
264 /* Dump the variable pool to F. */
266 varpool_node::dump_varpool (FILE *f
)
270 fprintf (f
, "variable pool:\n\n");
271 FOR_EACH_VARIABLE (node
)
275 /* Dump the variable pool to stderr. */
278 varpool_node::debug_varpool (void)
280 dump_varpool (stderr
);
283 /* Given an assembler name, lookup node. */
285 varpool_node::get_for_asmname (tree asmname
)
287 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
288 return dyn_cast
<varpool_node
*> (node
);
293 /* When doing LTO, read variable's constructor from disk if
294 it is not already present. */
297 varpool_node::get_constructor (void)
299 lto_file_decl_data
*file_data
;
300 const char *data
, *name
;
303 if (DECL_INITIAL (decl
) != error_mark_node
306 return DECL_INITIAL (decl
);
308 timevar_push (TV_IPA_LTO_CTORS_IN
);
310 file_data
= lto_file_data
;
311 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
313 /* We may have renamed the declaration, e.g., a static function. */
314 name
= lto_get_decl_name_mapping (file_data
, name
);
316 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
319 fatal_error (input_location
, "%s: section %s is missing",
320 file_data
->file_name
,
323 lto_input_variable_constructor (file_data
, this, data
);
324 gcc_assert (DECL_INITIAL (decl
) != error_mark_node
);
325 lto_stats
.num_function_bodies
++;
326 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
328 lto_free_function_in_decl_state_for_node (this);
329 timevar_pop (TV_IPA_LTO_CTORS_IN
);
330 return DECL_INITIAL (decl
);
333 /* Return true if variable has constructor that can be used for folding. */
336 varpool_node::ctor_useable_for_folding_p (void)
338 varpool_node
*real_node
= this;
340 if (real_node
->alias
&& real_node
->definition
)
341 real_node
= ultimate_alias_target ();
343 if (TREE_CODE (decl
) == CONST_DECL
344 || DECL_IN_CONSTANT_POOL (decl
))
346 if (TREE_THIS_VOLATILE (decl
))
349 /* If we do not have a constructor, we can't use it. */
350 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
351 && !real_node
->lto_file_data
)
354 /* Avoid attempts to load constructors that was not streamed. */
355 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
356 && real_node
->body_removed
)
359 /* Vtables are defined by their types and must match no matter of interposition
361 if (DECL_VIRTUAL_P (decl
))
363 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
364 classes not defined in the current TU so that it can refer
365 to them from typeinfo objects. Avoid returning NULL_TREE. */
366 return DECL_INITIAL (real_node
->decl
) != NULL
;
369 /* Alias of readonly variable is also readonly, since the variable is stored
370 in readonly memory. We also accept readonly aliases of non-readonly
371 locations assuming that user knows what he is asking for. */
372 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
375 /* Variables declared 'const' without an initializer
376 have zero as the initializer if they may not be
377 overridden at link or run time.
379 It is actually requirement for C++ compiler to optimize const variables
380 consistently. As a GNU extension, do not enfore this rule for user defined
381 weak variables, so we support interposition on:
382 static const int dummy = 0;
383 extern const int foo __attribute__((__weak__, __alias__("dummy")));
385 if ((!DECL_INITIAL (real_node
->decl
)
386 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
387 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
390 /* Variables declared `const' with an initializer are considered
391 to not be overwritable with different initializer by default.
393 ??? Previously we behaved so for scalar variables but not for array
398 /* If DECLARATION is constant variable and its initial value is known
399 (so we can do constant folding), return its constructor (DECL_INITIAL).
400 This may be an expression or NULL when DECL is initialized to 0.
401 Return ERROR_MARK_NODE otherwise.
403 In LTO this may actually trigger reading the constructor from disk.
404 For this reason varpool_ctor_useable_for_folding_p should be used when
405 the actual constructor value is not needed. */
408 ctor_for_folding (tree decl
)
410 varpool_node
*node
, *real_node
;
413 if (TREE_CODE (decl
) != VAR_DECL
414 && TREE_CODE (decl
) != CONST_DECL
)
415 return error_mark_node
;
417 /* Static constant bounds are created to be
418 used instead of constants and therefore
419 do not let folding it. */
420 if (POINTER_BOUNDS_P (decl
))
421 return error_mark_node
;
423 if (TREE_CODE (decl
) == CONST_DECL
424 || DECL_IN_CONSTANT_POOL (decl
))
425 return DECL_INITIAL (decl
);
427 if (TREE_THIS_VOLATILE (decl
))
428 return error_mark_node
;
430 /* Do not care about automatic variables. Those are never initialized
431 anyway, because gimplifier exapnds the code. */
432 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
434 gcc_assert (!TREE_PUBLIC (decl
));
435 return error_mark_node
;
438 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
440 real_node
= node
= varpool_node::get (decl
);
443 real_node
= node
->ultimate_alias_target ();
444 real_decl
= real_node
->decl
;
449 /* See if we are dealing with alias.
450 In most cases alias is just alternative symbol pointing to a given
451 constructor. This allows us to use interposition rules of DECL
452 constructor of REAL_NODE. However weakrefs are special by being just
453 alternative name of their target (if defined). */
454 if (decl
!= real_decl
)
456 gcc_assert (!DECL_INITIAL (decl
)
457 || (node
->alias
&& node
->get_alias_target () == real_node
)
458 || DECL_INITIAL (decl
) == error_mark_node
);
461 node
= node
->get_alias_target ();
466 if ((!DECL_VIRTUAL_P (real_decl
)
467 || DECL_INITIAL (real_decl
) == error_mark_node
468 || !DECL_INITIAL (real_decl
))
469 && (!node
|| !node
->ctor_useable_for_folding_p ()))
470 return error_mark_node
;
472 /* OK, we can return constructor. See if we need to fetch it from disk
474 if (DECL_INITIAL (real_decl
) != error_mark_node
476 return DECL_INITIAL (real_decl
);
477 return real_node
->get_constructor ();
480 /* Add the variable DECL to the varpool.
481 Unlike finalize_decl function is intended to be used
482 by middle end and allows insertion of new variable at arbitrary point
485 varpool_node::add (tree decl
)
488 varpool_node::finalize_decl (decl
);
489 node
= varpool_node::get_create (decl
);
490 symtab
->call_varpool_insertion_hooks (node
);
491 if (node
->externally_visible_p ())
492 node
->externally_visible
= true;
493 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
494 node
->no_reorder
= 1;
497 /* Return variable availability. See cgraph.h for description of individual
500 varpool_node::get_availability (void)
503 return AVAIL_NOT_AVAILABLE
;
504 if (!TREE_PUBLIC (decl
))
505 return AVAIL_AVAILABLE
;
506 if (DECL_IN_CONSTANT_POOL (decl
)
507 || DECL_VIRTUAL_P (decl
))
508 return AVAIL_AVAILABLE
;
509 if (alias
&& weakref
)
511 enum availability avail
;
513 ultimate_alias_target (&avail
)->get_availability ();
516 /* If the variable can be overwritten, return OVERWRITABLE. Takes
517 care of at least one notable extension - the COMDAT variables
518 used to share template instantiations in C++. */
519 if (decl_replaceable_p (decl
)
520 || DECL_EXTERNAL (decl
))
521 return AVAIL_INTERPOSABLE
;
522 return AVAIL_AVAILABLE
;
526 varpool_node::analyze (void)
528 /* When reading back varpool at LTO time, we re-construct the queue in order
529 to have "needed" list right by inserting all needed nodes into varpool.
530 We however don't want to re-analyze already analyzed nodes. */
533 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
534 /* Compute the alignment early so function body expanders are
535 already informed about increased alignment. */
536 align_variable (decl
, 0);
539 resolve_alias (varpool_node::get (alias_target
));
540 else if (DECL_INITIAL (decl
))
541 record_references_in_initializer (decl
, analyzed
);
545 /* Assemble thunks and aliases associated to varpool node. */
548 varpool_node::assemble_aliases (void)
552 FOR_EACH_ALIAS (this, ref
)
554 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
555 do_assemble_alias (alias
->decl
,
556 DECL_ASSEMBLER_NAME (decl
));
557 alias
->assemble_aliases ();
561 /* Output one variable, if necessary. Return whether we output it. */
564 varpool_node::assemble_decl (void)
566 /* Aliases are outout when their target is produced or by
571 /* Constant pool is output from RTL land when the reference
572 survive till this level. */
573 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
576 /* Decls with VALUE_EXPR should not be in the varpool at all. They
577 are not real variables, but just info for debugging and codegen.
578 Unfortunately at the moment emutls is not updating varpool correctly
579 after turning real vars into value_expr vars. */
580 if (DECL_HAS_VALUE_EXPR_P (decl
)
581 && !targetm
.have_tls
)
584 /* Hard register vars do not need to be output. */
585 if (DECL_HARD_REGISTER (decl
))
588 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
589 && TREE_CODE (decl
) == VAR_DECL
590 && !DECL_HAS_VALUE_EXPR_P (decl
));
592 if (!in_other_partition
593 && !DECL_EXTERNAL (decl
))
596 assemble_variable (decl
, 0, 1, 0);
597 gcc_assert (TREE_ASM_WRITTEN (decl
));
598 gcc_assert (definition
);
606 /* Add NODE to queue starting at FIRST.
607 The queue is linked via AUX pointers and terminated by pointer to 1. */
610 enqueue_node (varpool_node
*node
, varpool_node
**first
)
614 gcc_checking_assert (*first
);
619 /* Optimization of function bodies might've rendered some variables as
620 unnecessary so we want to avoid these from being compiled. Re-do
621 reachability starting from variables that are either externally visible
622 or was referred from the asm output routines. */
625 symbol_table::remove_unreferenced_decls (void)
627 varpool_node
*next
, *node
;
628 varpool_node
*first
= (varpool_node
*)(void *)1;
631 hash_set
<varpool_node
*> referenced
;
637 fprintf (dump_file
, "Trivially needed variables:");
638 FOR_EACH_DEFINED_VARIABLE (node
)
641 && (!node
->can_remove_if_no_refs_p ()
642 /* We just expanded all function bodies. See if any of
643 them needed the variable. */
644 || DECL_RTL_SET_P (node
->decl
)))
646 enqueue_node (node
, &first
);
648 fprintf (dump_file
, " %s", node
->asm_name ());
651 while (first
!= (varpool_node
*)(void *)1)
654 first
= (varpool_node
*)first
->aux
;
656 if (node
->same_comdat_group
)
659 for (next
= node
->same_comdat_group
;
661 next
= next
->same_comdat_group
)
663 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
664 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
665 enqueue_node (vnext
, &first
);
668 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
670 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
672 && !vnode
->in_other_partition
673 && (!DECL_EXTERNAL (ref
->referred
->decl
)
676 enqueue_node (vnode
, &first
);
678 referenced
.add (node
);
682 fprintf (dump_file
, "\nRemoving variables:");
683 for (node
= first_defined_variable (); node
; node
= next
)
685 next
= next_defined_variable (node
);
686 if (!node
->aux
&& !node
->no_reorder
)
689 fprintf (dump_file
, " %s", node
->asm_name ());
690 if (referenced
.contains(node
))
691 node
->remove_initializer ();
698 fprintf (dump_file
, "\n");
701 /* For variables in named sections make sure get_variable_section
702 is called before we switch to those sections. Then section
703 conflicts between read-only and read-only requiring relocations
704 sections can be resolved. */
706 varpool_node::finalize_named_section_flags (void)
708 if (!TREE_ASM_WRITTEN (decl
)
710 && !in_other_partition
711 && !DECL_EXTERNAL (decl
)
712 && TREE_CODE (decl
) == VAR_DECL
713 && !DECL_HAS_VALUE_EXPR_P (decl
)
715 get_variable_section (decl
, false);
718 /* Output all variables enqueued to be assembled. */
720 symbol_table::output_variables (void)
722 bool changed
= false;
728 remove_unreferenced_decls ();
730 timevar_push (TV_VAROUT
);
732 FOR_EACH_VARIABLE (node
)
733 if (!node
->definition
734 && !DECL_HAS_VALUE_EXPR_P (node
->decl
)
735 && !DECL_HARD_REGISTER (node
->decl
))
736 assemble_undefined_decl (node
->decl
);
737 FOR_EACH_DEFINED_VARIABLE (node
)
739 /* Handled in output_in_order. */
740 if (node
->no_reorder
)
743 node
->finalize_named_section_flags ();
746 FOR_EACH_DEFINED_VARIABLE (node
)
748 /* Handled in output_in_order. */
749 if (node
->no_reorder
)
751 if (node
->assemble_decl ())
754 timevar_pop (TV_VAROUT
);
758 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
759 Extra name aliases are output whenever DECL is output. */
762 varpool_node::create_alias (tree alias
, tree decl
)
764 varpool_node
*alias_node
;
766 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
767 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
768 alias_node
= varpool_node::get_create (alias
);
769 alias_node
->alias
= true;
770 alias_node
->definition
= true;
771 alias_node
->alias_target
= decl
;
772 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
773 alias_node
->weakref
= true;
777 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
778 Extra name aliases are output whenever DECL is output. */
781 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
783 varpool_node
*alias_node
;
785 #ifndef ASM_OUTPUT_DEF
786 /* If aliases aren't supported by the assembler, fail. */
789 alias_node
= varpool_node::create_alias (alias
, decl
);
790 alias_node
->cpp_implicit_alias
= true;
792 /* Extra name alias mechanizm creates aliases really late
793 via DECL_ASSEMBLER_NAME mechanizm.
794 This is unfortunate because they are not going through the
795 standard channels. Ensure they get output. */
796 if (symtab
->cpp_implicit_aliases_done
)
797 alias_node
->resolve_alias (varpool_node::get_create (decl
));
801 /* Worker for call_for_symbol_and_aliases. */
804 varpool_node::call_for_symbol_and_aliases_1 (bool (*callback
) (varpool_node
*,
807 bool include_overwritable
)
811 FOR_EACH_ALIAS (this, ref
)
813 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
814 if (include_overwritable
815 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
816 if (alias
->call_for_symbol_and_aliases (callback
, data
,
817 include_overwritable
))