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 if (ENABLE_OFFLOADING
)
158 g
->have_offload
= true;
160 vec_safe_push (offload_vars
, decl
);
161 node
->force_output
= 1;
165 node
->register_symbol ();
169 /* Remove variable from symbol table. */
172 varpool_node::remove (void)
174 symtab
->call_varpool_removal_hooks (this);
177 lto_free_function_in_decl_state_for_node (this);
178 lto_file_data
= NULL
;
181 /* When streaming we can have multiple nodes associated with decl. */
182 if (symtab
->state
== LTO_STREAMING
)
184 /* Keep constructor when it may be used for folding. We remove
185 references to external variables before final compilation. */
186 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
187 && !ctor_useable_for_folding_p ())
188 remove_initializer ();
194 /* Remove node initializer when it is no longer needed. */
196 varpool_node::remove_initializer (void)
198 if (DECL_INITIAL (decl
)
199 && !DECL_IN_CONSTANT_POOL (decl
)
200 /* Keep vtables for BINFO folding. */
201 && !DECL_VIRTUAL_P (decl
)
202 /* FIXME: http://gcc.gnu.org/PR55395 */
203 && debug_info_level
== DINFO_LEVEL_NONE
204 /* When doing declaration merging we have duplicate
205 entries for given decl. Do not attempt to remove
206 the boides, or we will end up remiving
208 && symtab
->state
!= LTO_STREAMING
)
209 DECL_INITIAL (decl
) = error_mark_node
;
212 /* Dump given varpool node to F. */
214 varpool_node::dump (FILE *f
)
217 fprintf (f
, " Availability: %s\n",
218 symtab
->function_flags_ready
219 ? cgraph_availability_names
[get_availability ()]
221 fprintf (f
, " Varpool flags:");
222 if (DECL_INITIAL (decl
))
223 fprintf (f
, " initialized");
225 fprintf (f
, " output");
226 if (used_by_single_function
)
227 fprintf (f
, " used-by-single-function");
228 if (need_bounds_init
)
229 fprintf (f
, " need-bounds-init");
230 if (TREE_READONLY (decl
))
231 fprintf (f
, " read-only");
232 if (ctor_useable_for_folding_p ())
233 fprintf (f
, " const-value-known");
235 fprintf (f
, " write-only");
237 fprintf (f
, " tls-%s", tls_model_names
[tls_model
]);
242 /* Dump given varpool node to stderr. */
243 void varpool_node::debug (void)
245 varpool_node::dump (stderr
);
248 /* Dump the variable pool to F. */
250 varpool_node::dump_varpool (FILE *f
)
254 fprintf (f
, "variable pool:\n\n");
255 FOR_EACH_VARIABLE (node
)
259 /* Dump the variable pool to stderr. */
262 varpool_node::debug_varpool (void)
264 dump_varpool (stderr
);
267 /* Given an assembler name, lookup node. */
269 varpool_node::get_for_asmname (tree asmname
)
271 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
272 return dyn_cast
<varpool_node
*> (node
);
277 /* When doing LTO, read variable's constructor from disk if
278 it is not already present. */
281 varpool_node::get_constructor (void)
283 lto_file_decl_data
*file_data
;
284 const char *data
, *name
;
287 if (DECL_INITIAL (decl
) != error_mark_node
290 return DECL_INITIAL (decl
);
292 timevar_push (TV_IPA_LTO_CTORS_IN
);
294 file_data
= lto_file_data
;
295 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
297 /* We may have renamed the declaration, e.g., a static function. */
298 name
= lto_get_decl_name_mapping (file_data
, name
);
299 struct lto_in_decl_state
*decl_state
300 = lto_get_function_in_decl_state (file_data
, decl
);
302 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
303 name
, &len
, decl_state
->compressed
);
305 fatal_error (input_location
, "%s: section %s is missing",
306 file_data
->file_name
,
309 lto_input_variable_constructor (file_data
, this, data
);
310 gcc_assert (DECL_INITIAL (decl
) != error_mark_node
);
311 lto_stats
.num_function_bodies
++;
312 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
313 data
, len
, decl_state
->compressed
);
314 lto_free_function_in_decl_state_for_node (this);
315 timevar_pop (TV_IPA_LTO_CTORS_IN
);
316 return DECL_INITIAL (decl
);
319 /* Return true if variable has constructor that can be used for folding. */
322 varpool_node::ctor_useable_for_folding_p (void)
324 varpool_node
*real_node
= this;
326 if (real_node
->alias
&& real_node
->definition
)
327 real_node
= ultimate_alias_target ();
329 if (TREE_CODE (decl
) == CONST_DECL
330 || DECL_IN_CONSTANT_POOL (decl
))
332 if (TREE_THIS_VOLATILE (decl
))
335 /* If we do not have a constructor, we can't use it. */
336 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
337 && !real_node
->lto_file_data
)
340 /* Avoid attempts to load constructors that was not streamed. */
341 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
342 && real_node
->body_removed
)
345 /* Vtables are defined by their types and must match no matter of interposition
347 if (DECL_VIRTUAL_P (decl
))
349 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
350 classes not defined in the current TU so that it can refer
351 to them from typeinfo objects. Avoid returning NULL_TREE. */
352 return DECL_INITIAL (real_node
->decl
) != NULL
;
355 /* Alias of readonly variable is also readonly, since the variable is stored
356 in readonly memory. We also accept readonly aliases of non-readonly
357 locations assuming that user knows what he is asking for. */
358 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
361 /* Variables declared 'const' without an initializer
362 have zero as the initializer if they may not be
363 overridden at link or run time.
365 It is actually requirement for C++ compiler to optimize const variables
366 consistently. As a GNU extension, do not enfore this rule for user defined
367 weak variables, so we support interposition on:
368 static const int dummy = 0;
369 extern const int foo __attribute__((__weak__, __alias__("dummy")));
371 if ((!DECL_INITIAL (real_node
->decl
)
372 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
373 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
376 /* Variables declared `const' with an initializer are considered
377 to not be overwritable with different initializer by default.
379 ??? Previously we behaved so for scalar variables but not for array
384 /* If DECLARATION is constant variable and its initial value is known
385 (so we can do constant folding), return its constructor (DECL_INITIAL).
386 This may be an expression or NULL when DECL is initialized to 0.
387 Return ERROR_MARK_NODE otherwise.
389 In LTO this may actually trigger reading the constructor from disk.
390 For this reason varpool_ctor_useable_for_folding_p should be used when
391 the actual constructor value is not needed. */
394 ctor_for_folding (tree decl
)
396 varpool_node
*node
, *real_node
;
399 if (TREE_CODE (decl
) != VAR_DECL
400 && TREE_CODE (decl
) != CONST_DECL
)
401 return error_mark_node
;
403 /* Static constant bounds are created to be
404 used instead of constants and therefore
405 do not let folding it. */
406 if (POINTER_BOUNDS_P (decl
))
407 return error_mark_node
;
409 if (TREE_CODE (decl
) == CONST_DECL
410 || DECL_IN_CONSTANT_POOL (decl
))
411 return DECL_INITIAL (decl
);
413 if (TREE_THIS_VOLATILE (decl
))
414 return error_mark_node
;
416 /* Do not care about automatic variables. Those are never initialized
417 anyway, because gimplifier exapnds the code. */
418 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
420 gcc_assert (!TREE_PUBLIC (decl
));
421 return error_mark_node
;
424 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
426 real_node
= node
= varpool_node::get (decl
);
429 real_node
= node
->ultimate_alias_target ();
430 real_decl
= real_node
->decl
;
435 /* See if we are dealing with alias.
436 In most cases alias is just alternative symbol pointing to a given
437 constructor. This allows us to use interposition rules of DECL
438 constructor of REAL_NODE. However weakrefs are special by being just
439 alternative name of their target (if defined). */
440 if (decl
!= real_decl
)
442 gcc_assert (!DECL_INITIAL (decl
)
443 || (node
->alias
&& node
->get_alias_target () == real_node
)
444 || DECL_INITIAL (decl
) == error_mark_node
);
445 while (node
->transparent_alias
&& node
->analyzed
)
447 node
= node
->get_alias_target ();
452 if ((!DECL_VIRTUAL_P (real_decl
)
453 || DECL_INITIAL (real_decl
) == error_mark_node
454 || !DECL_INITIAL (real_decl
))
455 && (!node
|| !node
->ctor_useable_for_folding_p ()))
456 return error_mark_node
;
458 /* OK, we can return constructor. See if we need to fetch it from disk
460 if (DECL_INITIAL (real_decl
) != error_mark_node
462 return DECL_INITIAL (real_decl
);
463 return real_node
->get_constructor ();
466 /* Add the variable DECL to the varpool.
467 Unlike finalize_decl function is intended to be used
468 by middle end and allows insertion of new variable at arbitrary point
471 varpool_node::add (tree decl
)
474 varpool_node::finalize_decl (decl
);
475 node
= varpool_node::get_create (decl
);
476 symtab
->call_varpool_insertion_hooks (node
);
477 if (node
->externally_visible_p ())
478 node
->externally_visible
= true;
479 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
480 node
->no_reorder
= 1;
483 /* Return variable availability. See cgraph.h for description of individual
486 varpool_node::get_availability (void)
489 return AVAIL_NOT_AVAILABLE
;
490 if (!TREE_PUBLIC (decl
))
491 return AVAIL_AVAILABLE
;
492 if (DECL_IN_CONSTANT_POOL (decl
)
493 || DECL_VIRTUAL_P (decl
))
494 return AVAIL_AVAILABLE
;
495 if (transparent_alias
&& definition
)
497 enum availability avail
;
499 ultimate_alias_target (&avail
);
502 /* If the variable can be overwritten, return OVERWRITABLE. Takes
503 care of at least one notable extension - the COMDAT variables
504 used to share template instantiations in C++. */
505 if (decl_replaceable_p (decl
)
506 || DECL_EXTERNAL (decl
))
507 return AVAIL_INTERPOSABLE
;
508 return AVAIL_AVAILABLE
;
512 varpool_node::analyze (void)
514 /* When reading back varpool at LTO time, we re-construct the queue in order
515 to have "needed" list right by inserting all needed nodes into varpool.
516 We however don't want to re-analyze already analyzed nodes. */
519 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
520 /* Compute the alignment early so function body expanders are
521 already informed about increased alignment. */
522 align_variable (decl
, 0);
525 resolve_alias (varpool_node::get (alias_target
));
526 else if (DECL_INITIAL (decl
))
527 record_references_in_initializer (decl
, analyzed
);
531 /* Assemble thunks and aliases associated to varpool node. */
534 varpool_node::assemble_aliases (void)
538 FOR_EACH_ALIAS (this, ref
)
540 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
541 if (!alias
->transparent_alias
)
542 do_assemble_alias (alias
->decl
,
543 DECL_ASSEMBLER_NAME (decl
));
544 alias
->assemble_aliases ();
548 /* Output one variable, if necessary. Return whether we output it. */
551 varpool_node::assemble_decl (void)
553 /* Aliases are outout when their target is produced or by
558 /* Constant pool is output from RTL land when the reference
559 survive till this level. */
560 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
563 /* Decls with VALUE_EXPR should not be in the varpool at all. They
564 are not real variables, but just info for debugging and codegen.
565 Unfortunately at the moment emutls is not updating varpool correctly
566 after turning real vars into value_expr vars. */
567 if (DECL_HAS_VALUE_EXPR_P (decl
)
568 && !targetm
.have_tls
)
571 /* Hard register vars do not need to be output. */
572 if (DECL_HARD_REGISTER (decl
))
575 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
576 && TREE_CODE (decl
) == VAR_DECL
577 && !DECL_HAS_VALUE_EXPR_P (decl
));
579 if (!in_other_partition
580 && !DECL_EXTERNAL (decl
))
583 assemble_variable (decl
, 0, 1, 0);
584 gcc_assert (TREE_ASM_WRITTEN (decl
));
585 gcc_assert (definition
);
587 /* After the parser has generated debugging information, augment
588 this information with any new location/etc information that may
589 have become available after the compilation proper. */
590 timevar_start (TV_PHASE_DBGINFO
);
591 debug_hooks
->late_global_decl (decl
);
592 timevar_stop (TV_PHASE_DBGINFO
);
599 /* Add NODE to queue starting at FIRST.
600 The queue is linked via AUX pointers and terminated by pointer to 1. */
603 enqueue_node (varpool_node
*node
, varpool_node
**first
)
607 gcc_checking_assert (*first
);
612 /* Optimization of function bodies might've rendered some variables as
613 unnecessary so we want to avoid these from being compiled. Re-do
614 reachability starting from variables that are either externally visible
615 or was referred from the asm output routines. */
618 symbol_table::remove_unreferenced_decls (void)
620 varpool_node
*next
, *node
;
621 varpool_node
*first
= (varpool_node
*)(void *)1;
624 hash_set
<varpool_node
*> referenced
;
630 fprintf (dump_file
, "Trivially needed variables:");
631 FOR_EACH_DEFINED_VARIABLE (node
)
634 && (!node
->can_remove_if_no_refs_p ()
635 /* We just expanded all function bodies. See if any of
636 them needed the variable. */
637 || DECL_RTL_SET_P (node
->decl
)))
639 enqueue_node (node
, &first
);
641 fprintf (dump_file
, " %s", node
->asm_name ());
644 while (first
!= (varpool_node
*)(void *)1)
647 first
= (varpool_node
*)first
->aux
;
649 if (node
->same_comdat_group
)
652 for (next
= node
->same_comdat_group
;
654 next
= next
->same_comdat_group
)
656 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
657 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
658 enqueue_node (vnext
, &first
);
661 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
663 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
665 && !vnode
->in_other_partition
666 && (!DECL_EXTERNAL (ref
->referred
->decl
)
669 enqueue_node (vnode
, &first
);
672 referenced
.add (vnode
);
673 while (vnode
&& vnode
->alias
&& vnode
->definition
)
675 vnode
= vnode
->get_alias_target ();
676 referenced
.add (vnode
);
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 #ifdef ACCEL_COMPILER
752 /* Do not assemble "omp declare target link" vars. */
753 if (DECL_HAS_VALUE_EXPR_P (node
->decl
)
754 && lookup_attribute ("omp declare target link",
755 DECL_ATTRIBUTES (node
->decl
)))
758 if (node
->assemble_decl ())
761 timevar_pop (TV_VAROUT
);
765 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
766 Extra name aliases are output whenever DECL is output. */
769 varpool_node::create_alias (tree alias
, tree decl
)
771 varpool_node
*alias_node
;
773 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
774 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
775 alias_node
= varpool_node::get_create (alias
);
776 alias_node
->alias
= true;
777 alias_node
->definition
= true;
778 alias_node
->alias_target
= decl
;
779 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
780 alias_node
->weakref
= alias_node
->transparent_alias
= true;
784 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
785 Extra name aliases are output whenever DECL is output. */
788 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
790 varpool_node
*alias_node
;
792 #ifndef ASM_OUTPUT_DEF
793 /* If aliases aren't supported by the assembler, fail. */
796 alias_node
= varpool_node::create_alias (alias
, decl
);
797 alias_node
->cpp_implicit_alias
= true;
799 /* Extra name alias mechanizm creates aliases really late
800 via DECL_ASSEMBLER_NAME mechanizm.
801 This is unfortunate because they are not going through the
802 standard channels. Ensure they get output. */
803 if (symtab
->cpp_implicit_aliases_done
)
804 alias_node
->resolve_alias (varpool_node::get_create (decl
));
808 /* Worker for call_for_symbol_and_aliases. */
811 varpool_node::call_for_symbol_and_aliases_1 (bool (*callback
) (varpool_node
*,
814 bool include_overwritable
)
818 FOR_EACH_ALIAS (this, ref
)
820 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
821 if (include_overwritable
822 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
823 if (alias
->call_for_symbol_and_aliases (callback
, data
,
824 include_overwritable
))