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"
28 #include "double-int.h"
35 #include "fold-const.h"
38 #include "basic-block.h"
41 #include "plugin-api.h"
42 #include "hard-reg-set.h"
47 #include "langhooks.h"
48 #include "diagnostic-core.h"
53 #include "gimple-expr.h"
55 #include "tree-ssa-alias.h"
57 #include "lto-streamer.h"
61 const char * const tls_model_names
[]={"none", "tls-emulated", "tls-real",
62 "tls-global-dynamic", "tls-local-dynamic",
63 "tls-initial-exec", "tls-local-exec"};
65 /* List of hooks triggered on varpool_node events. */
66 struct varpool_node_hook_list
{
67 varpool_node_hook hook
;
69 struct varpool_node_hook_list
*next
;
72 /* Register HOOK to be called with DATA on each removed node. */
73 varpool_node_hook_list
*
74 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
76 varpool_node_hook_list
*entry
;
77 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
79 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
89 /* Remove ENTRY from the list of hooks called on removing nodes. */
91 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
93 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
101 /* Call all node removal hooks. */
103 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
105 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
108 entry
->hook (node
, entry
->data
);
113 /* Register HOOK to be called with DATA on each inserted node. */
114 varpool_node_hook_list
*
115 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
117 varpool_node_hook_list
*entry
;
118 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
120 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
130 /* Remove ENTRY from the list of hooks called on inserted nodes. */
132 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
134 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
136 while (*ptr
!= entry
)
142 /* Call all node insertion hooks. */
144 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
146 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
149 entry
->hook (node
, entry
->data
);
154 /* Allocate new callgraph node and insert it into basic data structures. */
157 varpool_node::create_empty (void)
159 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
160 node
->type
= SYMTAB_VARIABLE
;
164 /* Return varpool node assigned to DECL. Create new one when needed. */
166 varpool_node::get_create (tree decl
)
168 varpool_node
*node
= varpool_node::get (decl
);
169 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
173 node
= varpool_node::create_empty ();
176 if ((flag_openacc
|| flag_openmp
)
177 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
)))
179 node
->offloadable
= 1;
180 #ifdef ENABLE_OFFLOADING
181 g
->have_offload
= true;
183 vec_safe_push (offload_vars
, decl
);
184 node
->force_output
= 1;
188 node
->register_symbol ();
192 /* Remove variable from symbol table. */
195 varpool_node::remove (void)
197 symtab
->call_varpool_removal_hooks (this);
200 /* When streaming we can have multiple nodes associated with decl. */
201 if (symtab
->state
== LTO_STREAMING
)
203 /* Keep constructor when it may be used for folding. We remove
204 references to external variables before final compilation. */
205 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
206 && !ctor_useable_for_folding_p ())
207 remove_initializer ();
211 /* Remove node initializer when it is no longer needed. */
213 varpool_node::remove_initializer (void)
215 if (DECL_INITIAL (decl
)
216 && !DECL_IN_CONSTANT_POOL (decl
)
217 /* Keep vtables for BINFO folding. */
218 && !DECL_VIRTUAL_P (decl
)
219 /* FIXME: http://gcc.gnu.org/PR55395 */
220 && debug_info_level
== DINFO_LEVEL_NONE
221 /* When doing declaration merging we have duplicate
222 entries for given decl. Do not attempt to remove
223 the boides, or we will end up remiving
225 && symtab
->state
!= LTO_STREAMING
)
226 DECL_INITIAL (decl
) = error_mark_node
;
229 /* Dump given varpool node to F. */
231 varpool_node::dump (FILE *f
)
234 fprintf (f
, " Availability: %s\n",
235 symtab
->function_flags_ready
236 ? cgraph_availability_names
[get_availability ()]
238 fprintf (f
, " Varpool flags:");
239 if (DECL_INITIAL (decl
))
240 fprintf (f
, " initialized");
242 fprintf (f
, " output");
243 if (used_by_single_function
)
244 fprintf (f
, " used-by-single-function");
245 if (need_bounds_init
)
246 fprintf (f
, " need-bounds-init");
247 if (TREE_READONLY (decl
))
248 fprintf (f
, " read-only");
249 if (ctor_useable_for_folding_p ())
250 fprintf (f
, " const-value-known");
252 fprintf (f
, " write-only");
254 fprintf (f
, " %s", tls_model_names
[tls_model
]);
259 /* Dump given varpool node to stderr. */
260 void varpool_node::debug (void)
262 varpool_node::dump (stderr
);
265 /* Dump the variable pool to F. */
267 varpool_node::dump_varpool (FILE *f
)
271 fprintf (f
, "variable pool:\n\n");
272 FOR_EACH_VARIABLE (node
)
276 /* Dump the variable pool to stderr. */
279 varpool_node::debug_varpool (void)
281 dump_varpool (stderr
);
284 /* Given an assembler name, lookup node. */
286 varpool_node::get_for_asmname (tree asmname
)
288 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
289 return dyn_cast
<varpool_node
*> (node
);
294 /* When doing LTO, read variable's constructor from disk if
295 it is not already present. */
298 varpool_node::get_constructor (void)
300 lto_file_decl_data
*file_data
;
301 const char *data
, *name
;
304 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 ("%s: section %s is missing",
320 file_data
->file_name
,
323 lto_input_variable_constructor (file_data
, this, data
);
324 lto_stats
.num_function_bodies
++;
325 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
327 lto_free_function_in_decl_state_for_node (this);
328 timevar_pop (TV_IPA_LTO_CTORS_IN
);
329 return DECL_INITIAL (decl
);
332 /* Return true if variable has constructor that can be used for folding. */
335 varpool_node::ctor_useable_for_folding_p (void)
337 varpool_node
*real_node
= this;
339 if (real_node
->alias
&& real_node
->definition
)
340 real_node
= ultimate_alias_target ();
342 if (TREE_CODE (decl
) == CONST_DECL
343 || DECL_IN_CONSTANT_POOL (decl
))
345 if (TREE_THIS_VOLATILE (decl
))
348 /* If we do not have a constructor, we can't use it. */
349 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
350 && !real_node
->lto_file_data
)
353 /* Avoid attempts to load constructors that was not streamed. */
354 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
355 && real_node
->body_removed
)
358 /* Vtables are defined by their types and must match no matter of interposition
360 if (DECL_VIRTUAL_P (decl
))
362 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
363 classes not defined in the current TU so that it can refer
364 to them from typeinfo objects. Avoid returning NULL_TREE. */
365 return DECL_INITIAL (real_node
->decl
) != NULL
;
368 /* Alias of readonly variable is also readonly, since the variable is stored
369 in readonly memory. We also accept readonly aliases of non-readonly
370 locations assuming that user knows what he is asking for. */
371 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
374 /* Variables declared 'const' without an initializer
375 have zero as the initializer if they may not be
376 overridden at link or run time.
378 It is actually requirement for C++ compiler to optimize const variables
379 consistently. As a GNU extension, do not enfore this rule for user defined
380 weak variables, so we support interposition on:
381 static const int dummy = 0;
382 extern const int foo __attribute__((__weak__, __alias__("dummy")));
384 if ((!DECL_INITIAL (real_node
->decl
)
385 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
386 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
389 /* Variables declared `const' with an initializer are considered
390 to not be overwritable with different initializer by default.
392 ??? Previously we behaved so for scalar variables but not for array
397 /* If DECLARATION is constant variable and its initial value is known
398 (so we can do constant folding), return its constructor (DECL_INITIAL).
399 This may be an expression or NULL when DECL is initialized to 0.
400 Return ERROR_MARK_NODE otherwise.
402 In LTO this may actually trigger reading the constructor from disk.
403 For this reason varpool_ctor_useable_for_folding_p should be used when
404 the actual constructor value is not needed. */
407 ctor_for_folding (tree decl
)
409 varpool_node
*node
, *real_node
;
412 if (TREE_CODE (decl
) != VAR_DECL
413 && TREE_CODE (decl
) != CONST_DECL
)
414 return error_mark_node
;
416 /* Static constant bounds are created to be
417 used instead of constants and therefore
418 do not let folding it. */
419 if (POINTER_BOUNDS_P (decl
))
420 return error_mark_node
;
422 if (TREE_CODE (decl
) == CONST_DECL
423 || DECL_IN_CONSTANT_POOL (decl
))
424 return DECL_INITIAL (decl
);
426 if (TREE_THIS_VOLATILE (decl
))
427 return error_mark_node
;
429 /* Do not care about automatic variables. Those are never initialized
430 anyway, because gimplifier exapnds the code. */
431 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
433 gcc_assert (!TREE_PUBLIC (decl
));
434 return error_mark_node
;
437 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
439 real_node
= node
= varpool_node::get (decl
);
442 real_node
= node
->ultimate_alias_target ();
443 real_decl
= real_node
->decl
;
448 /* See if we are dealing with alias.
449 In most cases alias is just alternative symbol pointing to a given
450 constructor. This allows us to use interposition rules of DECL
451 constructor of REAL_NODE. However weakrefs are special by being just
452 alternative name of their target (if defined). */
453 if (decl
!= real_decl
)
455 gcc_assert (!DECL_INITIAL (decl
)
456 || (node
->alias
&& node
->get_alias_target () == real_node
)
457 || DECL_INITIAL (decl
) == error_mark_node
);
460 node
= node
->get_alias_target ();
465 if ((!DECL_VIRTUAL_P (real_decl
)
466 || DECL_INITIAL (real_decl
) == error_mark_node
467 || !DECL_INITIAL (real_decl
))
468 && (!node
|| !node
->ctor_useable_for_folding_p ()))
469 return error_mark_node
;
471 /* OK, we can return constructor. See if we need to fetch it from disk
473 if (DECL_INITIAL (real_decl
) != error_mark_node
475 return DECL_INITIAL (real_decl
);
476 return real_node
->get_constructor ();
479 /* Add the variable DECL to the varpool.
480 Unlike finalize_decl function is intended to be used
481 by middle end and allows insertion of new variable at arbitrary point
484 varpool_node::add (tree decl
)
487 varpool_node::finalize_decl (decl
);
488 node
= varpool_node::get_create (decl
);
489 symtab
->call_varpool_insertion_hooks (node
);
490 if (node
->externally_visible_p ())
491 node
->externally_visible
= true;
492 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
493 node
->no_reorder
= 1;
496 /* Return variable availability. See cgraph.h for description of individual
499 varpool_node::get_availability (void)
502 return AVAIL_NOT_AVAILABLE
;
503 if (!TREE_PUBLIC (decl
))
504 return AVAIL_AVAILABLE
;
505 if (DECL_IN_CONSTANT_POOL (decl
)
506 || DECL_VIRTUAL_P (decl
))
507 return AVAIL_AVAILABLE
;
508 if (alias
&& weakref
)
510 enum availability avail
;
512 ultimate_alias_target (&avail
)->get_availability ();
515 /* If the variable can be overwritten, return OVERWRITABLE. Takes
516 care of at least one notable extension - the COMDAT variables
517 used to share template instantiations in C++. */
518 if (decl_replaceable_p (decl
)
519 || DECL_EXTERNAL (decl
))
520 return AVAIL_INTERPOSABLE
;
521 return AVAIL_AVAILABLE
;
525 varpool_node::analyze (void)
527 /* When reading back varpool at LTO time, we re-construct the queue in order
528 to have "needed" list right by inserting all needed nodes into varpool.
529 We however don't want to re-analyze already analyzed nodes. */
532 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
533 /* Compute the alignment early so function body expanders are
534 already informed about increased alignment. */
535 align_variable (decl
, 0);
538 resolve_alias (varpool_node::get (alias_target
));
539 else if (DECL_INITIAL (decl
))
540 record_references_in_initializer (decl
, analyzed
);
544 /* Assemble thunks and aliases associated to varpool node. */
547 varpool_node::assemble_aliases (void)
551 FOR_EACH_ALIAS (this, ref
)
553 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
554 do_assemble_alias (alias
->decl
,
555 DECL_ASSEMBLER_NAME (decl
));
556 alias
->assemble_aliases ();
560 /* Output one variable, if necessary. Return whether we output it. */
563 varpool_node::assemble_decl (void)
565 /* Aliases are outout when their target is produced or by
570 /* Constant pool is output from RTL land when the reference
571 survive till this level. */
572 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
575 /* Decls with VALUE_EXPR should not be in the varpool at all. They
576 are not real variables, but just info for debugging and codegen.
577 Unfortunately at the moment emutls is not updating varpool correctly
578 after turning real vars into value_expr vars. */
579 if (DECL_HAS_VALUE_EXPR_P (decl
)
580 && !targetm
.have_tls
)
583 /* Hard register vars do not need to be output. */
584 if (DECL_HARD_REGISTER (decl
))
587 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
588 && TREE_CODE (decl
) == VAR_DECL
589 && !DECL_HAS_VALUE_EXPR_P (decl
));
591 if (!in_other_partition
592 && !DECL_EXTERNAL (decl
))
595 assemble_variable (decl
, 0, 1, 0);
596 gcc_assert (TREE_ASM_WRITTEN (decl
));
597 gcc_assert (definition
);
605 /* Add NODE to queue starting at FIRST.
606 The queue is linked via AUX pointers and terminated by pointer to 1. */
609 enqueue_node (varpool_node
*node
, varpool_node
**first
)
613 gcc_checking_assert (*first
);
618 /* Optimization of function bodies might've rendered some variables as
619 unnecessary so we want to avoid these from being compiled. Re-do
620 reachability starting from variables that are either externally visible
621 or was referred from the asm output routines. */
624 symbol_table::remove_unreferenced_decls (void)
626 varpool_node
*next
, *node
;
627 varpool_node
*first
= (varpool_node
*)(void *)1;
630 hash_set
<varpool_node
*> referenced
;
636 fprintf (dump_file
, "Trivially needed variables:");
637 FOR_EACH_DEFINED_VARIABLE (node
)
640 && (!node
->can_remove_if_no_refs_p ()
641 /* We just expanded all function bodies. See if any of
642 them needed the variable. */
643 || DECL_RTL_SET_P (node
->decl
)))
645 enqueue_node (node
, &first
);
647 fprintf (dump_file
, " %s", node
->asm_name ());
650 while (first
!= (varpool_node
*)(void *)1)
653 first
= (varpool_node
*)first
->aux
;
655 if (node
->same_comdat_group
)
658 for (next
= node
->same_comdat_group
;
660 next
= next
->same_comdat_group
)
662 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
663 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
664 enqueue_node (vnext
, &first
);
667 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
669 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
671 && !vnode
->in_other_partition
672 && (!DECL_EXTERNAL (ref
->referred
->decl
)
675 enqueue_node (vnode
, &first
);
677 referenced
.add (node
);
681 fprintf (dump_file
, "\nRemoving variables:");
682 for (node
= first_defined_variable (); node
; node
= next
)
684 next
= next_defined_variable (node
);
685 if (!node
->aux
&& !node
->no_reorder
)
688 fprintf (dump_file
, " %s", node
->asm_name ());
689 if (referenced
.contains(node
))
690 node
->remove_initializer ();
697 fprintf (dump_file
, "\n");
700 /* For variables in named sections make sure get_variable_section
701 is called before we switch to those sections. Then section
702 conflicts between read-only and read-only requiring relocations
703 sections can be resolved. */
705 varpool_node::finalize_named_section_flags (void)
707 if (!TREE_ASM_WRITTEN (decl
)
709 && !in_other_partition
710 && !DECL_EXTERNAL (decl
)
711 && TREE_CODE (decl
) == VAR_DECL
712 && !DECL_HAS_VALUE_EXPR_P (decl
)
714 get_variable_section (decl
, false);
717 /* Output all variables enqueued to be assembled. */
719 symbol_table::output_variables (void)
721 bool changed
= false;
727 remove_unreferenced_decls ();
729 timevar_push (TV_VAROUT
);
731 FOR_EACH_VARIABLE (node
)
732 if (!node
->definition
)
733 assemble_undefined_decl (node
->decl
);
734 FOR_EACH_DEFINED_VARIABLE (node
)
736 /* Handled in output_in_order. */
737 if (node
->no_reorder
)
740 node
->finalize_named_section_flags ();
743 FOR_EACH_DEFINED_VARIABLE (node
)
745 /* Handled in output_in_order. */
746 if (node
->no_reorder
)
748 if (node
->assemble_decl ())
751 timevar_pop (TV_VAROUT
);
755 /* Create a new global variable of type TYPE. */
757 add_new_static_var (tree type
)
760 varpool_node
*new_node
;
762 new_decl
= create_tmp_var_raw (type
);
763 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
764 TREE_READONLY (new_decl
) = 0;
765 TREE_STATIC (new_decl
) = 1;
766 TREE_USED (new_decl
) = 1;
767 DECL_CONTEXT (new_decl
) = NULL_TREE
;
768 DECL_ABSTRACT_P (new_decl
) = false;
769 lang_hooks
.dup_lang_specific_decl (new_decl
);
770 new_node
= varpool_node::get_create (new_decl
);
771 varpool_node::finalize_decl (new_decl
);
773 return new_node
->decl
;
776 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
777 Extra name aliases are output whenever DECL is output. */
780 varpool_node::create_alias (tree alias
, tree decl
)
782 varpool_node
*alias_node
;
784 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
785 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
786 alias_node
= varpool_node::get_create (alias
);
787 alias_node
->alias
= true;
788 alias_node
->definition
= true;
789 alias_node
->alias_target
= decl
;
790 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
791 alias_node
->weakref
= true;
795 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
796 Extra name aliases are output whenever DECL is output. */
799 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
801 varpool_node
*alias_node
;
803 #ifndef ASM_OUTPUT_DEF
804 /* If aliases aren't supported by the assembler, fail. */
807 alias_node
= varpool_node::create_alias (alias
, decl
);
808 alias_node
->cpp_implicit_alias
= true;
810 /* Extra name alias mechanizm creates aliases really late
811 via DECL_ASSEMBLER_NAME mechanizm.
812 This is unfortunate because they are not going through the
813 standard channels. Ensure they get output. */
814 if (symtab
->cpp_implicit_aliases_done
)
815 alias_node
->resolve_alias (varpool_node::get_create (decl
));
819 /* Call calback on varpool symbol and aliases associated to varpool symbol.
820 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
824 varpool_node::call_for_node_and_aliases (bool (*callback
) (varpool_node
*,
827 bool include_overwritable
)
831 if (callback (this, data
))
834 FOR_EACH_ALIAS (this, ref
)
836 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
837 if (include_overwritable
838 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
839 if (alias
->call_for_node_and_aliases (callback
, data
,
840 include_overwritable
))