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"
30 #include "lto-streamer.h"
37 const char * const tls_model_names
[]={"none", "emulated",
38 "global-dynamic", "local-dynamic",
39 "initial-exec", "local-exec"};
41 /* List of hooks triggered on varpool_node events. */
42 struct varpool_node_hook_list
{
43 varpool_node_hook hook
;
45 struct varpool_node_hook_list
*next
;
48 /* Register HOOK to be called with DATA on each removed node. */
49 varpool_node_hook_list
*
50 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
52 varpool_node_hook_list
*entry
;
53 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
55 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
65 /* Remove ENTRY from the list of hooks called on removing nodes. */
67 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
69 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
77 /* Call all node removal hooks. */
79 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
81 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
84 entry
->hook (node
, entry
->data
);
89 /* Register HOOK to be called with DATA on each inserted node. */
90 varpool_node_hook_list
*
91 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
93 varpool_node_hook_list
*entry
;
94 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
96 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
106 /* Remove ENTRY from the list of hooks called on inserted nodes. */
108 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
110 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
112 while (*ptr
!= entry
)
118 /* Call all node insertion hooks. */
120 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
122 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
125 entry
->hook (node
, entry
->data
);
130 /* Allocate new callgraph node and insert it into basic data structures. */
133 varpool_node::create_empty (void)
135 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
136 node
->type
= SYMTAB_VARIABLE
;
140 /* Return varpool node assigned to DECL. Create new one when needed. */
142 varpool_node::get_create (tree decl
)
144 varpool_node
*node
= varpool_node::get (decl
);
145 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
149 node
= varpool_node::create_empty ();
152 if ((flag_openacc
|| flag_openmp
) && !DECL_EXTERNAL (decl
)
153 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
)))
155 node
->offloadable
= 1;
156 #ifdef ENABLE_OFFLOADING
157 g
->have_offload
= true;
159 vec_safe_push (offload_vars
, decl
);
160 node
->force_output
= 1;
164 node
->register_symbol ();
168 /* Remove variable from symbol table. */
171 varpool_node::remove (void)
173 symtab
->call_varpool_removal_hooks (this);
176 lto_free_function_in_decl_state_for_node (this);
177 lto_file_data
= NULL
;
180 /* When streaming we can have multiple nodes associated with decl. */
181 if (symtab
->state
== LTO_STREAMING
)
183 /* Keep constructor when it may be used for folding. We remove
184 references to external variables before final compilation. */
185 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
186 && !ctor_useable_for_folding_p ())
187 remove_initializer ();
193 /* Remove node initializer when it is no longer needed. */
195 varpool_node::remove_initializer (void)
197 if (DECL_INITIAL (decl
)
198 && !DECL_IN_CONSTANT_POOL (decl
)
199 /* Keep vtables for BINFO folding. */
200 && !DECL_VIRTUAL_P (decl
)
201 /* FIXME: http://gcc.gnu.org/PR55395 */
202 && debug_info_level
== DINFO_LEVEL_NONE
203 /* When doing declaration merging we have duplicate
204 entries for given decl. Do not attempt to remove
205 the boides, or we will end up remiving
207 && symtab
->state
!= LTO_STREAMING
)
208 DECL_INITIAL (decl
) = error_mark_node
;
211 /* Dump given varpool node to F. */
213 varpool_node::dump (FILE *f
)
216 fprintf (f
, " Availability: %s\n",
217 symtab
->function_flags_ready
218 ? cgraph_availability_names
[get_availability ()]
220 fprintf (f
, " Varpool flags:");
221 if (DECL_INITIAL (decl
))
222 fprintf (f
, " initialized");
224 fprintf (f
, " output");
225 if (used_by_single_function
)
226 fprintf (f
, " used-by-single-function");
227 if (need_bounds_init
)
228 fprintf (f
, " need-bounds-init");
229 if (TREE_READONLY (decl
))
230 fprintf (f
, " read-only");
231 if (ctor_useable_for_folding_p ())
232 fprintf (f
, " const-value-known");
234 fprintf (f
, " write-only");
236 fprintf (f
, " tls-%s", tls_model_names
[tls_model
]);
241 /* Dump given varpool node to stderr. */
242 void varpool_node::debug (void)
244 varpool_node::dump (stderr
);
247 /* Dump the variable pool to F. */
249 varpool_node::dump_varpool (FILE *f
)
253 fprintf (f
, "variable pool:\n\n");
254 FOR_EACH_VARIABLE (node
)
258 /* Dump the variable pool to stderr. */
261 varpool_node::debug_varpool (void)
263 dump_varpool (stderr
);
266 /* Given an assembler name, lookup node. */
268 varpool_node::get_for_asmname (tree asmname
)
270 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
271 return dyn_cast
<varpool_node
*> (node
);
276 /* When doing LTO, read variable's constructor from disk if
277 it is not already present. */
280 varpool_node::get_constructor (void)
282 lto_file_decl_data
*file_data
;
283 const char *data
, *name
;
286 if (DECL_INITIAL (decl
) != error_mark_node
289 return DECL_INITIAL (decl
);
291 timevar_push (TV_IPA_LTO_CTORS_IN
);
293 file_data
= lto_file_data
;
294 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
296 /* We may have renamed the declaration, e.g., a static function. */
297 name
= lto_get_decl_name_mapping (file_data
, name
);
299 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
302 fatal_error (input_location
, "%s: section %s is missing",
303 file_data
->file_name
,
306 lto_input_variable_constructor (file_data
, this, data
);
307 gcc_assert (DECL_INITIAL (decl
) != error_mark_node
);
308 lto_stats
.num_function_bodies
++;
309 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
311 lto_free_function_in_decl_state_for_node (this);
312 timevar_pop (TV_IPA_LTO_CTORS_IN
);
313 return DECL_INITIAL (decl
);
316 /* Return true if variable has constructor that can be used for folding. */
319 varpool_node::ctor_useable_for_folding_p (void)
321 varpool_node
*real_node
= this;
323 if (real_node
->alias
&& real_node
->definition
)
324 real_node
= ultimate_alias_target ();
326 if (TREE_CODE (decl
) == CONST_DECL
327 || DECL_IN_CONSTANT_POOL (decl
))
329 if (TREE_THIS_VOLATILE (decl
))
332 /* If we do not have a constructor, we can't use it. */
333 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
334 && !real_node
->lto_file_data
)
337 /* Avoid attempts to load constructors that was not streamed. */
338 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
339 && real_node
->body_removed
)
342 /* Vtables are defined by their types and must match no matter of interposition
344 if (DECL_VIRTUAL_P (decl
))
346 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
347 classes not defined in the current TU so that it can refer
348 to them from typeinfo objects. Avoid returning NULL_TREE. */
349 return DECL_INITIAL (real_node
->decl
) != NULL
;
352 /* Alias of readonly variable is also readonly, since the variable is stored
353 in readonly memory. We also accept readonly aliases of non-readonly
354 locations assuming that user knows what he is asking for. */
355 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
358 /* Variables declared 'const' without an initializer
359 have zero as the initializer if they may not be
360 overridden at link or run time.
362 It is actually requirement for C++ compiler to optimize const variables
363 consistently. As a GNU extension, do not enfore this rule for user defined
364 weak variables, so we support interposition on:
365 static const int dummy = 0;
366 extern const int foo __attribute__((__weak__, __alias__("dummy")));
368 if ((!DECL_INITIAL (real_node
->decl
)
369 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
370 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
373 /* Variables declared `const' with an initializer are considered
374 to not be overwritable with different initializer by default.
376 ??? Previously we behaved so for scalar variables but not for array
381 /* If DECLARATION is constant variable and its initial value is known
382 (so we can do constant folding), return its constructor (DECL_INITIAL).
383 This may be an expression or NULL when DECL is initialized to 0.
384 Return ERROR_MARK_NODE otherwise.
386 In LTO this may actually trigger reading the constructor from disk.
387 For this reason varpool_ctor_useable_for_folding_p should be used when
388 the actual constructor value is not needed. */
391 ctor_for_folding (tree decl
)
393 varpool_node
*node
, *real_node
;
396 if (TREE_CODE (decl
) != VAR_DECL
397 && TREE_CODE (decl
) != CONST_DECL
)
398 return error_mark_node
;
400 /* Static constant bounds are created to be
401 used instead of constants and therefore
402 do not let folding it. */
403 if (POINTER_BOUNDS_P (decl
))
404 return error_mark_node
;
406 if (TREE_CODE (decl
) == CONST_DECL
407 || DECL_IN_CONSTANT_POOL (decl
))
408 return DECL_INITIAL (decl
);
410 if (TREE_THIS_VOLATILE (decl
))
411 return error_mark_node
;
413 /* Do not care about automatic variables. Those are never initialized
414 anyway, because gimplifier exapnds the code. */
415 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
417 gcc_assert (!TREE_PUBLIC (decl
));
418 return error_mark_node
;
421 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
423 real_node
= node
= varpool_node::get (decl
);
426 real_node
= node
->ultimate_alias_target ();
427 real_decl
= real_node
->decl
;
432 /* See if we are dealing with alias.
433 In most cases alias is just alternative symbol pointing to a given
434 constructor. This allows us to use interposition rules of DECL
435 constructor of REAL_NODE. However weakrefs are special by being just
436 alternative name of their target (if defined). */
437 if (decl
!= real_decl
)
439 gcc_assert (!DECL_INITIAL (decl
)
440 || (node
->alias
&& node
->get_alias_target () == real_node
)
441 || DECL_INITIAL (decl
) == error_mark_node
);
444 node
= node
->get_alias_target ();
449 if ((!DECL_VIRTUAL_P (real_decl
)
450 || DECL_INITIAL (real_decl
) == error_mark_node
451 || !DECL_INITIAL (real_decl
))
452 && (!node
|| !node
->ctor_useable_for_folding_p ()))
453 return error_mark_node
;
455 /* OK, we can return constructor. See if we need to fetch it from disk
457 if (DECL_INITIAL (real_decl
) != error_mark_node
459 return DECL_INITIAL (real_decl
);
460 return real_node
->get_constructor ();
463 /* Add the variable DECL to the varpool.
464 Unlike finalize_decl function is intended to be used
465 by middle end and allows insertion of new variable at arbitrary point
468 varpool_node::add (tree decl
)
471 varpool_node::finalize_decl (decl
);
472 node
= varpool_node::get_create (decl
);
473 symtab
->call_varpool_insertion_hooks (node
);
474 if (node
->externally_visible_p ())
475 node
->externally_visible
= true;
476 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
477 node
->no_reorder
= 1;
480 /* Return variable availability. See cgraph.h for description of individual
483 varpool_node::get_availability (void)
486 return AVAIL_NOT_AVAILABLE
;
487 if (!TREE_PUBLIC (decl
))
488 return AVAIL_AVAILABLE
;
489 if (DECL_IN_CONSTANT_POOL (decl
)
490 || DECL_VIRTUAL_P (decl
))
491 return AVAIL_AVAILABLE
;
492 if (alias
&& weakref
)
494 enum availability avail
;
496 ultimate_alias_target (&avail
)->get_availability ();
499 /* If the variable can be overwritten, return OVERWRITABLE. Takes
500 care of at least one notable extension - the COMDAT variables
501 used to share template instantiations in C++. */
502 if (decl_replaceable_p (decl
)
503 || DECL_EXTERNAL (decl
))
504 return AVAIL_INTERPOSABLE
;
505 return AVAIL_AVAILABLE
;
509 varpool_node::analyze (void)
511 /* When reading back varpool at LTO time, we re-construct the queue in order
512 to have "needed" list right by inserting all needed nodes into varpool.
513 We however don't want to re-analyze already analyzed nodes. */
516 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
517 /* Compute the alignment early so function body expanders are
518 already informed about increased alignment. */
519 align_variable (decl
, 0);
522 resolve_alias (varpool_node::get (alias_target
));
523 else if (DECL_INITIAL (decl
))
524 record_references_in_initializer (decl
, analyzed
);
528 /* Assemble thunks and aliases associated to varpool node. */
531 varpool_node::assemble_aliases (void)
535 FOR_EACH_ALIAS (this, ref
)
537 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
538 do_assemble_alias (alias
->decl
,
539 DECL_ASSEMBLER_NAME (decl
));
540 alias
->assemble_aliases ();
544 /* Output one variable, if necessary. Return whether we output it. */
547 varpool_node::assemble_decl (void)
549 /* Aliases are outout when their target is produced or by
554 /* Constant pool is output from RTL land when the reference
555 survive till this level. */
556 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
559 /* Decls with VALUE_EXPR should not be in the varpool at all. They
560 are not real variables, but just info for debugging and codegen.
561 Unfortunately at the moment emutls is not updating varpool correctly
562 after turning real vars into value_expr vars. */
563 if (DECL_HAS_VALUE_EXPR_P (decl
)
564 && !targetm
.have_tls
)
567 /* Hard register vars do not need to be output. */
568 if (DECL_HARD_REGISTER (decl
))
571 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
572 && TREE_CODE (decl
) == VAR_DECL
573 && !DECL_HAS_VALUE_EXPR_P (decl
));
575 if (!in_other_partition
576 && !DECL_EXTERNAL (decl
))
579 assemble_variable (decl
, 0, 1, 0);
580 gcc_assert (TREE_ASM_WRITTEN (decl
));
581 gcc_assert (definition
);
583 /* After the parser has generated debugging information, augment
584 this information with any new location/etc information that may
585 have become available after the compilation proper. */
586 timevar_start (TV_PHASE_DBGINFO
);
587 debug_hooks
->late_global_decl (decl
);
588 timevar_stop (TV_PHASE_DBGINFO
);
595 /* Add NODE to queue starting at FIRST.
596 The queue is linked via AUX pointers and terminated by pointer to 1. */
599 enqueue_node (varpool_node
*node
, varpool_node
**first
)
603 gcc_checking_assert (*first
);
608 /* Optimization of function bodies might've rendered some variables as
609 unnecessary so we want to avoid these from being compiled. Re-do
610 reachability starting from variables that are either externally visible
611 or was referred from the asm output routines. */
614 symbol_table::remove_unreferenced_decls (void)
616 varpool_node
*next
, *node
;
617 varpool_node
*first
= (varpool_node
*)(void *)1;
620 hash_set
<varpool_node
*> referenced
;
626 fprintf (dump_file
, "Trivially needed variables:");
627 FOR_EACH_DEFINED_VARIABLE (node
)
630 && (!node
->can_remove_if_no_refs_p ()
631 /* We just expanded all function bodies. See if any of
632 them needed the variable. */
633 || DECL_RTL_SET_P (node
->decl
)))
635 enqueue_node (node
, &first
);
637 fprintf (dump_file
, " %s", node
->asm_name ());
640 while (first
!= (varpool_node
*)(void *)1)
643 first
= (varpool_node
*)first
->aux
;
645 if (node
->same_comdat_group
)
648 for (next
= node
->same_comdat_group
;
650 next
= next
->same_comdat_group
)
652 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
653 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
654 enqueue_node (vnext
, &first
);
657 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
659 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
661 && !vnode
->in_other_partition
662 && (!DECL_EXTERNAL (ref
->referred
->decl
)
665 enqueue_node (vnode
, &first
);
667 referenced
.add (node
);
671 fprintf (dump_file
, "\nRemoving variables:");
672 for (node
= first_defined_variable (); node
; node
= next
)
674 next
= next_defined_variable (node
);
675 if (!node
->aux
&& !node
->no_reorder
)
678 fprintf (dump_file
, " %s", node
->asm_name ());
679 if (referenced
.contains(node
))
680 node
->remove_initializer ();
687 fprintf (dump_file
, "\n");
690 /* For variables in named sections make sure get_variable_section
691 is called before we switch to those sections. Then section
692 conflicts between read-only and read-only requiring relocations
693 sections can be resolved. */
695 varpool_node::finalize_named_section_flags (void)
697 if (!TREE_ASM_WRITTEN (decl
)
699 && !in_other_partition
700 && !DECL_EXTERNAL (decl
)
701 && TREE_CODE (decl
) == VAR_DECL
702 && !DECL_HAS_VALUE_EXPR_P (decl
)
704 get_variable_section (decl
, false);
707 /* Output all variables enqueued to be assembled. */
709 symbol_table::output_variables (void)
711 bool changed
= false;
717 remove_unreferenced_decls ();
719 timevar_push (TV_VAROUT
);
721 FOR_EACH_VARIABLE (node
)
722 if (!node
->definition
723 && !DECL_HAS_VALUE_EXPR_P (node
->decl
)
724 && !DECL_HARD_REGISTER (node
->decl
))
725 assemble_undefined_decl (node
->decl
);
726 FOR_EACH_DEFINED_VARIABLE (node
)
728 /* Handled in output_in_order. */
729 if (node
->no_reorder
)
732 node
->finalize_named_section_flags ();
735 FOR_EACH_DEFINED_VARIABLE (node
)
737 /* Handled in output_in_order. */
738 if (node
->no_reorder
)
740 if (node
->assemble_decl ())
743 timevar_pop (TV_VAROUT
);
747 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
748 Extra name aliases are output whenever DECL is output. */
751 varpool_node::create_alias (tree alias
, tree decl
)
753 varpool_node
*alias_node
;
755 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
756 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
757 alias_node
= varpool_node::get_create (alias
);
758 alias_node
->alias
= true;
759 alias_node
->definition
= true;
760 alias_node
->alias_target
= decl
;
761 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
762 alias_node
->weakref
= true;
766 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
767 Extra name aliases are output whenever DECL is output. */
770 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
772 varpool_node
*alias_node
;
774 #ifndef ASM_OUTPUT_DEF
775 /* If aliases aren't supported by the assembler, fail. */
778 alias_node
= varpool_node::create_alias (alias
, decl
);
779 alias_node
->cpp_implicit_alias
= true;
781 /* Extra name alias mechanizm creates aliases really late
782 via DECL_ASSEMBLER_NAME mechanizm.
783 This is unfortunate because they are not going through the
784 standard channels. Ensure they get output. */
785 if (symtab
->cpp_implicit_aliases_done
)
786 alias_node
->resolve_alias (varpool_node::get_create (decl
));
790 /* Worker for call_for_symbol_and_aliases. */
793 varpool_node::call_for_symbol_and_aliases_1 (bool (*callback
) (varpool_node
*,
796 bool include_overwritable
)
800 FOR_EACH_ALIAS (this, ref
)
802 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
803 if (include_overwritable
804 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
805 if (alias
->call_for_symbol_and_aliases (callback
, data
,
806 include_overwritable
))