aboutsummaryrefslogtreecommitdiff
path: root/gcc/lto/lto.c
blob: 2b7e5a242e351e4b7583f8a40436176fc4907236 (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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* Top-level LTO routines.
   Copyright 2006 Free Software Foundation, Inc.
   Contributed by CodeSourcery, Inc.

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 2, 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 COPYING.  If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */

#include <sys/mman.h>
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "opts.h"
#include "toplev.h"
#include "tree.h"
#include "tm.h"
#include "cgraph.h"
#include "ggc.h"
#include "lto.h"
#include "lto-tree.h"
#include "tree-ssa-operands.h"  /* For init_ssa_operands.  */
#include "langhooks.h"
#include "lto-section.h"
#include "lto-section-in.h"
#include "lto-tree-in.h"
#include "lto-tags.h"  		/* For LTO_tree_tag_names.  */
#include "tree-pass.h"


/* Read the constructors and inits.  */

static void
lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
{
  size_t len;
  const char *data = lto_get_section_data (file_data,
					   LTO_section_static_initializer, NULL, &len);
  lto_input_constructors_and_inits (file_data, data);
  lto_free_section_data (file_data, LTO_section_static_initializer, NULL, data, len);
}

/* Read the function body for the function associated with NODE if possible.  */

static void
lto_materialize_function (struct cgraph_node *node)
{
  tree decl = node->decl;
  struct lto_file_decl_data *file_data = node->local.lto_file_data;
  const char *data;
  size_t len;
  tree step;
  const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));

  data = lto_get_section_data (file_data, LTO_section_function_body,
			       name, &len);
  if (data)
    {
      struct function *fn;

      /* This function has a definition.  */
      TREE_STATIC (decl) = 1;
      DECL_EXTERNAL (decl) = 0;

      allocate_struct_function (decl, false);
      lto_input_function_body (file_data, decl, data);
      fn = DECL_STRUCT_FUNCTION (decl);
      lto_free_section_data (file_data, LTO_section_function_body, name, data, len);

      /* Look for initializers of constant variables and private
	 statics.  */
      for (step = fn->local_decls;
	   step;
	   step = TREE_CHAIN (step))
	{
	  tree decl = TREE_VALUE (step);
	  if (TREE_CODE (decl) == VAR_DECL
	      && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
	      && flag_unit_at_a_time)
	    varpool_finalize_decl (decl);
	}
    }
  else
    /* ### Shouldn't we just check this and assert if not?  */
    DECL_EXTERNAL (decl) = 1;

  /* Let the middle end know about the function.  */
  rest_of_decl_compilation (decl,
                            /*top_level=*/1,
                            /*at_end=*/0);
  if (cgraph_node (decl)->needed)
    cgraph_mark_reachable_node (cgraph_node (decl));
}

/* ### */
/* Initialize the globals vector with pointers to well-known trees.  */

static void
preload_common_nodes (struct data_in *data_in)
{
  unsigned i;

  /* The global tree for the main identifier is filled in by language-specific
     front-end initialization that is not run in the LTO back-end.  It appears
     that all languages that perform such initialization currently do so in the
     same way, so we do it here.  */
  if (!main_identifier_node)
    main_identifier_node = get_identifier ("main");

  for (i = 0; i < TI_MAX; i++)
    {
#ifdef GLOBAL_STREAMER_TRACE
      fprintf (stderr, "preloaded 0x%x: ", i);
      print_generic_expr (stderr, global_trees[i], 0);
      fprintf (stderr, "\n");
#endif
      VEC_safe_push (tree, heap, data_in->globals_index, global_trees[i]);
    }

  for (i = 0; i < itk_none; i++)
    {
#ifdef GLOBAL_STREAMER_TRACE
      fprintf (stderr, "preloaded 0x%x: ", i);
      print_generic_expr (stderr, integer_types[i], 0);
      fprintf (stderr, "\n");
#endif
      VEC_safe_push (tree, heap, data_in->globals_index, integer_types[i]);
    }
}

