vectorizer cost model enhancement
[official-gcc.git] / gcc / lto-section-out.c
blob59eed71663046527d6548eacf11aabd88e6771b1
1 /* Functions for writing LTO sections.
3 Copyright (C) 2009-2013 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 "tm.h"
26 #include "tree.h"
27 #include "expr.h"
28 #include "params.h"
29 #include "input.h"
30 #include "hashtab.h"
31 #include "basic-block.h"
32 #include "tree-ssa.h"
33 #include "cgraph.h"
34 #include "function.h"
35 #include "ggc.h"
36 #include "except.h"
37 #include "vec.h"
38 #include "pointer-set.h"
39 #include "bitmap.h"
40 #include "langhooks.h"
41 #include "data-streamer.h"
42 #include "lto-streamer.h"
43 #include "lto-compress.h"
45 static vec<lto_out_decl_state_ptr> decl_state_stack;
47 /* List of out decl states used by functions. We use this to
48 generate the decl directory later. */
50 vec<lto_out_decl_state_ptr> lto_function_decl_states;
53 /*****************************************************************************
54 Output routines shared by all of the serialization passes.
55 *****************************************************************************/
58 /* Flush compressed stream data function, sends NUM_CHARS from CHARS
59 to the append lang hook, OPAQUE is currently always NULL. */
61 static void
62 lto_append_data (const char *chars, unsigned int num_chars, void *opaque)
64 gcc_assert (opaque == NULL);
65 lang_hooks.lto.append_data (chars, num_chars, opaque);
68 /* Pointer to the current compression stream. */
70 static struct lto_compression_stream *compression_stream = NULL;
72 /* Begin a new output section named NAME. If COMPRESS is true, zlib compress
73 the section. */
75 void
76 lto_begin_section (const char *name, bool compress)
78 lang_hooks.lto.begin_section (name);
80 /* FIXME lto: for now, suppress compression if the lang_hook that appends
81 data is anything other than assembler output. The effect here is that
82 we get compression of IL only in non-ltrans object files. */
83 gcc_assert (compression_stream == NULL);
84 if (compress)
85 compression_stream = lto_start_compression (lto_append_data, NULL);
89 /* End the current output section. */
91 void
92 lto_end_section (void)
94 if (compression_stream)
96 lto_end_compression (compression_stream);
97 compression_stream = NULL;
99 lang_hooks.lto.end_section ();
103 /* Write all of the chars in OBS to the assembler. Recycle the blocks
104 in obs as this is being done. */
106 void
107 lto_write_stream (struct lto_output_stream *obs)
109 unsigned int block_size = 1024;
110 struct lto_char_ptr_base *block;
111 struct lto_char_ptr_base *next_block;
112 if (!obs->first_block)
113 return;
115 for (block = obs->first_block; block; block = next_block)
117 const char *base = ((char *)block) + sizeof (struct lto_char_ptr_base);
118 unsigned int num_chars = block_size - sizeof (struct lto_char_ptr_base);
120 /* If this is not the last block, it is full. If it is the last
121 block, left_in_block indicates how many chars are unoccupied in
122 this block; subtract from num_chars to obtain occupancy. */
123 next_block = (struct lto_char_ptr_base *) block->ptr;
124 if (!next_block)
125 num_chars -= obs->left_in_block;
127 /* FIXME lto: WPA mode uses an ELF function as a lang_hook to append
128 output data. This hook is not happy with the way that compression
129 blocks up output differently to the way it's blocked here. So for
130 now, we don't compress WPA output. */
131 if (compression_stream)
133 lto_compress_block (compression_stream, base, num_chars);
134 lang_hooks.lto.append_data (NULL, 0, block);
136 else
137 lang_hooks.lto.append_data (base, num_chars, block);
138 block_size *= 2;
143 /* Adds a new block to output stream OBS. */
145 void
146 lto_append_block (struct lto_output_stream *obs)
148 struct lto_char_ptr_base *new_block;
150 gcc_assert (obs->left_in_block == 0);
152 if (obs->first_block == NULL)
154 /* This is the first time the stream has been written
155 into. */
156 obs->block_size = 1024;
157 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
158 obs->first_block = new_block;
160 else
162 struct lto_char_ptr_base *tptr;
163 /* Get a new block that is twice as big as the last block
164 and link it into the list. */
165 obs->block_size *= 2;
166 new_block = (struct lto_char_ptr_base*) xmalloc (obs->block_size);
167 /* The first bytes of the block are reserved as a pointer to
168 the next block. Set the chain of the full block to the
169 pointer to the new block. */
170 tptr = obs->current_block;
171 tptr->ptr = (char *) new_block;
174 /* Set the place for the next char at the first position after the
175 chain to the next block. */
176 obs->current_pointer
177 = ((char *) new_block) + sizeof (struct lto_char_ptr_base);
178 obs->current_block = new_block;
179 /* Null out the newly allocated block's pointer to the next block. */
180 new_block->ptr = NULL;
181 obs->left_in_block = obs->block_size - sizeof (struct lto_char_ptr_base);
185 /* Write raw DATA of length LEN to the output block OB. */
187 void
188 lto_output_data_stream (struct lto_output_stream *obs, const void *data,
189 size_t len)
191 while (len)
193 size_t copy;
195 /* No space left. */
196 if (obs->left_in_block == 0)
197 lto_append_block (obs);
199 /* Determine how many bytes to copy in this loop. */
200 if (len <= obs->left_in_block)
201 copy = len;
202 else
203 copy = obs->left_in_block;
205 /* Copy the data and do bookkeeping. */
206 memcpy (obs->current_pointer, data, copy);
207 obs->current_pointer += copy;
208 obs->total_size += copy;
209 obs->left_in_block -= copy;
210 data = (const char *) data + copy;
211 len -= copy;
216 /* Lookup NAME in ENCODER. If NAME is not found, create a new entry in
217 ENCODER for NAME with the next available index of ENCODER, then
218 print the index to OBS. True is returned if NAME was added to
219 ENCODER. The resulting index is stored in THIS_INDEX.
221 If OBS is NULL, the only action is to add NAME to the encoder. */
223 bool
224 lto_output_decl_index (struct lto_output_stream *obs,
225 struct lto_tree_ref_encoder *encoder,
226 tree name, unsigned int *this_index)
228 unsigned *slot;
229 unsigned int index;
230 bool new_entry_p = FALSE;
231 bool existed_p;
233 slot = encoder->tree_hash_table->insert (name, &existed_p);
234 if (!existed_p)
236 index = encoder->trees.length ();
237 *slot = index;
238 encoder->trees.safe_push (name);
239 new_entry_p = TRUE;
241 else
242 index = *slot;
244 if (obs)
245 streamer_write_uhwi_stream (obs, index);
246 *this_index = index;
247 return new_entry_p;
250 /* Output a field DECL to OBS. */
252 void
253 lto_output_field_decl_index (struct lto_out_decl_state *decl_state,
254 struct lto_output_stream * obs, tree decl)
256 unsigned int index;
257 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FIELD_DECL],
258 decl, &index);
261 /* Output a function DECL to OBS. */
263 void
264 lto_output_fn_decl_index (struct lto_out_decl_state *decl_state,
265 struct lto_output_stream * obs, tree decl)
267 unsigned int index;
268 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FN_DECL],
269 decl, &index);
272 /* Output a namespace DECL to OBS. */
274 void
275 lto_output_namespace_decl_index (struct lto_out_decl_state *decl_state,
276 struct lto_output_stream * obs, tree decl)
278 unsigned int index;
279 lto_output_decl_index (obs,
280 &decl_state->streams[LTO_DECL_STREAM_NAMESPACE_DECL],
281 decl, &index);
284 /* Output a static or extern var DECL to OBS. */
286 void
287 lto_output_var_decl_index (struct lto_out_decl_state *decl_state,
288 struct lto_output_stream * obs, tree decl)
290 unsigned int index;
291 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_VAR_DECL],
292 decl, &index);
295 /* Output a type DECL to OBS. */
297 void
298 lto_output_type_decl_index (struct lto_out_decl_state *decl_state,
299 struct lto_output_stream * obs, tree decl)
301 unsigned int index;
302 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE_DECL],
303 decl, &index);
306 /* Output a type REF to OBS. */
308 void
309 lto_output_type_ref_index (struct lto_out_decl_state *decl_state,
310 struct lto_output_stream *obs, tree ref)
312 unsigned int index;
313 lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE],
314 ref, &index);
318 /* Create the output block and return it. */
320 struct lto_simple_output_block *
321 lto_create_simple_output_block (enum lto_section_type section_type)
323 struct lto_simple_output_block *ob
324 = ((struct lto_simple_output_block *)
325 xcalloc (1, sizeof (struct lto_simple_output_block)));
327 ob->section_type = section_type;
328 ob->decl_state = lto_get_out_decl_state ();
329 ob->main_stream = ((struct lto_output_stream *)
330 xcalloc (1, sizeof (struct lto_output_stream)));
332 return ob;
336 /* Produce a simple section for one of the ipa passes. */
338 void
339 lto_destroy_simple_output_block (struct lto_simple_output_block *ob)
341 char *section_name;
342 struct lto_simple_header header;
343 struct lto_output_stream *header_stream;
345 section_name = lto_get_section_name (ob->section_type, NULL, NULL);
346 lto_begin_section (section_name, !flag_wpa);
347 free (section_name);
349 /* Write the header which says how to decode the pieces of the
350 t. */
351 memset (&header, 0, sizeof (struct lto_simple_header));
352 header.lto_header.major_version = LTO_major_version;
353 header.lto_header.minor_version = LTO_minor_version;
355 header.compressed_size = 0;
357 header.main_size = ob->main_stream->total_size;
359 header_stream = XCNEW (struct lto_output_stream);
360 lto_output_data_stream (header_stream, &header, sizeof header);
361 lto_write_stream (header_stream);
362 free (header_stream);
364 lto_write_stream (ob->main_stream);
366 /* Put back the assembly section that was there before we started
367 writing lto info. */
368 lto_end_section ();
370 free (ob->main_stream);
371 free (ob);
375 /* Return a new lto_out_decl_state. */
377 struct lto_out_decl_state *
378 lto_new_out_decl_state (void)
380 struct lto_out_decl_state *state = XCNEW (struct lto_out_decl_state);
381 int i;
383 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
384 lto_init_tree_ref_encoder (&state->streams[i]);
386 return state;
390 /* Delete STATE and components. */
392 void
393 lto_delete_out_decl_state (struct lto_out_decl_state *state)
395 int i;
397 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
398 lto_destroy_tree_ref_encoder (&state->streams[i]);
400 free (state);
404 /* Get the currently used lto_out_decl_state structure. */
406 struct lto_out_decl_state *
407 lto_get_out_decl_state (void)
409 return decl_state_stack.last ();
412 /* Push STATE to top of out decl stack. */
414 void
415 lto_push_out_decl_state (struct lto_out_decl_state *state)
417 decl_state_stack.safe_push (state);
420 /* Pop the currently used out-decl state from top of stack. */
422 struct lto_out_decl_state *
423 lto_pop_out_decl_state (void)
425 return decl_state_stack.pop ();
428 /* Record STATE after it has been used in serializing the body of
429 FN_DECL. STATE should no longer be used by the caller. The ownership
430 of it is taken over from this point. */
432 void
433 lto_record_function_out_decl_state (tree fn_decl,
434 struct lto_out_decl_state *state)
436 int i;
438 /* Strip all hash tables to save some memory. */
439 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
440 if (state->streams[i].tree_hash_table)
442 delete state->streams[i].tree_hash_table;
443 state->streams[i].tree_hash_table = NULL;
445 state->fn_decl = fn_decl;
446 lto_function_decl_states.safe_push (state);