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