1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 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"
27 #include "langhooks.h"
28 #include "diagnostic-core.h"
36 #include "tree-flow.h"
39 /* Return varpool node assigned to DECL. Create new one when needed. */
41 varpool_node_for_decl (tree decl
)
43 struct varpool_node
*node
= varpool_get_node (decl
);
44 gcc_assert (TREE_CODE (decl
) == VAR_DECL
45 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
) || in_lto_p
));
49 node
= ggc_alloc_cleared_varpool_node ();
50 node
->symbol
.type
= SYMTAB_VARIABLE
;
51 node
->symbol
.decl
= decl
;
52 symtab_register_node ((symtab_node
)node
);
56 /* Remove node from the varpool. */
58 varpool_remove_node (struct varpool_node
*node
)
60 symtab_unregister_node ((symtab_node
)node
);
61 if (DECL_INITIAL (node
->symbol
.decl
)
62 && !DECL_IN_CONSTANT_POOL (node
->symbol
.decl
)
63 /* Keep vtables for BINFO folding. */
64 && !DECL_VIRTUAL_P (node
->symbol
.decl
)
65 /* FIXME: http://gcc.gnu.org/PR55395 */
66 && debug_info_level
== DINFO_LEVEL_NONE
)
67 DECL_INITIAL (node
->symbol
.decl
) = error_mark_node
;
71 /* Dump given cgraph node. */
73 dump_varpool_node (FILE *f
, struct varpool_node
*node
)
75 dump_symtab_base (f
, (symtab_node
)node
);
76 fprintf (f
, " Availability: %s\n",
77 cgraph_function_flags_ready
78 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
80 fprintf (f
, " Varpool flags:");
81 if (DECL_INITIAL (node
->symbol
.decl
))
82 fprintf (f
, " initialized");
84 fprintf (f
, " analyzed");
86 fprintf (f
, " finalized");
88 fprintf (f
, " output");
89 if (TREE_READONLY (node
->symbol
.decl
))
90 fprintf (f
, " read-only");
91 if (const_value_known_p (node
->symbol
.decl
))
92 fprintf (f
, " const-value-known");
96 /* Dump the variable pool. */
98 dump_varpool (FILE *f
)
100 struct varpool_node
*node
;
102 fprintf (f
, "variable pool:\n\n");
103 FOR_EACH_VARIABLE (node
)
104 dump_varpool_node (f
, node
);
107 /* Dump the variable pool to stderr. */
112 dump_varpool (stderr
);
115 /* Given an assembler name, lookup node. */
116 struct varpool_node
*
117 varpool_node_for_asm (tree asmname
)
119 if (symtab_node node
= symtab_node_for_asm (asmname
))
120 if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
125 /* Determine if variable DECL is needed. That is, visible to something
126 either outside this translation unit, something magic in the system
129 decide_is_variable_needed (struct varpool_node
*node
, tree decl
)
131 if (DECL_EXTERNAL (decl
))
134 /* If the user told us it is used, then it must be so. */
135 if (node
->symbol
.force_output
)
138 /* Externally visible variables must be output. The exception is
139 COMDAT variables that must be output only when they are needed. */
140 if (TREE_PUBLIC (decl
)
141 && !DECL_COMDAT (decl
))
147 /* Return if DECL is constant and its initial value is known (so we can do
148 constant folding using DECL_INITIAL (decl)). */
151 const_value_known_p (tree decl
)
153 if (TREE_CODE (decl
) != VAR_DECL
154 &&TREE_CODE (decl
) != CONST_DECL
)
157 if (TREE_CODE (decl
) == CONST_DECL
158 || DECL_IN_CONSTANT_POOL (decl
))
161 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
163 if (!TREE_READONLY (decl
) || TREE_THIS_VOLATILE (decl
))
166 /* Gimplifier takes away constructors of local vars */
167 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
168 return DECL_INITIAL (decl
) != NULL
;
170 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
172 /* Variables declared 'const' without an initializer
173 have zero as the initializer if they may not be
174 overridden at link or run time. */
175 if (!DECL_INITIAL (decl
)
176 && (DECL_EXTERNAL (decl
)
177 || decl_replaceable_p (decl
)))
180 /* Variables declared `const' with an initializer are considered
181 to not be overwritable with different initializer by default.
183 ??? Previously we behaved so for scalar variables but not for array
188 /* Add the variable DECL to the varpool.
189 Unlike varpool_finalize_decl function is intended to be used
190 by middle end and allows insertion of new variable at arbitrary point
193 varpool_add_new_variable (tree decl
)
195 struct varpool_node
*node
;
196 varpool_finalize_decl (decl
);
197 node
= varpool_node_for_decl (decl
);
198 if (varpool_externally_visible_p (node
))
199 node
->symbol
.externally_visible
= true;
202 /* Return variable availability. See cgraph.h for description of individual
205 cgraph_variable_initializer_availability (struct varpool_node
*node
)
207 gcc_assert (cgraph_function_flags_ready
);
208 if (!node
->finalized
)
209 return AVAIL_NOT_AVAILABLE
;
210 if (!TREE_PUBLIC (node
->symbol
.decl
))
211 return AVAIL_AVAILABLE
;
212 /* If the variable can be overwritten, return OVERWRITABLE. Takes
213 care of at least two notable extensions - the COMDAT variables
214 used to share template instantiations in C++. */
215 if (!decl_replaceable_p (node
->symbol
.decl
))
216 return AVAIL_OVERWRITABLE
;
217 return AVAIL_AVAILABLE
;
221 varpool_analyze_node (struct varpool_node
*node
)
223 tree decl
= node
->symbol
.decl
;
225 /* When reading back varpool at LTO time, we re-construct the queue in order
226 to have "needed" list right by inserting all needed nodes into varpool.
227 We however don't want to re-analyze already analyzed nodes. */
230 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
231 /* Compute the alignment early so function body expanders are
232 already informed about increased alignment. */
233 align_variable (decl
, 0);
235 if (node
->alias
&& node
->alias_of
)
237 struct varpool_node
*tgt
= varpool_node_for_decl (node
->alias_of
);
238 struct varpool_node
*n
;
240 for (n
= tgt
; n
&& n
->alias
;
241 n
= n
->analyzed
? varpool_alias_aliased_node (n
) : NULL
)
244 error ("variable %q+D part of alias cycle", node
->symbol
.decl
);
248 if (!vec_safe_length (node
->symbol
.ref_list
.references
))
249 ipa_record_reference ((symtab_node
)node
, (symtab_node
)tgt
, IPA_REF_ALIAS
, NULL
);
250 if (node
->extra_name_alias
)
252 DECL_WEAK (node
->symbol
.decl
) = DECL_WEAK (node
->alias_of
);
253 DECL_EXTERNAL (node
->symbol
.decl
) = DECL_EXTERNAL (node
->alias_of
);
254 DECL_VISIBILITY (node
->symbol
.decl
) = DECL_VISIBILITY (node
->alias_of
);
255 fixup_same_cpp_alias_visibility ((symtab_node
) node
,
256 (symtab_node
) tgt
, node
->alias_of
);
259 else if (DECL_INITIAL (decl
))
260 record_references_in_initializer (decl
, node
->analyzed
);
261 node
->analyzed
= true;
264 /* Assemble thunks and aliases associated to NODE. */
267 assemble_aliases (struct varpool_node
*node
)
271 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
272 if (ref
->use
== IPA_REF_ALIAS
)
274 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
275 do_assemble_alias (alias
->symbol
.decl
,
276 DECL_ASSEMBLER_NAME (alias
->alias_of
));
277 assemble_aliases (alias
);
281 /* Output one variable, if necessary. Return whether we output it. */
284 varpool_assemble_decl (struct varpool_node
*node
)
286 tree decl
= node
->symbol
.decl
;
288 /* Aliases are outout when their target is produced or by
293 /* Constant pool is output from RTL land when the reference
294 survive till this level. */
295 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
298 /* Decls with VALUE_EXPR should not be in the varpool at all. They
299 are not real variables, but just info for debugging and codegen.
300 Unfortunately at the moment emutls is not updating varpool correctly
301 after turning real vars into value_expr vars. */
302 if (DECL_HAS_VALUE_EXPR_P (decl
)
303 && !targetm
.have_tls
)
306 /* Hard register vars do not need to be output. */
307 if (DECL_HARD_REGISTER (decl
))
310 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
311 && TREE_CODE (decl
) == VAR_DECL
312 && !DECL_HAS_VALUE_EXPR_P (decl
));
314 if (!node
->symbol
.in_other_partition
315 && !DECL_EXTERNAL (decl
))
317 assemble_variable (decl
, 0, 1, 0);
318 gcc_assert (TREE_ASM_WRITTEN (decl
));
320 assemble_aliases (node
);
327 /* Add NODE to queue starting at FIRST.
328 The queue is linked via AUX pointers and terminated by pointer to 1. */
331 enqueue_node (struct varpool_node
*node
, struct varpool_node
**first
)
333 if (node
->symbol
.aux
)
335 gcc_checking_assert (*first
);
336 node
->symbol
.aux
= *first
;
340 /* Optimization of function bodies might've rendered some variables as
341 unnecessary so we want to avoid these from being compiled. Re-do
342 reachability starting from variables that are either externally visible
343 or was referred from the asm output routines. */
346 varpool_remove_unreferenced_decls (void)
348 struct varpool_node
*next
, *node
;
349 struct varpool_node
*first
= (struct varpool_node
*)(void *)1;
356 if (cgraph_dump_file
)
357 fprintf (cgraph_dump_file
, "Trivially needed variables:");
358 FOR_EACH_DEFINED_VARIABLE (node
)
361 && (!varpool_can_remove_if_no_refs (node
)
362 /* We just expanded all function bodies. See if any of
363 them needed the variable. */
364 || DECL_RTL_SET_P (node
->symbol
.decl
)))
366 enqueue_node (node
, &first
);
367 if (cgraph_dump_file
)
368 fprintf (cgraph_dump_file
, " %s", varpool_node_asm_name (node
));
371 while (first
!= (struct varpool_node
*)(void *)1)
374 first
= (struct varpool_node
*)first
->symbol
.aux
;
376 if (node
->symbol
.same_comdat_group
)
379 for (next
= node
->symbol
.same_comdat_group
;
380 next
!= (symtab_node
)node
;
381 next
= next
->symbol
.same_comdat_group
)
383 varpool_node
*vnext
= dyn_cast
<varpool_node
> (next
);
384 if (vnext
&& vnext
->analyzed
)
385 enqueue_node (vnext
, &first
);
388 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
390 varpool_node
*vnode
= dyn_cast
<varpool_node
> (ref
->referred
);
392 && (!DECL_EXTERNAL (ref
->referred
->symbol
.decl
)
395 enqueue_node (vnode
, &first
);
398 if (cgraph_dump_file
)
399 fprintf (cgraph_dump_file
, "\nRemoving variables:");
400 for (node
= varpool_first_defined_variable (); node
; node
= next
)
402 next
= varpool_next_defined_variable (node
);
403 if (!node
->symbol
.aux
)
405 if (cgraph_dump_file
)
406 fprintf (cgraph_dump_file
, " %s", varpool_node_asm_name (node
));
407 varpool_remove_node (node
);
410 if (cgraph_dump_file
)
411 fprintf (cgraph_dump_file
, "\n");
414 /* For variables in named sections make sure get_variable_section
415 is called before we switch to those sections. Then section
416 conflicts between read-only and read-only requiring relocations
417 sections can be resolved. */
419 varpool_finalize_named_section_flags (struct varpool_node
*node
)
421 if (!TREE_ASM_WRITTEN (node
->symbol
.decl
)
423 && !node
->symbol
.in_other_partition
424 && !DECL_EXTERNAL (node
->symbol
.decl
)
425 && TREE_CODE (node
->symbol
.decl
) == VAR_DECL
426 && !DECL_HAS_VALUE_EXPR_P (node
->symbol
.decl
)
427 && DECL_SECTION_NAME (node
->symbol
.decl
))
428 get_variable_section (node
->symbol
.decl
, false);
431 /* Output all variables enqueued to be assembled. */
433 varpool_output_variables (void)
435 bool changed
= false;
436 struct varpool_node
*node
;
441 varpool_remove_unreferenced_decls ();
443 timevar_push (TV_VAROUT
);
445 FOR_EACH_DEFINED_VARIABLE (node
)
446 varpool_finalize_named_section_flags (node
);
448 FOR_EACH_DEFINED_VARIABLE (node
)
449 if (varpool_assemble_decl (node
))
451 timevar_pop (TV_VAROUT
);
455 /* Create a new global variable of type TYPE. */
457 add_new_static_var (tree type
)
460 struct varpool_node
*new_node
;
462 new_decl
= create_tmp_var_raw (type
, NULL
);
463 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
464 TREE_READONLY (new_decl
) = 0;
465 TREE_STATIC (new_decl
) = 1;
466 TREE_USED (new_decl
) = 1;
467 DECL_CONTEXT (new_decl
) = NULL_TREE
;
468 DECL_ABSTRACT (new_decl
) = 0;
469 lang_hooks
.dup_lang_specific_decl (new_decl
);
470 new_node
= varpool_node_for_decl (new_decl
);
471 varpool_finalize_decl (new_decl
);
473 return new_node
->symbol
.decl
;
476 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
477 Extra name aliases are output whenever DECL is output. */
479 struct varpool_node
*
480 varpool_create_variable_alias (tree alias
, tree decl
)
482 struct varpool_node
*alias_node
;
484 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
485 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
486 alias_node
= varpool_node_for_decl (alias
);
487 alias_node
->alias
= 1;
488 alias_node
->finalized
= 1;
489 alias_node
->alias_of
= decl
;
491 /* Extra name alias mechanizm creates aliases really late
492 via DECL_ASSEMBLER_NAME mechanizm.
493 This is unfortunate because they are not going through the
494 standard channels. Ensure they get output. */
495 if (cgraph_state
>= CGRAPH_STATE_IPA
)
497 varpool_analyze_node (alias_node
);
498 if (TREE_PUBLIC (alias
))
499 alias_node
->symbol
.externally_visible
= true;
504 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
505 Extra name aliases are output whenever DECL is output. */
507 struct varpool_node
*
508 varpool_extra_name_alias (tree alias
, tree decl
)
510 struct varpool_node
*alias_node
;
512 #ifndef ASM_OUTPUT_DEF
513 /* If aliases aren't supported by the assembler, fail. */
516 alias_node
= varpool_create_variable_alias (alias
, decl
);
517 alias_node
->extra_name_alias
= true;
521 /* Call calback on NODE and aliases associated to NODE.
522 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
526 varpool_for_node_and_aliases (struct varpool_node
*node
,
527 bool (*callback
) (struct varpool_node
*, void *),
529 bool include_overwritable
)
534 if (callback (node
, data
))
536 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
537 if (ref
->use
== IPA_REF_ALIAS
)
539 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
540 if (include_overwritable
541 || cgraph_variable_initializer_availability (alias
) > AVAIL_OVERWRITABLE
)
542 if (varpool_for_node_and_aliases (alias
, callback
, data
,
543 include_overwritable
))