gcc/
[official-gcc.git] / gcc / lto-streamer.c
blob3fbbe9ac480e757a38487f6d70f1ccde9163204f
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 "tm.h"
27 #include "toplev.h"
28 #include "flags.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "tree.h"
32 #include "fold-const.h"
33 #include "predict.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "bitmap.h"
42 #include "diagnostic-core.h"
43 #include "cgraph.h"
44 #include "tree-streamer.h"
45 #include "lto-streamer.h"
46 #include "lto-section-names.h"
47 #include "streamer-hooks.h"
49 /* Statistics gathered during LTO, WPA and LTRANS. */
50 struct lto_stats_d lto_stats;
52 /* LTO uses bitmaps with different life-times. So use a separate
53 obstack for all LTO bitmaps. */
54 static bitmap_obstack lto_obstack;
55 static bool lto_obstack_initialized;
57 const char *section_name_prefix = LTO_SECTION_NAME_PREFIX;
58 /* Set when streaming LTO for offloading compiler. */
59 bool lto_stream_offload_p;
61 /* Return a string representing LTO tag TAG. */
63 const char *
64 lto_tag_name (enum LTO_tags tag)
66 if (lto_tag_is_tree_code_p (tag))
68 /* For tags representing tree nodes, return the name of the
69 associated tree code. */
70 return get_tree_code_name (lto_tag_to_tree_code (tag));
73 if (lto_tag_is_gimple_code_p (tag))
75 /* For tags representing gimple statements, return the name of
76 the associated gimple code. */
77 return gimple_code_name[lto_tag_to_gimple_code (tag)];
80 switch (tag)
82 case LTO_null:
83 return "LTO_null";
84 case LTO_bb0:
85 return "LTO_bb0";
86 case LTO_bb1:
87 return "LTO_bb1";
88 case LTO_eh_region:
89 return "LTO_eh_region";
90 case LTO_function:
91 return "LTO_function";
92 case LTO_eh_table:
93 return "LTO_eh_table";
94 case LTO_ert_cleanup:
95 return "LTO_ert_cleanup";
96 case LTO_ert_try:
97 return "LTO_ert_try";
98 case LTO_ert_allowed_exceptions:
99 return "LTO_ert_allowed_exceptions";
100 case LTO_ert_must_not_throw:
101 return "LTO_ert_must_not_throw";
102 case LTO_tree_pickle_reference:
103 return "LTO_tree_pickle_reference";
104 case LTO_field_decl_ref:
105 return "LTO_field_decl_ref";
106 case LTO_function_decl_ref:
107 return "LTO_function_decl_ref";
108 case LTO_label_decl_ref:
109 return "LTO_label_decl_ref";
110 case LTO_namespace_decl_ref:
111 return "LTO_namespace_decl_ref";
112 case LTO_result_decl_ref:
113 return "LTO_result_decl_ref";
114 case LTO_ssa_name_ref:
115 return "LTO_ssa_name_ref";
116 case LTO_type_decl_ref:
117 return "LTO_type_decl_ref";
118 case LTO_type_ref:
119 return "LTO_type_ref";
120 case LTO_global_decl_ref:
121 return "LTO_global_decl_ref";
122 default:
123 return "LTO_UNKNOWN";
128 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
130 bitmap
131 lto_bitmap_alloc (void)
133 if (!lto_obstack_initialized)
135 bitmap_obstack_initialize (&lto_obstack);
136 lto_obstack_initialized = true;
138 return BITMAP_ALLOC (&lto_obstack);
141 /* Free bitmap B. */
143 void
144 lto_bitmap_free (bitmap b)
146 BITMAP_FREE (b);
150 /* Get a section name for a particular type or name. The NAME field
151 is only used if SECTION_TYPE is LTO_section_function_body. For all
152 others it is ignored. The callee of this function is responsible
153 to free the returned name. */
155 char *
156 lto_get_section_name (int section_type, const char *name, struct lto_file_decl_data *f)
158 const char *add;
159 char post[32];
160 const char *sep;
162 if (section_type == LTO_section_function_body)
164 gcc_assert (name != NULL);
165 if (name[0] == '*')
166 name++;
167 add = name;
168 sep = "";
170 else if (section_type < LTO_N_SECTION_TYPES)
172 add = lto_section_name[section_type];
173 sep = ".";
175 else
176 internal_error ("bytecode stream: unexpected LTO section %s", name);
178 /* Make the section name unique so that ld -r combining sections
179 doesn't confuse the reader with merged sections.
181 For options don't add a ID, the option reader cannot deal with them
182 and merging should be ok here. */
183 if (section_type == LTO_section_opts)
184 strcpy (post, "");
185 else if (f != NULL)
186 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, f->id);
187 else
188 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, get_random_seed (false));
189 return concat (section_name_prefix, sep, add, post, NULL);
193 /* Show various memory usage statistics related to LTO. */
195 void
196 print_lto_report (const char *s)
198 unsigned i;
200 fprintf (stderr, "[%s] # of input files: "
201 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s, lto_stats.num_input_files);
203 fprintf (stderr, "[%s] # of input cgraph nodes: "
204 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
205 lto_stats.num_input_cgraph_nodes);
207 fprintf (stderr, "[%s] # of function bodies: "
208 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
209 lto_stats.num_function_bodies);
211 for (i = 0; i < NUM_TREE_CODES; i++)
212 if (lto_stats.num_trees[i])
213 fprintf (stderr, "[%s] # of '%s' objects read: "
214 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
215 get_tree_code_name ((enum tree_code) i), lto_stats.num_trees[i]);
217 if (flag_lto)
219 fprintf (stderr, "[%s] Compression: "
220 HOST_WIDE_INT_PRINT_UNSIGNED " output bytes, "
221 HOST_WIDE_INT_PRINT_UNSIGNED " compressed bytes", s,
222 lto_stats.num_output_il_bytes,
223 lto_stats.num_compressed_il_bytes);
224 if (lto_stats.num_output_il_bytes > 0)
226 const float dividend = (float) lto_stats.num_compressed_il_bytes;
227 const float divisor = (float) lto_stats.num_output_il_bytes;
228 fprintf (stderr, " (ratio: %f)", dividend / divisor);
230 fprintf (stderr, "\n");
233 if (flag_wpa)
235 fprintf (stderr, "[%s] # of output files: "
236 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
237 lto_stats.num_output_files);
239 fprintf (stderr, "[%s] # of output symtab nodes: "
240 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
241 lto_stats.num_output_symtab_nodes);
243 fprintf (stderr, "[%s] # of output tree pickle references: "
244 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
245 lto_stats.num_pickle_refs_output);
246 fprintf (stderr, "[%s] # of output tree bodies: "
247 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
248 lto_stats.num_tree_bodies_output);
250 fprintf (stderr, "[%s] # callgraph partitions: "
251 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
252 lto_stats.num_cgraph_partitions);
254 fprintf (stderr, "[%s] Compression: "
255 HOST_WIDE_INT_PRINT_UNSIGNED " input bytes, "
256 HOST_WIDE_INT_PRINT_UNSIGNED " uncompressed bytes", s,
257 lto_stats.num_input_il_bytes,
258 lto_stats.num_uncompressed_il_bytes);
259 if (lto_stats.num_input_il_bytes > 0)
261 const float dividend = (float) lto_stats.num_uncompressed_il_bytes;
262 const float divisor = (float) lto_stats.num_input_il_bytes;
263 fprintf (stderr, " (ratio: %f)", dividend / divisor);
265 fprintf (stderr, "\n");
268 for (i = 0; i < LTO_N_SECTION_TYPES; i++)
269 fprintf (stderr, "[%s] Size of mmap'd section %s: "
270 HOST_WIDE_INT_PRINT_UNSIGNED " bytes\n", s,
271 lto_section_name[i], lto_stats.section_size[i]);
275 #ifdef LTO_STREAMER_DEBUG
276 struct tree_hash_entry
278 tree key;
279 intptr_t value;
282 struct tree_entry_hasher : nofree_ptr_hash <tree_hash_entry>
284 static inline hashval_t hash (const value_type *);
285 static inline bool equal (const value_type *, const compare_type *);
288 inline hashval_t
289 tree_entry_hasher::hash (const value_type *e)
291 return htab_hash_pointer (e->key);
294 inline bool
295 tree_entry_hasher::equal (const value_type *e1, const compare_type *e2)
297 return (e1->key == e2->key);
300 static hash_table<tree_hash_entry> *tree_htab;
301 #endif
303 /* Initialization common to the LTO reader and writer. */
305 void
306 lto_streamer_init (void)
308 #ifdef ENABLE_CHECKING
309 /* Check that all the TS_* handled by the reader and writer routines
310 match exactly the structures defined in treestruct.def. When a
311 new TS_* astructure is added, the streamer should be updated to
312 handle it. */
313 streamer_check_handled_ts_structures ();
314 #endif
316 #ifdef LTO_STREAMER_DEBUG
317 tree_htab = new hash_table<tree_hash_entry> (31);
318 #endif
322 /* Gate function for all LTO streaming passes. */
324 bool
325 gate_lto_out (void)
327 return ((flag_generate_lto || flag_generate_offload || in_lto_p)
328 /* Don't bother doing anything if the program has errors. */
329 && !seen_error ());
333 #ifdef LTO_STREAMER_DEBUG
334 /* Add a mapping between T and ORIG_T, which is the numeric value of
335 the original address of T as it was seen by the LTO writer. This
336 mapping is useful when debugging streaming problems. A debugging
337 session can be started on both reader and writer using ORIG_T
338 as a breakpoint value in both sessions.
340 Note that this mapping is transient and only valid while T is
341 being reconstructed. Once T is fully built, the mapping is
342 removed. */
344 void
345 lto_orig_address_map (tree t, intptr_t orig_t)
347 struct tree_hash_entry ent;
348 struct tree_hash_entry **slot;
350 ent.key = t;
351 ent.value = orig_t;
352 slot = tree_htab->find_slot (&ent, INSERT);
353 gcc_assert (!*slot);
354 *slot = XNEW (struct tree_hash_entry);
355 **slot = ent;
359 /* Get the original address of T as it was seen by the writer. This
360 is only valid while T is being reconstructed. */
362 intptr_t
363 lto_orig_address_get (tree t)
365 struct tree_hash_entry ent;
366 struct tree_hash_entry **slot;
368 ent.key = t;
369 slot = tree_htab->find_slot (&ent, NO_INSERT);
370 return (slot ? (*slot)->value : 0);
374 /* Clear the mapping of T to its original address. */
376 void
377 lto_orig_address_remove (tree t)
379 struct tree_hash_entry ent;
380 struct tree_hash_entry **slot;
382 ent.key = t;
383 slot = tree_htab->find_slot (&ent, NO_INSERT);
384 gcc_assert (slot);
385 free (*slot);
386 tree_htab->clear_slot (slot);
388 #endif
391 /* Check that the version MAJOR.MINOR is the correct version number. */
393 void
394 lto_check_version (int major, int minor)
396 if (major != LTO_major_version || minor != LTO_minor_version)
397 fatal_error (input_location,
398 "bytecode stream generated with LTO version %d.%d instead "
399 "of the expected %d.%d",
400 major, minor,
401 LTO_major_version, LTO_minor_version);
405 /* Initialize all the streamer hooks used for streaming GIMPLE. */
407 void
408 lto_streamer_hooks_init (void)
410 streamer_hooks_init ();
411 streamer_hooks.write_tree = lto_output_tree;
412 streamer_hooks.read_tree = lto_input_tree;
413 streamer_hooks.input_location = lto_input_location;
414 streamer_hooks.output_location = lto_output_location;