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