2010-11-11 Jakub Jelinek <jakub@redhat.com>
[official-gcc.git] / gcc / lto-opts.c
blob66010c68adf4f2b42599234e3f7cff7254dca5e3
1 /* LTO IL options.
3 Copyright 2009, 2010 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 "toplev.h"
36 #include "lto-streamer.h"
38 /* When a file is initially compiled, the options used when generating
39 the IL are not necessarily the same as those used when linking the
40 objects into the final executable. In general, most build systems
41 will proceed with something along the lines of:
43 $ gcc <cc-flags> -flto -c f1.c -o f1.o
44 $ gcc <cc-flags> -flto -c f2.c -o f2.o
45 ...
46 $ gcc <cc-flags> -flto -c fN.c -o fN.o
48 And the final link may or may not include the same <cc-flags> used
49 to generate the initial object files:
51 $ gcc <ld-flags> -flto -o prog f1.o ... fN.o
53 Since we will be generating final code during the link step, some
54 of the flags used during the compile step need to be re-applied
55 during the link step. For instance, flags in the -m family.
57 The idea is to save a selected set of <cc-flags> in a special
58 section of the initial object files. This section is then read
59 during linking and the options re-applied.
61 FIXME lto. Currently the scheme is limited in that only the
62 options saved on the first object file (f1.o) are read back during
63 the link step. This means that the options used to compile f1.o
64 will be applied to ALL the object files in the final link step.
65 More work needs to be done to implement a merging and validation
66 mechanism, as this will not be enough for all cases. */
68 /* Saved options hold the type of the option (currently CL_TARGET or
69 CL_COMMON), and the code, argument, and value. */
71 typedef struct GTY(()) opt_d
73 unsigned int type;
74 size_t code;
75 char *arg;
76 int value;
77 } opt_t;
79 DEF_VEC_O (opt_t);
80 DEF_VEC_ALLOC_O (opt_t, heap);
83 /* Options are held in two vectors, one for those registered by
84 command line handling code, and the other for those read in from
85 any LTO IL input. */
86 static VEC(opt_t, heap) *user_options = NULL;
87 static VEC(opt_t, heap) *file_options = NULL;
89 /* Iterate FROM in reverse, writing option codes not yet in CODES into *TO.
90 Mark each new option code encountered in CODES. */
92 static void
93 reverse_iterate_options (VEC(opt_t, heap) *from, VEC(opt_t, heap) **to,
94 bitmap codes)
96 int i;
98 for (i = VEC_length (opt_t, from); i > 0; i--)
100 const opt_t *const o = VEC_index (opt_t, from, i - 1);
102 if (bitmap_set_bit (codes, o->code))
103 VEC_safe_push (opt_t, heap, *to, o);
107 /* Concatenate options vectors FIRST and SECOND, rationalize so that only the
108 final of any given option remains, and return the result. */
110 static VEC(opt_t, heap) *
111 concatenate_options (VEC(opt_t, heap) *first, VEC(opt_t, heap) *second)
113 VEC(opt_t, heap) *results = NULL;
114 bitmap codes = lto_bitmap_alloc ();
116 reverse_iterate_options (second, &results, codes);
117 reverse_iterate_options (first, &results, codes);
119 lto_bitmap_free (codes);
120 return results;
123 /* Clear the options vector in *OPTS_P and set it to NULL. */
125 static void
126 clear_options (VEC(opt_t, heap) **opts_p)
128 int i;
129 opt_t *o;
131 FOR_EACH_VEC_ELT (opt_t, *opts_p, i, o)
132 free (o->arg);
134 VEC_free (opt_t, heap, *opts_p);
137 /* Write LENGTH bytes from ADDR to STREAM. */
139 static void
140 output_data_stream (struct lto_output_stream *stream,
141 const void *addr, size_t length)
143 lto_output_data_stream (stream, addr, length);
146 /* Write string STRING to STREAM. */
148 static void
149 output_string_stream (struct lto_output_stream *stream, const char *string)
151 bool flag = false;
153 if (string != NULL)
155 const size_t length = strlen (string);
157 flag = true;
158 output_data_stream (stream, &flag, sizeof (flag));
159 output_data_stream (stream, &length, sizeof (length));
160 output_data_stream (stream, string, length);
162 else
163 output_data_stream (stream, &flag, sizeof (flag));
166 /* Read LENGTH bytes from STREAM to ADDR. */
168 static void
169 input_data_block (struct lto_input_block *ib, void *addr, size_t length)
171 size_t i;
172 unsigned char *const buffer = (unsigned char *const) addr;
174 for (i = 0; i < length; i++)
175 buffer[i] = lto_input_1_unsigned (ib);
178 /* Return a string from IB. The string is allocated, and the caller is
179 responsible for freeing it. */
181 static char *
182 input_string_block (struct lto_input_block *ib)
184 bool flag;
186 input_data_block (ib, &flag, sizeof (flag));
187 if (flag)
189 size_t length;
190 char *string;
192 input_data_block (ib, &length, sizeof (length));
193 string = (char *) xcalloc (1, length + 1);
194 input_data_block (ib, string, length);
196 return string;
198 else
199 return NULL;
202 /* Return true if this option is one we need to save in LTO output files.
203 At present, we pass along all target options, and common options that
204 involve position independent code.
206 TODO This list of options requires expansion and rationalization.
207 Among others, optimization options may well be appropriate here. */
209 static bool
210 register_user_option_p (size_t code, int type)
212 if (type == CL_TARGET)
213 return true;
214 else if (type == CL_COMMON)
216 return (code == OPT_fPIC
217 || code == OPT_fpic
218 || code == OPT_fPIE
219 || code == OPT_fpie
220 || code == OPT_fcommon
221 || code == OPT_fexceptions);
224 return false;
227 /* Note command line option with the given TYPE and CODE, ARG, and VALUE.
228 If relevant to LTO, save it in the user options vector. */
230 void
231 lto_register_user_option (size_t code, const char *arg, int value, int type)
233 if (register_user_option_p (code, type))
235 opt_t o;
237 o.type = type;
238 o.code = code;
239 if (arg != NULL)
241 o.arg = (char *) xmalloc (strlen (arg) + 1);
242 strcpy (o.arg, arg);
244 else
245 o.arg = NULL;
246 o.value = value;
247 VEC_safe_push (opt_t, heap, user_options, &o);
251 /* Empty the saved user options vector. */
253 void
254 lto_clear_user_options (void)
256 clear_options (&user_options);
259 /* Empty the saved file options vector. */
261 void
262 lto_clear_file_options (void)
264 clear_options (&file_options);
267 /* Concatenate the user options and any file options read from an LTO IL
268 file, and serialize them to STREAM. File options precede user options
269 so that the latter override the former when reissued. */
271 static void
272 output_options (struct lto_output_stream *stream)
274 VEC(opt_t, heap) *opts = concatenate_options (file_options, user_options);
275 const size_t length = VEC_length (opt_t, opts);
276 int i;
277 opt_t *o;
279 output_data_stream (stream, &length, sizeof (length));
281 FOR_EACH_VEC_ELT (opt_t, opts, i, o)
283 output_data_stream (stream, &o->type, sizeof (o->type));
284 output_data_stream (stream, &o->code, sizeof (o->code));
285 output_string_stream (stream, o->arg);
286 output_data_stream (stream, &o->value, sizeof (o->value));
289 VEC_free (opt_t, heap, opts);
292 /* Write currently held options to an LTO IL section. */
294 void
295 lto_write_options (void)
297 char *const section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
298 struct lto_output_stream stream;
299 struct lto_simple_header header;
300 struct lto_output_stream *header_stream;
302 lto_begin_section (section_name, !flag_wpa);
303 free (section_name);
305 memset (&stream, 0, sizeof (stream));
306 output_options (&stream);
308 memset (&header, 0, sizeof (header));
309 header.lto_header.major_version = LTO_major_version;
310 header.lto_header.minor_version = LTO_minor_version;
311 header.lto_header.section_type = LTO_section_opts;
313 header.compressed_size = 0;
314 header.main_size = stream.total_size;
316 header_stream = ((struct lto_output_stream *)
317 xcalloc (1, sizeof (*header_stream)));
318 lto_output_data_stream (header_stream, &header, sizeof (header));
319 lto_write_stream (header_stream);
320 free (header_stream);
322 lto_write_stream (&stream);
323 lto_end_section ();
326 /* Unserialize an options vector from IB, and append to file_options. */
328 static void
329 input_options (struct lto_input_block *ib)
331 size_t length, i;
333 input_data_block (ib, &length, sizeof (length));
335 for (i = 0; i < length; i++)
337 opt_t o;
339 input_data_block (ib, &o.type, sizeof (o.type));
340 input_data_block (ib, &o.code, sizeof (o.code));
341 o.arg = input_string_block (ib);
342 input_data_block (ib, &o.value, sizeof (o.value));
343 VEC_safe_push (opt_t, heap, file_options, &o);
347 /* Read options from an LTO IL section. */
349 void
350 lto_read_file_options (struct lto_file_decl_data *file_data)
352 size_t len, l, skip;
353 const char *data, *p;
354 const struct lto_simple_header *header;
355 int32_t opts_offset;
356 struct lto_input_block ib;
358 data = lto_get_section_data (file_data, LTO_section_opts, NULL, &len);
359 if (!data)
360 return;
362 /* Option could be multiple sections merged (through ld -r)
363 Keep reading all options. This is ok right now because
364 the options just get mashed together anyways.
365 This will have to be done differently once lto-opts knows
366 how to associate options with different files. */
367 l = len;
368 p = data;
371 header = (const struct lto_simple_header *) p;
372 opts_offset = sizeof (*header);
374 lto_check_version (header->lto_header.major_version,
375 header->lto_header.minor_version);
377 LTO_INIT_INPUT_BLOCK (ib, p + opts_offset, 0, header->main_size);
378 input_options (&ib);
380 skip = header->main_size + opts_offset;
381 l -= skip;
382 p += skip;
384 while (l > 0);
386 lto_free_section_data (file_data, LTO_section_opts, 0, data, len);
389 /* Concatenate the user options and any file options read from an LTO IL
390 file, and reissue them as if all had just been read in from the command
391 line. As with serialization, file options precede user options. */
393 void
394 lto_reissue_options (void)
396 VEC(opt_t, heap) *opts = concatenate_options (file_options, user_options);
397 int i;
398 opt_t *o;
400 FOR_EACH_VEC_ELT (opt_t, opts, i, o)
402 void *flag_var = option_flag_var (o->code, &global_options);
404 if (flag_var)
405 set_option (&global_options, &global_options_set,
406 o->code, o->value, o->arg,
407 DK_UNSPECIFIED, global_dc);
409 if (o->type == CL_TARGET)
410 targetm.handle_option (o->code, o->arg, o->value);
411 else if (o->type == CL_COMMON)
412 gcc_assert (flag_var);
413 else
414 gcc_unreachable ();
417 VEC_free (opt_t, heap, opts);