/* ### */
/* Load in the global vars and all of the types from the main symbol
   table.  */

static void
lto_read_decls (struct lto_file_decl_data *decl_data, const void *data)
{
  struct lto_decl_header * header 
      = (struct lto_decl_header *) data;

  int32_t types_offset = sizeof (struct lto_decl_header); 
  int32_t fields_offset
      = types_offset + (header->num_types * sizeof (uint32_t));
  int32_t fns_offset 
      = fields_offset + (header->num_field_decls * sizeof (uint32_t));
  int32_t vars_offset 
      = fns_offset + (header->num_fn_decls * sizeof (uint32_t));
  int32_t type_decls_offset 
      = vars_offset + (header->num_var_decls * sizeof (uint32_t));
  int32_t namespace_decls_offset
      = type_decls_offset + (header->num_type_decls * sizeof (uint32_t));

  uint32_t *in_types       = (uint32_t*)(data + types_offset);
  uint32_t *in_field_decls = (uint32_t*)(data + fields_offset);
  uint32_t *in_fn_decls    = (uint32_t*)(data + fns_offset);
  uint32_t *in_var_decls   = (uint32_t*)(data + vars_offset);
  uint32_t *in_type_decls  = (uint32_t*)(data + type_decls_offset);
  uint32_t *in_namespace_decls  = (uint32_t*)(data + namespace_decls_offset);

  int32_t main_offset
      = namespace_decls_offset + (header->num_namespace_decls * sizeof (uint32_t));
  int32_t string_offset = main_offset + header->main_size;
#ifdef LTO_STREAM_DEBUGGING
  int32_t debug_main_offset = string_offset + header->string_size;
#endif

  struct lto_input_block ib_main;
  struct lto_input_block debug_main;
  struct data_in data_in;
  int i;

  LTO_INIT_INPUT_BLOCK (ib_main, data + main_offset, 0, header->main_size);
  LTO_INIT_INPUT_BLOCK (debug_main, data + debug_main_offset, 0, header->debug_main_size);

  decl_data->field_decls = xcalloc (header->num_field_decls, sizeof (tree));
  decl_data->fn_decls    = xcalloc (header->num_fn_decls, sizeof (tree));
  decl_data->var_decls   = xcalloc (header->num_var_decls, sizeof (tree));
  decl_data->type_decls  = xcalloc (header->num_type_decls, sizeof (tree));
  decl_data->namespace_decls = xcalloc (header->num_namespace_decls, sizeof (tree));
  decl_data->types       = xcalloc (header->num_types, sizeof (tree));

  memset (&data_in, 0, sizeof (struct data_in));
  data_in.file_data          = decl_data;
  data_in.strings            = data + string_offset;
  data_in.strings_len        = header->string_size;
  data_in.globals_index	     = NULL;
  data_in.global	     = true;  /* ### unused */

  /* ### This doesn't belong here */
  /* ### Need initialization not done in lto_static_init () */
  lto_static_init_local ();

#ifdef LTO_STREAM_DEBUGGING
  lto_debug_context.out = lto_debug_in_fun;
  lto_debug_context.indent = 0;
  lto_debug_context.tag_names = LTO_tree_tag_names;
  lto_debug_context.current_data = &debug_main;
#endif

  /* Preload references to well-known trees.  */
  preload_common_nodes (&data_in);

  /* Read the global declarations and types.  */
  /* ### We should be a bit more graceful regarding truncated files. */
  while (ib_main.p < ib_main.len)
    {
      input_tree (&ib_main, &data_in);
      gcc_assert (ib_main.p <= ib_main.len);
    }

  /* Construct index vectors for use when reading function bodies.  */

  for (i=0; i<header->num_types; i++)
    decl_data->types[i]
        = VEC_index (tree, data_in.globals_index, in_types[i]);
  decl_data->num_types = header->num_types;

  for (i=0; i<header->num_field_decls; i++)
    decl_data->field_decls[i]
        = VEC_index (tree, data_in.globals_index, in_field_decls[i]);
  decl_data->num_field_decls = header->num_field_decls;

  for (i=0; i<header->num_fn_decls; i++)
    decl_data->fn_decls[i]
        = VEC_index (tree, data_in.globals_index, in_fn_decls[i]);
  decl_data->num_fn_decls = header->num_fn_decls;

  for (i=0; i<header->num_var_decls; i++)
    decl_data->var_decls[i]
        = VEC_index (tree, data_in.globals_index, in_var_decls[i]);
  decl_data->num_var_decls = header->num_var_decls;

  for (i=0; i<header->num_type_decls; i++)
    decl_data->type_decls[i]
        = VEC_index (tree, data_in.globals_index, in_type_decls[i]);
  decl_data->num_type_decls = header->num_type_decls;

  for (i=0; i<header->num_namespace_decls; i++)
    decl_data->namespace_decls[i]
        = VEC_index (tree, data_in.globals_index, in_namespace_decls[i]);
  decl_data->num_namespace_decls = header->num_namespace_decls;

  /* The globals index vector is needed only while reading.  */

  VEC_free (tree, heap, data_in.globals_index);
}

