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