cgraph.h (struct varpool_node): Add aux.
[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 without creating new one. */
109 struct varpool_node *
110 varpool_get_node (tree decl)
111 {
112 struct varpool_node key, **slot;
113
114 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
115
116 if (!varpool_hash)
117 return NULL;
118 key.decl = decl;
119 slot = (struct varpool_node **)
120 htab_find_slot (varpool_hash, &key, INSERT);
121 return *slot;
122 }
123
124 /* Return varpool node assigned to DECL. Create new one when needed. */
125 struct varpool_node *
126 varpool_node (tree decl)
127 {
128 struct varpool_node key, *node, **slot;
129
130 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
131
132 if (!varpool_hash)
133 varpool_hash = htab_create_ggc (10, hash_varpool_node,
134 eq_varpool_node, NULL);
135 key.decl = decl;
136 slot = (struct varpool_node **)
137 htab_find_slot (varpool_hash, &key, INSERT);
138 if (*slot)
139 return *slot;
140 node = GGC_CNEW (struct varpool_node);
141 node->decl = decl;
142 node->order = cgraph_order++;
143 node->next = varpool_nodes;
144 ipa_empty_ref_list (&node->ref_list);
145 if (varpool_nodes)
146 varpool_nodes->prev = node;
147 varpool_nodes = node;
148 *slot = node;
149 return node;
150 }
151
152 /* Remove node from the varpool. */
153 void
154 varpool_remove_node (struct varpool_node *node)
155 {
156 void **slot;
157 slot = htab_find_slot (varpool_hash, node, NO_INSERT);
158 gcc_assert (*slot == node);
159 htab_clear_slot (varpool_hash, slot);
160 gcc_assert (!varpool_assembled_nodes_queue);
161 if (!node->alias)
162 while (node->extra_name)
163 varpool_remove_node (node->extra_name);
164 if (node->next)
165 node->next->prev = node->prev;
166 if (node->prev)
167 node->prev->next = node->next;
168 else
169 {
170 if (node->alias)
171 {
172 gcc_assert (node->extra_name->extra_name == node);
173 node->extra_name->extra_name = node->next;
174 }
175 else
176 {
177 gcc_assert (varpool_nodes == node);
178 varpool_nodes = node->next;
179 }
180 }
181 if (varpool_first_unanalyzed_node == node)
182 varpool_first_unanalyzed_node = node->next_needed;
183 if (node->next_needed)
184 node->next_needed->prev_needed = node->prev_needed;
185 else if (node->prev_needed)
186 {
187 gcc_assert (varpool_last_needed_node);
188 varpool_last_needed_node = node->prev_needed;
189 }
190 if (node->prev_needed)
191 node->prev_needed->next_needed = node->next_needed;
192 else if (node->next_needed)
193 {
194 gcc_assert (varpool_nodes_queue == node);
195 varpool_nodes_queue = node->next_needed;
196 }
197 ipa_remove_all_references (&node->ref_list);
198 ipa_remove_all_refering (&node->ref_list);
199 ggc_free (node);
200 }
201
202 /* Dump given cgraph node. */
203 void
204 dump_varpool_node (FILE *f, struct varpool_node *node)
205 {
206 fprintf (f, "%s:", varpool_node_name (node));
207 fprintf (f, " availability:%s",
208 cgraph_function_flags_ready
209 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
210 : "not-ready");
211 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
212 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
213 if (DECL_INITIAL (node->decl))
214 fprintf (f, " initialized");
215 if (TREE_ASM_WRITTEN (node->decl))
216 fprintf (f, " (asm written)");
217 if (node->needed)
218 fprintf (f, " needed");
219 if (node->analyzed)
220 fprintf (f, " analyzed");
221 if (node->finalized)
222 fprintf (f, " finalized");
223 if (node->output)
224 fprintf (f, " output");
225 if (node->externally_visible)
226 fprintf (f, " externally_visible");
227 if (node->in_other_partition)
228 fprintf (f, " in_other_partition");
229 else if (node->used_from_other_partition)
230 fprintf (f, " used_from_other_partition");
231 fprintf (f, "\n");
232 fprintf (f, " References: ");
233 ipa_dump_references (f, &node->ref_list);
234 fprintf (f, " Refering this var: ");
235 ipa_dump_refering (f, &node->ref_list);
236 }
237
238 /* Dump the variable pool. */
239 void
240 dump_varpool (FILE *f)
241 {
242 struct varpool_node *node;
243
244 fprintf (f, "variable pool:\n\n");
245 for (node = varpool_nodes; node; node = node->next)
246 dump_varpool_node (f, node);
247 }
248
249 /* Dump the variable pool to stderr. */
250
251 void
252 debug_varpool (void)
253 {
254 dump_varpool (stderr);
255 }
256
257 /* Given an assembler name, lookup node. */
258 struct varpool_node *
259 varpool_node_for_asm (tree asmname)
260 {
261 struct varpool_node *node;
262
263 for (node = varpool_nodes; node ; node = node->next)
264 if (decl_assembler_name_equal (node->decl, asmname))
265 return node;
266
267 return NULL;
268 }
269
270 /* Helper function for finalization code - add node into lists so it will
271 be analyzed and compiled. */
272 static void
273 varpool_enqueue_needed_node (struct varpool_node *node)
274 {
275 if (varpool_last_needed_node)
276 {
277 varpool_last_needed_node->next_needed = node;
278 node->prev_needed = varpool_last_needed_node;
279 }
280 varpool_last_needed_node = node;
281 node->next_needed = NULL;
282 if (!varpool_nodes_queue)
283 varpool_nodes_queue = node;
284 if (!varpool_first_unanalyzed_node)
285 varpool_first_unanalyzed_node = node;
286 notice_global_symbol (node->decl);
287 }
288
289 /* Notify finalize_compilation_unit that given node is reachable
290 or needed. */
291 void
292 varpool_mark_needed_node (struct varpool_node *node)
293 {
294 if (node->alias && node->extra_name)
295 node = node->extra_name;
296 if (!node->needed && node->finalized
297 && !TREE_ASM_WRITTEN (node->decl))
298 varpool_enqueue_needed_node (node);
299 node->needed = 1;
300 }
301
302 /* Reset the queue of needed nodes. */
303 void
304 varpool_reset_queue (void)
305 {
306 varpool_last_needed_node = NULL;
307 varpool_nodes_queue = NULL;
308 varpool_first_unanalyzed_node = NULL;
309 }
310
311 /* Determine if variable DECL is needed. That is, visible to something
312 either outside this translation unit, something magic in the system
313 configury */
314 bool
315 decide_is_variable_needed (struct varpool_node *node, tree decl)
316 {
317 if (node->used_from_other_partition)
318 return true;
319 /* If the user told us it is used, then it must be so. */
320 if ((node->externally_visible && !DECL_COMDAT (decl))
321 || node->force_output)
322 return true;
323
324 /* ??? If the assembler name is set by hand, it is possible to assemble
325 the name later after finalizing the function and the fact is noticed
326 in assemble_name then. This is arguably a bug. */
327 if (DECL_ASSEMBLER_NAME_SET_P (decl)
328 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
329 return true;
330
331 /* Externally visible variables must be output. The exception is
332 COMDAT variables that must be output only when they are needed. */
333 if (TREE_PUBLIC (decl)
334 && !flag_whole_program
335 && !flag_lto
336 && !flag_whopr
337 && !DECL_COMDAT (decl)
338 && !DECL_EXTERNAL (decl))
339 return true;
340
341 /* When emulating tls, we actually see references to the control
342 variable, rather than the user-level variable. */
343 if (!targetm.have_tls
344 && TREE_CODE (decl) == VAR_DECL
345 && DECL_THREAD_LOCAL_P (decl))
346 {
347 tree control = emutls_decl (decl);
348 if (decide_is_variable_needed (varpool_node (control), control))
349 return true;
350 }
351
352 /* When not reordering top level variables, we have to assume that
353 we are going to keep everything. */
354 if (flag_toplevel_reorder)
355 return false;
356
357 /* We want to emit COMDAT variables only when absolutely necessary. */
358 if (DECL_COMDAT (decl))
359 return false;
360 return true;
361 }
362
363 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
364 middle end to output the variable to asm file, if needed or externally
365 visible. */
366 void
367 varpool_finalize_decl (tree decl)
368 {
369 struct varpool_node *node = varpool_node (decl);
370
371 /* The first declaration of a variable that comes through this function
372 decides whether it is global (in C, has external linkage)
373 or local (in C, has internal linkage). So do nothing more
374 if this function has already run. */
375 if (node->finalized)
376 {
377 if (cgraph_global_info_ready)
378 varpool_assemble_pending_decls ();
379 return;
380 }
381 if (node->needed)
382 varpool_enqueue_needed_node (node);
383 node->finalized = true;
384 if (TREE_THIS_VOLATILE (decl))
385 node->force_output = true;
386
387 if (decide_is_variable_needed (node, decl))
388 varpool_mark_needed_node (node);
389 /* Since we reclaim unreachable nodes at the end of every language
390 level unit, we need to be conservative about possible entry points
391 there. */
392 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
393 varpool_mark_needed_node (node);
394 if (cgraph_global_info_ready)
395 varpool_assemble_pending_decls ();
396 }
397
398 /* Return variable availability. See cgraph.h for description of individual
399 return values. */
400 enum availability
401 cgraph_variable_initializer_availability (struct varpool_node *node)
402 {
403 gcc_assert (cgraph_function_flags_ready);
404 if (!node->finalized)
405 return AVAIL_NOT_AVAILABLE;
406 if (!TREE_PUBLIC (node->decl))
407 return AVAIL_AVAILABLE;
408 /* If the variable can be overwritten, return OVERWRITABLE. Takes
409 care of at least two notable extensions - the COMDAT variables
410 used to share template instantiations in C++. */
411 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
412 return AVAIL_OVERWRITABLE;
413 return AVAIL_AVAILABLE;
414 }
415
416 /* Walk the decls we marked as necessary and see if they reference new
417 variables or functions and add them into the worklists. */
418 bool
419 varpool_analyze_pending_decls (void)
420 {
421 bool changed = false;
422
423 timevar_push (TV_VARPOOL);
424 while (varpool_first_unanalyzed_node)
425 {
426 tree decl = varpool_first_unanalyzed_node->decl;
427 bool analyzed = varpool_first_unanalyzed_node->analyzed;
428
429 varpool_first_unanalyzed_node->analyzed = true;
430
431 varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed;
432
433 /* When reading back varpool at LTO time, we re-construct the queue in order
434 to have "needed" list right by inserting all needed nodes into varpool.
435 We however don't want to re-analyze already analyzed nodes. */
436 if (!analyzed)
437 {
438 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
439 /* Compute the alignment early so function body expanders are
440 already informed about increased alignment. */
441 align_variable (decl, 0);
442 }
443 if (DECL_INITIAL (decl))
444 record_references_in_initializer (decl, analyzed);
445 changed = true;
446 }
447 timevar_pop (TV_VARPOOL);
448 return changed;
449 }
450
451 /* Output one variable, if necessary. Return whether we output it. */
452 bool
453 varpool_assemble_decl (struct varpool_node *node)
454 {
455 tree decl = node->decl;
456
457 if (!TREE_ASM_WRITTEN (decl)
458 && !node->alias
459 && !node->in_other_partition
460 && !DECL_EXTERNAL (decl)
461 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
462 {
463 assemble_variable (decl, 0, 1, 0);
464 if (TREE_ASM_WRITTEN (decl))
465 {
466 struct varpool_node *alias;
467
468 node->next_needed = varpool_assembled_nodes_queue;
469 node->prev_needed = NULL;
470 if (varpool_assembled_nodes_queue)
471 varpool_assembled_nodes_queue->prev_needed = node;
472 varpool_assembled_nodes_queue = node;
473 node->finalized = 1;
474
475 /* Also emit any extra name aliases. */
476 for (alias = node->extra_name; alias; alias = alias->next)
477 {
478 /* Update linkage fields in case they've changed. */
479 DECL_WEAK (alias->decl) = DECL_WEAK (decl);
480 TREE_PUBLIC (alias->decl) = TREE_PUBLIC (decl);
481 DECL_VISIBILITY (alias->decl) = DECL_VISIBILITY (decl);
482 assemble_alias (alias->decl, DECL_ASSEMBLER_NAME (decl));
483 }
484
485 return true;
486 }
487 }
488
489 return false;
490 }
491
492 /* Optimization of function bodies might've rendered some variables as
493 unnecessary so we want to avoid these from being compiled.
494
495 This is done by pruning the queue and keeping only the variables that
496 really appear needed (ie they are either externally visible or referenced
497 by compiled function). Re-doing the reachability analysis on variables
498 brings back the remaining variables referenced by these. */
499 void
500 varpool_remove_unreferenced_decls (void)
501 {
502 struct varpool_node *next, *node = varpool_nodes_queue;
503
504 varpool_reset_queue ();
505
506 if (errorcount || sorrycount)
507 return;
508
509 while (node)
510 {
511 tree decl = node->decl;
512 next = node->next_needed;
513 node->needed = 0;
514
515 if (node->finalized
516 && (decide_is_variable_needed (node, decl)
517 /* ??? Cgraph does not yet rule the world with an iron hand,
518 and does not control the emission of debug information.
519 After a variable has its DECL_RTL set, we must assume that
520 it may be referenced by the debug information, and we can
521 no longer elide it. */
522 || DECL_RTL_SET_P (decl)))
523 varpool_mark_needed_node (node);
524
525 node = next;
526 }
527 /* Make sure we mark alias targets as used targets. */
528 finish_aliases_1 ();
529 varpool_analyze_pending_decls ();
530 }
531
532 /* Output all variables enqueued to be assembled. */
533 bool
534 varpool_assemble_pending_decls (void)
535 {
536 bool changed = false;
537
538 if (errorcount || sorrycount)
539 return false;
540
541 timevar_push (TV_VAROUT);
542 /* EH might mark decls as needed during expansion. This should be safe since
543 we don't create references to new function, but it should not be used
544 elsewhere. */
545 varpool_analyze_pending_decls ();
546
547 while (varpool_nodes_queue)
548 {
549 struct varpool_node *node = varpool_nodes_queue;
550
551 varpool_nodes_queue = varpool_nodes_queue->next_needed;
552 if (varpool_assemble_decl (node))
553 changed = true;
554 else
555 {
556 node->prev_needed = NULL;
557 node->next_needed = NULL;
558 }
559 }
560 /* varpool_nodes_queue is now empty, clear the pointer to the last element
561 in the queue. */
562 varpool_last_needed_node = NULL;
563 timevar_pop (TV_VAROUT);
564 return changed;
565 }
566
567 /* Remove all elements from the queue so we can re-use it for debug output. */
568 void
569 varpool_empty_needed_queue (void)
570 {
571 /* EH might mark decls as needed during expansion. This should be safe since
572 we don't create references to new function, but it should not be used
573 elsewhere. */
574 varpool_analyze_pending_decls ();
575
576 while (varpool_nodes_queue)
577 {
578 struct varpool_node *node = varpool_nodes_queue;
579 varpool_nodes_queue = varpool_nodes_queue->next_needed;
580 node->next_needed = NULL;
581 node->prev_needed = NULL;
582 }
583 /* varpool_nodes_queue is now empty, clear the pointer to the last element
584 in the queue. */
585 varpool_last_needed_node = NULL;
586 }
587
588 /* Create a new global variable of type TYPE. */
589 tree
590 add_new_static_var (tree type)
591 {
592 tree new_decl;
593 struct varpool_node *new_node;
594
595 new_decl = create_tmp_var (type, NULL);
596 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
597 TREE_READONLY (new_decl) = 0;
598 TREE_STATIC (new_decl) = 1;
599 TREE_USED (new_decl) = 1;
600 DECL_CONTEXT (new_decl) = NULL_TREE;
601 DECL_ABSTRACT (new_decl) = 0;
602 lang_hooks.dup_lang_specific_decl (new_decl);
603 create_var_ann (new_decl);
604 new_node = varpool_node (new_decl);
605 varpool_mark_needed_node (new_node);
606 add_referenced_var (new_decl);
607 varpool_finalize_decl (new_decl);
608
609 return new_node->decl;
610 }
611
612 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
613 Extra name aliases are output whenever DECL is output. */
614
615 bool
616 varpool_extra_name_alias (tree alias, tree decl)
617 {
618 struct varpool_node key, *alias_node, *decl_node, **slot;
619
620 #ifndef ASM_OUTPUT_DEF
621 /* If aliases aren't supported by the assembler, fail. */
622 return false;
623 #endif
624
625 gcc_assert (TREE_CODE (decl) == VAR_DECL);
626 gcc_assert (TREE_CODE (alias) == VAR_DECL);
627 /* Make sure the hash table has been created. */
628 decl_node = varpool_node (decl);
629
630 key.decl = alias;
631
632 slot = (struct varpool_node **) htab_find_slot (varpool_hash, &key, INSERT);
633
634 /* If the varpool_node has been already created, fail. */
635 if (*slot)
636 return false;
637
638 alias_node = GGC_CNEW (struct varpool_node);
639 alias_node->decl = alias;
640 alias_node->alias = 1;
641 alias_node->extra_name = decl_node;
642 alias_node->next = decl_node->extra_name;
643 ipa_empty_ref_list (&alias_node->ref_list);
644 if (decl_node->extra_name)
645 decl_node->extra_name->prev = alias_node;
646 decl_node->extra_name = alias_node;
647 *slot = alias_node;
648 return true;
649 }
650
651 #include "gt-varpool.h"