lto-opts.c (input_data_block): Move to lto-streamer-in.c.
[official-gcc.git] / gcc / lto-opts.c
blob9979e8da8215dd92778bc1f137fd2b5596914040
1 /* LTO IL options.
3 Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
4 Contributed by Simon Baldwin <simonb@google.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 "tree.h"
26 #include "hashtab.h"
27 #include "ggc.h"
28 #include "vec.h"
29 #include "bitmap.h"
30 #include "flags.h"
31 #include "opts.h"
32 #include "options.h"
33 #include "target.h"
34 #include "diagnostic.h"
35 #include "lto-streamer.h"
37 /* When a file is initially compiled, the options used when generating
38 the IL are not necessarily the same as those used when linking the
39 objects into the final executable. In general, most build systems
40 will proceed with something along the lines of:
42 $ gcc <cc-flags> -flto -c f1.c -o f1.o
43 $ gcc <cc-flags> -flto -c f2.c -o f2.o
44 ...
45 $ gcc <cc-flags> -flto -c fN.c -o fN.o
47 And the final link may or may not include the same <cc-flags> used
48 to generate the initial object files:
50 $ gcc <ld-flags> -flto -o prog f1.o ... fN.o
52 Since we will be generating final code during the link step, some
53 of the flags used during the compile step need to be re-applied
54 during the link step. For instance, flags in the -m family.
56 The idea is to save a selected set of <cc-flags> in a special
57 section of the initial object files. This section is then read
58 during linking and the options re-applied.
60 FIXME lto. Currently the scheme is limited in that only the
61 options saved on the first object file (f1.o) are read back during
62 the link step. This means that the options used to compile f1.o
63 will be applied to ALL the object files in the final link step.
64 More work needs to be done to implement a merging and validation
65 mechanism, as this will not be enough for all cases. */
67 /* Saved options hold the type of the option (currently CL_TARGET or
68 CL_COMMON), and the code, argument, and value. */
70 typedef struct GTY(()) opt_d
72 unsigned int type;
73 size_t code;
74 char *arg;
75 int value;
76 } opt_t;
78 DEF_VEC_O (opt_t);
79 DEF_VEC_ALLOC_O (opt_t, heap);
82 /* Options are held in two vectors, one for those registered by
83 command line handling code, and the other for those read in from
84 any LTO IL input. */
85 static VEC(opt_t, heap) *user_options = NULL;
86 static VEC(opt_t, heap) *file_options = NULL;
88 /* Iterate FROM in reverse, writing option codes not yet in CODES into *TO.
89 Mark each new option code encountered in CODES. */
91 static void
92 reverse_iterate_options (VEC(opt_t, heap) *from, VEC(opt_t, heap) **to,
93 bitmap codes)
95 int i;
97 for (i = VEC_length (opt_t, from); i > 0; i--)
99 const opt_t *const o = VEC_index (opt_t, from, i - 1);
101 if (bitmap_set_bit (codes, o->code))
102 VEC_safe_push (opt_t, heap, *to, o);
106 /* Concatenate options vectors FIRST and SECOND, rationalize so that only the
107 final of any given option remains, and return the result. */
109 static VEC(opt_t, heap) *
110 concatenate_options (VEC(opt_t, heap) *first, VEC(opt_t, heap) *second)
112 VEC(opt_t, heap) *results = NULL;
113 bitmap codes = lto_bitmap_alloc ();
115 reverse_iterate_options (second, &results, codes);
116 reverse_iterate_options (first, &results, codes);
118 lto_bitmap_free (codes);
119 return results;
122 /* Clear the options vector in *OPTS_P and set it to NULL. */
124 static void
125 clear_options (VEC(opt_t, heap) **opts_p)
127 int i;
128 opt_t *o;
130 FOR_EACH_VEC_ELT (opt_t, *opts_p, i, o)
131 free (o->arg);
133 VEC_free (opt_t, heap, *opts_p);
136 /* Write LENGTH bytes from ADDR to STREAM. */
138 static void
139 output_data_stream (struct lto_output_stream *stream,
140 const void *addr, size_t length)
142 lto_output_data_stream (stream, addr, length);
145 /* Write string STRING to STREAM. */
147 static void
148 output_string_stream (struct lto_output_stream *stream, const char *string)
150 bool flag = false;
152 if (string != NULL)
154 const size_t length = strlen (string);
156 flag = true;
157 output_data_stream (stream, &flag, sizeof (flag));
158 output_data_stream (stream, &length, sizeof (length));
159 output_data_stream (stream, string, length);
161 else
162 output_data_stream (stream, &flag, sizeof (flag));
165 /* Return a string from IB. The string is allocated, and the caller is
166 responsible for freeing it. */
168 static char *
169 input_string_block (struct lto_input_block *ib)
171 bool flag;
173 lto_input_data_block (ib, &flag, sizeof (flag));
174 if (flag)
176 size_t length;
177 char *string;
179 lto_input_data_block (ib, &length, sizeof (length));
180 string = (char *) xcalloc (1, length + 1);
181 lto_input_data_block (ib, string, length);
183 return string;
185 else
186 return NULL;
189 /* Return true if this option is one we need to save in LTO output files.
190 At present, we pass along all target options, and common options that
191 involve position independent code.
193 TODO This list of options requires expansion and rationalization.
194 Among others, optimization options may well be appropriate here. */
196 static bool
197 register_user_option_p (size_t code, int type)
199 if (type == CL_TARGET)
200 return true;
201 else if (type == CL_COMMON)
203 return (code == OPT_fPIC
204 || code == OPT_fpic
205 || code == OPT_fPIE
206 || code == OPT_fpie
207 || code == OPT_fcommon
208 || code == OPT_fexceptions);
211 return false;
214 /* Note command line option with the given TYPE and CODE, ARG, and VALUE.
215 If relevant to LTO, save it in the user options vector. */
217 void
218 lto_register_user_option (size_t code, const char *arg, int value, int type)
220 if (register_user_option_p (code, type))
222 opt_t o;
224 o.type = type;
225 o.code = code;
226 if (arg != NULL)
228 o.arg = (char *) xmalloc (strlen (arg) + 1);
229 strcpy (o.arg, arg);
231 else
232 o.arg = NULL;
233 o.value = value;
234 VEC_safe_push (opt_t, heap, user_options, &o);
238 /* Empty the saved user options vector. */
240 void
241 lto_clear_user_options (void)
243 clear_options (&user_options);
246 /* Empty the saved file options vector. */
248 void
249 lto_clear_file_options (void)
251 clear_options (&file_options);
254 /* Concatenate the user options and any file options read from an LTO IL
255 file, and serialize them to STREAM. File options precede user options
256 so that the latter override the former when reissued. */
258 static void
259 output_options (struct lto_output_stream *stream)
261 VEC(opt_t, heap) *opts = concatenate_options (file_options, user_options);
262 const size_t length = VEC_length (opt_t, opts);
263 int i;
264 opt_t *o;
266 output_data_stream (stream, &length, sizeof (length));
268 FOR_EACH_VEC_ELT (opt_t, opts, i, o)
270 output_data_stream (stream, &o->type, sizeof (o->type));
271 output_data_stream (stream, &o->code, sizeof (o->code));
272 output_string_stream (stream, o->arg);
273 output_data_stream (stream, &o->value, sizeof (o->value));
276 VEC_free (opt_t, heap, opts);
279 /* Write currently held options to an LTO IL section. */
281 void
282 lto_write_options (void)
284 char *const section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
285 struct lto_output_stream stream;
286 struct lto_simple_header header;
287 struct lto_output_stream *header_stream;
289 /* Targets and languages can provide defaults for -fexceptions but
290 we only process user options from the command-line. Until we
291 serialize out a white list of options from the new global state
292 explicitly append important options as user options here. */
293 if (flag_exceptions)
294 lto_register_user_option (OPT_fexceptions, NULL, 1, CL_COMMON);
296 lto_begin_section (section_name, !flag_wpa);
297 free (section_name);
299 memset (&stream, 0, sizeof (stream));
300 output_options (&stream);
302 memset (&header, 0, sizeof (header));
303 header.lto_header.major_version = LTO_major_version;
304 header.lto_header.minor_version = LTO_minor_version;
305 header.lto_header.section_type = LTO_section_opts;
307 header.compressed_size = 0;
308 header.main_size = stream.total_size;
310 header_stream = ((struct lto_output_stream *)
311 xcalloc (1, sizeof (*header_stream)));
312 lto_output_data_stream (header_stream, &header, sizeof (header));
313 lto_write_stream (header_stream);
314 free (header_stream);
316 lto_write_stream (&stream);
317 lto_end_section ();
320 /* Unserialize an options vector from IB, and append to file_options. */
322 static void
323 input_options (struct lto_input_block *ib)
325 size_t length, i;
327 lto_input_data_block (ib, &length, sizeof (length));
329 for (i = 0; i < length; i++)
331 opt_t o;
333 lto_input_data_block (ib, &o.type, sizeof (o.type));
334 lto_input_data_block (ib, &o.code, sizeof (o.code));
335 o.arg = input_string_block (ib);
336 lto_input_data_block (ib, &o.value, sizeof (o.value));
337 VEC_safe_push (opt_t, heap, file_options, &o);
341 /* Read options from an LTO IL section. */
343 void
344 lto_read_file_options (struct lto_file_decl_data *file_data)
346 size_t len, l, skip;
347 const char *data, *p;
348 const struct lto_simple_header *header;
349 int32_t opts_offset;
350 struct lto_input_block ib;
352 data = lto_get_section_data (file_data, LTO_section_opts, NULL, &len);
353 if (!data)
354 return;
356 /* Option could be multiple sections merged (through ld -r)
357 Keep reading all options. This is ok right now because
358 the options just get mashed together anyways.
359 This will have to be done differently once lto-opts knows
360 how to associate options with different files. */
361 l = len;
362 p = data;
365 header = (const struct lto_simple_header *) p;
366 opts_offset = sizeof (*header);
368 lto_check_version (header->lto_header.major_version,
369 header->lto_header.minor_version);
371 LTO_INIT_INPUT_BLOCK (ib, p + opts_offset, 0, header->main_size);
372 input_options (&ib);
374 skip = header->main_size + opts_offset;
375 l -= skip;
376 p += skip;
378 while (l > 0);
380 lto_free_section_data (file_data, LTO_section_opts, 0, data, len);
383 /* Concatenate the user options and any file options read from an LTO IL
384 file, and reissue them as if all had just been read in from the command
385 line. As with serialization, file options precede user options. */
387 void
388 lto_reissue_options (void)
390 VEC(opt_t, heap) *opts = concatenate_options (file_options, user_options);
391 int i;
392 opt_t *o;
394 FOR_EACH_VEC_ELT (opt_t, opts, i, o)
396 void *flag_var = option_flag_var (o->code, &global_options);
398 if (flag_var)
399 set_option (&global_options, &global_options_set,
400 o->code, o->value, o->arg,
401 DK_UNSPECIFIED, UNKNOWN_LOCATION, global_dc);
403 if (o->type == CL_TARGET)
405 struct cl_decoded_option decoded;
406 generate_option (o->code, o->arg, o->value, CL_TARGET, &decoded);
407 targetm.handle_option (&global_options, &global_options_set,
408 &decoded, UNKNOWN_LOCATION);
410 else if (o->type == CL_COMMON)
411 gcc_assert (flag_var);
412 else
413 gcc_unreachable ();
416 /* Flag_shlib is usually set by finish_options, but we are issuing flag_pic
417 too late. */
418 if (flag_pic && !flag_pie)
419 flag_shlib = 1;
420 VEC_free (opt_t, heap, opts);