OSDN Git Service

* gcc.doxy: Rename from tree-ssa.doxy.
[pf3gnuchains/gcc-fork.git] / gcc / varpool.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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 3, or (at your option) any later
10 version.
11
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
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "cgraph.h"
27 #include "langhooks.h"
28 #include "diagnostic.h"
29 #include "hashtab.h"
30 #include "ggc.h"
31 #include "timevar.h"
32 #include "debug.h" 
33 #include "target.h"
34 #include "output.h"
35 #include "tree-gimple.h"
36 #include "tree-flow.h"
37
38 /*  This file contains basic routines manipulating variable pool.
39
40     Varpool acts as interface in between the front-end and middle-end
41     and drives the decision process on what variables and when are
42     going to be compiled.
43
44     The varpool nodes are allocated lazily for declarations
45     either by frontend or at callgraph construction time.
46     All variables supposed to be output into final file needs to be
47     explicitly marked by frontend via VARPOOL_FINALIZE_DECL function.  */
48
49 /* Hash table used to convert declarations into nodes.  */
50 static GTY((param_is (struct varpool_node))) htab_t varpool_hash;
51
52 /* The linked list of cgraph varpool nodes.
53    Linked via node->next pointer.  */
54 struct varpool_node *varpool_nodes;
55
56 /* Queue of cgraph nodes scheduled to be lowered and output.
57    The queue is maintained via mark_needed_node, linked via node->next_needed
58    pointer. 
59
60    LAST_NNEDED_NODE points to the end of queue, so it can be maintained in forward
61    order.  QTY is needed to make it friendly to PCH.
62  
63    During unit-at-a-time compilation we construct the queue of needed variables
64    twice: first time it is during cgraph construction, second time it is at the
65    end of compilation in VARPOOL_REMOVE_UNREFERENCED_DECLS so we can avoid
66    optimized out variables being output.
67    
68    Each variable is thus first analyzed and then later possibly output.  
69    FIRST_UNANALYZED_NODE points to first node in queue that was not analyzed
70    yet and is moved via VARPOOL_ANALYZE_PENDING_DECLS.  */
71    
72 struct varpool_node *varpool_nodes_queue;
73 static GTY(()) struct varpool_node *varpool_last_needed_node;
74 static GTY(()) struct varpool_node *varpool_first_unanalyzed_node;
75
76 /* Lists all assembled variables to be sent to debugger output later on.  */
77 static GTY(()) struct varpool_node *varpool_assembled_nodes_queue;
78
79 /* Return name of the node used in debug output.  */
80 static const char *
81 varpool_node_name (struct varpool_node *node)
82 {
83   return lang_hooks.decl_printable_name (node->decl, 2);
84 }
85
86 /* Returns a hash code for P.  */
87 static hashval_t
88 hash_varpool_node (const void *p)
89 {
90   const struct varpool_node *n = (const struct varpool_node *) p;
91   return (hashval_t) DECL_UID (n->decl);
92 }
93
94 /* Returns nonzero if P1 and P2 are equal.  */
95 static int
96 eq_varpool_node (const void *p1, const void *p2)
97 {
98   const struct varpool_node *n1 =
99     (const struct varpool_node *) p1;
100   const struct varpool_node *n2 =
101     (const struct varpool_node *) p2;
102   return DECL_UID (n1->decl) == DECL_UID (n2->decl);
103 }
104
105 /* Return varpool node assigned to DECL.  Create new one when needed.  */
106 struct varpool_node *
107 varpool_node (tree decl)
108 {
109   struct varpool_node key, *node, **slot;
110
111   gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
112
113   if (!varpool_hash)
114     varpool_hash = htab_create_ggc (10, hash_varpool_node,
115                                            eq_varpool_node, NULL);
116   key.decl = decl;
117   slot = (struct varpool_node **)
118     htab_find_slot (varpool_hash, &key, INSERT);
119   if (*slot)
120     return *slot;
121   node = GGC_CNEW (struct varpool_node);
122   node->decl = decl;
123   node->order = cgraph_order++;
124   node->next = varpool_nodes;
125   varpool_nodes = node;
126   *slot = node;
127   return node;
128 }
129
130 /* Dump given cgraph node.  */
131 void
132 dump_varpool_node (FILE *f, struct varpool_node *node)
133 {
134   fprintf (f, "%s:", varpool_node_name (node));
135   fprintf (f, " availability:%s",
136            cgraph_function_flags_ready
137            ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
138            : "not-ready");
139   if (DECL_INITIAL (node->decl))
140     fprintf (f, " initialized");
141   if (node->needed)
142     fprintf (f, " needed");
143   if (node->analyzed)
144     fprintf (f, " analyzed");
145   if (node->finalized)
146     fprintf (f, " finalized");
147   if (node->output)
148     fprintf (f, " output");
149   if (node->externally_visible)
150     fprintf (f, " externally_visible");
151   fprintf (f, "\n");
152 }
153
154 /* Dump the variable pool.  */
155 void
156 dump_varpool (FILE *f)
157 {
158   struct varpool_node *node;
159
160   fprintf (f, "variable pool:\n\n");
161   for (node = varpool_nodes; node; node = node->next_needed)
162     dump_varpool_node (f, node);
163 }
164
165 /* Given an assembler name, lookup node.  */
166 struct varpool_node *
167 varpool_node_for_asm (tree asmname)
168 {
169   struct varpool_node *node;
170
171   for (node = varpool_nodes; node ; node = node->next)
172     if (decl_assembler_name_equal (node->decl, asmname))
173       return node;
174
175   return NULL;
176 }
177
178 /* Helper function for finalization code - add node into lists so it will
179    be analyzed and compiled.  */
180 static void
181 varpool_enqueue_needed_node (struct varpool_node *node)
182 {
183   if (varpool_last_needed_node)
184     varpool_last_needed_node->next_needed = node;
185   varpool_last_needed_node = node;
186   node->next_needed = NULL;
187   if (!varpool_nodes_queue)
188     varpool_nodes_queue = node;
189   if (!varpool_first_unanalyzed_node)
190     varpool_first_unanalyzed_node = node;
191   notice_global_symbol (node->decl);
192 }
193
194 /* Notify finalize_compilation_unit that given node is reachable
195    or needed.  */
196 void
197 varpool_mark_needed_node (struct varpool_node *node)
198 {
199   if (!node->needed && node->finalized
200       && !TREE_ASM_WRITTEN (node->decl))
201     varpool_enqueue_needed_node (node);
202   node->needed = 1;
203 }
204
205 /* Reset the queue of needed nodes.  */
206 static void
207 varpool_reset_queue (void)
208 {
209   varpool_last_needed_node = NULL;
210   varpool_nodes_queue = NULL;
211   varpool_first_unanalyzed_node = NULL;
212 }
213
214 /* Determine if variable DECL is needed.  That is, visible to something
215    either outside this translation unit, something magic in the system
216    configury, or (if not doing unit-at-a-time) to something we haven't
217    seen yet.  */
218 bool
219 decide_is_variable_needed (struct varpool_node *node, tree decl)
220 {
221   /* If the user told us it is used, then it must be so.  */
222   if (node->externally_visible || node->force_output)
223     return true;
224   if (!flag_unit_at_a_time
225       && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
226     return true;
227
228   /* ??? If the assembler name is set by hand, it is possible to assemble
229      the name later after finalizing the function and the fact is noticed
230      in assemble_name then.  This is arguably a bug.  */
231   if (DECL_ASSEMBLER_NAME_SET_P (decl)
232       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
233     return true;
234
235   /* If we decided it was needed before, but at the time we didn't have
236      the definition available, then it's still needed.  */
237   if (node->needed)
238     return true;
239
240   /* Externally visible variables must be output.  The exception is
241      COMDAT variables that must be output only when they are needed.  */
242   if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl)
243       && !DECL_EXTERNAL (decl))
244     return true;
245
246   /* When emulating tls, we actually see references to the control
247      variable, rather than the user-level variable.  */
248   if (!targetm.have_tls
249       && TREE_CODE (decl) == VAR_DECL
250       && DECL_THREAD_LOCAL_P (decl))
251     {
252       tree control = emutls_decl (decl);
253       if (decide_is_variable_needed (varpool_node (control), control))
254         return true;
255     }
256
257   /* When not reordering top level variables, we have to assume that
258      we are going to keep everything.  */
259   if (flag_unit_at_a_time && flag_toplevel_reorder)
260     return false;
261
262   /* We want to emit COMDAT variables only when absolutely necessary.  */
263   if (DECL_COMDAT (decl))
264     return false;
265   return true;
266 }
267
268 /* Mark DECL as finalized.  By finalizing the declaration, frontend instruct the
269    middle end to output the variable to asm file, if needed or externally
270    visible.  */
271 void
272 varpool_finalize_decl (tree decl)
273 {
274   struct varpool_node *node = varpool_node (decl);
275
276   /* The first declaration of a variable that comes through this function
277      decides whether it is global (in C, has external linkage)
278      or local (in C, has internal linkage).  So do nothing more
279      if this function has already run.  */
280   if (node->finalized)
281     {
282       if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
283         varpool_assemble_pending_decls ();
284       return;
285     }
286   if (node->needed)
287     varpool_enqueue_needed_node (node);
288   node->finalized = true;
289
290   if (decide_is_variable_needed (node, decl))
291     varpool_mark_needed_node (node);
292   /* Since we reclaim unreachable nodes at the end of every language
293      level unit, we need to be conservative about possible entry points
294      there.  */
295   else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
296     varpool_mark_needed_node (node);
297   if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
298     varpool_assemble_pending_decls ();
299 }
300
301 /* Return variable availability.  See cgraph.h for description of individual
302    return values.  */
303 enum availability
304 cgraph_variable_initializer_availability (struct varpool_node *node)
305 {
306   gcc_assert (cgraph_function_flags_ready);
307   if (!node->finalized)
308     return AVAIL_NOT_AVAILABLE;
309   if (!TREE_PUBLIC (node->decl))
310     return AVAIL_AVAILABLE;
311   /* If the variable can be overwritten, return OVERWRITABLE.  Takes
312      care of at least two notable extensions - the COMDAT variables
313      used to share template instantiations in C++.  */
314   if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
315     return AVAIL_OVERWRITABLE;
316   return AVAIL_AVAILABLE;
317 }
318
319 /* Walk the decls we marked as necessary and see if they reference new
320    variables or functions and add them into the worklists.  */
321 bool
322 varpool_analyze_pending_decls (void)
323 {
324   bool changed = false;
325   timevar_push (TV_CGRAPH);
326
327   while (varpool_first_unanalyzed_node)
328     {
329       tree decl = varpool_first_unanalyzed_node->decl;
330
331       varpool_first_unanalyzed_node->analyzed = true;
332
333       varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed;
334
335       /* Compute the alignment early so function body expanders are
336          already informed about increased alignment.  */
337       align_variable (decl, 0);
338
339       if (DECL_INITIAL (decl))
340         record_references_in_initializer (decl);
341       changed = true;
342     }
343   timevar_pop (TV_CGRAPH);
344   return changed;
345 }
346
347 /* Output one variable, if necessary.  Return whether we output it.  */
348 bool
349 varpool_assemble_decl (struct varpool_node *node)
350 {
351   tree decl = node->decl;
352
353   if (!TREE_ASM_WRITTEN (decl)
354       && !node->alias
355       && !DECL_EXTERNAL (decl)
356       && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl)))
357     {
358       assemble_variable (decl, 0, 1, 0);
359       return TREE_ASM_WRITTEN (decl);
360     }
361
362   return false;
363 }
364
365 /* Optimization of function bodies might've rendered some variables as
366    unnecessary so we want to avoid these from being compiled.
367
368    This is done by pruning the queue and keeping only the variables that
369    really appear needed (ie they are either externally visible or referenced
370    by compiled function). Re-doing the reachability analysis on variables
371    brings back the remaining variables referenced by these.  */
372 void
373 varpool_remove_unreferenced_decls (void)
374 {
375   struct varpool_node *next, *node = varpool_nodes_queue;
376
377   varpool_reset_queue ();
378
379   if (errorcount || sorrycount)
380     return;
381
382   while (node)
383     {
384       tree decl = node->decl;
385       next = node->next_needed;
386       node->needed = 0;
387
388       if (node->finalized
389           && (decide_is_variable_needed (node, decl)
390               /* ??? Cgraph does not yet rule the world with an iron hand,
391                  and does not control the emission of debug information.
392                  After a variable has its DECL_RTL set, we must assume that
393                  it may be referenced by the debug information, and we can
394                  no longer elide it.  */
395               || DECL_RTL_SET_P (decl)))
396         varpool_mark_needed_node (node);
397
398       node = next;
399     }
400   /* Make sure we mark alias targets as used targets.  */
401   finish_aliases_1 ();
402   varpool_analyze_pending_decls ();
403 }
404
405 /* Output all variables enqueued to be assembled.  */
406 bool
407 varpool_assemble_pending_decls (void)
408 {
409   bool changed = false;
410
411   if (errorcount || sorrycount)
412     return false;
413
414   /* EH might mark decls as needed during expansion.  This should be safe since
415      we don't create references to new function, but it should not be used
416      elsewhere.  */
417   varpool_analyze_pending_decls ();
418
419   while (varpool_nodes_queue)
420     {
421       struct varpool_node *node = varpool_nodes_queue;
422
423       varpool_nodes_queue = varpool_nodes_queue->next_needed;
424       if (varpool_assemble_decl (node))
425         {
426           changed = true;
427           node->next_needed = varpool_assembled_nodes_queue;
428           varpool_assembled_nodes_queue = node;
429           node->finalized = 1;
430         }
431       else
432         node->next_needed = NULL;
433     }
434   /* varpool_nodes_queue is now empty, clear the pointer to the last element
435      in the queue.  */
436   varpool_last_needed_node = NULL;
437   return changed;
438 }
439
440 /* Output all variables enqueued to be assembled.  */
441 void
442 varpool_output_debug_info (void)
443 {
444   timevar_push (TV_SYMOUT);
445   if (errorcount == 0 && sorrycount == 0)
446     while (varpool_assembled_nodes_queue)
447       {
448         struct varpool_node *node = varpool_assembled_nodes_queue;
449
450         /* Local static variables are never seen by check_global_declarations
451            so we need to output debug info by hand.  */
452         if (DECL_CONTEXT (node->decl)
453             && (TREE_CODE (DECL_CONTEXT (node->decl)) == BLOCK
454                 || TREE_CODE (DECL_CONTEXT (node->decl)) == FUNCTION_DECL)
455             && errorcount == 0 && sorrycount == 0)
456              (*debug_hooks->global_decl) (node->decl);
457         varpool_assembled_nodes_queue = node->next_needed;
458         node->next_needed = 0;
459       }
460   timevar_pop (TV_SYMOUT);
461 }
462
463 /* Create a new global variable of type TYPE.  */
464 tree
465 add_new_static_var (tree type)
466 {
467   tree new_decl;
468   struct varpool_node *new_node;
469
470   new_decl = create_tmp_var (type, NULL);
471   DECL_NAME (new_decl) = create_tmp_var_name (NULL);
472   TREE_READONLY (new_decl) = 0;
473   TREE_STATIC (new_decl) = 1;
474   TREE_USED (new_decl) = 1;
475   DECL_CONTEXT (new_decl) = NULL_TREE;
476   DECL_ABSTRACT (new_decl) = 0;
477   lang_hooks.dup_lang_specific_decl (new_decl);
478   create_var_ann (new_decl);
479   new_node = varpool_node (new_decl);
480   varpool_mark_needed_node (new_node);
481   add_referenced_var (new_decl);
482   varpool_finalize_decl (new_decl);
483
484   return new_node->decl;
485 }
486
487 #include "gt-varpool.h"