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"
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_NEEDED_NODE points to the end of queue, so it can be
61 maintained in forward order. GTY is needed to make it friendly to
64 During compilation we construct the queue of needed variables
65 twice: first time it is during cgraph construction, second time it is at the
66 end of compilation in VARPOOL_REMOVE_UNREFERENCED_DECLS so we can avoid
67 optimized out variables being output.
69 Each variable is thus first analyzed and then later possibly output.
70 FIRST_UNANALYZED_NODE points to first node in queue that was not analyzed
71 yet and is moved via VARPOOL_ANALYZE_PENDING_DECLS. */
73 struct varpool_node
*varpool_nodes_queue
;
74 static GTY(()) struct varpool_node
*varpool_last_needed_node
;
75 static GTY(()) struct varpool_node
*varpool_first_unanalyzed_node
;
77 /* Lists all assembled variables to be sent to debugger output later on. */
78 static GTY(()) struct varpool_node
*varpool_assembled_nodes_queue
;
80 /* Return name of the node used in debug output. */
82 varpool_node_name (struct varpool_node
*node
)
84 return lang_hooks
.decl_printable_name (node
->decl
, 2);
87 /* Returns a hash code for P. */
89 hash_varpool_node (const void *p
)
91 const struct varpool_node
*n
= (const struct varpool_node
*) p
;
92 return (hashval_t
) DECL_UID (n
->decl
);
95 /* Returns nonzero if P1 and P2 are equal. */
97 eq_varpool_node (const void *p1
, const void *p2
)
99 const struct varpool_node
*n1
=
100 (const struct varpool_node
*) p1
;
101 const struct varpool_node
*n2
=
102 (const struct varpool_node
*) p2
;
103 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
106 /* Return varpool node assigned to DECL. Create new one when needed. */
107 struct varpool_node
*
108 varpool_node (tree decl
)
110 struct varpool_node key
, *node
, **slot
;
112 gcc_assert (DECL_P (decl
) && TREE_CODE (decl
) != FUNCTION_DECL
);
115 varpool_hash
= htab_create_ggc (10, hash_varpool_node
,
116 eq_varpool_node
, NULL
);
118 slot
= (struct varpool_node
**)
119 htab_find_slot (varpool_hash
, &key
, INSERT
);
122 node
= GGC_CNEW (struct varpool_node
);
124 node
->order
= cgraph_order
++;
125 node
->next
= varpool_nodes
;
126 varpool_nodes
= node
;
131 /* Dump given cgraph node. */
133 dump_varpool_node (FILE *f
, struct varpool_node
*node
)
135 fprintf (f
, "%s:", varpool_node_name (node
));
136 fprintf (f
, " availability:%s",
137 cgraph_function_flags_ready
138 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
140 if (DECL_INITIAL (node
->decl
))
141 fprintf (f
, " initialized");
143 fprintf (f
, " needed");
145 fprintf (f
, " analyzed");
147 fprintf (f
, " finalized");
149 fprintf (f
, " output");
150 if (node
->externally_visible
)
151 fprintf (f
, " externally_visible");
155 /* Dump the variable pool. */
157 dump_varpool (FILE *f
)
159 struct varpool_node
*node
;
161 fprintf (f
, "variable pool:\n\n");
162 for (node
= varpool_nodes
; node
; node
= node
->next
)
163 dump_varpool_node (f
, node
);
166 /* Given an assembler name, lookup node. */
167 struct varpool_node
*
168 varpool_node_for_asm (tree asmname
)
170 struct varpool_node
*node
;
172 for (node
= varpool_nodes
; node
; node
= node
->next
)
173 if (decl_assembler_name_equal (node
->decl
, asmname
))
179 /* Helper function for finalization code - add node into lists so it will
180 be analyzed and compiled. */
182 varpool_enqueue_needed_node (struct varpool_node
*node
)
184 if (varpool_last_needed_node
)
185 varpool_last_needed_node
->next_needed
= node
;
186 varpool_last_needed_node
= node
;
187 node
->next_needed
= NULL
;
188 if (!varpool_nodes_queue
)
189 varpool_nodes_queue
= node
;
190 if (!varpool_first_unanalyzed_node
)
191 varpool_first_unanalyzed_node
= node
;
192 notice_global_symbol (node
->decl
);
195 /* Notify finalize_compilation_unit that given node is reachable
198 varpool_mark_needed_node (struct varpool_node
*node
)
200 if (!node
->needed
&& node
->finalized
201 && !TREE_ASM_WRITTEN (node
->decl
))
202 varpool_enqueue_needed_node (node
);
206 /* Reset the queue of needed nodes. */
208 varpool_reset_queue (void)
210 varpool_last_needed_node
= NULL
;
211 varpool_nodes_queue
= NULL
;
212 varpool_first_unanalyzed_node
= NULL
;
215 /* Determine if variable DECL is needed. That is, visible to something
216 either outside this translation unit, something magic in the system
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
)
225 /* ??? If the assembler name is set by hand, it is possible to assemble
226 the name later after finalizing the function and the fact is noticed
227 in assemble_name then. This is arguably a bug. */
228 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
229 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
232 /* If we decided it was needed before, but at the time we didn't have
233 the definition available, then it's still needed. */
237 /* Externally visible variables must be output. The exception is
238 COMDAT variables that must be output only when they are needed. */
239 if (TREE_PUBLIC (decl
) && !flag_whole_program
&& !DECL_COMDAT (decl
)
240 && !DECL_EXTERNAL (decl
))
243 /* When emulating tls, we actually see references to the control
244 variable, rather than the user-level variable. */
245 if (!targetm
.have_tls
246 && TREE_CODE (decl
) == VAR_DECL
247 && DECL_THREAD_LOCAL_P (decl
))
249 tree control
= emutls_decl (decl
);
250 if (decide_is_variable_needed (varpool_node (control
), control
))
254 /* When not reordering top level variables, we have to assume that
255 we are going to keep everything. */
256 if (flag_toplevel_reorder
)
259 /* We want to emit COMDAT variables only when absolutely necessary. */
260 if (DECL_COMDAT (decl
))
265 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
266 middle end to output the variable to asm file, if needed or externally
269 varpool_finalize_decl (tree decl
)
271 struct varpool_node
*node
= varpool_node (decl
);
273 /* The first declaration of a variable that comes through this function
274 decides whether it is global (in C, has external linkage)
275 or local (in C, has internal linkage). So do nothing more
276 if this function has already run. */
279 if (cgraph_global_info_ready
)
280 varpool_assemble_pending_decls ();
284 varpool_enqueue_needed_node (node
);
285 node
->finalized
= true;
287 if (decide_is_variable_needed (node
, decl
))
288 varpool_mark_needed_node (node
);
289 /* Since we reclaim unreachable nodes at the end of every language
290 level unit, we need to be conservative about possible entry points
292 else if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
293 varpool_mark_needed_node (node
);
294 if (cgraph_global_info_ready
)
295 varpool_assemble_pending_decls ();
298 /* Return variable availability. See cgraph.h for description of individual
301 cgraph_variable_initializer_availability (struct varpool_node
*node
)
303 gcc_assert (cgraph_function_flags_ready
);
304 if (!node
->finalized
)
305 return AVAIL_NOT_AVAILABLE
;
306 if (!TREE_PUBLIC (node
->decl
))
307 return AVAIL_AVAILABLE
;
308 /* If the variable can be overwritten, return OVERWRITABLE. Takes
309 care of at least two notable extensions - the COMDAT variables
310 used to share template instantiations in C++. */
311 if (!(*targetm
.binds_local_p
) (node
->decl
) && !DECL_COMDAT (node
->decl
))
312 return AVAIL_OVERWRITABLE
;
313 return AVAIL_AVAILABLE
;
316 /* Walk the decls we marked as necessary and see if they reference new
317 variables or functions and add them into the worklists. */
319 varpool_analyze_pending_decls (void)
321 bool changed
= false;
322 timevar_push (TV_CGRAPH
);
324 while (varpool_first_unanalyzed_node
)
326 tree decl
= varpool_first_unanalyzed_node
->decl
;
328 varpool_first_unanalyzed_node
->analyzed
= true;
330 varpool_first_unanalyzed_node
= varpool_first_unanalyzed_node
->next_needed
;
332 /* Compute the alignment early so function body expanders are
333 already informed about increased alignment. */
334 align_variable (decl
, 0);
336 if (DECL_INITIAL (decl
))
337 record_references_in_initializer (decl
);
340 timevar_pop (TV_CGRAPH
);
344 /* Output one variable, if necessary. Return whether we output it. */
346 varpool_assemble_decl (struct varpool_node
*node
)
348 tree decl
= node
->decl
;
350 if (!TREE_ASM_WRITTEN (decl
)
352 && !DECL_EXTERNAL (decl
)
353 && (TREE_CODE (decl
) != VAR_DECL
|| !DECL_HAS_VALUE_EXPR_P (decl
)))
355 assemble_variable (decl
, 0, 1, 0);
356 if (TREE_ASM_WRITTEN (decl
))
358 node
->next_needed
= varpool_assembled_nodes_queue
;
359 varpool_assembled_nodes_queue
= node
;
368 /* Optimization of function bodies might've rendered some variables as
369 unnecessary so we want to avoid these from being compiled.
371 This is done by pruning the queue and keeping only the variables that
372 really appear needed (ie they are either externally visible or referenced
373 by compiled function). Re-doing the reachability analysis on variables
374 brings back the remaining variables referenced by these. */
376 varpool_remove_unreferenced_decls (void)
378 struct varpool_node
*next
, *node
= varpool_nodes_queue
;
380 varpool_reset_queue ();
382 if (errorcount
|| sorrycount
)
387 tree decl
= node
->decl
;
388 next
= node
->next_needed
;
392 && (decide_is_variable_needed (node
, decl
)
393 /* ??? Cgraph does not yet rule the world with an iron hand,
394 and does not control the emission of debug information.
395 After a variable has its DECL_RTL set, we must assume that
396 it may be referenced by the debug information, and we can
397 no longer elide it. */
398 || DECL_RTL_SET_P (decl
)))
399 varpool_mark_needed_node (node
);
403 /* Make sure we mark alias targets as used targets. */
405 varpool_analyze_pending_decls ();
408 /* Output all variables enqueued to be assembled. */
410 varpool_assemble_pending_decls (void)
412 bool changed
= false;
414 if (errorcount
|| sorrycount
)
417 /* EH might mark decls as needed during expansion. This should be safe since
418 we don't create references to new function, but it should not be used
420 varpool_analyze_pending_decls ();
422 while (varpool_nodes_queue
)
424 struct varpool_node
*node
= varpool_nodes_queue
;
426 varpool_nodes_queue
= varpool_nodes_queue
->next_needed
;
427 if (varpool_assemble_decl (node
))
430 node
->next_needed
= NULL
;
432 /* varpool_nodes_queue is now empty, clear the pointer to the last element
434 varpool_last_needed_node
= NULL
;
438 /* Remove all elements from the queue so we can re-use it for debug output. */
440 varpool_empty_needed_queue (void)
442 /* EH might mark decls as needed during expansion. This should be safe since
443 we don't create references to new function, but it should not be used
445 varpool_analyze_pending_decls ();
447 while (varpool_nodes_queue
)
449 struct varpool_node
*node
= varpool_nodes_queue
;
450 varpool_nodes_queue
= varpool_nodes_queue
->next_needed
;
451 node
->next_needed
= NULL
;
453 /* varpool_nodes_queue is now empty, clear the pointer to the last element
455 varpool_last_needed_node
= NULL
;
458 /* Output all variables enqueued to be assembled. */
460 varpool_output_debug_info (void)
462 timevar_push (TV_SYMOUT
);
463 if (errorcount
== 0 && sorrycount
== 0)
464 while (varpool_assembled_nodes_queue
)
466 struct varpool_node
*node
= varpool_assembled_nodes_queue
;
468 /* Local static variables are never seen by check_global_declarations
469 so we need to output debug info by hand. */
470 if (DECL_CONTEXT (node
->decl
)
471 && (TREE_CODE (DECL_CONTEXT (node
->decl
)) == BLOCK
472 || TREE_CODE (DECL_CONTEXT (node
->decl
)) == FUNCTION_DECL
)
473 && errorcount
== 0 && sorrycount
== 0)
474 (*debug_hooks
->global_decl
) (node
->decl
);
475 varpool_assembled_nodes_queue
= node
->next_needed
;
476 node
->next_needed
= 0;
478 timevar_pop (TV_SYMOUT
);
481 /* Create a new global variable of type TYPE. */
483 add_new_static_var (tree type
)
486 struct varpool_node
*new_node
;
488 new_decl
= create_tmp_var (type
, NULL
);
489 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
490 TREE_READONLY (new_decl
) = 0;
491 TREE_STATIC (new_decl
) = 1;
492 TREE_USED (new_decl
) = 1;
493 DECL_CONTEXT (new_decl
) = NULL_TREE
;
494 DECL_ABSTRACT (new_decl
) = 0;
495 lang_hooks
.dup_lang_specific_decl (new_decl
);
496 create_var_ann (new_decl
);
497 new_node
= varpool_node (new_decl
);
498 varpool_mark_needed_node (new_node
);
499 add_referenced_var (new_decl
);
500 varpool_finalize_decl (new_decl
);
502 return new_node
->decl
;
505 #include "gt-varpool.h"