varpool.c revision 1.3
1/* Callgraph handling code. 2 Copyright (C) 2003-2013 Free Software Foundation, Inc. 3 Contributed by Jan Hubicka 4 5This file is part of GCC. 6 7GCC is free software; you can redistribute it and/or modify it under 8the terms of the GNU General Public License as published by the Free 9Software Foundation; either version 3, or (at your option) any later 10version. 11 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13WARRANTY; without even the implied warranty of MERCHANTABILITY or 14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15for more details. 16 17You should have received a copy of the GNU General Public License 18along 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-core.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 "gimple.h" 36#include "tree-flow.h" 37#include "flags.h" 38 39/* Return varpool node assigned to DECL. Create new one when needed. */ 40struct varpool_node * 41varpool_node_for_decl (tree decl) 42{ 43 struct varpool_node *node = varpool_get_node (decl); 44 gcc_assert (TREE_CODE (decl) == VAR_DECL 45 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl) || in_lto_p)); 46 if (node) 47 return node; 48 49 node = ggc_alloc_cleared_varpool_node (); 50 node->symbol.type = SYMTAB_VARIABLE; 51 node->symbol.decl = decl; 52 symtab_register_node ((symtab_node)node); 53 return node; 54} 55 56/* Remove node from the varpool. */ 57void 58varpool_remove_node (struct varpool_node *node) 59{ 60 symtab_unregister_node ((symtab_node)node); 61 if (DECL_INITIAL (node->symbol.decl) 62 && !DECL_IN_CONSTANT_POOL (node->symbol.decl) 63 /* Keep vtables for BINFO folding. */ 64 && !DECL_VIRTUAL_P (node->symbol.decl) 65 /* FIXME: http://gcc.gnu.org/PR55395 */ 66 && debug_info_level == DINFO_LEVEL_NONE) 67 DECL_INITIAL (node->symbol.decl) = error_mark_node; 68 ggc_free (node); 69} 70 71/* Dump given cgraph node. */ 72void 73dump_varpool_node (FILE *f, struct varpool_node *node) 74{ 75 dump_symtab_base (f, (symtab_node)node); 76 fprintf (f, " Availability: %s\n", 77 cgraph_function_flags_ready 78 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)] 79 : "not-ready"); 80 fprintf (f, " Varpool flags:"); 81 if (DECL_INITIAL (node->symbol.decl)) 82 fprintf (f, " initialized"); 83 if (node->analyzed) 84 fprintf (f, " analyzed"); 85 if (node->finalized) 86 fprintf (f, " finalized"); 87 if (node->output) 88 fprintf (f, " output"); 89 fprintf (f, "\n"); 90} 91 92/* Dump the variable pool. */ 93void 94dump_varpool (FILE *f) 95{ 96 struct varpool_node *node; 97 98 fprintf (f, "variable pool:\n\n"); 99 FOR_EACH_VARIABLE (node) 100 dump_varpool_node (f, node); 101} 102 103/* Dump the variable pool to stderr. */ 104 105DEBUG_FUNCTION void 106debug_varpool (void) 107{ 108 dump_varpool (stderr); 109} 110 111/* Given an assembler name, lookup node. */ 112struct varpool_node * 113varpool_node_for_asm (tree asmname) 114{ 115 if (symtab_node node = symtab_node_for_asm (asmname)) 116 if (varpool_node *vnode = dyn_cast <varpool_node> (node)) 117 return vnode; 118 return NULL; 119} 120 121/* Determine if variable DECL is needed. That is, visible to something 122 either outside this translation unit, something magic in the system 123 configury */ 124bool 125decide_is_variable_needed (struct varpool_node *node, tree decl) 126{ 127 if (DECL_EXTERNAL (decl)) 128 return false; 129 130 /* If the user told us it is used, then it must be so. */ 131 if (node->symbol.force_output) 132 return true; 133 134 /* Externally visible variables must be output. The exception is 135 COMDAT variables that must be output only when they are needed. */ 136 if (TREE_PUBLIC (decl) 137 && !DECL_COMDAT (decl)) 138 return true; 139 140 return false; 141} 142 143/* Return if DECL is constant and its initial value is known (so we can do 144 constant folding using DECL_INITIAL (decl)). */ 145 146bool 147const_value_known_p (tree decl) 148{ 149 if (TREE_CODE (decl) != VAR_DECL 150 &&TREE_CODE (decl) != CONST_DECL) 151 return false; 152 153 if (TREE_CODE (decl) == CONST_DECL 154 || DECL_IN_CONSTANT_POOL (decl)) 155 return true; 156 157 gcc_assert (TREE_CODE (decl) == VAR_DECL); 158 159 if (!TREE_READONLY (decl) || TREE_THIS_VOLATILE (decl)) 160 return false; 161 162 /* Gimplifier takes away constructors of local vars */ 163 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) 164 return DECL_INITIAL (decl) != NULL; 165 166 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl)); 167 168 /* Variables declared 'const' without an initializer 169 have zero as the initializer if they may not be 170 overridden at link or run time. */ 171 if (!DECL_INITIAL (decl) 172 && (DECL_EXTERNAL (decl) 173 || decl_replaceable_p (decl))) 174 return false; 175 176 /* Variables declared `const' with an initializer are considered 177 to not be overwritable with different initializer by default. 178 179 ??? Previously we behaved so for scalar variables but not for array 180 accesses. */ 181 return true; 182} 183 184/* Add the variable DECL to the varpool. 185 Unlike varpool_finalize_decl function is intended to be used 186 by middle end and allows insertion of new variable at arbitrary point 187 of compilation. */ 188void 189varpool_add_new_variable (tree decl) 190{ 191 struct varpool_node *node; 192 varpool_finalize_decl (decl); 193 node = varpool_node_for_decl (decl); 194 if (varpool_externally_visible_p (node, false)) 195 node->symbol.externally_visible = true; 196} 197 198/* Return variable availability. See cgraph.h for description of individual 199 return values. */ 200enum availability 201cgraph_variable_initializer_availability (struct varpool_node *node) 202{ 203 gcc_assert (cgraph_function_flags_ready); 204 if (!node->finalized) 205 return AVAIL_NOT_AVAILABLE; 206 if (!TREE_PUBLIC (node->symbol.decl)) 207 return AVAIL_AVAILABLE; 208 /* If the variable can be overwritten, return OVERWRITABLE. Takes 209 care of at least two notable extensions - the COMDAT variables 210 used to share template instantiations in C++. */ 211 if (!decl_replaceable_p (node->symbol.decl)) 212 return AVAIL_OVERWRITABLE; 213 return AVAIL_AVAILABLE; 214} 215 216void 217varpool_analyze_node (struct varpool_node *node) 218{ 219 tree decl = node->symbol.decl; 220 221 /* When reading back varpool at LTO time, we re-construct the queue in order 222 to have "needed" list right by inserting all needed nodes into varpool. 223 We however don't want to re-analyze already analyzed nodes. */ 224 if (!node->analyzed) 225 { 226 gcc_assert (!in_lto_p || cgraph_function_flags_ready); 227 /* Compute the alignment early so function body expanders are 228 already informed about increased alignment. */ 229 align_variable (decl, 0); 230 } 231 if (node->alias && node->alias_of) 232 { 233 struct varpool_node *tgt = varpool_node_for_decl (node->alias_of); 234 struct varpool_node *n; 235 236 for (n = tgt; n && n->alias; 237 n = n->analyzed ? varpool_alias_aliased_node (n) : NULL) 238 if (n == node) 239 { 240 error ("variable %q+D part of alias cycle", node->symbol.decl); 241 node->alias = false; 242 continue; 243 } 244 if (!vec_safe_length (node->symbol.ref_list.references)) 245 ipa_record_reference ((symtab_node)node, (symtab_node)tgt, IPA_REF_ALIAS, NULL); 246 if (node->extra_name_alias) 247 { 248 DECL_WEAK (node->symbol.decl) = DECL_WEAK (node->alias_of); 249 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->alias_of); 250 DECL_VISIBILITY (node->symbol.decl) = DECL_VISIBILITY (node->alias_of); 251 fixup_same_cpp_alias_visibility ((symtab_node) node, 252 (symtab_node) tgt, node->alias_of); 253 } 254 } 255 else if (DECL_INITIAL (decl)) 256 record_references_in_initializer (decl, node->analyzed); 257 node->analyzed = true; 258} 259 260/* Assemble thunks and aliases associated to NODE. */ 261 262static void 263assemble_aliases (struct varpool_node *node) 264{ 265 int i; 266 struct ipa_ref *ref; 267 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++) 268 if (ref->use == IPA_REF_ALIAS) 269 { 270 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref); 271 do_assemble_alias (alias->symbol.decl, 272 DECL_ASSEMBLER_NAME (alias->alias_of)); 273 assemble_aliases (alias); 274 } 275} 276 277/* Output one variable, if necessary. Return whether we output it. */ 278 279bool 280varpool_assemble_decl (struct varpool_node *node) 281{ 282 tree decl = node->symbol.decl; 283 284 /* Aliases are outout when their target is produced or by 285 output_weakrefs. */ 286 if (node->alias) 287 return false; 288 289 /* Constant pool is output from RTL land when the reference 290 survive till this level. */ 291 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl)) 292 return false; 293 294 /* Decls with VALUE_EXPR should not be in the varpool at all. They 295 are not real variables, but just info for debugging and codegen. 296 Unfortunately at the moment emutls is not updating varpool correctly 297 after turning real vars into value_expr vars. */ 298 if (DECL_HAS_VALUE_EXPR_P (decl) 299 && !targetm.have_tls) 300 return false; 301 302 /* Hard register vars do not need to be output. */ 303 if (DECL_HARD_REGISTER (decl)) 304 return false; 305 306 gcc_checking_assert (!TREE_ASM_WRITTEN (decl) 307 && TREE_CODE (decl) == VAR_DECL 308 && !DECL_HAS_VALUE_EXPR_P (decl)); 309 310 if (!node->symbol.in_other_partition 311 && !DECL_EXTERNAL (decl)) 312 { 313 assemble_variable (decl, 0, 1, 0); 314 gcc_assert (TREE_ASM_WRITTEN (decl)); 315 node->finalized = 1; 316 assemble_aliases (node); 317 return true; 318 } 319 320 return false; 321} 322 323/* Add NODE to queue starting at FIRST. 324 The queue is linked via AUX pointers and terminated by pointer to 1. */ 325 326static void 327enqueue_node (struct varpool_node *node, struct varpool_node **first) 328{ 329 if (node->symbol.aux) 330 return; 331 gcc_checking_assert (*first); 332 node->symbol.aux = *first; 333 *first = node; 334} 335 336/* Optimization of function bodies might've rendered some variables as 337 unnecessary so we want to avoid these from being compiled. Re-do 338 reachability starting from variables that are either externally visible 339 or was referred from the asm output routines. */ 340 341static void 342varpool_remove_unreferenced_decls (void) 343{ 344 struct varpool_node *next, *node; 345 struct varpool_node *first = (struct varpool_node *)(void *)1; 346 int i; 347 struct ipa_ref *ref; 348 349 if (seen_error ()) 350 return; 351 352 if (cgraph_dump_file) 353 fprintf (cgraph_dump_file, "Trivially needed variables:"); 354 FOR_EACH_DEFINED_VARIABLE (node) 355 { 356 if (node->analyzed 357 && (!varpool_can_remove_if_no_refs (node) 358 /* We just expanded all function bodies. See if any of 359 them needed the variable. */ 360 || DECL_RTL_SET_P (node->symbol.decl))) 361 { 362 enqueue_node (node, &first); 363 if (cgraph_dump_file) 364 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node)); 365 } 366 } 367 while (first != (struct varpool_node *)(void *)1) 368 { 369 node = first; 370 first = (struct varpool_node *)first->symbol.aux; 371 372 if (node->symbol.same_comdat_group) 373 { 374 symtab_node next; 375 for (next = node->symbol.same_comdat_group; 376 next != (symtab_node)node; 377 next = next->symbol.same_comdat_group) 378 { 379 varpool_node *vnext = dyn_cast <varpool_node> (next); 380 if (vnext && vnext->analyzed) 381 enqueue_node (vnext, &first); 382 } 383 } 384 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++) 385 { 386 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred); 387 if (vnode 388 && (!DECL_EXTERNAL (ref->referred->symbol.decl) 389 || vnode->alias) 390 && vnode->analyzed) 391 enqueue_node (vnode, &first); 392 } 393 } 394 if (cgraph_dump_file) 395 fprintf (cgraph_dump_file, "\nRemoving variables:"); 396 for (node = varpool_first_defined_variable (); node; node = next) 397 { 398 next = varpool_next_defined_variable (node); 399 if (!node->symbol.aux) 400 { 401 if (cgraph_dump_file) 402 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node)); 403 varpool_remove_node (node); 404 } 405 } 406 if (cgraph_dump_file) 407 fprintf (cgraph_dump_file, "\n"); 408} 409 410/* For variables in named sections make sure get_variable_section 411 is called before we switch to those sections. Then section 412 conflicts between read-only and read-only requiring relocations 413 sections can be resolved. */ 414void 415varpool_finalize_named_section_flags (struct varpool_node *node) 416{ 417 if (!TREE_ASM_WRITTEN (node->symbol.decl) 418 && !node->alias 419 && !node->symbol.in_other_partition 420 && !DECL_EXTERNAL (node->symbol.decl) 421 && TREE_CODE (node->symbol.decl) == VAR_DECL 422 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl) 423 && DECL_SECTION_NAME (node->symbol.decl)) 424 get_variable_section (node->symbol.decl, false); 425} 426 427/* Output all variables enqueued to be assembled. */ 428bool 429varpool_output_variables (void) 430{ 431 bool changed = false; 432 struct varpool_node *node; 433 434 if (seen_error ()) 435 return false; 436 437 varpool_remove_unreferenced_decls (); 438 439 timevar_push (TV_VAROUT); 440 441 FOR_EACH_DEFINED_VARIABLE (node) 442 varpool_finalize_named_section_flags (node); 443 444 FOR_EACH_DEFINED_VARIABLE (node) 445 if (varpool_assemble_decl (node)) 446 changed = true; 447 timevar_pop (TV_VAROUT); 448 return changed; 449} 450 451/* Create a new global variable of type TYPE. */ 452tree 453add_new_static_var (tree type) 454{ 455 tree new_decl; 456 struct varpool_node *new_node; 457 458 new_decl = create_tmp_var_raw (type, NULL); 459 DECL_NAME (new_decl) = create_tmp_var_name (NULL); 460 TREE_READONLY (new_decl) = 0; 461 TREE_STATIC (new_decl) = 1; 462 TREE_USED (new_decl) = 1; 463 DECL_CONTEXT (new_decl) = NULL_TREE; 464 DECL_ABSTRACT (new_decl) = 0; 465 lang_hooks.dup_lang_specific_decl (new_decl); 466 new_node = varpool_node_for_decl (new_decl); 467 varpool_finalize_decl (new_decl); 468 469 return new_node->symbol.decl; 470} 471 472/* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful. 473 Extra name aliases are output whenever DECL is output. */ 474 475struct varpool_node * 476varpool_create_variable_alias (tree alias, tree decl) 477{ 478 struct varpool_node *alias_node; 479 480 gcc_assert (TREE_CODE (decl) == VAR_DECL); 481 gcc_assert (TREE_CODE (alias) == VAR_DECL); 482 alias_node = varpool_node_for_decl (alias); 483 alias_node->alias = 1; 484 alias_node->finalized = 1; 485 alias_node->alias_of = decl; 486 487 /* Extra name alias mechanizm creates aliases really late 488 via DECL_ASSEMBLER_NAME mechanizm. 489 This is unfortunate because they are not going through the 490 standard channels. Ensure they get output. */ 491 if (cgraph_state >= CGRAPH_STATE_IPA) 492 { 493 varpool_analyze_node (alias_node); 494 if (TREE_PUBLIC (alias)) 495 alias_node->symbol.externally_visible = true; 496 } 497 return alias_node; 498} 499 500/* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful. 501 Extra name aliases are output whenever DECL is output. */ 502 503struct varpool_node * 504varpool_extra_name_alias (tree alias, tree decl) 505{ 506 struct varpool_node *alias_node; 507 508#ifndef ASM_OUTPUT_DEF 509 /* If aliases aren't supported by the assembler, fail. */ 510 return NULL; 511#endif 512 alias_node = varpool_create_variable_alias (alias, decl); 513 alias_node->extra_name_alias = true; 514 return alias_node; 515} 516 517/* Call calback on NODE and aliases associated to NODE. 518 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are 519 skipped. */ 520 521bool 522varpool_for_node_and_aliases (struct varpool_node *node, 523 bool (*callback) (struct varpool_node *, void *), 524 void *data, 525 bool include_overwritable) 526{ 527 int i; 528 struct ipa_ref *ref; 529 530 if (callback (node, data)) 531 return true; 532 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++) 533 if (ref->use == IPA_REF_ALIAS) 534 { 535 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref); 536 if (include_overwritable 537 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE) 538 if (varpool_for_node_and_aliases (alias, callback, data, 539 include_overwritable)) 540 return true; 541 } 542 return false; 543} 544