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"
32 #include "fold-const.h"
34 #include "hard-reg-set.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
42 #include "diagnostic-core.h"
43 #include "plugin-api.h"
46 #include "tree-streamer.h"
47 #include "lto-streamer.h"
48 #include "lto-section-names.h"
49 #include "streamer-hooks.h"
51 /* Statistics gathered during LTO, WPA and LTRANS. */
52 struct lto_stats_d lto_stats
;
54 /* LTO uses bitmaps with different life-times. So use a separate
55 obstack for all LTO bitmaps. */
56 static bitmap_obstack lto_obstack
;
57 static bool lto_obstack_initialized
;
59 const char *section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
60 /* Set when streaming LTO for offloading compiler. */
61 bool lto_stream_offload_p
;
63 /* Return a string representing LTO tag TAG. */
66 lto_tag_name (enum LTO_tags tag
)
68 if (lto_tag_is_tree_code_p (tag
))
70 /* For tags representing tree nodes, return the name of the
71 associated tree code. */
72 return get_tree_code_name (lto_tag_to_tree_code (tag
));
75 if (lto_tag_is_gimple_code_p (tag
))
77 /* For tags representing gimple statements, return the name of
78 the associated gimple code. */
79 return gimple_code_name
[lto_tag_to_gimple_code (tag
)];
91 return "LTO_eh_region";
93 return "LTO_function";
95 return "LTO_eh_table";
97 return "LTO_ert_cleanup";
100 case LTO_ert_allowed_exceptions
:
101 return "LTO_ert_allowed_exceptions";
102 case LTO_ert_must_not_throw
:
103 return "LTO_ert_must_not_throw";
104 case LTO_tree_pickle_reference
:
105 return "LTO_tree_pickle_reference";
106 case LTO_field_decl_ref
:
107 return "LTO_field_decl_ref";
108 case LTO_function_decl_ref
:
109 return "LTO_function_decl_ref";
110 case LTO_label_decl_ref
:
111 return "LTO_label_decl_ref";
112 case LTO_namespace_decl_ref
:
113 return "LTO_namespace_decl_ref";
114 case LTO_result_decl_ref
:
115 return "LTO_result_decl_ref";
116 case LTO_ssa_name_ref
:
117 return "LTO_ssa_name_ref";
118 case LTO_type_decl_ref
:
119 return "LTO_type_decl_ref";
121 return "LTO_type_ref";
122 case LTO_global_decl_ref
:
123 return "LTO_global_decl_ref";
125 return "LTO_UNKNOWN";
130 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
133 lto_bitmap_alloc (void)
135 if (!lto_obstack_initialized
)
137 bitmap_obstack_initialize (<o_obstack
);
138 lto_obstack_initialized
= true;
140 return BITMAP_ALLOC (<o_obstack
);
146 lto_bitmap_free (bitmap b
)
152 /* Get a section name for a particular type or name. The NAME field
153 is only used if SECTION_TYPE is LTO_section_function_body. For all
154 others it is ignored. The callee of this function is responsible
155 to free the returned name. */
158 lto_get_section_name (int section_type
, const char *name
, struct lto_file_decl_data
*f
)
164 if (section_type
== LTO_section_function_body
)
166 gcc_assert (name
!= NULL
);
172 else if (section_type
< LTO_N_SECTION_TYPES
)
174 add
= lto_section_name
[section_type
];
178 internal_error ("bytecode stream: unexpected LTO section %s", name
);
180 /* Make the section name unique so that ld -r combining sections
181 doesn't confuse the reader with merged sections.
183 For options don't add a ID, the option reader cannot deal with them
184 and merging should be ok here. */
185 if (section_type
== LTO_section_opts
)
188 sprintf (post
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, f
->id
);
190 sprintf (post
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, get_random_seed (false));
191 return concat (section_name_prefix
, sep
, add
, post
, NULL
);
195 /* Show various memory usage statistics related to LTO. */
198 print_lto_report (const char *s
)
202 fprintf (stderr
, "[%s] # of input files: "
203 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
, lto_stats
.num_input_files
);
205 fprintf (stderr
, "[%s] # of input cgraph nodes: "
206 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
207 lto_stats
.num_input_cgraph_nodes
);
209 fprintf (stderr
, "[%s] # of function bodies: "
210 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
211 lto_stats
.num_function_bodies
);
213 for (i
= 0; i
< NUM_TREE_CODES
; i
++)
214 if (lto_stats
.num_trees
[i
])
215 fprintf (stderr
, "[%s] # of '%s' objects read: "
216 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
217 get_tree_code_name ((enum tree_code
) i
), lto_stats
.num_trees
[i
]);
221 fprintf (stderr
, "[%s] Compression: "
222 HOST_WIDE_INT_PRINT_UNSIGNED
" output bytes, "
223 HOST_WIDE_INT_PRINT_UNSIGNED
" compressed bytes", s
,
224 lto_stats
.num_output_il_bytes
,
225 lto_stats
.num_compressed_il_bytes
);
226 if (lto_stats
.num_output_il_bytes
> 0)
228 const float dividend
= (float) lto_stats
.num_compressed_il_bytes
;
229 const float divisor
= (float) lto_stats
.num_output_il_bytes
;
230 fprintf (stderr
, " (ratio: %f)", dividend
/ divisor
);
232 fprintf (stderr
, "\n");
237 fprintf (stderr
, "[%s] # of output files: "
238 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
239 lto_stats
.num_output_files
);
241 fprintf (stderr
, "[%s] # of output symtab nodes: "
242 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
243 lto_stats
.num_output_symtab_nodes
);
245 fprintf (stderr
, "[%s] # of output tree pickle references: "
246 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
247 lto_stats
.num_pickle_refs_output
);
248 fprintf (stderr
, "[%s] # of output tree bodies: "
249 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
250 lto_stats
.num_tree_bodies_output
);
252 fprintf (stderr
, "[%s] # callgraph partitions: "
253 HOST_WIDE_INT_PRINT_UNSIGNED
"\n", s
,
254 lto_stats
.num_cgraph_partitions
);
256 fprintf (stderr
, "[%s] Compression: "
257 HOST_WIDE_INT_PRINT_UNSIGNED
" input bytes, "
258 HOST_WIDE_INT_PRINT_UNSIGNED
" uncompressed bytes", s
,
259 lto_stats
.num_input_il_bytes
,
260 lto_stats
.num_uncompressed_il_bytes
);
261 if (lto_stats
.num_input_il_bytes
> 0)
263 const float dividend
= (float) lto_stats
.num_uncompressed_il_bytes
;
264 const float divisor
= (float) lto_stats
.num_input_il_bytes
;
265 fprintf (stderr
, " (ratio: %f)", dividend
/ divisor
);
267 fprintf (stderr
, "\n");
270 for (i
= 0; i
< LTO_N_SECTION_TYPES
; i
++)
271 fprintf (stderr
, "[%s] Size of mmap'd section %s: "
272 HOST_WIDE_INT_PRINT_UNSIGNED
" bytes\n", s
,
273 lto_section_name
[i
], lto_stats
.section_size
[i
]);
277 #ifdef LTO_STREAMER_DEBUG
278 struct tree_hash_entry
284 struct tree_entry_hasher
: nofree_ptr_hash
<tree_hash_entry
>
286 static inline hashval_t
hash (const value_type
*);
287 static inline bool equal (const value_type
*, const compare_type
*);
291 tree_entry_hasher::hash (const value_type
*e
)
293 return htab_hash_pointer (e
->key
);
297 tree_entry_hasher::equal (const value_type
*e1
, const compare_type
*e2
)
299 return (e1
->key
== e2
->key
);
302 static hash_table
<tree_hash_entry
> *tree_htab
;
305 /* Initialization common to the LTO reader and writer. */
308 lto_streamer_init (void)
310 #ifdef ENABLE_CHECKING
311 /* Check that all the TS_* handled by the reader and writer routines
312 match exactly the structures defined in treestruct.def. When a
313 new TS_* astructure is added, the streamer should be updated to
315 streamer_check_handled_ts_structures ();
318 #ifdef LTO_STREAMER_DEBUG
319 tree_htab
= new hash_table
<tree_hash_entry
> (31);
324 /* Gate function for all LTO streaming passes. */
329 return ((flag_generate_lto
|| flag_generate_offload
|| in_lto_p
)
330 /* Don't bother doing anything if the program has errors. */
335 #ifdef LTO_STREAMER_DEBUG
336 /* Add a mapping between T and ORIG_T, which is the numeric value of
337 the original address of T as it was seen by the LTO writer. This
338 mapping is useful when debugging streaming problems. A debugging
339 session can be started on both reader and writer using ORIG_T
340 as a breakpoint value in both sessions.
342 Note that this mapping is transient and only valid while T is
343 being reconstructed. Once T is fully built, the mapping is
347 lto_orig_address_map (tree t
, intptr_t orig_t
)
349 struct tree_hash_entry ent
;
350 struct tree_hash_entry
**slot
;
354 slot
= tree_htab
->find_slot (&ent
, INSERT
);
356 *slot
= XNEW (struct tree_hash_entry
);
361 /* Get the original address of T as it was seen by the writer. This
362 is only valid while T is being reconstructed. */
365 lto_orig_address_get (tree t
)
367 struct tree_hash_entry ent
;
368 struct tree_hash_entry
**slot
;
371 slot
= tree_htab
->find_slot (&ent
, NO_INSERT
);
372 return (slot
? (*slot
)->value
: 0);
376 /* Clear the mapping of T to its original address. */
379 lto_orig_address_remove (tree t
)
381 struct tree_hash_entry ent
;
382 struct tree_hash_entry
**slot
;
385 slot
= tree_htab
->find_slot (&ent
, NO_INSERT
);
388 tree_htab
->clear_slot (slot
);
393 /* Check that the version MAJOR.MINOR is the correct version number. */
396 lto_check_version (int major
, int minor
)
398 if (major
!= LTO_major_version
|| minor
!= LTO_minor_version
)
399 fatal_error (input_location
,
400 "bytecode stream generated with LTO version %d.%d instead "
401 "of the expected %d.%d",
403 LTO_major_version
, LTO_minor_version
);
407 /* Initialize all the streamer hooks used for streaming GIMPLE. */
410 lto_streamer_hooks_init (void)
412 streamer_hooks_init ();
413 streamer_hooks
.write_tree
= lto_output_tree
;
414 streamer_hooks
.read_tree
= lto_input_tree
;
415 streamer_hooks
.input_location
= lto_input_location
;
416 streamer_hooks
.output_location
= lto_output_location
;