1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
37 #include "tree-flow.h"
40 /* Return varpool node assigned to DECL. Create new one when needed. */
42 varpool_node (tree decl
)
44 struct varpool_node
*node
= varpool_get_node (decl
);
45 gcc_assert (TREE_CODE (decl
) == VAR_DECL
46 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
) || in_lto_p
));
50 node
= ggc_alloc_cleared_varpool_node ();
51 node
->symbol
.type
= SYMTAB_VARIABLE
;
52 node
->symbol
.decl
= decl
;
53 symtab_register_node ((symtab_node
)node
);
57 /* Remove node from the varpool. */
59 varpool_remove_node (struct varpool_node
*node
)
61 symtab_unregister_node ((symtab_node
)node
);
62 if (DECL_INITIAL (node
->symbol
.decl
)
63 && !DECL_IN_CONSTANT_POOL (node
->symbol
.decl
)
64 /* Keep vtables for BINFO folding. */
65 && !DECL_VIRTUAL_P (node
->symbol
.decl
)
66 /* dbxout output constant initializers for readonly vars. */
67 && (!host_integerp (DECL_INITIAL (node
->symbol
.decl
), 0)
68 || !TREE_READONLY (node
->symbol
.decl
)))
69 DECL_INITIAL (node
->symbol
.decl
) = error_mark_node
;
73 /* Dump given cgraph node. */
75 dump_varpool_node (FILE *f
, struct varpool_node
*node
)
77 dump_symtab_base (f
, (symtab_node
)node
);
78 fprintf (f
, " Availability: %s\n",
79 cgraph_function_flags_ready
80 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
82 fprintf (f
, " Varpool flags:");
83 if (DECL_INITIAL (node
->symbol
.decl
))
84 fprintf (f
, " initialized");
86 fprintf (f
, " analyzed");
88 fprintf (f
, " finalized");
90 fprintf (f
, " output");
94 /* Dump the variable pool. */
96 dump_varpool (FILE *f
)
98 struct varpool_node
*node
;
100 fprintf (f
, "variable pool:\n\n");
101 FOR_EACH_VARIABLE (node
)
102 dump_varpool_node (f
, node
);
105 /* Dump the variable pool to stderr. */
110 dump_varpool (stderr
);
113 /* Given an assembler name, lookup node. */
114 struct varpool_node
*
115 varpool_node_for_asm (tree asmname
)
117 symtab_node node
= symtab_node_for_asm (asmname
);
118 if (node
&& symtab_variable_p (node
))
119 return varpool (node
);
123 /* Determine if variable DECL is needed. That is, visible to something
124 either outside this translation unit, something magic in the system
127 decide_is_variable_needed (struct varpool_node
*node
, tree decl
)
129 if (DECL_EXTERNAL (decl
))
132 /* If the user told us it is used, then it must be so. */
133 if (node
->symbol
.force_output
)
136 /* Externally visible variables must be output. The exception is
137 COMDAT variables that must be output only when they are needed. */
138 if (TREE_PUBLIC (decl
)
139 && !DECL_COMDAT (decl
))
145 /* Return if DECL is constant and its initial value is known (so we can do
146 constant folding using DECL_INITIAL (decl)). */
149 const_value_known_p (tree decl
)
151 if (TREE_CODE (decl
) != VAR_DECL
152 &&TREE_CODE (decl
) != CONST_DECL
)
155 if (TREE_CODE (decl
) == CONST_DECL
156 || DECL_IN_CONSTANT_POOL (decl
))
159 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
161 if (!TREE_READONLY (decl
) || TREE_THIS_VOLATILE (decl
))
164 /* Gimplifier takes away constructors of local vars */
165 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
166 return DECL_INITIAL (decl
) != NULL
;
168 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
170 /* Variables declared 'const' without an initializer
171 have zero as the initializer if they may not be
172 overridden at link or run time. */
173 if (!DECL_INITIAL (decl
)
174 && (DECL_EXTERNAL (decl
)
175 || decl_replaceable_p (decl
)))
178 /* Variables declared `const' with an initializer are considered
179 to not be overwritable with different initializer by default.
181 ??? Previously we behaved so for scalar variables but not for array
186 /* Add the variable DECL to the varpool.
187 Unlike varpool_finalize_decl function is intended to be used
188 by middle end and allows insertion of new variable at arbitrary point
191 varpool_add_new_variable (tree decl
)
193 struct varpool_node
*node
;
194 varpool_finalize_decl (decl
);
195 node
= varpool_node (decl
);
196 if (varpool_externally_visible_p (node
, false))
197 node
->symbol
.externally_visible
= true;
200 /* Return variable availability. See cgraph.h for description of individual
203 cgraph_variable_initializer_availability (struct varpool_node
*node
)
205 gcc_assert (cgraph_function_flags_ready
);
206 if (!node
->finalized
)
207 return AVAIL_NOT_AVAILABLE
;
208 if (!TREE_PUBLIC (node
->symbol
.decl
))
209 return AVAIL_AVAILABLE
;
210 /* If the variable can be overwritten, return OVERWRITABLE. Takes
211 care of at least two notable extensions - the COMDAT variables
212 used to share template instantiations in C++. */
213 if (!decl_replaceable_p (node
->symbol
.decl
))
214 return AVAIL_OVERWRITABLE
;
215 return AVAIL_AVAILABLE
;
219 varpool_analyze_node (struct varpool_node
*node
)
221 tree decl
= node
->symbol
.decl
;
223 /* When reading back varpool at LTO time, we re-construct the queue in order
224 to have "needed" list right by inserting all needed nodes into varpool.
225 We however don't want to re-analyze already analyzed nodes. */
228 gcc_assert (!in_lto_p
|| cgraph_function_flags_ready
);
229 /* Compute the alignment early so function body expanders are
230 already informed about increased alignment. */
231 align_variable (decl
, 0);
233 if (node
->alias
&& node
->alias_of
)
235 struct varpool_node
*tgt
= varpool_node (node
->alias_of
);
236 struct varpool_node
*n
;
238 for (n
= tgt
; n
&& n
->alias
;
239 n
= n
->analyzed
? varpool_alias_aliased_node (n
) : NULL
)
242 error ("variable %q+D part of alias cycle", node
->symbol
.decl
);
246 if (!VEC_length (ipa_ref_t
, node
->symbol
.ref_list
.references
))
247 ipa_record_reference ((symtab_node
)node
, (symtab_node
)tgt
, IPA_REF_ALIAS
, NULL
);
248 if (node
->extra_name_alias
)
250 DECL_WEAK (node
->symbol
.decl
) = DECL_WEAK (node
->alias_of
);
251 DECL_EXTERNAL (node
->symbol
.decl
) = DECL_EXTERNAL (node
->alias_of
);
252 DECL_VISIBILITY (node
->symbol
.decl
) = DECL_VISIBILITY (node
->alias_of
);
253 fixup_same_cpp_alias_visibility ((symtab_node
) node
,
254 (symtab_node
) tgt
, node
->alias_of
);
257 else if (DECL_INITIAL (decl
))
258 record_references_in_initializer (decl
, node
->analyzed
);
259 node
->analyzed
= true;
262 /* Assemble thunks and aliases associated to NODE. */
265 assemble_aliases (struct varpool_node
*node
)
269 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
270 if (ref
->use
== IPA_REF_ALIAS
)
272 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
273 do_assemble_alias (alias
->symbol
.decl
,
274 DECL_ASSEMBLER_NAME (alias
->alias_of
));
275 assemble_aliases (alias
);
279 /* Output one variable, if necessary. Return whether we output it. */
282 varpool_assemble_decl (struct varpool_node
*node
)
284 tree decl
= node
->symbol
.decl
;
286 /* Aliases are outout when their target is produced or by
291 /* Constant pool is output from RTL land when the reference
292 survive till this level. */
293 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
296 /* Decls with VALUE_EXPR should not be in the varpool at all. They
297 are not real variables, but just info for debugging and codegen.
298 Unfortunately at the moment emutls is not updating varpool correctly
299 after turning real vars into value_expr vars. */
300 if (DECL_HAS_VALUE_EXPR_P (decl
)
301 && !targetm
.have_tls
)
304 /* Hard register vars do not need to be output. */
305 if (DECL_HARD_REGISTER (decl
))
308 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
309 && TREE_CODE (decl
) == VAR_DECL
310 && !DECL_HAS_VALUE_EXPR_P (decl
));
312 if (!node
->symbol
.in_other_partition
313 && !DECL_EXTERNAL (decl
))
315 assemble_variable (decl
, 0, 1, 0);
316 gcc_assert (TREE_ASM_WRITTEN (decl
));
318 assemble_aliases (node
);
325 /* Add NODE to queue starting at FIRST.
326 The queue is linked via AUX pointers and terminated by pointer to 1. */
329 enqueue_node (struct varpool_node
*node
, struct varpool_node
**first
)
331 if (node
->symbol
.aux
)
333 gcc_checking_assert (*first
);
334 node
->symbol
.aux
= *first
;
338 /* Optimization of function bodies might've rendered some variables as
339 unnecessary so we want to avoid these from being compiled. Re-do
340 reachability starting from variables that are either externally visible
341 or was referred from the asm output routines. */
344 varpool_remove_unreferenced_decls (void)
346 struct varpool_node
*next
, *node
;
347 struct varpool_node
*first
= (struct varpool_node
*)(void *)1;
354 if (cgraph_dump_file
)
355 fprintf (cgraph_dump_file
, "Trivially needed variables:");
356 FOR_EACH_DEFINED_VARIABLE (node
)
359 && (!varpool_can_remove_if_no_refs (node
)
360 /* We just expanded all function bodies. See if any of
361 them needed the variable. */
362 || (!DECL_EXTERNAL (node
->symbol
.decl
)
363 && DECL_RTL_SET_P (node
->symbol
.decl
))))
365 enqueue_node (node
, &first
);
366 if (cgraph_dump_file
)
367 fprintf (cgraph_dump_file
, " %s", varpool_node_asm_name (node
));
370 while (first
!= (struct varpool_node
*)(void *)1)
373 first
= (struct varpool_node
*)first
->symbol
.aux
;
375 if (node
->symbol
.same_comdat_group
)
378 for (next
= node
->symbol
.same_comdat_group
;
379 next
!= (symtab_node
)node
;
380 next
= next
->symbol
.same_comdat_group
)
381 if (symtab_variable_p (next
)
382 && varpool (next
)->analyzed
)
383 enqueue_node (varpool (next
), &first
);
385 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
386 if (symtab_variable_p (ref
->referred
)
387 && (!DECL_EXTERNAL (ref
->referred
->symbol
.decl
)
388 || varpool (ref
->referred
)->alias
)
389 && varpool (ref
->referred
)->analyzed
)
390 enqueue_node (varpool (ref
->referred
), &first
);
392 if (cgraph_dump_file
)
393 fprintf (cgraph_dump_file
, "\nRemoving variables:");
394 for (node
= varpool_first_defined_variable (); node
; node
= next
)
396 next
= varpool_next_defined_variable (node
);
397 if (!node
->symbol
.aux
)
399 if (cgraph_dump_file
)
400 fprintf (cgraph_dump_file
, " %s", varpool_node_asm_name (node
));
401 varpool_remove_node (node
);
404 if (cgraph_dump_file
)
405 fprintf (cgraph_dump_file
, "\n");
408 /* For variables in named sections make sure get_variable_section
409 is called before we switch to those sections. Then section
410 conflicts between read-only and read-only requiring relocations
411 sections can be resolved. */
413 varpool_finalize_named_section_flags (struct varpool_node
*node
)
415 if (!TREE_ASM_WRITTEN (node
->symbol
.decl
)
417 && !node
->symbol
.in_other_partition
418 && !DECL_EXTERNAL (node
->symbol
.decl
)
419 && TREE_CODE (node
->symbol
.decl
) == VAR_DECL
420 && !DECL_HAS_VALUE_EXPR_P (node
->symbol
.decl
)
421 && DECL_SECTION_NAME (node
->symbol
.decl
))
422 get_variable_section (node
->symbol
.decl
, false);
425 /* Output all variables enqueued to be assembled. */
427 varpool_output_variables (void)
429 bool changed
= false;
430 struct varpool_node
*node
;
435 varpool_remove_unreferenced_decls ();
437 timevar_push (TV_VAROUT
);
439 FOR_EACH_DEFINED_VARIABLE (node
)
440 varpool_finalize_named_section_flags (node
);
442 FOR_EACH_DEFINED_VARIABLE (node
)
443 if (varpool_assemble_decl (node
))
445 timevar_pop (TV_VAROUT
);
449 /* Create a new global variable of type TYPE. */
451 add_new_static_var (tree type
)
454 struct varpool_node
*new_node
;
456 new_decl
= create_tmp_var_raw (type
, NULL
);
457 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
458 TREE_READONLY (new_decl
) = 0;
459 TREE_STATIC (new_decl
) = 1;
460 TREE_USED (new_decl
) = 1;
461 DECL_CONTEXT (new_decl
) = NULL_TREE
;
462 DECL_ABSTRACT (new_decl
) = 0;
463 lang_hooks
.dup_lang_specific_decl (new_decl
);
464 new_node
= varpool_node (new_decl
);
465 varpool_finalize_decl (new_decl
);
467 return new_node
->symbol
.decl
;
470 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
471 Extra name aliases are output whenever DECL is output. */
473 struct varpool_node
*
474 varpool_create_variable_alias (tree alias
, tree decl
)
476 struct varpool_node
*alias_node
;
478 gcc_assert (TREE_CODE (decl
) == VAR_DECL
);
479 gcc_assert (TREE_CODE (alias
) == VAR_DECL
);
480 alias_node
= varpool_node (alias
);
481 alias_node
->alias
= 1;
482 alias_node
->finalized
= 1;
483 alias_node
->alias_of
= decl
;
485 /* Extra name alias mechanizm creates aliases really late
486 via DECL_ASSEMBLER_NAME mechanizm.
487 This is unfortunate because they are not going through the
488 standard channels. Ensure they get output. */
489 if (cgraph_state
>= CGRAPH_STATE_IPA
)
491 varpool_analyze_node (alias_node
);
492 if (TREE_PUBLIC (alias
))
493 alias_node
->symbol
.externally_visible
= true;
498 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
499 Extra name aliases are output whenever DECL is output. */
501 struct varpool_node
*
502 varpool_extra_name_alias (tree alias
, tree decl
)
504 struct varpool_node
*alias_node
;
506 #ifndef ASM_OUTPUT_DEF
507 /* If aliases aren't supported by the assembler, fail. */
510 alias_node
= varpool_create_variable_alias (alias
, decl
);
511 alias_node
->extra_name_alias
= true;
515 /* Call calback on NODE and aliases associated to NODE.
516 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
520 varpool_for_node_and_aliases (struct varpool_node
*node
,
521 bool (*callback
) (struct varpool_node
*, void *),
523 bool include_overwritable
)
528 if (callback (node
, data
))
530 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
531 if (ref
->use
== IPA_REF_ALIAS
)
533 struct varpool_node
*alias
= ipa_ref_referring_varpool_node (ref
);
534 if (include_overwritable
535 || cgraph_variable_initializer_availability (alias
) > AVAIL_OVERWRITABLE
)
536 if (varpool_for_node_and_aliases (alias
, callback
, data
,
537 include_overwritable
))