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
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
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/>. */
24 #include "coretypes.h"
34 #include "diagnostic.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
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
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
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. */
93 reverse_iterate_options (VEC(opt_t
, heap
) *from
, VEC(opt_t
, heap
) **to
,
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
);
123 /* Clear the options vector in *OPTS_P and set it to NULL. */
126 clear_options (VEC(opt_t
, heap
) **opts_p
)
131 FOR_EACH_VEC_ELT (opt_t
, *opts_p
, i
, o
)
134 VEC_free (opt_t
, heap
, *opts_p
);
137 /* Write LENGTH bytes from ADDR to STREAM. */
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. */
149 output_string_stream (struct lto_output_stream
*stream
, const char *string
)
155 const size_t length
= strlen (string
);
158 output_data_stream (stream
, &flag
, sizeof (flag
));
159 output_data_stream (stream
, &length
, sizeof (length
));
160 output_data_stream (stream
, string
, length
);
163 output_data_stream (stream
, &flag
, sizeof (flag
));
166 /* Read LENGTH bytes from STREAM to ADDR. */
169 input_data_block (struct lto_input_block
*ib
, void *addr
, size_t length
)
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. */
182 input_string_block (struct lto_input_block
*ib
)
186 input_data_block (ib
, &flag
, sizeof (flag
));
192 input_data_block (ib
, &length
, sizeof (length
));
193 string
= (char *) xcalloc (1, length
+ 1);
194 input_data_block (ib
, string
, length
);
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. */
210 register_user_option_p (size_t code
, int type
)
212 if (type
== CL_TARGET
)
214 else if (type
== CL_COMMON
)
216 return (code
== OPT_fPIC
220 || code
== OPT_fcommon
221 || code
== OPT_fexceptions
);
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. */
231 lto_register_user_option (size_t code
, const char *arg
, int value
, int type
)
233 if (register_user_option_p (code
, type
))
241 o
.arg
= (char *) xmalloc (strlen (arg
) + 1);
247 VEC_safe_push (opt_t
, heap
, user_options
, &o
);
251 /* Empty the saved user options vector. */
254 lto_clear_user_options (void)
256 clear_options (&user_options
);
259 /* Empty the saved file options vector. */
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. */
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
);
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. */
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
);
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
);
326 /* Unserialize an options vector from IB, and append to file_options. */
329 input_options (struct lto_input_block
*ib
)
333 input_data_block (ib
, &length
, sizeof (length
));
335 for (i
= 0; i
< length
; i
++)
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. */
350 lto_read_file_options (struct lto_file_decl_data
*file_data
)
353 const char *data
, *p
;
354 const struct lto_simple_header
*header
;
356 struct lto_input_block ib
;
358 data
= lto_get_section_data (file_data
, LTO_section_opts
, NULL
, &len
);
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. */
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
);
380 skip
= header
->main_size
+ opts_offset
;
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. */
394 lto_reissue_options (void)
396 VEC(opt_t
, heap
) *opts
= concatenate_options (file_options
, user_options
);
400 FOR_EACH_VEC_ELT (opt_t
, opts
, i
, o
)
402 void *flag_var
= option_flag_var (o
->code
, &global_options
);
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
);
417 VEC_free (opt_t
, heap
, opts
);