/* Generate a TREE representation for all types and external decls
   entities in FILE.  If an entity in FILE has already been read (from
   another object file), merge the two entities.  Returns the
   file_data from the last context.

   FIXME, this is a bug that will go away with Maddox's streaming
   merge since there will no longer be contexts.

   Read all of the globals out of the file.  Then read the cgraph
   and process the .o index into the cgraph nodes so that it can open
   the .o file to load the functions and ipa information.   */

static struct lto_file_decl_data*
lto_file_read (lto_file *file)
{
  struct lto_file_decl_data* file_data;
  char *data;
  size_t len;
  htab_t section_hash_table;

  file_data = xmalloc (sizeof (struct lto_file_decl_data));

  file_data->file_name = file->filename;
  file_data->fd = -1;

  section_hash_table = lto_elf_build_section_table (file);
  file_data->section_hash_table = section_hash_table;
  
  data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
  lto_read_decls (file_data, data);
  lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);

  /* ### We never free file_data.  */

  return file_data;
}

/****************************************************************************
  Input routines for reading sections from .o files.

  FIXME: These routines may need to be generalized.  They assume that
  the .o file can be read into memory and the secions just mapped.
  This may not be true if the .o file is in some form of archive.
****************************************************************************/

/* Page size of machine is used for mmap and munmap calls.  */
static size_t page_mask;

/* Get the section data of length LEN from FILENAME starting at
   OFFSET.  The data segment must be freed by the caller when the
   caller is finished.  Returns NULL if all was not well.  */

static char *
lto_read_section_data (struct lto_file_decl_data *file_data,
		       intptr_t offset, size_t len)
{
  char *result;
  intptr_t computed_len;
  intptr_t computed_offset;
  intptr_t diff;

  if (!page_mask)
    {
      size_t page_size = sysconf (_SC_PAGE_SIZE);
      page_mask = ~(page_size - 1);
    }

  if (file_data->fd == -1)
    file_data->fd = open (file_data->file_name, O_RDONLY);

  if (file_data->fd == -1)
    return NULL;

  computed_offset = offset & page_mask;
  diff = offset - computed_offset;
  computed_len = len + diff;

  result = mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
		 file_data->fd, computed_offset);
  if (result == MAP_FAILED)
    {
      close (file_data->fd);
      return NULL;
    }

  return result + diff;
}    


/* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
   NAME will be null unless the section type is for a function
   body.  */

