1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 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 "basic-block.h"
31 #include "plugin-api.h"
36 #include "hard-reg-set.h"
41 #include "langhooks.h"
42 #include "diagnostic-core.h"
47 #include "gimple-expr.h"
49 #include "tree-ssa-alias.h"
51 #include "lto-streamer.h"
55 const char * const tls_model_names
[]={"none", "tls-emulated", "tls-real",
56 "tls-global-dynamic", "tls-local-dynamic",
57 "tls-initial-exec", "tls-local-exec"};
59 /* List of hooks triggered on varpool_node events. */
60 struct varpool_node_hook_list
{
61 varpool_node_hook hook
;
63 struct varpool_node_hook_list
*next
;
66 /* Register HOOK to be called with DATA on each removed node. */
67 varpool_node_hook_list
*
68 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
70 varpool_node_hook_list
*entry
;
71 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
73 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
83 /* Remove ENTRY from the list of hooks called on removing nodes. */
85 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
87 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
95 /* Call all node removal hooks. */
97 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
99 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
102 entry
->hook (node
, entry
->data
);
107 /* Register HOOK to be called with DATA on each inserted node. */
108 varpool_node_hook_list
*
109 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
111 varpool_node_hook_list
*entry
;
112 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
114 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
124 /* Remove ENTRY from the list of hooks called on inserted nodes. */
126 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
128 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
130 while (*ptr
!= entry
)
136 /* Call all node insertion hooks. */
138 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
140 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
143 entry
->hook (node
, entry
->data
);
148 /* Allocate new callgraph node and insert it into basic data structures. */
151 varpool_node::create_empty (void)
153 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
154 node
->type
= SYMTAB_VARIABLE
;
158 /* Return varpool node assigned to DECL. Create new one when needed. */
160 varpool_node::get_create (tree decl
)
162 varpool_node
*node
= varpool_node::get (decl
);
163 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
167 node
= varpool_node::create_empty ();
171 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
)))
173 node
->offloadable
= 1;
174 #ifdef ENABLE_OFFLOADING
175 g
->have_offload
= true;
177 vec_safe_push (offload_vars
, decl
);
181 node
->register_symbol ();
185 /* Remove variable from symbol table. */
188 varpool_node::remove (void)
190 symtab
->call_varpool_removal_hooks (this);
193 /* When streaming we can have multiple nodes associated with decl. */
194 if (symtab
->state
== LTO_STREAMING
)
196 /* Keep constructor when it may be used for folding. We remove
197 references to external variables before final compilation. */
198 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
199 && !ctor_useable_for_folding_p ())
200 remove_initializer ();
204 /* Remove node initializer when it is no longer needed. */
206 varpool_node::remove_initializer (void)
208 if (DECL_INITIAL (decl
)
209 && !DECL_IN_CONSTANT_POOL (decl
)
210 /* Keep vtables for BINFO folding. */
211 && !DECL_VIRTUAL_P (decl
)
212 /* FIXME: http://gcc.gnu.org/PR55395 */
213 && debug_info_level
== DINFO_LEVEL_NONE
214 /* When doing declaration merging we have duplicate
215 entries for given decl. Do not attempt to remove
216 the boides, or we will end up remiving
218 && symtab
->state
!= LTO_STREAMING
)
219 DECL_INITIAL (decl
) = error_mark_node
;
222 /* Dump given varpool node to F. */
224 varpool_node::dump (FILE *f
)
227 fprintf (f
, " Availability: %s\n",
228 symtab
->function_flags_ready
229 ? cgraph_availability_names
[get_availability ()]
231 fprintf (f
, " Varpool flags:");
232 if (DECL_INITIAL (decl
))
233 fprintf (f
, " initialized");
235 fprintf (f
, " output");
236 if (used_by_single_function
)
237 fprintf (f
, " used-by-single-function");
238 if (need_bounds_init
)
239 fprintf (f
, " need-bounds-init");
240 if (TREE_READONLY (decl
))
241 fprintf (f
, " read-only");
242 if (ctor_useable_for_folding_p ())
243 fprintf (f
, " const-value-known");
245 fprintf (f
, " write-only");
247 fprintf (f
, " %s", tls_model_names
[tls_model
]);
252 /* Dump given varpool node to stderr. */
253 void varpool_node::debug (void)
255 varpool_node::dump (stderr
);
258 /* Dump the variable pool to F. */
260 varpool_node::dump_varpool (FILE *f
)
264 fprintf (f
, "variable pool:\n\n");
265 FOR_EACH_VARIABLE (node
)
269 /* Dump the variable pool to stderr. */
272 varpool_node::debug_varpool (void)
274 dump_varpool (stderr
);
277 /* Given an assembler name, lookup node. */
279 varpool_node::get_for_asmname (tree asmname
)
281 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
282 return dyn_cast
<varpool_node
*> (node
);
287 /* When doing LTO, read variable's constructor from disk if
288 it is not already present. */
291 varpool_node::get_constructor (void)
293 lto_file_decl_data
*file_data
;
294 const char *data
, *name
;
297 if (DECL_INITIAL (decl
) != error_mark_node
299 return DECL_INITIAL (decl
);
301 timevar_push (TV_IPA_LTO_CTORS_IN
);
303 file_data
= lto_file_data
;
304 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
306 /* We may have renamed the declaration, e.g., a static function. */
307 name
= lto_get_decl_name_mapping (file_data
, name
);
309 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
312 fatal_error ("%s: section %s is missing",
313 file_data
->file_name
,
316 lto_input_variable_constructor (file_data
, this, data
);
317 lto_stats
.num_function_bodies
++;
318 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
320 lto_free_function_in_decl_state_for_node (this);
321 timevar_pop (TV_IPA_LTO_CTORS_IN
);
322 return DECL_INITIAL (decl
);
325 /* Return true if variable has constructor that can be used for folding. */
328 varpool_node::ctor_useable_for_folding_p (void)
330 varpool_node
*real_node
= this;
332 if (real_node
->alias
&& real_node
->definition
)
333 real_node
= ultimate_alias_target ();
335 if (TREE_CODE (decl
) == CONST_DECL
336 || DECL_IN_CONSTANT_POOL (decl
))
338 if (TREE_THIS_VOLATILE (decl
))
341 /* If we do not have a constructor, we can't use it. */
342 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
343 && !real_node
->lto_file_data
)
346 /* Avoid attempts to load constructors that was not streamed. */
347 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
348 && real_node
->body_removed
)
351 /* Vtables are defined by their types and must match no matter of interposition
353 if (DECL_VIRTUAL_P (decl
))
355 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
356 classes not defined in the current TU so that it can refer
357 to them from typeinfo objects. Avoid returning NULL_TREE. */
358 return DECL_INITIAL (real_node
->decl
) != NULL
;
361 /* Alias of readonly variable is also readonly, since the variable is stored
362 in readonly memory. We also accept readonly aliases of non-readonly
363 locations assuming that user knows what he is asking for. */
364 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
367 /* Variables declared 'const' without an initializer
368 have zero as the initializer if they may not be
369 overridden at link or run time.
371 It is actually requirement for C++ compiler to optimize const variables
372 consistently. As a GNU extension, do not enfore this rule for user defined
373 weak variables, so we support interposition on:
374 static const int dummy = 0;
375 extern const int foo __attribute__((__weak__, __alias__("dummy")));
377 if ((!DECL_INITIAL (real_node
->decl
)
378 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
379 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
382 /* Variables declared `const' with an initializer are considered
383 to not be overwritable with different initializer by default.
385 ??? Previously we behaved so for scalar variables but not for array
390 /* If DECLARATION is constant variable and its initial value is known
391 (so we can do constant folding), return its constructor (DECL_INITIAL).
392 This may be an expression or NULL when DECL is initialized to 0.
393 Return ERROR_MARK_NODE otherwise.
395 In LTO this may actually trigger reading the constructor from disk.
396 For this reason varpool_ctor_useable_for_folding_p should be used when
397 the actual constructor value is not needed. */
400 ctor_for_folding (tree decl
)
402 varpool_node
*node
, *real_node
;
405 if (TREE_CODE (decl
) != VAR_DECL
406 && TREE_CODE (decl
) != CONST_DECL
)
407 return error_mark_node
;
409 /* Static constant bounds are created to be
410 used instead of constants and therefore
411 do not let folding it. */
412 if (POINTER_BOUNDS_P (decl
))
413 return error_mark_node
;
415 if (TREE_CODE (decl
) == CONST_DECL
416 || DECL_IN_CONSTANT_POOL (decl
))
417 return DECL_INITIAL (decl
);
419 if (TREE_THIS_VOLATILE (decl
))
420 return error_mark_node
;
422 /* Do not care about automatic variables. Those are never initialized
423 anyway, because gimplifier exapnds the code. */
424 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
426 gcc_assert (!TREE_PUBLIC (decl
));
427 return error_mark_node
;
430 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
432 real_node
= node
= varpool_node::get (decl
);
435 real_node
= node
->ultimate_alias_target ();
436 real_decl
= real_node
->decl
;
441 /* See if we are dealing with alias.
442 In most cases alias is just alternative symbol pointing to a given
443 constructor. This allows us to use interposition rules of DECL
444 constructor of REAL_NODE. However weakrefs are special by being just
445 alternative name of their target (if defined). */
446 if (decl
!= real_decl
)
448 gcc_assert (!DECL_INITIAL (decl
)
449 || (node
->alias
&& node
->get_alias_target () == real_node
)
450 || DECL_INITIAL (decl
) == error_mark_node
);
453 node
= node
->get_alias_target ();
458 if ((!DECL_VIRTUAL_P (real_decl
)
459 || DECL_INITIAL (real_decl
) == error_mark_node
460 || !DECL_INITIAL (real_decl
))
461 && (!node
|| !node
->ctor_useable_for_folding_p ()))
462 return error_mark_node
;
464 /* OK, we can return constructor. See if we need to fetch it from disk
466 if (DECL_INITIAL (real_decl
) != error_mark_node
468 return DECL_INITIAL (real_decl
);
469 return real_node
->get_constructor ();
472 /* Add the variable DECL to the varpool.
473 Unlike finalize_decl function is intended to be used
474 by middle end and allows insertion of new variable at arbitrary point
477 varpool_node::add (tree decl
)
480 varpool_node::finalize_decl (decl
);
481 node
= varpool_node::get_create (decl
);
482 symtab
->call_varpool_insertion_hooks (node
);
483 if (node
->externally_visible_p ())
484 node
->externally_visible
= true;
485 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
486 node
->no_reorder
= 1;
489 /* Return variable availability. See cgraph.h for description of individual
492 varpool_node::get_availability (void)
495 return AVAIL_NOT_AVAILABLE
;
496 if (!TREE_PUBLIC (decl
))
497 return AVAIL_AVAILABLE
;
498 if (DECL_IN_CONSTANT_POOL (decl
)
499 || DECL_VIRTUAL_P (decl
))
500 return AVAIL_AVAILABLE
;
501 if (alias
&& weakref
)
503 enum availability avail
;
505 ultimate_alias_target (&avail
)->get_availability ();
508 /* If the variable can be overwritten, return OVERWRITABLE. Takes
509 care of at least one notable extension - the COMDAT variables
510 used to share template instantiations in C++. */
511 if (decl_replaceable_p (decl
)
512 || DECL_EXTERNAL (decl
))
513 return AVAIL_INTERPOSABLE
;
514 return AVAIL_AVAILABLE
;
518 varpool_node::analyze (void)
520 /* When reading back varpool at LTO time, we re-construct the queue in order
521 to have "needed" list right by inserting all needed nodes into varpool.
522 We however don't want to re-analyze already analyzed nodes. */
525 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
526 /* Compute the alignment early so function body expanders are
527 already informed about increased alignment. */
528 align_variable (decl
, 0);
531 resolve_alias (varpool_node::get (alias_target
));
532 else if (DECL_INITIAL (decl
))
533 record_references_in_initializer (decl
, analyzed
);
537 /* Assemble thunks and aliases associated to varpool node. */
540 varpool_node::assemble_aliases (void)
544 FOR_EACH_ALIAS (this, ref
)
546 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
547 do_assemble_alias (alias
->decl
,
548 DECL_ASSEMBLER_NAME (decl
));
549 alias
->assemble_aliases ();
553 /* Output one variable, if necessary. Return whether we output it. */
556 varpool_node::assemble_decl (void)
558 /* Aliases are outout when their target is produced or by
563 /* Constant pool is output from RTL land when the reference
564 survive till this level. */
565 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
568 /* Decls with VALUE_EXPR should not be in the varpool at all. They
569 are not real variables, but just info for debugging and codegen.
570 Unfortunately at the moment emutls is not updating varpool correctly
571 after turning real vars into value_expr vars. */
572 if (DECL_HAS_VALUE_EXPR_P (decl
)
573 && !targetm
.have_tls
)
576 /* Hard register vars do not need to be output. */
577 if (DECL_HARD_REGISTER (decl
))
580 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
581 && TREE_CODE (decl
) == VAR_DECL
582 && !DECL_HAS_VALUE_EXPR_P (decl
));
584 if (!in_other_partition
585 && !DECL_EXTERNAL (decl
))
588 assemble_variable (decl
, 0, 1, 0);
589 gcc_assert (TREE_ASM_WRITTEN (decl
));
590 gcc_assert (definition
);
598 /* Add NODE to queue starting at FIRST.
599 The queue is linked via AUX pointers and terminated by pointer to 1. */
602 enqueue_node (varpool_node
*node
, varpool_node
**first
)
606 gcc_checking_assert (*first
);
611 /* Optimization of function bodies might've rendered some variables as
612 unnecessary so we want to avoid these from being compiled. Re-do
613 reachability starting from variables that are either externally visible
614 or was referred from the asm output routines. */
617 symbol_table::remove_unreferenced_decls (void)
619 varpool_node
*next
, *node
;
620 varpool_node
*first
= (varpool_node
*)(void *)1;
623 hash_set
<varpool_node
*> referenced
;
629 fprintf (dump_file
, "Trivially needed variables:");
630 FOR_EACH_DEFINED_VARIABLE (node
)
633 && (!node
->can_remove_if_no_refs_p ()
634 /* We just expanded all function bodies. See if any of
635 them needed the variable. */
636 || DECL_RTL_SET_P (node
->decl
)))
638 enqueue_node (node
, &first
);
640 fprintf (dump_file
, " %s", node
->asm_name ());
643 while (first
!= (varpool_node
*)(void *)1)
646 first
= (varpool_node
*)first
->aux
;
648 if (node
->same_comdat_group
)
651 for (next
= node
->same_comdat_group
;
653 next
= next
->same_comdat_group
)
655 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
656 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
657 enqueue_node (vnext
, &first
);
660 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
662 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
664 && !vnode
->in_other_partition
665 && (!DECL_EXTERNAL (ref
->referred
->decl
)
668 enqueue_node (vnode
, &first
);
670 referenced
.add (node
);
674 fprintf (dump_file
, "\nRemoving variables:");
675 for (node
= first_defined_variable (); node
; node
= next
)
677 next
= next_defined_variable (node
);
678 if (!node
->aux
&& !node
->no_reorder
)
681 fprintf (dump_file
, " %s", node
->asm_name ());
682 if (referenced
.contains(node
))
683 node
->remove_initializer ();
690 fprintf (dump_file
, "\n");
693 /* For variables in named sections make sure get_variable_section
694 is called before we switch to those sections. Then section
695 conflicts between read-only and read-only requiring relocations
696 sections can be resolved. */
698 varpool_node::finalize_named_section_flags (void)
700 if (!TREE_ASM_WRITTEN (decl
)
702 && !in_other_partition
703 && !DECL_EXTERNAL (decl
)
704 && TREE_CODE (decl
) == VAR_DECL
705 && !DECL_HAS_VALUE_EXPR_P (decl
)
707 get_variable_section (decl
, false);
710 /* Output all variables enqueued to be assembled. */
712 symbol_table::output_variables (void)
714 bool changed
= false;
720 remove_unreferenced_decls ();
722 timevar_push (TV_VAROUT
);
724 FOR_EACH_VARIABLE (node
)
725 if (!node
->definition
)
726 assemble_undefined_decl (node
->decl
);
727 FOR_EACH_DEFINED_VARIABLE (node
)
729 /* Handled in output_in_order. */
730 if (node
->no_reorder
)
733 node
->finalize_named_section_flags ();
736 FOR_EACH_DEFINED_VARIABLE (node
)
738 /* Handled in output_in_order. */
739 if (node
->no_reorder
)
741 if (node
->assemble_decl ())
744 timevar_pop (TV_VAROUT
);
748 /* Create a new global variable of type TYPE. */
750 add_new_static_var (tree type
)
753 varpool_node
*new_node
;
755 new_decl
= create_tmp_var_raw (type
, NULL
);
756 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
757 TREE_READONLY (new_decl
) = 0;
758 TREE_STATIC (new_decl
) = 1;
759 TREE_USED (new_decl
) = 1;
760 DECL_CONTEXT (new_decl
) = NULL_TREE
;
761 DECL_ABSTRACT_P (new_decl
) = false;
762 lang_hooks
.dup_lang_specific_decl (new_decl
);
763 new_node
= varpool_node::get_create (new_decl
);
764 varpool_node::finalize_decl (new_decl
);
766 return new_node
->decl
;
769 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
770 Extra name aliases are output whenever DECL is output. */
773 varpool_node::create_alias (tree alias
, tree decl
)
775 varpool_node
*alias_node
;
777 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
778 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
779 alias_node
= varpool_node::get_create (alias
);
780 alias_node
->alias
= true;
781 alias_node
->definition
= true;
782 alias_node
->alias_target
= decl
;
783 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
784 alias_node
->weakref
= true;
788 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
789 Extra name aliases are output whenever DECL is output. */
792 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
794 varpool_node
*alias_node
;
796 #ifndef ASM_OUTPUT_DEF
797 /* If aliases aren't supported by the assembler, fail. */
800 alias_node
= varpool_node::create_alias (alias
, decl
);
801 alias_node
->cpp_implicit_alias
= true;
803 /* Extra name alias mechanizm creates aliases really late
804 via DECL_ASSEMBLER_NAME mechanizm.
805 This is unfortunate because they are not going through the
806 standard channels. Ensure they get output. */
807 if (symtab
->cpp_implicit_aliases_done
)
808 alias_node
->resolve_alias (varpool_node::get_create (decl
));
812 /* Call calback on varpool symbol and aliases associated to varpool symbol.
813 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
817 varpool_node::call_for_node_and_aliases (bool (*callback
) (varpool_node
*,
820 bool include_overwritable
)
824 if (callback (this, data
))
827 FOR_EACH_ALIAS (this, ref
)
829 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
830 if (include_overwritable
831 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
832 if (alias
->call_for_node_and_aliases (callback
, data
,
833 include_overwritable
))