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