static const char *
get_section_data (struct lto_file_decl_data *file_data,
		      enum lto_section_type section_type,
		      const char *name,
		      size_t *len)
{
  htab_t section_hash_table = file_data->section_hash_table;
  struct lto_section_slot *f_slot;
  struct lto_section_slot s_slot;
  const char *section_name = lto_get_section_name (section_type, name);
  char * data = NULL;

  s_slot.name = section_name;
  f_slot = (struct lto_section_slot *)htab_find (section_hash_table, &s_slot);
  if (f_slot)
    {
      data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
      *len = f_slot->len;
    }

  free ((char *)section_name);
  return data;
}


/* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
   starts at OFFSET and has LEN bytes.  */

static void
free_section_data (struct lto_file_decl_data *file_data,
		       enum lto_section_type section_type ATTRIBUTE_UNUSED,
		       const char *name ATTRIBUTE_UNUSED,
		       const char *offset, size_t len)
{
  intptr_t computed_len;
  intptr_t computed_offset;
  intptr_t diff;

  if (file_data->fd == -1)
    return;

  computed_offset = ((intptr_t)offset) & page_mask;
  diff = (intptr_t)offset - computed_offset;
  computed_len = len + diff;

  munmap ((void *)computed_offset, computed_len);
}


/* Needed so the garbage collector knows to root around in functions we
   have not yet materialized and the huge DIE -> tree table we keep
   around.  */
static GTY(()) lto_file *current_lto_file;

void
lto_main (int debug_p ATTRIBUTE_UNUSED)
{
  unsigned int i;
  unsigned int j = 0;
  tree decl;
  struct cgraph_node *node; 
  struct lto_file_decl_data** all_file_decl_data 
    = XNEWVEC (struct lto_file_decl_data*, num_in_fnames + 1);
  struct lto_file_decl_data* file_data = NULL;

  /* Set the hooks so that all of the ipa passes can read in their data.  */
  lto_set_in_hooks (all_file_decl_data, get_section_data,
		    free_section_data);

  /* Read all of the object files specified on the command line.  */
  for (i = 0; i < num_in_fnames; ++i)
    {
      current_lto_file = lto_elf_file_open (in_fnames[i]);
      if (!current_lto_file)
	break;
      file_data = lto_file_read (current_lto_file);
      if (!file_data)
	break;

      all_file_decl_data [j++] = file_data;

      lto_elf_file_close (current_lto_file);
      current_lto_file = NULL;
    }

  all_file_decl_data [j] = NULL;

  /* FIXME!!! This loop needs to be changed to use the pass manager to
     call the ipa passes directly.  */
  for (i = 0; i < j; i++)
    {
      struct lto_file_decl_data* file_data = all_file_decl_data [i];

      lto_materialize_constructors_and_inits (file_data);
    }

  ipa_read_summaries ();

  /* Now that we have input the cgraph, we need to clear all of the aux
     nodes and read the functions.  

     FIXME!!!!! This loop obviously leaves a lot to be desired:
     1) it loads all of the functions at once.  
     2) it closes and reopens the files over and over again. 

     It would obviously be better for the cgraph code to look to load
     a batch of functions and sort those functions by the file they
     come from and then load all of the functions from a give .o file
     at one time.  This of course will require that the open and close
     code be pulled out of lto_materialize_function, but that is a
     small part of what will be a complex set of management
     issues.  */
  for (node = cgraph_nodes; node; node = node->next)
    {
      /* FIXME!!!  There really needs to be some check to see if the
	 function is really not external here.  Currently the only
	 check is to see if the section was defined in the file_data
	 index.  There is of course the value in the node->aux field
	 that is nulled out in the previous line, but we should really
	 be able to look at the cgraph info at the is point and make
	 the proper determination.   Honza will fix this.  */
      lto_materialize_function (node);
    }

  /* Inform the middle end about the global variables we have seen.  */
  for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
    rest_of_decl_compilation (decl,
                              /*top_level=*/1,
                              /*at_end=*/0);

  /* Let the middle end know that we have read and merged all of the
     input files.  */ 
  /*cgraph_finalize_compilation_unit ();*/
  cgraph_optimize ();
}

#include "gt-lto-lto.h"