[AArch64, ARM] Introduce "mrs" type attribute.
[official-gcc.git] / gcc / varpool.c
blobf558aae0bd1d1932b9a4fb37fe55c143459d414f
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-flow.h"
37 #include "flags.h"
39 /* Allocate new callgraph node and insert it into basic data structures. */
41 struct varpool_node *
42 varpool_create_empty_node (void)
44 struct varpool_node *node = ggc_alloc_cleared_varpool_node ();
45 node->symbol.type = SYMTAB_VARIABLE;
46 return node;
49 /* Return varpool node assigned to DECL. Create new one when needed. */
50 struct varpool_node *
51 varpool_node_for_decl (tree decl)
53 struct varpool_node *node = varpool_get_node (decl);
54 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
55 if (node)
56 return node;
58 node = varpool_create_empty_node ();
59 node->symbol.decl = decl;
60 symtab_register_node ((symtab_node)node);
61 return node;
64 /* Remove node from the varpool. */
65 void
66 varpool_remove_node (struct varpool_node *node)
68 symtab_unregister_node ((symtab_node)node);
69 tree init;
71 /* Because we remove references from external functions before final compilation,
72 we may end up removing useful constructors.
73 FIXME: We probably want to trace boundaries better. */
74 if ((init = ctor_for_folding (node->symbol.decl)) == error_mark_node)
75 varpool_remove_initializer (node);
76 else
77 DECL_INITIAL (node->symbol.decl) = init;
78 ggc_free (node);
81 /* Renove node initializer when it is no longer needed. */
82 void
83 varpool_remove_initializer (struct varpool_node *node)
85 if (DECL_INITIAL (node->symbol.decl)
86 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
87 /* Keep vtables for BINFO folding. */
88 && !DECL_VIRTUAL_P (node->symbol.decl)
89 /* FIXME: http://gcc.gnu.org/PR55395 */
90 && debug_info_level == DINFO_LEVEL_NONE
91 /* When doing declaration merging we have duplicate
92 entries for given decl. Do not attempt to remove
93 the boides, or we will end up remiving
94 wrong one. */
95 && cgraph_state != CGRAPH_LTO_STREAMING)
96 DECL_INITIAL (node->symbol.decl) = error_mark_node;
99 /* Dump given cgraph node. */
100 void
101 dump_varpool_node (FILE *f, struct varpool_node *node)
103 dump_symtab_base (f, (symtab_node)node);
104 fprintf (f, " Availability: %s\n",
105 cgraph_function_flags_ready
106 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
107 : "not-ready");
108 fprintf (f, " Varpool flags:");
109 if (DECL_INITIAL (node->symbol.decl))
110 fprintf (f, " initialized");
111 if (node->output)
112 fprintf (f, " output");
113 if (TREE_READONLY (node->symbol.decl))
114 fprintf (f, " read-only");
115 if (ctor_for_folding (node->symbol.decl) != error_mark_node)
116 fprintf (f, " const-value-known");
117 fprintf (f, "\n");
120 /* Dump the variable pool. */
121 void
122 dump_varpool (FILE *f)
124 struct varpool_node *node;
126 fprintf (f, "variable pool:\n\n");
127 FOR_EACH_VARIABLE (node)
128 dump_varpool_node (f, node);
131 /* Dump the variable pool to stderr. */
133 DEBUG_FUNCTION void
134 debug_varpool (void)
136 dump_varpool (stderr);
139 /* Given an assembler name, lookup node. */
140 struct varpool_node *
141 varpool_node_for_asm (tree asmname)
143 if (symtab_node node = symtab_node_for_asm (asmname))
144 return dyn_cast <varpool_node> (node);
145 else
146 return NULL;
149 /* Return if DECL is constant and its initial value is known (so we can do
150 constant folding using DECL_INITIAL (decl)).
151 Return ERROR_MARK_NODE when value is unknown. */
153 tree
154 ctor_for_folding (tree decl)
156 struct varpool_node *node, *real_node;
157 tree real_decl;
159 if (TREE_CODE (decl) != VAR_DECL
160 && TREE_CODE (decl) != CONST_DECL)
161 return error_mark_node;
163 if (TREE_CODE (decl) == CONST_DECL
164 || DECL_IN_CONSTANT_POOL (decl))
165 return DECL_INITIAL (decl);
167 if (TREE_THIS_VOLATILE (decl))
168 return error_mark_node;
170 /* Do not care about automatic variables. Those are never initialized
171 anyway, because gimplifier exapnds the code*/
172 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
174 gcc_assert (!TREE_PUBLIC (decl));
175 return error_mark_node;
178 gcc_assert (TREE_CODE (decl) == VAR_DECL);
180 node = varpool_get_node (decl);
181 if (node)
183 real_node = varpool_variable_node (node);
184 real_decl = real_node->symbol.decl;
186 else
187 real_decl = decl;
189 /* See if we are dealing with alias.
190 In most cases alias is just alternative symbol pointing to a given
191 constructor. This allows us to use interposition rules of DECL
192 constructor of REAL_NODE. However weakrefs are special by being just
193 alternative name of their target (if defined). */
194 if (decl != real_decl)
196 gcc_assert (!DECL_INITIAL (decl)
197 || DECL_INITIAL (decl) == error_mark_node);
198 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
200 node = varpool_alias_target (node);
201 decl = node->symbol.decl;
205 /* Vtables are defined by their types and must match no matter of interposition
206 rules. */
207 if (DECL_VIRTUAL_P (real_decl))
209 gcc_checking_assert (TREE_READONLY (real_decl));
210 return DECL_INITIAL (real_decl);
213 /* If thre is no constructor, we have nothing to do. */
214 if (DECL_INITIAL (real_decl) == error_mark_node)
215 return error_mark_node;
217 /* Non-readonly alias of readonly variable is also de-facto readonly,
218 because the variable itself is in readonly section.
219 We also honnor READONLY flag on alias assuming that user knows
220 what he is doing. */
221 if (!TREE_READONLY (decl) && !TREE_READONLY (real_decl))
222 return error_mark_node;
224 /* Variables declared 'const' without an initializer
225 have zero as the initializer if they may not be
226 overridden at link or run time. */
227 if (!DECL_INITIAL (real_decl)
228 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
229 return error_mark_node;
231 /* Variables declared `const' with an initializer are considered
232 to not be overwritable with different initializer by default.
234 ??? Previously we behaved so for scalar variables but not for array
235 accesses. */
236 return DECL_INITIAL (real_decl);
239 /* Add the variable DECL to the varpool.
240 Unlike varpool_finalize_decl function is intended to be used
241 by middle end and allows insertion of new variable at arbitrary point
242 of compilation. */
243 void
244 varpool_add_new_variable (tree decl)
246 struct varpool_node *node;
247 varpool_finalize_decl (decl);
248 node = varpool_node_for_decl (decl);
249 if (varpool_externally_visible_p (node))
250 node->symbol.externally_visible = true;
253 /* Return variable availability. See cgraph.h for description of individual
254 return values. */
255 enum availability
256 cgraph_variable_initializer_availability (struct varpool_node *node)
258 gcc_assert (cgraph_function_flags_ready);
259 if (!node->symbol.definition)
260 return AVAIL_NOT_AVAILABLE;
261 if (!TREE_PUBLIC (node->symbol.decl))
262 return AVAIL_AVAILABLE;
263 if (DECL_IN_CONSTANT_POOL (node->symbol.decl)
264 || DECL_VIRTUAL_P (node->symbol.decl))
265 return AVAIL_AVAILABLE;
266 if (node->symbol.alias && node->symbol.weakref)
268 enum availability avail;
270 cgraph_variable_initializer_availability
271 (varpool_variable_node (node, &avail));
272 return avail;
274 /* If the variable can be overwritten, return OVERWRITABLE. Takes
275 care of at least one notable extension - the COMDAT variables
276 used to share template instantiations in C++. */
277 if (decl_replaceable_p (node->symbol.decl)
278 || DECL_EXTERNAL (node->symbol.decl))
279 return AVAIL_OVERWRITABLE;
280 return AVAIL_AVAILABLE;
283 void
284 varpool_analyze_node (struct varpool_node *node)
286 tree decl = node->symbol.decl;
288 /* When reading back varpool at LTO time, we re-construct the queue in order
289 to have "needed" list right by inserting all needed nodes into varpool.
290 We however don't want to re-analyze already analyzed nodes. */
291 if (!node->symbol.analyzed)
293 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
294 /* Compute the alignment early so function body expanders are
295 already informed about increased alignment. */
296 align_variable (decl, 0);
298 if (node->symbol.alias)
299 symtab_resolve_alias
300 ((symtab_node) node, (symtab_node) varpool_get_node (node->symbol.alias_target));
301 else if (DECL_INITIAL (decl))
302 record_references_in_initializer (decl, node->symbol.analyzed);
303 node->symbol.analyzed = true;
306 /* Assemble thunks and aliases associated to NODE. */
308 static void
309 assemble_aliases (struct varpool_node *node)
311 int i;
312 struct ipa_ref *ref;
313 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
314 if (ref->use == IPA_REF_ALIAS)
316 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
317 do_assemble_alias (alias->symbol.decl,
318 DECL_ASSEMBLER_NAME (node->symbol.decl));
319 assemble_aliases (alias);
323 /* Output one variable, if necessary. Return whether we output it. */
325 bool
326 varpool_assemble_decl (struct varpool_node *node)
328 tree decl = node->symbol.decl;
330 /* Aliases are outout when their target is produced or by
331 output_weakrefs. */
332 if (node->symbol.alias)
333 return false;
335 /* Constant pool is output from RTL land when the reference
336 survive till this level. */
337 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
338 return false;
340 /* Decls with VALUE_EXPR should not be in the varpool at all. They
341 are not real variables, but just info for debugging and codegen.
342 Unfortunately at the moment emutls is not updating varpool correctly
343 after turning real vars into value_expr vars. */
344 if (DECL_HAS_VALUE_EXPR_P (decl)
345 && !targetm.have_tls)
346 return false;
348 /* Hard register vars do not need to be output. */
349 if (DECL_HARD_REGISTER (decl))
350 return false;
352 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
353 && TREE_CODE (decl) == VAR_DECL
354 && !DECL_HAS_VALUE_EXPR_P (decl));
356 if (!node->symbol.in_other_partition
357 && !DECL_EXTERNAL (decl))
359 assemble_variable (decl, 0, 1, 0);
360 gcc_assert (TREE_ASM_WRITTEN (decl));
361 node->symbol.definition = true;
362 assemble_aliases (node);
363 return true;
366 return false;
369 /* Add NODE to queue starting at FIRST.
370 The queue is linked via AUX pointers and terminated by pointer to 1. */
372 static void
373 enqueue_node (struct varpool_node *node, struct varpool_node **first)
375 if (node->symbol.aux)
376 return;
377 gcc_checking_assert (*first);
378 node->symbol.aux = *first;
379 *first = node;
382 /* Optimization of function bodies might've rendered some variables as
383 unnecessary so we want to avoid these from being compiled. Re-do
384 reachability starting from variables that are either externally visible
385 or was referred from the asm output routines. */
387 static void
388 varpool_remove_unreferenced_decls (void)
390 struct varpool_node *next, *node;
391 struct varpool_node *first = (struct varpool_node *)(void *)1;
392 int i;
393 struct ipa_ref *ref;
395 if (seen_error ())
396 return;
398 if (cgraph_dump_file)
399 fprintf (cgraph_dump_file, "Trivially needed variables:");
400 FOR_EACH_DEFINED_VARIABLE (node)
402 if (node->symbol.analyzed
403 && (!varpool_can_remove_if_no_refs (node)
404 /* We just expanded all function bodies. See if any of
405 them needed the variable. */
406 || DECL_RTL_SET_P (node->symbol.decl)))
408 enqueue_node (node, &first);
409 if (cgraph_dump_file)
410 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
413 while (first != (struct varpool_node *)(void *)1)
415 node = first;
416 first = (struct varpool_node *)first->symbol.aux;
418 if (node->symbol.same_comdat_group)
420 symtab_node next;
421 for (next = node->symbol.same_comdat_group;
422 next != (symtab_node)node;
423 next = next->symbol.same_comdat_group)
425 varpool_node *vnext = dyn_cast <varpool_node> (next);
426 if (vnext && vnext->symbol.analyzed)
427 enqueue_node (vnext, &first);
430 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
432 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
433 if (vnode
434 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
435 || vnode->symbol.alias)
436 && vnode->symbol.analyzed)
437 enqueue_node (vnode, &first);
440 if (cgraph_dump_file)
441 fprintf (cgraph_dump_file, "\nRemoving variables:");
442 for (node = varpool_first_defined_variable (); node; node = next)
444 next = varpool_next_defined_variable (node);
445 if (!node->symbol.aux)
447 if (cgraph_dump_file)
448 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
449 varpool_remove_node (node);
452 if (cgraph_dump_file)
453 fprintf (cgraph_dump_file, "\n");
456 /* For variables in named sections make sure get_variable_section
457 is called before we switch to those sections. Then section
458 conflicts between read-only and read-only requiring relocations
459 sections can be resolved. */
460 void
461 varpool_finalize_named_section_flags (struct varpool_node *node)
463 if (!TREE_ASM_WRITTEN (node->symbol.decl)
464 && !node->symbol.alias
465 && !node->symbol.in_other_partition
466 && !DECL_EXTERNAL (node->symbol.decl)
467 && TREE_CODE (node->symbol.decl) == VAR_DECL
468 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
469 && DECL_SECTION_NAME (node->symbol.decl))
470 get_variable_section (node->symbol.decl, false);
473 /* Output all variables enqueued to be assembled. */
474 bool
475 varpool_output_variables (void)
477 bool changed = false;
478 struct varpool_node *node;
480 if (seen_error ())
481 return false;
483 varpool_remove_unreferenced_decls ();
485 timevar_push (TV_VAROUT);
487 FOR_EACH_DEFINED_VARIABLE (node)
488 varpool_finalize_named_section_flags (node);
490 FOR_EACH_DEFINED_VARIABLE (node)
491 if (varpool_assemble_decl (node))
492 changed = true;
493 timevar_pop (TV_VAROUT);
494 return changed;
497 /* Create a new global variable of type TYPE. */
498 tree
499 add_new_static_var (tree type)
501 tree new_decl;
502 struct varpool_node *new_node;
504 new_decl = create_tmp_var_raw (type, NULL);
505 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
506 TREE_READONLY (new_decl) = 0;
507 TREE_STATIC (new_decl) = 1;
508 TREE_USED (new_decl) = 1;
509 DECL_CONTEXT (new_decl) = NULL_TREE;
510 DECL_ABSTRACT (new_decl) = 0;
511 lang_hooks.dup_lang_specific_decl (new_decl);
512 new_node = varpool_node_for_decl (new_decl);
513 varpool_finalize_decl (new_decl);
515 return new_node->symbol.decl;
518 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
519 Extra name aliases are output whenever DECL is output. */
521 struct varpool_node *
522 varpool_create_variable_alias (tree alias, tree decl)
524 struct varpool_node *alias_node;
526 gcc_assert (TREE_CODE (decl) == VAR_DECL);
527 gcc_assert (TREE_CODE (alias) == VAR_DECL);
528 alias_node = varpool_node_for_decl (alias);
529 alias_node->symbol.alias = true;
530 alias_node->symbol.definition = true;
531 alias_node->symbol.alias_target = decl;
532 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
533 alias_node->symbol.weakref = true;
534 return alias_node;
537 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
538 Extra name aliases are output whenever DECL is output. */
540 struct varpool_node *
541 varpool_extra_name_alias (tree alias, tree decl)
543 struct varpool_node *alias_node;
545 #ifndef ASM_OUTPUT_DEF
546 /* If aliases aren't supported by the assembler, fail. */
547 return NULL;
548 #endif
549 alias_node = varpool_create_variable_alias (alias, decl);
550 alias_node->symbol.cpp_implicit_alias = true;
552 /* Extra name alias mechanizm creates aliases really late
553 via DECL_ASSEMBLER_NAME mechanizm.
554 This is unfortunate because they are not going through the
555 standard channels. Ensure they get output. */
556 if (cpp_implicit_aliases_done)
557 symtab_resolve_alias ((symtab_node)alias_node,
558 (symtab_node)varpool_node_for_decl (decl));
559 return alias_node;
562 /* Call calback on NODE and aliases associated to NODE.
563 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
564 skipped. */
566 bool
567 varpool_for_node_and_aliases (struct varpool_node *node,
568 bool (*callback) (struct varpool_node *, void *),
569 void *data,
570 bool include_overwritable)
572 int i;
573 struct ipa_ref *ref;
575 if (callback (node, data))
576 return true;
577 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
578 if (ref->use == IPA_REF_ALIAS)
580 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
581 if (include_overwritable
582 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
583 if (varpool_for_node_and_aliases (alias, callback, data,
584 include_overwritable))
585 return true;
587 return false;