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