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 "langhooks.h"
29 #include "diagnostic-core.h"
35 #include "gimple-expr.h"
37 #include "pointer-set.h"
38 #include "tree-ssa-alias.h"
40 #include "lto-streamer.h"
43 const char * const tls_model_names
[]={"none", "tls-emulated", "tls-real",
44 "tls-global-dynamic", "tls-local-dynamic",
45 "tls-initial-exec", "tls-local-exec"};
47 /* List of hooks triggered on varpool_node events. */
48 struct varpool_node_hook_list
{
49 varpool_node_hook hook
;
51 struct varpool_node_hook_list
*next
;
54 /* List of hooks triggered when a node is removed. */
55 struct varpool_node_hook_list
*first_varpool_node_removal_hook
;
56 /* List of hooks triggered when an variable is inserted. */
57 struct varpool_node_hook_list
*first_varpool_variable_insertion_hook
;
59 /* Register HOOK to be called with DATA on each removed node. */
60 struct varpool_node_hook_list
*
61 varpool_add_node_removal_hook (varpool_node_hook hook
, void *data
)
63 struct varpool_node_hook_list
*entry
;
64 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
66 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
76 /* Remove ENTRY from the list of hooks called on removing nodes. */
78 varpool_remove_node_removal_hook (struct varpool_node_hook_list
*entry
)
80 struct varpool_node_hook_list
**ptr
= &first_varpool_node_removal_hook
;
88 /* Call all node removal hooks. */
90 varpool_call_node_removal_hooks (varpool_node
*node
)
92 struct varpool_node_hook_list
*entry
= first_varpool_node_removal_hook
;
95 entry
->hook (node
, entry
->data
);
100 /* Register HOOK to be called with DATA on each inserted node. */
101 struct varpool_node_hook_list
*
102 varpool_add_variable_insertion_hook (varpool_node_hook hook
, void *data
)
104 struct varpool_node_hook_list
*entry
;
105 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
107 entry
= (struct varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
117 /* Remove ENTRY from the list of hooks called on inserted nodes. */
119 varpool_remove_variable_insertion_hook (struct varpool_node_hook_list
*entry
)
121 struct varpool_node_hook_list
**ptr
= &first_varpool_variable_insertion_hook
;
123 while (*ptr
!= entry
)
129 /* Call all node insertion hooks. */
131 varpool_call_variable_insertion_hooks (varpool_node
*node
)
133 struct varpool_node_hook_list
*entry
= first_varpool_variable_insertion_hook
;
136 entry
->hook (node
, entry
->data
);
141 /* Allocate new callgraph node and insert it into basic data structures. */
144 varpool_node::create_empty (void)
146 varpool_node
*node
= ggc_cleared_alloc
<varpool_node
> ();
147 node
->type
= SYMTAB_VARIABLE
;
151 /* Return varpool node assigned to DECL. Create new one when needed. */
153 varpool_node::get_create (tree decl
)
155 varpool_node
*node
= varpool_node::get (decl
);
156 gcc_checking_assert (TREE_CODE (decl
) == VAR_DECL
);
160 node
= varpool_node::create_empty ();
162 node
->register_symbol ();
166 /* Remove variable from symbol table. */
169 varpool_node::remove (void)
171 varpool_call_node_removal_hooks (this);
174 /* When streaming we can have multiple nodes associated with decl. */
175 if (cgraph_state
== CGRAPH_LTO_STREAMING
)
177 /* Keep constructor when it may be used for folding. We remove
178 references to external variables before final compilation. */
179 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
180 && !ctor_useable_for_folding_p ())
181 remove_initializer ();
185 /* Remove node initializer when it is no longer needed. */
187 varpool_node::remove_initializer (void)
189 if (DECL_INITIAL (decl
)
190 && !DECL_IN_CONSTANT_POOL (decl
)
191 /* Keep vtables for BINFO folding. */
192 && !DECL_VIRTUAL_P (decl
)
193 /* FIXME: http://gcc.gnu.org/PR55395 */
194 && debug_info_level
== DINFO_LEVEL_NONE
195 /* When doing declaration merging we have duplicate
196 entries for given decl. Do not attempt to remove
197 the boides, or we will end up remiving
199 && cgraph_state
!= CGRAPH_LTO_STREAMING
)
200 DECL_INITIAL (decl
) = error_mark_node
;
203 /* Dump given varpool node to F. */
205 varpool_node::dump (FILE *f
)
208 fprintf (f
, " Availability: %s\n",
209 cgraph_function_flags_ready
210 ? cgraph_availability_names
[get_availability ()]
212 fprintf (f
, " Varpool flags:");
213 if (DECL_INITIAL (decl
))
214 fprintf (f
, " initialized");
216 fprintf (f
, " output");
217 if (used_by_single_function
)
218 fprintf (f
, " used-by-single-function");
219 if (TREE_READONLY (decl
))
220 fprintf (f
, " read-only");
221 if (ctor_useable_for_folding_p ())
222 fprintf (f
, " const-value-known");
224 fprintf (f
, " write-only");
226 fprintf (f
, " %s", tls_model_names
[tls_model
]);
231 /* Dump given varpool node to stderr. */
232 void varpool_node::debug (void)
234 varpool_node::dump (stderr
);
237 /* Dump the variable pool to F. */
239 varpool_node::dump_varpool (FILE *f
)
243 fprintf (f
, "variable pool:\n\n");
244 FOR_EACH_VARIABLE (node
)
248 /* Dump the variable pool to stderr. */
251 varpool_node::debug_varpool (void)
253 dump_varpool (stderr
);
256 /* Given an assembler name, lookup node. */
258 varpool_node::get_for_asmname (tree asmname
)
260 if (symtab_node
*node
= symtab_node_for_asm (asmname
))
261 return dyn_cast
<varpool_node
*> (node
);
266 /* When doing LTO, read variable's constructor from disk if
267 it is not already present. */
270 varpool_node::get_constructor (void)
272 struct lto_file_decl_data
*file_data
;
273 const char *data
, *name
;
276 if (DECL_INITIAL (decl
) != error_mark_node
278 return DECL_INITIAL (decl
);
280 timevar_push (TV_IPA_LTO_CTORS_IN
);
282 file_data
= lto_file_data
;
283 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
285 /* We may have renamed the declaration, e.g., a static function. */
286 name
= lto_get_decl_name_mapping (file_data
, name
);
288 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
291 fatal_error ("%s: section %s is missing",
292 file_data
->file_name
,
295 lto_input_variable_constructor (file_data
, this, data
);
296 lto_stats
.num_function_bodies
++;
297 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
299 lto_free_function_in_decl_state_for_node (this);
300 timevar_pop (TV_IPA_LTO_CTORS_IN
);
301 return DECL_INITIAL (decl
);
304 /* Return true if variable has constructor that can be used for folding. */
307 varpool_node::ctor_useable_for_folding_p (void)
309 varpool_node
*real_node
= this;
311 if (real_node
->alias
&& real_node
->definition
)
312 real_node
= ultimate_alias_target ();
314 if (TREE_CODE (decl
) == CONST_DECL
315 || DECL_IN_CONSTANT_POOL (decl
))
317 if (TREE_THIS_VOLATILE (decl
))
320 /* If we do not have a constructor, we can't use it. */
321 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
322 && !real_node
->lto_file_data
)
325 /* Vtables are defined by their types and must match no matter of interposition
327 if (DECL_VIRTUAL_P (decl
))
329 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
330 classes not defined in the current TU so that it can refer
331 to them from typeinfo objects. Avoid returning NULL_TREE. */
332 return DECL_INITIAL (real_node
->decl
) != NULL
;
335 /* Alias of readonly variable is also readonly, since the variable is stored
336 in readonly memory. We also accept readonly aliases of non-readonly
337 locations assuming that user knows what he is asking for. */
338 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
341 /* Variables declared 'const' without an initializer
342 have zero as the initializer if they may not be
343 overridden at link or run time.
345 It is actually requirement for C++ compiler to optimize const variables
346 consistently. As a GNU extension, do not enfore this rule for user defined
347 weak variables, so we support interposition on:
348 static const int dummy = 0;
349 extern const int foo __attribute__((__weak__, __alias__("dummy")));
351 if ((!DECL_INITIAL (real_node
->decl
)
352 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
353 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
356 /* Variables declared `const' with an initializer are considered
357 to not be overwritable with different initializer by default.
359 ??? Previously we behaved so for scalar variables but not for array
364 /* If DECLARATION is constant variable and its initial value is known
365 (so we can do constant folding), return its constructor (DECL_INITIAL).
366 This may be an expression or NULL when DECL is initialized to 0.
367 Return ERROR_MARK_NODE otherwise.
369 In LTO this may actually trigger reading the constructor from disk.
370 For this reason varpool_ctor_useable_for_folding_p should be used when
371 the actual constructor value is not needed. */
374 ctor_for_folding (tree decl
)
376 varpool_node
*node
, *real_node
;
379 if (TREE_CODE (decl
) != VAR_DECL
380 && TREE_CODE (decl
) != CONST_DECL
)
381 return error_mark_node
;
383 if (TREE_CODE (decl
) == CONST_DECL
384 || DECL_IN_CONSTANT_POOL (decl
))
385 return DECL_INITIAL (decl
);
387 if (TREE_THIS_VOLATILE (decl
))
388 return error_mark_node
;
390 /* Do not care about automatic variables. Those are never initialized
391 anyway, because gimplifier exapnds the code. */
392 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
394 gcc_assert (!TREE_PUBLIC (decl
));
395 return error_mark_node
;
398 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
400 real_node
= node
= varpool_node::get (decl
);
403 real_node
= node
->ultimate_alias_target ();
404 real_decl
= real_node
->decl
;
409 /* See if we are dealing with alias.
410 In most cases alias is just alternative symbol pointing to a given
411 constructor. This allows us to use interposition rules of DECL
412 constructor of REAL_NODE. However weakrefs are special by being just
413 alternative name of their target (if defined). */
414 if (decl
!= real_decl
)
416 gcc_assert (!DECL_INITIAL (decl
)
417 || (node
->alias
&& node
->get_alias_target () == real_node
)
418 || DECL_INITIAL (decl
) == error_mark_node
);
421 node
= node
->get_alias_target ();
426 if ((!DECL_VIRTUAL_P (real_decl
)
427 || DECL_INITIAL (real_decl
) == error_mark_node
428 || !DECL_INITIAL (real_decl
))
429 && (!node
|| !node
->ctor_useable_for_folding_p ()))
430 return error_mark_node
;
432 /* OK, we can return constructor. See if we need to fetch it from disk
434 if (DECL_INITIAL (real_decl
) != error_mark_node
436 return DECL_INITIAL (real_decl
);
437 return real_node
->get_constructor ();
440 /* Add the variable DECL to the varpool.
441 Unlike varpool_finalize_decl function is intended to be used
442 by middle end and allows insertion of new variable at arbitrary point
445 varpool_add_new_variable (tree decl
)
448 varpool_node::finalize_decl (decl
);
449 node
= varpool_node::get_create (decl
);
450 varpool_call_variable_insertion_hooks (node
);
451 if (node
->externally_visible_p ())
452 node
->externally_visible
= true;
455 /* Return variable availability. See cgraph.h for description of individual
458 varpool_node::get_availability (void)
461 return AVAIL_NOT_AVAILABLE
;
462 if (!TREE_PUBLIC (decl
))
463 return AVAIL_AVAILABLE
;
464 if (DECL_IN_CONSTANT_POOL (decl
)
465 || DECL_VIRTUAL_P (decl
))
466 return AVAIL_AVAILABLE
;
467 if (alias
&& weakref
)
469 enum availability avail
;
471 ultimate_alias_target (&avail
)->get_availability ();
474 /* If the variable can be overwritten, return OVERWRITABLE. Takes
475 care of at least one notable extension - the COMDAT variables
476 used to share template instantiations in C++. */
477 if (decl_replaceable_p (decl
)
478 || DECL_EXTERNAL (decl
))
479 return AVAIL_INTERPOSABLE
;
480 return AVAIL_AVAILABLE
;
484 varpool_node::analyze (void)
486 /* When reading back varpool at LTO time, we re-construct the queue in order
487 to have "needed" list right by inserting all needed nodes into varpool.
488 We however don't want to re-analyze already analyzed nodes. */
491 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
492 /* Compute the alignment early so function body expanders are
493 already informed about increased alignment. */
494 align_variable (decl
, 0);
497 resolve_alias (varpool_node::get (alias_target
));
498 else if (DECL_INITIAL (decl
))
499 record_references_in_initializer (decl
, analyzed
);
503 /* Assemble thunks and aliases associated to varpool node. */
506 varpool_node::assemble_aliases (void)
510 FOR_EACH_ALIAS (this, ref
)
512 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
513 do_assemble_alias (alias
->decl
,
514 DECL_ASSEMBLER_NAME (decl
));
515 alias
->assemble_aliases ();
519 /* Output one variable, if necessary. Return whether we output it. */
522 varpool_node::assemble_decl (void)
524 /* Aliases are outout when their target is produced or by
529 /* Constant pool is output from RTL land when the reference
530 survive till this level. */
531 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
534 /* Decls with VALUE_EXPR should not be in the varpool at all. They
535 are not real variables, but just info for debugging and codegen.
536 Unfortunately at the moment emutls is not updating varpool correctly
537 after turning real vars into value_expr vars. */
538 if (DECL_HAS_VALUE_EXPR_P (decl
)
539 && !targetm
.have_tls
)
542 /* Hard register vars do not need to be output. */
543 if (DECL_HARD_REGISTER (decl
))
546 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
547 && TREE_CODE (decl
) == VAR_DECL
548 && !DECL_HAS_VALUE_EXPR_P (decl
));
550 if (!in_other_partition
551 && !DECL_EXTERNAL (decl
))
554 assemble_variable (decl
, 0, 1, 0);
555 gcc_assert (TREE_ASM_WRITTEN (decl
));
556 gcc_assert (definition
);
564 /* Add NODE to queue starting at FIRST.
565 The queue is linked via AUX pointers and terminated by pointer to 1. */
568 enqueue_node (varpool_node
*node
, varpool_node
**first
)
572 gcc_checking_assert (*first
);
577 /* Optimization of function bodies might've rendered some variables as
578 unnecessary so we want to avoid these from being compiled. Re-do
579 reachability starting from variables that are either externally visible
580 or was referred from the asm output routines. */
583 varpool_remove_unreferenced_decls (void)
585 varpool_node
*next
, *node
;
586 varpool_node
*first
= (varpool_node
*)(void *)1;
588 struct ipa_ref
*ref
= NULL
;
589 hash_set
<varpool_node
*> referenced
;
594 if (cgraph_dump_file
)
595 fprintf (cgraph_dump_file
, "Trivially needed variables:");
596 FOR_EACH_DEFINED_VARIABLE (node
)
599 && (!node
->can_remove_if_no_refs_p ()
600 /* We just expanded all function bodies. See if any of
601 them needed the variable. */
602 || DECL_RTL_SET_P (node
->decl
)))
604 enqueue_node (node
, &first
);
605 if (cgraph_dump_file
)
606 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
609 while (first
!= (varpool_node
*)(void *)1)
612 first
= (varpool_node
*)first
->aux
;
614 if (node
->same_comdat_group
)
617 for (next
= node
->same_comdat_group
;
619 next
= next
->same_comdat_group
)
621 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
622 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
623 enqueue_node (vnext
, &first
);
626 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
628 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
630 && !vnode
->in_other_partition
631 && (!DECL_EXTERNAL (ref
->referred
->decl
)
634 enqueue_node (vnode
, &first
);
636 referenced
.add (node
);
639 if (cgraph_dump_file
)
640 fprintf (cgraph_dump_file
, "\nRemoving variables:");
641 for (node
= varpool_first_defined_variable (); node
; node
= next
)
643 next
= varpool_next_defined_variable (node
);
646 if (cgraph_dump_file
)
647 fprintf (cgraph_dump_file
, " %s", node
->asm_name ());
648 if (referenced
.contains (node
))
649 node
->remove_initializer ();
655 if (cgraph_dump_file
)
656 fprintf (cgraph_dump_file
, "\n");
659 /* For variables in named sections make sure get_variable_section
660 is called before we switch to those sections. Then section
661 conflicts between read-only and read-only requiring relocations
662 sections can be resolved. */
664 varpool_node::finalize_named_section_flags (void)
666 if (!TREE_ASM_WRITTEN (decl
)
668 && !in_other_partition
669 && !DECL_EXTERNAL (decl
)
670 && TREE_CODE (decl
) == VAR_DECL
671 && !DECL_HAS_VALUE_EXPR_P (decl
)
673 get_variable_section (decl
, false);
676 /* Output all variables enqueued to be assembled. */
678 varpool_node::output_variables (void)
680 bool changed
= false;
686 varpool_remove_unreferenced_decls ();
688 timevar_push (TV_VAROUT
);
690 FOR_EACH_DEFINED_VARIABLE (node
)
691 node
->finalize_named_section_flags ();
693 FOR_EACH_DEFINED_VARIABLE (node
)
694 if (node
->assemble_decl ())
696 timevar_pop (TV_VAROUT
);
700 /* Create a new global variable of type TYPE. */
702 add_new_static_var (tree type
)
705 varpool_node
*new_node
;
707 new_decl
= create_tmp_var_raw (type
, NULL
);
708 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
709 TREE_READONLY (new_decl
) = 0;
710 TREE_STATIC (new_decl
) = 1;
711 TREE_USED (new_decl
) = 1;
712 DECL_CONTEXT (new_decl
) = NULL_TREE
;
713 DECL_ABSTRACT (new_decl
) = 0;
714 lang_hooks
.dup_lang_specific_decl (new_decl
);
715 new_node
= varpool_node::get_create (new_decl
);
716 varpool_node::finalize_decl (new_decl
);
718 return new_node
->decl
;
721 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
722 Extra name aliases are output whenever DECL is output. */
725 varpool_node::create_alias (tree alias
, tree decl
)
727 varpool_node
*alias_node
;
729 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
730 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
731 alias_node
= varpool_node::get_create (alias
);
732 alias_node
->alias
= true;
733 alias_node
->definition
= true;
734 alias_node
->alias_target
= decl
;
735 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
736 alias_node
->weakref
= true;
740 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
741 Extra name aliases are output whenever DECL is output. */
744 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
746 varpool_node
*alias_node
;
748 #ifndef ASM_OUTPUT_DEF
749 /* If aliases aren't supported by the assembler, fail. */
752 alias_node
= varpool_node::create_alias (alias
, decl
);
753 alias_node
->cpp_implicit_alias
= true;
755 /* Extra name alias mechanizm creates aliases really late
756 via DECL_ASSEMBLER_NAME mechanizm.
757 This is unfortunate because they are not going through the
758 standard channels. Ensure they get output. */
759 if (cpp_implicit_aliases_done
)
760 alias_node
->resolve_alias (varpool_node::get_create (decl
));
764 /* Call calback on varpool symbol and aliases associated to varpool symbol.
765 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
769 varpool_node::call_for_node_and_aliases (bool (*callback
) (varpool_node
*,
772 bool include_overwritable
)
776 if (callback (this, data
))
779 FOR_EACH_ALIAS (this, ref
)
781 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
782 if (include_overwritable
783 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
784 if (alias
->call_for_node_and_aliases (callback
, data
,
785 include_overwritable
))