* c-c++-common/ubsan/float-cast-overflow-6.c: Add i?86-*-* target.
[official-gcc.git] / gcc / lto-streamer.c
blobb6cac0b5e9976acb74489d54190912566538f6b6
1 /* Miscellaneous utilities for GIMPLE streaming. Things that are used
2 in both input and output are here.
4 Copyright (C) 2009-2014 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 "tree.h"
30 #include "predict.h"
31 #include "vec.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "bitmap.h"
45 #include "diagnostic-core.h"
46 #include "hash-map.h"
47 #include "plugin-api.h"
48 #include "ipa-ref.h"
49 #include "cgraph.h"
50 #include "tree-streamer.h"
51 #include "lto-streamer.h"
52 #include "lto-section-names.h"
53 #include "streamer-hooks.h"
55 /* Statistics gathered during LTO, WPA and LTRANS. */
56 struct lto_stats_d lto_stats;
58 /* LTO uses bitmaps with different life-times. So use a separate
59 obstack for all LTO bitmaps. */
60 static bitmap_obstack lto_obstack;
61 static bool lto_obstack_initialized;
64 /* Return a string representing LTO tag TAG. */
66 const char *
67 lto_tag_name (enum LTO_tags tag)
69 if (lto_tag_is_tree_code_p (tag))
71 /* For tags representing tree nodes, return the name of the
72 associated tree code. */
73 return get_tree_code_name (lto_tag_to_tree_code (tag));
76 if (lto_tag_is_gimple_code_p (tag))
78 /* For tags representing gimple statements, return the name of
79 the associated gimple code. */
80 return gimple_code_name[lto_tag_to_gimple_code (tag)];
83 switch (tag)
85 case LTO_null:
86 return "LTO_null";
87 case LTO_bb0:
88 return "LTO_bb0";
89 case LTO_bb1:
90 return "LTO_bb1";
91 case LTO_eh_region:
92 return "LTO_eh_region";
93 case LTO_function:
94 return "LTO_function";
95 case LTO_eh_table:
96 return "LTO_eh_table";
97 case LTO_ert_cleanup:
98 return "LTO_ert_cleanup";
99 case LTO_ert_try:
100 return "LTO_ert_try";
101 case LTO_ert_allowed_exceptions:
102 return "LTO_ert_allowed_exceptions";
103 case LTO_ert_must_not_throw:
104 return "LTO_ert_must_not_throw";
105 case LTO_tree_pickle_reference:
106 return "LTO_tree_pickle_reference";
107 case LTO_field_decl_ref:
108 return "LTO_field_decl_ref";
109 case LTO_function_decl_ref:
110 return "LTO_function_decl_ref";
111 case LTO_label_decl_ref:
112 return "LTO_label_decl_ref";
113 case LTO_namespace_decl_ref:
114 return "LTO_namespace_decl_ref";
115 case LTO_result_decl_ref:
116 return "LTO_result_decl_ref";
117 case LTO_ssa_name_ref:
118 return "LTO_ssa_name_ref";
119 case LTO_type_decl_ref:
120 return "LTO_type_decl_ref";
121 case LTO_type_ref:
122 return "LTO_type_ref";
123 case LTO_global_decl_ref:
124 return "LTO_global_decl_ref";
125 default:
126 return "LTO_UNKNOWN";
131 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
133 bitmap
134 lto_bitmap_alloc (void)
136 if (!lto_obstack_initialized)
138 bitmap_obstack_initialize (&lto_obstack);
139 lto_obstack_initialized = true;
141 return BITMAP_ALLOC (&lto_obstack);
144 /* Free bitmap B. */
146 void
147 lto_bitmap_free (bitmap b)
149 BITMAP_FREE (b);
153 /* Get a section name for a particular type or name. The NAME field
154 is only used if SECTION_TYPE is LTO_section_function_body. For all
155 others it is ignored. The callee of this function is responsible
156 to free the returned name. */
158 char *
159 lto_get_section_name (int section_type, const char *name, struct lto_file_decl_data *f)
161 const char *add;
162 char post[32];
163 const char *sep;
165 if (section_type == LTO_section_function_body)
167 gcc_assert (name != NULL);
168 if (name[0] == '*')
169 name++;
170 add = name;
171 sep = "";
173 else if (section_type < LTO_N_SECTION_TYPES)
175 add = lto_section_name[section_type];
176 sep = ".";
178 else
179 internal_error ("bytecode stream: unexpected LTO section %s", name);
181 /* Make the section name unique so that ld -r combining sections
182 doesn't confuse the reader with merged sections.
184 For options don't add a ID, the option reader cannot deal with them
185 and merging should be ok here. */
186 if (section_type == LTO_section_opts)
187 strcpy (post, "");
188 else if (f != NULL)
189 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, f->id);
190 else
191 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, get_random_seed (false));
192 return concat (LTO_SECTION_NAME_PREFIX, sep, add, post, NULL);
196 /* Show various memory usage statistics related to LTO. */
198 void
199 print_lto_report (const char *s)
201 unsigned i;
203 fprintf (stderr, "[%s] # of input files: "
204 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s, lto_stats.num_input_files);
206 fprintf (stderr, "[%s] # of input cgraph nodes: "
207 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
208 lto_stats.num_input_cgraph_nodes);
210 fprintf (stderr, "[%s] # of function bodies: "
211 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
212 lto_stats.num_function_bodies);
214 for (i = 0; i < NUM_TREE_CODES; i++)
215 if (lto_stats.num_trees[i])
216 fprintf (stderr, "[%s] # of '%s' objects read: "
217 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
218 get_tree_code_name ((enum tree_code) i), lto_stats.num_trees[i]);
220 if (flag_lto)
222 fprintf (stderr, "[%s] Compression: "
223 HOST_WIDE_INT_PRINT_UNSIGNED " output bytes, "
224 HOST_WIDE_INT_PRINT_UNSIGNED " compressed bytes", s,
225 lto_stats.num_output_il_bytes,
226 lto_stats.num_compressed_il_bytes);
227 if (lto_stats.num_output_il_bytes > 0)
229 const float dividend = (float) lto_stats.num_compressed_il_bytes;
230 const float divisor = (float) lto_stats.num_output_il_bytes;
231 fprintf (stderr, " (ratio: %f)", dividend / divisor);
233 fprintf (stderr, "\n");
236 if (flag_wpa)
238 fprintf (stderr, "[%s] # of output files: "
239 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
240 lto_stats.num_output_files);
242 fprintf (stderr, "[%s] # of output symtab nodes: "
243 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
244 lto_stats.num_output_symtab_nodes);
246 fprintf (stderr, "[%s] # of output tree pickle references: "
247 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
248 lto_stats.num_pickle_refs_output);
249 fprintf (stderr, "[%s] # of output tree bodies: "
250 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
251 lto_stats.num_tree_bodies_output);
253 fprintf (stderr, "[%s] # callgraph partitions: "
254 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
255 lto_stats.num_cgraph_partitions);
257 fprintf (stderr, "[%s] Compression: "
258 HOST_WIDE_INT_PRINT_UNSIGNED " input bytes, "
259 HOST_WIDE_INT_PRINT_UNSIGNED " uncompressed bytes", s,
260 lto_stats.num_input_il_bytes,
261 lto_stats.num_uncompressed_il_bytes);
262 if (lto_stats.num_input_il_bytes > 0)
264 const float dividend = (float) lto_stats.num_uncompressed_il_bytes;
265 const float divisor = (float) lto_stats.num_input_il_bytes;
266 fprintf (stderr, " (ratio: %f)", dividend / divisor);
268 fprintf (stderr, "\n");
271 for (i = 0; i < LTO_N_SECTION_TYPES; i++)
272 fprintf (stderr, "[%s] Size of mmap'd section %s: "
273 HOST_WIDE_INT_PRINT_UNSIGNED " bytes\n", s,
274 lto_section_name[i], lto_stats.section_size[i]);
278 #ifdef LTO_STREAMER_DEBUG
279 struct tree_hash_entry
281 tree key;
282 intptr_t value;
285 struct tree_entry_hasher : typed_noop_remove <tree_hash_entry>
287 typedef tree_hash_entry value_type;
288 typedef tree_hash_entry compare_type;
289 static inline hashval_t hash (const value_type *);
290 static inline bool equal (const value_type *, const compare_type *);
293 inline hashval_t
294 tree_entry_hasher::hash (const value_type *e)
296 return htab_hash_pointer (e->key);
299 inline bool
300 tree_entry_hasher::equal (const value_type *e1, const compare_type *e2)
302 return (e1->key == e2->key);
305 static hash_table<tree_hash_entry> *tree_htab;
306 #endif
308 /* Initialization common to the LTO reader and writer. */
310 void
311 lto_streamer_init (void)
313 /* Check that all the TS_* handled by the reader and writer routines
314 match exactly the structures defined in treestruct.def. When a
315 new TS_* astructure is added, the streamer should be updated to
316 handle it. */
317 streamer_check_handled_ts_structures ();
319 #ifdef LTO_STREAMER_DEBUG
320 tree_htab = new hash_table<tree_hash_entry> (31);
321 #endif
325 /* Gate function for all LTO streaming passes. */
327 bool
328 gate_lto_out (void)
330 return ((flag_generate_lto || in_lto_p)
331 /* Don't bother doing anything if the program has errors. */
332 && !seen_error ());
336 #ifdef LTO_STREAMER_DEBUG
337 /* Add a mapping between T and ORIG_T, which is the numeric value of
338 the original address of T as it was seen by the LTO writer. This
339 mapping is useful when debugging streaming problems. A debugging
340 session can be started on both reader and writer using ORIG_T
341 as a breakpoint value in both sessions.
343 Note that this mapping is transient and only valid while T is
344 being reconstructed. Once T is fully built, the mapping is
345 removed. */
347 void
348 lto_orig_address_map (tree t, intptr_t orig_t)
350 struct tree_hash_entry ent;
351 struct tree_hash_entry **slot;
353 ent.key = t;
354 ent.value = orig_t;
355 slot = tree_htab->find_slot (&ent, INSERT);
356 gcc_assert (!*slot);
357 *slot = XNEW (struct tree_hash_entry);
358 **slot = ent;
362 /* Get the original address of T as it was seen by the writer. This
363 is only valid while T is being reconstructed. */
365 intptr_t
366 lto_orig_address_get (tree t)
368 struct tree_hash_entry ent;
369 struct tree_hash_entry **slot;
371 ent.key = t;
372 slot = tree_htab->find_slot (&ent, NO_INSERT);
373 return (slot ? (*slot)->value : 0);
377 /* Clear the mapping of T to its original address. */
379 void
380 lto_orig_address_remove (tree t)
382 struct tree_hash_entry ent;
383 struct tree_hash_entry **slot;
385 ent.key = t;
386 slot = tree_htab->find_slot (&ent, NO_INSERT);
387 gcc_assert (slot);
388 free (*slot);
389 tree_htab->clear_slot (slot);
391 #endif
394 /* Check that the version MAJOR.MINOR is the correct version number. */
396 void
397 lto_check_version (int major, int minor)
399 if (major != LTO_major_version || minor != LTO_minor_version)
400 fatal_error ("bytecode stream generated with LTO version %d.%d instead "
401 "of the expected %d.%d",
402 major, minor,
403 LTO_major_version, LTO_minor_version);
407 /* Initialize all the streamer hooks used for streaming GIMPLE. */
409 void
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;