1 /* Miscellaneous utilities for GIMPLE streaming. Things that are used
2 in both input and output are here.
4 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 Contributed by Doug Kwan <dougkwan@google.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
29 #include "hard-reg-set.h"
33 #include "fold-const.h"
34 #include "internal-fn.h"
35 #include "diagnostic-core.h"
37 #include "tree-streamer.h"
38 #include "lto-streamer.h"
39 #include "lto-section-names.h"
41 /* Statistics gathered during LTO, WPA and LTRANS. */
42 struct lto_stats_d lto_stats
;
44 /* LTO uses bitmaps with different life-times. So use a separate
45 obstack for all LTO bitmaps. */
46 static bitmap_obstack lto_obstack
;
47 static bool lto_obstack_initialized
;
49 const char *section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
50 /* Set when streaming LTO for offloading compiler. */
51 bool lto_stream_offload_p
;
53 /* Return a string representing LTO tag TAG. */
56 lto_tag_name (enum LTO_tags tag
)
58 if (lto_tag_is_tree_code_p (tag
))
60 /* For tags representing tree nodes, return the name of the
61 associated tree code. */
62 return get_tree_code_name (lto_tag_to_tree_code (tag
));
65 if (lto_tag_is_gimple_code_p (tag
))
67 /* For tags representing gimple statements, return the name of
68 the associated gimple code. */
69 return gimple_code_name
[lto_tag_to_gimple_code (tag
)];
81 return "LTO_eh_region";
83 return "LTO_function";
85 return "LTO_eh_table";
87 return "LTO_ert_cleanup";
90 case LTO_ert_allowed_exceptions
:
91 return "LTO_ert_allowed_exceptions";
92 case LTO_ert_must_not_throw
:
93 return "LTO_ert_must_not_throw";
94 case LTO_tree_pickle_reference
:
95 return "LTO_tree_pickle_reference";
96 case LTO_field_decl_ref
:
97 return "LTO_field_decl_ref";
98 case LTO_function_decl_ref
:
99 return "LTO_function_decl_ref";
100 case LTO_label_decl_ref
:
101 return "LTO_label_decl_ref";
102 case LTO_namespace_decl_ref
:
103 return "LTO_namespace_decl_ref";
104 case LTO_result_decl_ref
:
105 return "LTO_result_decl_ref";
106 case LTO_ssa_name_ref
:
107 return "LTO_ssa_name_ref";
108 case LTO_type_decl_ref
:
109 return "LTO_type_decl_ref";
111 return "LTO_type_ref";
112 case LTO_global_decl_ref
:
113 return "LTO_global_decl_ref";
115 return "LTO_UNKNOWN";
120 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
123 lto_bitmap_alloc (void)
125 if (!lto_obstack_initialized
)
127 bitmap_obstack_initialize (<o_obstack
);
128 lto_obstack_initialized
= true;
130 return BITMAP_ALLOC (<o_obstack
);
136 lto_bitmap_free (bitmap b
)
142 /* Get a section name for a particular type or name. The NAME field
143 is only used if SECTION_TYPE is LTO_section_function_body. For all
144 others it is ignored. The callee of this function is responsible
145 to free the returned name. */
148 lto_get_section_name (int section_type
, const char *name
, struct lto_file_decl_data
*f
)
154 if (section_type
== LTO_section_function_body
)
156 gcc_assert (name
!= NULL
);
162 else if (section_type
< LTO_N_SECTION_TYPES
)
164 add
= lto_section_name
[section_type
];
168 internal_error ("bytecode stream: unexpected LTO section %s", name
);
170 /* Make the section name unique so that ld -r combining sections
171 doesn't confuse the reader with merged sections.
173 For options don't add a ID, the option reader cannot deal with them
174 and merging should be ok here. */
175 if (section_type
== LTO_section_opts
)
178 sprintf (post
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, f
->id
);
180 sprintf (post
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, get_random_seed (false));
181 return concat (section_name_prefix
, sep
, add
, post
, NULL
);
185 /* Show various memory usage statistics related to LTO. */
188 print_lto_report (const char *s
)
192 fprintf (stderr
, "[%s] # of input files: "
193 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
, lto_stats
.num_input_files
);
195 fprintf (stderr
, "[%s] # of input cgraph nodes: "
196 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
197 lto_stats
.num_input_cgraph_nodes
);
199 fprintf (stderr
, "[%s] # of function bodies: "
200 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
201 lto_stats
.num_function_bodies
);
203 for (i
= 0; i
< NUM_TREE_CODES
; i
++)
204 if (lto_stats
.num_trees
[i
])
205 fprintf (stderr
, "[%s] # of '%s' objects read: "
206 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
207 get_tree_code_name ((enum tree_code
) i
), lto_stats
.num_trees
[i
]);
211 fprintf (stderr
, "[%s] Compression: "
212 HOST_WIDE_INT_PRINT_UNSIGNED
" output bytes, "
213 HOST_WIDE_INT_PRINT_UNSIGNED
" compressed bytes", s
,
214 lto_stats
.num_output_il_bytes
,
215 lto_stats
.num_compressed_il_bytes
);
216 if (lto_stats
.num_output_il_bytes
> 0)
218 const float dividend
= (float) lto_stats
.num_compressed_il_bytes
;
219 const float divisor
= (float) lto_stats
.num_output_il_bytes
;
220 fprintf (stderr
, " (ratio: %f)", dividend
/ divisor
);
222 fprintf (stderr
, "\n");
227 fprintf (stderr
, "[%s] # of output files: "
228 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
229 lto_stats
.num_output_files
);
231 fprintf (stderr
, "[%s] # of output symtab nodes: "
232 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
233 lto_stats
.num_output_symtab_nodes
);
235 fprintf (stderr
, "[%s] # of output tree pickle references: "
236 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
237 lto_stats
.num_pickle_refs_output
);
238 fprintf (stderr
, "[%s] # of output tree bodies: "
239 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
240 lto_stats
.num_tree_bodies_output
);
242 fprintf (stderr
, "[%s] # callgraph partitions: "
243 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
244 lto_stats
.num_cgraph_partitions
);
246 fprintf (stderr
, "[%s] Compression: "
247 HOST_WIDE_INT_PRINT_UNSIGNED
" input bytes, "
248 HOST_WIDE_INT_PRINT_UNSIGNED
" uncompressed bytes", s
,
249 lto_stats
.num_input_il_bytes
,
250 lto_stats
.num_uncompressed_il_bytes
);
251 if (lto_stats
.num_input_il_bytes
> 0)
253 const float dividend
= (float) lto_stats
.num_uncompressed_il_bytes
;
254 const float divisor
= (float) lto_stats
.num_input_il_bytes
;
255 fprintf (stderr
, " (ratio: %f)", dividend
/ divisor
);
257 fprintf (stderr
, "\n");
260 for (i
= 0; i
< LTO_N_SECTION_TYPES
; i
++)
261 fprintf (stderr
, "[%s] Size of mmap'd section %s: "
262 HOST_WIDE_INT_PRINT_UNSIGNED
" bytes\n", s
,
263 lto_section_name
[i
], lto_stats
.section_size
[i
]);
267 #ifdef LTO_STREAMER_DEBUG
268 struct tree_hash_entry
274 struct tree_entry_hasher
: nofree_ptr_hash
<tree_hash_entry
>
276 static inline hashval_t
hash (const value_type
*);
277 static inline bool equal (const value_type
*, const compare_type
*);
281 tree_entry_hasher::hash (const value_type
*e
)
283 return htab_hash_pointer (e
->key
);
287 tree_entry_hasher::equal (const value_type
*e1
, const compare_type
*e2
)
289 return (e1
->key
== e2
->key
);
292 static hash_table
<tree_hash_entry
> *tree_htab
;
295 /* Initialization common to the LTO reader and writer. */
298 lto_streamer_init (void)
300 #ifdef ENABLE_CHECKING
301 /* Check that all the TS_* handled by the reader and writer routines
302 match exactly the structures defined in treestruct.def. When a
303 new TS_* astructure is added, the streamer should be updated to
305 streamer_check_handled_ts_structures ();
308 #ifdef LTO_STREAMER_DEBUG
309 tree_htab
= new hash_table
<tree_hash_entry
> (31);
314 /* Gate function for all LTO streaming passes. */
319 return ((flag_generate_lto
|| flag_generate_offload
|| in_lto_p
)
320 /* Don't bother doing anything if the program has errors. */
325 #ifdef LTO_STREAMER_DEBUG
326 /* Add a mapping between T and ORIG_T, which is the numeric value of
327 the original address of T as it was seen by the LTO writer. This
328 mapping is useful when debugging streaming problems. A debugging
329 session can be started on both reader and writer using ORIG_T
330 as a breakpoint value in both sessions.
332 Note that this mapping is transient and only valid while T is
333 being reconstructed. Once T is fully built, the mapping is
337 lto_orig_address_map (tree t
, intptr_t orig_t
)
339 struct tree_hash_entry ent
;
340 struct tree_hash_entry
**slot
;
344 slot
= tree_htab
->find_slot (&ent
, INSERT
);
346 *slot
= XNEW (struct tree_hash_entry
);
351 /* Get the original address of T as it was seen by the writer. This
352 is only valid while T is being reconstructed. */
355 lto_orig_address_get (tree t
)
357 struct tree_hash_entry ent
;
358 struct tree_hash_entry
**slot
;
361 slot
= tree_htab
->find_slot (&ent
, NO_INSERT
);
362 return (slot
? (*slot
)->value
: 0);
366 /* Clear the mapping of T to its original address. */
369 lto_orig_address_remove (tree t
)
371 struct tree_hash_entry ent
;
372 struct tree_hash_entry
**slot
;
375 slot
= tree_htab
->find_slot (&ent
, NO_INSERT
);
378 tree_htab
->clear_slot (slot
);
383 /* Check that the version MAJOR.MINOR is the correct version number. */
386 lto_check_version (int major
, int minor
)
388 if (major
!= LTO_major_version
|| minor
!= LTO_minor_version
)
389 fatal_error (input_location
,
390 "bytecode stream generated with LTO version %d.%d instead "
391 "of the expected %d.%d",
393 LTO_major_version
, LTO_minor_version
);
397 /* Initialize all the streamer hooks used for streaming GIMPLE. */
400 lto_streamer_hooks_init (void)
402 streamer_hooks_init ();
403 streamer_hooks
.write_tree
= lto_output_tree
;
404 streamer_hooks
.read_tree
= lto_input_tree
;
405 streamer_hooks
.input_location
= lto_input_location
;
406 streamer_hooks
.output_location
= lto_output_location
;