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