1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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
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
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/>. */
24 #include "coretypes.h"
28 #include "langhooks.h"
29 #include "diagnostic.h"
37 #include "tree-flow.h"
39 /* This file contains basic routines manipulating variable pool.
41 Varpool acts as interface in between the front-end and middle-end
42 and drives the decision process on what variables and when are
45 The varpool nodes are allocated lazily for declarations
46 either by frontend or at callgraph construction time.
47 All variables supposed to be output into final file needs to be
48 explicitly marked by frontend via VARPOOL_FINALIZE_DECL function. */
50 /* Hash table used to convert declarations into nodes. */
51 static GTY((param_is (struct varpool_node
))) htab_t varpool_hash
;
53 /* The linked list of cgraph varpool nodes.
54 Linked via node->next pointer. */
55 struct varpool_node
*varpool_nodes
;
57 /* Queue of cgraph nodes scheduled to be lowered and output.
58 The queue is maintained via mark_needed_node, linked via node->next_needed
61 LAST_NEEDED_NODE points to the end of queue, so it can be
62 maintained in forward order. GTY is needed to make it friendly to
65 During compilation we construct the queue of needed variables
66 twice: first time it is during cgraph construction, second time it is at the
67 end of compilation in VARPOOL_REMOVE_UNREFERENCED_DECLS so we can avoid
68 optimized out variables being output.
70 Each variable is thus first analyzed and then later possibly output.
71 FIRST_UNANALYZED_NODE points to first node in queue that was not analyzed
72 yet and is moved via VARPOOL_ANALYZE_PENDING_DECLS. */
74 struct varpool_node
*varpool_nodes_queue
;
75 static GTY(()) struct varpool_node
*varpool_last_needed_node
;
76 static GTY(()) struct varpool_node
*varpool_first_unanalyzed_node
;
78 /* Lists all assembled variables to be sent to debugger output later on. */
79 static GTY(()) struct varpool_node
*varpool_assembled_nodes_queue
;
81 /* Return name of the node used in debug output. */
83 varpool_node_name (struct varpool_node
*node
)
85 return lang_hooks
.decl_printable_name (node
->decl
, 2);
88 /* Returns a hash code for P. */
90 hash_varpool_node (const void *p
)
92 const struct varpool_node
*n
= (const struct varpool_node
*) p
;
93 return (hashval_t
) DECL_UID (n
->decl
);
96 /* Returns nonzero if P1 and P2 are equal. */
98 eq_varpool_node (const void *p1
, const void *p2
)
100 const struct varpool_node
*n1
=
101 (const struct varpool_node
*) p1
;
102 const struct varpool_node
*n2
=
103 (const struct varpool_node
*) p2
;
104 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
107 /* Return varpool node assigned to DECL. Create new one when needed. */
108 struct varpool_node
*
109 varpool_node (tree decl
)
111 struct varpool_node key
, *node
, **slot
;
113 gcc_assert (DECL_P (decl
) && TREE_CODE (decl
) != FUNCTION_DECL
);
116 varpool_hash
= htab_create_ggc (10, hash_varpool_node
,
117 eq_varpool_node
, NULL
);
119 slot
= (struct varpool_node
**)
120 htab_find_slot (varpool_hash
, &key
, INSERT
);
123 node
= GGC_CNEW (struct varpool_node
);
125 node
->order
= cgraph_order
++;
126 node
->next
= varpool_nodes
;
127 varpool_nodes
= node
;
132 /* Dump given cgraph node. */
134 dump_varpool_node (FILE *f
, struct varpool_node
*node
)
136 fprintf (f
, "%s:", varpool_node_name (node
));
137 fprintf (f
, " availability:%s",
138 cgraph_function_flags_ready
139 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
141 if (DECL_INITIAL (node
->decl
))
142 fprintf (f
, " initialized");
144 fprintf (f
, " needed");
146 fprintf (f
, " analyzed");
148 fprintf (f
, " finalized");
150 fprintf (f
, " output");
151 if (node
->externally_visible
)
152 fprintf (f
, " externally_visible");
156 /* Dump the variable pool. */
158 dump_varpool (FILE *f
)
160 struct varpool_node
*node
;
162 fprintf (f
, "variable pool:\n\n");
163 for (node
= varpool_nodes
; node
; node
= node
->next
)
164 dump_varpool_node (f
, node
);
167 /* Dump the variable pool to stderr. */
172 dump_varpool (stderr
);
175 /* Given an assembler name, lookup node. */
176 struct varpool_node
*
177 varpool_node_for_asm (tree asmname
)
179 struct varpool_node
*node
;
181 for (node
= varpool_nodes
; node
; node
= node
->next
)
182 if (decl_assembler_name_equal (node
->decl
, asmname
))
188 /* Helper function for finalization code - add node into lists so it will
189 be analyzed and compiled. */
191 varpool_enqueue_needed_node (struct varpool_node
*node
)
193 if (varpool_last_needed_node
)
194 varpool_last_needed_node
->next_needed
= node
;
195 varpool_last_needed_node
= node
;
196 node
->next_needed
= NULL
;
197 if (!varpool_nodes_queue
)
198 varpool_nodes_queue
= node
;
199 if (!varpool_first_unanalyzed_node
)
200 varpool_first_unanalyzed_node
= node
;
201 notice_global_symbol (node
->decl
);
204 /* Notify finalize_compilation_unit that given node is reachable
207 varpool_mark_needed_node (struct varpool_node
*node
)
209 if (!node
->needed
&& node
->finalized
210 && !TREE_ASM_WRITTEN (node
->decl
))
211 varpool_enqueue_needed_node (node
);
215 /* Reset the queue of needed nodes. */
217 varpool_reset_queue (void)
219 varpool_last_needed_node
= NULL
;
220 varpool_nodes_queue
= NULL
;
221 varpool_first_unanalyzed_node
= NULL
;
224 /* Determine if variable DECL is needed. That is, visible to something
225 either outside this translation unit, something magic in the system
228 decide_is_variable_needed (struct varpool_node
*node
, tree decl
)
230 /* If the user told us it is used, then it must be so. */
231 if (node
->externally_visible
|| node
->force_output
)
234 /* ??? If the assembler name is set by hand, it is possible to assemble
235 the name later after finalizing the function and the fact is noticed
236 in assemble_name then. This is arguably a bug. */
237 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
238 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
241 /* If we decided it was needed before, but at the time we didn't have
242 the definition available, then it's still needed. */
246 /* Externally visible variables must be output. The exception is
247 COMDAT variables that must be output only when they are needed. */
248 if (TREE_PUBLIC (decl
) && !flag_whole_program
&& !DECL_COMDAT (decl
)
249 && !DECL_EXTERNAL (decl
))
252 /* When emulating tls, we actually see references to the control
253 variable, rather than the user-level variable. */
254 if (!targetm
.have_tls
255 && TREE_CODE (decl
) == VAR_DECL
256 && DECL_THREAD_LOCAL_P (decl
))
258 tree control
= emutls_decl (decl
);
259 if (decide_is_variable_needed (varpool_node (control
), control
))
263 /* When not reordering top level variables, we have to assume that
264 we are going to keep everything. */
265 if (flag_toplevel_reorder
)
268 /* We want to emit COMDAT variables only when absolutely necessary. */
269 if (DECL_COMDAT (decl
))
274 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
275 middle end to output the variable to asm file, if needed or externally
278 varpool_finalize_decl (tree decl
)
280 struct varpool_node
*node
= varpool_node (decl
);
282 /* The first declaration of a variable that comes through this function
283 decides whether it is global (in C, has external linkage)
284 or local (in C, has internal linkage). So do nothing more
285 if this function has already run. */
288 if (cgraph_global_info_ready
)
289 varpool_assemble_pending_decls ();
293 varpool_enqueue_needed_node (node
);
294 node
->finalized
= true;
296 if (decide_is_variable_needed (node
, decl
))
297 varpool_mark_needed_node (node
);
298 /* Since we reclaim unreachable nodes at the end of every language
299 level unit, we need to be conservative about possible entry points
301 else if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
302 varpool_mark_needed_node (node
);
303 if (cgraph_global_info_ready
)
304 varpool_assemble_pending_decls ();
307 /* Return variable availability. See cgraph.h for description of individual
310 cgraph_variable_initializer_availability (struct varpool_node
*node
)
312 gcc_assert (cgraph_function_flags_ready
);
313 if (!node
->finalized
)
314 return AVAIL_NOT_AVAILABLE
;
315 if (!TREE_PUBLIC (node
->decl
))
316 return AVAIL_AVAILABLE
;
317 /* If the variable can be overwritten, return OVERWRITABLE. Takes
318 care of at least two notable extensions - the COMDAT variables
319 used to share template instantiations in C++. */
320 if (!(*targetm
.binds_local_p
) (node
->decl
) && !DECL_COMDAT (node
->decl
))
321 return AVAIL_OVERWRITABLE
;
322 return AVAIL_AVAILABLE
;
325 /* Walk the decls we marked as necessary and see if they reference new
326 variables or functions and add them into the worklists. */
328 varpool_analyze_pending_decls (void)
330 bool changed
= false;
331 timevar_push (TV_CGRAPH
);
333 while (varpool_first_unanalyzed_node
)
335 tree decl
= varpool_first_unanalyzed_node
->decl
;
337 varpool_first_unanalyzed_node
->analyzed
= true;
339 varpool_first_unanalyzed_node
= varpool_first_unanalyzed_node
->next_needed
;
341 /* Compute the alignment early so function body expanders are
342 already informed about increased alignment. */
343 align_variable (decl
, 0);
345 if (DECL_INITIAL (decl
))
346 record_references_in_initializer (decl
);
349 timevar_pop (TV_CGRAPH
);
353 /* Output one variable, if necessary. Return whether we output it. */
355 varpool_assemble_decl (struct varpool_node
*node
)
357 tree decl
= node
->decl
;
359 if (!TREE_ASM_WRITTEN (decl
)
361 && !DECL_EXTERNAL (decl
)
362 && (TREE_CODE (decl
) != VAR_DECL
|| !DECL_HAS_VALUE_EXPR_P (decl
)))
364 assemble_variable (decl
, 0, 1, 0);
365 if (TREE_ASM_WRITTEN (decl
))
367 node
->next_needed
= varpool_assembled_nodes_queue
;
368 varpool_assembled_nodes_queue
= node
;
377 /* Optimization of function bodies might've rendered some variables as
378 unnecessary so we want to avoid these from being compiled.
380 This is done by pruning the queue and keeping only the variables that
381 really appear needed (ie they are either externally visible or referenced
382 by compiled function). Re-doing the reachability analysis on variables
383 brings back the remaining variables referenced by these. */
385 varpool_remove_unreferenced_decls (void)
387 struct varpool_node
*next
, *node
= varpool_nodes_queue
;
389 varpool_reset_queue ();
391 if (errorcount
|| sorrycount
)
396 tree decl
= node
->decl
;
397 next
= node
->next_needed
;
401 && (decide_is_variable_needed (node
, decl
)
402 /* ??? Cgraph does not yet rule the world with an iron hand,
403 and does not control the emission of debug information.
404 After a variable has its DECL_RTL set, we must assume that
405 it may be referenced by the debug information, and we can
406 no longer elide it. */
407 || DECL_RTL_SET_P (decl
)))
408 varpool_mark_needed_node (node
);
412 /* Make sure we mark alias targets as used targets. */
414 varpool_analyze_pending_decls ();
417 /* Output all variables enqueued to be assembled. */
419 varpool_assemble_pending_decls (void)
421 bool changed
= false;
423 if (errorcount
|| sorrycount
)
426 /* EH might mark decls as needed during expansion. This should be safe since
427 we don't create references to new function, but it should not be used
429 varpool_analyze_pending_decls ();
431 while (varpool_nodes_queue
)
433 struct varpool_node
*node
= varpool_nodes_queue
;
435 varpool_nodes_queue
= varpool_nodes_queue
->next_needed
;
436 if (varpool_assemble_decl (node
))
439 node
->next_needed
= NULL
;
441 /* varpool_nodes_queue is now empty, clear the pointer to the last element
443 varpool_last_needed_node
= NULL
;
447 /* Remove all elements from the queue so we can re-use it for debug output. */
449 varpool_empty_needed_queue (void)
451 /* EH might mark decls as needed during expansion. This should be safe since
452 we don't create references to new function, but it should not be used
454 varpool_analyze_pending_decls ();
456 while (varpool_nodes_queue
)
458 struct varpool_node
*node
= varpool_nodes_queue
;
459 varpool_nodes_queue
= varpool_nodes_queue
->next_needed
;
460 node
->next_needed
= NULL
;
462 /* varpool_nodes_queue is now empty, clear the pointer to the last element
464 varpool_last_needed_node
= NULL
;
467 /* Create a new global variable of type TYPE. */
469 add_new_static_var (tree type
)
472 struct varpool_node
*new_node
;
474 new_decl
= create_tmp_var (type
, NULL
);
475 DECL_NAME (new_decl
) = create_tmp_var_name (NULL
);
476 TREE_READONLY (new_decl
) = 0;
477 TREE_STATIC (new_decl
) = 1;
478 TREE_USED (new_decl
) = 1;
479 DECL_CONTEXT (new_decl
) = NULL_TREE
;
480 DECL_ABSTRACT (new_decl
) = 0;
481 lang_hooks
.dup_lang_specific_decl (new_decl
);
482 create_var_ann (new_decl
);
483 new_node
= varpool_node (new_decl
);
484 varpool_mark_needed_node (new_node
);
485 add_referenced_var (new_decl
);
486 varpool_finalize_decl (new_decl
);
488 return new_node
->decl
;
491 #include "gt-varpool.h"