OSDN Git Service

2010-11-13 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / lto-opts.c
1 /* LTO IL options.
2
3    Copyright 2009, 2010 Free Software Foundation, Inc.
4    Contributed by Simon Baldwin <simonb@google.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
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"
37
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:
42
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
47
48    And the final link may or may not include the same <cc-flags> used
49    to generate the initial object files:
50
51         $ gcc <ld-flags> -flto -o prog f1.o ... fN.o
52
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.
56
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.
60
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.  */
67
68 /* Saved options hold the type of the option (currently CL_TARGET or
69    CL_COMMON), and the code, argument, and value.  */
70
71 typedef struct GTY(()) opt_d
72 {
73   unsigned int type;
74   size_t code;
75   char *arg;
76   int value;
77 } opt_t;
78
79 DEF_VEC_O (opt_t);
80 DEF_VEC_ALLOC_O (opt_t, heap);
81
82
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;
88
89 /* Iterate FROM in reverse, writing option codes not yet in CODES into *TO.
90    Mark each new option code encountered in CODES.  */
91
92 static void
93 reverse_iterate_options (VEC(opt_t, heap) *from, VEC(opt_t, heap) **to,
94                          bitmap codes)
95 {
96   int i;
97
98   for (i = VEC_length (opt_t, from); i > 0; i--)
99     {
100       const opt_t *const o = VEC_index (opt_t, from, i - 1);
101
102       if (bitmap_set_bit (codes, o->code))
103         VEC_safe_push (opt_t, heap, *to, o);
104     }
105 }
106
107 /* Concatenate options vectors FIRST and SECOND, rationalize so that only the
108    final of any given option remains, and return the result.  */
109
110 static VEC(opt_t, heap) *
111 concatenate_options (VEC(opt_t, heap) *first, VEC(opt_t, heap) *second)
112 {
113   VEC(opt_t, heap) *results = NULL;
114   bitmap codes = lto_bitmap_alloc ();
115
116   reverse_iterate_options (second, &results, codes);
117   reverse_iterate_options (first, &results, codes);
118
119   lto_bitmap_free (codes);
120   return results;
121 }
122
123 /* Clear the options vector in *OPTS_P and set it to NULL.  */
124
125 static void
126 clear_options (VEC(opt_t, heap) **opts_p)
127 {
128   int i;
129   opt_t *o;
130
131   FOR_EACH_VEC_ELT (opt_t, *opts_p, i, o)
132     free (o->arg);
133
134   VEC_free (opt_t, heap, *opts_p);
135 }
136
137 /* Write LENGTH bytes from ADDR to STREAM.  */
138
139 static void
140 output_data_stream (struct lto_output_stream *stream,
141                     const void *addr, size_t length)
142 {
143   lto_output_data_stream (stream, addr, length);
144 }
145
146 /* Write string STRING to STREAM.  */
147
148 static void
149 output_string_stream (struct lto_output_stream *stream, const char *string)
150 {
151   bool flag = false;
152
153   if (string != NULL)
154     {
155       const size_t length = strlen (string);
156
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);
161     }
162   else
163     output_data_stream (stream, &flag, sizeof (flag));
164 }
165
166 /* Read LENGTH bytes from STREAM to ADDR.  */
167
168 static void
169 input_data_block (struct lto_input_block *ib, void *addr, size_t length)
170 {
171   size_t i;
172   unsigned char *const buffer = (unsigned char *const) addr;
173
174   for (i = 0; i < length; i++)
175     buffer[i] = lto_input_1_unsigned (ib);
176 }
177
178 /* Return a string from IB.  The string is allocated, and the caller is
179    responsible for freeing it.  */
180
181 static char *
182 input_string_block (struct lto_input_block *ib)
183 {
184   bool flag;
185
186   input_data_block (ib, &flag, sizeof (flag));
187   if (flag)
188     {
189       size_t length;
190       char *string;
191
192       input_data_block (ib, &length, sizeof (length));
193       string = (char *) xcalloc (1, length + 1);
194       input_data_block (ib, string, length);
195
196       return string;
197     }
198   else
199     return NULL;
200 }
201
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.
205
206    TODO This list of options requires expansion and rationalization.
207    Among others, optimization options may well be appropriate here.  */
208
209 static bool
210 register_user_option_p (size_t code, int type)
211 {
212   if (type == CL_TARGET)
213     return true;
214   else if (type == CL_COMMON)
215     {
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);
222     }
223
224   return false;
225 }
226
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.  */
229
230 void
231 lto_register_user_option (size_t code, const char *arg, int value, int type)
232 {
233   if (register_user_option_p (code, type))
234     {
235       opt_t o;
236
237       o.type = type;
238       o.code = code;
239       if (arg != NULL)
240         {
241           o.arg = (char *) xmalloc (strlen (arg) + 1);
242           strcpy (o.arg, arg);
243         }
244       else
245         o.arg = NULL;
246       o.value = value;
247       VEC_safe_push (opt_t, heap, user_options, &o);
248     }
249 }
250
251 /* Empty the saved user options vector.  */
252
253 void
254 lto_clear_user_options (void)
255 {
256   clear_options (&user_options);
257 }
258
259 /* Empty the saved file options vector.  */
260
261 void
262 lto_clear_file_options (void)
263 {
264   clear_options (&file_options);
265 }
266
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.  */
270
271 static void
272 output_options (struct lto_output_stream *stream)
273 {
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;
278
279   output_data_stream (stream, &length, sizeof (length));
280
281   FOR_EACH_VEC_ELT (opt_t, opts, i, o)
282     {
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));
287     }
288
289   VEC_free (opt_t, heap, opts);
290 }
291
292 /* Write currently held options to an LTO IL section.  */
293
294 void
295 lto_write_options (void)
296 {
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;
301
302   lto_begin_section (section_name, !flag_wpa);
303   free (section_name);
304
305   memset (&stream, 0, sizeof (stream));
306   output_options (&stream);
307
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;
312
313   header.compressed_size = 0;
314   header.main_size = stream.total_size;
315
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);
321
322   lto_write_stream (&stream);
323   lto_end_section ();
324 }
325
326 /* Unserialize an options vector from IB, and append to file_options.  */
327
328 static void
329 input_options (struct lto_input_block *ib)
330 {
331   size_t length, i;
332
333   input_data_block (ib, &length, sizeof (length));
334
335   for (i = 0; i < length; i++)
336     {
337       opt_t o;
338
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);
344     }
345 }
346
347 /* Read options from an LTO IL section.  */
348
349 void
350 lto_read_file_options (struct lto_file_decl_data *file_data)
351 {
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;
357
358   data = lto_get_section_data (file_data, LTO_section_opts, NULL, &len);
359   if (!data)
360           return;
361
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;
369   do 
370     { 
371       header = (const struct lto_simple_header *) p;
372       opts_offset = sizeof (*header);
373
374       lto_check_version (header->lto_header.major_version,
375                          header->lto_header.minor_version);
376       
377       LTO_INIT_INPUT_BLOCK (ib, p + opts_offset, 0, header->main_size);
378       input_options (&ib);
379       
380       skip = header->main_size + opts_offset;
381       l -= skip;
382       p += skip;
383     } 
384   while (l > 0);
385
386   lto_free_section_data (file_data, LTO_section_opts, 0, data, len);
387 }
388
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.  */
392
393 void
394 lto_reissue_options (void)
395 {
396   VEC(opt_t, heap) *opts = concatenate_options (file_options, user_options);
397   int i;
398   opt_t *o;
399
400   FOR_EACH_VEC_ELT (opt_t, opts, i, o)
401     {
402       void *flag_var = option_flag_var (o->code, &global_options);
403
404       if (flag_var)
405         set_option (&global_options, &global_options_set,
406                     o->code, o->value, o->arg,
407                     DK_UNSPECIFIED, UNKNOWN_LOCATION, global_dc);
408
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 ();
415     }
416
417   VEC_free (opt_t, heap, opts);
418 }