comparison gcc/lto-streamer.c @ 69:1b10fe6932e1

merge 69
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sun, 21 Aug 2011 07:53:12 +0900
parents f6334be47118
children 04ced10e8804
comparison
equal deleted inserted replaced
66:b362627d71ba 69:1b10fe6932e1
1 /* Miscellaneous utilities for GIMPLE streaming. Things that are used 1 /* Miscellaneous utilities for GIMPLE streaming. Things that are used
2 in both input and output are here. 2 in both input and output are here.
3 3
4 Copyright 2009 Free Software Foundation, Inc. 4 Copyright 2009, 2010 Free Software Foundation, Inc.
5 Contributed by Doug Kwan <dougkwan@google.com> 5 Contributed by Doug Kwan <dougkwan@google.com>
6 6
7 This file is part of GCC. 7 This file is part of GCC.
8 8
9 GCC is free software; you can redistribute it and/or modify it under 9 GCC is free software; you can redistribute it and/or modify it under
27 #include "toplev.h" 27 #include "toplev.h"
28 #include "flags.h" 28 #include "flags.h"
29 #include "tree.h" 29 #include "tree.h"
30 #include "gimple.h" 30 #include "gimple.h"
31 #include "tree-flow.h" 31 #include "tree-flow.h"
32 #include "diagnostic.h" 32 #include "diagnostic-core.h"
33 #include "bitmap.h" 33 #include "bitmap.h"
34 #include "vec.h" 34 #include "vec.h"
35 #include "lto-streamer.h" 35 #include "lto-streamer.h"
36 36
37 /* Statistics gathered during LTO, WPA and LTRANS. */ 37 /* Statistics gathered during LTO, WPA and LTRANS. */
131 BITMAP_FREE (b); 131 BITMAP_FREE (b);
132 } 132 }
133 133
134 134
135 /* Get a section name for a particular type or name. The NAME field 135 /* Get a section name for a particular type or name. The NAME field
136 is only used if SECTION_TYPE is LTO_section_function_body or 136 is only used if SECTION_TYPE is LTO_section_function_body. For all
137 LTO_static_initializer. For all others it is ignored. The callee 137 others it is ignored. The callee of this function is responsible
138 of this function is responcible to free the returned name. */ 138 to free the returned name. */
139 139
140 char * 140 char *
141 lto_get_section_name (int section_type, const char *name) 141 lto_get_section_name (int section_type, const char *name, struct lto_file_decl_data *f)
142 { 142 {
143 switch (section_type) 143 const char *add;
144 { 144 char post[32];
145 case LTO_section_function_body: 145 const char *sep;
146 return concat (LTO_SECTION_NAME_PREFIX, name, NULL); 146
147 147 if (section_type == LTO_section_function_body)
148 case LTO_section_static_initializer: 148 {
149 return concat (LTO_SECTION_NAME_PREFIX, ".statics", NULL); 149 gcc_assert (name != NULL);
150 150 if (name[0] == '*')
151 case LTO_section_symtab: 151 name++;
152 return concat (LTO_SECTION_NAME_PREFIX, ".symtab", NULL); 152 add = name;
153 153 sep = "";
154 case LTO_section_decls: 154 }
155 return concat (LTO_SECTION_NAME_PREFIX, ".decls", NULL); 155 else if (section_type < LTO_N_SECTION_TYPES)
156 156 {
157 case LTO_section_cgraph: 157 add = lto_section_name[section_type];
158 return concat (LTO_SECTION_NAME_PREFIX, ".cgraph", NULL); 158 sep = ".";
159 159 }
160 case LTO_section_jump_functions: 160 else
161 return concat (LTO_SECTION_NAME_PREFIX, ".jmpfuncs", NULL); 161 internal_error ("bytecode stream: unexpected LTO section %s", name);
162 162
163 case LTO_section_ipa_pure_const: 163 /* Make the section name unique so that ld -r combining sections
164 return concat (LTO_SECTION_NAME_PREFIX, ".pureconst", NULL); 164 doesn't confuse the reader with merged sections.
165 165
166 case LTO_section_ipa_reference: 166 For options don't add a ID, the option reader cannot deal with them
167 return concat (LTO_SECTION_NAME_PREFIX, ".reference", NULL); 167 and merging should be ok here.
168 168
169 case LTO_section_wpa_fixup: 169 XXX: use crc64 to minimize collisions? */
170 return concat (LTO_SECTION_NAME_PREFIX, ".wpa_fixup", NULL); 170 if (section_type == LTO_section_opts)
171 171 strcpy (post, "");
172 case LTO_section_opts: 172 else
173 return concat (LTO_SECTION_NAME_PREFIX, ".opts", NULL); 173 sprintf (post, ".%x", f ? f->id : crc32_string(0, get_random_seed (false)));
174 174 return concat (LTO_SECTION_NAME_PREFIX, sep, add, post, NULL);
175 default:
176 internal_error ("bytecode stream: unexpected LTO section %s", name);
177 }
178 } 175 }
179 176
180 177
181 /* Show various memory usage statistics related to LTO. */ 178 /* Show various memory usage statistics related to LTO. */
182 179
253 250
254 for (i = 0; i < LTO_N_SECTION_TYPES; i++) 251 for (i = 0; i < LTO_N_SECTION_TYPES; i++)
255 fprintf (stderr, "[%s] Size of mmap'd section %s: " 252 fprintf (stderr, "[%s] Size of mmap'd section %s: "
256 HOST_WIDE_INT_PRINT_UNSIGNED " bytes\n", s, 253 HOST_WIDE_INT_PRINT_UNSIGNED " bytes\n", s,
257 lto_section_name[i], lto_stats.section_size[i]); 254 lto_section_name[i], lto_stats.section_size[i]);
258 }
259
260
261 /* Create a new bitpack. */
262
263 struct bitpack_d *
264 bitpack_create (void)
265 {
266 return XCNEW (struct bitpack_d);
267 }
268
269
270 /* Free the memory used by bitpack BP. */
271
272 void
273 bitpack_delete (struct bitpack_d *bp)
274 {
275 VEC_free (bitpack_word_t, heap, bp->values);
276 free (bp);
277 }
278
279
280 /* Return an index to the word in bitpack BP that contains the
281 next NBITS. */
282
283 static inline unsigned
284 bp_get_next_word (struct bitpack_d *bp, unsigned nbits)
285 {
286 unsigned last, ix;
287
288 /* In principle, the next word to use is determined by the
289 number of bits already processed in BP. */
290 ix = bp->num_bits / BITS_PER_BITPACK_WORD;
291
292 /* All the encoded bit patterns in BP are contiguous, therefore if
293 the next NBITS would straddle over two different words, move the
294 index to the next word and update the number of encoded bits
295 by adding up the hole of unused bits created by this move. */
296 bp->first_unused_bit %= BITS_PER_BITPACK_WORD;
297 last = bp->first_unused_bit + nbits - 1;
298 if (last >= BITS_PER_BITPACK_WORD)
299 {
300 ix++;
301 bp->num_bits += (BITS_PER_BITPACK_WORD - bp->first_unused_bit);
302 bp->first_unused_bit = 0;
303 }
304
305 return ix;
306 }
307
308
309 /* Pack NBITS of value VAL into bitpack BP. */
310
311 void
312 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
313 {
314 unsigned ix;
315 bitpack_word_t word;
316
317 /* We cannot encode more bits than BITS_PER_BITPACK_WORD. */
318 gcc_assert (nbits > 0 && nbits <= BITS_PER_BITPACK_WORD);
319
320 /* Compute which word will contain the next NBITS. */
321 ix = bp_get_next_word (bp, nbits);
322 if (ix >= VEC_length (bitpack_word_t, bp->values))
323 {
324 /* If there is no room left in the last word of the values
325 array, add a new word. Additionally, we should only
326 need to add a single word, since every pack operation cannot
327 use more bits than fit in a single word. */
328 gcc_assert (ix < VEC_length (bitpack_word_t, bp->values) + 1);
329 VEC_safe_push (bitpack_word_t, heap, bp->values, 0);
330 }
331
332 /* Grab the last word to pack VAL into. */
333 word = VEC_index (bitpack_word_t, bp->values, ix);
334
335 /* To fit VAL in WORD, we need to shift VAL to the left to
336 skip the bottom BP->FIRST_UNUSED_BIT bits. */
337 gcc_assert (BITS_PER_BITPACK_WORD >= bp->first_unused_bit + nbits);
338 val <<= bp->first_unused_bit;
339
340 /* Update WORD with VAL. */
341 word |= val;
342
343 /* Update BP. */
344 VEC_replace (bitpack_word_t, bp->values, ix, word);
345 bp->num_bits += nbits;
346 bp->first_unused_bit += nbits;
347 }
348
349
350 /* Unpack the next NBITS from bitpack BP. */
351
352 bitpack_word_t
353 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
354 {
355 bitpack_word_t val, word, mask;
356 unsigned ix;
357
358 /* We cannot decode more bits than BITS_PER_BITPACK_WORD. */
359 gcc_assert (nbits > 0 && nbits <= BITS_PER_BITPACK_WORD);
360
361 /* Compute which word contains the next NBITS. */
362 ix = bp_get_next_word (bp, nbits);
363 word = VEC_index (bitpack_word_t, bp->values, ix);
364
365 /* Compute the mask to get NBITS from WORD. */
366 mask = (nbits == BITS_PER_BITPACK_WORD)
367 ? (bitpack_word_t) -1
368 : ((bitpack_word_t) 1 << nbits) - 1;
369
370 /* Shift WORD to the right to skip over the bits already decoded
371 in word. */
372 word >>= bp->first_unused_bit;
373
374 /* Apply the mask to obtain the requested value. */
375 val = word & mask;
376
377 /* Update BP->NUM_BITS for the next unpack operation. */
378 bp->num_bits += nbits;
379 bp->first_unused_bit += nbits;
380
381 return val;
382 } 255 }
383 256
384 257
385 /* Check that all the TS_* structures handled by the lto_output_* and 258 /* Check that all the TS_* structures handled by the lto_output_* and
386 lto_input_* routines are exactly ALL the structures defined in 259 lto_input_* routines are exactly ALL the structures defined in
428 handled_p[TS_STATEMENT_LIST] = true; 301 handled_p[TS_STATEMENT_LIST] = true;
429 handled_p[TS_CONSTRUCTOR] = true; 302 handled_p[TS_CONSTRUCTOR] = true;
430 handled_p[TS_OMP_CLAUSE] = true; 303 handled_p[TS_OMP_CLAUSE] = true;
431 handled_p[TS_OPTIMIZATION] = true; 304 handled_p[TS_OPTIMIZATION] = true;
432 handled_p[TS_TARGET_OPTION] = true; 305 handled_p[TS_TARGET_OPTION] = true;
306 handled_p[TS_TRANSLATION_UNIT_DECL] = true;
433 307
434 /* Anything not marked above will trigger the following assertion. 308 /* Anything not marked above will trigger the following assertion.
435 If this assertion triggers, it means that there is a new TS_* 309 If this assertion triggers, it means that there is a new TS_*
436 structure that should be handled by the streamer. */ 310 structure that should be handled by the streamer. */
437 for (i = 0; i < LAST_TS_ENUM; i++) 311 for (i = 0; i < LAST_TS_ENUM; i++)
450 324
451 /* Grow the array of nodes and offsets to accomodate T at IX. */ 325 /* Grow the array of nodes and offsets to accomodate T at IX. */
452 if (ix >= (int) VEC_length (tree, cache->nodes)) 326 if (ix >= (int) VEC_length (tree, cache->nodes))
453 { 327 {
454 size_t sz = ix + (20 + ix) / 4; 328 size_t sz = ix + (20 + ix) / 4;
455 VEC_safe_grow_cleared (tree, gc, cache->nodes, sz); 329 VEC_safe_grow_cleared (tree, heap, cache->nodes, sz);
456 VEC_safe_grow_cleared (unsigned, heap, cache->offsets, sz); 330 VEC_safe_grow_cleared (unsigned, heap, cache->offsets, sz);
457 } 331 }
458 332
459 VEC_replace (tree, cache->nodes, ix, t); 333 VEC_replace (tree, cache->nodes, ix, t);
460 VEC_replace (unsigned, cache->offsets, ix, offset); 334 VEC_replace (unsigned, cache->offsets, ix, offset);
492 if (insert_at_next_slot_p) 366 if (insert_at_next_slot_p)
493 ix = cache->next_slot++; 367 ix = cache->next_slot++;
494 else 368 else
495 ix = *ix_p; 369 ix = *ix_p;
496 370
497 entry = XCNEW (struct tree_int_map); 371 entry = (struct tree_int_map *)pool_alloc (cache->node_map_entries);
498 entry->base.from = t; 372 entry->base.from = t;
499 entry->to = (unsigned) ix; 373 entry->to = (unsigned) ix;
500 *slot = entry; 374 *slot = entry;
501 375
502 /* If no offset was given, store the invalid offset -1. */ 376 /* If no offset was given, store the invalid offset -1. */
646 520
647 if (node == NULL_TREE) 521 if (node == NULL_TREE)
648 return; 522 return;
649 523
650 if (TYPE_P (node)) 524 if (TYPE_P (node))
651 *nodep = node = gimple_register_type (node); 525 {
526 /* Type merging will get confused by the canonical types as they
527 are set by the middle-end. */
528 if (in_lto_p)
529 TYPE_CANONICAL (node) = NULL_TREE;
530 node = gimple_register_type (node);
531 TYPE_CANONICAL (node) = gimple_register_canonical_type (node);
532 *nodep = node;
533 }
652 534
653 /* Return if node is already seen. */ 535 /* Return if node is already seen. */
654 if (pointer_set_insert (seen_nodes, node)) 536 if (pointer_set_insert (seen_nodes, node))
655 return; 537 return;
656 538
754 636
755 cache = XCNEW (struct lto_streamer_cache_d); 637 cache = XCNEW (struct lto_streamer_cache_d);
756 638
757 cache->node_map = htab_create (101, tree_int_map_hash, tree_int_map_eq, NULL); 639 cache->node_map = htab_create (101, tree_int_map_hash, tree_int_map_eq, NULL);
758 640
641 cache->node_map_entries = create_alloc_pool ("node map",
642 sizeof (struct tree_int_map),
643 100);
644
759 /* Load all the well-known tree nodes that are always created by 645 /* Load all the well-known tree nodes that are always created by
760 the compiler on startup. This prevents writing them out 646 the compiler on startup. This prevents writing them out
761 unnecessarily. */ 647 unnecessarily. */
762 common_nodes = lto_get_common_nodes (); 648 common_nodes = lto_get_common_nodes ();
763 649
764 for (i = 0; VEC_iterate (tree, common_nodes, i, node); i++) 650 FOR_EACH_VEC_ELT (tree, common_nodes, i, node)
765 preload_common_node (cache, node); 651 preload_common_node (cache, node);
766 652
767 VEC_free(tree, heap, common_nodes); 653 VEC_free(tree, heap, common_nodes);
768 654
769 return cache; 655 return cache;
777 { 663 {
778 if (c == NULL) 664 if (c == NULL)
779 return; 665 return;
780 666
781 htab_delete (c->node_map); 667 htab_delete (c->node_map);
782 VEC_free (tree, gc, c->nodes); 668 free_alloc_pool (c->node_map_entries);
669 VEC_free (tree, heap, c->nodes);
783 VEC_free (unsigned, heap, c->offsets); 670 VEC_free (unsigned, heap, c->offsets);
784 free (c); 671 free (c);
785 } 672 }
786 673
674
675 #ifdef LTO_STREAMER_DEBUG
676 static htab_t tree_htab;
677
678 struct tree_hash_entry
679 {
680 tree key;
681 intptr_t value;
682 };
683
684 static hashval_t
685 hash_tree (const void *p)
686 {
687 const struct tree_hash_entry *e = (const struct tree_hash_entry *) p;
688 return htab_hash_pointer (e->key);
689 }
690
691 static int
692 eq_tree (const void *p1, const void *p2)
693 {
694 const struct tree_hash_entry *e1 = (const struct tree_hash_entry *) p1;
695 const struct tree_hash_entry *e2 = (const struct tree_hash_entry *) p2;
696 return (e1->key == e2->key);
697 }
698 #endif
787 699
788 /* Initialization common to the LTO reader and writer. */ 700 /* Initialization common to the LTO reader and writer. */
789 701
790 void 702 void
791 lto_streamer_init (void) 703 lto_streamer_init (void)
793 /* Check that all the TS_* handled by the reader and writer routines 705 /* Check that all the TS_* handled by the reader and writer routines
794 match exactly the structures defined in treestruct.def. When a 706 match exactly the structures defined in treestruct.def. When a
795 new TS_* astructure is added, the streamer should be updated to 707 new TS_* astructure is added, the streamer should be updated to
796 handle it. */ 708 handle it. */
797 check_handled_ts_structures (); 709 check_handled_ts_structures ();
710
711 #ifdef LTO_STREAMER_DEBUG
712 tree_htab = htab_create (31, hash_tree, eq_tree, NULL);
713 #endif
798 } 714 }
799 715
800 716
801 /* Gate function for all LTO streaming passes. */ 717 /* Gate function for all LTO streaming passes. */
802 718
803 bool 719 bool
804 gate_lto_out (void) 720 gate_lto_out (void)
805 { 721 {
806 return ((flag_generate_lto || in_lto_p) 722 return ((flag_generate_lto || in_lto_p)
807 /* Don't bother doing anything if the program has errors. */ 723 /* Don't bother doing anything if the program has errors. */
808 && !(errorcount || sorrycount)); 724 && !seen_error ());
809 } 725 }
810 726
811 727
812 #ifdef LTO_STREAMER_DEBUG 728 #ifdef LTO_STREAMER_DEBUG
813 /* Add a mapping between T and ORIG_T, which is the numeric value of 729 /* Add a mapping between T and ORIG_T, which is the numeric value of
821 removed. */ 737 removed. */
822 738
823 void 739 void
824 lto_orig_address_map (tree t, intptr_t orig_t) 740 lto_orig_address_map (tree t, intptr_t orig_t)
825 { 741 {
826 /* FIXME lto. Using the annotation field is quite hacky as it relies 742 struct tree_hash_entry ent;
827 on the GC not running while T is being rematerialized. It would 743 struct tree_hash_entry **slot;
828 be cleaner to use a hash table here. */ 744
829 t->base.ann = (union tree_ann_d *) orig_t; 745 ent.key = t;
746 ent.value = orig_t;
747 slot
748 = (struct tree_hash_entry **) htab_find_slot (tree_htab, &ent, INSERT);
749 gcc_assert (!*slot);
750 *slot = XNEW (struct tree_hash_entry);
751 **slot = ent;
830 } 752 }
831 753
832 754
833 /* Get the original address of T as it was seen by the writer. This 755 /* Get the original address of T as it was seen by the writer. This
834 is only valid while T is being reconstructed. */ 756 is only valid while T is being reconstructed. */
835 757
836 intptr_t 758 intptr_t
837 lto_orig_address_get (tree t) 759 lto_orig_address_get (tree t)
838 { 760 {
839 return (intptr_t) t->base.ann; 761 struct tree_hash_entry ent;
762 struct tree_hash_entry **slot;
763
764 ent.key = t;
765 slot
766 = (struct tree_hash_entry **) htab_find_slot (tree_htab, &ent, NO_INSERT);
767 return (slot ? (*slot)->value : 0);
840 } 768 }
841 769
842 770
843 /* Clear the mapping of T to its original address. */ 771 /* Clear the mapping of T to its original address. */
844 772
845 void 773 void
846 lto_orig_address_remove (tree t) 774 lto_orig_address_remove (tree t)
847 { 775 {
848 t->base.ann = NULL; 776 struct tree_hash_entry ent;
777 struct tree_hash_entry **slot;
778
779 ent.key = t;
780 slot
781 = (struct tree_hash_entry **) htab_find_slot (tree_htab, &ent, NO_INSERT);
782 gcc_assert (slot);
783 free (*slot);
784 htab_clear_slot (tree_htab, (PTR *)slot);
849 } 785 }
850 #endif 786 #endif
851 787
852 788
853 /* Check that the version MAJOR.MINOR is the correct version number. */ 789 /* Check that the version MAJOR.MINOR is the correct version number. */