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