PR target/47063
[official-gcc.git] / gcc / varpool.c
blobd266ce9a8cf1fb10bbbb57e055463aada15e90c1
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cgraph.h"
28 #include "langhooks.h"
29 #include "diagnostic-core.h"
30 #include "hashtab.h"
31 #include "ggc.h"
32 #include "timevar.h"
33 #include "debug.h"
34 #include "target.h"
35 #include "output.h"
36 #include "gimple.h"
37 #include "tree-flow.h"
38 #include "flags.h"
40 /* This file contains basic routines manipulating variable pool.
42 Varpool acts as interface in between the front-end and middle-end
43 and drives the decision process on what variables and when are
44 going to be compiled.
46 The varpool nodes are allocated lazily for declarations
47 either by frontend or at callgraph construction time.
48 All variables supposed to be output into final file needs to be
49 explicitly marked by frontend via VARPOOL_FINALIZE_DECL function. */
51 /* Hash table used to convert declarations into nodes. */
52 static GTY((param_is (struct varpool_node))) htab_t varpool_hash;
54 /* The linked list of cgraph varpool nodes.
55 Linked via node->next pointer. */
56 struct varpool_node *varpool_nodes;
58 /* Queue of cgraph nodes scheduled to be lowered and output.
59 The queue is maintained via mark_needed_node, linked via node->next_needed
60 pointer.
62 LAST_NEEDED_NODE points to the end of queue, so it can be
63 maintained in forward order. GTY is needed to make it friendly to
64 PCH.
66 During compilation we construct the queue of needed variables
67 twice: first time it is during cgraph construction, second time it is at the
68 end of compilation in VARPOOL_REMOVE_UNREFERENCED_DECLS so we can avoid
69 optimized out variables being output.
71 Each variable is thus first analyzed and then later possibly output.
72 FIRST_UNANALYZED_NODE points to first node in queue that was not analyzed
73 yet and is moved via VARPOOL_ANALYZE_PENDING_DECLS. */
75 struct varpool_node *varpool_nodes_queue;
76 static GTY(()) struct varpool_node *varpool_last_needed_node;
77 static GTY(()) struct varpool_node *varpool_first_unanalyzed_node;
79 /* Lists all assembled variables to be sent to debugger output later on. */
80 static GTY(()) struct varpool_node *varpool_assembled_nodes_queue;
82 /* Return name of the node used in debug output. */
83 const char *
84 varpool_node_name (struct varpool_node *node)
86 return lang_hooks.decl_printable_name (node->decl, 2);
89 /* Returns a hash code for P. */
90 static hashval_t
91 hash_varpool_node (const void *p)
93 const struct varpool_node *n = (const struct varpool_node *) p;
94 return (hashval_t) DECL_UID (n->decl);
97 /* Returns nonzero if P1 and P2 are equal. */
98 static int
99 eq_varpool_node (const void *p1, const void *p2)
101 const struct varpool_node *n1 =
102 (const struct varpool_node *) p1;
103 const struct varpool_node *n2 =
104 (const struct varpool_node *) p2;
105 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
108 /* Return varpool node assigned to DECL without creating new one. */
109 struct varpool_node *
110 varpool_get_node (const_tree decl)
112 struct varpool_node key, **slot;
114 gcc_assert (TREE_CODE (decl) == VAR_DECL
115 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
117 if (!varpool_hash)
118 return NULL;
119 key.decl = CONST_CAST2 (tree, const_tree, decl);
120 slot = (struct varpool_node **)
121 htab_find_slot (varpool_hash, &key, NO_INSERT);
122 if (!slot)
123 return NULL;
124 return *slot;
127 /* Return varpool node assigned to DECL. Create new one when needed. */
128 struct varpool_node *
129 varpool_node (tree decl)
131 struct varpool_node key, *node, **slot;
133 gcc_assert (TREE_CODE (decl) == VAR_DECL
134 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
136 if (!varpool_hash)
137 varpool_hash = htab_create_ggc (10, hash_varpool_node,
138 eq_varpool_node, NULL);
139 key.decl = decl;
140 slot = (struct varpool_node **)
141 htab_find_slot (varpool_hash, &key, INSERT);
142 if (*slot)
143 return *slot;
144 node = ggc_alloc_cleared_varpool_node ();
145 node->decl = decl;
146 node->order = cgraph_order++;
147 node->next = varpool_nodes;
148 ipa_empty_ref_list (&node->ref_list);
149 if (varpool_nodes)
150 varpool_nodes->prev = node;
151 varpool_nodes = node;
152 *slot = node;
153 return node;
156 /* Remove node from the varpool. */
157 void
158 varpool_remove_node (struct varpool_node *node)
160 void **slot;
161 slot = htab_find_slot (varpool_hash, node, NO_INSERT);
162 gcc_assert (*slot == node);
163 htab_clear_slot (varpool_hash, slot);
164 gcc_assert (!varpool_assembled_nodes_queue);
165 if (!node->alias)
166 while (node->extra_name)
167 varpool_remove_node (node->extra_name);
168 if (node->next)
169 node->next->prev = node->prev;
170 if (node->prev)
171 node->prev->next = node->next;
172 else
174 if (node->alias && node->extra_name)
176 gcc_assert (node->extra_name->extra_name == node);
177 node->extra_name->extra_name = node->next;
179 else
181 gcc_assert (varpool_nodes == node);
182 varpool_nodes = node->next;
185 if (varpool_first_unanalyzed_node == node)
186 varpool_first_unanalyzed_node = node->next_needed;
187 if (node->next_needed)
188 node->next_needed->prev_needed = node->prev_needed;
189 else if (node->prev_needed)
191 gcc_assert (varpool_last_needed_node);
192 varpool_last_needed_node = node->prev_needed;
194 if (node->prev_needed)
195 node->prev_needed->next_needed = node->next_needed;
196 else if (node->next_needed)
198 gcc_assert (varpool_nodes_queue == node);
199 varpool_nodes_queue = node->next_needed;
201 if (node->same_comdat_group)
203 struct varpool_node *prev;
204 for (prev = node->same_comdat_group;
205 prev->same_comdat_group != node;
206 prev = prev->same_comdat_group)
208 if (node->same_comdat_group == prev)
209 prev->same_comdat_group = NULL;
210 else
211 prev->same_comdat_group = node->same_comdat_group;
212 node->same_comdat_group = NULL;
214 ipa_remove_all_references (&node->ref_list);
215 ipa_remove_all_refering (&node->ref_list);
216 ggc_free (node);
219 /* Dump given cgraph node. */
220 void
221 dump_varpool_node (FILE *f, struct varpool_node *node)
223 fprintf (f, "%s:", varpool_node_name (node));
224 fprintf (f, " availability:%s",
225 cgraph_function_flags_ready
226 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
227 : "not-ready");
228 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
229 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
230 if (DECL_INITIAL (node->decl))
231 fprintf (f, " initialized");
232 if (TREE_ASM_WRITTEN (node->decl))
233 fprintf (f, " (asm written)");
234 if (node->needed)
235 fprintf (f, " needed");
236 if (node->analyzed)
237 fprintf (f, " analyzed");
238 if (node->finalized)
239 fprintf (f, " finalized");
240 if (node->output)
241 fprintf (f, " output");
242 if (node->externally_visible)
243 fprintf (f, " externally_visible");
244 if (node->resolution != LDPR_UNKNOWN)
245 fprintf (f, " %s",
246 ld_plugin_symbol_resolution_names[(int)node->resolution]);
247 if (node->in_other_partition)
248 fprintf (f, " in_other_partition");
249 else if (node->used_from_other_partition)
250 fprintf (f, " used_from_other_partition");
251 fprintf (f, "\n");
252 fprintf (f, " References: ");
253 ipa_dump_references (f, &node->ref_list);
254 fprintf (f, " Refering this var: ");
255 ipa_dump_refering (f, &node->ref_list);
258 /* Dump the variable pool. */
259 void
260 dump_varpool (FILE *f)
262 struct varpool_node *node;
264 fprintf (f, "variable pool:\n\n");
265 for (node = varpool_nodes; node; node = node->next)
266 dump_varpool_node (f, node);
269 /* Dump the variable pool to stderr. */
271 DEBUG_FUNCTION void
272 debug_varpool (void)
274 dump_varpool (stderr);
277 /* Given an assembler name, lookup node. */
278 struct varpool_node *
279 varpool_node_for_asm (tree asmname)
281 struct varpool_node *node;
283 for (node = varpool_nodes; node ; node = node->next)
284 if (decl_assembler_name_equal (node->decl, asmname))
285 return node;
287 return NULL;
290 /* Helper function for finalization code - add node into lists so it will
291 be analyzed and compiled. */
292 static void
293 varpool_enqueue_needed_node (struct varpool_node *node)
295 if (varpool_last_needed_node)
297 varpool_last_needed_node->next_needed = node;
298 node->prev_needed = varpool_last_needed_node;
300 varpool_last_needed_node = node;
301 node->next_needed = NULL;
302 if (!varpool_nodes_queue)
303 varpool_nodes_queue = node;
304 if (!varpool_first_unanalyzed_node)
305 varpool_first_unanalyzed_node = node;
306 notice_global_symbol (node->decl);
309 /* Notify finalize_compilation_unit that given node is reachable
310 or needed. */
311 void
312 varpool_mark_needed_node (struct varpool_node *node)
314 if (node->alias && node->extra_name)
315 node = node->extra_name;
316 if (!node->needed && node->finalized
317 && !TREE_ASM_WRITTEN (node->decl))
318 varpool_enqueue_needed_node (node);
319 node->needed = 1;
322 /* Reset the queue of needed nodes. */
323 void
324 varpool_reset_queue (void)
326 varpool_last_needed_node = NULL;
327 varpool_nodes_queue = NULL;
328 varpool_first_unanalyzed_node = NULL;
331 /* Determine if variable DECL is needed. That is, visible to something
332 either outside this translation unit, something magic in the system
333 configury */
334 bool
335 decide_is_variable_needed (struct varpool_node *node, tree decl)
337 /* If the user told us it is used, then it must be so. */
338 if (node->force_output)
339 return true;
341 gcc_assert (!DECL_EXTERNAL (decl));
343 /* Externally visible variables must be output. The exception is
344 COMDAT variables that must be output only when they are needed. */
345 if (TREE_PUBLIC (decl)
346 && !DECL_COMDAT (decl)
347 && !DECL_EXTERNAL (decl))
348 return true;
350 /* When not reordering top level variables, we have to assume that
351 we are going to keep everything. */
352 if (!flag_toplevel_reorder)
353 return true;
354 return false;
357 /* Return if DECL is constant and its initial value is known (so we can do
358 constant folding using DECL_INITIAL (decl)). */
360 bool
361 const_value_known_p (tree decl)
363 if (TREE_CODE (decl) != VAR_DECL
364 &&TREE_CODE (decl) != CONST_DECL)
365 return false;
367 if (TREE_CODE (decl) == CONST_DECL
368 || DECL_IN_CONSTANT_POOL (decl))
369 return true;
371 gcc_assert (TREE_CODE (decl) == VAR_DECL);
373 if (!TREE_READONLY (decl))
374 return false;
376 /* Gimplifier takes away constructors of local vars */
377 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
378 return DECL_INITIAL (decl) != NULL;
380 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
382 /* Variables declared 'const' without an initializer
383 have zero as the initializer if they may not be
384 overridden at link or run time. */
385 if (!DECL_INITIAL (decl)
386 && (DECL_EXTERNAL (decl)
387 || decl_replaceable_p (decl)))
388 return false;
390 /* Variables declared `const' with an initializer are considered
391 to not be overwritable with different initializer by default.
393 ??? Previously we behaved so for scalar variables but not for array
394 accesses. */
395 return true;
398 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
399 middle end to output the variable to asm file, if needed or externally
400 visible. */
401 void
402 varpool_finalize_decl (tree decl)
404 struct varpool_node *node = varpool_node (decl);
406 gcc_assert (TREE_STATIC (decl));
408 /* The first declaration of a variable that comes through this function
409 decides whether it is global (in C, has external linkage)
410 or local (in C, has internal linkage). So do nothing more
411 if this function has already run. */
412 if (node->finalized)
414 if (cgraph_global_info_ready)
415 varpool_assemble_pending_decls ();
416 return;
418 if (node->needed)
419 varpool_enqueue_needed_node (node);
420 node->finalized = true;
421 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl))
422 node->force_output = true;
424 if (decide_is_variable_needed (node, decl))
425 varpool_mark_needed_node (node);
426 if (cgraph_global_info_ready)
427 varpool_assemble_pending_decls ();
430 /* Return variable availability. See cgraph.h for description of individual
431 return values. */
432 enum availability
433 cgraph_variable_initializer_availability (struct varpool_node *node)
435 gcc_assert (cgraph_function_flags_ready);
436 if (!node->finalized)
437 return AVAIL_NOT_AVAILABLE;
438 if (!TREE_PUBLIC (node->decl))
439 return AVAIL_AVAILABLE;
440 /* If the variable can be overwritten, return OVERWRITABLE. Takes
441 care of at least two notable extensions - the COMDAT variables
442 used to share template instantiations in C++. */
443 if (!decl_replaceable_p (node->decl))
444 return AVAIL_OVERWRITABLE;
445 return AVAIL_AVAILABLE;
448 /* Walk the decls we marked as necessary and see if they reference new
449 variables or functions and add them into the worklists. */
450 bool
451 varpool_analyze_pending_decls (void)
453 bool changed = false;
455 timevar_push (TV_VARPOOL);
456 while (varpool_first_unanalyzed_node)
458 struct varpool_node *node = varpool_first_unanalyzed_node, *next;
459 tree decl = node->decl;
460 bool analyzed = node->analyzed;
462 varpool_first_unanalyzed_node->analyzed = true;
464 varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed;
466 /* When reading back varpool at LTO time, we re-construct the queue in order
467 to have "needed" list right by inserting all needed nodes into varpool.
468 We however don't want to re-analyze already analyzed nodes. */
469 if (!analyzed)
471 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
472 /* Compute the alignment early so function body expanders are
473 already informed about increased alignment. */
474 align_variable (decl, 0);
476 if (DECL_INITIAL (decl))
477 record_references_in_initializer (decl, analyzed);
478 if (node->same_comdat_group)
480 for (next = node->same_comdat_group;
481 next != node;
482 next = next->same_comdat_group)
483 varpool_mark_needed_node (next);
485 changed = true;
487 timevar_pop (TV_VARPOOL);
488 return changed;
491 /* Output one variable, if necessary. Return whether we output it. */
492 bool
493 varpool_assemble_decl (struct varpool_node *node)
495 tree decl = node->decl;
497 if (!TREE_ASM_WRITTEN (decl)
498 && !node->alias
499 && !node->in_other_partition
500 && !DECL_EXTERNAL (decl)
501 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
503 assemble_variable (decl, 0, 1, 0);
504 if (TREE_ASM_WRITTEN (decl))
506 struct varpool_node *alias;
508 node->next_needed = varpool_assembled_nodes_queue;
509 node->prev_needed = NULL;
510 if (varpool_assembled_nodes_queue)
511 varpool_assembled_nodes_queue->prev_needed = node;
512 varpool_assembled_nodes_queue = node;
513 node->finalized = 1;
515 /* Also emit any extra name aliases. */
516 for (alias = node->extra_name; alias; alias = alias->next)
518 /* Update linkage fields in case they've changed. */
519 DECL_WEAK (alias->decl) = DECL_WEAK (decl);
520 TREE_PUBLIC (alias->decl) = TREE_PUBLIC (decl);
521 DECL_VISIBILITY (alias->decl) = DECL_VISIBILITY (decl);
522 assemble_alias (alias->decl, DECL_ASSEMBLER_NAME (decl));
525 return true;
529 return false;
532 /* Optimization of function bodies might've rendered some variables as
533 unnecessary so we want to avoid these from being compiled.
535 This is done by pruning the queue and keeping only the variables that
536 really appear needed (ie they are either externally visible or referenced
537 by compiled function). Re-doing the reachability analysis on variables
538 brings back the remaining variables referenced by these. */
539 void
540 varpool_remove_unreferenced_decls (void)
542 struct varpool_node *next, *node = varpool_nodes_queue;
544 varpool_reset_queue ();
546 if (seen_error ())
547 return;
549 while (node)
551 next = node->next_needed;
552 node->needed = 0;
554 if (node->analyzed
555 && (!varpool_can_remove_if_no_refs (node)
556 /* We just expanded all function bodies. See if any of
557 them needed the variable. */
558 || DECL_RTL_SET_P (node->decl)))
559 varpool_mark_needed_node (node);
561 node = next;
563 /* Make sure we mark alias targets as used targets. */
564 finish_aliases_1 ();
565 varpool_analyze_pending_decls ();
568 /* Output all variables enqueued to be assembled. */
569 bool
570 varpool_assemble_pending_decls (void)
572 bool changed = false;
574 if (seen_error ())
575 return false;
577 timevar_push (TV_VAROUT);
578 /* EH might mark decls as needed during expansion. This should be safe since
579 we don't create references to new function, but it should not be used
580 elsewhere. */
581 varpool_analyze_pending_decls ();
583 while (varpool_nodes_queue)
585 struct varpool_node *node = varpool_nodes_queue;
587 varpool_nodes_queue = varpool_nodes_queue->next_needed;
588 if (varpool_assemble_decl (node))
589 changed = true;
590 else
592 node->prev_needed = NULL;
593 node->next_needed = NULL;
596 /* varpool_nodes_queue is now empty, clear the pointer to the last element
597 in the queue. */
598 varpool_last_needed_node = NULL;
599 timevar_pop (TV_VAROUT);
600 return changed;
603 /* Remove all elements from the queue so we can re-use it for debug output. */
604 void
605 varpool_empty_needed_queue (void)
607 /* EH might mark decls as needed during expansion. This should be safe since
608 we don't create references to new function, but it should not be used
609 elsewhere. */
610 varpool_analyze_pending_decls ();
612 while (varpool_nodes_queue)
614 struct varpool_node *node = varpool_nodes_queue;
615 varpool_nodes_queue = varpool_nodes_queue->next_needed;
616 node->next_needed = NULL;
617 node->prev_needed = NULL;
619 /* varpool_nodes_queue is now empty, clear the pointer to the last element
620 in the queue. */
621 varpool_last_needed_node = NULL;
624 /* Create a new global variable of type TYPE. */
625 tree
626 add_new_static_var (tree type)
628 tree new_decl;
629 struct varpool_node *new_node;
631 new_decl = create_tmp_var (type, NULL);
632 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
633 TREE_READONLY (new_decl) = 0;
634 TREE_STATIC (new_decl) = 1;
635 TREE_USED (new_decl) = 1;
636 DECL_CONTEXT (new_decl) = NULL_TREE;
637 DECL_ABSTRACT (new_decl) = 0;
638 lang_hooks.dup_lang_specific_decl (new_decl);
639 create_var_ann (new_decl);
640 new_node = varpool_node (new_decl);
641 varpool_mark_needed_node (new_node);
642 add_referenced_var (new_decl);
643 varpool_finalize_decl (new_decl);
645 return new_node->decl;
648 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
649 Extra name aliases are output whenever DECL is output. */
651 struct varpool_node *
652 varpool_extra_name_alias (tree alias, tree decl)
654 struct varpool_node key, *alias_node, *decl_node, **slot;
656 #ifndef ASM_OUTPUT_DEF
657 /* If aliases aren't supported by the assembler, fail. */
658 return false;
659 #endif
661 gcc_assert (TREE_CODE (decl) == VAR_DECL);
662 gcc_assert (TREE_CODE (alias) == VAR_DECL);
663 /* Make sure the hash table has been created. */
664 decl_node = varpool_node (decl);
666 key.decl = alias;
668 slot = (struct varpool_node **) htab_find_slot (varpool_hash, &key, INSERT);
670 /* If the varpool_node has been already created, fail. */
671 if (*slot)
672 return NULL;
674 alias_node = ggc_alloc_cleared_varpool_node ();
675 alias_node->decl = alias;
676 alias_node->alias = 1;
677 alias_node->extra_name = decl_node;
678 alias_node->next = decl_node->extra_name;
679 ipa_empty_ref_list (&alias_node->ref_list);
680 if (decl_node->extra_name)
681 decl_node->extra_name->prev = alias_node;
682 decl_node->extra_name = alias_node;
683 *slot = alias_node;
684 return alias_node;
687 /* Return true when NODE is known to be used from other (non-LTO) object file.
688 Known only when doing LTO via linker plugin. */
690 bool
691 varpool_used_from_object_file_p (struct varpool_node *node)
693 struct varpool_node *alias;
695 if (!TREE_PUBLIC (node->decl))
696 return false;
697 if (resolution_used_from_other_file_p (node->resolution))
698 return true;
699 for (alias = node->extra_name; alias; alias = alias->next)
700 if (TREE_PUBLIC (alias->decl)
701 && resolution_used_from_other_file_p (alias->resolution))
702 return true;
703 return false;
706 #include "gt-varpool.h"