1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 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
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
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/>. */
23 #include "coretypes.h"
27 #include "langhooks.h"
28 #include "diagnostic.h"
35 #include "tree-gimple.h"
36 #include "tree-flow.h"
38 /* This file contains basic routines manipulating variable pool.
40 Varpool acts as interface in between the front-end and middle-end
41 and drives the decision process on what variables and when are
44 The varpool nodes are allocated lazily for declarations
45 either by frontend or at callgraph construction time.
46 All variables supposed to be output into final file needs to be
47 explicitly marked by frontend via VARPOOL_FINALIZE_DECL function. */
49 /* Hash table used to convert declarations into nodes. */
50 static GTY((param_is (struct varpool_node))) htab_t varpool_hash;
52 /* The linked list of cgraph varpool nodes.
53 Linked via node->next pointer. */
54 struct varpool_node *varpool_nodes;
56 /* Queue of cgraph nodes scheduled to be lowered and output.
57 The queue is maintained via mark_needed_node, linked via node->next_needed
60 LAST_NNEDED_NODE points to the end of queue, so it can be maintained in forward
61 order. QTY is needed to make it friendly to PCH.
63 During unit-at-a-time compilation we construct the queue of needed variables
64 twice: first time it is during cgraph construction, second time it is at the
65 end of compilation in VARPOOL_REMOVE_UNREFERENCED_DECLS so we can avoid
66 optimized out variables being output.
68 Each variable is thus first analyzed and then later possibly output.
69 FIRST_UNANALYZED_NODE points to first node in queue that was not analyzed
70 yet and is moved via VARPOOL_ANALYZE_PENDING_DECLS. */
72 struct varpool_node *varpool_nodes_queue;
73 static GTY(()) struct varpool_node *varpool_last_needed_node;
74 static GTY(()) struct varpool_node *varpool_first_unanalyzed_node;
76 /* Lists all assembled variables to be sent to debugger output later on. */
77 static GTY(()) struct varpool_node *varpool_assembled_nodes_queue;
79 /* Return name of the node used in debug output. */
81 varpool_node_name (struct varpool_node *node)
83 return lang_hooks.decl_printable_name (node->decl, 2);
86 /* Returns a hash code for P. */
88 hash_varpool_node (const void *p)
90 const struct varpool_node *n = (const struct varpool_node *) p;
91 return (hashval_t) DECL_UID (n->decl);
94 /* Returns nonzero if P1 and P2 are equal. */
96 eq_varpool_node (const void *p1, const void *p2)
98 const struct varpool_node *n1 =
99 (const struct varpool_node *) p1;
100 const struct varpool_node *n2 =
101 (const struct varpool_node *) p2;
102 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
105 /* Return varpool node assigned to DECL. Create new one when needed. */
106 struct varpool_node *
107 varpool_node (tree decl)
109 struct varpool_node key, *node, **slot;
111 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
114 varpool_hash = htab_create_ggc (10, hash_varpool_node,
115 eq_varpool_node, NULL);
117 slot = (struct varpool_node **)
118 htab_find_slot (varpool_hash, &key, INSERT);
121 node = GGC_CNEW (struct varpool_node);
123 node->order = cgraph_order++;
124 node->next = varpool_nodes;
125 varpool_nodes = node;
130 /* Dump given cgraph node. */
132 dump_varpool_node (FILE *f, struct varpool_node *node)
134 fprintf (f, "%s:", varpool_node_name (node));
135 fprintf (f, " availability:%s",
136 cgraph_function_flags_ready
137 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
139 if (DECL_INITIAL (node->decl))
140 fprintf (f, " initialized");
142 fprintf (f, " needed");
144 fprintf (f, " analyzed");
146 fprintf (f, " finalized");
148 fprintf (f, " output");
149 if (node->externally_visible)
150 fprintf (f, " externally_visible");
154 /* Dump the variable pool. */
156 dump_varpool (FILE *f)
158 struct varpool_node *node;
160 fprintf (f, "variable pool:\n\n");
161 for (node = varpool_nodes; node; node = node->next_needed)
162 dump_varpool_node (f, node);
165 /* Given an assembler name, lookup node. */
166 struct varpool_node *
167 varpool_node_for_asm (tree asmname)
169 struct varpool_node *node;
171 for (node = varpool_nodes; node ; node = node->next)
172 if (decl_assembler_name_equal (node->decl, asmname))
178 /* Helper function for finalization code - add node into lists so it will
179 be analyzed and compiled. */
181 varpool_enqueue_needed_node (struct varpool_node *node)
183 if (varpool_last_needed_node)
184 varpool_last_needed_node->next_needed = node;
185 varpool_last_needed_node = node;
186 node->next_needed = NULL;
187 if (!varpool_nodes_queue)
188 varpool_nodes_queue = node;
189 if (!varpool_first_unanalyzed_node)
190 varpool_first_unanalyzed_node = node;
191 notice_global_symbol (node->decl);
194 /* Notify finalize_compilation_unit that given node is reachable
197 varpool_mark_needed_node (struct varpool_node *node)
199 if (!node->needed && node->finalized
200 && !TREE_ASM_WRITTEN (node->decl))
201 varpool_enqueue_needed_node (node);
205 /* Reset the queue of needed nodes. */
207 varpool_reset_queue (void)
209 varpool_last_needed_node = NULL;
210 varpool_nodes_queue = NULL;
211 varpool_first_unanalyzed_node = NULL;
214 /* Determine if variable DECL is needed. That is, visible to something
215 either outside this translation unit, something magic in the system
216 configury, or (if not doing unit-at-a-time) to something we haven't
219 decide_is_variable_needed (struct varpool_node *node, tree decl)
221 /* If the user told us it is used, then it must be so. */
222 if (node->externally_visible || node->force_output)
224 if (!flag_unit_at_a_time
225 && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
228 /* ??? If the assembler name is set by hand, it is possible to assemble
229 the name later after finalizing the function and the fact is noticed
230 in assemble_name then. This is arguably a bug. */
231 if (DECL_ASSEMBLER_NAME_SET_P (decl)
232 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
235 /* If we decided it was needed before, but at the time we didn't have
236 the definition available, then it's still needed. */
240 /* Externally visible variables must be output. The exception is
241 COMDAT variables that must be output only when they are needed. */
242 if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl)
243 && !DECL_EXTERNAL (decl))
246 /* When emulating tls, we actually see references to the control
247 variable, rather than the user-level variable. */
248 if (!targetm.have_tls
249 && TREE_CODE (decl) == VAR_DECL
250 && DECL_THREAD_LOCAL_P (decl))
252 tree control = emutls_decl (decl);
253 if (decide_is_variable_needed (varpool_node (control), control))
257 /* When not reordering top level variables, we have to assume that
258 we are going to keep everything. */
259 if (flag_unit_at_a_time && flag_toplevel_reorder)
262 /* We want to emit COMDAT variables only when absolutely necessary. */
263 if (DECL_COMDAT (decl))
268 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
269 middle end to output the variable to asm file, if needed or externally
272 varpool_finalize_decl (tree decl)
274 struct varpool_node *node = varpool_node (decl);
276 /* The first declaration of a variable that comes through this function
277 decides whether it is global (in C, has external linkage)
278 or local (in C, has internal linkage). So do nothing more
279 if this function has already run. */
282 if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
283 varpool_assemble_pending_decls ();
287 varpool_enqueue_needed_node (node);
288 node->finalized = true;
290 if (decide_is_variable_needed (node, decl))
291 varpool_mark_needed_node (node);
292 /* Since we reclaim unreachable nodes at the end of every language
293 level unit, we need to be conservative about possible entry points
295 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
296 varpool_mark_needed_node (node);
297 if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
298 varpool_assemble_pending_decls ();
301 /* Return variable availability. See cgraph.h for description of individual
304 cgraph_variable_initializer_availability (struct varpool_node *node)
306 gcc_assert (cgraph_function_flags_ready);
307 if (!node->finalized)
308 return AVAIL_NOT_AVAILABLE;
309 if (!TREE_PUBLIC (node->decl))
310 return AVAIL_AVAILABLE;
311 /* If the variable can be overwritten, return OVERWRITABLE. Takes
312 care of at least two notable extensions - the COMDAT variables
313 used to share template instantiations in C++. */
314 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
315 return AVAIL_OVERWRITABLE;
316 return AVAIL_AVAILABLE;
319 /* Walk the decls we marked as necessary and see if they reference new
320 variables or functions and add them into the worklists. */
322 varpool_analyze_pending_decls (void)
324 bool changed = false;
325 timevar_push (TV_CGRAPH);
327 while (varpool_first_unanalyzed_node)
329 tree decl = varpool_first_unanalyzed_node->decl;
331 varpool_first_unanalyzed_node->analyzed = true;
333 varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed;
335 /* Compute the alignment early so function body expanders are
336 already informed about increased alignment. */
337 align_variable (decl, 0);
339 if (DECL_INITIAL (decl))
340 record_references_in_initializer (decl);
343 timevar_pop (TV_CGRAPH);
347 /* Output one variable, if necessary. Return whether we output it. */
349 varpool_assemble_decl (struct varpool_node *node)
351 tree decl = node->decl;
353 if (!TREE_ASM_WRITTEN (decl)
355 && !DECL_EXTERNAL (decl)
356 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
358 assemble_variable (decl, 0, 1, 0);
359 return TREE_ASM_WRITTEN (decl);
365 /* Optimization of function bodies might've rendered some variables as
366 unnecessary so we want to avoid these from being compiled.
368 This is done by pruning the queue and keeping only the variables that
369 really appear needed (ie they are either externally visible or referenced
370 by compiled function). Re-doing the reachability analysis on variables
371 brings back the remaining variables referenced by these. */
373 varpool_remove_unreferenced_decls (void)
375 struct varpool_node *next, *node = varpool_nodes_queue;
377 varpool_reset_queue ();
379 if (errorcount || sorrycount)
384 tree decl = node->decl;
385 next = node->next_needed;
389 && (decide_is_variable_needed (node, decl)
390 /* ??? Cgraph does not yet rule the world with an iron hand,
391 and does not control the emission of debug information.
392 After a variable has its DECL_RTL set, we must assume that
393 it may be referenced by the debug information, and we can
394 no longer elide it. */
395 || DECL_RTL_SET_P (decl)))
396 varpool_mark_needed_node (node);
400 /* Make sure we mark alias targets as used targets. */
402 varpool_analyze_pending_decls ();
405 /* Output all variables enqueued to be assembled. */
407 varpool_assemble_pending_decls (void)
409 bool changed = false;
411 if (errorcount || sorrycount)
414 /* EH might mark decls as needed during expansion. This should be safe since
415 we don't create references to new function, but it should not be used
417 varpool_analyze_pending_decls ();
419 while (varpool_nodes_queue)
421 struct varpool_node *node = varpool_nodes_queue;
423 varpool_nodes_queue = varpool_nodes_queue->next_needed;
424 if (varpool_assemble_decl (node))
427 node->next_needed = varpool_assembled_nodes_queue;
428 varpool_assembled_nodes_queue = node;
432 node->next_needed = NULL;
434 /* varpool_nodes_queue is now empty, clear the pointer to the last element
436 varpool_last_needed_node = NULL;
440 /* Output all variables enqueued to be assembled. */
442 varpool_output_debug_info (void)
444 timevar_push (TV_SYMOUT);
445 if (errorcount == 0 && sorrycount == 0)
446 while (varpool_assembled_nodes_queue)
448 struct varpool_node *node = varpool_assembled_nodes_queue;
450 /* Local static variables are never seen by check_global_declarations
451 so we need to output debug info by hand. */
452 if (DECL_CONTEXT (node->decl)
453 && (TREE_CODE (DECL_CONTEXT (node->decl)) == BLOCK
454 || TREE_CODE (DECL_CONTEXT (node->decl)) == FUNCTION_DECL)
455 && errorcount == 0 && sorrycount == 0)
456 (*debug_hooks->global_decl) (node->decl);
457 varpool_assembled_nodes_queue = node->next_needed;
458 node->next_needed = 0;
460 timevar_pop (TV_SYMOUT);
463 /* Create a new global variable of type TYPE. */
465 add_new_static_var (tree type)
468 struct varpool_node *new_node;
470 new_decl = create_tmp_var (type, NULL);
471 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
472 TREE_READONLY (new_decl) = 0;
473 TREE_STATIC (new_decl) = 1;
474 TREE_USED (new_decl) = 1;
475 DECL_CONTEXT (new_decl) = NULL_TREE;
476 DECL_ABSTRACT (new_decl) = 0;
477 lang_hooks.dup_lang_specific_decl (new_decl);
478 create_var_ann (new_decl);
479 new_node = varpool_node (new_decl);
480 varpool_mark_needed_node (new_node);
481 add_referenced_var (new_decl);
482 varpool_finalize_decl (new_decl);
484 return new_node->decl;
487 #include "gt-varpool.h"