1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "coretypes.h"
28 #include "langhooks.h"
29 #include "diagnostic.h"
37 /* This file contains basic routines manipulating variable pool.
39 Varpool acts as interface in between the front-end and middle-end
40 and drives the decision process on what variables and when are
43 The varpool nodes are allocated lazily for declarations
44 either by frontend or at callgraph construction time.
45 All variables supposed to be output into final file needs to be
46 explicitly marked by frontend via VARPOOL_FINALIZE_DECL function. */
48 /* Hash table used to convert declarations into nodes. */
49 static GTY((param_is (struct varpool_node
))) htab_t varpool_hash
;
51 /* The linked list of cgraph varpool nodes.
52 Linked via node->next pointer. */
53 struct varpool_node
*varpool_nodes
;
55 /* Queue of cgraph nodes scheduled to be lowered and output.
56 The queue is maintained via mark_needed_node, linked via node->next_needed
59 LAST_NNEDED_NODE points to the end of queue, so it can be maintained in forward
60 order. QTY is needed to make it friendly to PCH.
62 During unit-at-a-time compilation we construct the queue of needed variables
63 twice: first time it is during cgraph construction, second time it is at the
64 end of compilation in VARPOOL_REMOVE_UNREFERENCED_DECLS so we can avoid
65 optimized out variables being output.
67 Each variable is thus first analyzed and then later possibly output.
68 FIRST_UNANALYZED_NODE points to first node in queue that was not analyzed
69 yet and is moved via VARPOOL_ANALYZE_PENDING_DECLS. */
71 struct varpool_node
*varpool_nodes_queue
;
72 static GTY(()) struct varpool_node
*varpool_last_needed_node
;
73 static GTY(()) struct varpool_node
*varpool_first_unanalyzed_node
;
75 /* Lists all assembled variables to be sent to debugger output later on. */
76 static GTY(()) struct varpool_node
*varpool_assembled_nodes_queue
;
78 /* Return name of the node used in debug output. */
80 varpool_node_name (struct varpool_node
*node
)
82 return lang_hooks
.decl_printable_name (node
->decl
, 2);
85 /* Returns a hash code for P. */
87 hash_varpool_node (const void *p
)
89 const struct varpool_node
*n
= (const struct varpool_node
*) p
;
90 return (hashval_t
) DECL_UID (n
->decl
);
93 /* Returns nonzero if P1 and P2 are equal. */
95 eq_varpool_node (const void *p1
, const void *p2
)
97 const struct varpool_node
*n1
=
98 (const struct varpool_node
*) p1
;
99 const struct varpool_node
*n2
=
100 (const struct varpool_node
*) p2
;
101 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
104 /* Return varpool node assigned to DECL. Create new one when needed. */
105 struct varpool_node
*
106 varpool_node (tree decl
)
108 struct varpool_node key
, *node
, **slot
;
110 gcc_assert (DECL_P (decl
) && TREE_CODE (decl
) != FUNCTION_DECL
);
113 varpool_hash
= htab_create_ggc (10, hash_varpool_node
,
114 eq_varpool_node
, NULL
);
116 slot
= (struct varpool_node
**)
117 htab_find_slot (varpool_hash
, &key
, INSERT
);
120 node
= GGC_CNEW (struct varpool_node
);
122 node
->order
= cgraph_order
++;
123 node
->next
= varpool_nodes
;
124 varpool_nodes
= node
;
129 /* Dump given cgraph node. */
131 dump_varpool_node (FILE *f
, struct varpool_node
*node
)
133 fprintf (f
, "%s:", varpool_node_name (node
));
134 fprintf (f
, " availability:%s",
135 cgraph_function_flags_ready
136 ? cgraph_availability_names
[cgraph_variable_initializer_availability (node
)]
138 if (DECL_INITIAL (node
->decl
))
139 fprintf (f
, " initialized");
141 fprintf (f
, " needed");
143 fprintf (f
, " analyzed");
145 fprintf (f
, " finalized");
147 fprintf (f
, " output");
148 if (node
->externally_visible
)
149 fprintf (f
, " externally_visible");
153 /* Dump the variable pool. */
155 dump_varpool (FILE *f
)
157 struct varpool_node
*node
;
159 fprintf (f
, "variable pool:\n\n");
160 for (node
= varpool_nodes
; node
; node
= node
->next_needed
)
161 dump_varpool_node (f
, node
);
164 /* Given an assembler name, lookup node. */
165 struct varpool_node
*
166 varpool_node_for_asm (tree asmname
)
168 struct varpool_node
*node
;
170 for (node
= varpool_nodes
; node
; node
= node
->next
)
171 if (decl_assembler_name_equal (node
->decl
, asmname
))
177 /* Helper function for finalization code - add node into lists so it will
178 be analyzed and compiled. */
180 varpool_enqueue_needed_node (struct varpool_node
*node
)
182 if (varpool_last_needed_node
)
183 varpool_last_needed_node
->next_needed
= node
;
184 varpool_last_needed_node
= node
;
185 node
->next_needed
= NULL
;
186 if (!varpool_nodes_queue
)
187 varpool_nodes_queue
= node
;
188 if (!varpool_first_unanalyzed_node
)
189 varpool_first_unanalyzed_node
= node
;
190 notice_global_symbol (node
->decl
);
193 /* Notify finalize_compilation_unit that given node is reachable
196 varpool_mark_needed_node (struct varpool_node
*node
)
198 if (!node
->needed
&& node
->finalized
199 && !TREE_ASM_WRITTEN (node
->decl
))
200 varpool_enqueue_needed_node (node
);
204 /* Reset the queue of needed nodes. */
206 varpool_reset_queue (void)
208 varpool_last_needed_node
= NULL
;
209 varpool_nodes_queue
= NULL
;
210 varpool_first_unanalyzed_node
= NULL
;
213 /* Determine if variable DECL is needed. That is, visible to something
214 either outside this translation unit, something magic in the system
215 configury, or (if not doing unit-at-a-time) to something we haven't
218 decide_is_variable_needed (struct varpool_node
*node
, tree decl
)
220 /* If the user told us it is used, then it must be so. */
221 if (node
->externally_visible
)
223 if (!flag_unit_at_a_time
224 && lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
227 /* ??? If the assembler name is set by hand, it is possible to assemble
228 the name later after finalizing the function and the fact is noticed
229 in assemble_name then. This is arguably a bug. */
230 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
231 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
234 /* If we decided it was needed before, but at the time we didn't have
235 the definition available, then it's still needed. */
239 /* Externally visible variables must be output. The exception is
240 COMDAT variables that must be output only when they are needed. */
241 if (TREE_PUBLIC (decl
) && !flag_whole_program
&& !DECL_COMDAT (decl
)
242 && !DECL_EXTERNAL (decl
))
245 /* When not reordering top level variables, we have to assume that
246 we are going to keep everything. */
247 if (flag_unit_at_a_time
&& flag_toplevel_reorder
)
250 /* We want to emit COMDAT variables only when absolutely necessary. */
251 if (DECL_COMDAT (decl
))
256 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
257 middle end to output the variable to asm file, if needed or externally
260 varpool_finalize_decl (tree decl
)
262 struct varpool_node
*node
= varpool_node (decl
);
264 /* The first declaration of a variable that comes through this function
265 decides whether it is global (in C, has external linkage)
266 or local (in C, has internal linkage). So do nothing more
267 if this function has already run. */
270 if (cgraph_global_info_ready
|| (!flag_unit_at_a_time
&& !flag_openmp
))
271 varpool_assemble_pending_decls ();
275 varpool_enqueue_needed_node (node
);
276 node
->finalized
= true;
278 if (decide_is_variable_needed (node
, decl
))
279 varpool_mark_needed_node (node
);
280 /* Since we reclaim unreachable nodes at the end of every language
281 level unit, we need to be conservative about possible entry points
283 else if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
284 varpool_mark_needed_node (node
);
285 if (cgraph_global_info_ready
|| (!flag_unit_at_a_time
&& !flag_openmp
))
286 varpool_assemble_pending_decls ();
289 /* Return variable availability. See cgraph.h for description of individual
292 cgraph_variable_initializer_availability (struct varpool_node
*node
)
294 gcc_assert (cgraph_function_flags_ready
);
295 if (!node
->finalized
)
296 return AVAIL_NOT_AVAILABLE
;
297 if (!TREE_PUBLIC (node
->decl
))
298 return AVAIL_AVAILABLE
;
299 /* If the variable can be overwritten, return OVERWRITABLE. Takes
300 care of at least two notable extensions - the COMDAT variables
301 used to share template instantiations in C++. */
302 if (!(*targetm
.binds_local_p
) (node
->decl
) && !DECL_COMDAT (node
->decl
))
303 return AVAIL_OVERWRITABLE
;
304 return AVAIL_AVAILABLE
;
307 /* Walk the decls we marked as necessary and see if they reference new
308 variables or functions and add them into the worklists. */
310 varpool_analyze_pending_decls (void)
312 bool changed
= false;
313 timevar_push (TV_CGRAPH
);
315 while (varpool_first_unanalyzed_node
)
317 tree decl
= varpool_first_unanalyzed_node
->decl
;
319 varpool_first_unanalyzed_node
->analyzed
= true;
321 varpool_first_unanalyzed_node
= varpool_first_unanalyzed_node
->next_needed
;
323 /* Compute the alignment early so function body expanders are
324 already informed about increased alignment. */
325 align_variable (decl
, 0);
327 if (DECL_INITIAL (decl
))
328 record_references_in_initializer (decl
);
331 timevar_pop (TV_CGRAPH
);
335 /* Output one variable, if necessary. Return whether we output it. */
337 varpool_assemble_decl (struct varpool_node
*node
)
339 tree decl
= node
->decl
;
341 if (!TREE_ASM_WRITTEN (decl
)
343 && !DECL_EXTERNAL (decl
)
344 && (TREE_CODE (decl
) != VAR_DECL
|| !DECL_HAS_VALUE_EXPR_P (decl
)))
346 assemble_variable (decl
, 0, 1, 0);
347 return TREE_ASM_WRITTEN (decl
);
353 /* Optimization of function bodies might've rendered some variables as
354 unnecessary so we want to avoid these from being compiled.
356 This is done by pruning the queue and keeping only the variables that
357 really appear needed (ie they are either externally visible or referenced
358 by compiled function). Re-doing the reachability analysis on variables
359 brings back the remaining variables referenced by these. */
361 varpool_remove_unreferenced_decls (void)
363 struct varpool_node
*next
, *node
= varpool_nodes_queue
;
365 varpool_reset_queue ();
367 if (errorcount
|| sorrycount
)
372 tree decl
= node
->decl
;
373 next
= node
->next_needed
;
377 && ((DECL_ASSEMBLER_NAME_SET_P (decl
)
378 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
379 || node
->force_output
380 || decide_is_variable_needed (node
, decl
)
381 /* ??? Cgraph does not yet rule the world with an iron hand,
382 and does not control the emission of debug information.
383 After a variable has its DECL_RTL set, we must assume that
384 it may be referenced by the debug information, and we can
385 no longer elide it. */
386 || DECL_RTL_SET_P (decl
)))
387 varpool_mark_needed_node (node
);
391 /* Make sure we mark alias targets as used targets. */
393 varpool_analyze_pending_decls ();
396 /* Output all variables enqueued to be assembled. */
398 varpool_assemble_pending_decls (void)
400 bool changed
= false;
402 if (errorcount
|| sorrycount
)
405 /* EH might mark decls as needed during expansion. This should be safe since
406 we don't create references to new function, but it should not be used
408 varpool_analyze_pending_decls ();
410 while (varpool_nodes_queue
)
412 struct varpool_node
*node
= varpool_nodes_queue
;
414 varpool_nodes_queue
= varpool_nodes_queue
->next_needed
;
415 if (varpool_assemble_decl (node
))
418 node
->next_needed
= varpool_assembled_nodes_queue
;
419 varpool_assembled_nodes_queue
= node
;
423 node
->next_needed
= NULL
;
428 /* Output all variables enqueued to be assembled. */
430 varpool_output_debug_info (void)
432 timevar_push (TV_SYMOUT
);
433 if (errorcount
== 0 && sorrycount
== 0)
434 while (varpool_assembled_nodes_queue
)
436 struct varpool_node
*node
= varpool_assembled_nodes_queue
;
438 /* Local static variables are never seen by check_global_declarations
439 so we need to output debug info by hand. */
440 if (DECL_CONTEXT (node
->decl
)
441 && (TREE_CODE (DECL_CONTEXT (node
->decl
)) == BLOCK
442 || TREE_CODE (DECL_CONTEXT (node
->decl
)) == FUNCTION_DECL
)
443 && errorcount
== 0 && sorrycount
== 0)
444 (*debug_hooks
->global_decl
) (node
->decl
);
445 varpool_assembled_nodes_queue
= node
->next_needed
;
446 node
->next_needed
= 0;
448 timevar_pop (TV_SYMOUT
);
451 #include "gt-varpool.h"