1*38fd1498Szrj /* Functions for writing LTO sections.
2*38fd1498Szrj
3*38fd1498Szrj Copyright (C) 2009-2018 Free Software Foundation, Inc.
4*38fd1498Szrj Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5*38fd1498Szrj
6*38fd1498Szrj This file is part of GCC.
7*38fd1498Szrj
8*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
9*38fd1498Szrj the terms of the GNU General Public License as published by the Free
10*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
11*38fd1498Szrj version.
12*38fd1498Szrj
13*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*38fd1498Szrj for more details.
17*38fd1498Szrj
18*38fd1498Szrj You should have received a copy of the GNU General Public License
19*38fd1498Szrj along with GCC; see the file COPYING3. If not see
20*38fd1498Szrj <http://www.gnu.org/licenses/>. */
21*38fd1498Szrj
22*38fd1498Szrj #include "config.h"
23*38fd1498Szrj #include "system.h"
24*38fd1498Szrj #include "coretypes.h"
25*38fd1498Szrj #include "backend.h"
26*38fd1498Szrj #include "rtl.h"
27*38fd1498Szrj #include "tree.h"
28*38fd1498Szrj #include "gimple.h"
29*38fd1498Szrj #include "cgraph.h"
30*38fd1498Szrj #include "data-streamer.h"
31*38fd1498Szrj #include "langhooks.h"
32*38fd1498Szrj #include "lto-compress.h"
33*38fd1498Szrj
34*38fd1498Szrj static vec<lto_out_decl_state_ptr> decl_state_stack;
35*38fd1498Szrj
36*38fd1498Szrj /* List of out decl states used by functions. We use this to
37*38fd1498Szrj generate the decl directory later. */
38*38fd1498Szrj
39*38fd1498Szrj vec<lto_out_decl_state_ptr> lto_function_decl_states;
40*38fd1498Szrj
41*38fd1498Szrj
42*38fd1498Szrj /*****************************************************************************
43*38fd1498Szrj Output routines shared by all of the serialization passes.
44*38fd1498Szrj *****************************************************************************/
45*38fd1498Szrj
46*38fd1498Szrj
47*38fd1498Szrj /* Flush compressed stream data function, sends NUM_CHARS from CHARS
48*38fd1498Szrj to the append lang hook, OPAQUE is currently always NULL. */
49*38fd1498Szrj
50*38fd1498Szrj static void
lto_append_data(const char * chars,unsigned int num_chars,void * opaque)51*38fd1498Szrj lto_append_data (const char *chars, unsigned int num_chars, void *opaque)
52*38fd1498Szrj {
53*38fd1498Szrj gcc_assert (opaque == NULL);
54*38fd1498Szrj lang_hooks.lto.append_data (chars, num_chars, opaque);
55*38fd1498Szrj }
56*38fd1498Szrj
57*38fd1498Szrj /* Pointer to the current compression stream. */
58*38fd1498Szrj
59*38fd1498Szrj static struct lto_compression_stream *compression_stream = NULL;
60*38fd1498Szrj
61*38fd1498Szrj /* Begin a new output section named NAME. If COMPRESS is true, zlib compress
62*38fd1498Szrj the section. */
63*38fd1498Szrj
64*38fd1498Szrj void
lto_begin_section(const char * name,bool compress)65*38fd1498Szrj lto_begin_section (const char *name, bool compress)
66*38fd1498Szrj {
67*38fd1498Szrj lang_hooks.lto.begin_section (name);
68*38fd1498Szrj
69*38fd1498Szrj gcc_assert (compression_stream == NULL);
70*38fd1498Szrj if (compress)
71*38fd1498Szrj compression_stream = lto_start_compression (lto_append_data, NULL);
72*38fd1498Szrj }
73*38fd1498Szrj
74*38fd1498Szrj
75*38fd1498Szrj /* End the current output section. */
76*38fd1498Szrj
77*38fd1498Szrj void
lto_end_section(void)78*38fd1498Szrj lto_end_section (void)
79*38fd1498Szrj {
80*38fd1498Szrj if (compression_stream)
81*38fd1498Szrj {
82*38fd1498Szrj lto_end_compression (compression_stream);
83*38fd1498Szrj compression_stream = NULL;
84*38fd1498Szrj }
85*38fd1498Szrj lang_hooks.lto.end_section ();
86*38fd1498Szrj }
87*38fd1498Szrj
88*38fd1498Szrj /* Write SIZE bytes starting at DATA to the assembler. */
89*38fd1498Szrj
90*38fd1498Szrj void
lto_write_data(const void * data,unsigned int size)91*38fd1498Szrj lto_write_data (const void *data, unsigned int size)
92*38fd1498Szrj {
93*38fd1498Szrj if (compression_stream)
94*38fd1498Szrj lto_compress_block (compression_stream, (const char *)data, size);
95*38fd1498Szrj else
96*38fd1498Szrj lang_hooks.lto.append_data ((const char *)data, size, NULL);
97*38fd1498Szrj }
98*38fd1498Szrj
99*38fd1498Szrj /* Write SIZE bytes starting at DATA to the assembler. */
100*38fd1498Szrj
101*38fd1498Szrj void
lto_write_raw_data(const void * data,unsigned int size)102*38fd1498Szrj lto_write_raw_data (const void *data, unsigned int size)
103*38fd1498Szrj {
104*38fd1498Szrj lang_hooks.lto.append_data ((const char *)data, size, NULL);
105*38fd1498Szrj }
106*38fd1498Szrj
107*38fd1498Szrj /* Write all of the chars in OBS to the assembler. Recycle the blocks
108*38fd1498Szrj in obs as this is being done. */
109*38fd1498Szrj
110*38fd1498Szrj void
lto_write_stream(struct lto_output_stream * obs)111*38fd1498Szrj lto_write_stream (struct lto_output_stream *obs)
112*38fd1498Szrj {
113*38fd1498Szrj unsigned int block_size = 1024;
114*38fd1498Szrj struct lto_char_ptr_base *block;
115*38fd1498Szrj struct lto_char_ptr_base *next_block;
116*38fd1498Szrj if (!obs->first_block)
117*38fd1498Szrj return;
118*38fd1498Szrj
119*38fd1498Szrj for (block = obs->first_block; block; block = next_block)
120*38fd1498Szrj {
121*38fd1498Szrj const char *base = ((char *)block) + sizeof (struct lto_char_ptr_base);
122*38fd1498Szrj unsigned int num_chars = block_size - sizeof (struct lto_char_ptr_base);
123*38fd1498Szrj
124*38fd1498Szrj /* If this is not the last block, it is full. If it is the last
125*38fd1498Szrj block, left_in_block indicates how many chars are unoccupied in
126*38fd1498Szrj this block; subtract from num_chars to obtain occupancy. */
127*38fd1498Szrj next_block = (struct lto_char_ptr_base *) block->ptr;
128*38fd1498Szrj if (!next_block)
129*38fd1498Szrj num_chars -= obs->left_in_block;
130*38fd1498Szrj
131*38fd1498Szrj if (compression_stream)
132*38fd1498Szrj lto_compress_block (compression_stream, base, num_chars);
133*38fd1498Szrj else
134*38fd1498Szrj lang_hooks.lto.append_data (base, num_chars, block);
135*38fd1498Szrj free (block);
136*38fd1498Szrj block_size *= 2;
137*38fd1498Szrj }
138*38fd1498Szrj }
139*38fd1498Szrj
140*38fd1498Szrj
141*38fd1498Szrj /* Lookup NAME in ENCODER. If NAME is not found, create a new entry in
142*38fd1498Szrj ENCODER for NAME with the next available index of ENCODER, then
143*38fd1498Szrj print the index to OBS. True is returned if NAME was added to
144*38fd1498Szrj ENCODER. The resulting index is stored in THIS_INDEX.
145*38fd1498Szrj
146*38fd1498Szrj If OBS is NULL, the only action is to add NAME to the encoder. */
147*38fd1498Szrj
148*38fd1498Szrj bool
lto_output_decl_index(struct lto_output_stream * obs,struct lto_tree_ref_encoder * encoder,tree name,unsigned int * this_index)149*38fd1498Szrj lto_output_decl_index (struct lto_output_stream *obs,
150*38fd1498Szrj struct lto_tree_ref_encoder *encoder,
151*38fd1498Szrj tree name, unsigned int *this_index)
152*38fd1498Szrj {
153*38fd1498Szrj bool new_entry_p = FALSE;
154*38fd1498Szrj bool existed_p;
155*38fd1498Szrj
156*38fd1498Szrj unsigned int &index
157*38fd1498Szrj = encoder->tree_hash_table->get_or_insert (name, &existed_p);
158*38fd1498Szrj if (!existed_p)
159*38fd1498Szrj {
160*38fd1498Szrj index = encoder->trees.length ();
161*38fd1498Szrj encoder->trees.safe_push (name);
162*38fd1498Szrj new_entry_p = TRUE;
163*38fd1498Szrj }
164*38fd1498Szrj
165*38fd1498Szrj if (obs)
166*38fd1498Szrj streamer_write_uhwi_stream (obs, index);
167*38fd1498Szrj *this_index = index;
168*38fd1498Szrj return new_entry_p;
169*38fd1498Szrj }
170*38fd1498Szrj
171*38fd1498Szrj /* Output a field DECL to OBS. */
172*38fd1498Szrj
173*38fd1498Szrj void
lto_output_field_decl_index(struct lto_out_decl_state * decl_state,struct lto_output_stream * obs,tree decl)174*38fd1498Szrj lto_output_field_decl_index (struct lto_out_decl_state *decl_state,
175*38fd1498Szrj struct lto_output_stream * obs, tree decl)
176*38fd1498Szrj {
177*38fd1498Szrj unsigned int index;
178*38fd1498Szrj lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FIELD_DECL],
179*38fd1498Szrj decl, &index);
180*38fd1498Szrj }
181*38fd1498Szrj
182*38fd1498Szrj /* Output a function DECL to OBS. */
183*38fd1498Szrj
184*38fd1498Szrj void
lto_output_fn_decl_index(struct lto_out_decl_state * decl_state,struct lto_output_stream * obs,tree decl)185*38fd1498Szrj lto_output_fn_decl_index (struct lto_out_decl_state *decl_state,
186*38fd1498Szrj struct lto_output_stream * obs, tree decl)
187*38fd1498Szrj {
188*38fd1498Szrj unsigned int index;
189*38fd1498Szrj lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FN_DECL],
190*38fd1498Szrj decl, &index);
191*38fd1498Szrj }
192*38fd1498Szrj
193*38fd1498Szrj /* Output a namespace DECL to OBS. */
194*38fd1498Szrj
195*38fd1498Szrj void
lto_output_namespace_decl_index(struct lto_out_decl_state * decl_state,struct lto_output_stream * obs,tree decl)196*38fd1498Szrj lto_output_namespace_decl_index (struct lto_out_decl_state *decl_state,
197*38fd1498Szrj struct lto_output_stream * obs, tree decl)
198*38fd1498Szrj {
199*38fd1498Szrj unsigned int index;
200*38fd1498Szrj lto_output_decl_index (obs,
201*38fd1498Szrj &decl_state->streams[LTO_DECL_STREAM_NAMESPACE_DECL],
202*38fd1498Szrj decl, &index);
203*38fd1498Szrj }
204*38fd1498Szrj
205*38fd1498Szrj /* Output a static or extern var DECL to OBS. */
206*38fd1498Szrj
207*38fd1498Szrj void
lto_output_var_decl_index(struct lto_out_decl_state * decl_state,struct lto_output_stream * obs,tree decl)208*38fd1498Szrj lto_output_var_decl_index (struct lto_out_decl_state *decl_state,
209*38fd1498Szrj struct lto_output_stream * obs, tree decl)
210*38fd1498Szrj {
211*38fd1498Szrj unsigned int index;
212*38fd1498Szrj lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_VAR_DECL],
213*38fd1498Szrj decl, &index);
214*38fd1498Szrj }
215*38fd1498Szrj
216*38fd1498Szrj /* Output a type DECL to OBS. */
217*38fd1498Szrj
218*38fd1498Szrj void
lto_output_type_decl_index(struct lto_out_decl_state * decl_state,struct lto_output_stream * obs,tree decl)219*38fd1498Szrj lto_output_type_decl_index (struct lto_out_decl_state *decl_state,
220*38fd1498Szrj struct lto_output_stream * obs, tree decl)
221*38fd1498Szrj {
222*38fd1498Szrj unsigned int index;
223*38fd1498Szrj lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE_DECL],
224*38fd1498Szrj decl, &index);
225*38fd1498Szrj }
226*38fd1498Szrj
227*38fd1498Szrj /* Output a type REF to OBS. */
228*38fd1498Szrj
229*38fd1498Szrj void
lto_output_type_ref_index(struct lto_out_decl_state * decl_state,struct lto_output_stream * obs,tree ref)230*38fd1498Szrj lto_output_type_ref_index (struct lto_out_decl_state *decl_state,
231*38fd1498Szrj struct lto_output_stream *obs, tree ref)
232*38fd1498Szrj {
233*38fd1498Szrj unsigned int index;
234*38fd1498Szrj lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE],
235*38fd1498Szrj ref, &index);
236*38fd1498Szrj }
237*38fd1498Szrj
238*38fd1498Szrj
239*38fd1498Szrj /* Create the output block and return it. */
240*38fd1498Szrj
241*38fd1498Szrj struct lto_simple_output_block *
lto_create_simple_output_block(enum lto_section_type section_type)242*38fd1498Szrj lto_create_simple_output_block (enum lto_section_type section_type)
243*38fd1498Szrj {
244*38fd1498Szrj struct lto_simple_output_block *ob
245*38fd1498Szrj = ((struct lto_simple_output_block *)
246*38fd1498Szrj xcalloc (1, sizeof (struct lto_simple_output_block)));
247*38fd1498Szrj
248*38fd1498Szrj ob->section_type = section_type;
249*38fd1498Szrj ob->decl_state = lto_get_out_decl_state ();
250*38fd1498Szrj ob->main_stream = ((struct lto_output_stream *)
251*38fd1498Szrj xcalloc (1, sizeof (struct lto_output_stream)));
252*38fd1498Szrj
253*38fd1498Szrj return ob;
254*38fd1498Szrj }
255*38fd1498Szrj
256*38fd1498Szrj
257*38fd1498Szrj /* Produce a simple section for one of the ipa passes. */
258*38fd1498Szrj
259*38fd1498Szrj void
lto_destroy_simple_output_block(struct lto_simple_output_block * ob)260*38fd1498Szrj lto_destroy_simple_output_block (struct lto_simple_output_block *ob)
261*38fd1498Szrj {
262*38fd1498Szrj char *section_name;
263*38fd1498Szrj struct lto_simple_header header;
264*38fd1498Szrj
265*38fd1498Szrj section_name = lto_get_section_name (ob->section_type, NULL, NULL);
266*38fd1498Szrj lto_begin_section (section_name, !flag_wpa);
267*38fd1498Szrj free (section_name);
268*38fd1498Szrj
269*38fd1498Szrj /* Write the header which says how to decode the pieces of the
270*38fd1498Szrj t. */
271*38fd1498Szrj memset (&header, 0, sizeof (struct lto_simple_header));
272*38fd1498Szrj header.major_version = LTO_major_version;
273*38fd1498Szrj header.minor_version = LTO_minor_version;
274*38fd1498Szrj header.main_size = ob->main_stream->total_size;
275*38fd1498Szrj lto_write_data (&header, sizeof header);
276*38fd1498Szrj
277*38fd1498Szrj lto_write_stream (ob->main_stream);
278*38fd1498Szrj
279*38fd1498Szrj /* Put back the assembly section that was there before we started
280*38fd1498Szrj writing lto info. */
281*38fd1498Szrj lto_end_section ();
282*38fd1498Szrj
283*38fd1498Szrj free (ob->main_stream);
284*38fd1498Szrj free (ob);
285*38fd1498Szrj }
286*38fd1498Szrj
287*38fd1498Szrj
288*38fd1498Szrj /* Return a new lto_out_decl_state. */
289*38fd1498Szrj
290*38fd1498Szrj struct lto_out_decl_state *
lto_new_out_decl_state(void)291*38fd1498Szrj lto_new_out_decl_state (void)
292*38fd1498Szrj {
293*38fd1498Szrj struct lto_out_decl_state *state = XCNEW (struct lto_out_decl_state);
294*38fd1498Szrj int i;
295*38fd1498Szrj
296*38fd1498Szrj for (i = 0; i < LTO_N_DECL_STREAMS; i++)
297*38fd1498Szrj lto_init_tree_ref_encoder (&state->streams[i]);
298*38fd1498Szrj
299*38fd1498Szrj /* At WPA time we do not compress sections by default. */
300*38fd1498Szrj state->compressed = !flag_wpa;
301*38fd1498Szrj
302*38fd1498Szrj return state;
303*38fd1498Szrj }
304*38fd1498Szrj
305*38fd1498Szrj
306*38fd1498Szrj /* Delete STATE and components. */
307*38fd1498Szrj
308*38fd1498Szrj void
lto_delete_out_decl_state(struct lto_out_decl_state * state)309*38fd1498Szrj lto_delete_out_decl_state (struct lto_out_decl_state *state)
310*38fd1498Szrj {
311*38fd1498Szrj int i;
312*38fd1498Szrj
313*38fd1498Szrj for (i = 0; i < LTO_N_DECL_STREAMS; i++)
314*38fd1498Szrj lto_destroy_tree_ref_encoder (&state->streams[i]);
315*38fd1498Szrj
316*38fd1498Szrj free (state);
317*38fd1498Szrj }
318*38fd1498Szrj
319*38fd1498Szrj
320*38fd1498Szrj /* Get the currently used lto_out_decl_state structure. */
321*38fd1498Szrj
322*38fd1498Szrj struct lto_out_decl_state *
lto_get_out_decl_state(void)323*38fd1498Szrj lto_get_out_decl_state (void)
324*38fd1498Szrj {
325*38fd1498Szrj return decl_state_stack.last ();
326*38fd1498Szrj }
327*38fd1498Szrj
328*38fd1498Szrj /* Push STATE to top of out decl stack. */
329*38fd1498Szrj
330*38fd1498Szrj void
lto_push_out_decl_state(struct lto_out_decl_state * state)331*38fd1498Szrj lto_push_out_decl_state (struct lto_out_decl_state *state)
332*38fd1498Szrj {
333*38fd1498Szrj decl_state_stack.safe_push (state);
334*38fd1498Szrj }
335*38fd1498Szrj
336*38fd1498Szrj /* Pop the currently used out-decl state from top of stack. */
337*38fd1498Szrj
338*38fd1498Szrj struct lto_out_decl_state *
lto_pop_out_decl_state(void)339*38fd1498Szrj lto_pop_out_decl_state (void)
340*38fd1498Szrj {
341*38fd1498Szrj return decl_state_stack.pop ();
342*38fd1498Szrj }
343*38fd1498Szrj
344*38fd1498Szrj /* Record STATE after it has been used in serializing the body of
345*38fd1498Szrj FN_DECL. STATE should no longer be used by the caller. The ownership
346*38fd1498Szrj of it is taken over from this point. */
347*38fd1498Szrj
348*38fd1498Szrj void
lto_record_function_out_decl_state(tree fn_decl,struct lto_out_decl_state * state)349*38fd1498Szrj lto_record_function_out_decl_state (tree fn_decl,
350*38fd1498Szrj struct lto_out_decl_state *state)
351*38fd1498Szrj {
352*38fd1498Szrj int i;
353*38fd1498Szrj
354*38fd1498Szrj /* Strip all hash tables to save some memory. */
355*38fd1498Szrj for (i = 0; i < LTO_N_DECL_STREAMS; i++)
356*38fd1498Szrj if (state->streams[i].tree_hash_table)
357*38fd1498Szrj {
358*38fd1498Szrj delete state->streams[i].tree_hash_table;
359*38fd1498Szrj state->streams[i].tree_hash_table = NULL;
360*38fd1498Szrj }
361*38fd1498Szrj state->fn_decl = fn_decl;
362*38fd1498Szrj lto_function_decl_states.safe_push (state);
363*38fd1498Szrj }
364