aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.6/gcc/lto-opts.c
blob: 71a7067ce2282cb3f94e03a7fd59c284d74ccebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* LTO IL options.

   Copyright 2009, 2010 Free Software Foundation, Inc.
   Contributed by Simon Baldwin <simonb@google.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "hashtab.h"
#include "ggc.h"
#include "vec.h"
#include "bitmap.h"
#include "flags.h"
#include "opts.h"
#include "options.h"
#include "target.h"
#include "diagnostic.h"
#include "lto-streamer.h"

/* When a file is initially compiled, the options used when generating
   the IL are not necessarily the same as those used when linking the
   objects into the final executable.  In general, most build systems
   will proceed with something along the lines of:

   	$ gcc <cc-flags> -flto -c f1.c -o f1.o
	$ gcc <cc-flags> -flto -c f2.c -o f2.o
	...
	$ gcc <cc-flags> -flto -c fN.c -o fN.o

   And the final link may or may not include the same <cc-flags> used
   to generate the initial object files:

   	$ gcc <ld-flags> -flto -o prog f1.o ... fN.o

   Since we will be generating final code during the link step, some
   of the flags used during the compile step need to be re-applied
   during the link step.  For instance, flags in the -m family.

   The idea is to save a selected set of <cc-flags> in a special
   section of the initial object files.  This section is then read
   during linking and the options re-applied.

   FIXME lto.  Currently the scheme is limited in that only the
   options saved on the first object file (f1.o) are read back during
   the link step.  This means that the options used to compile f1.o
   will be applied to ALL the object files in the final link step.
   More work needs to be done to implement a merging and validation
   mechanism, as this will not be enough for all cases.  */

/* Saved options hold the type of the option (currently CL_TARGET or
   CL_COMMON), and the code, argument, and value.  */

typedef struct GTY(()) opt_d
{
  unsigned int type;
  size_t code;
  char *arg;
  int value;
} opt_t;

DEF_VEC_O (opt_t);
DEF_VEC_ALLOC_O (opt_t, heap);


/* Options are held in two vectors, one for those registered by
   command line handling code, and the other for those read in from
   any LTO IL input.  */
static VEC(opt_t, heap) *user_options = NULL;
static VEC(opt_t, heap) *file_options = NULL;

/* Iterate FROM in reverse, writing option codes not yet in CODES into *TO.
   Mark each new option code encountered in CODES.  */

static void
reverse_iterate_options (VEC(opt_t, heap) *from, VEC(opt_t, heap) **to,
			 bitmap codes)
{
  int i;

  for (i = VEC_length (opt_t, from); i > 0; i--)
    {
      const opt_t *const o = VEC_index (opt_t, from, i - 1);

      if (bitmap_set_bit (codes, o->code))
	VEC_safe_push (opt_t, heap, *to, o);
    }
}

/* Concatenate options vectors FIRST and SECOND, rationalize so that only the
   final of any given option remains, and return the result.  */

static VEC(opt_t, heap) *
concatenate_options (VEC(opt_t, heap) *first, VEC(opt_t, heap) *second)
{
  VEC(opt_t, heap) *results = NULL;
  bitmap codes = lto_bitmap_alloc ();

  reverse_iterate_options (second, &results, codes);
  reverse_iterate_options (first, &results, codes);

  lto_bitmap_free (codes);
  return results;
}

/* Clear the options vector in *OPTS_P and set it to NULL.  */

static void
clear_options (VEC(opt_t, heap) **opts_p)
{
  int i;
  opt_t *o;

  FOR_EACH_VEC_ELT (opt_t, *opts_p, i, o)
    free (o->arg);

  VEC_free (opt_t, heap, *opts_p);
}

/* Write LENGTH bytes from ADDR to STREAM.  */

static void
output_data_stream (struct lto_output_stream *stream,
                    const void *addr, size_t length)
{
  lto_output_data_stream (stream, addr, length);
}

/* Write string STRING to STREAM.  */

static void
output_string_stream (struct lto_output_stream *stream, const char *string)
{
  bool flag = false;

  if (string != NULL)
    {
      const size_t length = strlen (string);

      flag = true;
      output_data_stream (stream, &flag, sizeof (flag));
      output_data_stream (stream, &length, sizeof (length));
      output_data_stream (stream, string, length);
    }
  else
    output_data_stream (stream, &flag, sizeof (flag));
}

/* Read LENGTH bytes from STREAM to ADDR.  */

static void
input_data_block (struct lto_input_block *ib, void *addr, size_t length)
{
  size_t i;
  unsigned char *const buffer = (unsigned char *const) addr;

  for (i = 0; i < length; i++)
    buffer[i] = lto_input_1_unsigned (ib);
}

/* Return a string from IB.  The string is allocated, and the caller is
   responsible for freeing it.  */

static char *
input_string_block (struct lto_input_block *ib)
{
  bool flag;

  input_data_block (ib, &flag, sizeof (flag));
  if (flag)
    {
      size_t length;
      char *string;

      input_data_block (ib, &length, sizeof (length));
      string = (char *) xcalloc (1, length + 1);
      input_data_block (ib, string, length);

      return string;
    }
  else
    return NULL;
}

/* Return true if this option is one we need to save in LTO output files.
   At present, we pass along all target options, and common options that
   involve position independent code.

   TODO This list of options requires expansion and rationalization.
   Among others, optimization options may well be appropriate here.  */

static bool
register_user_option_p (size_t code, int type)
{
  if (type == CL_TARGET)
    return true;
  else if (type == CL_COMMON)
    {
      return (code == OPT_fPIC
	      || code == OPT_fpic
	      || code == OPT_fPIE
	      || code == OPT_fpie
	      || code == OPT_fcommon
	      || code == OPT_fexceptions);
    }

  return false;
}

