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