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