2015-11-23 Matthias Klose <doko@ubuntu.com>
[official-gcc.git] / gcc / lto-section-out.c
blob56b460bea5423327f152d1d5a50fdb2ae20518d3
1 /* Functions for writing LTO sections.
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cgraph.h"
30 #include "data-streamer.h"
31 #include "langhooks.h"
32 #include "lto-compress.h"
34 static vec<lto_out_decl_state_ptr> decl_state_stack;
36 /* List of out decl states used by functions. We use this to
37 generate the decl directory later. */
39 vec<lto_out_decl_state_ptr> lto_function_decl_states;
42 /*****************************************************************************
43 Output routines shared by all of the serialization passes.
44 *****************************************************************************/
47 /* Flush compressed stream data function, sends NUM_CHARS from CHARS
48 to the append lang hook, OPAQUE is currently always NULL. */
50 static void
51 lto_append_data (const char *chars, unsigned int num_chars, void *opaque)
53 gcc_assert (opaque == NULL);
54 lang_hooks.lto.append_data (chars, num_chars, opaque);
57 /* Pointer to the current compression stream. */
59 static struct lto_compression_stream *compression_stream = NULL;
61 /* Begin a new output section named NAME. If COMPRESS is true, zlib compress
62 the section. */
64 void
65 lto_begin_section (const char *name, bool compress)
67 lang_hooks.lto.begin_section (name);
69 /* FIXME lto: for now, suppress compression if the lang_hook that appends
70 data is anything other than assembler output. The effect here is that
71 we get compression of IL only in non-ltrans object files. */
72 gcc_assert (compression_stream == NULL);
73 if (compress)
74 compression_stream = lto_start_compression (lto_append_data, NULL);
78 /* End the current output section. */
80 void
81 lto_end_section (void)
83 if (compression_stream)
85 lto_end_compression (compression_stream);
86 compression_stream = NULL;
88 lang_hooks.lto.end_section ();
91 /* Write SIZE bytes starting at DATA to the assembler. */
93 void
94 lto_write_data (const void *data, unsigned int size)
96 if (compression_stream)
97 lto_compress_block (compression_stream, (const char *)data, size);
98 else
99 lang_hooks.lto.append_data ((const char *)data, size, NULL);
102 /* Write all of the chars in OBS to the assembler. Recycle the blocks
103 in obs as this is being done. */
105 void
106 lto_write_stream (struct lto_output_stream *obs)
108 unsigned int block_size = 1024;
109 struct lto_char_ptr_base *block;
110 struct lto_char_ptr_base *next_block;
111 if (!obs->first_block)
112 return;
114 for (block = obs->first_block; block; block = next_block)
116 const char *base = ((char *)block) + sizeof (struct lto_char_ptr_base);
117 unsigned int num_chars = block_size - sizeof (struct lto_char_ptr_base);
119 /* If this is not the last block, it is full. If it is the last
120 block, left_in_block indicates how many chars are unoccupied in
121 this block; subtract from num_chars to obtain occupancy. */
122 next_block = (struct lto_char_ptr_base *) block->ptr;
123 if (!next_block)
124 num_chars -= obs->left_in_block;
126 /* FIXME lto: WPA mode uses an ELF function as a lang_hook to append
127 output data. This hook is not happy with the way that compression
128 blocks up output differently to the way it's blocked here. So for
129 now, we don't compress WPA output. */
130 if (compression_stream)
131 lto_compress_block (compression_stream, base, num_chars);
132 else
133 lang_hooks.lto.append_data (base, num_chars, block);
134 free (block);
135 block_size *= 2;
140 /* Lookup NAME in ENCODER. If NAME is not found, create a new entry in
141 ENCODER for NAME with the next available index of ENCODER, then
142 print the index to OBS. True is returned if NAME was added to
143 ENCODER. The resulting index is stored in THIS_INDEX.
145 If OBS is NULL, the only action is to add NAME to the encoder. */
147 bool
148 lto_output_decl_index (struct lto_output_stream *obs,
149 struct lto_tree_ref_encoder *encoder,
150 tree name, unsigned int *this_index)
152 bool new_entry_p = FALSE;
153 bool existed_p;
155 unsigned int &index
156 = encoder->tree_hash_table->get_or_insert (name, &existed_p);
157 if (!existed_p)
159 index = encoder->trees.length ();
160 encoder->trees.safe_push (name);
161 new_entry_p = TRUE;
164 if (obs)
165 streamer_write_uhwi_stream (obs, index);
166 *this_index = index;
167 return new_entry_p;
170 /* Output a field DECL to OBS. */
172 void
173 lto_output_field_decl_index (struct lto_out_decl_state *decl_state,
174 struct lto_output_stream * obs, tree decl)
176 unsigned int index;
177 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FIELD_DECL],
178 decl, &index);
181 /* Output a function DECL to OBS. */
183 void
184 lto_output_fn_decl_index (struct lto_out_decl_state *decl_state,
185 struct lto_output_stream * obs, tree decl)
187 unsigned int index;
188 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FN_DECL],
189 decl, &index);
192 /* Output a namespace DECL to OBS. */
194 void
195 lto_output_namespace_decl_index (struct lto_out_decl_state *decl_state,
196 struct lto_output_stream * obs, tree decl)
198 unsigned int index;
199 lto_output_decl_index (obs,
200 &decl_state->streams[LTO_DECL_STREAM_NAMESPACE_DECL],
201 decl, &index);
204 /* Output a static or extern var DECL to OBS. */
206 void
207 lto_output_var_decl_index (struct lto_out_decl_state *decl_state,
208 struct lto_output_stream * obs, tree decl)
210 unsigned int index;
211 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_VAR_DECL],
212 decl, &index);
215 /* Output a type DECL to OBS. */
217 void
218 lto_output_type_decl_index (struct lto_out_decl_state *decl_state,
219 struct lto_output_stream * obs, tree decl)
221 unsigned int index;
222 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE_DECL],
223 decl, &index);
226 /* Output a type REF to OBS. */
228 void
229 lto_output_type_ref_index (struct lto_out_decl_state *decl_state,
230 struct lto_output_stream *obs, tree ref)
232 unsigned int index;
233 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE],
234 ref, &index);
238 /* Create the output block and return it. */
240 struct lto_simple_output_block *
241 lto_create_simple_output_block (enum lto_section_type section_type)
243 struct lto_simple_output_block *ob
244 = ((struct lto_simple_output_block *)
245 xcalloc (1, sizeof (struct lto_simple_output_block)));
247 ob->section_type = section_type;
248 ob->decl_state = lto_get_out_decl_state ();
249 ob->main_stream = ((struct lto_output_stream *)
250 xcalloc (1, sizeof (struct lto_output_stream)));
252 return ob;
256 /* Produce a simple section for one of the ipa passes. */
258 void
259 lto_destroy_simple_output_block (struct lto_simple_output_block *ob)
261 char *section_name;
262 struct lto_simple_header header;
264 section_name = lto_get_section_name (ob->section_type, NULL, NULL);
265 lto_begin_section (section_name, !flag_wpa);
266 free (section_name);
268 /* Write the header which says how to decode the pieces of the
269 t. */
270 memset (&header, 0, sizeof (struct lto_simple_header));
271 header.major_version = LTO_major_version;
272 header.minor_version = LTO_minor_version;
273 header.main_size = ob->main_stream->total_size;
274 lto_write_data (&header, sizeof header);
276 lto_write_stream (ob->main_stream);
278 /* Put back the assembly section that was there before we started
279 writing lto info. */
280 lto_end_section ();
282 free (ob->main_stream);
283 free (ob);
287 /* Return a new lto_out_decl_state. */
289 struct lto_out_decl_state *
290 lto_new_out_decl_state (void)
292 struct lto_out_decl_state *state = XCNEW (struct lto_out_decl_state);
293 int i;
295 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
296 lto_init_tree_ref_encoder (&state->streams[i]);
298 return state;
302 /* Delete STATE and components. */
304 void
305 lto_delete_out_decl_state (struct lto_out_decl_state *state)
307 int i;
309 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
310 lto_destroy_tree_ref_encoder (&state->streams[i]);
312 free (state);
316 /* Get the currently used lto_out_decl_state structure. */
318 struct lto_out_decl_state *
319 lto_get_out_decl_state (void)
321 return decl_state_stack.last ();
324 /* Push STATE to top of out decl stack. */
326 void
327 lto_push_out_decl_state (struct lto_out_decl_state *state)
329 decl_state_stack.safe_push (state);
332 /* Pop the currently used out-decl state from top of stack. */
334 struct lto_out_decl_state *
335 lto_pop_out_decl_state (void)
337 return decl_state_stack.pop ();
340 /* Record STATE after it has been used in serializing the body of
341 FN_DECL. STATE should no longer be used by the caller. The ownership
342 of it is taken over from this point. */
344 void
345 lto_record_function_out_decl_state (tree fn_decl,
346 struct lto_out_decl_state *state)
348 int i;
350 /* Strip all hash tables to save some memory. */
351 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
352 if (state->streams[i].tree_hash_table)
354 delete state->streams[i].tree_hash_table;
355 state->streams[i].tree_hash_table = NULL;
357 state->fn_decl = fn_decl;
358 lto_function_decl_states.safe_push (state);