MIPS compact branch support
[official-gcc.git] / gcc / lto-streamer.c
blobb34ab8bc01ac5ff3647846ccbb09abe78ef2994b
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
12 version.
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
17 for more details.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "hard-reg-set.h"
30 #include "toplev.h"
31 #include "flags.h"
32 #include "alias.h"
33 #include "fold-const.h"
34 #include "internal-fn.h"
35 #include "diagnostic-core.h"
36 #include "cgraph.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. */
55 const char *
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)];
72 switch (tag)
74 case LTO_null:
75 return "LTO_null";
76 case LTO_bb0:
77 return "LTO_bb0";
78 case LTO_bb1:
79 return "LTO_bb1";
80 case LTO_eh_region:
81 return "LTO_eh_region";
82 case LTO_function:
83 return "LTO_function";
84 case LTO_eh_table:
85 return "LTO_eh_table";
86 case LTO_ert_cleanup:
87 return "LTO_ert_cleanup";
88 case LTO_ert_try:
89 return "LTO_ert_try";
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";
110 case LTO_type_ref:
111 return "LTO_type_ref";
112 case LTO_global_decl_ref:
113 return "LTO_global_decl_ref";
114 default:
115 return "LTO_UNKNOWN";
120 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
122 bitmap
123 lto_bitmap_alloc (void)
125 if (!lto_obstack_initialized)
127 bitmap_obstack_initialize (&lto_obstack);
128 lto_obstack_initialized = true;
130 return BITMAP_ALLOC (&lto_obstack);
133 /* Free bitmap B. */
135 void
136 lto_bitmap_free (bitmap b)
138 BITMAP_FREE (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. */
147 char *
148 lto_get_section_name (int section_type, const char *name, struct lto_file_decl_data *f)
150 const char *add;
151 char post[32];
152 const char *sep;
154 if (section_type == LTO_section_function_body)
156 gcc_assert (name != NULL);
157 if (name[0] == '*')
158 name++;
159 add = name;
160 sep = "";
162 else if (section_type < LTO_N_SECTION_TYPES)
164 add = lto_section_name[section_type];
165 sep = ".";
167 else
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)
176 strcpy (post, "");
177 else if (f != NULL)
178 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, f->id);
179 else
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. */
187 void
188 print_lto_report (const char *s)
190 unsigned i;
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]);
209 if (flag_lto)
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");
225 if (flag_wpa)
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
270 tree key;
271 intptr_t value;
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 *);
280 inline hashval_t
281 tree_entry_hasher::hash (const value_type *e)
283 return htab_hash_pointer (e->key);
286 inline bool
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;
293 #endif
295 /* Initialization common to the LTO reader and writer. */
297 void
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
304 handle it. */
305 streamer_check_handled_ts_structures ();
306 #endif
308 #ifdef LTO_STREAMER_DEBUG
309 tree_htab = new hash_table<tree_hash_entry> (31);
310 #endif
314 /* Gate function for all LTO streaming passes. */
316 bool
317 gate_lto_out (void)
319 return ((flag_generate_lto || flag_generate_offload || in_lto_p)
320 /* Don't bother doing anything if the program has errors. */
321 && !seen_error ());
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
334 removed. */
336 void
337 lto_orig_address_map (tree t, intptr_t orig_t)
339 struct tree_hash_entry ent;
340 struct tree_hash_entry **slot;
342 ent.key = t;
343 ent.value = orig_t;
344 slot = tree_htab->find_slot (&ent, INSERT);
345 gcc_assert (!*slot);
346 *slot = XNEW (struct tree_hash_entry);
347 **slot = ent;
351 /* Get the original address of T as it was seen by the writer. This
352 is only valid while T is being reconstructed. */
354 intptr_t
355 lto_orig_address_get (tree t)
357 struct tree_hash_entry ent;
358 struct tree_hash_entry **slot;
360 ent.key = t;
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. */
368 void
369 lto_orig_address_remove (tree t)
371 struct tree_hash_entry ent;
372 struct tree_hash_entry **slot;
374 ent.key = t;
375 slot = tree_htab->find_slot (&ent, NO_INSERT);
376 gcc_assert (slot);
377 free (*slot);
378 tree_htab->clear_slot (slot);
380 #endif
383 /* Check that the version MAJOR.MINOR is the correct version number. */
385 void
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",
392 major, minor,
393 LTO_major_version, LTO_minor_version);
397 /* Initialize all the streamer hooks used for streaming GIMPLE. */
399 void
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;