1 /* Miscellaneous utilities for GIMPLE streaming. Things that are used
2 in both input and output are here.
4 Copyright (C) 2009-2013 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"
32 #include "diagnostic-core.h"
34 #include "tree-streamer.h"
35 #include "lto-streamer.h"
36 #include "streamer-hooks.h"
38 /* Statistics gathered during LTO, WPA and LTRANS. */
39 struct lto_stats_d lto_stats
;
41 /* LTO uses bitmaps with different life-times. So use a separate
42 obstack for all LTO bitmaps. */
43 static bitmap_obstack lto_obstack
;
44 static bool lto_obstack_initialized
;
47 /* Return a string representing LTO tag TAG. */
50 lto_tag_name (enum LTO_tags tag
)
52 if (lto_tag_is_tree_code_p (tag
))
54 /* For tags representing tree nodes, return the name of the
55 associated tree code. */
56 return get_tree_code_name (lto_tag_to_tree_code (tag
));
59 if (lto_tag_is_gimple_code_p (tag
))
61 /* For tags representing gimple statements, return the name of
62 the associated gimple code. */
63 return gimple_code_name
[lto_tag_to_gimple_code (tag
)];
75 return "LTO_eh_region";
77 return "LTO_function";
79 return "LTO_eh_table";
81 return "LTO_ert_cleanup";
84 case LTO_ert_allowed_exceptions
:
85 return "LTO_ert_allowed_exceptions";
86 case LTO_ert_must_not_throw
:
87 return "LTO_ert_must_not_throw";
88 case LTO_tree_pickle_reference
:
89 return "LTO_tree_pickle_reference";
90 case LTO_field_decl_ref
:
91 return "LTO_field_decl_ref";
92 case LTO_function_decl_ref
:
93 return "LTO_function_decl_ref";
94 case LTO_label_decl_ref
:
95 return "LTO_label_decl_ref";
96 case LTO_namespace_decl_ref
:
97 return "LTO_namespace_decl_ref";
98 case LTO_result_decl_ref
:
99 return "LTO_result_decl_ref";
100 case LTO_ssa_name_ref
:
101 return "LTO_ssa_name_ref";
102 case LTO_type_decl_ref
:
103 return "LTO_type_decl_ref";
105 return "LTO_type_ref";
106 case LTO_global_decl_ref
:
107 return "LTO_global_decl_ref";
109 return "LTO_UNKNOWN";
114 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
117 lto_bitmap_alloc (void)
119 if (!lto_obstack_initialized
)
121 bitmap_obstack_initialize (<o_obstack
);
122 lto_obstack_initialized
= true;
124 return BITMAP_ALLOC (<o_obstack
);
130 lto_bitmap_free (bitmap b
)
136 /* Get a section name for a particular type or name. The NAME field
137 is only used if SECTION_TYPE is LTO_section_function_body. For all
138 others it is ignored. The callee of this function is responsible
139 to free the returned name. */
142 lto_get_section_name (int section_type
, const char *name
, struct lto_file_decl_data
*f
)
148 if (section_type
== LTO_section_function_body
)
150 gcc_assert (name
!= NULL
);
156 else if (section_type
< LTO_N_SECTION_TYPES
)
158 add
= lto_section_name
[section_type
];
162 internal_error ("bytecode stream: unexpected LTO section %s", name
);
164 /* Make the section name unique so that ld -r combining sections
165 doesn't confuse the reader with merged sections.
167 For options don't add a ID, the option reader cannot deal with them
168 and merging should be ok here. */
169 if (section_type
== LTO_section_opts
)
172 sprintf (post
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, f
->id
);
174 sprintf (post
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, get_random_seed (false));
175 return concat (LTO_SECTION_NAME_PREFIX
, sep
, add
, post
, NULL
);
179 /* Show various memory usage statistics related to LTO. */
182 print_lto_report (const char *s
)
186 fprintf (stderr
, "[%s] # of input files: "
187 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
, lto_stats
.num_input_files
);
189 fprintf (stderr
, "[%s] # of input cgraph nodes: "
190 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
191 lto_stats
.num_input_cgraph_nodes
);
193 fprintf (stderr
, "[%s] # of function bodies: "
194 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
195 lto_stats
.num_function_bodies
);
197 for (i
= 0; i
< NUM_TREE_CODES
; i
++)
198 if (lto_stats
.num_trees
[i
])
199 fprintf (stderr
, "[%s] # of '%s' objects read: "
200 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
201 get_tree_code_name ((enum tree_code
) i
), lto_stats
.num_trees
[i
]);
205 fprintf (stderr
, "[%s] Compression: "
206 HOST_WIDE_INT_PRINT_UNSIGNED
" output bytes, "
207 HOST_WIDE_INT_PRINT_UNSIGNED
" compressed bytes", s
,
208 lto_stats
.num_output_il_bytes
,
209 lto_stats
.num_compressed_il_bytes
);
210 if (lto_stats
.num_output_il_bytes
> 0)
212 const float dividend
= (float) lto_stats
.num_compressed_il_bytes
;
213 const float divisor
= (float) lto_stats
.num_output_il_bytes
;
214 fprintf (stderr
, " (ratio: %f)", dividend
/ divisor
);
216 fprintf (stderr
, "\n");
221 fprintf (stderr
, "[%s] # of output files: "
222 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
223 lto_stats
.num_output_files
);
225 fprintf (stderr
, "[%s] # of output symtab nodes: "
226 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
227 lto_stats
.num_output_symtab_nodes
);
229 fprintf (stderr
, "[%s] # of output tree pickle references: "
230 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
231 lto_stats
.num_pickle_refs_output
);
232 fprintf (stderr
, "[%s] # of output tree bodies: "
233 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
234 lto_stats
.num_tree_bodies_output
);
236 fprintf (stderr
, "[%s] # callgraph partitions: "
237 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
238 lto_stats
.num_cgraph_partitions
);
240 fprintf (stderr
, "[%s] Compression: "
241 HOST_WIDE_INT_PRINT_UNSIGNED
" input bytes, "
242 HOST_WIDE_INT_PRINT_UNSIGNED
" uncompressed bytes", s
,
243 lto_stats
.num_input_il_bytes
,
244 lto_stats
.num_uncompressed_il_bytes
);
245 if (lto_stats
.num_input_il_bytes
> 0)
247 const float dividend
= (float) lto_stats
.num_uncompressed_il_bytes
;
248 const float divisor
= (float) lto_stats
.num_input_il_bytes
;
249 fprintf (stderr
, " (ratio: %f)", dividend
/ divisor
);
251 fprintf (stderr
, "\n");
254 for (i
= 0; i
< LTO_N_SECTION_TYPES
; i
++)
255 fprintf (stderr
, "[%s] Size of mmap'd section %s: "
256 HOST_WIDE_INT_PRINT_UNSIGNED
" bytes\n", s
,
257 lto_section_name
[i
], lto_stats
.section_size
[i
]);
261 #ifdef LTO_STREAMER_DEBUG
262 struct tree_hash_entry
268 struct tree_entry_hasher
: typed_noop_remove
<tree_hash_entry
>
270 typedef tree_hash_entry value_type
;
271 typedef tree_hash_entry compare_type
;
272 static inline hashval_t
hash (const value_type
*);
273 static inline bool equal (const value_type
*, const compare_type
*);
277 tree_entry_hasher::hash (const value_type
*e
)
279 return htab_hash_pointer (e
->key
);
283 tree_entry_hasher::equal (const value_type
*e1
, const compare_type
*e2
)
285 return (e1
->key
== e2
->key
);
288 static hash_table
<tree_hash_entry
> tree_htab
;
291 /* Initialization common to the LTO reader and writer. */
294 lto_streamer_init (void)
296 /* Check that all the TS_* handled by the reader and writer routines
297 match exactly the structures defined in treestruct.def. When a
298 new TS_* astructure is added, the streamer should be updated to
300 streamer_check_handled_ts_structures ();
302 #ifdef LTO_STREAMER_DEBUG
303 tree_htab
.create (31);
308 /* Gate function for all LTO streaming passes. */
313 return ((flag_generate_lto
|| in_lto_p
)
314 /* Don't bother doing anything if the program has errors. */
319 #ifdef LTO_STREAMER_DEBUG
320 /* Add a mapping between T and ORIG_T, which is the numeric value of
321 the original address of T as it was seen by the LTO writer. This
322 mapping is useful when debugging streaming problems. A debugging
323 session can be started on both reader and writer using ORIG_T
324 as a breakpoint value in both sessions.
326 Note that this mapping is transient and only valid while T is
327 being reconstructed. Once T is fully built, the mapping is
331 lto_orig_address_map (tree t
, intptr_t orig_t
)
333 struct tree_hash_entry ent
;
334 struct tree_hash_entry
**slot
;
338 slot
= tree_htab
.find_slot (&ent
, INSERT
);
340 *slot
= XNEW (struct tree_hash_entry
);
345 /* Get the original address of T as it was seen by the writer. This
346 is only valid while T is being reconstructed. */
349 lto_orig_address_get (tree t
)
351 struct tree_hash_entry ent
;
352 struct tree_hash_entry
**slot
;
355 slot
= tree_htab
.find_slot (&ent
, NO_INSERT
);
356 return (slot
? (*slot
)->value
: 0);
360 /* Clear the mapping of T to its original address. */
363 lto_orig_address_remove (tree t
)
365 struct tree_hash_entry ent
;
366 struct tree_hash_entry
**slot
;
369 slot
= tree_htab
.find_slot (&ent
, NO_INSERT
);
372 tree_htab
.clear_slot (slot
);
377 /* Check that the version MAJOR.MINOR is the correct version number. */
380 lto_check_version (int major
, int minor
)
382 if (major
!= LTO_major_version
|| minor
!= LTO_minor_version
)
383 fatal_error ("bytecode stream generated with LTO version %d.%d instead "
384 "of the expected %d.%d",
386 LTO_major_version
, LTO_minor_version
);
390 /* Initialize all the streamer hooks used for streaming GIMPLE. */
393 lto_streamer_hooks_init (void)
395 streamer_hooks_init ();
396 streamer_hooks
.write_tree
= lto_output_tree
;
397 streamer_hooks
.read_tree
= lto_input_tree
;
398 streamer_hooks
.input_location
= lto_input_location
;
399 streamer_hooks
.output_location
= lto_output_location
;