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