]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/varpool.c
gimple.h: Remove all includes.
[thirdparty/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 "varasm.h"
27 #include "cgraph.h"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
30 #include "hashtab.h"
31 #include "timevar.h"
32 #include "debug.h"
33 #include "target.h"
34 #include "output.h"
35 #include "gimple-expr.h"
36 #include "flags.h"
37
38 /* List of hooks triggered on varpool_node events. */
39 struct varpool_node_hook_list {
40 varpool_node_hook hook;
41 void *data;
42 struct varpool_node_hook_list *next;
43 };
44
45 /* List of hooks triggered when a node is removed. */
46 struct varpool_node_hook_list *first_varpool_node_removal_hook;
47 /* List of hooks triggered when an variable is inserted. */
48 struct varpool_node_hook_list *first_varpool_variable_insertion_hook;
49
50 /* Register HOOK to be called with DATA on each removed node. */
51 struct varpool_node_hook_list *
52 varpool_add_node_removal_hook (varpool_node_hook hook, void *data)
53 {
54 struct varpool_node_hook_list *entry;
55 struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook;
56
57 entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry));
58 entry->hook = hook;
59 entry->data = data;
60 entry->next = NULL;
61 while (*ptr)
62 ptr = &(*ptr)->next;
63 *ptr = entry;
64 return entry;
65 }
66
67 /* Remove ENTRY from the list of hooks called on removing nodes. */
68 void
69 varpool_remove_node_removal_hook (struct varpool_node_hook_list *entry)
70 {
71 struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook;
72
73 while (*ptr != entry)
74 ptr = &(*ptr)->next;
75 *ptr = entry->next;
76 free (entry);
77 }
78
79 /* Call all node removal hooks. */
80 static void
81 varpool_call_node_removal_hooks (struct varpool_node *node)
82 {
83 struct varpool_node_hook_list *entry = first_varpool_node_removal_hook;
84 while (entry)
85 {
86 entry->hook (node, entry->data);
87 entry = entry->next;
88 }
89 }
90
91 /* Register HOOK to be called with DATA on each inserted node. */
92 struct varpool_node_hook_list *
93 varpool_add_variable_insertion_hook (varpool_node_hook hook, void *data)
94 {
95 struct varpool_node_hook_list *entry;
96 struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook;
97
98 entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry));
99 entry->hook = hook;
100 entry->data = data;
101 entry->next = NULL;
102 while (*ptr)
103 ptr = &(*ptr)->next;
104 *ptr = entry;
105 return entry;
106 }
107
108 /* Remove ENTRY from the list of hooks called on inserted nodes. */
109 void
110 varpool_remove_variable_insertion_hook (struct varpool_node_hook_list *entry)
111 {
112 struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook;
113
114 while (*ptr != entry)
115 ptr = &(*ptr)->next;
116 *ptr = entry->next;
117 free (entry);
118 }
119
120 /* Call all node insertion hooks. */
121 void
122 varpool_call_variable_insertion_hooks (struct varpool_node *node)
123 {
124 struct varpool_node_hook_list *entry = first_varpool_variable_insertion_hook;
125 while (entry)
126 {
127 entry->hook (node, entry->data);
128 entry = entry->next;
129 }
130 }
131
132 /* Allocate new callgraph node and insert it into basic data structures. */
133
134 struct varpool_node *
135 varpool_create_empty_node (void)
136 {
137 struct varpool_node *node = ggc_alloc_cleared_varpool_node ();
138 node->type = SYMTAB_VARIABLE;
139 return node;
140 }
141
142 /* Return varpool node assigned to DECL. Create new one when needed. */
143 struct varpool_node *
144 varpool_node_for_decl (tree decl)
145 {
146 struct varpool_node *node = varpool_get_node (decl);
147 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
148 if (node)
149 return node;
150
151 node = varpool_create_empty_node ();
152 node->decl = decl;
153 symtab_register_node (node);
154 return node;
155 }
156
157 /* Remove node from the varpool. */
158 void
159 varpool_remove_node (struct varpool_node *node)
160 {
161 tree init;
162 varpool_call_node_removal_hooks (node);
163 symtab_unregister_node (node);
164
165 /* Because we remove references from external functions before final compilation,
166 we may end up removing useful constructors.
167 FIXME: We probably want to trace boundaries better. */
168 if ((init = ctor_for_folding (node->decl)) == error_mark_node)
169 varpool_remove_initializer (node);
170 else
171 DECL_INITIAL (node->decl) = init;
172 ggc_free (node);
173 }
174
175 /* Renove node initializer when it is no longer needed. */
176 void
177 varpool_remove_initializer (struct varpool_node *node)
178 {
179 if (DECL_INITIAL (node->decl)
180 && !DECL_IN_CONSTANT_POOL (node->decl)
181 /* Keep vtables for BINFO folding. */
182 && !DECL_VIRTUAL_P (node->decl)
183 /* FIXME: http://gcc.gnu.org/PR55395 */
184 && debug_info_level == DINFO_LEVEL_NONE
185 /* When doing declaration merging we have duplicate
186 entries for given decl. Do not attempt to remove
187 the boides, or we will end up remiving
188 wrong one. */
189 && cgraph_state != CGRAPH_LTO_STREAMING)
190 DECL_INITIAL (node->decl) = error_mark_node;
191 }
192
193 /* Dump given cgraph node. */
194 void
195 dump_varpool_node (FILE *f, struct varpool_node *node)
196 {
197 dump_symtab_base (f, node);
198 fprintf (f, " Availability: %s\n",
199 cgraph_function_flags_ready
200 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
201 : "not-ready");
202 fprintf (f, " Varpool flags:");
203 if (DECL_INITIAL (node->decl))
204 fprintf (f, " initialized");
205 if (node->output)
206 fprintf (f, " output");
207 if (node->need_bounds_init)
208 fprintf (f, " need-bounds-init");
209 if (TREE_READONLY (node->decl))
210 fprintf (f, " read-only");
211 if (ctor_for_folding (node->decl) != error_mark_node)
212 fprintf (f, " const-value-known");
213 fprintf (f, "\n");
214 }
215
216 /* Dump the variable pool. */
217 void
218 dump_varpool (FILE *f)
219 {
220 struct varpool_node *node;
221
222 fprintf (f, "variable pool:\n\n");
223 FOR_EACH_VARIABLE (node)
224 dump_varpool_node (f, node);
225 }
226
227 /* Dump the variable pool to stderr. */
228
229 DEBUG_FUNCTION void
230 debug_varpool (void)
231 {
232 dump_varpool (stderr);
233 }
234
235 /* Given an assembler name, lookup node. */
236 struct varpool_node *
237 varpool_node_for_asm (tree asmname)
238 {
239 if (symtab_node *node = symtab_node_for_asm (asmname))
240 return dyn_cast <varpool_node> (node);
241 else
242 return NULL;
243 }
244
245 /* Return if DECL is constant and its initial value is known (so we can do
246 constant folding using DECL_INITIAL (decl)).
247 Return ERROR_MARK_NODE when value is unknown. */
248
249 tree
250 ctor_for_folding (tree decl)
251 {
252 struct varpool_node *node, *real_node;
253 tree real_decl;
254
255 if (TREE_CODE (decl) != VAR_DECL
256 && TREE_CODE (decl) != CONST_DECL)
257 return error_mark_node;
258
259 if (TREE_CODE (decl) == CONST_DECL
260 || DECL_IN_CONSTANT_POOL (decl))
261 return DECL_INITIAL (decl);
262
263 if (TREE_THIS_VOLATILE (decl))
264 return error_mark_node;
265
266 /* Do not care about automatic variables. Those are never initialized
267 anyway, because gimplifier exapnds the code*/
268 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
269 {
270 gcc_assert (!TREE_PUBLIC (decl));
271 return error_mark_node;
272 }
273
274 gcc_assert (TREE_CODE (decl) == VAR_DECL);
275
276 node = varpool_get_node (decl);
277 if (node)
278 {
279 real_node = varpool_variable_node (node);
280 real_decl = real_node->decl;
281 }
282 else
283 real_decl = decl;
284
285 /* See if we are dealing with alias.
286 In most cases alias is just alternative symbol pointing to a given
287 constructor. This allows us to use interposition rules of DECL
288 constructor of REAL_NODE. However weakrefs are special by being just
289 alternative name of their target (if defined). */
290 if (decl != real_decl)
291 {
292 gcc_assert (!DECL_INITIAL (decl)
293 || DECL_INITIAL (decl) == error_mark_node);
294 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
295 {
296 node = varpool_alias_target (node);
297 decl = node->decl;
298 }
299 }
300
301 /* Vtables are defined by their types and must match no matter of interposition
302 rules. */
303 if (DECL_VIRTUAL_P (real_decl))
304 {
305 gcc_checking_assert (TREE_READONLY (real_decl));
306 return DECL_INITIAL (real_decl);
307 }
308
309 /* If there is no constructor, we have nothing to do. */
310 if (DECL_INITIAL (real_decl) == error_mark_node)
311 return error_mark_node;
312
313 /* Non-readonly alias of readonly variable is also de-facto readonly,
314 because the variable itself is in readonly section.
315 We also honnor READONLY flag on alias assuming that user knows
316 what he is doing. */
317 if (!TREE_READONLY (decl) && !TREE_READONLY (real_decl))
318 return error_mark_node;
319
320 /* Variables declared 'const' without an initializer
321 have zero as the initializer if they may not be
322 overridden at link or run time. */
323 if (!DECL_INITIAL (real_decl)
324 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
325 return error_mark_node;
326
327 /* Variables declared `const' with an initializer are considered
328 to not be overwritable with different initializer by default.
329
330 ??? Previously we behaved so for scalar variables but not for array
331 accesses. */
332 return DECL_INITIAL (real_decl);
333 }
334
335 /* Add the variable DECL to the varpool.
336 Unlike varpool_finalize_decl function is intended to be used
337 by middle end and allows insertion of new variable at arbitrary point
338 of compilation. */
339 void
340 varpool_add_new_variable (tree decl)
341 {
342 struct varpool_node *node;
343 varpool_finalize_decl (decl);
344 node = varpool_node_for_decl (decl);
345 varpool_call_variable_insertion_hooks (node);
346 if (varpool_externally_visible_p (node))
347 node->externally_visible = true;
348 }
349
350 /* Return variable availability. See cgraph.h for description of individual
351 return values. */
352 enum availability
353 cgraph_variable_initializer_availability (struct varpool_node *node)
354 {
355 gcc_assert (cgraph_function_flags_ready);
356 if (!node->definition)
357 return AVAIL_NOT_AVAILABLE;
358 if (!TREE_PUBLIC (node->decl))
359 return AVAIL_AVAILABLE;
360 if (DECL_IN_CONSTANT_POOL (node->decl)
361 || DECL_VIRTUAL_P (node->decl))
362 return AVAIL_AVAILABLE;
363 if (node->alias && node->weakref)
364 {
365 enum availability avail;
366
367 cgraph_variable_initializer_availability
368 (varpool_variable_node (node, &avail));
369 return avail;
370 }
371 /* If the variable can be overwritten, return OVERWRITABLE. Takes
372 care of at least one notable extension - the COMDAT variables
373 used to share template instantiations in C++. */
374 if (decl_replaceable_p (node->decl)
375 || DECL_EXTERNAL (node->decl))
376 return AVAIL_OVERWRITABLE;
377 return AVAIL_AVAILABLE;
378 }
379
380 void
381 varpool_analyze_node (struct varpool_node *node)
382 {
383 tree decl = node->decl;
384
385 /* When reading back varpool at LTO time, we re-construct the queue in order
386 to have "needed" list right by inserting all needed nodes into varpool.
387 We however don't want to re-analyze already analyzed nodes. */
388 if (!node->analyzed)
389 {
390 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
391 /* Compute the alignment early so function body expanders are
392 already informed about increased alignment. */
393 align_variable (decl, 0);
394 }
395 if (node->alias)
396 symtab_resolve_alias
397 (node, varpool_get_node (node->alias_target));
398 else if (DECL_INITIAL (decl))
399 record_references_in_initializer (decl, node->analyzed);
400 node->analyzed = true;
401 }
402
403 /* Assemble thunks and aliases associated to NODE. */
404
405 static void
406 assemble_aliases (struct varpool_node *node)
407 {
408 int i;
409 struct ipa_ref *ref;
410 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
411 if (ref->use == IPA_REF_ALIAS)
412 {
413 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
414 do_assemble_alias (alias->decl,
415 DECL_ASSEMBLER_NAME (node->decl));
416 assemble_aliases (alias);
417 }
418 }
419
420 /* Output one variable, if necessary. Return whether we output it. */
421
422 bool
423 varpool_assemble_decl (struct varpool_node *node)
424 {
425 tree decl = node->decl;
426
427 /* Aliases are outout when their target is produced or by
428 output_weakrefs. */
429 if (node->alias)
430 return false;
431
432 /* Constant pool is output from RTL land when the reference
433 survive till this level. */
434 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
435 return false;
436
437 /* Decls with VALUE_EXPR should not be in the varpool at all. They
438 are not real variables, but just info for debugging and codegen.
439 Unfortunately at the moment emutls is not updating varpool correctly
440 after turning real vars into value_expr vars. */
441 if (DECL_HAS_VALUE_EXPR_P (decl)
442 && !targetm.have_tls)
443 return false;
444
445 /* Hard register vars do not need to be output. */
446 if (DECL_HARD_REGISTER (decl))
447 return false;
448
449 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
450 && TREE_CODE (decl) == VAR_DECL
451 && !DECL_HAS_VALUE_EXPR_P (decl));
452
453 if (!node->in_other_partition
454 && !DECL_EXTERNAL (decl))
455 {
456 assemble_variable (decl, 0, 1, 0);
457 gcc_assert (TREE_ASM_WRITTEN (decl));
458 node->definition = true;
459 assemble_aliases (node);
460 return true;
461 }
462
463 return false;
464 }
465
466 /* Add NODE to queue starting at FIRST.
467 The queue is linked via AUX pointers and terminated by pointer to 1. */
468
469 static void
470 enqueue_node (struct varpool_node *node, struct varpool_node **first)
471 {
472 if (node->aux)
473 return;
474 gcc_checking_assert (*first);
475 node->aux = *first;
476 *first = node;
477 }
478
479 /* Optimization of function bodies might've rendered some variables as
480 unnecessary so we want to avoid these from being compiled. Re-do
481 reachability starting from variables that are either externally visible
482 or was referred from the asm output routines. */
483
484 static void
485 varpool_remove_unreferenced_decls (void)
486 {
487 struct varpool_node *next, *node;
488 struct varpool_node *first = (struct varpool_node *)(void *)1;
489 int i;
490 struct ipa_ref *ref;
491
492 if (seen_error ())
493 return;
494
495 if (cgraph_dump_file)
496 fprintf (cgraph_dump_file, "Trivially needed variables:");
497 FOR_EACH_DEFINED_VARIABLE (node)
498 {
499 if (node->analyzed
500 && (!varpool_can_remove_if_no_refs (node)
501 /* We just expanded all function bodies. See if any of
502 them needed the variable. */
503 || DECL_RTL_SET_P (node->decl)))
504 {
505 enqueue_node (node, &first);
506 if (cgraph_dump_file)
507 fprintf (cgraph_dump_file, " %s", node->asm_name ());
508 }
509 }
510 while (first != (struct varpool_node *)(void *)1)
511 {
512 node = first;
513 first = (struct varpool_node *)first->aux;
514
515 if (node->same_comdat_group)
516 {
517 symtab_node *next;
518 for (next = node->same_comdat_group;
519 next != node;
520 next = next->same_comdat_group)
521 {
522 varpool_node *vnext = dyn_cast <varpool_node> (next);
523 if (vnext && vnext->analyzed)
524 enqueue_node (vnext, &first);
525 }
526 }
527 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
528 {
529 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
530 if (vnode
531 && (!DECL_EXTERNAL (ref->referred->decl)
532 || vnode->alias)
533 && vnode->analyzed)
534 enqueue_node (vnode, &first);
535 }
536 }
537 if (cgraph_dump_file)
538 fprintf (cgraph_dump_file, "\nRemoving variables:");
539 for (node = varpool_first_defined_variable (); node; node = next)
540 {
541 next = varpool_next_defined_variable (node);
542 if (!node->aux)
543 {
544 if (cgraph_dump_file)
545 fprintf (cgraph_dump_file, " %s", node->asm_name ());
546 varpool_remove_node (node);
547 }
548 }
549 if (cgraph_dump_file)
550 fprintf (cgraph_dump_file, "\n");
551 }
552
553 /* For variables in named sections make sure get_variable_section
554 is called before we switch to those sections. Then section
555 conflicts between read-only and read-only requiring relocations
556 sections can be resolved. */
557 void
558 varpool_finalize_named_section_flags (struct varpool_node *node)
559 {
560 if (!TREE_ASM_WRITTEN (node->decl)
561 && !node->alias
562 && !node->in_other_partition
563 && !DECL_EXTERNAL (node->decl)
564 && TREE_CODE (node->decl) == VAR_DECL
565 && !DECL_HAS_VALUE_EXPR_P (node->decl)
566 && DECL_SECTION_NAME (node->decl))
567 get_variable_section (node->decl, false);
568 }
569
570 /* Output all variables enqueued to be assembled. */
571 bool
572 varpool_output_variables (void)
573 {
574 bool changed = false;
575 struct varpool_node *node;
576
577 if (seen_error ())
578 return false;
579
580 varpool_remove_unreferenced_decls ();
581
582 timevar_push (TV_VAROUT);
583
584 FOR_EACH_DEFINED_VARIABLE (node)
585 varpool_finalize_named_section_flags (node);
586
587 FOR_EACH_DEFINED_VARIABLE (node)
588 if (varpool_assemble_decl (node))
589 changed = true;
590 timevar_pop (TV_VAROUT);
591 return changed;
592 }
593
594 /* Create a new global variable of type TYPE. */
595 tree
596 add_new_static_var (tree type)
597 {
598 tree new_decl;
599 struct varpool_node *new_node;
600
601 new_decl = create_tmp_var_raw (type, NULL);
602 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
603 TREE_READONLY (new_decl) = 0;
604 TREE_STATIC (new_decl) = 1;
605 TREE_USED (new_decl) = 1;
606 DECL_CONTEXT (new_decl) = NULL_TREE;
607 DECL_ABSTRACT (new_decl) = 0;
608 lang_hooks.dup_lang_specific_decl (new_decl);
609 new_node = varpool_node_for_decl (new_decl);
610 varpool_finalize_decl (new_decl);
611
612 return new_node->decl;
613 }
614
615 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
616 Extra name aliases are output whenever DECL is output. */
617
618 struct varpool_node *
619 varpool_create_variable_alias (tree alias, tree decl)
620 {
621 struct varpool_node *alias_node;
622
623 gcc_assert (TREE_CODE (decl) == VAR_DECL);
624 gcc_assert (TREE_CODE (alias) == VAR_DECL);
625 alias_node = varpool_node_for_decl (alias);
626 alias_node->alias = true;
627 alias_node->definition = true;
628 alias_node->alias_target = decl;
629 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
630 alias_node->weakref = true;
631 return alias_node;
632 }
633
634 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
635 Extra name aliases are output whenever DECL is output. */
636
637 struct varpool_node *
638 varpool_extra_name_alias (tree alias, tree decl)
639 {
640 struct varpool_node *alias_node;
641
642 #ifndef ASM_OUTPUT_DEF
643 /* If aliases aren't supported by the assembler, fail. */
644 return NULL;
645 #endif
646 alias_node = varpool_create_variable_alias (alias, decl);
647 alias_node->cpp_implicit_alias = true;
648
649 /* Extra name alias mechanizm creates aliases really late
650 via DECL_ASSEMBLER_NAME mechanizm.
651 This is unfortunate because they are not going through the
652 standard channels. Ensure they get output. */
653 if (cpp_implicit_aliases_done)
654 symtab_resolve_alias (alias_node,
655 varpool_node_for_decl (decl));
656 return alias_node;
657 }
658
659 /* Call calback on NODE and aliases associated to NODE.
660 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
661 skipped. */
662
663 bool
664 varpool_for_node_and_aliases (struct varpool_node *node,
665 bool (*callback) (struct varpool_node *, void *),
666 void *data,
667 bool include_overwritable)
668 {
669 int i;
670 struct ipa_ref *ref;
671
672 if (callback (node, data))
673 return true;
674 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
675 if (ref->use == IPA_REF_ALIAS)
676 {
677 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
678 if (include_overwritable
679 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
680 if (varpool_for_node_and_aliases (alias, callback, data,
681 include_overwritable))
682 return true;
683 }
684 return false;
685 }