Mercurial > hg > CbC > CbC_gcc
comparison gcc/varpool.c @ 0:a06113de4d67
first commit
author | kent <kent@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2009 14:47:48 +0900 |
parents | |
children | 77e2b8dfacca |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a06113de4d67 |
---|---|
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 /* Given an assembler name, lookup node. */ | |
168 struct varpool_node * | |
169 varpool_node_for_asm (tree asmname) | |
170 { | |
171 struct varpool_node *node; | |
172 | |
173 for (node = varpool_nodes; node ; node = node->next) | |
174 if (decl_assembler_name_equal (node->decl, asmname)) | |
175 return node; | |
176 | |
177 return NULL; | |
178 } | |
179 | |
180 /* Helper function for finalization code - add node into lists so it will | |
181 be analyzed and compiled. */ | |
182 static void | |
183 varpool_enqueue_needed_node (struct varpool_node *node) | |
184 { | |
185 if (varpool_last_needed_node) | |
186 varpool_last_needed_node->next_needed = node; | |
187 varpool_last_needed_node = node; | |
188 node->next_needed = NULL; | |
189 if (!varpool_nodes_queue) | |
190 varpool_nodes_queue = node; | |
191 if (!varpool_first_unanalyzed_node) | |
192 varpool_first_unanalyzed_node = node; | |
193 notice_global_symbol (node->decl); | |
194 } | |
195 | |
196 /* Notify finalize_compilation_unit that given node is reachable | |
197 or needed. */ | |
198 void | |
199 varpool_mark_needed_node (struct varpool_node *node) | |
200 { | |
201 if (!node->needed && node->finalized | |
202 && !TREE_ASM_WRITTEN (node->decl)) | |
203 varpool_enqueue_needed_node (node); | |
204 node->needed = 1; | |
205 } | |
206 | |
207 /* Reset the queue of needed nodes. */ | |
208 static void | |
209 varpool_reset_queue (void) | |
210 { | |
211 varpool_last_needed_node = NULL; | |
212 varpool_nodes_queue = NULL; | |
213 varpool_first_unanalyzed_node = NULL; | |
214 } | |
215 | |
216 /* Determine if variable DECL is needed. That is, visible to something | |
217 either outside this translation unit, something magic in the system | |
218 configury */ | |
219 bool | |
220 decide_is_variable_needed (struct varpool_node *node, tree decl) | |
221 { | |
222 /* If the user told us it is used, then it must be so. */ | |
223 if (node->externally_visible || node->force_output) | |
224 return true; | |
225 | |
226 /* ??? If the assembler name is set by hand, it is possible to assemble | |
227 the name later after finalizing the function and the fact is noticed | |
228 in assemble_name then. This is arguably a bug. */ | |
229 if (DECL_ASSEMBLER_NAME_SET_P (decl) | |
230 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))) | |
231 return true; | |
232 | |
233 /* If we decided it was needed before, but at the time we didn't have | |
234 the definition available, then it's still needed. */ | |
235 if (node->needed) | |
236 return true; | |
237 | |
238 /* Externally visible variables must be output. The exception is | |
239 COMDAT variables that must be output only when they are needed. */ | |
240 if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl) | |
241 && !DECL_EXTERNAL (decl)) | |
242 return true; | |
243 | |
244 /* When emulating tls, we actually see references to the control | |
245 variable, rather than the user-level variable. */ | |
246 if (!targetm.have_tls | |
247 && TREE_CODE (decl) == VAR_DECL | |
248 && DECL_THREAD_LOCAL_P (decl)) | |
249 { | |
250 tree control = emutls_decl (decl); | |
251 if (decide_is_variable_needed (varpool_node (control), control)) | |
252 return true; | |
253 } | |
254 | |
255 /* When not reordering top level variables, we have to assume that | |
256 we are going to keep everything. */ | |
257 if (flag_toplevel_reorder) | |
258 return false; | |
259 | |
260 /* We want to emit COMDAT variables only when absolutely necessary. */ | |
261 if (DECL_COMDAT (decl)) | |
262 return false; | |
263 return true; | |
264 } | |
265 | |
266 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the | |
267 middle end to output the variable to asm file, if needed or externally | |
268 visible. */ | |
269 void | |
270 varpool_finalize_decl (tree decl) | |
271 { | |
272 struct varpool_node *node = varpool_node (decl); | |
273 | |
274 /* The first declaration of a variable that comes through this function | |
275 decides whether it is global (in C, has external linkage) | |
276 or local (in C, has internal linkage). So do nothing more | |
277 if this function has already run. */ | |
278 if (node->finalized) | |
279 { | |
280 if (cgraph_global_info_ready) | |
281 varpool_assemble_pending_decls (); | |
282 return; | |
283 } | |
284 if (node->needed) | |
285 varpool_enqueue_needed_node (node); | |
286 node->finalized = true; | |
287 | |
288 if (decide_is_variable_needed (node, decl)) | |
289 varpool_mark_needed_node (node); | |
290 /* Since we reclaim unreachable nodes at the end of every language | |
291 level unit, we need to be conservative about possible entry points | |
292 there. */ | |
293 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) | |
294 varpool_mark_needed_node (node); | |
295 if (cgraph_global_info_ready) | |
296 varpool_assemble_pending_decls (); | |
297 } | |
298 | |
299 /* Return variable availability. See cgraph.h for description of individual | |
300 return values. */ | |
301 enum availability | |
302 cgraph_variable_initializer_availability (struct varpool_node *node) | |
303 { | |
304 gcc_assert (cgraph_function_flags_ready); | |
305 if (!node->finalized) | |
306 return AVAIL_NOT_AVAILABLE; | |
307 if (!TREE_PUBLIC (node->decl)) | |
308 return AVAIL_AVAILABLE; | |
309 /* If the variable can be overwritten, return OVERWRITABLE. Takes | |
310 care of at least two notable extensions - the COMDAT variables | |
311 used to share template instantiations in C++. */ | |
312 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl)) | |
313 return AVAIL_OVERWRITABLE; | |
314 return AVAIL_AVAILABLE; | |
315 } | |
316 | |
317 /* Walk the decls we marked as necessary and see if they reference new | |
318 variables or functions and add them into the worklists. */ | |
319 bool | |
320 varpool_analyze_pending_decls (void) | |
321 { | |
322 bool changed = false; | |
323 timevar_push (TV_CGRAPH); | |
324 | |
325 while (varpool_first_unanalyzed_node) | |
326 { | |
327 tree decl = varpool_first_unanalyzed_node->decl; | |
328 | |
329 varpool_first_unanalyzed_node->analyzed = true; | |
330 | |
331 varpool_first_unanalyzed_node = varpool_first_unanalyzed_node->next_needed; | |
332 | |
333 /* Compute the alignment early so function body expanders are | |
334 already informed about increased alignment. */ | |
335 align_variable (decl, 0); | |
336 | |
337 if (DECL_INITIAL (decl)) | |
338 record_references_in_initializer (decl); | |
339 changed = true; | |
340 } | |
341 timevar_pop (TV_CGRAPH); | |
342 return changed; | |
343 } | |
344 | |
345 /* Output one variable, if necessary. Return whether we output it. */ | |
346 bool | |
347 varpool_assemble_decl (struct varpool_node *node) | |
348 { | |
349 tree decl = node->decl; | |
350 | |
351 if (!TREE_ASM_WRITTEN (decl) | |
352 && !node->alias | |
353 && !DECL_EXTERNAL (decl) | |
354 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))) | |
355 { | |
356 assemble_variable (decl, 0, 1, 0); | |
357 if (TREE_ASM_WRITTEN (decl)) | |
358 { | |
359 node->next_needed = varpool_assembled_nodes_queue; | |
360 varpool_assembled_nodes_queue = node; | |
361 node->finalized = 1; | |
362 return true; | |
363 } | |
364 } | |
365 | |
366 return false; | |
367 } | |
368 | |
369 /* Optimization of function bodies might've rendered some variables as | |
370 unnecessary so we want to avoid these from being compiled. | |
371 | |
372 This is done by pruning the queue and keeping only the variables that | |
373 really appear needed (ie they are either externally visible or referenced | |
374 by compiled function). Re-doing the reachability analysis on variables | |
375 brings back the remaining variables referenced by these. */ | |
376 void | |
377 varpool_remove_unreferenced_decls (void) | |
378 { | |
379 struct varpool_node *next, *node = varpool_nodes_queue; | |
380 | |
381 varpool_reset_queue (); | |
382 | |
383 if (errorcount || sorrycount) | |
384 return; | |
385 | |
386 while (node) | |
387 { | |
388 tree decl = node->decl; | |
389 next = node->next_needed; | |
390 node->needed = 0; | |
391 | |
392 if (node->finalized | |
393 && (decide_is_variable_needed (node, decl) | |
394 /* ??? Cgraph does not yet rule the world with an iron hand, | |
395 and does not control the emission of debug information. | |
396 After a variable has its DECL_RTL set, we must assume that | |
397 it may be referenced by the debug information, and we can | |
398 no longer elide it. */ | |
399 || DECL_RTL_SET_P (decl))) | |
400 varpool_mark_needed_node (node); | |
401 | |
402 node = next; | |
403 } | |
404 /* Make sure we mark alias targets as used targets. */ | |
405 finish_aliases_1 (); | |
406 varpool_analyze_pending_decls (); | |
407 } | |
408 | |
409 /* Output all variables enqueued to be assembled. */ | |
410 bool | |
411 varpool_assemble_pending_decls (void) | |
412 { | |
413 bool changed = false; | |
414 | |
415 if (errorcount || sorrycount) | |
416 return false; | |
417 | |
418 /* EH might mark decls as needed during expansion. This should be safe since | |
419 we don't create references to new function, but it should not be used | |
420 elsewhere. */ | |
421 varpool_analyze_pending_decls (); | |
422 | |
423 while (varpool_nodes_queue) | |
424 { | |
425 struct varpool_node *node = varpool_nodes_queue; | |
426 | |
427 varpool_nodes_queue = varpool_nodes_queue->next_needed; | |
428 if (varpool_assemble_decl (node)) | |
429 changed = true; | |
430 else | |
431 node->next_needed = NULL; | |
432 } | |
433 /* varpool_nodes_queue is now empty, clear the pointer to the last element | |
434 in the queue. */ | |
435 varpool_last_needed_node = NULL; | |
436 return changed; | |
437 } | |
438 | |
439 /* Remove all elements from the queue so we can re-use it for debug output. */ | |
440 void | |
441 varpool_empty_needed_queue (void) | |
442 { | |
443 /* EH might mark decls as needed during expansion. This should be safe since | |
444 we don't create references to new function, but it should not be used | |
445 elsewhere. */ | |
446 varpool_analyze_pending_decls (); | |
447 | |
448 while (varpool_nodes_queue) | |
449 { | |
450 struct varpool_node *node = varpool_nodes_queue; | |
451 varpool_nodes_queue = varpool_nodes_queue->next_needed; | |
452 node->next_needed = NULL; | |
453 } | |
454 /* varpool_nodes_queue is now empty, clear the pointer to the last element | |
455 in the queue. */ | |
456 varpool_last_needed_node = NULL; | |
457 } | |
458 | |
459 /* Create a new global variable of type TYPE. */ | |
460 tree | |
461 add_new_static_var (tree type) | |
462 { | |
463 tree new_decl; | |
464 struct varpool_node *new_node; | |
465 | |
466 new_decl = create_tmp_var (type, NULL); | |
467 DECL_NAME (new_decl) = create_tmp_var_name (NULL); | |
468 TREE_READONLY (new_decl) = 0; | |
469 TREE_STATIC (new_decl) = 1; | |
470 TREE_USED (new_decl) = 1; | |
471 DECL_CONTEXT (new_decl) = NULL_TREE; | |
472 DECL_ABSTRACT (new_decl) = 0; | |
473 lang_hooks.dup_lang_specific_decl (new_decl); | |
474 create_var_ann (new_decl); | |
475 new_node = varpool_node (new_decl); | |
476 varpool_mark_needed_node (new_node); | |
477 add_referenced_var (new_decl); | |
478 varpool_finalize_decl (new_decl); | |
479 | |
480 return new_node->decl; | |
481 } | |
482 | |
483 #include "gt-varpool.h" |