40decfc867b542c6eacdfc561f8b69baf581cbdf
[gcc.git] / gcc / varpool.c
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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.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"
39
40 /* This file contains basic routines manipulating variable pool.
41
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.
45
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. */
50
51 /* Hash table used to convert declarations into nodes. */
52 static GTY((param_is (struct varpool_node))) htab_t varpool_hash;
53
54 /* The linked list of cgraph varpool nodes.
55 Linked via node->next pointer. */
56 struct varpool_node *varpool_nodes;
57
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.
61
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.
65
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.
70
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. */
74
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;
78
79 /* Lists all assembled variables to be sent to debugger output later on. */
80 static GTY(()) struct varpool_node *varpool_assembled_nodes_queue;
81
82 /* Return name of the node used in debug output. */
83 const char *
84 varpool_node_name (struct varpool_node *node)
85 {
86 return lang_hooks.decl_printable_name (node->decl, 2);
87 }
88
89 /* Returns a hash code for P. */
90 static hashval_t
91 hash_varpool_node (const void *p)
92 {
93 const struct varpool_node *n = (const struct varpool_node *) p;
94 return (hashval_t) DECL_UID (n->decl);
95 }
96
97 /* Returns nonzero if P1 and P2 are equal. */
98 static int
99 eq_varpool_node (const void *p1, const void *p2)
100 {
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);
106 }
107
108 /* Return varpool node assigned to DECL. Create new one when needed. */
109 struct varpool_node *
110 varpool_node (tree decl)
111 {
112 struct varpool_node key, *node, **slot;
113
114 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
115
116 if (!varpool_hash)
117 varpool_hash = htab_create_ggc (10, hash_varpool_node,
118 eq_varpool_node, NULL);
119 key.decl = decl;
120 slot = (struct varpool_node **)
121 htab_find_slot (varpool_hash, &key, INSERT);
122 if (*slot)
123 return *slot;
124 node = GGC_CNEW (struct varpool_node);
125 node->decl = decl;
126 node->order = cgraph_order++;
127 node->next = varpool_nodes;
128 varpool_nodes = node;
129 *slot = node;
130 return node;
131 }
132
133 /* Dump given cgraph node. */
134 void
135 dump_varpool_node (FILE *f, struct varpool_node *node)
136 {
137 fprintf (f, "%s:", varpool_node_name (node));
138 fprintf (f, " availability:%s",
139 cgraph_function_flags_ready
140 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
141 : "not-ready");
142 if (DECL_INITIAL (node->decl))
143 fprintf (f, " initialized");
144 if (node->needed)
145 fprintf (f, " needed");
146 if (node->analyzed)
147 fprintf (f, " analyzed");
148 if (node->finalized)
149 fprintf (f, " finalized");
150 if (node->output)
151 fprintf (f, " output");
152 if (node->externally_visible)
153 fprintf (f, " externally_visible");
154 fprintf (f, "\n");
155 }
156
157 /* Dump the variable pool. */
158 void
159 dump_varpool (FILE *f)
160 {
161 struct varpool_node *node;
162
163 fprintf (f, "variable pool:\n\n");
164 for (node = varpool_nodes; node; node = node->next)
165 dump_varpool_node (f, node);
166 }
167
168 /* Dump the variable pool to stderr. */
169
170 void
171 debug_varpool (void)
172 {
173 dump_varpool (stderr);
174 }
175
176 /* Given an assembler name, lookup node. */
177 struct varpool_node *
178 varpool_node_for_asm (tree asmname)
179 {
180 struct varpool_node *node;
181
182 for (node = varpool_nodes; node ; node = node->next)
183 if (decl_assembler_name_equal (node->decl, asmname))
184 return node;
185
186 return NULL;
187 }
188
189 /* Helper function for finalization code - add node into lists so it will
190 be analyzed and compiled. */
191 static void
192 varpool_enqueue_needed_node (struct varpool_node *node)
193 {
194 if (varpool_last_needed_node)
195 varpool_last_needed_node->next_needed = node;
196 varpool_last_needed_node = node;
197 node->next_needed = NULL;
198 if (!varpool_nodes_queue)
199 varpool_nodes_queue = node;
200 if (!varpool_first_unanalyzed_node)
201 varpool_first_unanalyzed_node = node;
202 notice_global_symbol (node->decl);
203 }
204
205 /* Notify finalize_compilation_unit that given node is reachable
206 or needed. */
207 void
208 varpool_mark_needed_node (struct varpool_node *node)
209 {
210 if (node->alias && node->extra_name)
211 node = node->extra_name;
212 if (!node->needed && node->finalized
213 && !TREE_ASM_WRITTEN (node->decl))
214 varpool_enqueue_needed_node (node);
215 node->needed = 1;
216 }
217
218 /* Reset the queue of needed nodes. */
219 static void
220 varpool_reset_queue (void)
221 {
222 varpool_last_needed_node = NULL;
223 varpool_nodes_queue = NULL;
224 varpool_first_unanalyzed_node = NULL;
225 }
226
227 /* Determine if variable DECL is needed. That is, visible to something
228 either outside this translation unit, something magic in the system
229 configury */
230 bool
231 decide_is_variable_needed (struct varpool_node *node, tree decl)
232 {
233 /* We do not track variable references at all and thus have no idea if the
234 variable was referenced in some other partition or not.
235 FIXME: We really need address taken edges in callgraph and varpool to
236 drive WPA and decide whether other partition might reference it or not. */
237 if (flag_ltrans)
238 return true;
239 /* If the user told us it is used, then it must be so. */
240 if ((node->externally_visible && !DECL_COMDAT (decl))
241 || node->force_output)
242 return true;
243
244 /* ??? If the assembler name is set by hand, it is possible to assemble
245 the name later after finalizing the function and the fact is noticed
246 in assemble_name then. This is arguably a bug. */
247 if (DECL_ASSEMBLER_NAME_SET_P (decl)
248 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
249 return true;
250
251 /* Externally visible variables must be output. The exception is
252 COMDAT variables that must be output only when they are needed. */
253 if (TREE_PUBLIC (decl)
254 && !flag_whole_program
255 && !flag_lto
256 && !flag_whopr
257 && !DECL_COMDAT (decl)
258 && !DECL_EXTERNAL (decl))
259 return true;
260
261 /* When emulating tls, we actually see references to the control
262 variable, rather than the user-level variable. */
263 if (!targetm.have_tls
264 && TREE_CODE (decl) == VAR_DECL
265 && DECL_THREAD_LOCAL_P (decl))
266 {
267 tree control = emutls_decl (decl);
268 if (decide_is_variable_needed (varpool_node (control), control))
269 return true;
270 }
271
272 /* When not reordering top level variables, we have to assume that
273 we are going to keep everything. */
274 if (flag_toplevel_reorder)
275 return false;
276
277 /* We want to emit COMDAT variables only when absolutely necessary. */
278 if (DECL_COMDAT (decl))
279 return false;
280 return true;
281 }
282
283 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
284 middle end to output the variable to asm file, if needed or externally
285 visible. */
286 void
287 varpool_finalize_decl (tree decl)
288 {
289 struct varpool_node *node = varpool_node (decl);
290
291 /* FIXME: We don't really stream varpool datastructure and instead rebuild it
292 by varpool_finalize_decl. This is not quite correct since this way we can't
293 attach any info to varpool. Eventually we will want to stream varpool nodes
294 and the flags.
295
296 For the moment just prevent analysis of varpool nodes to happen again, so
297 we will re-try to compute "address_taken" flag of varpool that breaks
298 in presence of clones. */
299 if (in_lto_p)
300 node->analyzed = true;
301
302 /* The first declaration of a variable that comes through this function
303 decides whether it is global (in C, has external linkage)
304 or local (in C, has internal linkage). So do nothing more
305 if this function has already run. */
306 if (node->finalized)
307 {
308 if (cgraph_global_info_ready)
309 varpool_assemble_pending_decls ();
310 return;
311 }
312 if (node->needed)
313 varpool_enqueue_needed_node (node);
314 node->finalized = true;
315
316 if (decide_is_variable_needed (node, decl))
317 varpool_mark_needed_node (node);
318 /* Since we reclaim unreachable nodes at the end of every language
319 level unit, we need to be conservative about possible entry points
320 there. */
321 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
322 varpool_mark_needed_node (node);
323 if (cgraph_global_info_ready)
324 varpool_assemble_pending_decls ();
325 }
326
327 /* Return variable availability. See cgraph.h for description of individual
328 return values. */
329 enum availability
330 cgraph_variable_initializer_availability (struct varpool_node *node)
331 {
332 gcc_assert (cgraph_function_flags_ready);
333 if (!node->finalized)
334 return AVAIL_NOT_AVAILABLE;
335 if (!TREE_PUBLIC (node->decl))
336 return AVAIL_AVAILABLE;
337 /* If the variable can be overwritten, return OVERWRITABLE. Takes
338 care of at least two notable extensions - the COMDAT variables
339 used to share template instantiations in C++. */
340 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
341 return AVAIL_OVERWRITABLE;
342 return AVAIL_AVAILABLE;
343 }
344
345 /* Walk the decls we marked as necessary and see if they reference new
346 variables or functions and add them into the worklists. */
347 bool
348 varpool_analyze_pending_decls (void)
349 {
350 bool changed = false;
351 timevar_push (TV_CGRAPH);
352
353 while (varpool_first_unanalyzed_node)
354 {
355 tree decl = varpool_first_unanalyzed_node->decl;
356 bool analyzed = varpool_first_unanalyzed_node->analyzed;
357
358 varpool_first_unanalyzed_node->analyzed = true;
359
360 varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed;
361
362 /* When reading back varpool at LTO time, we re-construct the queue in order
363 to have "needed" list right by inserting all needed nodes into varpool.
364 We however don't want to re-analyze already analyzed nodes. */
365 if (!analyzed)
366 {
367 gcc_assert (!in_lto_p);
368 /* Compute the alignment early so function body expanders are
369 already informed about increased alignment. */
370 align_variable (decl, 0);
371 }
372 if (DECL_INITIAL (decl))
373 record_references_in_initializer (decl, analyzed);
374 changed = true;
375 }
376 timevar_pop (TV_CGRAPH);
377 return changed;
378 }
379
380 /* Output one variable, if necessary. Return whether we output it. */
381 bool
382 varpool_assemble_decl (struct varpool_node *node)
383 {
384 tree decl = node->decl;
385
386 if (!TREE_ASM_WRITTEN (decl)
387 && !node->alias
388 && !DECL_EXTERNAL (decl)
389 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
390 {
391 assemble_variable (decl, 0, 1, 0);
392 if (TREE_ASM_WRITTEN (decl))
393 {
394 struct varpool_node *alias;
395
396 node->next_needed = varpool_assembled_nodes_queue;
397 varpool_assembled_nodes_queue = node;
398 node->finalized = 1;
399
400 /* Also emit any extra name aliases. */
401 for (alias = node->extra_name; alias; alias = alias->next)
402 {
403 /* Update linkage fields in case they've changed. */
404 DECL_WEAK (alias->decl) = DECL_WEAK (decl);
405 TREE_PUBLIC (alias->decl) = TREE_PUBLIC (decl);
406 DECL_VISIBILITY (alias->decl) = DECL_VISIBILITY (decl);
407 assemble_alias (alias->decl, DECL_ASSEMBLER_NAME (decl));
408 }
409
410 return true;
411 }
412 }
413
414 return false;
415 }
416
417 /* Optimization of function bodies might've rendered some variables as
418 unnecessary so we want to avoid these from being compiled.
419
420 This is done by pruning the queue and keeping only the variables that
421 really appear needed (ie they are either externally visible or referenced
422 by compiled function). Re-doing the reachability analysis on variables
423 brings back the remaining variables referenced by these. */
424 void
425 varpool_remove_unreferenced_decls (void)
426 {
427 struct varpool_node *next, *node = varpool_nodes_queue;
428
429 varpool_reset_queue ();
430
431 if (errorcount || sorrycount)
432 return;
433
434 while (node)
435 {
436 tree decl = node->decl;
437 next = node->next_needed;
438 node->needed = 0;
439
440 if (node->finalized
441 && (decide_is_variable_needed (node, decl)
442 /* ??? Cgraph does not yet rule the world with an iron hand,
443 and does not control the emission of debug information.
444 After a variable has its DECL_RTL set, we must assume that
445 it may be referenced by the debug information, and we can
446 no longer elide it. */
447 || DECL_RTL_SET_P (decl)))
448 varpool_mark_needed_node (node);
449
450 node = next;
451 }
452 /* Make sure we mark alias targets as used targets. */
453 finish_aliases_1 ();
454 varpool_analyze_pending_decls ();
455 }
456
457 /* Output all variables enqueued to be assembled. */
458 bool
459 varpool_assemble_pending_decls (void)
460 {
461 bool changed = false;
462
463 if (errorcount || sorrycount)
464 return false;
465
466 /* EH might mark decls as needed during expansion. This should be safe since
467 we don't create references to new function, but it should not be used
468 elsewhere. */
469 varpool_analyze_pending_decls ();
470
471 while (varpool_nodes_queue)
472 {
473 struct varpool_node *node = varpool_nodes_queue;
474
475 varpool_nodes_queue = varpool_nodes_queue->next_needed;
476 if (varpool_assemble_decl (node))
477 changed = true;
478 else
479 node->next_needed = NULL;
480 }
481 /* varpool_nodes_queue is now empty, clear the pointer to the last element
482 in the queue. */
483 varpool_last_needed_node = NULL;
484 return changed;
485 }
486
487 /* Remove all elements from the queue so we can re-use it for debug output. */
488 void
489 varpool_empty_needed_queue (void)
490 {
491 /* EH might mark decls as needed during expansion. This should be safe since
492 we don't create references to new function, but it should not be used
493 elsewhere. */
494 varpool_analyze_pending_decls ();
495
496 while (varpool_nodes_queue)
497 {
498 struct varpool_node *node = varpool_nodes_queue;
499 varpool_nodes_queue = varpool_nodes_queue->next_needed;
500 node->next_needed = NULL;
501 }
502 /* varpool_nodes_queue is now empty, clear the pointer to the last element
503 in the queue. */
504 varpool_last_needed_node = NULL;
505 }
506
507 /* Create a new global variable of type TYPE. */
508 tree
509 add_new_static_var (tree type)
510 {
511 tree new_decl;
512 struct varpool_node *new_node;
513
514 new_decl = create_tmp_var (type, NULL);
515 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
516 TREE_READONLY (new_decl) = 0;
517 TREE_STATIC (new_decl) = 1;
518 TREE_USED (new_decl) = 1;
519 DECL_CONTEXT (new_decl) = NULL_TREE;
520 DECL_ABSTRACT (new_decl) = 0;
521 lang_hooks.dup_lang_specific_decl (new_decl);
522 create_var_ann (new_decl);
523 new_node = varpool_node (new_decl);
524 varpool_mark_needed_node (new_node);
525 add_referenced_var (new_decl);
526 varpool_finalize_decl (new_decl);
527
528 return new_node->decl;
529 }
530
531 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
532 Extra name aliases are output whenever DECL is output. */
533
534 bool
535 varpool_extra_name_alias (tree alias, tree decl)
536 {
537 struct varpool_node key, *alias_node, *decl_node, **slot;
538
539 #ifndef ASM_OUTPUT_DEF
540 /* If aliases aren't supported by the assembler, fail. */
541 return false;
542 #endif
543
544 gcc_assert (TREE_CODE (decl) == VAR_DECL);
545 gcc_assert (TREE_CODE (alias) == VAR_DECL);
546 /* Make sure the hash table has been created. */
547 decl_node = varpool_node (decl);
548
549 key.decl = alias;
550
551 slot = (struct varpool_node **) htab_find_slot (varpool_hash, &key, INSERT);
552
553 /* If the varpool_node has been already created, fail. */
554 if (*slot)
555 return false;
556
557 alias_node = GGC_CNEW (struct varpool_node);
558 alias_node->decl = alias;
559 alias_node->alias = 1;
560 alias_node->extra_name = decl_node;
561 alias_node->next = decl_node->extra_name;
562 decl_node->extra_name = alias_node;
563 *slot = alias_node;
564 return true;
565 }
566
567 #include "gt-varpool.h"