OSDN Git Service

2009-08-04 David Daney <ddaney@caviumnetworks.com>
[pf3gnuchains/gcc-fork.git] / gcc / varpool.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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
39 /*  This file contains basic routines manipulating variable pool.
40
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
43     going to be compiled.
44
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.  */
49
50 /* Hash table used to convert declarations into nodes.  */
51 static GTY((param_is (struct varpool_node))) htab_t varpool_hash;
52
53 /* The linked list of cgraph varpool nodes.
54    Linked via node->next pointer.  */
55 struct varpool_node *varpool_nodes;
56
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
59    pointer. 
60
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
63    PCH.
64  
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.
69    
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.  */
73    
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;
77
78 /* Lists all assembled variables to be sent to debugger output later on.  */
79 static GTY(()) struct varpool_node *varpool_assembled_nodes_queue;
80
81 /* Return name of the node used in debug output.  */
82 static const char *
83 varpool_node_name (struct varpool_node *node)
84 {
85   return lang_hooks.decl_printable_name (node->decl, 2);
86 }
87
88 /* Returns a hash code for P.  */
89 static hashval_t
90 hash_varpool_node (const void *p)
91 {
92   const struct varpool_node *n = (const struct varpool_node *) p;
93   return (hashval_t) DECL_UID (n->decl);
94 }
95
96 /* Returns nonzero if P1 and P2 are equal.  */
97 static int
98 eq_varpool_node (const void *p1, const void *p2)
99 {
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);
105 }
106
107 /* Return varpool node assigned to DECL.  Create new one when needed.  */
108 struct varpool_node *
109 varpool_node (tree decl)
110 {
111   struct varpool_node key, *node, **slot;
112
113   gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
114
115   if (!varpool_hash)
116     varpool_hash = htab_create_ggc (10, hash_varpool_node,
117                                            eq_varpool_node, NULL);
118   key.decl = decl;
119   slot = (struct varpool_node **)
120     htab_find_slot (varpool_hash, &key, INSERT);
121   if (*slot)
122     return *slot;
123   node = GGC_CNEW (struct varpool_node);
124   node->decl = decl;
125   node->order = cgraph_order++;
126   node->next = varpool_nodes;
127   varpool_nodes = node;
128   *slot = node;
129   return node;
130 }
131
132 /* Dump given cgraph node.  */
133 void
134 dump_varpool_node (FILE *f, struct varpool_node *node)
135 {
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)]
140            : "not-ready");
141   if (DECL_INITIAL (node->decl))
142     fprintf (f, " initialized");
143   if (node->needed)
144     fprintf (f, " needed");
145   if (node->analyzed)
146     fprintf (f, " analyzed");
147   if (node->finalized)
148     fprintf (f, " finalized");
149   if (node->output)
150     fprintf (f, " output");
151   if (node->externally_visible)
152     fprintf (f, " externally_visible");
153   fprintf (f, "\n");
154 }
155
156 /* Dump the variable pool.  */
157 void
158 dump_varpool (FILE *f)
159 {
160   struct varpool_node *node;
161
162   fprintf (f, "variable pool:\n\n");
163   for (node = varpool_nodes; node; node = node->next)
164     dump_varpool_node (f, node);
165 }
166
167 /* Dump the variable pool to stderr.  */
168
169 void
170 debug_varpool (void)
171 {
172   dump_varpool (stderr);
173 }
174
175 /* Given an assembler name, lookup node.  */
176 struct varpool_node *
177 varpool_node_for_asm (tree asmname)
178 {
179   struct varpool_node *node;
180
181   for (node = varpool_nodes; node ; node = node->next)
182     if (decl_assembler_name_equal (node->decl, asmname))
183       return node;
184
185   return NULL;
186 }
187
188 /* Helper function for finalization code - add node into lists so it will
189    be analyzed and compiled.  */
190 static void
191 varpool_enqueue_needed_node (struct varpool_node *node)
192 {
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);
202 }
203
204 /* Notify finalize_compilation_unit that given node is reachable
205    or needed.  */
206 void
207 varpool_mark_needed_node (struct varpool_node *node)
208 {
209   if (!node->needed && node->finalized
210       && !TREE_ASM_WRITTEN (node->decl))
211     varpool_enqueue_needed_node (node);
212   node->needed = 1;
213 }
214
215 /* Reset the queue of needed nodes.  */
216 static void
217 varpool_reset_queue (void)
218 {
219   varpool_last_needed_node = NULL;
220   varpool_nodes_queue = NULL;
221   varpool_first_unanalyzed_node = NULL;
222 }
223
224 /* Determine if variable DECL is needed.  That is, visible to something
225    either outside this translation unit, something magic in the system
226    configury */
227 bool
228 decide_is_variable_needed (struct varpool_node *node, tree decl)
229 {
230   /* If the user told us it is used, then it must be so.  */
231   if (node->externally_visible || node->force_output)
232     return true;
233
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)))
239     return true;
240
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.  */
243   if (node->needed)
244     return true;
245
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))
250     return true;
251
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))
257     {
258       tree control = emutls_decl (decl);
259       if (decide_is_variable_needed (varpool_node (control), control))
260         return true;
261     }
262
263   /* When not reordering top level variables, we have to assume that
264      we are going to keep everything.  */
265   if (flag_toplevel_reorder)
266     return false;
267
268   /* We want to emit COMDAT variables only when absolutely necessary.  */
269   if (DECL_COMDAT (decl))
270     return false;
271   return true;
272 }
273
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
276    visible.  */
277 void
278 varpool_finalize_decl (tree decl)
279 {
280   struct varpool_node *node = varpool_node (decl);
281
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.  */
286   if (node->finalized)
287     {
288       if (cgraph_global_info_ready)
289         varpool_assemble_pending_decls ();
290       return;
291     }
292   if (node->needed)
293     varpool_enqueue_needed_node (node);
294   node->finalized = true;
295
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
300      there.  */
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 ();
305 }
306
307 /* Return variable availability.  See cgraph.h for description of individual
308    return values.  */
309 enum availability
310 cgraph_variable_initializer_availability (struct varpool_node *node)
311 {
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;
323 }
324
325 /* Walk the decls we marked as necessary and see if they reference new
326    variables or functions and add them into the worklists.  */
327 bool
328 varpool_analyze_pending_decls (void)
329 {
330   bool changed = false;
331   timevar_push (TV_CGRAPH);
332
333   while (varpool_first_unanalyzed_node)
334     {
335       tree decl = varpool_first_unanalyzed_node->decl;
336
337       varpool_first_unanalyzed_node->analyzed = true;
338
339       varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed;
340
341       /* Compute the alignment early so function body expanders are
342          already informed about increased alignment.  */
343       align_variable (decl, 0);
344
345       if (DECL_INITIAL (decl))
346         record_references_in_initializer (decl);
347       changed = true;
348     }
349   timevar_pop (TV_CGRAPH);
350   return changed;
351 }
352
353 /* Output one variable, if necessary.  Return whether we output it.  */
354 bool
355 varpool_assemble_decl (struct varpool_node *node)
356 {
357   tree decl = node->decl;
358
359   if (!TREE_ASM_WRITTEN (decl)
360       && !node->alias
361       && !DECL_EXTERNAL (decl)
362       && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
363     {
364       assemble_variable (decl, 0, 1, 0);
365       if (TREE_ASM_WRITTEN (decl))
366         {
367           node->next_needed = varpool_assembled_nodes_queue;
368           varpool_assembled_nodes_queue = node;
369           node->finalized = 1;
370           return true;
371         }
372     }
373
374   return false;
375 }
376
377 /* Optimization of function bodies might've rendered some variables as
378    unnecessary so we want to avoid these from being compiled.
379
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.  */
384 void
385 varpool_remove_unreferenced_decls (void)
386 {
387   struct varpool_node *next, *node = varpool_nodes_queue;
388
389   varpool_reset_queue ();
390
391   if (errorcount || sorrycount)
392     return;
393
394   while (node)
395     {
396       tree decl = node->decl;
397       next = node->next_needed;
398       node->needed = 0;
399
400       if (node->finalized
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);
409
410       node = next;
411     }
412   /* Make sure we mark alias targets as used targets.  */
413   finish_aliases_1 ();
414   varpool_analyze_pending_decls ();
415 }
416
417 /* Output all variables enqueued to be assembled.  */
418 bool
419 varpool_assemble_pending_decls (void)
420 {
421   bool changed = false;
422
423   if (errorcount || sorrycount)
424     return false;
425
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
428      elsewhere.  */
429   varpool_analyze_pending_decls ();
430
431   while (varpool_nodes_queue)
432     {
433       struct varpool_node *node = varpool_nodes_queue;
434
435       varpool_nodes_queue = varpool_nodes_queue->next_needed;
436       if (varpool_assemble_decl (node))
437         changed = true;
438       else
439         node->next_needed = NULL;
440     }
441   /* varpool_nodes_queue is now empty, clear the pointer to the last element
442      in the queue.  */
443   varpool_last_needed_node = NULL;
444   return changed;
445 }
446
447 /* Remove all elements from the queue so we can re-use it for debug output.  */
448 void
449 varpool_empty_needed_queue (void)
450 {
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
453      elsewhere.  */
454   varpool_analyze_pending_decls ();
455
456   while (varpool_nodes_queue)
457     {
458       struct varpool_node *node = varpool_nodes_queue;
459       varpool_nodes_queue = varpool_nodes_queue->next_needed;
460       node->next_needed = NULL;
461     }
462   /* varpool_nodes_queue is now empty, clear the pointer to the last element
463      in the queue.  */
464   varpool_last_needed_node = NULL;
465 }
466
467 /* Create a new global variable of type TYPE.  */
468 tree
469 add_new_static_var (tree type)
470 {
471   tree new_decl;
472   struct varpool_node *new_node;
473
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);
487
488   return new_node->decl;
489 }
490
491 #include "gt-varpool.h"