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", "emulated",
62 "global-dynamic", "local-dynamic",
63 "initial-exec", "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
) && !DECL_EXTERNAL (decl
)
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 lto_free_function_in_decl_state_for_node (this);
201 lto_file_data
= NULL
;
204 /* When streaming we can have multiple nodes associated with decl. */
205 if (symtab
->state
== LTO_STREAMING
)
207 /* Keep constructor when it may be used for folding. We remove
208 references to external variables before final compilation. */
209 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
210 && !ctor_useable_for_folding_p ())
211 remove_initializer ();
217 /* Remove node initializer when it is no longer needed. */
219 varpool_node::remove_initializer (void)
221 if (DECL_INITIAL (decl
)
222 && !DECL_IN_CONSTANT_POOL (decl
)
223 /* Keep vtables for BINFO folding. */
224 && !DECL_VIRTUAL_P (decl
)
225 /* FIXME: http://gcc.gnu.org/PR55395 */
226 && debug_info_level
== DINFO_LEVEL_NONE
227 /* When doing declaration merging we have duplicate
228 entries for given decl. Do not attempt to remove
229 the boides, or we will end up remiving
231 && symtab
->state
!= LTO_STREAMING
)
232 DECL_INITIAL (decl
) = error_mark_node
;
235 /* Dump given varpool node to F. */
237 varpool_node::dump (FILE *f
)
240 fprintf (f
, " Availability: %s\n",
241 symtab
->function_flags_ready
242 ? cgraph_availability_names
[get_availability ()]
244 fprintf (f
, " Varpool flags:");
245 if (DECL_INITIAL (decl
))
246 fprintf (f
, " initialized");
248 fprintf (f
, " output");
249 if (used_by_single_function
)
250 fprintf (f
, " used-by-single-function");
251 if (need_bounds_init
)
252 fprintf (f
, " need-bounds-init");
253 if (TREE_READONLY (decl
))
254 fprintf (f
, " read-only");
255 if (ctor_useable_for_folding_p ())
256 fprintf (f
, " const-value-known");
258 fprintf (f
, " write-only");
260 fprintf (f
, " tls-%s", tls_model_names
[tls_model
]);
265 /* Dump given varpool node to stderr. */
266 void varpool_node::debug (void)
268 varpool_node::dump (stderr
);
271 /* Dump the variable pool to F. */
273 varpool_node::dump_varpool (FILE *f
)
277 fprintf (f
, "variable pool:\n\n");
278 FOR_EACH_VARIABLE (node
)
282 /* Dump the variable pool to stderr. */
285 varpool_node::debug_varpool (void)
287 dump_varpool (stderr
);
290 /* Given an assembler name, lookup node. */
292 varpool_node::get_for_asmname (tree asmname
)
294 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
295 return dyn_cast
<varpool_node
*> (node
);
300 /* When doing LTO, read variable's constructor from disk if
301 it is not already present. */
304 varpool_node::get_constructor (void)
306 lto_file_decl_data
*file_data
;
307 const char *data
, *name
;
310 if (DECL_INITIAL (decl
) != error_mark_node
313 return DECL_INITIAL (decl
);
315 timevar_push (TV_IPA_LTO_CTORS_IN
);
317 file_data
= lto_file_data
;
318 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
320 /* We may have renamed the declaration, e.g., a static function. */
321 name
= lto_get_decl_name_mapping (file_data
, name
);
323 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
326 fatal_error (input_location
, "%s: section %s is missing",
327 file_data
->file_name
,
330 lto_input_variable_constructor (file_data
, this, data
);
331 gcc_assert (DECL_INITIAL (decl
) != error_mark_node
);
332 lto_stats
.num_function_bodies
++;
333 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
335 lto_free_function_in_decl_state_for_node (this);
336 timevar_pop (TV_IPA_LTO_CTORS_IN
);
337 return DECL_INITIAL (decl
);
340 /* Return true if variable has constructor that can be used for folding. */
343 varpool_node::ctor_useable_for_folding_p (void)
345 varpool_node
*real_node
= this;
347 if (real_node
->alias
&& real_node
->definition
)
348 real_node
= ultimate_alias_target ();
350 if (TREE_CODE (decl
) == CONST_DECL
351 || DECL_IN_CONSTANT_POOL (decl
))
353 if (TREE_THIS_VOLATILE (decl
))
356 /* If we do not have a constructor, we can't use it. */
357 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
358 && !real_node
->lto_file_data
)
361 /* Avoid attempts to load constructors that was not streamed. */
362 if (flag_ltrans
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
363 && real_node
->body_removed
)
366 /* Vtables are defined by their types and must match no matter of interposition
368 if (DECL_VIRTUAL_P (decl
))
370 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
371 classes not defined in the current TU so that it can refer
372 to them from typeinfo objects. Avoid returning NULL_TREE. */
373 return DECL_INITIAL (real_node
->decl
) != NULL
;
376 /* Alias of readonly variable is also readonly, since the variable is stored
377 in readonly memory. We also accept readonly aliases of non-readonly
378 locations assuming that user knows what he is asking for. */
379 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
382 /* Variables declared 'const' without an initializer
383 have zero as the initializer if they may not be
384 overridden at link or run time.
386 It is actually requirement for C++ compiler to optimize const variables
387 consistently. As a GNU extension, do not enfore this rule for user defined
388 weak variables, so we support interposition on:
389 static const int dummy = 0;
390 extern const int foo __attribute__((__weak__, __alias__("dummy")));
392 if ((!DECL_INITIAL (real_node
->decl
)
393 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
394 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
397 /* Variables declared `const' with an initializer are considered
398 to not be overwritable with different initializer by default.
400 ??? Previously we behaved so for scalar variables but not for array
405 /* If DECLARATION is constant variable and its initial value is known
406 (so we can do constant folding), return its constructor (DECL_INITIAL).
407 This may be an expression or NULL when DECL is initialized to 0.
408 Return ERROR_MARK_NODE otherwise.
410 In LTO this may actually trigger reading the constructor from disk.
411 For this reason varpool_ctor_useable_for_folding_p should be used when
412 the actual constructor value is not needed. */
415 ctor_for_folding (tree decl
)
417 varpool_node
*node
, *real_node
;
420 if (TREE_CODE (decl
) != VAR_DECL
421 && TREE_CODE (decl
) != CONST_DECL
)
422 return error_mark_node
;
424 /* Static constant bounds are created to be
425 used instead of constants and therefore
426 do not let folding it. */
427 if (POINTER_BOUNDS_P (decl
))
428 return error_mark_node
;
430 if (TREE_CODE (decl
) == CONST_DECL
431 || DECL_IN_CONSTANT_POOL (decl
))
432 return DECL_INITIAL (decl
);
434 if (TREE_THIS_VOLATILE (decl
))
435 return error_mark_node
;
437 /* Do not care about automatic variables. Those are never initialized
438 anyway, because gimplifier exapnds the code. */
439 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
441 gcc_assert (!TREE_PUBLIC (decl
));
442 return error_mark_node
;
445 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
447 real_node
= node
= varpool_node::get (decl
);
450 real_node
= node
->ultimate_alias_target ();
451 real_decl
= real_node
->decl
;
456 /* See if we are dealing with alias.
457 In most cases alias is just alternative symbol pointing to a given
458 constructor. This allows us to use interposition rules of DECL
459 constructor of REAL_NODE. However weakrefs are special by being just
460 alternative name of their target (if defined). */
461 if (decl
!= real_decl
)
463 gcc_assert (!DECL_INITIAL (decl
)
464 || (node
->alias
&& node
->get_alias_target () == real_node
)
465 || DECL_INITIAL (decl
) == error_mark_node
);
468 node
= node
->get_alias_target ();
473 if ((!DECL_VIRTUAL_P (real_decl
)
474 || DECL_INITIAL (real_decl
) == error_mark_node
475 || !DECL_INITIAL (real_decl
))
476 && (!node
|| !node
->ctor_useable_for_folding_p ()))
477 return error_mark_node
;
479 /* OK, we can return constructor. See if we need to fetch it from disk
481 if (DECL_INITIAL (real_decl
) != error_mark_node
483 return DECL_INITIAL (real_decl
);
484 return real_node
->get_constructor ();
487 /* Add the variable DECL to the varpool.
488 Unlike finalize_decl function is intended to be used
489 by middle end and allows insertion of new variable at arbitrary point
492 varpool_node::add (tree decl
)
495 varpool_node::finalize_decl (decl
);
496 node
= varpool_node::get_create (decl
);
497 symtab
->call_varpool_insertion_hooks (node
);
498 if (node
->externally_visible_p ())
499 node
->externally_visible
= true;
500 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
501 node
->no_reorder
= 1;
504 /* Return variable availability. See cgraph.h for description of individual
507 varpool_node::get_availability (void)
510 return AVAIL_NOT_AVAILABLE
;
511 if (!TREE_PUBLIC (decl
))
512 return AVAIL_AVAILABLE
;
513 if (DECL_IN_CONSTANT_POOL (decl
)
514 || DECL_VIRTUAL_P (decl
))
515 return AVAIL_AVAILABLE
;
516 if (alias
&& weakref
)
518 enum availability avail
;
520 ultimate_alias_target (&avail
)->get_availability ();
523 /* If the variable can be overwritten, return OVERWRITABLE. Takes
524 care of at least one notable extension - the COMDAT variables
525 used to share template instantiations in C++. */
526 if (decl_replaceable_p (decl
)
527 || DECL_EXTERNAL (decl
))
528 return AVAIL_INTERPOSABLE
;
529 return AVAIL_AVAILABLE
;
533 varpool_node::analyze (void)
535 /* When reading back varpool at LTO time, we re-construct the queue in order
536 to have "needed" list right by inserting all needed nodes into varpool.
537 We however don't want to re-analyze already analyzed nodes. */
540 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
541 /* Compute the alignment early so function body expanders are
542 already informed about increased alignment. */
543 align_variable (decl
, 0);
546 resolve_alias (varpool_node::get (alias_target
));
547 else if (DECL_INITIAL (decl
))
548 record_references_in_initializer (decl
, analyzed
);
552 /* Assemble thunks and aliases associated to varpool node. */
555 varpool_node::assemble_aliases (void)
559 FOR_EACH_ALIAS (this, ref
)
561 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
562 do_assemble_alias (alias
->decl
,
563 DECL_ASSEMBLER_NAME (decl
));
564 alias
->assemble_aliases ();
568 /* Output one variable, if necessary. Return whether we output it. */
571 varpool_node::assemble_decl (void)
573 /* Aliases are outout when their target is produced or by
578 /* Constant pool is output from RTL land when the reference
579 survive till this level. */
580 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
583 /* Decls with VALUE_EXPR should not be in the varpool at all. They
584 are not real variables, but just info for debugging and codegen.
585 Unfortunately at the moment emutls is not updating varpool correctly
586 after turning real vars into value_expr vars. */
587 if (DECL_HAS_VALUE_EXPR_P (decl
)
588 && !targetm
.have_tls
)
591 /* Hard register vars do not need to be output. */
592 if (DECL_HARD_REGISTER (decl
))
595 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
596 && TREE_CODE (decl
) == VAR_DECL
597 && !DECL_HAS_VALUE_EXPR_P (decl
));
599 if (!in_other_partition
600 && !DECL_EXTERNAL (decl
))
603 assemble_variable (decl
, 0, 1, 0);
604 gcc_assert (TREE_ASM_WRITTEN (decl
));
605 gcc_assert (definition
);
613 /* Add NODE to queue starting at FIRST.
614 The queue is linked via AUX pointers and terminated by pointer to 1. */
617 enqueue_node (varpool_node
*node
, varpool_node
**first
)
621 gcc_checking_assert (*first
);
626 /* Optimization of function bodies might've rendered some variables as
627 unnecessary so we want to avoid these from being compiled. Re-do
628 reachability starting from variables that are either externally visible
629 or was referred from the asm output routines. */
632 symbol_table::remove_unreferenced_decls (void)
634 varpool_node
*next
, *node
;
635 varpool_node
*first
= (varpool_node
*)(void *)1;
638 hash_set
<varpool_node
*> referenced
;
644 fprintf (dump_file
, "Trivially needed variables:");
645 FOR_EACH_DEFINED_VARIABLE (node
)
648 && (!node
->can_remove_if_no_refs_p ()
649 /* We just expanded all function bodies. See if any of
650 them needed the variable. */
651 || DECL_RTL_SET_P (node
->decl
)))
653 enqueue_node (node
, &first
);
655 fprintf (dump_file
, " %s", node
->asm_name ());
658 while (first
!= (varpool_node
*)(void *)1)
661 first
= (varpool_node
*)first
->aux
;
663 if (node
->same_comdat_group
)
666 for (next
= node
->same_comdat_group
;
668 next
= next
->same_comdat_group
)
670 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
671 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
672 enqueue_node (vnext
, &first
);
675 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
677 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
679 && !vnode
->in_other_partition
680 && (!DECL_EXTERNAL (ref
->referred
->decl
)
683 enqueue_node (vnode
, &first
);
685 referenced
.add (node
);
689 fprintf (dump_file
, "\nRemoving variables:");
690 for (node
= first_defined_variable (); node
; node
= next
)
692 next
= next_defined_variable (node
);
693 if (!node
->aux
&& !node
->no_reorder
)
696 fprintf (dump_file
, " %s", node
->asm_name ());
697 if (referenced
.contains(node
))
698 node
->remove_initializer ();
705 fprintf (dump_file
, "\n");
708 /* For variables in named sections make sure get_variable_section
709 is called before we switch to those sections. Then section
710 conflicts between read-only and read-only requiring relocations
711 sections can be resolved. */
713 varpool_node::finalize_named_section_flags (void)
715 if (!TREE_ASM_WRITTEN (decl
)
717 && !in_other_partition
718 && !DECL_EXTERNAL (decl
)
719 && TREE_CODE (decl
) == VAR_DECL
720 && !DECL_HAS_VALUE_EXPR_P (decl
)
722 get_variable_section (decl
, false);
725 /* Output all variables enqueued to be assembled. */
727 symbol_table::output_variables (void)
729 bool changed
= false;
735 remove_unreferenced_decls ();
737 timevar_push (TV_VAROUT
);
739 FOR_EACH_VARIABLE (node
)
740 if (!node
->definition
741 && !DECL_HAS_VALUE_EXPR_P (node
->decl
)
742 && !DECL_HARD_REGISTER (node
->decl
))
743 assemble_undefined_decl (node
->decl
);
744 FOR_EACH_DEFINED_VARIABLE (node
)
746 /* Handled in output_in_order. */
747 if (node
->no_reorder
)
750 node
->finalize_named_section_flags ();
753 FOR_EACH_DEFINED_VARIABLE (node
)
755 /* Handled in output_in_order. */
756 if (node
->no_reorder
)
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
= 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
))