]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto-streamer.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / lto-streamer.c
1 /* Miscellaneous utilities for GIMPLE streaming. Things that are used
2 in both input and output are here.
3
4 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 Contributed by Doug Kwan <dougkwan@google.com>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
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 "hash-set.h"
30 #include "vec.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "predict.h"
38 #include "hard-reg-set.h"
39 #include "input.h"
40 #include "function.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-expr.h"
45 #include "is-a.h"
46 #include "gimple.h"
47 #include "bitmap.h"
48 #include "diagnostic-core.h"
49 #include "hash-map.h"
50 #include "plugin-api.h"
51 #include "ipa-ref.h"
52 #include "cgraph.h"
53 #include "tree-streamer.h"
54 #include "lto-streamer.h"
55 #include "lto-section-names.h"
56 #include "streamer-hooks.h"
57
58 /* Statistics gathered during LTO, WPA and LTRANS. */
59 struct lto_stats_d lto_stats;
60
61 /* LTO uses bitmaps with different life-times. So use a separate
62 obstack for all LTO bitmaps. */
63 static bitmap_obstack lto_obstack;
64 static bool lto_obstack_initialized;
65
66 const char *section_name_prefix = LTO_SECTION_NAME_PREFIX;
67 /* Set when streaming LTO for offloading compiler. */
68 bool lto_stream_offload_p;
69
70 /* Return a string representing LTO tag TAG. */
71
72 const char *
73 lto_tag_name (enum LTO_tags tag)
74 {
75 if (lto_tag_is_tree_code_p (tag))
76 {
77 /* For tags representing tree nodes, return the name of the
78 associated tree code. */
79 return get_tree_code_name (lto_tag_to_tree_code (tag));
80 }
81
82 if (lto_tag_is_gimple_code_p (tag))
83 {
84 /* For tags representing gimple statements, return the name of
85 the associated gimple code. */
86 return gimple_code_name[lto_tag_to_gimple_code (tag)];
87 }
88
89 switch (tag)
90 {
91 case LTO_null:
92 return "LTO_null";
93 case LTO_bb0:
94 return "LTO_bb0";
95 case LTO_bb1:
96 return "LTO_bb1";
97 case LTO_eh_region:
98 return "LTO_eh_region";
99 case LTO_function:
100 return "LTO_function";
101 case LTO_eh_table:
102 return "LTO_eh_table";
103 case LTO_ert_cleanup:
104 return "LTO_ert_cleanup";
105 case LTO_ert_try:
106 return "LTO_ert_try";
107 case LTO_ert_allowed_exceptions:
108 return "LTO_ert_allowed_exceptions";
109 case LTO_ert_must_not_throw:
110 return "LTO_ert_must_not_throw";
111 case LTO_tree_pickle_reference:
112 return "LTO_tree_pickle_reference";
113 case LTO_field_decl_ref:
114 return "LTO_field_decl_ref";
115 case LTO_function_decl_ref:
116 return "LTO_function_decl_ref";
117 case LTO_label_decl_ref:
118 return "LTO_label_decl_ref";
119 case LTO_namespace_decl_ref:
120 return "LTO_namespace_decl_ref";
121 case LTO_result_decl_ref:
122 return "LTO_result_decl_ref";
123 case LTO_ssa_name_ref:
124 return "LTO_ssa_name_ref";
125 case LTO_type_decl_ref:
126 return "LTO_type_decl_ref";
127 case LTO_type_ref:
128 return "LTO_type_ref";
129 case LTO_global_decl_ref:
130 return "LTO_global_decl_ref";
131 default:
132 return "LTO_UNKNOWN";
133 }
134 }
135
136
137 /* Allocate a bitmap from heap. Initializes the LTO obstack if necessary. */
138
139 bitmap
140 lto_bitmap_alloc (void)
141 {
142 if (!lto_obstack_initialized)
143 {
144 bitmap_obstack_initialize (&lto_obstack);
145 lto_obstack_initialized = true;
146 }
147 return BITMAP_ALLOC (&lto_obstack);
148 }
149
150 /* Free bitmap B. */
151
152 void
153 lto_bitmap_free (bitmap b)
154 {
155 BITMAP_FREE (b);
156 }
157
158
159 /* Get a section name for a particular type or name. The NAME field
160 is only used if SECTION_TYPE is LTO_section_function_body. For all
161 others it is ignored. The callee of this function is responsible
162 to free the returned name. */
163
164 char *
165 lto_get_section_name (int section_type, const char *name, struct lto_file_decl_data *f)
166 {
167 const char *add;
168 char post[32];
169 const char *sep;
170
171 if (section_type == LTO_section_function_body)
172 {
173 gcc_assert (name != NULL);
174 if (name[0] == '*')
175 name++;
176 add = name;
177 sep = "";
178 }
179 else if (section_type < LTO_N_SECTION_TYPES)
180 {
181 add = lto_section_name[section_type];
182 sep = ".";
183 }
184 else
185 internal_error ("bytecode stream: unexpected LTO section %s", name);
186
187 /* Make the section name unique so that ld -r combining sections
188 doesn't confuse the reader with merged sections.
189
190 For options don't add a ID, the option reader cannot deal with them
191 and merging should be ok here. */
192 if (section_type == LTO_section_opts)
193 strcpy (post, "");
194 else if (f != NULL)
195 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, f->id);
196 else
197 sprintf (post, "." HOST_WIDE_INT_PRINT_HEX_PURE, get_random_seed (false));
198 return concat (section_name_prefix, sep, add, post, NULL);
199 }
200
201
202 /* Show various memory usage statistics related to LTO. */
203
204 void
205 print_lto_report (const char *s)
206 {
207 unsigned i;
208
209 fprintf (stderr, "[%s] # of input files: "
210 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s, lto_stats.num_input_files);
211
212 fprintf (stderr, "[%s] # of input cgraph nodes: "
213 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
214 lto_stats.num_input_cgraph_nodes);
215
216 fprintf (stderr, "[%s] # of function bodies: "
217 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
218 lto_stats.num_function_bodies);
219
220 for (i = 0; i < NUM_TREE_CODES; i++)
221 if (lto_stats.num_trees[i])
222 fprintf (stderr, "[%s] # of '%s' objects read: "
223 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
224 get_tree_code_name ((enum tree_code) i), lto_stats.num_trees[i]);
225
226 if (flag_lto)
227 {
228 fprintf (stderr, "[%s] Compression: "
229 HOST_WIDE_INT_PRINT_UNSIGNED " output bytes, "
230 HOST_WIDE_INT_PRINT_UNSIGNED " compressed bytes", s,
231 lto_stats.num_output_il_bytes,
232 lto_stats.num_compressed_il_bytes);
233 if (lto_stats.num_output_il_bytes > 0)
234 {
235 const float dividend = (float) lto_stats.num_compressed_il_bytes;
236 const float divisor = (float) lto_stats.num_output_il_bytes;
237 fprintf (stderr, " (ratio: %f)", dividend / divisor);
238 }
239 fprintf (stderr, "\n");
240 }
241
242 if (flag_wpa)
243 {
244 fprintf (stderr, "[%s] # of output files: "
245 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
246 lto_stats.num_output_files);
247
248 fprintf (stderr, "[%s] # of output symtab nodes: "
249 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
250 lto_stats.num_output_symtab_nodes);
251
252 fprintf (stderr, "[%s] # of output tree pickle references: "
253 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
254 lto_stats.num_pickle_refs_output);
255 fprintf (stderr, "[%s] # of output tree bodies: "
256 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
257 lto_stats.num_tree_bodies_output);
258
259 fprintf (stderr, "[%s] # callgraph partitions: "
260 HOST_WIDE_INT_PRINT_UNSIGNED "\n", s,
261 lto_stats.num_cgraph_partitions);
262
263 fprintf (stderr, "[%s] Compression: "
264 HOST_WIDE_INT_PRINT_UNSIGNED " input bytes, "
265 HOST_WIDE_INT_PRINT_UNSIGNED " uncompressed bytes", s,
266 lto_stats.num_input_il_bytes,
267 lto_stats.num_uncompressed_il_bytes);
268 if (lto_stats.num_input_il_bytes > 0)
269 {
270 const float dividend = (float) lto_stats.num_uncompressed_il_bytes;
271 const float divisor = (float) lto_stats.num_input_il_bytes;
272 fprintf (stderr, " (ratio: %f)", dividend / divisor);
273 }
274 fprintf (stderr, "\n");
275 }
276
277 for (i = 0; i < LTO_N_SECTION_TYPES; i++)
278 fprintf (stderr, "[%s] Size of mmap'd section %s: "
279 HOST_WIDE_INT_PRINT_UNSIGNED " bytes\n", s,
280 lto_section_name[i], lto_stats.section_size[i]);
281 }
282
283
284 #ifdef LTO_STREAMER_DEBUG
285 struct tree_hash_entry
286 {
287 tree key;
288 intptr_t value;
289 };
290
291 struct tree_entry_hasher : typed_noop_remove <tree_hash_entry>
292 {
293 typedef tree_hash_entry value_type;
294 typedef tree_hash_entry compare_type;
295 static inline hashval_t hash (const value_type *);
296 static inline bool equal (const value_type *, const compare_type *);
297 };
298
299 inline hashval_t
300 tree_entry_hasher::hash (const value_type *e)
301 {
302 return htab_hash_pointer (e->key);
303 }
304
305 inline bool
306 tree_entry_hasher::equal (const value_type *e1, const compare_type *e2)
307 {
308 return (e1->key == e2->key);
309 }
310
311 static hash_table<tree_hash_entry> *tree_htab;
312 #endif
313
314 /* Initialization common to the LTO reader and writer. */
315
316 void
317 lto_streamer_init (void)
318 {
319 #ifdef ENABLE_CHECKING
320 /* Check that all the TS_* handled by the reader and writer routines
321 match exactly the structures defined in treestruct.def. When a
322 new TS_* astructure is added, the streamer should be updated to
323 handle it. */
324 streamer_check_handled_ts_structures ();
325 #endif
326
327 #ifdef LTO_STREAMER_DEBUG
328 tree_htab = new hash_table<tree_hash_entry> (31);
329 #endif
330 }
331
332
333 /* Gate function for all LTO streaming passes. */
334
335 bool
336 gate_lto_out (void)
337 {
338 return ((flag_generate_lto || flag_generate_offload || in_lto_p)
339 /* Don't bother doing anything if the program has errors. */
340 && !seen_error ());
341 }
342
343
344 #ifdef LTO_STREAMER_DEBUG
345 /* Add a mapping between T and ORIG_T, which is the numeric value of
346 the original address of T as it was seen by the LTO writer. This
347 mapping is useful when debugging streaming problems. A debugging
348 session can be started on both reader and writer using ORIG_T
349 as a breakpoint value in both sessions.
350
351 Note that this mapping is transient and only valid while T is
352 being reconstructed. Once T is fully built, the mapping is
353 removed. */
354
355 void
356 lto_orig_address_map (tree t, intptr_t orig_t)
357 {
358 struct tree_hash_entry ent;
359 struct tree_hash_entry **slot;
360
361 ent.key = t;
362 ent.value = orig_t;
363 slot = tree_htab->find_slot (&ent, INSERT);
364 gcc_assert (!*slot);
365 *slot = XNEW (struct tree_hash_entry);
366 **slot = ent;
367 }
368
369
370 /* Get the original address of T as it was seen by the writer. This
371 is only valid while T is being reconstructed. */
372
373 intptr_t
374 lto_orig_address_get (tree t)
375 {
376 struct tree_hash_entry ent;
377 struct tree_hash_entry **slot;
378
379 ent.key = t;
380 slot = tree_htab->find_slot (&ent, NO_INSERT);
381 return (slot ? (*slot)->value : 0);
382 }
383
384
385 /* Clear the mapping of T to its original address. */
386
387 void
388 lto_orig_address_remove (tree t)
389 {
390 struct tree_hash_entry ent;
391 struct tree_hash_entry **slot;
392
393 ent.key = t;
394 slot = tree_htab->find_slot (&ent, NO_INSERT);
395 gcc_assert (slot);
396 free (*slot);
397 tree_htab->clear_slot (slot);
398 }
399 #endif
400
401
402 /* Check that the version MAJOR.MINOR is the correct version number. */
403
404 void
405 lto_check_version (int major, int minor)
406 {
407 if (major != LTO_major_version || minor != LTO_minor_version)
408 fatal_error (input_location,
409 "bytecode stream generated with LTO version %d.%d instead "
410 "of the expected %d.%d",
411 major, minor,
412 LTO_major_version, LTO_minor_version);
413 }
414
415
416 /* Initialize all the streamer hooks used for streaming GIMPLE. */
417
418 void
419 lto_streamer_hooks_init (void)
420 {
421 streamer_hooks_init ();
422 streamer_hooks.write_tree = lto_output_tree;
423 streamer_hooks.read_tree = lto_input_tree;
424 streamer_hooks.input_location = lto_input_location;
425 streamer_hooks.output_location = lto_output_location;
426 }