Mercurial > hg > CbC > CbC_gcc
comparison gcc/varpool.c @ 67:f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
author | nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 22 Mar 2011 17:18:12 +0900 |
parents | b7f97abdc517 |
children | 04ced10e8804 |
comparison
equal
deleted
inserted
replaced
65:65488c3d617d | 67:f6334be47118 |
---|---|
1 /* Callgraph handling code. | 1 /* Callgraph handling code. |
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010 | 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011 |
3 Free Software Foundation, Inc. | 3 Free Software Foundation, Inc. |
4 Contributed by Jan Hubicka | 4 Contributed by Jan Hubicka |
5 | 5 |
6 This file is part of GCC. | 6 This file is part of GCC. |
7 | 7 |
24 #include "coretypes.h" | 24 #include "coretypes.h" |
25 #include "tm.h" | 25 #include "tm.h" |
26 #include "tree.h" | 26 #include "tree.h" |
27 #include "cgraph.h" | 27 #include "cgraph.h" |
28 #include "langhooks.h" | 28 #include "langhooks.h" |
29 #include "diagnostic.h" | 29 #include "diagnostic-core.h" |
30 #include "hashtab.h" | 30 #include "hashtab.h" |
31 #include "ggc.h" | 31 #include "ggc.h" |
32 #include "timevar.h" | 32 #include "timevar.h" |
33 #include "debug.h" | 33 #include "debug.h" |
34 #include "target.h" | 34 #include "target.h" |
105 return DECL_UID (n1->decl) == DECL_UID (n2->decl); | 105 return DECL_UID (n1->decl) == DECL_UID (n2->decl); |
106 } | 106 } |
107 | 107 |
108 /* Return varpool node assigned to DECL without creating new one. */ | 108 /* Return varpool node assigned to DECL without creating new one. */ |
109 struct varpool_node * | 109 struct varpool_node * |
110 varpool_get_node (tree decl) | 110 varpool_get_node (const_tree decl) |
111 { | 111 { |
112 struct varpool_node key, **slot; | 112 struct varpool_node key, **slot; |
113 | 113 |
114 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL); | 114 gcc_assert (TREE_CODE (decl) == VAR_DECL |
115 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); | |
115 | 116 |
116 if (!varpool_hash) | 117 if (!varpool_hash) |
117 return NULL; | 118 return NULL; |
118 key.decl = decl; | 119 key.decl = CONST_CAST2 (tree, const_tree, decl); |
119 slot = (struct varpool_node **) | 120 slot = (struct varpool_node **) |
120 htab_find_slot (varpool_hash, &key, INSERT); | 121 htab_find_slot (varpool_hash, &key, NO_INSERT); |
122 if (!slot) | |
123 return NULL; | |
121 return *slot; | 124 return *slot; |
122 } | 125 } |
123 | 126 |
124 /* Return varpool node assigned to DECL. Create new one when needed. */ | 127 /* Return varpool node assigned to DECL. Create new one when needed. */ |
125 struct varpool_node * | 128 struct varpool_node * |
126 varpool_node (tree decl) | 129 varpool_node (tree decl) |
127 { | 130 { |
128 struct varpool_node key, *node, **slot; | 131 struct varpool_node key, *node, **slot; |
129 | 132 |
130 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL); | 133 gcc_assert (TREE_CODE (decl) == VAR_DECL |
134 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); | |
131 | 135 |
132 if (!varpool_hash) | 136 if (!varpool_hash) |
133 varpool_hash = htab_create_ggc (10, hash_varpool_node, | 137 varpool_hash = htab_create_ggc (10, hash_varpool_node, |
134 eq_varpool_node, NULL); | 138 eq_varpool_node, NULL); |
135 key.decl = decl; | 139 key.decl = decl; |
136 slot = (struct varpool_node **) | 140 slot = (struct varpool_node **) |
137 htab_find_slot (varpool_hash, &key, INSERT); | 141 htab_find_slot (varpool_hash, &key, INSERT); |
138 if (*slot) | 142 if (*slot) |
139 return *slot; | 143 return *slot; |
140 node = GGC_CNEW (struct varpool_node); | 144 node = ggc_alloc_cleared_varpool_node (); |
141 node->decl = decl; | 145 node->decl = decl; |
142 node->order = cgraph_order++; | 146 node->order = cgraph_order++; |
143 node->next = varpool_nodes; | 147 node->next = varpool_nodes; |
144 ipa_empty_ref_list (&node->ref_list); | 148 ipa_empty_ref_list (&node->ref_list); |
145 if (varpool_nodes) | 149 if (varpool_nodes) |
235 fprintf (f, " finalized"); | 239 fprintf (f, " finalized"); |
236 if (node->output) | 240 if (node->output) |
237 fprintf (f, " output"); | 241 fprintf (f, " output"); |
238 if (node->externally_visible) | 242 if (node->externally_visible) |
239 fprintf (f, " externally_visible"); | 243 fprintf (f, " externally_visible"); |
244 if (node->resolution != LDPR_UNKNOWN) | |
245 fprintf (f, " %s", | |
246 ld_plugin_symbol_resolution_names[(int)node->resolution]); | |
240 if (node->in_other_partition) | 247 if (node->in_other_partition) |
241 fprintf (f, " in_other_partition"); | 248 fprintf (f, " in_other_partition"); |
242 else if (node->used_from_other_partition) | 249 else if (node->used_from_other_partition) |
243 fprintf (f, " used_from_other_partition"); | 250 fprintf (f, " used_from_other_partition"); |
244 fprintf (f, "\n"); | 251 fprintf (f, "\n"); |
259 dump_varpool_node (f, node); | 266 dump_varpool_node (f, node); |
260 } | 267 } |
261 | 268 |
262 /* Dump the variable pool to stderr. */ | 269 /* Dump the variable pool to stderr. */ |
263 | 270 |
264 void | 271 DEBUG_FUNCTION void |
265 debug_varpool (void) | 272 debug_varpool (void) |
266 { | 273 { |
267 dump_varpool (stderr); | 274 dump_varpool (stderr); |
268 } | 275 } |
269 | 276 |
325 either outside this translation unit, something magic in the system | 332 either outside this translation unit, something magic in the system |
326 configury */ | 333 configury */ |
327 bool | 334 bool |
328 decide_is_variable_needed (struct varpool_node *node, tree decl) | 335 decide_is_variable_needed (struct varpool_node *node, tree decl) |
329 { | 336 { |
330 if (node->used_from_other_partition) | 337 /* If the user told us it is used, then it must be so. */ |
338 if (node->force_output) | |
331 return true; | 339 return true; |
332 /* If the user told us it is used, then it must be so. */ | 340 |
333 if ((node->externally_visible && !DECL_COMDAT (decl)) | 341 gcc_assert (!DECL_EXTERNAL (decl)); |
334 || node->force_output) | |
335 return true; | |
336 | 342 |
337 /* Externally visible variables must be output. The exception is | 343 /* Externally visible variables must be output. The exception is |
338 COMDAT variables that must be output only when they are needed. */ | 344 COMDAT variables that must be output only when they are needed. */ |
339 if (TREE_PUBLIC (decl) | 345 if (TREE_PUBLIC (decl) |
340 && !flag_whole_program | |
341 && !flag_lto | |
342 && !flag_whopr | |
343 && !DECL_COMDAT (decl) | 346 && !DECL_COMDAT (decl) |
344 && !DECL_EXTERNAL (decl)) | 347 && !DECL_EXTERNAL (decl)) |
345 return true; | 348 return true; |
346 | 349 |
347 /* When emulating tls, we actually see references to the control | |
348 variable, rather than the user-level variable. */ | |
349 if (!targetm.have_tls | |
350 && TREE_CODE (decl) == VAR_DECL | |
351 && DECL_THREAD_LOCAL_P (decl)) | |
352 { | |
353 tree control = emutls_decl (decl); | |
354 if (decide_is_variable_needed (varpool_node (control), control)) | |
355 return true; | |
356 } | |
357 | |
358 /* When not reordering top level variables, we have to assume that | 350 /* When not reordering top level variables, we have to assume that |
359 we are going to keep everything. */ | 351 we are going to keep everything. */ |
360 if (flag_toplevel_reorder) | 352 if (!flag_toplevel_reorder) |
353 return true; | |
354 return false; | |
355 } | |
356 | |
357 /* Return if DECL is constant and its initial value is known (so we can do | |
358 constant folding using DECL_INITIAL (decl)). */ | |
359 | |
360 bool | |
361 const_value_known_p (tree decl) | |
362 { | |
363 if (TREE_CODE (decl) != VAR_DECL | |
364 &&TREE_CODE (decl) != CONST_DECL) | |
361 return false; | 365 return false; |
362 | 366 |
363 /* We want to emit COMDAT variables only when absolutely necessary. */ | 367 if (TREE_CODE (decl) == CONST_DECL |
364 if (DECL_COMDAT (decl)) | 368 || DECL_IN_CONSTANT_POOL (decl)) |
369 return true; | |
370 | |
371 gcc_assert (TREE_CODE (decl) == VAR_DECL); | |
372 | |
373 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl)) | |
365 return false; | 374 return false; |
375 | |
376 /* Gimplifier takes away constructors of local vars */ | |
377 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) | |
378 return DECL_INITIAL (decl) != NULL; | |
379 | |
380 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl)); | |
381 | |
382 /* Variables declared 'const' without an initializer | |
383 have zero as the initializer if they may not be | |
384 overridden at link or run time. */ | |
385 if (!DECL_INITIAL (decl) | |
386 && (DECL_EXTERNAL (decl) | |
387 || decl_replaceable_p (decl))) | |
388 return false; | |
389 | |
390 /* Variables declared `const' with an initializer are considered | |
391 to not be overwritable with different initializer by default. | |
392 | |
393 ??? Previously we behaved so for scalar variables but not for array | |
394 accesses. */ | |
366 return true; | 395 return true; |
367 } | 396 } |
368 | 397 |
369 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the | 398 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the |
370 middle end to output the variable to asm file, if needed or externally | 399 middle end to output the variable to asm file, if needed or externally |
371 visible. */ | 400 visible. */ |
372 void | 401 void |
373 varpool_finalize_decl (tree decl) | 402 varpool_finalize_decl (tree decl) |
374 { | 403 { |
375 struct varpool_node *node = varpool_node (decl); | 404 struct varpool_node *node = varpool_node (decl); |
405 | |
406 gcc_assert (TREE_STATIC (decl)); | |
376 | 407 |
377 /* The first declaration of a variable that comes through this function | 408 /* The first declaration of a variable that comes through this function |
378 decides whether it is global (in C, has external linkage) | 409 decides whether it is global (in C, has external linkage) |
379 or local (in C, has internal linkage). So do nothing more | 410 or local (in C, has internal linkage). So do nothing more |
380 if this function has already run. */ | 411 if this function has already run. */ |
389 node->finalized = true; | 420 node->finalized = true; |
390 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)) | 421 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)) |
391 node->force_output = true; | 422 node->force_output = true; |
392 | 423 |
393 if (decide_is_variable_needed (node, decl)) | 424 if (decide_is_variable_needed (node, decl)) |
394 varpool_mark_needed_node (node); | |
395 /* Since we reclaim unreachable nodes at the end of every language | |
396 level unit, we need to be conservative about possible entry points | |
397 there. */ | |
398 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) | |
399 varpool_mark_needed_node (node); | 425 varpool_mark_needed_node (node); |
400 if (cgraph_global_info_ready) | 426 if (cgraph_global_info_ready) |
401 varpool_assemble_pending_decls (); | 427 varpool_assemble_pending_decls (); |
402 } | 428 } |
403 | 429 |
412 if (!TREE_PUBLIC (node->decl)) | 438 if (!TREE_PUBLIC (node->decl)) |
413 return AVAIL_AVAILABLE; | 439 return AVAIL_AVAILABLE; |
414 /* If the variable can be overwritten, return OVERWRITABLE. Takes | 440 /* If the variable can be overwritten, return OVERWRITABLE. Takes |
415 care of at least two notable extensions - the COMDAT variables | 441 care of at least two notable extensions - the COMDAT variables |
416 used to share template instantiations in C++. */ | 442 used to share template instantiations in C++. */ |
417 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl)) | 443 if (!decl_replaceable_p (node->decl)) |
418 return AVAIL_OVERWRITABLE; | 444 return AVAIL_OVERWRITABLE; |
419 return AVAIL_AVAILABLE; | 445 return AVAIL_AVAILABLE; |
420 } | 446 } |
421 | 447 |
422 /* Walk the decls we marked as necessary and see if they reference new | 448 /* Walk the decls we marked as necessary and see if they reference new |
515 { | 541 { |
516 struct varpool_node *next, *node = varpool_nodes_queue; | 542 struct varpool_node *next, *node = varpool_nodes_queue; |
517 | 543 |
518 varpool_reset_queue (); | 544 varpool_reset_queue (); |
519 | 545 |
520 if (errorcount || sorrycount) | 546 if (seen_error ()) |
521 return; | 547 return; |
522 | 548 |
523 while (node) | 549 while (node) |
524 { | 550 { |
525 tree decl = node->decl; | |
526 next = node->next_needed; | 551 next = node->next_needed; |
527 node->needed = 0; | 552 node->needed = 0; |
528 | 553 |
529 if (node->finalized | 554 if (node->analyzed |
530 && (decide_is_variable_needed (node, decl) | 555 && (!varpool_can_remove_if_no_refs (node) |
531 /* ??? Cgraph does not yet rule the world with an iron hand, | 556 /* We just expanded all function bodies. See if any of |
532 and does not control the emission of debug information. | 557 them needed the variable. */ |
533 After a variable has its DECL_RTL set, we must assume that | 558 || DECL_RTL_SET_P (node->decl))) |
534 it may be referenced by the debug information, and we can | |
535 no longer elide it. */ | |
536 || DECL_RTL_SET_P (decl))) | |
537 varpool_mark_needed_node (node); | 559 varpool_mark_needed_node (node); |
538 | 560 |
539 node = next; | 561 node = next; |
540 } | 562 } |
541 /* Make sure we mark alias targets as used targets. */ | 563 /* Make sure we mark alias targets as used targets. */ |
542 finish_aliases_1 (); | 564 finish_aliases_1 (); |
543 varpool_analyze_pending_decls (); | 565 varpool_analyze_pending_decls (); |
544 } | 566 } |
545 | 567 |
568 /* For variables in named sections make sure get_variable_section | |
569 is called before we switch to those sections. Then section | |
570 conflicts between read-only and read-only requiring relocations | |
571 sections can be resolved. */ | |
572 void | |
573 varpool_finalize_named_section_flags (struct varpool_node *node) | |
574 { | |
575 if (!TREE_ASM_WRITTEN (node->decl) | |
576 && !node->alias | |
577 && !node->in_other_partition | |
578 && !DECL_EXTERNAL (node->decl) | |
579 && TREE_CODE (node->decl) == VAR_DECL | |
580 && !DECL_HAS_VALUE_EXPR_P (node->decl) | |
581 && DECL_SECTION_NAME (node->decl)) | |
582 get_variable_section (node->decl, false); | |
583 } | |
584 | |
546 /* Output all variables enqueued to be assembled. */ | 585 /* Output all variables enqueued to be assembled. */ |
547 bool | 586 bool |
548 varpool_assemble_pending_decls (void) | 587 varpool_assemble_pending_decls (void) |
549 { | 588 { |
550 bool changed = false; | 589 bool changed = false; |
551 | 590 struct varpool_node *node; |
552 if (errorcount || sorrycount) | 591 |
592 if (seen_error ()) | |
553 return false; | 593 return false; |
554 | 594 |
555 timevar_push (TV_VAROUT); | 595 timevar_push (TV_VAROUT); |
556 /* EH might mark decls as needed during expansion. This should be safe since | 596 /* EH might mark decls as needed during expansion. This should be safe since |
557 we don't create references to new function, but it should not be used | 597 we don't create references to new function, but it should not be used |
558 elsewhere. */ | 598 elsewhere. */ |
559 varpool_analyze_pending_decls (); | 599 varpool_analyze_pending_decls (); |
600 | |
601 for (node = varpool_nodes_queue; node; node = node->next_needed) | |
602 varpool_finalize_named_section_flags (node); | |
560 | 603 |
561 while (varpool_nodes_queue) | 604 while (varpool_nodes_queue) |
562 { | 605 { |
563 struct varpool_node *node = varpool_nodes_queue; | 606 struct varpool_node *node = varpool_nodes_queue; |
564 | 607 |
624 } | 667 } |
625 | 668 |
626 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful. | 669 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful. |
627 Extra name aliases are output whenever DECL is output. */ | 670 Extra name aliases are output whenever DECL is output. */ |
628 | 671 |
629 bool | 672 struct varpool_node * |
630 varpool_extra_name_alias (tree alias, tree decl) | 673 varpool_extra_name_alias (tree alias, tree decl) |
631 { | 674 { |
632 struct varpool_node key, *alias_node, *decl_node, **slot; | 675 struct varpool_node key, *alias_node, *decl_node, **slot; |
633 | 676 |
634 #ifndef ASM_OUTPUT_DEF | 677 #ifndef ASM_OUTPUT_DEF |
645 | 688 |
646 slot = (struct varpool_node **) htab_find_slot (varpool_hash, &key, INSERT); | 689 slot = (struct varpool_node **) htab_find_slot (varpool_hash, &key, INSERT); |
647 | 690 |
648 /* If the varpool_node has been already created, fail. */ | 691 /* If the varpool_node has been already created, fail. */ |
649 if (*slot) | 692 if (*slot) |
650 return false; | 693 return NULL; |
651 | 694 |
652 alias_node = GGC_CNEW (struct varpool_node); | 695 alias_node = ggc_alloc_cleared_varpool_node (); |
653 alias_node->decl = alias; | 696 alias_node->decl = alias; |
654 alias_node->alias = 1; | 697 alias_node->alias = 1; |
655 alias_node->extra_name = decl_node; | 698 alias_node->extra_name = decl_node; |
656 alias_node->next = decl_node->extra_name; | 699 alias_node->next = decl_node->extra_name; |
657 ipa_empty_ref_list (&alias_node->ref_list); | 700 ipa_empty_ref_list (&alias_node->ref_list); |
658 if (decl_node->extra_name) | 701 if (decl_node->extra_name) |
659 decl_node->extra_name->prev = alias_node; | 702 decl_node->extra_name->prev = alias_node; |
660 decl_node->extra_name = alias_node; | 703 decl_node->extra_name = alias_node; |
661 *slot = alias_node; | 704 *slot = alias_node; |
662 return true; | 705 return alias_node; |
706 } | |
707 | |
708 /* Return true when NODE is known to be used from other (non-LTO) object file. | |
709 Known only when doing LTO via linker plugin. */ | |
710 | |
711 bool | |
712 varpool_used_from_object_file_p (struct varpool_node *node) | |
713 { | |
714 struct varpool_node *alias; | |
715 | |
716 if (!TREE_PUBLIC (node->decl)) | |
717 return false; | |
718 if (resolution_used_from_other_file_p (node->resolution)) | |
719 return true; | |
720 for (alias = node->extra_name; alias; alias = alias->next) | |
721 if (TREE_PUBLIC (alias->decl) | |
722 && resolution_used_from_other_file_p (alias->resolution)) | |
723 return true; | |
724 return false; | |
663 } | 725 } |
664 | 726 |
665 #include "gt-varpool.h" | 727 #include "gt-varpool.h" |