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"
31 #include "diagnostic-core.h"
33 #include "fold-const.h"
35 #include "langhooks.h"
42 const char * const tls_model_names
[]={"none", "emulated",
43 "global-dynamic", "local-dynamic",
44 "initial-exec", "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 /* Register HOOK to be called with DATA on each removed node. */
54 varpool_node_hook_list
*
55 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
57 varpool_node_hook_list
*entry
;
58 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
60 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
70 /* Remove ENTRY from the list of hooks called on removing nodes. */
72 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
74 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
82 /* Call all node removal hooks. */
84 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
86 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
89 entry
->hook (node
, entry
->data
);
94 /* Register HOOK to be called with DATA on each inserted node. */
95 varpool_node_hook_list
*
96 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
98 varpool_node_hook_list
*entry
;
99 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
101 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
111 /* Remove ENTRY from the list of hooks called on inserted nodes. */
113 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
115 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
117 while (*ptr
!= entry
)
123 /* Call all node insertion hooks. */
125 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
127 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
130 entry
->hook (node
, entry
->data
);
135 /* Allocate new callgraph node and insert it into basic data structures. */
138 varpool_node::create_empty (void)
140 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
141 node
->type
= SYMTAB_VARIABLE
;
145 /* Return varpool node assigned to DECL. Create new one when needed. */
147 varpool_node::get_create (tree decl
)
149 varpool_node
*node
= varpool_node::get (decl
);
150 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
154 node
= varpool_node::create_empty ();
157 if ((flag_openacc
|| flag_openmp
) && !DECL_EXTERNAL (decl
)
158 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
)))
160 node
->offloadable
= 1;
161 #ifdef ENABLE_OFFLOADING
162 g
->have_offload
= true;
164 vec_safe_push (offload_vars
, decl
);
165 node
->force_output
= 1;
169 node
->register_symbol ();
173 /* Remove variable from symbol table. */
176 varpool_node::remove (void)
178 symtab
->call_varpool_removal_hooks (this);
181 lto_free_function_in_decl_state_for_node (this);
182 lto_file_data
= NULL
;
185 /* When streaming we can have multiple nodes associated with decl. */
186 if (symtab
->state
== LTO_STREAMING
)
188 /* Keep constructor when it may be used for folding. We remove
189 references to external variables before final compilation. */
190 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
191 && !ctor_useable_for_folding_p ())
192 remove_initializer ();
198 /* Remove node initializer when it is no longer needed. */
200 varpool_node::remove_initializer (void)
202 if (DECL_INITIAL (decl
)
203 && !DECL_IN_CONSTANT_POOL (decl
)
204 /* Keep vtables for BINFO folding. */
205 && !DECL_VIRTUAL_P (decl
)
206 /* FIXME: http://gcc.gnu.org/PR55395 */
207 && debug_info_level
== DINFO_LEVEL_NONE
208 /* When doing declaration merging we have duplicate
209 entries for given decl. Do not attempt to remove
210 the boides, or we will end up remiving
212 && symtab
->state
!= LTO_STREAMING
)
213 DECL_INITIAL (decl
) = error_mark_node
;
216 /* Dump given varpool node to F. */
218 varpool_node::dump (FILE *f
)
221 fprintf (f
, " Availability: %s\n",
222 symtab
->function_flags_ready
223 ? cgraph_availability_names
[get_availability ()]
225 fprintf (f
, " Varpool flags:");
226 if (DECL_INITIAL (decl
))
227 fprintf (f
, " initialized");
229 fprintf (f
, " output");
230 if (used_by_single_function
)
231 fprintf (f
, " used-by-single-function");
232 if (need_bounds_init
)
233 fprintf (f
, " need-bounds-init");
234 if (TREE_READONLY (decl
))
235 fprintf (f
, " read-only");
236 if (ctor_useable_for_folding_p ())
237 fprintf (f
, " const-value-known");
239 fprintf (f
, " write-only");
241 fprintf (f
, " tls-%s", tls_model_names
[tls_model
]);
246 /* Dump given varpool node to stderr. */
247 void varpool_node::debug (void)
249 varpool_node::dump (stderr
);
252 /* Dump the variable pool to F. */
254 varpool_node::dump_varpool (FILE *f
)
258 fprintf (f
, "variable pool:\n\n");
259 FOR_EACH_VARIABLE (node
)
263 /* Dump the variable pool to stderr. */
266 varpool_node::debug_varpool (void)
268 dump_varpool (stderr
);
271 /* Given an assembler name, lookup node. */
273 varpool_node::get_for_asmname (tree asmname
)
275 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
276 return dyn_cast
<varpool_node
*> (node
);
281 /* When doing LTO, read variable's constructor from disk if
282 it is not already present. */
285 varpool_node::get_constructor (void)
287 lto_file_decl_data
*file_data
;
288 const char *data
, *name
;
291 if (DECL_INITIAL (decl
) != error_mark_node
294 return DECL_INITIAL (decl
);
296 timevar_push (TV_IPA_LTO_CTORS_IN
);
298 file_data
= lto_file_data
;
299 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
301 /* We may have renamed the declaration, e.g., a static function. */
302 name
= lto_get_decl_name_mapping (file_data
, name
);
304 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
307 fatal_error (input_location
, "%s: section %s is missing",
308 file_data
->file_name
,
311 lto_input_variable_constructor (file_data
, this, data
);
312 gcc_assert (DECL_INITIAL (decl
) != error_mark_node
);
313 lto_stats
.num_function_bodies
++;
314 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
316 lto_free_function_in_decl_state_for_node (this);
317 timevar_pop (TV_IPA_LTO_CTORS_IN
);
318 return DECL_INITIAL (decl
);
321 /* Return true if variable has constructor that can be used for folding. */
324 varpool_node::ctor_useable_for_folding_p (void)
326 varpool_node
*real_node
= this;
328 if (real_node
->alias
&& real_node
->definition
)
329 real_node
= ultimate_alias_target ();
331 if (TREE_CODE (decl
) == CONST_DECL
332 || DECL_IN_CONSTANT_POOL (decl
))
334 if (TREE_THIS_VOLATILE (decl
))
337 /* If we do not have a constructor, we can't use it. */
338 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
339 && !real_node
->lto_file_data
)
342 /* Avoid attempts to load constructors that was not streamed. */
343 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
344 && real_node
->body_removed
)
347 /* Vtables are defined by their types and must match no matter of interposition
349 if (DECL_VIRTUAL_P (decl
))
351 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
352 classes not defined in the current TU so that it can refer
353 to them from typeinfo objects. Avoid returning NULL_TREE. */
354 return DECL_INITIAL (real_node
->decl
) != NULL
;
357 /* Alias of readonly variable is also readonly, since the variable is stored
358 in readonly memory. We also accept readonly aliases of non-readonly
359 locations assuming that user knows what he is asking for. */
360 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
363 /* Variables declared 'const' without an initializer
364 have zero as the initializer if they may not be
365 overridden at link or run time.
367 It is actually requirement for C++ compiler to optimize const variables
368 consistently. As a GNU extension, do not enfore this rule for user defined
369 weak variables, so we support interposition on:
370 static const int dummy = 0;
371 extern const int foo __attribute__((__weak__, __alias__("dummy")));
373 if ((!DECL_INITIAL (real_node
->decl
)
374 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
375 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
378 /* Variables declared `const' with an initializer are considered
379 to not be overwritable with different initializer by default.
381 ??? Previously we behaved so for scalar variables but not for array
386 /* If DECLARATION is constant variable and its initial value is known
387 (so we can do constant folding), return its constructor (DECL_INITIAL).
388 This may be an expression or NULL when DECL is initialized to 0.
389 Return ERROR_MARK_NODE otherwise.
391 In LTO this may actually trigger reading the constructor from disk.
392 For this reason varpool_ctor_useable_for_folding_p should be used when
393 the actual constructor value is not needed. */
396 ctor_for_folding (tree decl
)
398 varpool_node
*node
, *real_node
;
401 if (TREE_CODE (decl
) != VAR_DECL
402 && TREE_CODE (decl
) != CONST_DECL
)
403 return error_mark_node
;
405 /* Static constant bounds are created to be
406 used instead of constants and therefore
407 do not let folding it. */
408 if (POINTER_BOUNDS_P (decl
))
409 return error_mark_node
;
411 if (TREE_CODE (decl
) == CONST_DECL
412 || DECL_IN_CONSTANT_POOL (decl
))
413 return DECL_INITIAL (decl
);
415 if (TREE_THIS_VOLATILE (decl
))
416 return error_mark_node
;
418 /* Do not care about automatic variables. Those are never initialized
419 anyway, because gimplifier exapnds the code. */
420 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
422 gcc_assert (!TREE_PUBLIC (decl
));
423 return error_mark_node
;
426 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
428 real_node
= node
= varpool_node::get (decl
);
431 real_node
= node
->ultimate_alias_target ();
432 real_decl
= real_node
->decl
;
437 /* See if we are dealing with alias.
438 In most cases alias is just alternative symbol pointing to a given
439 constructor. This allows us to use interposition rules of DECL
440 constructor of REAL_NODE. However weakrefs are special by being just
441 alternative name of their target (if defined). */
442 if (decl
!= real_decl
)
444 gcc_assert (!DECL_INITIAL (decl
)
445 || (node
->alias
&& node
->get_alias_target () == real_node
)
446 || DECL_INITIAL (decl
) == error_mark_node
);
449 node
= node
->get_alias_target ();
454 if ((!DECL_VIRTUAL_P (real_decl
)
455 || DECL_INITIAL (real_decl
) == error_mark_node
456 || !DECL_INITIAL (real_decl
))
457 && (!node
|| !node
->ctor_useable_for_folding_p ()))
458 return error_mark_node
;
460 /* OK, we can return constructor. See if we need to fetch it from disk
462 if (DECL_INITIAL (real_decl
) != error_mark_node
464 return DECL_INITIAL (real_decl
);
465 return real_node
->get_constructor ();
468 /* Add the variable DECL to the varpool.
469 Unlike finalize_decl function is intended to be used
470 by middle end and allows insertion of new variable at arbitrary point
473 varpool_node::add (tree decl
)
476 varpool_node::finalize_decl (decl
);
477 node
= varpool_node::get_create (decl
);
478 symtab
->call_varpool_insertion_hooks (node
);
479 if (node
->externally_visible_p ())
480 node
->externally_visible
= true;
481 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
482 node
->no_reorder
= 1;
485 /* Return variable availability. See cgraph.h for description of individual
488 varpool_node::get_availability (void)
491 return AVAIL_NOT_AVAILABLE
;
492 if (!TREE_PUBLIC (decl
))
493 return AVAIL_AVAILABLE
;
494 if (DECL_IN_CONSTANT_POOL (decl
)
495 || DECL_VIRTUAL_P (decl
))
496 return AVAIL_AVAILABLE
;
497 if (alias
&& weakref
)
499 enum availability avail
;
501 ultimate_alias_target (&avail
)->get_availability ();
504 /* If the variable can be overwritten, return OVERWRITABLE. Takes
505 care of at least one notable extension - the COMDAT variables
506 used to share template instantiations in C++. */
507 if (decl_replaceable_p (decl
)
508 || DECL_EXTERNAL (decl
))
509 return AVAIL_INTERPOSABLE
;
510 return AVAIL_AVAILABLE
;
514 varpool_node::analyze (void)
516 /* When reading back varpool at LTO time, we re-construct the queue in order
517 to have "needed" list right by inserting all needed nodes into varpool.
518 We however don't want to re-analyze already analyzed nodes. */
521 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
522 /* Compute the alignment early so function body expanders are
523 already informed about increased alignment. */
524 align_variable (decl
, 0);
527 resolve_alias (varpool_node::get (alias_target
));
528 else if (DECL_INITIAL (decl
))
529 record_references_in_initializer (decl
, analyzed
);
533 /* Assemble thunks and aliases associated to varpool node. */
536 varpool_node::assemble_aliases (void)
540 FOR_EACH_ALIAS (this, ref
)
542 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
543 do_assemble_alias (alias
->decl
,
544 DECL_ASSEMBLER_NAME (decl
));
545 alias
->assemble_aliases ();
549 /* Output one variable, if necessary. Return whether we output it. */
552 varpool_node::assemble_decl (void)
554 /* Aliases are outout when their target is produced or by
559 /* Constant pool is output from RTL land when the reference
560 survive till this level. */
561 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
564 /* Decls with VALUE_EXPR should not be in the varpool at all. They
565 are not real variables, but just info for debugging and codegen.
566 Unfortunately at the moment emutls is not updating varpool correctly
567 after turning real vars into value_expr vars. */
568 if (DECL_HAS_VALUE_EXPR_P (decl
)
569 && !targetm
.have_tls
)
572 /* Hard register vars do not need to be output. */
573 if (DECL_HARD_REGISTER (decl
))
576 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
577 && TREE_CODE (decl
) == VAR_DECL
578 && !DECL_HAS_VALUE_EXPR_P (decl
));
580 if (!in_other_partition
581 && !DECL_EXTERNAL (decl
))
584 assemble_variable (decl
, 0, 1, 0);
585 gcc_assert (TREE_ASM_WRITTEN (decl
));
586 gcc_assert (definition
);
588 /* After the parser has generated debugging information, augment
589 this information with any new location/etc information that may
590 have become available after the compilation proper. */
591 timevar_start (TV_PHASE_DBGINFO
);
592 debug_hooks
->late_global_decl (decl
);
593 timevar_stop (TV_PHASE_DBGINFO
);
600 /* Add NODE to queue starting at FIRST.
601 The queue is linked via AUX pointers and terminated by pointer to 1. */
604 enqueue_node (varpool_node
*node
, varpool_node
**first
)
608 gcc_checking_assert (*first
);
613 /* Optimization of function bodies might've rendered some variables as
614 unnecessary so we want to avoid these from being compiled. Re-do
615 reachability starting from variables that are either externally visible
616 or was referred from the asm output routines. */
619 symbol_table::remove_unreferenced_decls (void)
621 varpool_node
*next
, *node
;
622 varpool_node
*first
= (varpool_node
*)(void *)1;
625 hash_set
<varpool_node
*> referenced
;
631 fprintf (dump_file
, "Trivially needed variables:");
632 FOR_EACH_DEFINED_VARIABLE (node
)
635 && (!node
->can_remove_if_no_refs_p ()
636 /* We just expanded all function bodies. See if any of
637 them needed the variable. */
638 || DECL_RTL_SET_P (node
->decl
)))
640 enqueue_node (node
, &first
);
642 fprintf (dump_file
, " %s", node
->asm_name ());
645 while (first
!= (varpool_node
*)(void *)1)
648 first
= (varpool_node
*)first
->aux
;
650 if (node
->same_comdat_group
)
653 for (next
= node
->same_comdat_group
;
655 next
= next
->same_comdat_group
)
657 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
658 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
659 enqueue_node (vnext
, &first
);
662 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
664 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
666 && !vnode
->in_other_partition
667 && (!DECL_EXTERNAL (ref
->referred
->decl
)
670 enqueue_node (vnode
, &first
);
672 referenced
.add (node
);
676 fprintf (dump_file
, "\nRemoving variables:");
677 for (node
= first_defined_variable (); node
; node
= next
)
679 next
= next_defined_variable (node
);
680 if (!node
->aux
&& !node
->no_reorder
)
683 fprintf (dump_file
, " %s", node
->asm_name ());
684 if (referenced
.contains(node
))
685 node
->remove_initializer ();
692 fprintf (dump_file
, "\n");
695 /* For variables in named sections make sure get_variable_section
696 is called before we switch to those sections. Then section
697 conflicts between read-only and read-only requiring relocations
698 sections can be resolved. */
700 varpool_node::finalize_named_section_flags (void)
702 if (!TREE_ASM_WRITTEN (decl
)
704 && !in_other_partition
705 && !DECL_EXTERNAL (decl
)
706 && TREE_CODE (decl
) == VAR_DECL
707 && !DECL_HAS_VALUE_EXPR_P (decl
)
709 get_variable_section (decl
, false);
712 /* Output all variables enqueued to be assembled. */
714 symbol_table::output_variables (void)
716 bool changed
= false;
722 remove_unreferenced_decls ();
724 timevar_push (TV_VAROUT
);
726 FOR_EACH_VARIABLE (node
)
727 if (!node
->definition
728 && !DECL_HAS_VALUE_EXPR_P (node
->decl
)
729 && !DECL_HARD_REGISTER (node
->decl
))
730 assemble_undefined_decl (node
->decl
);
731 FOR_EACH_DEFINED_VARIABLE (node
)
733 /* Handled in output_in_order. */
734 if (node
->no_reorder
)
737 node
->finalize_named_section_flags ();
740 FOR_EACH_DEFINED_VARIABLE (node
)
742 /* Handled in output_in_order. */
743 if (node
->no_reorder
)
745 if (node
->assemble_decl ())
748 timevar_pop (TV_VAROUT
);
752 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
753 Extra name aliases are output whenever DECL is output. */
756 varpool_node::create_alias (tree alias
, tree decl
)
758 varpool_node
*alias_node
;
760 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
761 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
762 alias_node
= varpool_node::get_create (alias
);
763 alias_node
->alias
= true;
764 alias_node
->definition
= true;
765 alias_node
->alias_target
= decl
;
766 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
767 alias_node
->weakref
= true;
771 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
772 Extra name aliases are output whenever DECL is output. */
775 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
777 varpool_node
*alias_node
;
779 #ifndef ASM_OUTPUT_DEF
780 /* If aliases aren't supported by the assembler, fail. */
783 alias_node
= varpool_node::create_alias (alias
, decl
);
784 alias_node
->cpp_implicit_alias
= true;
786 /* Extra name alias mechanizm creates aliases really late
787 via DECL_ASSEMBLER_NAME mechanizm.
788 This is unfortunate because they are not going through the
789 standard channels. Ensure they get output. */
790 if (symtab
->cpp_implicit_aliases_done
)
791 alias_node
->resolve_alias (varpool_node::get_create (decl
));
795 /* Worker for call_for_symbol_and_aliases. */
798 varpool_node::call_for_symbol_and_aliases_1 (bool (*callback
) (varpool_node
*,
801 bool include_overwritable
)
805 FOR_EACH_ALIAS (this, ref
)
807 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
808 if (include_overwritable
809 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
810 if (alias
->call_for_symbol_and_aliases (callback
, data
,
811 include_overwritable
))