/* Note command line option with the given TYPE and CODE, ARG, and VALUE.
   If relevant to LTO, save it in the user options vector.  */

void
lto_register_user_option (size_t code, const char *arg, int value, int type)
{
  if (register_user_option_p (code, type))
    {
      opt_t o;

      o.type = type;
      o.code = code;
      if (arg != NULL)
	{
	  o.arg = (char *) xmalloc (strlen (arg) + 1);
	  strcpy (o.arg, arg);
	}
      else
	o.arg = NULL;
      o.value = value;
      VEC_safe_push (opt_t, heap, user_options, &o);
    }
}

/* Empty the saved user options vector.  */

void
lto_clear_user_options (void)
{
  clear_options (&user_options);
}

/* Empty the saved file options vector.  */

void
lto_clear_file_options (void)
{
  clear_options (&file_options);
}

/* Concatenate the user options and any file options read from an LTO IL
   file, and serialize them to STREAM.  File options precede user options
   so that the latter override the former when reissued.  */

static void
output_options (struct lto_output_stream *stream)
{
  VEC(opt_t, heap) *opts = concatenate_options (file_options, user_options);
  const size_t length = VEC_length (opt_t, opts);
  int i;
  opt_t *o;

  output_data_stream (stream, &length, sizeof (length));

  FOR_EACH_VEC_ELT (opt_t, opts, i, o)
    {
      output_data_stream (stream, &o->type, sizeof (o->type));
      output_data_stream (stream, &o->code, sizeof (o->code));
      output_string_stream (stream, o->arg);
      output_data_stream (stream, &o->value, sizeof (o->value));
    }

  VEC_free (opt_t, heap, opts);
}

/* Write currently held options to an LTO IL section.  */

void
lto_write_options (void)
{
  char *const section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
  struct lto_output_stream stream;
  struct lto_simple_header header;
  struct lto_output_stream *header_stream;

  /* Targets and languages can provide defaults for -fexceptions but
     we only process user options from the command-line.  Until we
     serialize out a white list of options from the new global state
     explicitly append important options as user options here.  */
  if (flag_exceptions)
    lto_register_user_option (OPT_fexceptions, NULL, 1, CL_COMMON);

  lto_begin_section (section_name, !flag_wpa);
  free (section_name);

  memset (&stream, 0, sizeof (stream));
  output_options (&stream);

  memset (&header, 0, sizeof (header));
  header.lto_header.major_version = LTO_major_version;
  header.lto_header.minor_version = LTO_minor_version;
  header.lto_header.section_type = LTO_section_opts;

  header.compressed_size = 0;
  header.main_size = stream.total_size;

  header_stream = ((struct lto_output_stream *)
		   xcalloc (1, sizeof (*header_stream)));
  lto_output_data_stream (header_stream, &header, sizeof (header));
  lto_write_stream (header_stream);
  free (header_stream);

  lto_write_stream (&stream);
  lto_end_section ();
}

/* Unserialize an options vector from IB, and append to file_options.  */

static void
input_options (struct lto_input_block *ib)
{
  size_t length, i;

  input_data_block (ib, &length, sizeof (length));

  for (i = 0; i < length; i++)
    {
      opt_t o;

      input_data_block (ib, &o.type, sizeof (o.type));
      input_data_block (ib, &o.code, sizeof (o.code));
      o.arg = input_string_block (ib);
      input_data_block (ib, &o.value, sizeof (o.value));
      VEC_safe_push (opt_t, heap, file_options, &o);
    }
}

/* Read options from an LTO IL section.  */

void
lto_read_file_options (struct lto_file_decl_data *file_data)
{
  size_t len, l, skip;
  const char *data, *p;
  const struct lto_simple_header *header;
  int opts_offset;
  struct lto_input_block ib;

  data = lto_get_section_data (file_data, LTO_section_opts, NULL, &len);
  if (!data)
	  return;

  /* Option could be multiple sections merged (through ld -r) 
     Keep reading all options.  This is ok right now because
     the options just get mashed together anyways.
     This will have to be done differently once lto-opts knows
     how to associate options with different files. */
  l = len;
  p = data;
  do 
    { 
      header = (const struct lto_simple_header *) p;
      opts_offset = sizeof (*header);

      lto_check_version (header->lto_header.major_version,
			 header->lto_header.minor_version);

      LTO_INIT_INPUT_BLOCK (ib, p + opts_offset, 0, header->main_size);
      input_options (&ib);

      skip = header->main_size + opts_offset;
      l -= skip;
      p += skip;
    } 
  while (l > 0);

  lto_free_section_data (file_data, LTO_section_opts, 0, data, len);
}

/* Concatenate the user options and any file options read from an LTO IL
   file, and reissue them as if all had just been read in from the command
   line.  As with serialization, file options precede user options.  */

void
lto_reissue_options (void)
{
  VEC(opt_t, heap) *opts = concatenate_options (file_options, user_options);
  int i;
  opt_t *o;

  FOR_EACH_VEC_ELT (opt_t, opts, i, o)
    {
      void *flag_var = option_flag_var (o->code, &global_options);

      if (flag_var)
	set_option (&global_options, &global_options_set,
		    o->code, o->value, o->arg,
		    DK_UNSPECIFIED, UNKNOWN_LOCATION, global_dc);

      if (o->type == CL_TARGET)
	targetm.handle_option (o->code, o->arg, o->value);
      else if (o->type == CL_COMMON)
	gcc_assert (flag_var);
      else
	gcc_unreachable ();
    }

  /* Flag_shlib is usually set by finish_options, but we are issuing flag_pic
     too late.  */
  if (flag_pic && !flag_pie)
    flag_shlib = 1;
  VEC_free (opt_t, heap, opts);
}