/cp
[official-gcc.git] / gcc / lto-streamer.c
blob57e6ea55ccdd005dbee1789f04772a5a7ccd3736
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"
40 #include "streamer-hooks.h"
42 /* Statistics gathered during LTO, WPA and LTRANS. */
43 struct lto_stats_d lto_stats;
45 /* LTO uses bitmaps with different life-times. So use a separate
46 obstack for all LTO bitmaps. */
47 static bitmap_obstack lto_obstack;
48 static bool lto_obstack_initialized;
50 const char *section_name_prefix = LTO_SECTION_NAME_PREFIX;
51 /* Set when streaming LTO for offloading compiler. */
52 bool lto_stream_offload_p;
54 /* Return a string representing LTO tag TAG. */
56 const char *
57 lto_tag_name (enum LTO_tags tag)
59 if (lto_tag_is_tree_code_p (tag))
61 /* For tags representing tree nodes, return the name of the
62 associated tree code. */
63 return get_tree_code_name (lto_tag_to_tree_code (tag));
66 if (lto_tag_is_gimple_code_p (tag))
68 /* For tags representing gimple statements, return the name of
69 the associated gimple code. */
70 return gimple_code_name[lto_tag_to_gimple_code (tag)];
73 switch (tag)
75 case LTO_null:
76 return "LTO_null";
77 case LTO_bb0:
78 return "LTO_bb0";
79 case LTO_bb1:
80 return "LTO_bb1";
81 case LTO_eh_region:
82 return "LTO_eh_region";
83 case LTO_function:
84 return "LTO_function";
85 case LTO_eh_table:
86 return "LTO_eh_table";
87 case LTO_ert_cleanup:
88 return "LTO_ert_cleanup";
89 case LTO_ert_try:
90 return "LTO_ert_try";
91 case LTO_ert_allowed_exceptions:
92 return "LTO_ert_allowed_exceptions";
93 case LTO_ert_must_not_throw:
94 return "LTO_ert_must_not_throw";
95 case LTO_tree_pickle_reference:
96 return "LTO_tree_pickle_reference";
97 case LTO_field_decl_ref:
98 return "LTO_field_decl_ref";
99 case LTO_function_decl_ref:
100 return "LTO_function_decl_ref";
101 case LTO_label_decl_ref:
102 return "LTO_label_decl_ref";
103 case LTO_namespace_decl_ref:
104 return "LTO_namespace_decl_ref";
105 case LTO_result_decl_ref:
106 return "LTO_result_decl_ref";
107 case LTO_ssa_name_ref:
108 return "LTO_ssa_name_ref";
109 case LTO_type_decl_ref:
110 return "LTO_type_decl_ref";
111 case LTO_type_ref:
112 return "LTO_type_ref";
113 case LTO_global_decl_ref:
114 return "LTO_global_decl_ref";
115 default:
116 return "LTO_UNKNOWN";
121 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
123 bitmap
124 lto_bitmap_alloc (void)
126 if (!lto_obstack_initialized)
128 bitmap_obstack_initialize (&lto_obstack);
129 lto_obstack_initialized = true;
131 return BITMAP_ALLOC (&lto_obstack);
134 /* Free bitmap B. */
136 void
137 lto_bitmap_free (bitmap b)
139 BITMAP_FREE (b);
143 /* Get a section name for a particular type or name. The NAME field
144 is only used if SECTION_TYPE is LTO_section_function_body. For all
145 others it is ignored. The callee of this function is responsible
146 to free the returned name. */
148 char *
149 lto_get_section_name (int section_type, const char *name, struct lto_file_decl_data *f)
151 const char *add;
152 char post[32];
153 const char *sep;
155 if (section_type == LTO_section_function_body)
157 gcc_assert (name != NULL);
158 if (name[0] == '*')
159 name++;
160 add = name;
161 sep = "";
163 else if (section_type < LTO_N_SECTION_TYPES)
165 add = lto_section_name[section_type];
166 sep = ".";
168 else
169 internal_error ("bytecode stream: unexpected LTO section %s", name);
171 /* Make the section name unique so that ld -r combining sections
172 doesn't confuse the reader with merged sections.
174 For options don't add a ID, the option reader cannot deal with them
175 and merging should be ok here. */
176 if (section_type == LTO_section_opts)
177 strcpy (post, "");
178 else if (f != NULL)
179 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, f->id);
180 else
181 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, get_random_seed (false));
182 return concat (section_name_prefix, sep, add, post, NULL);
186 /* Show various memory usage statistics related to LTO. */
188 void
189 print_lto_report (const char *s)
191 unsigned i;
193 fprintf (stderr, "[%s] # of input files: "
194 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s, lto_stats.num_input_files);
196 fprintf (stderr, "[%s] # of input cgraph nodes: "
197 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
198 lto_stats.num_input_cgraph_nodes);
200 fprintf (stderr, "[%s] # of function bodies: "
201 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
202 lto_stats.num_function_bodies);
204 for (i = 0; i < NUM_TREE_CODES; i++)
205 if (lto_stats.num_trees[i])
206 fprintf (stderr, "[%s] # of '%s' objects read: "
207 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
208 get_tree_code_name ((enum tree_code) i), lto_stats.num_trees[i]);
210 if (flag_lto)
212 fprintf (stderr, "[%s] Compression: "
213 HOST_WIDE_INT_PRINT_UNSIGNED " output bytes, "
214 HOST_WIDE_INT_PRINT_UNSIGNED " compressed bytes", s,
215 lto_stats.num_output_il_bytes,
216 lto_stats.num_compressed_il_bytes);
217 if (lto_stats.num_output_il_bytes > 0)
219 const float dividend = (float) lto_stats.num_compressed_il_bytes;
220 const float divisor = (float) lto_stats.num_output_il_bytes;
221 fprintf (stderr, " (ratio: %f)", dividend / divisor);
223 fprintf (stderr, "\n");
226 if (flag_wpa)
228 fprintf (stderr, "[%s] # of output files: "
229 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
230 lto_stats.num_output_files);
232 fprintf (stderr, "[%s] # of output symtab nodes: "
233 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
234 lto_stats.num_output_symtab_nodes);
236 fprintf (stderr, "[%s] # of output tree pickle references: "
237 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
238 lto_stats.num_pickle_refs_output);
239 fprintf (stderr, "[%s] # of output tree bodies: "
240 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
241 lto_stats.num_tree_bodies_output);
243 fprintf (stderr, "[%s] # callgraph partitions: "
244 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
245 lto_stats.num_cgraph_partitions);
247 fprintf (stderr, "[%s] Compression: "
248 HOST_WIDE_INT_PRINT_UNSIGNED " input bytes, "
249 HOST_WIDE_INT_PRINT_UNSIGNED " uncompressed bytes", s,
250 lto_stats.num_input_il_bytes,
251 lto_stats.num_uncompressed_il_bytes);
252 if (lto_stats.num_input_il_bytes > 0)
254 const float dividend = (float) lto_stats.num_uncompressed_il_bytes;
255 const float divisor = (float) lto_stats.num_input_il_bytes;
256 fprintf (stderr, " (ratio: %f)", dividend / divisor);
258 fprintf (stderr, "\n");
261 for (i = 0; i < LTO_N_SECTION_TYPES; i++)
262 fprintf (stderr, "[%s] Size of mmap'd section %s: "
263 HOST_WIDE_INT_PRINT_UNSIGNED " bytes\n", s,
264 lto_section_name[i], lto_stats.section_size[i]);
268 #ifdef LTO_STREAMER_DEBUG
269 struct tree_hash_entry
271 tree key;
272 intptr_t value;
275 struct tree_entry_hasher : nofree_ptr_hash <tree_hash_entry>
277 static inline hashval_t hash (const value_type *);
278 static inline bool equal (const value_type *, const compare_type *);
281 inline hashval_t
282 tree_entry_hasher::hash (const value_type *e)
284 return htab_hash_pointer (e->key);
287 inline bool
288 tree_entry_hasher::equal (const value_type *e1, const compare_type *e2)
290 return (e1->key == e2->key);
293 static hash_table<tree_hash_entry> *tree_htab;
294 #endif
296 /* Initialization common to the LTO reader and writer. */
298 void
299 lto_streamer_init (void)
301 #ifdef ENABLE_CHECKING
302 /* Check that all the TS_* handled by the reader and writer routines
303 match exactly the structures defined in treestruct.def. When a
304 new TS_* astructure is added, the streamer should be updated to
305 handle it. */
306 streamer_check_handled_ts_structures ();
307 #endif
309 #ifdef LTO_STREAMER_DEBUG
310 tree_htab = new hash_table<tree_hash_entry> (31);
311 #endif
315 /* Gate function for all LTO streaming passes. */
317 bool
318 gate_lto_out (void)
320 return ((flag_generate_lto || flag_generate_offload || in_lto_p)
321 /* Don't bother doing anything if the program has errors. */
322 && !seen_error ());
326 #ifdef LTO_STREAMER_DEBUG
327 /* Add a mapping between T and ORIG_T, which is the numeric value of
328 the original address of T as it was seen by the LTO writer. This
329 mapping is useful when debugging streaming problems. A debugging
330 session can be started on both reader and writer using ORIG_T
331 as a breakpoint value in both sessions.
333 Note that this mapping is transient and only valid while T is
334 being reconstructed. Once T is fully built, the mapping is
335 removed. */
337 void
338 lto_orig_address_map (tree t, intptr_t orig_t)
340 struct tree_hash_entry ent;
341 struct tree_hash_entry **slot;
343 ent.key = t;
344 ent.value = orig_t;
345 slot = tree_htab->find_slot (&ent, INSERT);
346 gcc_assert (!*slot);
347 *slot = XNEW (struct tree_hash_entry);
348 **slot = ent;
352 /* Get the original address of T as it was seen by the writer. This
353 is only valid while T is being reconstructed. */
355 intptr_t
356 lto_orig_address_get (tree t)
358 struct tree_hash_entry ent;
359 struct tree_hash_entry **slot;
361 ent.key = t;
362 slot = tree_htab->find_slot (&ent, NO_INSERT);
363 return (slot ? (*slot)->value : 0);
367 /* Clear the mapping of T to its original address. */
369 void
370 lto_orig_address_remove (tree t)
372 struct tree_hash_entry ent;
373 struct tree_hash_entry **slot;
375 ent.key = t;
376 slot = tree_htab->find_slot (&ent, NO_INSERT);
377 gcc_assert (slot);
378 free (*slot);
379 tree_htab->clear_slot (slot);
381 #endif
384 /* Check that the version MAJOR.MINOR is the correct version number. */
386 void
387 lto_check_version (int major, int minor)
389 if (major != LTO_major_version || minor != LTO_minor_version)
390 fatal_error (input_location,
391 "bytecode stream generated with LTO version %d.%d instead "
392 "of the expected %d.%d",
393 major, minor,
394 LTO_major_version, LTO_minor_version);
398 /* Initialize all the streamer hooks used for streaming GIMPLE. */
400 void
401 lto_streamer_hooks_init (void)
403 streamer_hooks_init ();
404 streamer_hooks.write_tree = lto_output_tree;
405 streamer_hooks.read_tree = lto_input_tree;
406 streamer_hooks.input_location = lto_input_location;
407 streamer_hooks.output_location = lto_output_location;