1 /* Top-level LTO routines. 2 Copyright 2009, 2010, 2011 Free Software Foundation, Inc. 3 Contributed by CodeSourcery, Inc. 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 "opts.h" 25 #include "toplev.h" 26 #include "tree.h" 27 #include "tree-flow.h" 28 #include "output.h" 29 #include "diagnostic-core.h" 30 #include "tm.h" 31 #include "cgraph.h" 32 #include "ggc.h" 33 #include "tree-ssa-operands.h" 34 #include "tree-pass.h" 35 #include "langhooks.h" 36 #include "vec.h" 37 #include "bitmap.h" 38 #include "pointer-set.h" 39 #include "ipa-prop.h" 40 #include "common.h" 41 #include "debug.h" 42 #include "timevar.h" 43 #include "gimple.h" 44 #include "lto.h" 45 #include "lto-tree.h" 46 #include "lto-streamer.h" 47 #include "tree-streamer.h" 48 #include "splay-tree.h" 49 #include "params.h" 50 #include "ipa-inline.h" 51 #include "ipa-utils.h" 52 53 static GTY(()) tree first_personality_decl; 54 55 /* Returns a hash code for P. */ 56 57 static hashval_t 58 hash_name (const void *p) 59 { 60 const struct lto_section_slot *ds = (const struct lto_section_slot *) p; 61 return (hashval_t) htab_hash_string (ds->name); 62 } 63 64 65 /* Returns nonzero if P1 and P2 are equal. */ 66 67 static int 68 eq_name (const void *p1, const void *p2) 69 { 70 const struct lto_section_slot *s1 = 71 (const struct lto_section_slot *) p1; 72 const struct lto_section_slot *s2 = 73 (const struct lto_section_slot *) p2; 74 75 return strcmp (s1->name, s2->name) == 0; 76 } 77 78 /* Free lto_section_slot */ 79 80 static void 81 free_with_string (void *arg) 82 { 83 struct lto_section_slot *s = (struct lto_section_slot *)arg; 84 85 free (CONST_CAST (char *, s->name)); 86 free (arg); 87 } 88 89 /* Create section hash table */ 90 91 htab_t 92 lto_obj_create_section_hash_table (void) 93 { 94 return htab_create (37, hash_name, eq_name, free_with_string); 95 } 96 97 /* Delete an allocated integer KEY in the splay tree. */ 98 99 static void 100 lto_splay_tree_delete_id (splay_tree_key key) 101 { 102 free ((void *) key); 103 } 104 105 /* Compare splay tree node ids A and B. */ 106 107 static int 108 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b) 109 { 110 unsigned HOST_WIDE_INT ai; 111 unsigned HOST_WIDE_INT bi; 112 113 ai = *(unsigned HOST_WIDE_INT *) a; 114 bi = *(unsigned HOST_WIDE_INT *) b; 115 116 if (ai < bi) 117 return -1; 118 else if (ai > bi) 119 return 1; 120 return 0; 121 } 122 123 /* Look up splay tree node by ID in splay tree T. */ 124 125 static splay_tree_node 126 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id) 127 { 128 return splay_tree_lookup (t, (splay_tree_key) &id); 129 } 130 131 /* Check if KEY has ID. */ 132 133 static bool 134 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id) 135 { 136 return *(unsigned HOST_WIDE_INT *) key == id; 137 } 138 139 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value. 140 The ID is allocated separately because we need HOST_WIDE_INTs which may 141 be wider than a splay_tree_key. */ 142 143 static void 144 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id, 145 struct lto_file_decl_data *file_data) 146 { 147 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT); 148 *idp = id; 149 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data); 150 } 151 152 /* Create a splay tree. */ 153 154 static splay_tree 155 lto_splay_tree_new (void) 156 { 157 return splay_tree_new (lto_splay_tree_compare_ids, 158 lto_splay_tree_delete_id, 159 NULL); 160 } 161 162 /* Read the constructors and inits. */ 163 164 static void 165 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data) 166 { 167 size_t len; 168 const char *data = lto_get_section_data (file_data, 169 LTO_section_static_initializer, 170 NULL, &len); 171 lto_input_constructors_and_inits (file_data, data); 172 lto_free_section_data (file_data, LTO_section_static_initializer, NULL, 173 data, len); 174 } 175 176 /* Return true when NODE has a clone that is analyzed (i.e. we need 177 to load its body even if the node itself is not needed). */ 178 179 static bool 180 has_analyzed_clone_p (struct cgraph_node *node) 181 { 182 struct cgraph_node *orig = node; 183 node = node->clones; 184 if (node) 185 while (node != orig) 186 { 187 if (node->analyzed) 188 return true; 189 if (node->clones) 190 node = node->clones; 191 else if (node->next_sibling_clone) 192 node = node->next_sibling_clone; 193 else 194 { 195 while (node != orig && !node->next_sibling_clone) 196 node = node->clone_of; 197 if (node != orig) 198 node = node->next_sibling_clone; 199 } 200 } 201 return false; 202 } 203 204 /* Read the function body for the function associated with NODE. */ 205 206 static void 207 lto_materialize_function (struct cgraph_node *node) 208 { 209 tree decl; 210 struct lto_file_decl_data *file_data; 211 const char *data, *name; 212 size_t len; 213 214 decl = node->decl; 215 /* Read in functions with body (analyzed nodes) 216 and also functions that are needed to produce virtual clones. */ 217 if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node)) 218 { 219 /* Clones and thunks don't need to be read. */ 220 if (node->clone_of) 221 return; 222 223 /* Load the function body only if not operating in WPA mode. In 224 WPA mode, the body of the function is not needed. */ 225 if (!flag_wpa) 226 { 227 file_data = node->local.lto_file_data; 228 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 229 230 /* We may have renamed the declaration, e.g., a static function. */ 231 name = lto_get_decl_name_mapping (file_data, name); 232 233 data = lto_get_section_data (file_data, LTO_section_function_body, 234 name, &len); 235 if (!data) 236 fatal_error ("%s: section %s is missing", 237 file_data->file_name, 238 name); 239 240 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL); 241 242 allocate_struct_function (decl, false); 243 announce_function (decl); 244 lto_input_function_body (file_data, decl, data); 245 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl) 246 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl); 247 lto_stats.num_function_bodies++; 248 lto_free_section_data (file_data, LTO_section_function_body, name, 249 data, len); 250 ggc_collect (); 251 } 252 } 253 254 /* Let the middle end know about the function. */ 255 rest_of_decl_compilation (decl, 1, 0); 256 } 257 258 259 /* Decode the content of memory pointed to by DATA in the in decl 260 state object STATE. DATA_IN points to a data_in structure for 261 decoding. Return the address after the decoded object in the 262 input. */ 263 264 static const uint32_t * 265 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data, 266 struct lto_in_decl_state *state) 267 { 268 uint32_t ix; 269 tree decl; 270 uint32_t i, j; 271 272 ix = *data++; 273 decl = streamer_tree_cache_get (data_in->reader_cache, ix); 274 if (TREE_CODE (decl) != FUNCTION_DECL) 275 { 276 gcc_assert (decl == void_type_node); 277 decl = NULL_TREE; 278 } 279 state->fn_decl = decl; 280 281 for (i = 0; i < LTO_N_DECL_STREAMS; i++) 282 { 283 uint32_t size = *data++; 284 tree *decls = ggc_alloc_vec_tree (size); 285 286 for (j = 0; j < size; j++) 287 decls[j] = streamer_tree_cache_get (data_in->reader_cache, data[j]); 288 289 state->streams[i].size = size; 290 state->streams[i].trees = decls; 291 data += size; 292 } 293 294 return data; 295 } 296 297 /* A hashtable of trees that potentially refer to variables or functions 298 that must be replaced with their prevailing variant. */ 299 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t 300 tree_with_vars; 301 302 /* Remember that T is a tree that (potentially) refers to a variable 303 or function decl that may be replaced with its prevailing variant. */ 304 static void 305 remember_with_vars (tree t) 306 { 307 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t; 308 } 309 310 #define GIMPLE_REGISTER_TYPE(tt) \ 311 (TREE_VISITED (tt) ? gimple_register_type (tt) : tt) 312 313 #define LTO_FIXUP_TREE(tt) \ 314 do \ 315 { \ 316 if (tt) \ 317 { \ 318 if (TYPE_P (tt)) \ 319 (tt) = GIMPLE_REGISTER_TYPE (tt); \ 320 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \ 321 remember_with_vars (t); \ 322 } \ 323 } while (0) 324 325 static void lto_fixup_types (tree); 326 327 /* Fix up fields of a tree_typed T. */ 328 329 static void 330 lto_ft_typed (tree t) 331 { 332 LTO_FIXUP_TREE (TREE_TYPE (t)); 333 } 334 335 /* Fix up fields of a tree_common T. */ 336 337 static void 338 lto_ft_common (tree t) 339 { 340 lto_ft_typed (t); 341 LTO_FIXUP_TREE (TREE_CHAIN (t)); 342 } 343 344 /* Fix up fields of a decl_minimal T. */ 345 346 static void 347 lto_ft_decl_minimal (tree t) 348 { 349 lto_ft_common (t); 350 LTO_FIXUP_TREE (DECL_NAME (t)); 351 LTO_FIXUP_TREE (DECL_CONTEXT (t)); 352 } 353 354 /* Fix up fields of a decl_common T. */ 355 356 static void 357 lto_ft_decl_common (tree t) 358 { 359 lto_ft_decl_minimal (t); 360 LTO_FIXUP_TREE (DECL_SIZE (t)); 361 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t)); 362 LTO_FIXUP_TREE (DECL_INITIAL (t)); 363 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t)); 364 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t)); 365 } 366 367 /* Fix up fields of a decl_with_vis T. */ 368 369 static void 370 lto_ft_decl_with_vis (tree t) 371 { 372 lto_ft_decl_common (t); 373 374 /* Accessor macro has side-effects, use field-name here. */ 375 LTO_FIXUP_TREE (t->decl_with_vis.assembler_name); 376 LTO_FIXUP_TREE (DECL_SECTION_NAME (t)); 377 } 378 379 /* Fix up fields of a decl_non_common T. */ 380 381 static void 382 lto_ft_decl_non_common (tree t) 383 { 384 lto_ft_decl_with_vis (t); 385 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t)); 386 LTO_FIXUP_TREE (DECL_RESULT_FLD (t)); 387 LTO_FIXUP_TREE (DECL_VINDEX (t)); 388 /* The C frontends may create exact duplicates for DECL_ORIGINAL_TYPE 389 like for 'typedef enum foo foo'. We have no way of avoiding to 390 merge them and dwarf2out.c cannot deal with this, 391 so fix this up by clearing DECL_ORIGINAL_TYPE in this case. */ 392 if (TREE_CODE (t) == TYPE_DECL 393 && DECL_ORIGINAL_TYPE (t) == TREE_TYPE (t)) 394 DECL_ORIGINAL_TYPE (t) = NULL_TREE; 395 } 396 397 /* Fix up fields of a decl_non_common T. */ 398 399 static void 400 lto_ft_function (tree t) 401 { 402 lto_ft_decl_non_common (t); 403 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t)); 404 } 405 406 /* Fix up fields of a field_decl T. */ 407 408 static void 409 lto_ft_field_decl (tree t) 410 { 411 lto_ft_decl_common (t); 412 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t)); 413 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t)); 414 LTO_FIXUP_TREE (DECL_QUALIFIER (t)); 415 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t)); 416 LTO_FIXUP_TREE (DECL_FCONTEXT (t)); 417 } 418 419 /* Fix up fields of a type T. */ 420 421 static void 422 lto_ft_type (tree t) 423 { 424 lto_ft_common (t); 425 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t)); 426 LTO_FIXUP_TREE (TYPE_SIZE (t)); 427 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t)); 428 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t)); 429 LTO_FIXUP_TREE (TYPE_NAME (t)); 430 431 /* Accessors are for derived node types only. */ 432 if (!POINTER_TYPE_P (t)) 433 LTO_FIXUP_TREE (TYPE_MINVAL (t)); 434 LTO_FIXUP_TREE (TYPE_MAXVAL (t)); 435 436 /* Accessor is for derived node types only. */ 437 LTO_FIXUP_TREE (t->type_non_common.binfo); 438 439 LTO_FIXUP_TREE (TYPE_CONTEXT (t)); 440 } 441 442 /* Fix up fields of a BINFO T. */ 443 444 static void 445 lto_ft_binfo (tree t) 446 { 447 unsigned HOST_WIDE_INT i, n; 448 tree base, saved_base; 449 450 lto_ft_common (t); 451 LTO_FIXUP_TREE (BINFO_VTABLE (t)); 452 LTO_FIXUP_TREE (BINFO_OFFSET (t)); 453 LTO_FIXUP_TREE (BINFO_VIRTUALS (t)); 454 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t)); 455 n = VEC_length (tree, BINFO_BASE_ACCESSES (t)); 456 for (i = 0; i < n; i++) 457 { 458 saved_base = base = BINFO_BASE_ACCESS (t, i); 459 LTO_FIXUP_TREE (base); 460 if (base != saved_base) 461 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base); 462 } 463 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t)); 464 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t)); 465 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t)); 466 n = BINFO_N_BASE_BINFOS (t); 467 for (i = 0; i < n; i++) 468 { 469 saved_base = base = BINFO_BASE_BINFO (t, i); 470 LTO_FIXUP_TREE (base); 471 if (base != saved_base) 472 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base); 473 } 474 } 475 476 /* Fix up fields of a CONSTRUCTOR T. */ 477 478 static void 479 lto_ft_constructor (tree t) 480 { 481 unsigned HOST_WIDE_INT idx; 482 constructor_elt *ce; 483 484 lto_ft_typed (t); 485 486 for (idx = 0; 487 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce); 488 idx++) 489 { 490 LTO_FIXUP_TREE (ce->index); 491 LTO_FIXUP_TREE (ce->value); 492 } 493 } 494 495 /* Fix up fields of an expression tree T. */ 496 497 static void 498 lto_ft_expr (tree t) 499 { 500 int i; 501 lto_ft_typed (t); 502 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i) 503 LTO_FIXUP_TREE (TREE_OPERAND (t, i)); 504 } 505 506 /* Given a tree T fixup fields of T by replacing types with their merged 507 variant and other entities by an equal entity from an earlier compilation 508 unit, or an entity being canonical in a different way. This includes 509 for instance integer or string constants. */ 510 511 static void 512 lto_fixup_types (tree t) 513 { 514 switch (TREE_CODE (t)) 515 { 516 case IDENTIFIER_NODE: 517 break; 518 519 case TREE_LIST: 520 LTO_FIXUP_TREE (TREE_VALUE (t)); 521 LTO_FIXUP_TREE (TREE_PURPOSE (t)); 522 LTO_FIXUP_TREE (TREE_CHAIN (t)); 523 break; 524 525 case FIELD_DECL: 526 lto_ft_field_decl (t); 527 break; 528 529 case LABEL_DECL: 530 case CONST_DECL: 531 case PARM_DECL: 532 case RESULT_DECL: 533 case IMPORTED_DECL: 534 lto_ft_decl_common (t); 535 break; 536 537 case VAR_DECL: 538 lto_ft_decl_with_vis (t); 539 break; 540 541 case TYPE_DECL: 542 lto_ft_decl_non_common (t); 543 break; 544 545 case FUNCTION_DECL: 546 lto_ft_function (t); 547 break; 548 549 case TREE_BINFO: 550 lto_ft_binfo (t); 551 break; 552 553 case PLACEHOLDER_EXPR: 554 lto_ft_common (t); 555 break; 556 557 case BLOCK: 558 case TRANSLATION_UNIT_DECL: 559 case OPTIMIZATION_NODE: 560 case TARGET_OPTION_NODE: 561 break; 562 563 default: 564 if (TYPE_P (t)) 565 lto_ft_type (t); 566 else if (TREE_CODE (t) == CONSTRUCTOR) 567 lto_ft_constructor (t); 568 else if (CONSTANT_CLASS_P (t)) 569 LTO_FIXUP_TREE (TREE_TYPE (t)); 570 else if (EXPR_P (t)) 571 { 572 lto_ft_expr (t); 573 } 574 else 575 { 576 remember_with_vars (t); 577 } 578 } 579 } 580 581 582 /* Return the resolution for the decl with index INDEX from DATA_IN. */ 583 584 static enum ld_plugin_symbol_resolution 585 get_resolution (struct data_in *data_in, unsigned index) 586 { 587 if (data_in->globals_resolution) 588 { 589 ld_plugin_symbol_resolution_t ret; 590 /* We can have references to not emitted functions in 591 DECL_FUNCTION_PERSONALITY at least. So we can and have 592 to indeed return LDPR_UNKNOWN in some cases. */ 593 if (VEC_length (ld_plugin_symbol_resolution_t, 594 data_in->globals_resolution) <= index) 595 return LDPR_UNKNOWN; 596 ret = VEC_index (ld_plugin_symbol_resolution_t, 597 data_in->globals_resolution, 598 index); 599 return ret; 600 } 601 else 602 /* Delay resolution finding until decl merging. */ 603 return LDPR_UNKNOWN; 604 } 605 606 607 /* Register DECL with the global symbol table and change its 608 name if necessary to avoid name clashes for static globals across 609 different files. */ 610 611 static void 612 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl) 613 { 614 tree context; 615 616 /* Variable has file scope, not local. Need to ensure static variables 617 between different files don't clash unexpectedly. */ 618 if (!TREE_PUBLIC (decl) 619 && !((context = decl_function_context (decl)) 620 && auto_var_in_fn_p (decl, context))) 621 { 622 /* ??? We normally pre-mangle names before we serialize them 623 out. Here, in lto1, we do not know the language, and 624 thus cannot do the mangling again. Instead, we just 625 append a suffix to the mangled name. The resulting name, 626 however, is not a properly-formed mangled name, and will 627 confuse any attempt to unmangle it. */ 628 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 629 char *label; 630 631 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl)); 632 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label)); 633 rest_of_decl_compilation (decl, 1, 0); 634 VEC_safe_push (tree, gc, lto_global_var_decls, decl); 635 } 636 637 /* If this variable has already been declared, queue the 638 declaration for merging. */ 639 if (TREE_PUBLIC (decl)) 640 { 641 unsigned ix; 642 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix)) 643 gcc_unreachable (); 644 lto_symtab_register_decl (decl, get_resolution (data_in, ix), 645 data_in->file_data); 646 } 647 } 648 649 650 /* Register DECL with the global symbol table and change its 651 name if necessary to avoid name clashes for static globals across 652 different files. DATA_IN contains descriptors and tables for the 653 file being read. */ 654 655 static void 656 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl) 657 { 658 /* Need to ensure static entities between different files 659 don't clash unexpectedly. */ 660 if (!TREE_PUBLIC (decl)) 661 { 662 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it 663 may set the assembler name where it was previously empty. */ 664 tree old_assembler_name = decl->decl_with_vis.assembler_name; 665 666 /* FIXME lto: We normally pre-mangle names before we serialize 667 them out. Here, in lto1, we do not know the language, and 668 thus cannot do the mangling again. Instead, we just append a 669 suffix to the mangled name. The resulting name, however, is 670 not a properly-formed mangled name, and will confuse any 671 attempt to unmangle it. */ 672 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 673 char *label; 674 675 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl)); 676 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label)); 677 678 /* We may arrive here with the old assembler name not set 679 if the function body is not needed, e.g., it has been 680 inlined away and does not appear in the cgraph. */ 681 if (old_assembler_name) 682 { 683 tree new_assembler_name = DECL_ASSEMBLER_NAME (decl); 684 685 /* Make the original assembler name available for later use. 686 We may have used it to indicate the section within its 687 object file where the function body may be found. 688 FIXME lto: Find a better way to maintain the function decl 689 to body section mapping so we don't need this hack. */ 690 lto_record_renamed_decl (data_in->file_data, 691 IDENTIFIER_POINTER (old_assembler_name), 692 IDENTIFIER_POINTER (new_assembler_name)); 693 } 694 } 695 696 /* If this variable has already been declared, queue the 697 declaration for merging. */ 698 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl)) 699 { 700 unsigned ix; 701 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix)) 702 gcc_unreachable (); 703 lto_symtab_register_decl (decl, get_resolution (data_in, ix), 704 data_in->file_data); 705 } 706 } 707 708 709 /* Given a streamer cache structure DATA_IN (holding a sequence of trees 710 for one compilation unit) go over all trees starting at index FROM until the 711 end of the sequence and replace fields of those trees, and the trees 712 themself with their canonical variants as per gimple_register_type. */ 713 714 static void 715 uniquify_nodes (struct data_in *data_in, unsigned from) 716 { 717 struct streamer_tree_cache_d *cache = data_in->reader_cache; 718 unsigned len = VEC_length (tree, cache->nodes); 719 unsigned i; 720 721 /* Go backwards because children streamed for the first time come 722 as part of their parents, and hence are created after them. */ 723 724 /* First register all the types in the cache. This makes sure to 725 have the original structure in the type cycles when registering 726 them and computing hashes. */ 727 for (i = len; i-- > from;) 728 { 729 tree t = VEC_index (tree, cache->nodes, i); 730 if (t && TYPE_P (t)) 731 { 732 tree newt = gimple_register_type (t); 733 /* Mark non-prevailing types so we fix them up. No need 734 to reset that flag afterwards - nothing that refers 735 to those types is left and they are collected. */ 736 if (newt != t) 737 TREE_VISITED (t) = 1; 738 } 739 } 740 741 /* Second fixup all trees in the new cache entries. */ 742 for (i = len; i-- > from;) 743 { 744 tree t = VEC_index (tree, cache->nodes, i); 745 tree oldt = t; 746 if (!t) 747 continue; 748 749 /* First fixup the fields of T. */ 750 lto_fixup_types (t); 751 752 if (!TYPE_P (t)) 753 continue; 754 755 /* Now try to find a canonical variant of T itself. */ 756 t = GIMPLE_REGISTER_TYPE (t); 757 758 if (t == oldt) 759 { 760 /* The following re-creates proper variant lists while fixing up 761 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the 762 variant list state before fixup is broken. */ 763 tree tem, mv; 764 765 #ifdef ENABLE_CHECKING 766 /* Remove us from our main variant list if we are not the 767 variant leader. */ 768 if (TYPE_MAIN_VARIANT (t) != t) 769 { 770 tem = TYPE_MAIN_VARIANT (t); 771 while (tem && TYPE_NEXT_VARIANT (tem) != t) 772 tem = TYPE_NEXT_VARIANT (tem); 773 gcc_assert (!tem && !TYPE_NEXT_VARIANT (t)); 774 } 775 #endif 776 777 /* Query our new main variant. */ 778 mv = GIMPLE_REGISTER_TYPE (TYPE_MAIN_VARIANT (t)); 779 780 /* If we were the variant leader and we get replaced ourselves drop 781 all variants from our list. */ 782 if (TYPE_MAIN_VARIANT (t) == t 783 && mv != t) 784 { 785 tem = t; 786 while (tem) 787 { 788 tree tem2 = TYPE_NEXT_VARIANT (tem); 789 TYPE_NEXT_VARIANT (tem) = NULL_TREE; 790 tem = tem2; 791 } 792 } 793 794 /* If we are not our own variant leader link us into our new leaders 795 variant list. */ 796 if (mv != t) 797 { 798 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv); 799 TYPE_NEXT_VARIANT (mv) = t; 800 if (RECORD_OR_UNION_TYPE_P (t)) 801 TYPE_BINFO (t) = TYPE_BINFO (mv); 802 } 803 804 /* Finally adjust our main variant and fix it up. */ 805 TYPE_MAIN_VARIANT (t) = mv; 806 807 /* The following reconstructs the pointer chains 808 of the new pointed-to type if we are a main variant. We do 809 not stream those so they are broken before fixup. */ 810 if (TREE_CODE (t) == POINTER_TYPE 811 && TYPE_MAIN_VARIANT (t) == t) 812 { 813 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t)); 814 TYPE_POINTER_TO (TREE_TYPE (t)) = t; 815 } 816 else if (TREE_CODE (t) == REFERENCE_TYPE 817 && TYPE_MAIN_VARIANT (t) == t) 818 { 819 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t)); 820 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t; 821 } 822 } 823 824 else 825 { 826 if (RECORD_OR_UNION_TYPE_P (t)) 827 { 828 tree f1, f2; 829 if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt)) 830 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt); 831 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2)) 832 { 833 unsigned ix; 834 gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2)); 835 if (!streamer_tree_cache_lookup (cache, f2, &ix)) 836 gcc_unreachable (); 837 /* If we're going to replace an element which we'd 838 still visit in the next iterations, we wouldn't 839 handle it, so do it here. We do have to handle it 840 even though the field_decl itself will be removed, 841 as it could refer to e.g. integer_cst which we 842 wouldn't reach via any other way, hence they 843 (and their type) would stay uncollected. */ 844 /* ??? We should rather make sure to replace all 845 references to f2 with f1. That means handling 846 COMPONENT_REFs and CONSTRUCTOR elements in 847 lto_fixup_types and special-case the field-decl 848 operand handling. */ 849 if (ix < i) 850 lto_fixup_types (f2); 851 streamer_tree_cache_insert_at (cache, f1, ix); 852 } 853 } 854 855 /* If we found a tree that is equal to oldt replace it in the 856 cache, so that further users (in the various LTO sections) 857 make use of it. */ 858 streamer_tree_cache_insert_at (cache, t, i); 859 } 860 } 861 862 /* Finally compute the canonical type of all TREE_TYPEs and register 863 VAR_DECL and FUNCTION_DECL nodes in the symbol table. 864 From this point there are no longer any types with 865 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems. 866 This step requires the TYPE_POINTER_TO lists being present, so 867 make sure it is done last. */ 868 for (i = len; i-- > from;) 869 { 870 tree t = VEC_index (tree, cache->nodes, i); 871 if (t == NULL_TREE) 872 continue; 873 874 if (TREE_CODE (t) == VAR_DECL) 875 lto_register_var_decl_in_symtab (data_in, t); 876 else if (TREE_CODE (t) == FUNCTION_DECL && !DECL_BUILT_IN (t)) 877 lto_register_function_decl_in_symtab (data_in, t); 878 else if (!flag_wpa 879 && TREE_CODE (t) == TYPE_DECL) 880 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t)); 881 else if (TYPE_P (t) && !TYPE_CANONICAL (t)) 882 TYPE_CANONICAL (t) = gimple_register_canonical_type (t); 883 } 884 } 885 886 887 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA. 888 RESOLUTIONS is the set of symbols picked by the linker (read from the 889 resolution file when the linker plugin is being used). */ 890 891 static void 892 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data, 893 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions) 894 { 895 const struct lto_decl_header *header = (const struct lto_decl_header *) data; 896 const int decl_offset = sizeof (struct lto_decl_header); 897 const int main_offset = decl_offset + header->decl_state_size; 898 const int string_offset = main_offset + header->main_size; 899 struct lto_input_block ib_main; 900 struct data_in *data_in; 901 unsigned int i; 902 const uint32_t *data_ptr, *data_end; 903 uint32_t num_decl_states; 904 905 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0, 906 header->main_size); 907 908 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset, 909 header->string_size, resolutions); 910 911 /* We do not uniquify the pre-loaded cache entries, those are middle-end 912 internal types that should not be merged. */ 913 914 /* Read the global declarations and types. */ 915 while (ib_main.p < ib_main.len) 916 { 917 tree t; 918 unsigned from = VEC_length (tree, data_in->reader_cache->nodes); 919 t = stream_read_tree (&ib_main, data_in); 920 gcc_assert (t && ib_main.p <= ib_main.len); 921 uniquify_nodes (data_in, from); 922 } 923 924 /* Read in lto_in_decl_state objects. */ 925 data_ptr = (const uint32_t *) ((const char*) data + decl_offset); 926 data_end = 927 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size); 928 num_decl_states = *data_ptr++; 929 930 gcc_assert (num_decl_states > 0); 931 decl_data->global_decl_state = lto_new_in_decl_state (); 932 data_ptr = lto_read_in_decl_state (data_in, data_ptr, 933 decl_data->global_decl_state); 934 935 /* Read in per-function decl states and enter them in hash table. */ 936 decl_data->function_decl_states = 937 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL); 938 939 for (i = 1; i < num_decl_states; i++) 940 { 941 struct lto_in_decl_state *state = lto_new_in_decl_state (); 942 void **slot; 943 944 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state); 945 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT); 946 gcc_assert (*slot == NULL); 947 *slot = state; 948 } 949 950 if (data_ptr != data_end) 951 internal_error ("bytecode stream: garbage at the end of symbols section"); 952 953 /* Set the current decl state to be the global state. */ 954 decl_data->current_decl_state = decl_data->global_decl_state; 955 956 lto_data_in_delete (data_in); 957 } 958 959 /* Custom version of strtoll, which is not portable. */ 960 961 static HOST_WIDEST_INT 962 lto_parse_hex (const char *p) 963 { 964 HOST_WIDEST_INT ret = 0; 965 966 for (; *p != '\0'; ++p) 967 { 968 char c = *p; 969 unsigned char part; 970 ret <<= 4; 971 if (c >= '0' && c <= '9') 972 part = c - '0'; 973 else if (c >= 'a' && c <= 'f') 974 part = c - 'a' + 10; 975 else if (c >= 'A' && c <= 'F') 976 part = c - 'A' + 10; 977 else 978 internal_error ("could not parse hex number"); 979 ret |= part; 980 } 981 982 return ret; 983 } 984 985 /* Read resolution for file named FILE_NAME. The resolution is read from 986 RESOLUTION. */ 987 988 static void 989 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file) 990 { 991 /* We require that objects in the resolution file are in the same 992 order as the lto1 command line. */ 993 unsigned int name_len; 994 char *obj_name; 995 unsigned int num_symbols; 996 unsigned int i; 997 struct lto_file_decl_data *file_data; 998 splay_tree_node nd = NULL; 999 1000 if (!resolution) 1001 return; 1002 1003 name_len = strlen (file->filename); 1004 obj_name = XNEWVEC (char, name_len + 1); 1005 fscanf (resolution, " "); /* Read white space. */ 1006 1007 fread (obj_name, sizeof (char), name_len, resolution); 1008 obj_name[name_len] = '\0'; 1009 if (filename_cmp (obj_name, file->filename) != 0) 1010 internal_error ("unexpected file name %s in linker resolution file. " 1011 "Expected %s", obj_name, file->filename); 1012 if (file->offset != 0) 1013 { 1014 int t; 1015 char offset_p[17]; 1016 HOST_WIDEST_INT offset; 1017 t = fscanf (resolution, "@0x%16s", offset_p); 1018 if (t != 1) 1019 internal_error ("could not parse file offset"); 1020 offset = lto_parse_hex (offset_p); 1021 if (offset != file->offset) 1022 internal_error ("unexpected offset"); 1023 } 1024 1025 free (obj_name); 1026 1027 fscanf (resolution, "%u", &num_symbols); 1028 1029 for (i = 0; i < num_symbols; i++) 1030 { 1031 int t; 1032 unsigned index; 1033 unsigned HOST_WIDE_INT id; 1034 char r_str[27]; 1035 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0; 1036 unsigned int j; 1037 unsigned int lto_resolution_str_len = 1038 sizeof (lto_resolution_str) / sizeof (char *); 1039 res_pair rp; 1040 1041 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n", 1042 &index, &id, r_str); 1043 if (t != 3) 1044 internal_error ("invalid line in the resolution file"); 1045 1046 for (j = 0; j < lto_resolution_str_len; j++) 1047 { 1048 if (strcmp (lto_resolution_str[j], r_str) == 0) 1049 { 1050 r = (enum ld_plugin_symbol_resolution) j; 1051 break; 1052 } 1053 } 1054 if (j == lto_resolution_str_len) 1055 internal_error ("invalid resolution in the resolution file"); 1056 1057 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id))) 1058 { 1059 nd = lto_splay_tree_lookup (file_ids, id); 1060 if (nd == NULL) 1061 internal_error ("resolution sub id %wx not in object file", id); 1062 } 1063 1064 file_data = (struct lto_file_decl_data *)nd->value; 1065 /* The indexes are very sparse. To save memory save them in a compact 1066 format that is only unpacked later when the subfile is processed. */ 1067 rp.res = r; 1068 rp.index = index; 1069 VEC_safe_push (res_pair, heap, file_data->respairs, &rp); 1070 if (file_data->max_index < index) 1071 file_data->max_index = index; 1072 } 1073 } 1074 1075 /* List of file_decl_datas */ 1076 struct file_data_list 1077 { 1078 struct lto_file_decl_data *first, *last; 1079 }; 1080 1081 /* Is the name for a id'ed LTO section? */ 1082 1083 static int 1084 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id) 1085 { 1086 const char *s; 1087 1088 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX))) 1089 return 0; 1090 s = strrchr (name, '.'); 1091 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1; 1092 } 1093 1094 /* Create file_data of each sub file id */ 1095 1096 static int 1097 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids, 1098 struct file_data_list *list) 1099 { 1100 struct lto_section_slot s_slot, *new_slot; 1101 unsigned HOST_WIDE_INT id; 1102 splay_tree_node nd; 1103 void **hash_slot; 1104 char *new_name; 1105 struct lto_file_decl_data *file_data; 1106 1107 if (!lto_section_with_id (ls->name, &id)) 1108 return 1; 1109 1110 /* Find hash table of sub module id */ 1111 nd = lto_splay_tree_lookup (file_ids, id); 1112 if (nd != NULL) 1113 { 1114 file_data = (struct lto_file_decl_data *)nd->value; 1115 } 1116 else 1117 { 1118 file_data = ggc_alloc_lto_file_decl_data (); 1119 memset(file_data, 0, sizeof (struct lto_file_decl_data)); 1120 file_data->id = id; 1121 file_data->section_hash_table = lto_obj_create_section_hash_table ();; 1122 lto_splay_tree_insert (file_ids, id, file_data); 1123 1124 /* Maintain list in linker order */ 1125 if (!list->first) 1126 list->first = file_data; 1127 if (list->last) 1128 list->last->next = file_data; 1129 list->last = file_data; 1130 } 1131 1132 /* Copy section into sub module hash table */ 1133 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1); 1134 s_slot.name = new_name; 1135 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT); 1136 gcc_assert (*hash_slot == NULL); 1137 1138 new_slot = XDUP (struct lto_section_slot, ls); 1139 new_slot->name = new_name; 1140 *hash_slot = new_slot; 1141 return 1; 1142 } 1143 1144 /* Read declarations and other initializations for a FILE_DATA. */ 1145 1146 static void 1147 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file) 1148 { 1149 const char *data; 1150 size_t len; 1151 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions = NULL; 1152 int i; 1153 res_pair *rp; 1154 1155 /* Create vector for fast access of resolution. We do this lazily 1156 to save memory. */ 1157 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap, 1158 resolutions, 1159 file_data->max_index + 1); 1160 for (i = 0; VEC_iterate (res_pair, file_data->respairs, i, rp); i++) 1161 VEC_replace (ld_plugin_symbol_resolution_t, resolutions, rp->index, rp->res); 1162 VEC_free (res_pair, heap, file_data->respairs); 1163 1164 file_data->renaming_hash_table = lto_create_renaming_table (); 1165 file_data->file_name = file->filename; 1166 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len); 1167 if (data == NULL) 1168 { 1169 internal_error ("cannot read LTO decls from %s", file_data->file_name); 1170 return; 1171 } 1172 /* Frees resolutions */ 1173 lto_read_decls (file_data, data, resolutions); 1174 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len); 1175 } 1176 1177 /* Finalize FILE_DATA in FILE and increase COUNT. */ 1178 1179 static int 1180 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data, 1181 int *count) 1182 { 1183 lto_file_finalize (file_data, file); 1184 if (cgraph_dump_file) 1185 fprintf (cgraph_dump_file, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n", 1186 file_data->file_name, file_data->id); 1187 (*count)++; 1188 return 0; 1189 } 1190 1191 /* Generate a TREE representation for all types and external decls 1192 entities in FILE. 1193 1194 Read all of the globals out of the file. Then read the cgraph 1195 and process the .o index into the cgraph nodes so that it can open 1196 the .o file to load the functions and ipa information. */ 1197 1198 static struct lto_file_decl_data * 1199 lto_file_read (lto_file *file, FILE *resolution_file, int *count) 1200 { 1201 struct lto_file_decl_data *file_data = NULL; 1202 splay_tree file_ids; 1203 htab_t section_hash_table; 1204 struct lto_section_slot *section; 1205 struct file_data_list file_list; 1206 struct lto_section_list section_list; 1207 1208 memset (§ion_list, 0, sizeof (struct lto_section_list)); 1209 section_hash_table = lto_obj_build_section_table (file, §ion_list); 1210 1211 /* Find all sub modules in the object and put their sections into new hash 1212 tables in a splay tree. */ 1213 file_ids = lto_splay_tree_new (); 1214 memset (&file_list, 0, sizeof (struct file_data_list)); 1215 for (section = section_list.first; section != NULL; section = section->next) 1216 create_subid_section_table (section, file_ids, &file_list); 1217 1218 /* Add resolutions to file ids */ 1219 lto_resolution_read (file_ids, resolution_file, file); 1220 1221 /* Finalize each lto file for each submodule in the merged object */ 1222 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next) 1223 lto_create_files_from_ids (file, file_data, count); 1224 1225 splay_tree_delete (file_ids); 1226 htab_delete (section_hash_table); 1227 1228 return file_list.first; 1229 } 1230 1231 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE 1232 #define LTO_MMAP_IO 1 1233 #endif 1234 1235 #if LTO_MMAP_IO 1236 /* Page size of machine is used for mmap and munmap calls. */ 1237 static size_t page_mask; 1238 #endif 1239 1240 /* Get the section data of length LEN from FILENAME starting at 1241 OFFSET. The data segment must be freed by the caller when the 1242 caller is finished. Returns NULL if all was not well. */ 1243 1244 static char * 1245 lto_read_section_data (struct lto_file_decl_data *file_data, 1246 intptr_t offset, size_t len) 1247 { 1248 char *result; 1249 static int fd = -1; 1250 static char *fd_name; 1251 #if LTO_MMAP_IO 1252 intptr_t computed_len; 1253 intptr_t computed_offset; 1254 intptr_t diff; 1255 #endif 1256 1257 /* Keep a single-entry file-descriptor cache. The last file we 1258 touched will get closed at exit. 1259 ??? Eventually we want to add a more sophisticated larger cache 1260 or rather fix function body streaming to not stream them in 1261 practically random order. */ 1262 if (fd != -1 1263 && filename_cmp (fd_name, file_data->file_name) != 0) 1264 { 1265 free (fd_name); 1266 close (fd); 1267 fd = -1; 1268 } 1269 if (fd == -1) 1270 { 1271 fd = open (file_data->file_name, O_RDONLY|O_BINARY); 1272 if (fd == -1) 1273 { 1274 fatal_error ("Cannot open %s", file_data->file_name); 1275 return NULL; 1276 } 1277 fd_name = xstrdup (file_data->file_name); 1278 } 1279 1280 #if LTO_MMAP_IO 1281 if (!page_mask) 1282 { 1283 size_t page_size = sysconf (_SC_PAGE_SIZE); 1284 page_mask = ~(page_size - 1); 1285 } 1286 1287 computed_offset = offset & page_mask; 1288 diff = offset - computed_offset; 1289 computed_len = len + diff; 1290 1291 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE, 1292 fd, computed_offset); 1293 if (result == MAP_FAILED) 1294 { 1295 fatal_error ("Cannot map %s", file_data->file_name); 1296 return NULL; 1297 } 1298 1299 return result + diff; 1300 #else 1301 result = (char *) xmalloc (len); 1302 if (lseek (fd, offset, SEEK_SET) != offset 1303 || read (fd, result, len) != (ssize_t) len) 1304 { 1305 free (result); 1306 fatal_error ("Cannot read %s", file_data->file_name); 1307 result = NULL; 1308 } 1309 #ifdef __MINGW32__ 1310 /* Native windows doesn't supports delayed unlink on opened file. So 1311 we close file here again. This produces higher I/O load, but at least 1312 it prevents to have dangling file handles preventing unlink. */ 1313 free (fd_name); 1314 fd_name = NULL; 1315 close (fd); 1316 fd = -1; 1317 #endif 1318 return result; 1319 #endif 1320 } 1321 1322 1323 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME. 1324 NAME will be NULL unless the section type is for a function 1325 body. */ 1326 1327 static const char * 1328 get_section_data (struct lto_file_decl_data *file_data, 1329 enum lto_section_type section_type, 1330 const char *name, 1331 size_t *len) 1332 { 1333 htab_t section_hash_table = file_data->section_hash_table; 1334 struct lto_section_slot *f_slot; 1335 struct lto_section_slot s_slot; 1336 const char *section_name = lto_get_section_name (section_type, name, file_data); 1337 char *data = NULL; 1338 1339 *len = 0; 1340 s_slot.name = section_name; 1341 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot); 1342 if (f_slot) 1343 { 1344 data = lto_read_section_data (file_data, f_slot->start, f_slot->len); 1345 *len = f_slot->len; 1346 } 1347 1348 free (CONST_CAST (char *, section_name)); 1349 return data; 1350 } 1351 1352 1353 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that 1354 starts at OFFSET and has LEN bytes. */ 1355 1356 static void 1357 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED, 1358 enum lto_section_type section_type ATTRIBUTE_UNUSED, 1359 const char *name ATTRIBUTE_UNUSED, 1360 const char *offset, size_t len ATTRIBUTE_UNUSED) 1361 { 1362 #if LTO_MMAP_IO 1363 intptr_t computed_len; 1364 intptr_t computed_offset; 1365 intptr_t diff; 1366 #endif 1367 1368 #if LTO_MMAP_IO 1369 computed_offset = ((intptr_t) offset) & page_mask; 1370 diff = (intptr_t) offset - computed_offset; 1371 computed_len = len + diff; 1372 1373 munmap ((caddr_t) computed_offset, computed_len); 1374 #else 1375 free (CONST_CAST(char *, offset)); 1376 #endif 1377 } 1378 1379 /* Structure describing ltrans partitions. */ 1380 1381 struct ltrans_partition_def 1382 { 1383 cgraph_node_set cgraph_set; 1384 varpool_node_set varpool_set; 1385 const char * name; 1386 int insns; 1387 }; 1388 1389 typedef struct ltrans_partition_def *ltrans_partition; 1390 DEF_VEC_P(ltrans_partition); 1391 DEF_VEC_ALLOC_P(ltrans_partition,heap); 1392 1393 static VEC(ltrans_partition, heap) *ltrans_partitions; 1394 1395 static void add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node); 1396 static void add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode); 1397 1398 /* Create new partition with name NAME. */ 1399 static ltrans_partition 1400 new_partition (const char *name) 1401 { 1402 ltrans_partition part = XCNEW (struct ltrans_partition_def); 1403 part->cgraph_set = cgraph_node_set_new (); 1404 part->varpool_set = varpool_node_set_new (); 1405 part->name = name; 1406 part->insns = 0; 1407 VEC_safe_push (ltrans_partition, heap, ltrans_partitions, part); 1408 return part; 1409 } 1410 1411 /* Free memory used by ltrans datastructures. */ 1412 static void 1413 free_ltrans_partitions (void) 1414 { 1415 unsigned int idx; 1416 ltrans_partition part; 1417 for (idx = 0; VEC_iterate (ltrans_partition, ltrans_partitions, idx, part); idx++) 1418 { 1419 free_cgraph_node_set (part->cgraph_set); 1420 free (part); 1421 } 1422 VEC_free (ltrans_partition, heap, ltrans_partitions); 1423 } 1424 1425 /* See all references that go to comdat objects and bring them into partition too. */ 1426 static void 1427 add_references_to_partition (ltrans_partition part, struct ipa_ref_list *refs) 1428 { 1429 int i; 1430 struct ipa_ref *ref; 1431 for (i = 0; ipa_ref_list_reference_iterate (refs, i, ref); i++) 1432 { 1433 if (ref->refered_type == IPA_REF_CGRAPH 1434 && DECL_COMDAT (cgraph_function_node (ipa_ref_node (ref), NULL)->decl) 1435 && !cgraph_node_in_set_p (ipa_ref_node (ref), part->cgraph_set)) 1436 add_cgraph_node_to_partition (part, ipa_ref_node (ref)); 1437 else 1438 if (ref->refered_type == IPA_REF_VARPOOL 1439 && DECL_COMDAT (ipa_ref_varpool_node (ref)->decl) 1440 && !varpool_node_in_set_p (ipa_ref_varpool_node (ref), part->varpool_set)) 1441 add_varpool_node_to_partition (part, ipa_ref_varpool_node (ref)); 1442 } 1443 } 1444 1445 /* Worker for add_cgraph_node_to_partition. */ 1446 1447 static bool 1448 add_cgraph_node_to_partition_1 (struct cgraph_node *node, void *data) 1449 { 1450 ltrans_partition part = (ltrans_partition) data; 1451 1452 /* non-COMDAT aliases of COMDAT functions needs to be output just once. */ 1453 if (!DECL_COMDAT (node->decl) 1454 && !node->global.inlined_to 1455 && node->aux) 1456 { 1457 gcc_assert (node->thunk.thunk_p || node->alias); 1458 return false; 1459 } 1460 1461 if (node->aux) 1462 { 1463 node->in_other_partition = 1; 1464 if (cgraph_dump_file) 1465 fprintf (cgraph_dump_file, "Node %s/%i now used in multiple partitions\n", 1466 cgraph_node_name (node), node->uid); 1467 } 1468 node->aux = (void *)((size_t)node->aux + 1); 1469 cgraph_node_set_add (part->cgraph_set, node); 1470 return false; 1471 } 1472 1473 /* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */ 1474 1475 static void 1476 add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node) 1477 { 1478 struct cgraph_edge *e; 1479 cgraph_node_set_iterator csi; 1480 struct cgraph_node *n; 1481 1482 /* We always decide on functions, not associated thunks and aliases. */ 1483 node = cgraph_function_node (node, NULL); 1484 1485 /* If NODE is already there, we have nothing to do. */ 1486 csi = cgraph_node_set_find (part->cgraph_set, node); 1487 if (!csi_end_p (csi)) 1488 return; 1489 1490 cgraph_for_node_thunks_and_aliases (node, add_cgraph_node_to_partition_1, part, true); 1491 1492 part->insns += inline_summary (node)->self_size; 1493 1494 1495 cgraph_node_set_add (part->cgraph_set, node); 1496 1497 for (e = node->callees; e; e = e->next_callee) 1498 if ((!e->inline_failed 1499 || DECL_COMDAT (cgraph_function_node (e->callee, NULL)->decl)) 1500 && !cgraph_node_in_set_p (e->callee, part->cgraph_set)) 1501 add_cgraph_node_to_partition (part, e->callee); 1502 1503 add_references_to_partition (part, &node->ref_list); 1504 1505 if (node->same_comdat_group) 1506 for (n = node->same_comdat_group; n != node; n = n->same_comdat_group) 1507 add_cgraph_node_to_partition (part, n); 1508 } 1509 1510 /* Add VNODE to partition as well as comdat references partition PART. */ 1511 1512 static void 1513 add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode) 1514 { 1515 varpool_node_set_iterator vsi; 1516 1517 vnode = varpool_variable_node (vnode, NULL); 1518 1519 /* If NODE is already there, we have nothing to do. */ 1520 vsi = varpool_node_set_find (part->varpool_set, vnode); 1521 if (!vsi_end_p (vsi)) 1522 return; 1523 1524 varpool_node_set_add (part->varpool_set, vnode); 1525 1526 if (vnode->aux) 1527 { 1528 vnode->in_other_partition = 1; 1529 if (cgraph_dump_file) 1530 fprintf (cgraph_dump_file, "Varpool node %s now used in multiple partitions\n", 1531 varpool_node_name (vnode)); 1532 } 1533 vnode->aux = (void *)((size_t)vnode->aux + 1); 1534 1535 add_references_to_partition (part, &vnode->ref_list); 1536 1537 if (vnode->same_comdat_group 1538 && !varpool_node_in_set_p (vnode->same_comdat_group, part->varpool_set)) 1539 add_varpool_node_to_partition (part, vnode->same_comdat_group); 1540 } 1541 1542 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES 1543 and number of varpool nodes is N_VARPOOL_NODES. */ 1544 1545 static void 1546 undo_partition (ltrans_partition partition, unsigned int n_cgraph_nodes, 1547 unsigned int n_varpool_nodes) 1548 { 1549 while (VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes) > 1550 n_cgraph_nodes) 1551 { 1552 struct cgraph_node *node = VEC_index (cgraph_node_ptr, 1553 partition->cgraph_set->nodes, 1554 n_cgraph_nodes); 1555 partition->insns -= inline_summary (node)->self_size; 1556 cgraph_node_set_remove (partition->cgraph_set, node); 1557 node->aux = (void *)((size_t)node->aux - 1); 1558 } 1559 while (VEC_length (varpool_node_ptr, partition->varpool_set->nodes) > 1560 n_varpool_nodes) 1561 { 1562 struct varpool_node *node = VEC_index (varpool_node_ptr, 1563 partition->varpool_set->nodes, 1564 n_varpool_nodes); 1565 varpool_node_set_remove (partition->varpool_set, node); 1566 node->aux = (void *)((size_t)node->aux - 1); 1567 } 1568 } 1569 1570 /* Return true if NODE should be partitioned. 1571 This means that partitioning algorithm should put NODE into one of partitions. 1572 This apply to most functions with bodies. Functions that are not partitions 1573 are put into every unit needing them. This is the case of i.e. COMDATs. */ 1574 1575 static bool 1576 partition_cgraph_node_p (struct cgraph_node *node) 1577 { 1578 /* We will get proper partition based on function they are inlined to. */ 1579 if (node->global.inlined_to) 1580 return false; 1581 /* Nodes without a body do not need partitioning. */ 1582 if (!node->analyzed) 1583 return false; 1584 /* Extern inlines and comdat are always only in partitions they are needed. */ 1585 if (DECL_EXTERNAL (node->decl) 1586 || (DECL_COMDAT (node->decl) 1587 && !cgraph_used_from_object_file_p (node))) 1588 return false; 1589 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl))) 1590 return false; 1591 return true; 1592 } 1593 1594 /* Return true if VNODE should be partitioned. 1595 This means that partitioning algorithm should put VNODE into one of partitions. */ 1596 1597 static bool 1598 partition_varpool_node_p (struct varpool_node *vnode) 1599 { 1600 if (vnode->alias || !vnode->needed) 1601 return false; 1602 /* Constant pool and comdat are always only in partitions they are needed. */ 1603 if (DECL_IN_CONSTANT_POOL (vnode->decl) 1604 || (DECL_COMDAT (vnode->decl) 1605 && !vnode->force_output 1606 && !varpool_used_from_object_file_p (vnode))) 1607 return false; 1608 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->decl))) 1609 return false; 1610 return true; 1611 } 1612 1613 /* Group cgrah nodes by input files. This is used mainly for testing 1614 right now. */ 1615 1616 static void 1617 lto_1_to_1_map (void) 1618 { 1619 struct cgraph_node *node; 1620 struct varpool_node *vnode; 1621 struct lto_file_decl_data *file_data; 1622 struct pointer_map_t *pmap; 1623 ltrans_partition partition; 1624 void **slot; 1625 int npartitions = 0; 1626 1627 timevar_push (TV_WHOPR_WPA); 1628 1629 pmap = pointer_map_create (); 1630 1631 for (node = cgraph_nodes; node; node = node->next) 1632 { 1633 if (!partition_cgraph_node_p (node) 1634 || node->aux) 1635 continue; 1636 1637 file_data = node->local.lto_file_data; 1638 1639 if (file_data) 1640 { 1641 slot = pointer_map_contains (pmap, file_data); 1642 if (slot) 1643 partition = (ltrans_partition) *slot; 1644 else 1645 { 1646 partition = new_partition (file_data->file_name); 1647 slot = pointer_map_insert (pmap, file_data); 1648 *slot = partition; 1649 npartitions++; 1650 } 1651 } 1652 else if (!file_data 1653 && VEC_length (ltrans_partition, ltrans_partitions)) 1654 partition = VEC_index (ltrans_partition, ltrans_partitions, 0); 1655 else 1656 { 1657 partition = new_partition (""); 1658 slot = pointer_map_insert (pmap, NULL); 1659 *slot = partition; 1660 npartitions++; 1661 } 1662 1663 add_cgraph_node_to_partition (partition, node); 1664 } 1665 1666 for (vnode = varpool_nodes; vnode; vnode = vnode->next) 1667 { 1668 if (!partition_varpool_node_p (vnode) 1669 || vnode->aux) 1670 continue; 1671 file_data = vnode->lto_file_data; 1672 slot = pointer_map_contains (pmap, file_data); 1673 if (slot) 1674 partition = (ltrans_partition) *slot; 1675 else 1676 { 1677 partition = new_partition (file_data->file_name); 1678 slot = pointer_map_insert (pmap, file_data); 1679 *slot = partition; 1680 npartitions++; 1681 } 1682 1683 add_varpool_node_to_partition (partition, vnode); 1684 } 1685 for (node = cgraph_nodes; node; node = node->next) 1686 node->aux = NULL; 1687 for (vnode = varpool_nodes; vnode; vnode = vnode->next) 1688 vnode->aux = NULL; 1689 1690 /* If the cgraph is empty, create one cgraph node set so that there is still 1691 an output file for any variables that need to be exported in a DSO. */ 1692 if (!npartitions) 1693 new_partition ("empty"); 1694 1695 pointer_map_destroy (pmap); 1696 1697 timevar_pop (TV_WHOPR_WPA); 1698 1699 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition, 1700 ltrans_partitions); 1701 } 1702 1703 /* Helper function for qsort; sort nodes by order. */ 1704 static int 1705 node_cmp (const void *pa, const void *pb) 1706 { 1707 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa; 1708 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb; 1709 return b->order - a->order; 1710 } 1711 1712 /* Helper function for qsort; sort nodes by order. */ 1713 static int 1714 varpool_node_cmp (const void *pa, const void *pb) 1715 { 1716 const struct varpool_node *a = *(const struct varpool_node * const *) pa; 1717 const struct varpool_node *b = *(const struct varpool_node * const *) pb; 1718 return b->order - a->order; 1719 } 1720 1721 /* Group cgraph nodes into equally-sized partitions. 1722 1723 The partitioning algorithm is simple: nodes are taken in predefined order. 1724 The order corresponds to the order we want functions to have in the final 1725 output. In the future this will be given by function reordering pass, but 1726 at the moment we use the topological order, which is a good approximation. 1727 1728 The goal is to partition this linear order into intervals (partitions) so 1729 that all the partitions have approximately the same size and the number of 1730 callgraph or IPA reference edges crossing boundaries is minimal. 1731 1732 This is a lot faster (O(n) in size of callgraph) than algorithms doing 1733 priority-based graph clustering that are generally O(n^2) and, since 1734 WHOPR is designed to make things go well across partitions, it leads 1735 to good results. 1736 1737 We compute the expected size of a partition as: 1738 1739 max (total_size / lto_partitions, min_partition_size) 1740 1741 We use dynamic expected size of partition so small programs are partitioned 1742 into enough partitions to allow use of multiple CPUs, while large programs 1743 are not partitioned too much. Creating too many partitions significantly 1744 increases the streaming overhead. 1745 1746 In the future, we would like to bound the maximal size of partitions so as 1747 to prevent the LTRANS stage from consuming too much memory. At the moment, 1748 however, the WPA stage is the most memory intensive for large benchmarks, 1749 since too many types and declarations are read into memory. 1750 1751 The function implements a simple greedy algorithm. Nodes are being added 1752 to the current partition until after 3/4 of the expected partition size is 1753 reached. Past this threshold, we keep track of boundary size (number of 1754 edges going to other partitions) and continue adding functions until after 1755 the current partition has grown to twice the expected partition size. Then 1756 the process is undone to the point where the minimal ratio of boundary size 1757 and in-partition calls was reached. */ 1758 1759 static void 1760 lto_balanced_map (void) 1761 { 1762 int n_nodes = 0; 1763 int n_varpool_nodes = 0, varpool_pos = 0; 1764 struct cgraph_node **postorder = 1765 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes); 1766 struct cgraph_node **order = XNEWVEC (struct cgraph_node *, cgraph_max_uid); 1767 struct varpool_node **varpool_order = NULL; 1768 int i, postorder_len; 1769 struct cgraph_node *node; 1770 int total_size = 0, best_total_size = 0; 1771 int partition_size; 1772 ltrans_partition partition; 1773 unsigned int last_visited_cgraph_node = 0, last_visited_varpool_node = 0; 1774 struct varpool_node *vnode; 1775 int cost = 0, internal = 0; 1776 int best_n_nodes = 0, best_n_varpool_nodes = 0, best_i = 0, best_cost = 1777 INT_MAX, best_internal = 0; 1778 int npartitions; 1779 int current_order = -1; 1780 1781 for (vnode = varpool_nodes; vnode; vnode = vnode->next) 1782 gcc_assert (!vnode->aux); 1783 /* Until we have better ordering facility, use toplogical order. 1784 Include only nodes we will partition and compute estimate of program 1785 size. Note that since nodes that are not partitioned might be put into 1786 multiple partitions, this is just an estimate of real size. This is why 1787 we keep partition_size updated after every partition is finalized. */ 1788 postorder_len = ipa_reverse_postorder (postorder); 1789 1790 for (i = 0; i < postorder_len; i++) 1791 { 1792 node = postorder[i]; 1793 if (partition_cgraph_node_p (node)) 1794 { 1795 order[n_nodes++] = node; 1796 total_size += inline_summary (node)->size; 1797 } 1798 } 1799 free (postorder); 1800 1801 if (!flag_toplevel_reorder) 1802 { 1803 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp); 1804 1805 for (vnode = varpool_nodes; vnode; vnode = vnode->next) 1806 if (partition_varpool_node_p (vnode)) 1807 n_varpool_nodes++; 1808 varpool_order = XNEWVEC (struct varpool_node *, n_varpool_nodes); 1809 1810 n_varpool_nodes = 0; 1811 for (vnode = varpool_nodes; vnode; vnode = vnode->next) 1812 if (partition_varpool_node_p (vnode)) 1813 varpool_order[n_varpool_nodes++] = vnode; 1814 qsort (varpool_order, n_varpool_nodes, sizeof (struct varpool_node *), 1815 varpool_node_cmp); 1816 } 1817 1818 /* Compute partition size and create the first partition. */ 1819 partition_size = total_size / PARAM_VALUE (PARAM_LTO_PARTITIONS); 1820 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE)) 1821 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE); 1822 npartitions = 1; 1823 partition = new_partition (""); 1824 if (cgraph_dump_file) 1825 fprintf (cgraph_dump_file, "Total unit size: %i, partition size: %i\n", 1826 total_size, partition_size); 1827 1828 for (i = 0; i < n_nodes; i++) 1829 { 1830 if (order[i]->aux) 1831 continue; 1832 1833 current_order = order[i]->order; 1834 1835 if (!flag_toplevel_reorder) 1836 while (varpool_pos < n_varpool_nodes && varpool_order[varpool_pos]->order < current_order) 1837 { 1838 if (!varpool_order[varpool_pos]->aux) 1839 add_varpool_node_to_partition (partition, varpool_order[varpool_pos]); 1840 varpool_pos++; 1841 } 1842 1843 add_cgraph_node_to_partition (partition, order[i]); 1844 total_size -= inline_summary (order[i])->size; 1845 1846 1847 /* Once we added a new node to the partition, we also want to add 1848 all referenced variables unless they was already added into some 1849 earlier partition. 1850 add_cgraph_node_to_partition adds possibly multiple nodes and 1851 variables that are needed to satisfy needs of ORDER[i]. 1852 We remember last visited cgraph and varpool node from last iteration 1853 of outer loop that allows us to process every new addition. 1854 1855 At the same time we compute size of the boundary into COST. Every 1856 callgraph or IPA reference edge leaving the partition contributes into 1857 COST. Every edge inside partition was earlier computed as one leaving 1858 it and thus we need to subtract it from COST. */ 1859 while (last_visited_cgraph_node < 1860 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes) 1861 || last_visited_varpool_node < VEC_length (varpool_node_ptr, 1862 partition->varpool_set-> 1863 nodes)) 1864 { 1865 struct ipa_ref_list *refs; 1866 int j; 1867 struct ipa_ref *ref; 1868 bool cgraph_p = false; 1869 1870 if (last_visited_cgraph_node < 1871 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes)) 1872 { 1873 struct cgraph_edge *edge; 1874 1875 cgraph_p = true; 1876 node = VEC_index (cgraph_node_ptr, partition->cgraph_set->nodes, 1877 last_visited_cgraph_node); 1878 refs = &node->ref_list; 1879 1880 last_visited_cgraph_node++; 1881 1882 gcc_assert (node->analyzed); 1883 1884 /* Compute boundary cost of callgraph edges. */ 1885 for (edge = node->callees; edge; edge = edge->next_callee) 1886 if (edge->callee->analyzed) 1887 { 1888 int edge_cost = edge->frequency; 1889 cgraph_node_set_iterator csi; 1890 1891 if (!edge_cost) 1892 edge_cost = 1; 1893 gcc_assert (edge_cost > 0); 1894 csi = cgraph_node_set_find (partition->cgraph_set, edge->callee); 1895 if (!csi_end_p (csi) 1896 && csi.index < last_visited_cgraph_node - 1) 1897 cost -= edge_cost, internal+= edge_cost; 1898 else 1899 cost += edge_cost; 1900 } 1901 for (edge = node->callers; edge; edge = edge->next_caller) 1902 { 1903 int edge_cost = edge->frequency; 1904 cgraph_node_set_iterator csi; 1905 1906 gcc_assert (edge->caller->analyzed); 1907 if (!edge_cost) 1908 edge_cost = 1; 1909 gcc_assert (edge_cost > 0); 1910 csi = cgraph_node_set_find (partition->cgraph_set, edge->caller); 1911 if (!csi_end_p (csi) 1912 && csi.index < last_visited_cgraph_node) 1913 cost -= edge_cost; 1914 else 1915 cost += edge_cost; 1916 } 1917 } 1918 else 1919 { 1920 refs = 1921 &VEC_index (varpool_node_ptr, partition->varpool_set->nodes, 1922 last_visited_varpool_node)->ref_list; 1923 last_visited_varpool_node++; 1924 } 1925 1926 /* Compute boundary cost of IPA REF edges and at the same time look into 1927 variables referenced from current partition and try to add them. */ 1928 for (j = 0; ipa_ref_list_reference_iterate (refs, j, ref); j++) 1929 if (ref->refered_type == IPA_REF_VARPOOL) 1930 { 1931 varpool_node_set_iterator vsi; 1932 1933 vnode = ipa_ref_varpool_node (ref); 1934 if (!vnode->finalized) 1935 continue; 1936 if (!vnode->aux && flag_toplevel_reorder 1937 && partition_varpool_node_p (vnode)) 1938 add_varpool_node_to_partition (partition, vnode); 1939 vsi = varpool_node_set_find (partition->varpool_set, vnode); 1940 if (!vsi_end_p (vsi) 1941 && vsi.index < last_visited_varpool_node - !cgraph_p) 1942 cost--, internal++; 1943 else 1944 cost++; 1945 } 1946 else 1947 { 1948 cgraph_node_set_iterator csi; 1949 1950 node = ipa_ref_node (ref); 1951 if (!node->analyzed) 1952 continue; 1953 csi = cgraph_node_set_find (partition->cgraph_set, node); 1954 if (!csi_end_p (csi) 1955 && csi.index < last_visited_cgraph_node - cgraph_p) 1956 cost--, internal++; 1957 else 1958 cost++; 1959 } 1960 for (j = 0; ipa_ref_list_refering_iterate (refs, j, ref); j++) 1961 if (ref->refering_type == IPA_REF_VARPOOL) 1962 { 1963 varpool_node_set_iterator vsi; 1964 1965 vnode = ipa_ref_refering_varpool_node (ref); 1966 gcc_assert (vnode->finalized); 1967 if (!vnode->aux && flag_toplevel_reorder 1968 && partition_varpool_node_p (vnode)) 1969 add_varpool_node_to_partition (partition, vnode); 1970 vsi = varpool_node_set_find (partition->varpool_set, vnode); 1971 if (!vsi_end_p (vsi) 1972 && vsi.index < last_visited_varpool_node) 1973 cost--; 1974 else 1975 cost++; 1976 } 1977 else 1978 { 1979 cgraph_node_set_iterator csi; 1980 1981 node = ipa_ref_refering_node (ref); 1982 gcc_assert (node->analyzed); 1983 csi = cgraph_node_set_find (partition->cgraph_set, node); 1984 if (!csi_end_p (csi) 1985 && csi.index < last_visited_cgraph_node) 1986 cost--; 1987 else 1988 cost++; 1989 } 1990 } 1991 1992 /* If the partition is large enough, start looking for smallest boundary cost. */ 1993 if (partition->insns < partition_size * 3 / 4 1994 || best_cost == INT_MAX 1995 || ((!cost 1996 || (best_internal * (HOST_WIDE_INT) cost 1997 > (internal * (HOST_WIDE_INT)best_cost))) 1998 && partition->insns < partition_size * 5 / 4)) 1999 { 2000 best_cost = cost; 2001 best_internal = internal; 2002 best_i = i; 2003 best_n_nodes = VEC_length (cgraph_node_ptr, 2004 partition->cgraph_set->nodes); 2005 best_n_varpool_nodes = VEC_length (varpool_node_ptr, 2006 partition->varpool_set->nodes); 2007 best_total_size = total_size; 2008 } 2009 if (cgraph_dump_file) 2010 fprintf (cgraph_dump_file, "Step %i: added %s/%i, size %i, cost %i/%i best %i/%i, step %i\n", i, 2011 cgraph_node_name (order[i]), order[i]->uid, partition->insns, cost, internal, 2012 best_cost, best_internal, best_i); 2013 /* Partition is too large, unwind into step when best cost was reached and 2014 start new partition. */ 2015 if (partition->insns > 2 * partition_size) 2016 { 2017 if (best_i != i) 2018 { 2019 if (cgraph_dump_file) 2020 fprintf (cgraph_dump_file, "Unwinding %i insertions to step %i\n", 2021 i - best_i, best_i); 2022 undo_partition (partition, best_n_nodes, best_n_varpool_nodes); 2023 } 2024 i = best_i; 2025 /* When we are finished, avoid creating empty partition. */ 2026 while (i < n_nodes - 1 && order[i + 1]->aux) 2027 i++; 2028 if (i == n_nodes - 1) 2029 break; 2030 partition = new_partition (""); 2031 last_visited_cgraph_node = 0; 2032 last_visited_varpool_node = 0; 2033 total_size = best_total_size; 2034 cost = 0; 2035 2036 if (cgraph_dump_file) 2037 fprintf (cgraph_dump_file, "New partition\n"); 2038 best_n_nodes = 0; 2039 best_n_varpool_nodes = 0; 2040 best_cost = INT_MAX; 2041 2042 /* Since the size of partitions is just approximate, update the size after 2043 we finished current one. */ 2044 if (npartitions < PARAM_VALUE (PARAM_LTO_PARTITIONS)) 2045 partition_size = total_size 2046 / (PARAM_VALUE (PARAM_LTO_PARTITIONS) - npartitions); 2047 else 2048 partition_size = INT_MAX; 2049 2050 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE)) 2051 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE); 2052 npartitions ++; 2053 } 2054 } 2055 2056 /* Varables that are not reachable from the code go into last partition. */ 2057 if (flag_toplevel_reorder) 2058 { 2059 for (vnode = varpool_nodes; vnode; vnode = vnode->next) 2060 if (partition_varpool_node_p (vnode) && !vnode->aux) 2061 add_varpool_node_to_partition (partition, vnode); 2062 } 2063 else 2064 { 2065 while (varpool_pos < n_varpool_nodes) 2066 { 2067 if (!varpool_order[varpool_pos]->aux) 2068 add_varpool_node_to_partition (partition, varpool_order[varpool_pos]); 2069 varpool_pos++; 2070 } 2071 free (varpool_order); 2072 } 2073 free (order); 2074 } 2075 2076 /* Promote variable VNODE to be static. */ 2077 2078 static bool 2079 promote_var (struct varpool_node *vnode) 2080 { 2081 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl)) 2082 return false; 2083 gcc_assert (flag_wpa); 2084 TREE_PUBLIC (vnode->decl) = 1; 2085 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN; 2086 DECL_VISIBILITY_SPECIFIED (vnode->decl) = true; 2087 if (cgraph_dump_file) 2088 fprintf (cgraph_dump_file, 2089 "Promoting var as hidden: %s\n", varpool_node_name (vnode)); 2090 return true; 2091 } 2092 2093 /* Promote function NODE to be static. */ 2094 2095 static bool 2096 promote_fn (struct cgraph_node *node) 2097 { 2098 gcc_assert (flag_wpa); 2099 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl)) 2100 return false; 2101 TREE_PUBLIC (node->decl) = 1; 2102 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN; 2103 DECL_VISIBILITY_SPECIFIED (node->decl) = true; 2104 if (cgraph_dump_file) 2105 fprintf (cgraph_dump_file, 2106 "Promoting function as hidden: %s/%i\n", 2107 cgraph_node_name (node), node->uid); 2108 return true; 2109 } 2110 2111 /* Find out all static decls that need to be promoted to global because 2112 of cross file sharing. This function must be run in the WPA mode after 2113 all inlinees are added. */ 2114 2115 static void 2116 lto_promote_cross_file_statics (void) 2117 { 2118 struct varpool_node *vnode; 2119 unsigned i, n_sets; 2120 cgraph_node_set set; 2121 varpool_node_set vset; 2122 cgraph_node_set_iterator csi; 2123 varpool_node_set_iterator vsi; 2124 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL; 2125 struct pointer_set_t *inserted = pointer_set_create (); 2126 2127 gcc_assert (flag_wpa); 2128 2129 n_sets = VEC_length (ltrans_partition, ltrans_partitions); 2130 for (i = 0; i < n_sets; i++) 2131 { 2132 ltrans_partition part 2133 = VEC_index (ltrans_partition, ltrans_partitions, i); 2134 set = part->cgraph_set; 2135 vset = part->varpool_set; 2136 2137 /* If node called or referred to from other partition, it needs to be 2138 globalized. */ 2139 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi)) 2140 { 2141 struct cgraph_node *node = csi_node (csi); 2142 if (node->local.externally_visible) 2143 continue; 2144 if (node->global.inlined_to) 2145 continue; 2146 if ((!DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl)) 2147 && (referenced_from_other_partition_p (&node->ref_list, set, vset) 2148 || reachable_from_other_partition_p (node, set))) 2149 promote_fn (node); 2150 } 2151 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi)) 2152 { 2153 vnode = vsi_node (vsi); 2154 /* Constant pool references use internal labels and thus can not 2155 be made global. It is sensible to keep those ltrans local to 2156 allow better optimization. */ 2157 if (!DECL_IN_CONSTANT_POOL (vnode->decl) && !DECL_COMDAT (vnode->decl) 2158 && !vnode->externally_visible && vnode->analyzed 2159 && referenced_from_other_partition_p (&vnode->ref_list, 2160 set, vset)) 2161 promote_var (vnode); 2162 } 2163 2164 /* We export the initializer of a read-only var into each partition 2165 referencing the var. Folding might take declarations from the 2166 initializer and use them, so everything referenced from the 2167 initializer can be accessed from this partition after folding. 2168 2169 This means that we need to promote all variables and functions 2170 referenced from all initializers of read-only vars referenced 2171 from this partition that are not in this partition. This needs 2172 to be done recursively. */ 2173 for (vnode = varpool_nodes; vnode; vnode = vnode->next) 2174 if (const_value_known_p (vnode->decl) 2175 && DECL_INITIAL (vnode->decl) 2176 && !varpool_node_in_set_p (vnode, vset) 2177 && referenced_from_this_partition_p (&vnode->ref_list, set, vset) 2178 && !pointer_set_insert (inserted, vnode)) 2179 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode); 2180 2181 while (!VEC_empty (varpool_node_ptr, promoted_initializers)) 2182 { 2183 int i; 2184 struct ipa_ref *ref; 2185 2186 vnode = VEC_pop (varpool_node_ptr, promoted_initializers); 2187 for (i = 0; 2188 ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref); 2189 i++) 2190 { 2191 if (ref->refered_type == IPA_REF_CGRAPH) 2192 { 2193 struct cgraph_node *n = ipa_ref_node (ref); 2194 gcc_assert (!n->global.inlined_to); 2195 if (!n->local.externally_visible 2196 && !cgraph_node_in_set_p (n, set)) 2197 promote_fn (n); 2198 } 2199 else 2200 { 2201 struct varpool_node *v = ipa_ref_varpool_node (ref); 2202 if (varpool_node_in_set_p (v, vset)) 2203 continue; 2204 2205 /* Constant pool references use internal labels and thus 2206 cannot be made global. It is sensible to keep those 2207 ltrans local to allow better optimization. */ 2208 if (DECL_IN_CONSTANT_POOL (v->decl)) 2209 { 2210 if (!pointer_set_insert (inserted, vnode)) 2211 VEC_safe_push (varpool_node_ptr, heap, 2212 promoted_initializers, v); 2213 } 2214 else if (!v->externally_visible && v->analyzed) 2215 { 2216 if (promote_var (v) 2217 && DECL_INITIAL (v->decl) 2218 && const_value_known_p (v->decl) 2219 && !pointer_set_insert (inserted, vnode)) 2220 VEC_safe_push (varpool_node_ptr, heap, 2221 promoted_initializers, v); 2222 } 2223 } 2224 } 2225 } 2226 } 2227 pointer_set_destroy (inserted); 2228 } 2229 2230 static lto_file *current_lto_file; 2231 2232 /* Helper for qsort; compare partitions and return one with smaller size. 2233 We sort from greatest to smallest so parallel build doesn't stale on the 2234 longest compilation being executed too late. */ 2235 2236 static int 2237 cmp_partitions_size (const void *a, const void *b) 2238 { 2239 const struct ltrans_partition_def *pa 2240 = *(struct ltrans_partition_def *const *)a; 2241 const struct ltrans_partition_def *pb 2242 = *(struct ltrans_partition_def *const *)b; 2243 return pb->insns - pa->insns; 2244 } 2245 2246 /* Helper for qsort; compare partitions and return one with smaller order. */ 2247 2248 static int 2249 cmp_partitions_order (const void *a, const void *b) 2250 { 2251 const struct ltrans_partition_def *pa 2252 = *(struct ltrans_partition_def *const *)a; 2253 const struct ltrans_partition_def *pb 2254 = *(struct ltrans_partition_def *const *)b; 2255 int ordera = -1, orderb = -1; 2256 2257 if (VEC_length (cgraph_node_ptr, pa->cgraph_set->nodes)) 2258 ordera = VEC_index (cgraph_node_ptr, pa->cgraph_set->nodes, 0)->order; 2259 else if (VEC_length (varpool_node_ptr, pa->varpool_set->nodes)) 2260 ordera = VEC_index (varpool_node_ptr, pa->varpool_set->nodes, 0)->order; 2261 if (VEC_length (cgraph_node_ptr, pb->cgraph_set->nodes)) 2262 orderb = VEC_index (cgraph_node_ptr, pb->cgraph_set->nodes, 0)->order; 2263 else if (VEC_length (varpool_node_ptr, pb->varpool_set->nodes)) 2264 orderb = VEC_index (varpool_node_ptr, pb->varpool_set->nodes, 0)->order; 2265 return orderb - ordera; 2266 } 2267 2268 /* Write all output files in WPA mode and the file with the list of 2269 LTRANS units. */ 2270 2271 static void 2272 lto_wpa_write_files (void) 2273 { 2274 unsigned i, n_sets; 2275 lto_file *file; 2276 cgraph_node_set set; 2277 varpool_node_set vset; 2278 ltrans_partition part; 2279 FILE *ltrans_output_list_stream; 2280 char *temp_filename; 2281 size_t blen; 2282 2283 /* Open the LTRANS output list. */ 2284 if (!ltrans_output_list) 2285 fatal_error ("no LTRANS output list filename provided"); 2286 ltrans_output_list_stream = fopen (ltrans_output_list, "w"); 2287 if (ltrans_output_list_stream == NULL) 2288 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list); 2289 2290 timevar_push (TV_WHOPR_WPA); 2291 2292 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part) 2293 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr, 2294 part->cgraph_set->nodes); 2295 2296 /* Find out statics that need to be promoted 2297 to globals with hidden visibility because they are accessed from multiple 2298 partitions. */ 2299 lto_promote_cross_file_statics (); 2300 2301 timevar_pop (TV_WHOPR_WPA); 2302 2303 timevar_push (TV_WHOPR_WPA_IO); 2304 2305 /* Generate a prefix for the LTRANS unit files. */ 2306 blen = strlen (ltrans_output_list); 2307 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o")); 2308 strcpy (temp_filename, ltrans_output_list); 2309 if (blen > sizeof (".out") 2310 && strcmp (temp_filename + blen - sizeof (".out") + 1, 2311 ".out") == 0) 2312 temp_filename[blen - sizeof (".out") + 1] = '\0'; 2313 blen = strlen (temp_filename); 2314 2315 n_sets = VEC_length (ltrans_partition, ltrans_partitions); 2316 2317 /* Sort partitions by size so small ones are compiled last. 2318 FIXME: Even when not reordering we may want to output one list for parallel make 2319 and other for final link command. */ 2320 VEC_qsort (ltrans_partition, ltrans_partitions, 2321 flag_toplevel_reorder ? cmp_partitions_size : cmp_partitions_order); 2322 for (i = 0; i < n_sets; i++) 2323 { 2324 size_t len; 2325 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i); 2326 2327 set = part->cgraph_set; 2328 vset = part->varpool_set; 2329 2330 /* Write all the nodes in SET. */ 2331 sprintf (temp_filename + blen, "%u.o", i); 2332 file = lto_obj_file_open (temp_filename, true); 2333 if (!file) 2334 fatal_error ("lto_obj_file_open() failed"); 2335 2336 if (!quiet_flag) 2337 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns); 2338 if (cgraph_dump_file) 2339 { 2340 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n", 2341 part->name, temp_filename, part->insns); 2342 fprintf (cgraph_dump_file, "cgraph nodes:"); 2343 dump_cgraph_node_set (cgraph_dump_file, set); 2344 fprintf (cgraph_dump_file, "varpool nodes:"); 2345 dump_varpool_node_set (cgraph_dump_file, vset); 2346 } 2347 gcc_checking_assert (cgraph_node_set_nonempty_p (set) 2348 || varpool_node_set_nonempty_p (vset) || !i); 2349 2350 lto_set_current_out_file (file); 2351 2352 ipa_write_optimization_summaries (set, vset); 2353 2354 lto_set_current_out_file (NULL); 2355 lto_obj_file_close (file); 2356 2357 len = strlen (temp_filename); 2358 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len 2359 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1) 2360 fatal_error ("writing to LTRANS output list %s: %m", 2361 ltrans_output_list); 2362 } 2363 2364 lto_stats.num_output_files += n_sets; 2365 2366 /* Close the LTRANS output list. */ 2367 if (fclose (ltrans_output_list_stream)) 2368 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list); 2369 2370 free_ltrans_partitions(); 2371 2372 timevar_pop (TV_WHOPR_WPA_IO); 2373 } 2374 2375 2376 /* If TT is a variable or function decl replace it with its 2377 prevailing variant. */ 2378 #define LTO_SET_PREVAIL(tt) \ 2379 do {\ 2380 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \ 2381 tt = lto_symtab_prevailing_decl (tt); \ 2382 } while (0) 2383 2384 /* Ensure that TT isn't a replacable var of function decl. */ 2385 #define LTO_NO_PREVAIL(tt) \ 2386 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt)) 2387 2388 /* Given a tree T replace all fields referring to variables or functions 2389 with their prevailing variant. */ 2390 static void 2391 lto_fixup_prevailing_decls (tree t) 2392 { 2393 enum tree_code code = TREE_CODE (t); 2394 LTO_NO_PREVAIL (TREE_TYPE (t)); 2395 if (CODE_CONTAINS_STRUCT (code, TS_COMMON)) 2396 LTO_NO_PREVAIL (TREE_CHAIN (t)); 2397 if (DECL_P (t)) 2398 { 2399 LTO_NO_PREVAIL (DECL_NAME (t)); 2400 LTO_SET_PREVAIL (DECL_CONTEXT (t)); 2401 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON)) 2402 { 2403 LTO_SET_PREVAIL (DECL_SIZE (t)); 2404 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t)); 2405 LTO_SET_PREVAIL (DECL_INITIAL (t)); 2406 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t)); 2407 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t)); 2408 } 2409 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS)) 2410 { 2411 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name); 2412 LTO_NO_PREVAIL (DECL_SECTION_NAME (t)); 2413 } 2414 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON)) 2415 { 2416 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t)); 2417 LTO_NO_PREVAIL (DECL_RESULT_FLD (t)); 2418 LTO_NO_PREVAIL (DECL_VINDEX (t)); 2419 } 2420 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL)) 2421 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t)); 2422 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL)) 2423 { 2424 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t)); 2425 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t)); 2426 LTO_NO_PREVAIL (DECL_QUALIFIER (t)); 2427 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t)); 2428 LTO_NO_PREVAIL (DECL_FCONTEXT (t)); 2429 } 2430 } 2431 else if (TYPE_P (t)) 2432 { 2433 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t)); 2434 LTO_SET_PREVAIL (TYPE_SIZE (t)); 2435 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t)); 2436 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t)); 2437 LTO_NO_PREVAIL (TYPE_NAME (t)); 2438 2439 LTO_SET_PREVAIL (TYPE_MINVAL (t)); 2440 LTO_SET_PREVAIL (TYPE_MAXVAL (t)); 2441 LTO_SET_PREVAIL (t->type_non_common.binfo); 2442 2443 LTO_SET_PREVAIL (TYPE_CONTEXT (t)); 2444 2445 LTO_NO_PREVAIL (TYPE_CANONICAL (t)); 2446 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t)); 2447 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t)); 2448 } 2449 else if (EXPR_P (t)) 2450 { 2451 int i; 2452 LTO_NO_PREVAIL (t->exp.block); 2453 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i) 2454 LTO_SET_PREVAIL (TREE_OPERAND (t, i)); 2455 } 2456 else 2457 { 2458 switch (code) 2459 { 2460 case TREE_LIST: 2461 LTO_SET_PREVAIL (TREE_VALUE (t)); 2462 LTO_SET_PREVAIL (TREE_PURPOSE (t)); 2463 break; 2464 default: 2465 gcc_unreachable (); 2466 } 2467 } 2468 } 2469 #undef LTO_SET_PREVAIL 2470 #undef LTO_NO_PREVAIL 2471 2472 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE, 2473 replaces var and function decls with the corresponding prevailing def. */ 2474 2475 static void 2476 lto_fixup_state (struct lto_in_decl_state *state) 2477 { 2478 unsigned i, si; 2479 struct lto_tree_ref_table *table; 2480 2481 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs, 2482 we still need to walk from all DECLs to find the reachable 2483 FUNCTION_DECLs and VAR_DECLs. */ 2484 for (si = 0; si < LTO_N_DECL_STREAMS; si++) 2485 { 2486 table = &state->streams[si]; 2487 for (i = 0; i < table->size; i++) 2488 { 2489 tree *tp = table->trees + i; 2490 if (VAR_OR_FUNCTION_DECL_P (*tp)) 2491 *tp = lto_symtab_prevailing_decl (*tp); 2492 } 2493 } 2494 } 2495 2496 /* A callback of htab_traverse. Just extracts a state from SLOT 2497 and calls lto_fixup_state. */ 2498 2499 static int 2500 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED) 2501 { 2502 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot; 2503 lto_fixup_state (state); 2504 return 1; 2505 } 2506 2507 /* Fix the decls from all FILES. Replaces each decl with the corresponding 2508 prevailing one. */ 2509 2510 static void 2511 lto_fixup_decls (struct lto_file_decl_data **files) 2512 { 2513 unsigned int i; 2514 htab_iterator hi; 2515 tree t; 2516 2517 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi) 2518 lto_fixup_prevailing_decls (t); 2519 2520 for (i = 0; files[i]; i++) 2521 { 2522 struct lto_file_decl_data *file = files[i]; 2523 struct lto_in_decl_state *state = file->global_decl_state; 2524 lto_fixup_state (state); 2525 2526 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL); 2527 } 2528 } 2529 2530 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data; 2531 2532 /* Turn file datas for sub files into a single array, so that they look 2533 like separate files for further passes. */ 2534 2535 static void 2536 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix) 2537 { 2538 struct lto_file_decl_data *n, *next; 2539 int i, k; 2540 2541 lto_stats.num_input_files = count; 2542 all_file_decl_data 2543 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1); 2544 /* Set the hooks so that all of the ipa passes can read in their data. */ 2545 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data); 2546 for (i = 0, k = 0; i < last_file_ix; i++) 2547 { 2548 for (n = orig[i]; n != NULL; n = next) 2549 { 2550 all_file_decl_data[k++] = n; 2551 next = n->next; 2552 n->next = NULL; 2553 } 2554 } 2555 all_file_decl_data[k] = NULL; 2556 gcc_assert (k == count); 2557 } 2558 2559 /* Input file data before flattening (i.e. splitting them to subfiles to support 2560 incremental linking. */ 2561 static int real_file_count; 2562 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data; 2563 2564 /* Read all the symbols from the input files FNAMES. NFILES is the 2565 number of files requested in the command line. Instantiate a 2566 global call graph by aggregating all the sub-graphs found in each 2567 file. */ 2568 2569 static void 2570 read_cgraph_and_symbols (unsigned nfiles, const char **fnames) 2571 { 2572 unsigned int i, last_file_ix; 2573 FILE *resolution; 2574 struct cgraph_node *node; 2575 int count = 0; 2576 struct lto_file_decl_data **decl_data; 2577 2578 init_cgraph (); 2579 2580 timevar_push (TV_IPA_LTO_DECL_IN); 2581 2582 real_file_decl_data 2583 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1); 2584 real_file_count = nfiles; 2585 2586 /* Read the resolution file. */ 2587 resolution = NULL; 2588 if (resolution_file_name) 2589 { 2590 int t; 2591 unsigned num_objects; 2592 2593 resolution = fopen (resolution_file_name, "r"); 2594 if (resolution == NULL) 2595 fatal_error ("could not open symbol resolution file: %m"); 2596 2597 t = fscanf (resolution, "%u", &num_objects); 2598 gcc_assert (t == 1); 2599 2600 /* True, since the plugin splits the archives. */ 2601 gcc_assert (num_objects == nfiles); 2602 } 2603 2604 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer, 2605 NULL); 2606 2607 if (!quiet_flag) 2608 fprintf (stderr, "Reading object files:"); 2609 2610 /* Read all of the object files specified on the command line. */ 2611 for (i = 0, last_file_ix = 0; i < nfiles; ++i) 2612 { 2613 struct lto_file_decl_data *file_data = NULL; 2614 if (!quiet_flag) 2615 { 2616 fprintf (stderr, " %s", fnames[i]); 2617 fflush (stderr); 2618 } 2619 2620 current_lto_file = lto_obj_file_open (fnames[i], false); 2621 if (!current_lto_file) 2622 break; 2623 2624 file_data = lto_file_read (current_lto_file, resolution, &count); 2625 if (!file_data) 2626 { 2627 lto_obj_file_close (current_lto_file); 2628 current_lto_file = NULL; 2629 break; 2630 } 2631 2632 decl_data[last_file_ix++] = file_data; 2633 2634 lto_obj_file_close (current_lto_file); 2635 current_lto_file = NULL; 2636 ggc_collect (); 2637 } 2638 2639 lto_flatten_files (decl_data, count, last_file_ix); 2640 lto_stats.num_input_files = count; 2641 ggc_free(decl_data); 2642 real_file_decl_data = NULL; 2643 2644 if (resolution_file_name) 2645 fclose (resolution); 2646 2647 /* Set the hooks so that all of the ipa passes can read in their data. */ 2648 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data); 2649 2650 timevar_pop (TV_IPA_LTO_DECL_IN); 2651 2652 if (!quiet_flag) 2653 fprintf (stderr, "\nReading the callgraph\n"); 2654 2655 timevar_push (TV_IPA_LTO_CGRAPH_IO); 2656 /* Read the callgraph. */ 2657 input_cgraph (); 2658 timevar_pop (TV_IPA_LTO_CGRAPH_IO); 2659 2660 if (!quiet_flag) 2661 fprintf (stderr, "Merging declarations\n"); 2662 2663 timevar_push (TV_IPA_LTO_DECL_MERGE); 2664 /* Merge global decls. */ 2665 lto_symtab_merge_decls (); 2666 2667 /* If there were errors during symbol merging bail out, we have no 2668 good way to recover here. */ 2669 if (seen_error ()) 2670 fatal_error ("errors during merging of translation units"); 2671 2672 /* Fixup all decls and types and free the type hash tables. */ 2673 lto_fixup_decls (all_file_decl_data); 2674 htab_delete (tree_with_vars); 2675 tree_with_vars = NULL; 2676 free_gimple_type_tables (); 2677 ggc_collect (); 2678 2679 timevar_pop (TV_IPA_LTO_DECL_MERGE); 2680 /* Each pass will set the appropriate timer. */ 2681 2682 if (!quiet_flag) 2683 fprintf (stderr, "Reading summaries\n"); 2684 2685 /* Read the IPA summary data. */ 2686 if (flag_ltrans) 2687 ipa_read_optimization_summaries (); 2688 else 2689 ipa_read_summaries (); 2690 2691 /* Finally merge the cgraph according to the decl merging decisions. */ 2692 timevar_push (TV_IPA_LTO_CGRAPH_MERGE); 2693 if (cgraph_dump_file) 2694 { 2695 fprintf (cgraph_dump_file, "Before merging:\n"); 2696 dump_cgraph (cgraph_dump_file); 2697 dump_varpool (cgraph_dump_file); 2698 } 2699 lto_symtab_merge_cgraph_nodes (); 2700 ggc_collect (); 2701 2702 if (flag_ltrans) 2703 for (node = cgraph_nodes; node; node = node->next) 2704 { 2705 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization 2706 summaries computed and needs to apply changes. At the moment WHOPR only 2707 supports inlining, so we can push it here by hand. In future we need to stream 2708 this field into ltrans compilation. */ 2709 if (node->analyzed) 2710 VEC_safe_push (ipa_opt_pass, heap, 2711 node->ipa_transforms_to_apply, 2712 (ipa_opt_pass)&pass_ipa_inline); 2713 } 2714 lto_symtab_free (); 2715 2716 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE); 2717 2718 timevar_push (TV_IPA_LTO_DECL_INIT_IO); 2719 2720 /* FIXME lto. This loop needs to be changed to use the pass manager to 2721 call the ipa passes directly. */ 2722 if (!seen_error ()) 2723 for (i = 0; i < last_file_ix; i++) 2724 { 2725 struct lto_file_decl_data *file_data = all_file_decl_data [i]; 2726 lto_materialize_constructors_and_inits (file_data); 2727 } 2728 2729 /* Indicate that the cgraph is built and ready. */ 2730 cgraph_function_flags_ready = true; 2731 2732 timevar_pop (TV_IPA_LTO_DECL_INIT_IO); 2733 ggc_free (all_file_decl_data); 2734 all_file_decl_data = NULL; 2735 } 2736 2737 2738 /* Materialize all the bodies for all the nodes in the callgraph. */ 2739 2740 static void 2741 materialize_cgraph (void) 2742 { 2743 tree decl; 2744 struct cgraph_node *node; 2745 unsigned i; 2746 timevar_id_t lto_timer; 2747 2748 if (!quiet_flag) 2749 fprintf (stderr, 2750 flag_wpa ? "Materializing decls:" : "Reading function bodies:"); 2751 2752 2753 /* Now that we have input the cgraph, we need to clear all of the aux 2754 nodes and read the functions if we are not running in WPA mode. */ 2755 timevar_push (TV_IPA_LTO_GIMPLE_IN); 2756 2757 for (node = cgraph_nodes; node; node = node->next) 2758 { 2759 if (node->local.lto_file_data) 2760 { 2761 lto_materialize_function (node); 2762 lto_stats.num_input_cgraph_nodes++; 2763 } 2764 } 2765 2766 timevar_pop (TV_IPA_LTO_GIMPLE_IN); 2767 2768 /* Start the appropriate timer depending on the mode that we are 2769 operating in. */ 2770 lto_timer = (flag_wpa) ? TV_WHOPR_WPA 2771 : (flag_ltrans) ? TV_WHOPR_LTRANS 2772 : TV_LTO; 2773 timevar_push (lto_timer); 2774 2775 current_function_decl = NULL; 2776 set_cfun (NULL); 2777 2778 /* Inform the middle end about the global variables we have seen. */ 2779 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl) 2780 rest_of_decl_compilation (decl, 1, 0); 2781 2782 if (!quiet_flag) 2783 fprintf (stderr, "\n"); 2784 2785 timevar_pop (lto_timer); 2786 } 2787 2788 2789 /* Perform whole program analysis (WPA) on the callgraph and write out the 2790 optimization plan. */ 2791 2792 static void 2793 do_whole_program_analysis (void) 2794 { 2795 /* Note that since we are in WPA mode, materialize_cgraph will not 2796 actually read in all the function bodies. It only materializes 2797 the decls and cgraph nodes so that analysis can be performed. */ 2798 materialize_cgraph (); 2799 2800 /* Reading in the cgraph uses different timers, start timing WPA now. */ 2801 timevar_push (TV_WHOPR_WPA); 2802 2803 if (pre_ipa_mem_report) 2804 { 2805 fprintf (stderr, "Memory consumption before IPA\n"); 2806 dump_memory_report (false); 2807 } 2808 2809 cgraph_function_flags_ready = true; 2810 2811 if (cgraph_dump_file) 2812 { 2813 dump_cgraph (cgraph_dump_file); 2814 dump_varpool (cgraph_dump_file); 2815 } 2816 bitmap_obstack_initialize (NULL); 2817 cgraph_state = CGRAPH_STATE_IPA_SSA; 2818 2819 execute_ipa_pass_list (all_regular_ipa_passes); 2820 2821 if (cgraph_dump_file) 2822 { 2823 fprintf (cgraph_dump_file, "Optimized "); 2824 dump_cgraph (cgraph_dump_file); 2825 dump_varpool (cgraph_dump_file); 2826 } 2827 verify_cgraph (); 2828 bitmap_obstack_release (NULL); 2829 2830 /* We are about to launch the final LTRANS phase, stop the WPA timer. */ 2831 timevar_pop (TV_WHOPR_WPA); 2832 2833 if (flag_lto_partition_1to1) 2834 lto_1_to_1_map (); 2835 else 2836 lto_balanced_map (); 2837 2838 if (!quiet_flag) 2839 { 2840 fprintf (stderr, "\nStreaming out"); 2841 fflush (stderr); 2842 } 2843 lto_wpa_write_files (); 2844 ggc_collect (); 2845 if (!quiet_flag) 2846 fprintf (stderr, "\n"); 2847 2848 if (post_ipa_mem_report) 2849 { 2850 fprintf (stderr, "Memory consumption after IPA\n"); 2851 dump_memory_report (false); 2852 } 2853 2854 /* Show the LTO report before launching LTRANS. */ 2855 if (flag_lto_report) 2856 print_lto_report (); 2857 } 2858 2859 2860 static GTY(()) tree lto_eh_personality_decl; 2861 2862 /* Return the LTO personality function decl. */ 2863 2864 tree 2865 lto_eh_personality (void) 2866 { 2867 if (!lto_eh_personality_decl) 2868 { 2869 /* Use the first personality DECL for our personality if we don't 2870 support multiple ones. This ensures that we don't artificially 2871 create the need for them in a single-language program. */ 2872 if (first_personality_decl && !dwarf2out_do_cfi_asm ()) 2873 lto_eh_personality_decl = first_personality_decl; 2874 else 2875 lto_eh_personality_decl = lhd_gcc_personality (); 2876 } 2877 2878 return lto_eh_personality_decl; 2879 } 2880 2881 /* Set the process name based on the LTO mode. */ 2882 2883 static void 2884 lto_process_name (void) 2885 { 2886 if (flag_lto) 2887 setproctitle ("lto1-lto"); 2888 if (flag_wpa) 2889 setproctitle ("lto1-wpa"); 2890 if (flag_ltrans) 2891 setproctitle ("lto1-ltrans"); 2892 } 2893 2894 2895 /* Initialize the LTO front end. */ 2896 2897 static void 2898 lto_init (void) 2899 { 2900 lto_process_name (); 2901 lto_streamer_hooks_init (); 2902 lto_reader_init (); 2903 lto_set_in_hooks (NULL, get_section_data, free_section_data); 2904 memset (<o_stats, 0, sizeof (lto_stats)); 2905 bitmap_obstack_initialize (NULL); 2906 gimple_register_cfg_hooks (); 2907 } 2908 2909 2910 /* Main entry point for the GIMPLE front end. This front end has 2911 three main personalities: 2912 2913 - LTO (-flto). All the object files on the command line are 2914 loaded in memory and processed as a single translation unit. 2915 This is the traditional link-time optimization behavior. 2916 2917 - WPA (-fwpa). Only the callgraph and summary information for 2918 files in the command file are loaded. A single callgraph 2919 (without function bodies) is instantiated for the whole set of 2920 files. IPA passes are only allowed to analyze the call graph 2921 and make transformation decisions. The callgraph is 2922 partitioned, each partition is written to a new object file 2923 together with the transformation decisions. 2924 2925 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA 2926 summary files from running again. Since WPA computed summary 2927 information and decided what transformations to apply, LTRANS 2928 simply applies them. */ 2929 2930 void 2931 lto_main (void) 2932 { 2933 /* Initialize the LTO front end. */ 2934 lto_init (); 2935 2936 /* Read all the symbols and call graph from all the files in the 2937 command line. */ 2938 read_cgraph_and_symbols (num_in_fnames, in_fnames); 2939 2940 if (!seen_error ()) 2941 { 2942 /* If WPA is enabled analyze the whole call graph and create an 2943 optimization plan. Otherwise, read in all the function 2944 bodies and continue with optimization. */ 2945 if (flag_wpa) 2946 do_whole_program_analysis (); 2947 else 2948 { 2949 materialize_cgraph (); 2950 2951 /* Let the middle end know that we have read and merged all of 2952 the input files. */ 2953 cgraph_optimize (); 2954 2955 /* FIXME lto, if the processes spawned by WPA fail, we miss 2956 the chance to print WPA's report, so WPA will call 2957 print_lto_report before launching LTRANS. If LTRANS was 2958 launched directly by the driver we would not need to do 2959 this. */ 2960 if (flag_lto_report) 2961 print_lto_report (); 2962 } 2963 } 2964 } 2965 2966 #include "gt-lto-lto.h" 2967