* symtab.c (symtab_resolve_alias): Do not remove alias attribute.
[gcc.git] / gcc / varpool.c
1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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
10 version.
11
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
15 for more details.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "cgraph.h"
27 #include "langhooks.h"
28 #include "diagnostic-core.h"
29 #include "hashtab.h"
30 #include "ggc.h"
31 #include "timevar.h"
32 #include "debug.h"
33 #include "target.h"
34 #include "output.h"
35 #include "gimple.h"
36 #include "tree-flow.h"
37 #include "flags.h"
38
39 /* Return varpool node assigned to DECL. Create new one when needed. */
40 struct varpool_node *
41 varpool_node_for_decl (tree decl)
42 {
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));
46 if (node)
47 return node;
48
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);
53 return node;
54 }
55
56 /* Remove node from the varpool. */
57 void
58 varpool_remove_node (struct varpool_node *node)
59 {
60 symtab_unregister_node ((symtab_node)node);
61
62 /* Because we remove references from external functions before final compilation,
63 we may end up removing useful constructors.
64 FIXME: We probably want to trace boundaries better. */
65 if (!const_value_known_p (node->symbol.decl))
66 varpool_remove_initializer (node);
67 ggc_free (node);
68 }
69
70 /* Renove node initializer when it is no longer needed. */
71 void
72 varpool_remove_initializer (struct varpool_node *node)
73 {
74 if (DECL_INITIAL (node->symbol.decl)
75 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
76 /* Keep vtables for BINFO folding. */
77 && !DECL_VIRTUAL_P (node->symbol.decl)
78 /* FIXME: http://gcc.gnu.org/PR55395 */
79 && debug_info_level == DINFO_LEVEL_NONE)
80 DECL_INITIAL (node->symbol.decl) = error_mark_node;
81 }
82
83 /* Dump given cgraph node. */
84 void
85 dump_varpool_node (FILE *f, struct varpool_node *node)
86 {
87 dump_symtab_base (f, (symtab_node)node);
88 fprintf (f, " Availability: %s\n",
89 cgraph_function_flags_ready
90 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
91 : "not-ready");
92 fprintf (f, " Varpool flags:");
93 if (DECL_INITIAL (node->symbol.decl))
94 fprintf (f, " initialized");
95 if (node->output)
96 fprintf (f, " output");
97 if (TREE_READONLY (node->symbol.decl))
98 fprintf (f, " read-only");
99 if (const_value_known_p (node->symbol.decl))
100 fprintf (f, " const-value-known");
101 fprintf (f, "\n");
102 }
103
104 /* Dump the variable pool. */
105 void
106 dump_varpool (FILE *f)
107 {
108 struct varpool_node *node;
109
110 fprintf (f, "variable pool:\n\n");
111 FOR_EACH_VARIABLE (node)
112 dump_varpool_node (f, node);
113 }
114
115 /* Dump the variable pool to stderr. */
116
117 DEBUG_FUNCTION void
118 debug_varpool (void)
119 {
120 dump_varpool (stderr);
121 }
122
123 /* Given an assembler name, lookup node. */
124 struct varpool_node *
125 varpool_node_for_asm (tree asmname)
126 {
127 if (symtab_node node = symtab_node_for_asm (asmname))
128 return dyn_cast <varpool_node> (node);
129 else
130 return NULL;
131 }
132
133 /* Return if DECL is constant and its initial value is known (so we can do
134 constant folding using DECL_INITIAL (decl)). */
135
136 bool
137 const_value_known_p (tree decl)
138 {
139 if (TREE_CODE (decl) != VAR_DECL
140 &&TREE_CODE (decl) != CONST_DECL)
141 return false;
142
143 if (TREE_CODE (decl) == CONST_DECL
144 || DECL_IN_CONSTANT_POOL (decl))
145 return true;
146
147 gcc_assert (TREE_CODE (decl) == VAR_DECL);
148
149 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl))
150 return false;
151
152 /* Gimplifier takes away constructors of local vars */
153 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
154 return DECL_INITIAL (decl) != NULL;
155
156 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
157
158 /* Variables declared 'const' without an initializer
159 have zero as the initializer if they may not be
160 overridden at link or run time. */
161 if (!DECL_INITIAL (decl)
162 && (DECL_EXTERNAL (decl)
163 || decl_replaceable_p (decl)))
164 return false;
165
166 /* Variables declared `const' with an initializer are considered
167 to not be overwritable with different initializer by default.
168
169 ??? Previously we behaved so for scalar variables but not for array
170 accesses. */
171 return true;
172 }
173
174 /* Add the variable DECL to the varpool.
175 Unlike varpool_finalize_decl function is intended to be used
176 by middle end and allows insertion of new variable at arbitrary point
177 of compilation. */
178 void
179 varpool_add_new_variable (tree decl)
180 {
181 struct varpool_node *node;
182 varpool_finalize_decl (decl);
183 node = varpool_node_for_decl (decl);
184 if (varpool_externally_visible_p (node))
185 node->symbol.externally_visible = true;
186 }
187
188 /* Return variable availability. See cgraph.h for description of individual
189 return values. */
190 enum availability
191 cgraph_variable_initializer_availability (struct varpool_node *node)
192 {
193 gcc_assert (cgraph_function_flags_ready);
194 if (!node->symbol.definition)
195 return AVAIL_NOT_AVAILABLE;
196 if (!TREE_PUBLIC (node->symbol.decl))
197 return AVAIL_AVAILABLE;
198 /* If the variable can be overwritten, return OVERWRITABLE. Takes
199 care of at least one notable extension - the COMDAT variables
200 used to share template instantiations in C++. */
201 if (!decl_replaceable_p (node->symbol.decl))
202 return AVAIL_OVERWRITABLE;
203 return AVAIL_AVAILABLE;
204 }
205
206 void
207 varpool_analyze_node (struct varpool_node *node)
208 {
209 tree decl = node->symbol.decl;
210
211 /* When reading back varpool at LTO time, we re-construct the queue in order
212 to have "needed" list right by inserting all needed nodes into varpool.
213 We however don't want to re-analyze already analyzed nodes. */
214 if (!node->symbol.analyzed)
215 {
216 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
217 /* Compute the alignment early so function body expanders are
218 already informed about increased alignment. */
219 align_variable (decl, 0);
220 }
221 if (node->symbol.alias)
222 symtab_resolve_alias
223 ((symtab_node) node, (symtab_node) varpool_get_node (node->symbol.alias_target));
224 else if (DECL_INITIAL (decl))
225 record_references_in_initializer (decl, node->symbol.analyzed);
226 node->symbol.analyzed = true;
227 }
228
229 /* Assemble thunks and aliases associated to NODE. */
230
231 static void
232 assemble_aliases (struct varpool_node *node)
233 {
234 int i;
235 struct ipa_ref *ref;
236 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
237 if (ref->use == IPA_REF_ALIAS)
238 {
239 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
240 do_assemble_alias (alias->symbol.decl,
241 DECL_ASSEMBLER_NAME (node->symbol.decl));
242 assemble_aliases (alias);
243 }
244 }
245
246 /* Output one variable, if necessary. Return whether we output it. */
247
248 bool
249 varpool_assemble_decl (struct varpool_node *node)
250 {
251 tree decl = node->symbol.decl;
252
253 /* Aliases are outout when their target is produced or by
254 output_weakrefs. */
255 if (node->symbol.alias)
256 return false;
257
258 /* Constant pool is output from RTL land when the reference
259 survive till this level. */
260 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
261 return false;
262
263 /* Decls with VALUE_EXPR should not be in the varpool at all. They
264 are not real variables, but just info for debugging and codegen.
265 Unfortunately at the moment emutls is not updating varpool correctly
266 after turning real vars into value_expr vars. */
267 if (DECL_HAS_VALUE_EXPR_P (decl)
268 && !targetm.have_tls)
269 return false;
270
271 /* Hard register vars do not need to be output. */
272 if (DECL_HARD_REGISTER (decl))
273 return false;
274
275 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
276 && TREE_CODE (decl) == VAR_DECL
277 && !DECL_HAS_VALUE_EXPR_P (decl));
278
279 if (!node->symbol.in_other_partition
280 && !DECL_EXTERNAL (decl))
281 {
282 assemble_variable (decl, 0, 1, 0);
283 gcc_assert (TREE_ASM_WRITTEN (decl));
284 node->symbol.definition = true;
285 assemble_aliases (node);
286 return true;
287 }
288
289 return false;
290 }
291
292 /* Add NODE to queue starting at FIRST.
293 The queue is linked via AUX pointers and terminated by pointer to 1. */
294
295 static void
296 enqueue_node (struct varpool_node *node, struct varpool_node **first)
297 {
298 if (node->symbol.aux)
299 return;
300 gcc_checking_assert (*first);
301 node->symbol.aux = *first;
302 *first = node;
303 }
304
305 /* Optimization of function bodies might've rendered some variables as
306 unnecessary so we want to avoid these from being compiled. Re-do
307 reachability starting from variables that are either externally visible
308 or was referred from the asm output routines. */
309
310 static void
311 varpool_remove_unreferenced_decls (void)
312 {
313 struct varpool_node *next, *node;
314 struct varpool_node *first = (struct varpool_node *)(void *)1;
315 int i;
316 struct ipa_ref *ref;
317
318 if (seen_error ())
319 return;
320
321 if (cgraph_dump_file)
322 fprintf (cgraph_dump_file, "Trivially needed variables:");
323 FOR_EACH_DEFINED_VARIABLE (node)
324 {
325 if (node->symbol.analyzed
326 && (!varpool_can_remove_if_no_refs (node)
327 /* We just expanded all function bodies. See if any of
328 them needed the variable. */
329 || DECL_RTL_SET_P (node->symbol.decl)))
330 {
331 enqueue_node (node, &first);
332 if (cgraph_dump_file)
333 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
334 }
335 }
336 while (first != (struct varpool_node *)(void *)1)
337 {
338 node = first;
339 first = (struct varpool_node *)first->symbol.aux;
340
341 if (node->symbol.same_comdat_group)
342 {
343 symtab_node next;
344 for (next = node->symbol.same_comdat_group;
345 next != (symtab_node)node;
346 next = next->symbol.same_comdat_group)
347 {
348 varpool_node *vnext = dyn_cast <varpool_node> (next);
349 if (vnext && vnext->symbol.analyzed)
350 enqueue_node (vnext, &first);
351 }
352 }
353 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
354 {
355 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
356 if (vnode
357 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
358 || vnode->symbol.alias)
359 && vnode->symbol.analyzed)
360 enqueue_node (vnode, &first);
361 }
362 }
363 if (cgraph_dump_file)
364 fprintf (cgraph_dump_file, "\nRemoving variables:");
365 for (node = varpool_first_defined_variable (); node; node = next)
366 {
367 next = varpool_next_defined_variable (node);
368 if (!node->symbol.aux)
369 {
370 if (cgraph_dump_file)
371 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
372 varpool_remove_node (node);
373 }
374 }
375 if (cgraph_dump_file)
376 fprintf (cgraph_dump_file, "\n");
377 }
378
379 /* For variables in named sections make sure get_variable_section
380 is called before we switch to those sections. Then section
381 conflicts between read-only and read-only requiring relocations
382 sections can be resolved. */
383 void
384 varpool_finalize_named_section_flags (struct varpool_node *node)
385 {
386 if (!TREE_ASM_WRITTEN (node->symbol.decl)
387 && !node->symbol.alias
388 && !node->symbol.in_other_partition
389 && !DECL_EXTERNAL (node->symbol.decl)
390 && TREE_CODE (node->symbol.decl) == VAR_DECL
391 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
392 && DECL_SECTION_NAME (node->symbol.decl))
393 get_variable_section (node->symbol.decl, false);
394 }
395
396 /* Output all variables enqueued to be assembled. */
397 bool
398 varpool_output_variables (void)
399 {
400 bool changed = false;
401 struct varpool_node *node;
402
403 if (seen_error ())
404 return false;
405
406 varpool_remove_unreferenced_decls ();
407
408 timevar_push (TV_VAROUT);
409
410 FOR_EACH_DEFINED_VARIABLE (node)
411 varpool_finalize_named_section_flags (node);
412
413 FOR_EACH_DEFINED_VARIABLE (node)
414 if (varpool_assemble_decl (node))
415 changed = true;
416 timevar_pop (TV_VAROUT);
417 return changed;
418 }
419
420 /* Create a new global variable of type TYPE. */
421 tree
422 add_new_static_var (tree type)
423 {
424 tree new_decl;
425 struct varpool_node *new_node;
426
427 new_decl = create_tmp_var_raw (type, NULL);
428 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
429 TREE_READONLY (new_decl) = 0;
430 TREE_STATIC (new_decl) = 1;
431 TREE_USED (new_decl) = 1;
432 DECL_CONTEXT (new_decl) = NULL_TREE;
433 DECL_ABSTRACT (new_decl) = 0;
434 lang_hooks.dup_lang_specific_decl (new_decl);
435 new_node = varpool_node_for_decl (new_decl);
436 varpool_finalize_decl (new_decl);
437
438 return new_node->symbol.decl;
439 }
440
441 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
442 Extra name aliases are output whenever DECL is output. */
443
444 struct varpool_node *
445 varpool_create_variable_alias (tree alias, tree decl)
446 {
447 struct varpool_node *alias_node;
448
449 gcc_assert (TREE_CODE (decl) == VAR_DECL);
450 gcc_assert (TREE_CODE (alias) == VAR_DECL);
451 alias_node = varpool_node_for_decl (alias);
452 alias_node->symbol.alias = true;
453 alias_node->symbol.definition = true;
454 alias_node->symbol.alias_target = decl;
455 return alias_node;
456 }
457
458 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
459 Extra name aliases are output whenever DECL is output. */
460
461 struct varpool_node *
462 varpool_extra_name_alias (tree alias, tree decl)
463 {
464 struct varpool_node *alias_node;
465
466 #ifndef ASM_OUTPUT_DEF
467 /* If aliases aren't supported by the assembler, fail. */
468 return NULL;
469 #endif
470 alias_node = varpool_create_variable_alias (alias, decl);
471 alias_node->symbol.cpp_implicit_alias = true;
472
473 /* Extra name alias mechanizm creates aliases really late
474 via DECL_ASSEMBLER_NAME mechanizm.
475 This is unfortunate because they are not going through the
476 standard channels. Ensure they get output. */
477 if (cpp_implicit_aliases_done)
478 symtab_resolve_alias ((symtab_node)alias_node,
479 (symtab_node)varpool_node_for_decl (decl));
480 return alias_node;
481 }
482
483 /* Call calback on NODE and aliases associated to NODE.
484 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
485 skipped. */
486
487 bool
488 varpool_for_node_and_aliases (struct varpool_node *node,
489 bool (*callback) (struct varpool_node *, void *),
490 void *data,
491 bool include_overwritable)
492 {
493 int i;
494 struct ipa_ref *ref;
495
496 if (callback (node, data))
497 return true;
498 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
499 if (ref->use == IPA_REF_ALIAS)
500 {
501 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
502 if (include_overwritable
503 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
504 if (varpool_for_node_and_aliases (alias, callback, data,
505 include_overwritable))
506 return true;
507 }
508 return false;
509 }