OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [lto/] [lto.c] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 716 jeremybenn
/* Top-level LTO routines.
2
   Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
3
   Contributed by CodeSourcery, Inc.
4
 
5
This file is part of GCC.
6
 
7
GCC is free software; you can redistribute it and/or modify it under
8
the terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 3, or (at your option) any later
10
version.
11
 
12
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13
WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15
for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING3.  If not see
19
<http://www.gnu.org/licenses/>.  */
20
 
21
#include "config.h"
22
#include "system.h"
23
#include "coretypes.h"
24
#include "opts.h"
25
#include "toplev.h"
26
#include "tree.h"
27
#include "tree-flow.h"
28
#include "diagnostic-core.h"
29
#include "tm.h"
30
#include "cgraph.h"
31
#include "ggc.h"
32
#include "tree-ssa-operands.h"
33
#include "tree-pass.h"
34
#include "langhooks.h"
35
#include "vec.h"
36
#include "bitmap.h"
37
#include "pointer-set.h"
38
#include "ipa-prop.h"
39
#include "common.h"
40
#include "debug.h"
41
#include "timevar.h"
42
#include "gimple.h"
43
#include "lto.h"
44
#include "lto-tree.h"
45
#include "lto-streamer.h"
46
#include "tree-streamer.h"
47
#include "splay-tree.h"
48
#include "params.h"
49
#include "ipa-inline.h"
50
#include "ipa-utils.h"
51
 
52
static GTY(()) tree first_personality_decl;
53
 
54
/* Returns a hash code for P.  */
55
 
56
static hashval_t
57
hash_name (const void *p)
58
{
59
  const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
60
  return (hashval_t) htab_hash_string (ds->name);
61
}
62
 
63
 
64
/* Returns nonzero if P1 and P2 are equal.  */
65
 
66
static int
67
eq_name (const void *p1, const void *p2)
68
{
69
  const struct lto_section_slot *s1 =
70
    (const struct lto_section_slot *) p1;
71
  const struct lto_section_slot *s2 =
72
    (const struct lto_section_slot *) p2;
73
 
74
  return strcmp (s1->name, s2->name) == 0;
75
}
76
 
77
/* Free lto_section_slot */
78
 
79
static void
80
free_with_string (void *arg)
81
{
82
  struct lto_section_slot *s = (struct lto_section_slot *)arg;
83
 
84
  free (CONST_CAST (char *, s->name));
85
  free (arg);
86
}
87
 
88
/* Create section hash table */
89
 
90
htab_t
91
lto_obj_create_section_hash_table (void)
92
{
93
  return htab_create (37, hash_name, eq_name, free_with_string);
94
}
95
 
96
/* Delete an allocated integer KEY in the splay tree.  */
97
 
98
static void
99
lto_splay_tree_delete_id (splay_tree_key key)
100
{
101
  free ((void *) key);
102
}
103
 
104
/* Compare splay tree node ids A and B.  */
105
 
106
static int
107
lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
108
{
109
  unsigned HOST_WIDE_INT ai;
110
  unsigned HOST_WIDE_INT bi;
111
 
112
  ai = *(unsigned HOST_WIDE_INT *) a;
113
  bi = *(unsigned HOST_WIDE_INT *) b;
114
 
115
  if (ai < bi)
116
    return -1;
117
  else if (ai > bi)
118
    return 1;
119
  return 0;
120
}
121
 
122
/* Look up splay tree node by ID in splay tree T.  */
123
 
124
static splay_tree_node
125
lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
126
{
127
  return splay_tree_lookup (t, (splay_tree_key) &id);
128
}
129
 
130
/* Check if KEY has ID.  */
131
 
132
static bool
133
lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
134
{
135
  return *(unsigned HOST_WIDE_INT *) key == id;
136
}
137
 
138
/* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
139
   The ID is allocated separately because we need HOST_WIDE_INTs which may
140
   be wider than a splay_tree_key. */
141
 
142
static void
143
lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
144
                       struct lto_file_decl_data *file_data)
145
{
146
  unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
147
  *idp = id;
148
  splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
149
}
150
 
151
/* Create a splay tree.  */
152
 
153
static splay_tree
154
lto_splay_tree_new (void)
155
{
156
  return splay_tree_new (lto_splay_tree_compare_ids,
157
                         lto_splay_tree_delete_id,
158
                         NULL);
159
}
160
 
161
/* Read the constructors and inits.  */
162
 
163
static void
164
lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
165
{
166
  size_t len;
167
  const char *data = lto_get_section_data (file_data,
168
                                           LTO_section_static_initializer,
169
                                           NULL, &len);
170
  lto_input_constructors_and_inits (file_data, data);
171
  lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
172
                         data, len);
173
}
174
 
175
/* Return true when NODE has a clone that is analyzed (i.e. we need
176
   to load its body even if the node itself is not needed).  */
177
 
178
static bool
179
has_analyzed_clone_p (struct cgraph_node *node)
180
{
181
  struct cgraph_node *orig = node;
182
  node = node->clones;
183
  if (node)
184
    while (node != orig)
185
      {
186
        if (node->analyzed)
187
          return true;
188
        if (node->clones)
189
          node = node->clones;
190
        else if (node->next_sibling_clone)
191
          node = node->next_sibling_clone;
192
        else
193
          {
194
            while (node != orig && !node->next_sibling_clone)
195
              node = node->clone_of;
196
            if (node != orig)
197
              node = node->next_sibling_clone;
198
          }
199
      }
200
  return false;
201
}
202
 
203
/* Read the function body for the function associated with NODE.  */
204
 
205
static void
206
lto_materialize_function (struct cgraph_node *node)
207
{
208
  tree decl;
209
  struct lto_file_decl_data *file_data;
210
  const char *data, *name;
211
  size_t len;
212
 
213
  decl = node->decl;
214
  /* Read in functions with body (analyzed nodes)
215
     and also functions that are needed to produce virtual clones.  */
216
  if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node))
217
    {
218
      /* Clones and thunks don't need to be read.  */
219
      if (node->clone_of)
220
        return;
221
 
222
      /* Load the function body only if not operating in WPA mode.  In
223
         WPA mode, the body of the function is not needed.  */
224
      if (!flag_wpa)
225
        {
226
          file_data = node->local.lto_file_data;
227
          name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
228
 
229
          /* We may have renamed the declaration, e.g., a static function.  */
230
          name = lto_get_decl_name_mapping (file_data, name);
231
 
232
          data = lto_get_section_data (file_data, LTO_section_function_body,
233
                                       name, &len);
234
          if (!data)
235
            fatal_error ("%s: section %s is missing",
236
                         file_data->file_name,
237
                         name);
238
 
239
          gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
240
 
241
          allocate_struct_function (decl, false);
242
          announce_function (decl);
243
          lto_input_function_body (file_data, decl, data);
244
          if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
245
            first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
246
          lto_stats.num_function_bodies++;
247
          lto_free_section_data (file_data, LTO_section_function_body, name,
248
                                 data, len);
249
          ggc_collect ();
250
        }
251
    }
252
 
253
  /* Let the middle end know about the function.  */
254
  rest_of_decl_compilation (decl, 1, 0);
255
}
256
 
257
 
258
/* Decode the content of memory pointed to by DATA in the in decl
259
   state object STATE. DATA_IN points to a data_in structure for
260
   decoding. Return the address after the decoded object in the
261
   input.  */
262
 
263
static const uint32_t *
264
lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
265
                        struct lto_in_decl_state *state)
266
{
267
  uint32_t ix;
268
  tree decl;
269
  uint32_t i, j;
270
 
271
  ix = *data++;
272
  decl = streamer_tree_cache_get (data_in->reader_cache, ix);
273
  if (TREE_CODE (decl) != FUNCTION_DECL)
274
    {
275
      gcc_assert (decl == void_type_node);
276
      decl = NULL_TREE;
277
    }
278
  state->fn_decl = decl;
279
 
280
  for (i = 0; i < LTO_N_DECL_STREAMS; i++)
281
    {
282
      uint32_t size = *data++;
283
      tree *decls = ggc_alloc_vec_tree (size);
284
 
285
      for (j = 0; j < size; j++)
286
        decls[j] = streamer_tree_cache_get (data_in->reader_cache, data[j]);
287
 
288
      state->streams[i].size = size;
289
      state->streams[i].trees = decls;
290
      data += size;
291
    }
292
 
293
  return data;
294
}
295
 
296
/* A hashtable of trees that potentially refer to variables or functions
297
   that must be replaced with their prevailing variant.  */
298
static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
299
  tree_with_vars;
300
 
301
/* Remember that T is a tree that (potentially) refers to a variable
302
   or function decl that may be replaced with its prevailing variant.  */
303
static void
304
remember_with_vars (tree t)
305
{
306
  *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
307
}
308
 
309
#define GIMPLE_REGISTER_TYPE(tt) \
310
   (TREE_VISITED (tt) ? gimple_register_type (tt) : tt)
311
 
312
#define LTO_FIXUP_TREE(tt) \
313
  do \
314
    { \
315
      if (tt) \
316
        { \
317
          if (TYPE_P (tt)) \
318
            (tt) = GIMPLE_REGISTER_TYPE (tt); \
319
          if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
320
            remember_with_vars (t); \
321
        } \
322
    } while (0)
323
 
324
static void lto_fixup_types (tree);
325
 
326
/* Fix up fields of a tree_typed T.  */
327
 
328
static void
329
lto_ft_typed (tree t)
330
{
331
  LTO_FIXUP_TREE (TREE_TYPE (t));
332
}
333
 
334
/* Fix up fields of a tree_common T.  */
335
 
336
static void
337
lto_ft_common (tree t)
338
{
339
  lto_ft_typed (t);
340
  LTO_FIXUP_TREE (TREE_CHAIN (t));
341
}
342
 
343
/* Fix up fields of a decl_minimal T.  */
344
 
345
static void
346
lto_ft_decl_minimal (tree t)
347
{
348
  lto_ft_common (t);
349
  LTO_FIXUP_TREE (DECL_NAME (t));
350
  LTO_FIXUP_TREE (DECL_CONTEXT (t));
351
}
352
 
353
/* Fix up fields of a decl_common T.  */
354
 
355
static void
356
lto_ft_decl_common (tree t)
357
{
358
  lto_ft_decl_minimal (t);
359
  LTO_FIXUP_TREE (DECL_SIZE (t));
360
  LTO_FIXUP_TREE (DECL_SIZE_UNIT (t));
361
  LTO_FIXUP_TREE (DECL_INITIAL (t));
362
  LTO_FIXUP_TREE (DECL_ATTRIBUTES (t));
363
  LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t));
364
}
365
 
366
/* Fix up fields of a decl_with_vis T.  */
367
 
368
static void
369
lto_ft_decl_with_vis (tree t)
370
{
371
  lto_ft_decl_common (t);
372
 
373
  /* Accessor macro has side-effects, use field-name here. */
374
  LTO_FIXUP_TREE (t->decl_with_vis.assembler_name);
375
  LTO_FIXUP_TREE (DECL_SECTION_NAME (t));
376
}
377
 
378
/* Fix up fields of a decl_non_common T.  */
379
 
380
static void
381
lto_ft_decl_non_common (tree t)
382
{
383
  lto_ft_decl_with_vis (t);
384
  LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t));
385
  LTO_FIXUP_TREE (DECL_RESULT_FLD (t));
386
  LTO_FIXUP_TREE (DECL_VINDEX (t));
387
  /* The C frontends may create exact duplicates for DECL_ORIGINAL_TYPE
388
     like for 'typedef enum foo foo'.  We have no way of avoiding to
389
     merge them and dwarf2out.c cannot deal with this,
390
     so fix this up by clearing DECL_ORIGINAL_TYPE in this case.  */
391
  if (TREE_CODE (t) == TYPE_DECL
392
      && DECL_ORIGINAL_TYPE (t) == TREE_TYPE (t))
393
    DECL_ORIGINAL_TYPE (t) = NULL_TREE;
394
}
395
 
396
/* Fix up fields of a decl_non_common T.  */
397
 
398
static void
399
lto_ft_function (tree t)
400
{
401
  lto_ft_decl_non_common (t);
402
  LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t));
403
}
404
 
405
/* Fix up fields of a field_decl T.  */
406
 
407
static void
408
lto_ft_field_decl (tree t)
409
{
410
  lto_ft_decl_common (t);
411
  LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t));
412
  LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t));
413
  LTO_FIXUP_TREE (DECL_QUALIFIER (t));
414
  LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t));
415
  LTO_FIXUP_TREE (DECL_FCONTEXT (t));
416
}
417
 
418
/* Fix up fields of a type T.  */
419
 
420
static void
421
lto_ft_type (tree t)
422
{
423
  lto_ft_common (t);
424
  LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t));
425
  LTO_FIXUP_TREE (TYPE_SIZE (t));
426
  LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t));
427
  LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t));
428
  LTO_FIXUP_TREE (TYPE_NAME (t));
429
 
430
  /* Accessors are for derived node types only. */
431
  if (!POINTER_TYPE_P (t))
432
    LTO_FIXUP_TREE (TYPE_MINVAL (t));
433
  LTO_FIXUP_TREE (TYPE_MAXVAL (t));
434
 
435
  /* Accessor is for derived node types only. */
436
  LTO_FIXUP_TREE (t->type_non_common.binfo);
437
 
438
  LTO_FIXUP_TREE (TYPE_CONTEXT (t));
439
}
440
 
441
/* Fix up fields of a BINFO T.  */
442
 
443
static void
444
lto_ft_binfo (tree t)
445
{
446
  unsigned HOST_WIDE_INT i, n;
447
  tree base, saved_base;
448
 
449
  lto_ft_common (t);
450
  LTO_FIXUP_TREE (BINFO_VTABLE (t));
451
  LTO_FIXUP_TREE (BINFO_OFFSET (t));
452
  LTO_FIXUP_TREE (BINFO_VIRTUALS (t));
453
  LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t));
454
  n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
455
  for (i = 0; i < n; i++)
456
    {
457
      saved_base = base = BINFO_BASE_ACCESS (t, i);
458
      LTO_FIXUP_TREE (base);
459
      if (base != saved_base)
460
        VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
461
    }
462
  LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t));
463
  LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t));
464
  LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t));
465
  n = BINFO_N_BASE_BINFOS (t);
466
  for (i = 0; i < n; i++)
467
    {
468
      saved_base = base = BINFO_BASE_BINFO (t, i);
469
      LTO_FIXUP_TREE (base);
470
      if (base != saved_base)
471
        VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
472
    }
473
}
474
 
475
/* Fix up fields of a CONSTRUCTOR T.  */
476
 
477
static void
478
lto_ft_constructor (tree t)
479
{
480
  unsigned HOST_WIDE_INT idx;
481
  constructor_elt *ce;
482
 
483
  lto_ft_typed (t);
484
 
485
  for (idx = 0;
486
       VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
487
       idx++)
488
    {
489
      LTO_FIXUP_TREE (ce->index);
490
      LTO_FIXUP_TREE (ce->value);
491
    }
492
}
493
 
494
/* Fix up fields of an expression tree T.  */
495
 
496
static void
497
lto_ft_expr (tree t)
498
{
499
  int i;
500
  lto_ft_typed (t);
501
  for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
502
    LTO_FIXUP_TREE (TREE_OPERAND (t, i));
503
}
504
 
505
/* Given a tree T fixup fields of T by replacing types with their merged
506
   variant and other entities by an equal entity from an earlier compilation
507
   unit, or an entity being canonical in a different way.  This includes
508
   for instance integer or string constants.  */
509
 
510
static void
511
lto_fixup_types (tree t)
512
{
513
  switch (TREE_CODE (t))
514
    {
515
    case IDENTIFIER_NODE:
516
      break;
517
 
518
    case TREE_LIST:
519
      LTO_FIXUP_TREE (TREE_VALUE (t));
520
      LTO_FIXUP_TREE (TREE_PURPOSE (t));
521
      LTO_FIXUP_TREE (TREE_CHAIN (t));
522
      break;
523
 
524
    case FIELD_DECL:
525
      lto_ft_field_decl (t);
526
      break;
527
 
528
    case LABEL_DECL:
529
    case CONST_DECL:
530
    case PARM_DECL:
531
    case RESULT_DECL:
532
    case IMPORTED_DECL:
533
      lto_ft_decl_common (t);
534
      break;
535
 
536
    case VAR_DECL:
537
      lto_ft_decl_with_vis (t);
538
      break;
539
 
540
    case TYPE_DECL:
541
      lto_ft_decl_non_common (t);
542
      break;
543
 
544
    case FUNCTION_DECL:
545
      lto_ft_function (t);
546
      break;
547
 
548
    case TREE_BINFO:
549
      lto_ft_binfo (t);
550
      break;
551
 
552
    case PLACEHOLDER_EXPR:
553
      lto_ft_common (t);
554
      break;
555
 
556
    case BLOCK:
557
    case TRANSLATION_UNIT_DECL:
558
    case OPTIMIZATION_NODE:
559
    case TARGET_OPTION_NODE:
560
      break;
561
 
562
    default:
563
      if (TYPE_P (t))
564
        lto_ft_type (t);
565
      else if (TREE_CODE (t) == CONSTRUCTOR)
566
        lto_ft_constructor (t);
567
      else if (CONSTANT_CLASS_P (t))
568
        LTO_FIXUP_TREE (TREE_TYPE (t));
569
      else if (EXPR_P (t))
570
        {
571
          lto_ft_expr (t);
572
        }
573
      else
574
        {
575
          remember_with_vars (t);
576
        }
577
    }
578
}
579
 
580
 
581
/* Return the resolution for the decl with index INDEX from DATA_IN. */
582
 
583
static enum ld_plugin_symbol_resolution
584
get_resolution (struct data_in *data_in, unsigned index)
585
{
586
  if (data_in->globals_resolution)
587
    {
588
      ld_plugin_symbol_resolution_t ret;
589
      /* We can have references to not emitted functions in
590
         DECL_FUNCTION_PERSONALITY at least.  So we can and have
591
         to indeed return LDPR_UNKNOWN in some cases.   */
592
      if (VEC_length (ld_plugin_symbol_resolution_t,
593
                      data_in->globals_resolution) <= index)
594
        return LDPR_UNKNOWN;
595
      ret = VEC_index (ld_plugin_symbol_resolution_t,
596
                       data_in->globals_resolution,
597
                       index);
598
      return ret;
599
    }
600
  else
601
    /* Delay resolution finding until decl merging.  */
602
    return LDPR_UNKNOWN;
603
}
604
 
605
 
606
/* Register DECL with the global symbol table and change its
607
   name if necessary to avoid name clashes for static globals across
608
   different files.  */
609
 
610
static void
611
lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl)
612
{
613
  tree context;
614
 
615
  /* Variable has file scope, not local. Need to ensure static variables
616
     between different files don't clash unexpectedly.  */
617
  if (!TREE_PUBLIC (decl)
618
      && !((context = decl_function_context (decl))
619
           && auto_var_in_fn_p (decl, context)))
620
    {
621
      /* ??? We normally pre-mangle names before we serialize them
622
         out.  Here, in lto1, we do not know the language, and
623
         thus cannot do the mangling again. Instead, we just
624
         append a suffix to the mangled name.  The resulting name,
625
         however, is not a properly-formed mangled name, and will
626
         confuse any attempt to unmangle it.  */
627
      const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
628
      char *label;
629
 
630
      ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
631
      SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
632
      rest_of_decl_compilation (decl, 1, 0);
633
      VEC_safe_push (tree, gc, lto_global_var_decls, decl);
634
    }
635
 
636
  /* If this variable has already been declared, queue the
637
     declaration for merging.  */
638
  if (TREE_PUBLIC (decl))
639
    {
640
      unsigned ix;
641
      if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
642
        gcc_unreachable ();
643
      lto_symtab_register_decl (decl, get_resolution (data_in, ix),
644
                                data_in->file_data);
645
    }
646
}
647
 
648
 
649
/* Register DECL with the global symbol table and change its
650
   name if necessary to avoid name clashes for static globals across
651
   different files.  DATA_IN contains descriptors and tables for the
652
   file being read.  */
653
 
654
static void
655
lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl)
656
{
657
  /* Need to ensure static entities between different files
658
     don't clash unexpectedly.  */
659
  if (!TREE_PUBLIC (decl))
660
    {
661
      /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
662
         may set the assembler name where it was previously empty.  */
663
      tree old_assembler_name = decl->decl_with_vis.assembler_name;
664
 
665
      /* FIXME lto: We normally pre-mangle names before we serialize
666
         them out.  Here, in lto1, we do not know the language, and
667
         thus cannot do the mangling again. Instead, we just append a
668
         suffix to the mangled name.  The resulting name, however, is
669
         not a properly-formed mangled name, and will confuse any
670
         attempt to unmangle it.  */
671
      const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
672
      char *label;
673
 
674
      ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
675
      SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
676
 
677
      /* We may arrive here with the old assembler name not set
678
         if the function body is not needed, e.g., it has been
679
         inlined away and does not appear in the cgraph.  */
680
      if (old_assembler_name)
681
        {
682
          tree new_assembler_name = DECL_ASSEMBLER_NAME (decl);
683
 
684
          /* Make the original assembler name available for later use.
685
             We may have used it to indicate the section within its
686
             object file where the function body may be found.
687
             FIXME lto: Find a better way to maintain the function decl
688
             to body section mapping so we don't need this hack.  */
689
          lto_record_renamed_decl (data_in->file_data,
690
                                   IDENTIFIER_POINTER (old_assembler_name),
691
                                   IDENTIFIER_POINTER (new_assembler_name));
692
        }
693
    }
694
 
695
  /* If this variable has already been declared, queue the
696
     declaration for merging.  */
697
  if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
698
    {
699
      unsigned ix;
700
      if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
701
        gcc_unreachable ();
702
      lto_symtab_register_decl (decl, get_resolution (data_in, ix),
703
                                data_in->file_data);
704
    }
705
}
706
 
707
 
708
/* Given a streamer cache structure DATA_IN (holding a sequence of trees
709
   for one compilation unit) go over all trees starting at index FROM until the
710
   end of the sequence and replace fields of those trees, and the trees
711
   themself with their canonical variants as per gimple_register_type.  */
712
 
713
static void
714
uniquify_nodes (struct data_in *data_in, unsigned from)
715
{
716
  struct streamer_tree_cache_d *cache = data_in->reader_cache;
717
  unsigned len = VEC_length (tree, cache->nodes);
718
  unsigned i;
719
 
720
  /* Go backwards because children streamed for the first time come
721
     as part of their parents, and hence are created after them.  */
722
 
723
  /* First register all the types in the cache.  This makes sure to
724
     have the original structure in the type cycles when registering
725
     them and computing hashes.  */
726
  for (i = len; i-- > from;)
727
    {
728
      tree t = VEC_index (tree, cache->nodes, i);
729
      if (t && TYPE_P (t))
730
        {
731
          tree newt = gimple_register_type (t);
732
          /* Mark non-prevailing types so we fix them up.  No need
733
             to reset that flag afterwards - nothing that refers
734
             to those types is left and they are collected.  */
735
          if (newt != t)
736
            TREE_VISITED (t) = 1;
737
        }
738
    }
739
 
740
  /* Second fixup all trees in the new cache entries.  */
741
  for (i = len; i-- > from;)
742
    {
743
      tree t = VEC_index (tree, cache->nodes, i);
744
      tree oldt = t;
745
      if (!t)
746
        continue;
747
 
748
      /* First fixup the fields of T.  */
749
      lto_fixup_types (t);
750
 
751
      if (!TYPE_P (t))
752
        continue;
753
 
754
      /* Now try to find a canonical variant of T itself.  */
755
      t = GIMPLE_REGISTER_TYPE (t);
756
 
757
      if (t == oldt)
758
        {
759
          /* The following re-creates proper variant lists while fixing up
760
             the variant leaders.  We do not stream TYPE_NEXT_VARIANT so the
761
             variant list state before fixup is broken.  */
762
          tree tem, mv;
763
 
764
          /* Remove us from our main variant list if we are not the
765
             variant leader.  */
766
          if (TYPE_MAIN_VARIANT (t) != t)
767
            {
768
              tem = TYPE_MAIN_VARIANT (t);
769
              while (tem && TYPE_NEXT_VARIANT (tem) != t)
770
                tem = TYPE_NEXT_VARIANT (tem);
771
              if (tem)
772
                TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
773
              TYPE_NEXT_VARIANT (t) = NULL_TREE;
774
            }
775
 
776
          /* Query our new main variant.  */
777
          mv = GIMPLE_REGISTER_TYPE (TYPE_MAIN_VARIANT (t));
778
 
779
          /* If we were the variant leader and we get replaced ourselves drop
780
             all variants from our list.  */
781
          if (TYPE_MAIN_VARIANT (t) == t
782
              && mv != t)
783
            {
784
              tem = t;
785
              while (tem)
786
                {
787
                  tree tem2 = TYPE_NEXT_VARIANT (tem);
788
                  TYPE_NEXT_VARIANT (tem) = NULL_TREE;
789
                  tem = tem2;
790
                }
791
            }
792
 
793
          /* If we are not our own variant leader link us into our new leaders
794
             variant list.  */
795
          if (mv != t)
796
            {
797
              TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
798
              TYPE_NEXT_VARIANT (mv) = t;
799
              if (RECORD_OR_UNION_TYPE_P (t))
800
                TYPE_BINFO (t) = TYPE_BINFO (mv);
801
            }
802
 
803
          /* Finally adjust our main variant and fix it up.  */
804
          TYPE_MAIN_VARIANT (t) = mv;
805
 
806
          /* The following reconstructs the pointer chains
807
             of the new pointed-to type if we are a main variant.  We do
808
             not stream those so they are broken before fixup.  */
809
          if (TREE_CODE (t) == POINTER_TYPE
810
              && TYPE_MAIN_VARIANT (t) == t)
811
            {
812
              TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
813
              TYPE_POINTER_TO (TREE_TYPE (t)) = t;
814
            }
815
          else if (TREE_CODE (t) == REFERENCE_TYPE
816
                   && TYPE_MAIN_VARIANT (t) == t)
817
            {
818
              TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
819
              TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
820
            }
821
        }
822
 
823
      else
824
        {
825
          if (RECORD_OR_UNION_TYPE_P (t))
826
            {
827
              tree f1, f2;
828
              if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt))
829
                for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt);
830
                     f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
831
                  {
832
                    unsigned ix;
833
                    gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2));
834
                    if (!streamer_tree_cache_lookup (cache, f2, &ix))
835
                      gcc_unreachable ();
836
                    /* If we're going to replace an element which we'd
837
                       still visit in the next iterations, we wouldn't
838
                       handle it, so do it here.  We do have to handle it
839
                       even though the field_decl itself will be removed,
840
                       as it could refer to e.g. integer_cst which we
841
                       wouldn't reach via any other way, hence they
842
                       (and their type) would stay uncollected.  */
843
                    /* ???  We should rather make sure to replace all
844
                       references to f2 with f1.  That means handling
845
                       COMPONENT_REFs and CONSTRUCTOR elements in
846
                       lto_fixup_types and special-case the field-decl
847
                       operand handling.  */
848
                    if (ix < i)
849
                      lto_fixup_types (f2);
850
                    streamer_tree_cache_insert_at (cache, f1, ix);
851
                  }
852
            }
853
 
854
          /* If we found a tree that is equal to oldt replace it in the
855
             cache, so that further users (in the various LTO sections)
856
             make use of it.  */
857
          streamer_tree_cache_insert_at (cache, t, i);
858
        }
859
    }
860
 
861
  /* Finally compute the canonical type of all TREE_TYPEs and register
862
     VAR_DECL and FUNCTION_DECL nodes in the symbol table.
863
     From this point there are no longer any types with
864
     TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
865
     This step requires the TYPE_POINTER_TO lists being present, so
866
     make sure it is done last.  */
867
  for (i = len; i-- > from;)
868
    {
869
      tree t = VEC_index (tree, cache->nodes, i);
870
      if (t == NULL_TREE)
871
        continue;
872
 
873
      if (TREE_CODE (t) == VAR_DECL)
874
        lto_register_var_decl_in_symtab (data_in, t);
875
      else if (TREE_CODE (t) == FUNCTION_DECL && !DECL_BUILT_IN (t))
876
        lto_register_function_decl_in_symtab (data_in, t);
877
      else if (!flag_wpa
878
               && TREE_CODE (t) == TYPE_DECL)
879
        debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
880
      else if (TYPE_P (t) && !TYPE_CANONICAL (t))
881
        TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
882
    }
883
}
884
 
885
 
886
/* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
887
   RESOLUTIONS is the set of symbols picked by the linker (read from the
888
   resolution file when the linker plugin is being used).  */
889
 
890
static void
891
lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
892
                VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
893
{
894
  const struct lto_decl_header *header = (const struct lto_decl_header *) data;
895
  const int decl_offset = sizeof (struct lto_decl_header);
896
  const int main_offset = decl_offset + header->decl_state_size;
897
  const int string_offset = main_offset + header->main_size;
898
  struct lto_input_block ib_main;
899
  struct data_in *data_in;
900
  unsigned int i;
901
  const uint32_t *data_ptr, *data_end;
902
  uint32_t num_decl_states;
903
 
904
  LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
905
                        header->main_size);
906
 
907
  data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
908
                                header->string_size, resolutions);
909
 
910
  /* We do not uniquify the pre-loaded cache entries, those are middle-end
911
     internal types that should not be merged.  */
912
 
913
  /* Read the global declarations and types.  */
914
  while (ib_main.p < ib_main.len)
915
    {
916
      tree t;
917
      unsigned from = VEC_length (tree, data_in->reader_cache->nodes);
918
      t = stream_read_tree (&ib_main, data_in);
919
      gcc_assert (t && ib_main.p <= ib_main.len);
920
      uniquify_nodes (data_in, from);
921
    }
922
 
923
  /* Read in lto_in_decl_state objects.  */
924
  data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
925
  data_end =
926
     (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
927
  num_decl_states = *data_ptr++;
928
 
929
  gcc_assert (num_decl_states > 0);
930
  decl_data->global_decl_state = lto_new_in_decl_state ();
931
  data_ptr = lto_read_in_decl_state (data_in, data_ptr,
932
                                     decl_data->global_decl_state);
933
 
934
  /* Read in per-function decl states and enter them in hash table.  */
935
  decl_data->function_decl_states =
936
    htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
937
 
938
  for (i = 1; i < num_decl_states; i++)
939
    {
940
      struct lto_in_decl_state *state = lto_new_in_decl_state ();
941
      void **slot;
942
 
943
      data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
944
      slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
945
      gcc_assert (*slot == NULL);
946
      *slot = state;
947
    }
948
 
949
  if (data_ptr != data_end)
950
    internal_error ("bytecode stream: garbage at the end of symbols section");
951
 
952
  /* Set the current decl state to be the global state. */
953
  decl_data->current_decl_state = decl_data->global_decl_state;
954
 
955
  lto_data_in_delete (data_in);
956
}
957
 
958
/* Custom version of strtoll, which is not portable.  */
959
 
960
static HOST_WIDEST_INT
961
lto_parse_hex (const char *p)
962
{
963
  HOST_WIDEST_INT ret = 0;
964
 
965
  for (; *p != '\0'; ++p)
966
    {
967
      char c = *p;
968
      unsigned char part;
969
      ret <<= 4;
970
      if (c >= '0' && c <= '9')
971
        part = c - '0';
972
      else if (c >= 'a' && c <= 'f')
973
        part = c - 'a' + 10;
974
      else if (c >= 'A' && c <= 'F')
975
        part = c - 'A' + 10;
976
      else
977
        internal_error ("could not parse hex number");
978
      ret |= part;
979
    }
980
 
981
  return ret;
982
}
983
 
984
/* Read resolution for file named FILE_NAME. The resolution is read from
985
   RESOLUTION. */
986
 
987
static void
988
lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
989
{
990
  /* We require that objects in the resolution file are in the same
991
     order as the lto1 command line. */
992
  unsigned int name_len;
993
  char *obj_name;
994
  unsigned int num_symbols;
995
  unsigned int i;
996
  struct lto_file_decl_data *file_data;
997
  unsigned max_index = 0;
998
  splay_tree_node nd = NULL;
999
 
1000
  if (!resolution)
1001
    return;
1002
 
1003
  name_len = strlen (file->filename);
1004
  obj_name = XNEWVEC (char, name_len + 1);
1005
  fscanf (resolution, " ");   /* Read white space. */
1006
 
1007
  fread (obj_name, sizeof (char), name_len, resolution);
1008
  obj_name[name_len] = '\0';
1009
  if (filename_cmp (obj_name, file->filename) != 0)
1010
    internal_error ("unexpected file name %s in linker resolution file. "
1011
                    "Expected %s", obj_name, file->filename);
1012
  if (file->offset != 0)
1013
    {
1014
      int t;
1015
      char offset_p[17];
1016
      HOST_WIDEST_INT offset;
1017
      t = fscanf (resolution, "@0x%16s", offset_p);
1018
      if (t != 1)
1019
        internal_error ("could not parse file offset");
1020
      offset = lto_parse_hex (offset_p);
1021
      if (offset != file->offset)
1022
        internal_error ("unexpected offset");
1023
    }
1024
 
1025
  free (obj_name);
1026
 
1027
  fscanf (resolution, "%u", &num_symbols);
1028
 
1029
  for (i = 0; i < num_symbols; i++)
1030
    {
1031
      int t;
1032
      unsigned index;
1033
      unsigned HOST_WIDE_INT id;
1034
      char r_str[27];
1035
      enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1036
      unsigned int j;
1037
      unsigned int lto_resolution_str_len =
1038
        sizeof (lto_resolution_str) / sizeof (char *);
1039
 
1040
      t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1041
                  &index, &id, r_str);
1042
      if (t != 3)
1043
        internal_error ("invalid line in the resolution file");
1044
      if (index > max_index)
1045
        max_index = index;
1046
 
1047
      for (j = 0; j < lto_resolution_str_len; j++)
1048
        {
1049
          if (strcmp (lto_resolution_str[j], r_str) == 0)
1050
            {
1051
              r = (enum ld_plugin_symbol_resolution) j;
1052
              break;
1053
            }
1054
        }
1055
      if (j == lto_resolution_str_len)
1056
        internal_error ("invalid resolution in the resolution file");
1057
 
1058
      if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1059
        {
1060
          nd = lto_splay_tree_lookup (file_ids, id);
1061
          if (nd == NULL)
1062
            internal_error ("resolution sub id " HOST_WIDE_INT_PRINT_HEX_PURE
1063
                            " not in object file", id);
1064
        }
1065
 
1066
      file_data = (struct lto_file_decl_data *)nd->value;
1067
      VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap,
1068
                             file_data->resolutions,
1069
                             max_index + 1);
1070
      VEC_replace (ld_plugin_symbol_resolution_t,
1071
                   file_data->resolutions, index, r);
1072
    }
1073
}
1074
 
1075
/* List of file_decl_datas */
1076
struct file_data_list
1077
  {
1078
    struct lto_file_decl_data *first, *last;
1079
  };
1080
 
1081
/* Is the name for a id'ed LTO section? */
1082
 
1083
static int
1084
lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1085
{
1086
  const char *s;
1087
 
1088
  if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
1089
    return 0;
1090
  s = strrchr (name, '.');
1091
  return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1092
}
1093
 
1094
/* Create file_data of each sub file id */
1095
 
1096
static int
1097
create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1098
                            struct file_data_list *list)
1099
{
1100
  struct lto_section_slot s_slot, *new_slot;
1101
  unsigned HOST_WIDE_INT id;
1102
  splay_tree_node nd;
1103
  void **hash_slot;
1104
  char *new_name;
1105
  struct lto_file_decl_data *file_data;
1106
 
1107
  if (!lto_section_with_id (ls->name, &id))
1108
    return 1;
1109
 
1110
  /* Find hash table of sub module id */
1111
  nd = lto_splay_tree_lookup (file_ids, id);
1112
  if (nd != NULL)
1113
    {
1114
      file_data = (struct lto_file_decl_data *)nd->value;
1115
    }
1116
  else
1117
    {
1118
      file_data = ggc_alloc_lto_file_decl_data ();
1119
      memset(file_data, 0, sizeof (struct lto_file_decl_data));
1120
      file_data->id = id;
1121
      file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1122
      lto_splay_tree_insert (file_ids, id, file_data);
1123
 
1124
      /* Maintain list in linker order */
1125
      if (!list->first)
1126
        list->first = file_data;
1127
      if (list->last)
1128
        list->last->next = file_data;
1129
      list->last = file_data;
1130
    }
1131
 
1132
  /* Copy section into sub module hash table */
1133
  new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
1134
  s_slot.name = new_name;
1135
  hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
1136
  gcc_assert (*hash_slot == NULL);
1137
 
1138
  new_slot = XDUP (struct lto_section_slot, ls);
1139
  new_slot->name = new_name;
1140
  *hash_slot = new_slot;
1141
  return 1;
1142
}
1143
 
1144
/* Read declarations and other initializations for a FILE_DATA. */
1145
 
1146
static void
1147
lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
1148
{
1149
  const char *data;
1150
  size_t len;
1151
 
1152
  file_data->renaming_hash_table = lto_create_renaming_table ();
1153
  file_data->file_name = file->filename;
1154
  data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
1155
  if (data == NULL)
1156
    {
1157
      internal_error ("cannot read LTO decls from %s", file_data->file_name);
1158
      return;
1159
    }
1160
  lto_read_decls (file_data, data, file_data->resolutions);
1161
  lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
1162
}
1163
 
1164
/* Finalize FILE_DATA in FILE and increase COUNT. */
1165
 
1166
static int
1167
lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
1168
                           int *count)
1169
{
1170
  lto_file_finalize (file_data, file);
1171
  if (cgraph_dump_file)
1172
    fprintf (cgraph_dump_file, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
1173
             file_data->file_name, file_data->id);
1174
  (*count)++;
1175
  return 0;
1176
}
1177
 
1178
/* Generate a TREE representation for all types and external decls
1179
   entities in FILE.
1180
 
1181
   Read all of the globals out of the file.  Then read the cgraph
1182
   and process the .o index into the cgraph nodes so that it can open
1183
   the .o file to load the functions and ipa information.   */
1184
 
1185
static struct lto_file_decl_data *
1186
lto_file_read (lto_file *file, FILE *resolution_file, int *count)
1187
{
1188
  struct lto_file_decl_data *file_data = NULL;
1189
  splay_tree file_ids;
1190
  htab_t section_hash_table;
1191
  struct lto_section_slot *section;
1192
  struct file_data_list file_list;
1193
  struct lto_section_list section_list;
1194
 
1195
  memset (&section_list, 0, sizeof (struct lto_section_list));
1196
  section_hash_table = lto_obj_build_section_table (file, &section_list);
1197
 
1198
  /* Find all sub modules in the object and put their sections into new hash
1199
     tables in a splay tree. */
1200
  file_ids = lto_splay_tree_new ();
1201
  memset (&file_list, 0, sizeof (struct file_data_list));
1202
  for (section = section_list.first; section != NULL; section = section->next)
1203
    create_subid_section_table (section, file_ids, &file_list);
1204
 
1205
  /* Add resolutions to file ids */
1206
  lto_resolution_read (file_ids, resolution_file, file);
1207
 
1208
  /* Finalize each lto file for each submodule in the merged object */
1209
  for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
1210
    lto_create_files_from_ids (file, file_data, count);
1211
 
1212
  splay_tree_delete (file_ids);
1213
  htab_delete (section_hash_table);
1214
 
1215
  return file_list.first;
1216
}
1217
 
1218
#if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
1219
#define LTO_MMAP_IO 1
1220
#endif
1221
 
1222
#if LTO_MMAP_IO
1223
/* Page size of machine is used for mmap and munmap calls.  */
1224
static size_t page_mask;
1225
#endif
1226
 
1227
/* Get the section data of length LEN from FILENAME starting at
1228
   OFFSET.  The data segment must be freed by the caller when the
1229
   caller is finished.  Returns NULL if all was not well.  */
1230
 
1231
static char *
1232
lto_read_section_data (struct lto_file_decl_data *file_data,
1233
                       intptr_t offset, size_t len)
1234
{
1235
  char *result;
1236
  static int fd = -1;
1237
  static char *fd_name;
1238
#if LTO_MMAP_IO
1239
  intptr_t computed_len;
1240
  intptr_t computed_offset;
1241
  intptr_t diff;
1242
#endif
1243
 
1244
  /* Keep a single-entry file-descriptor cache.  The last file we
1245
     touched will get closed at exit.
1246
     ???  Eventually we want to add a more sophisticated larger cache
1247
     or rather fix function body streaming to not stream them in
1248
     practically random order.  */
1249
  if (fd != -1
1250
      && filename_cmp (fd_name, file_data->file_name) != 0)
1251
    {
1252
      free (fd_name);
1253
      close (fd);
1254
      fd = -1;
1255
    }
1256
  if (fd == -1)
1257
    {
1258
      fd = open (file_data->file_name, O_RDONLY|O_BINARY);
1259
      if (fd == -1)
1260
        {
1261
          fatal_error ("Cannot open %s", file_data->file_name);
1262
          return NULL;
1263
        }
1264
      fd_name = xstrdup (file_data->file_name);
1265
    }
1266
 
1267
#if LTO_MMAP_IO
1268
  if (!page_mask)
1269
    {
1270
      size_t page_size = sysconf (_SC_PAGE_SIZE);
1271
      page_mask = ~(page_size - 1);
1272
    }
1273
 
1274
  computed_offset = offset & page_mask;
1275
  diff = offset - computed_offset;
1276
  computed_len = len + diff;
1277
 
1278
  result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
1279
                          fd, computed_offset);
1280
  if (result == MAP_FAILED)
1281
    {
1282
      fatal_error ("Cannot map %s", file_data->file_name);
1283
      return NULL;
1284
    }
1285
 
1286
  return result + diff;
1287
#else
1288
  result = (char *) xmalloc (len);
1289
  if (lseek (fd, offset, SEEK_SET) != offset
1290
      || read (fd, result, len) != (ssize_t) len)
1291
    {
1292
      free (result);
1293
      fatal_error ("Cannot read %s", file_data->file_name);
1294
      result = NULL;
1295
    }
1296
#ifdef __MINGW32__
1297
  /* Native windows doesn't supports delayed unlink on opened file. So
1298
     we close file here again. This produces higher I/O load, but at least
1299
     it prevents to have dangling file handles preventing unlink.  */
1300
  free (fd_name);
1301
  fd_name = NULL;
1302
  close (fd);
1303
  fd = -1;
1304
#endif
1305
  return result;
1306
#endif
1307
}
1308
 
1309
 
1310
/* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1311
   NAME will be NULL unless the section type is for a function
1312
   body.  */
1313
 
1314
static const char *
1315
get_section_data (struct lto_file_decl_data *file_data,
1316
                      enum lto_section_type section_type,
1317
                      const char *name,
1318
                      size_t *len)
1319
{
1320
  htab_t section_hash_table = file_data->section_hash_table;
1321
  struct lto_section_slot *f_slot;
1322
  struct lto_section_slot s_slot;
1323
  const char *section_name = lto_get_section_name (section_type, name, file_data);
1324
  char *data = NULL;
1325
 
1326
  *len = 0;
1327
  s_slot.name = section_name;
1328
  f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
1329
  if (f_slot)
1330
    {
1331
      data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
1332
      *len = f_slot->len;
1333
    }
1334
 
1335
  free (CONST_CAST (char *, section_name));
1336
  return data;
1337
}
1338
 
1339
 
1340
/* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1341
   starts at OFFSET and has LEN bytes.  */
1342
 
1343
static void
1344
free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
1345
                   enum lto_section_type section_type ATTRIBUTE_UNUSED,
1346
                   const char *name ATTRIBUTE_UNUSED,
1347
                   const char *offset, size_t len ATTRIBUTE_UNUSED)
1348
{
1349
#if LTO_MMAP_IO
1350
  intptr_t computed_len;
1351
  intptr_t computed_offset;
1352
  intptr_t diff;
1353
#endif
1354
 
1355
#if LTO_MMAP_IO
1356
  computed_offset = ((intptr_t) offset) & page_mask;
1357
  diff = (intptr_t) offset - computed_offset;
1358
  computed_len = len + diff;
1359
 
1360
  munmap ((caddr_t) computed_offset, computed_len);
1361
#else
1362
  free (CONST_CAST(char *, offset));
1363
#endif
1364
}
1365
 
1366
/* Structure describing ltrans partitions.  */
1367
 
1368
struct ltrans_partition_def
1369
{
1370
  cgraph_node_set cgraph_set;
1371
  varpool_node_set varpool_set;
1372
  const char * name;
1373
  int insns;
1374
};
1375
 
1376
typedef struct ltrans_partition_def *ltrans_partition;
1377
DEF_VEC_P(ltrans_partition);
1378
DEF_VEC_ALLOC_P(ltrans_partition,heap);
1379
 
1380
static VEC(ltrans_partition, heap) *ltrans_partitions;
1381
 
1382
static void add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node);
1383
static void add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode);
1384
 
1385
/* Create new partition with name NAME.  */
1386
static ltrans_partition
1387
new_partition (const char *name)
1388
{
1389
  ltrans_partition part = XCNEW (struct ltrans_partition_def);
1390
  part->cgraph_set = cgraph_node_set_new ();
1391
  part->varpool_set = varpool_node_set_new ();
1392
  part->name = name;
1393
  part->insns = 0;
1394
  VEC_safe_push (ltrans_partition, heap, ltrans_partitions, part);
1395
  return part;
1396
}
1397
 
1398
/* Free memory used by ltrans datastructures.  */
1399
static void
1400
free_ltrans_partitions (void)
1401
{
1402
  unsigned int idx;
1403
  ltrans_partition part;
1404
  for (idx = 0; VEC_iterate (ltrans_partition, ltrans_partitions, idx, part); idx++)
1405
    {
1406
      free_cgraph_node_set (part->cgraph_set);
1407
      free (part);
1408
    }
1409
  VEC_free (ltrans_partition, heap, ltrans_partitions);
1410
}
1411
 
1412
/* See all references that go to comdat objects and bring them into partition too.  */
1413
static void
1414
add_references_to_partition (ltrans_partition part, struct ipa_ref_list *refs)
1415
{
1416
  int i;
1417
  struct ipa_ref *ref;
1418
  for (i = 0; ipa_ref_list_reference_iterate (refs, i, ref); i++)
1419
    {
1420
      if (ref->refered_type == IPA_REF_CGRAPH
1421
          && DECL_COMDAT (cgraph_function_node (ipa_ref_node (ref), NULL)->decl)
1422
          && !cgraph_node_in_set_p (ipa_ref_node (ref), part->cgraph_set))
1423
        add_cgraph_node_to_partition (part, ipa_ref_node (ref));
1424
      else
1425
        if (ref->refered_type == IPA_REF_VARPOOL
1426
            && DECL_COMDAT (ipa_ref_varpool_node (ref)->decl)
1427
            && !varpool_node_in_set_p (ipa_ref_varpool_node (ref), part->varpool_set))
1428
          add_varpool_node_to_partition (part, ipa_ref_varpool_node (ref));
1429
    }
1430
}
1431
 
1432
/* Worker for add_cgraph_node_to_partition.  */
1433
 
1434
static bool
1435
add_cgraph_node_to_partition_1 (struct cgraph_node *node, void *data)
1436
{
1437
  ltrans_partition part = (ltrans_partition) data;
1438
 
1439
  /* non-COMDAT aliases of COMDAT functions needs to be output just once.  */
1440
  if (!DECL_COMDAT (node->decl)
1441
      && !node->global.inlined_to
1442
      && node->aux)
1443
    {
1444
      gcc_assert (node->thunk.thunk_p || node->alias);
1445
      return false;
1446
    }
1447
 
1448
  if (node->aux)
1449
    {
1450
      node->in_other_partition = 1;
1451
      if (cgraph_dump_file)
1452
        fprintf (cgraph_dump_file, "Node %s/%i now used in multiple partitions\n",
1453
                 cgraph_node_name (node), node->uid);
1454
    }
1455
  node->aux = (void *)((size_t)node->aux + 1);
1456
  cgraph_node_set_add (part->cgraph_set, node);
1457
  return false;
1458
}
1459
 
1460
/* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */
1461
 
1462
static void
1463
add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node)
1464
{
1465
  struct cgraph_edge *e;
1466
  cgraph_node_set_iterator csi;
1467
  struct cgraph_node *n;
1468
 
1469
  /* We always decide on functions, not associated thunks and aliases.  */
1470
  node = cgraph_function_node (node, NULL);
1471
 
1472
  /* If NODE is already there, we have nothing to do.  */
1473
  csi = cgraph_node_set_find (part->cgraph_set, node);
1474
  if (!csi_end_p (csi))
1475
    return;
1476
 
1477
  cgraph_for_node_thunks_and_aliases (node, add_cgraph_node_to_partition_1, part, true);
1478
 
1479
  part->insns += inline_summary (node)->self_size;
1480
 
1481
 
1482
  cgraph_node_set_add (part->cgraph_set, node);
1483
 
1484
  for (e = node->callees; e; e = e->next_callee)
1485
    if ((!e->inline_failed
1486
         || DECL_COMDAT (cgraph_function_node (e->callee, NULL)->decl))
1487
        && !cgraph_node_in_set_p (e->callee, part->cgraph_set))
1488
      add_cgraph_node_to_partition (part, e->callee);
1489
 
1490
  add_references_to_partition (part, &node->ref_list);
1491
 
1492
  if (node->same_comdat_group)
1493
    for (n = node->same_comdat_group; n != node; n = n->same_comdat_group)
1494
      add_cgraph_node_to_partition (part, n);
1495
}
1496
 
1497
/* Add VNODE to partition as well as comdat references partition PART. */
1498
 
1499
static void
1500
add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode)
1501
{
1502
  varpool_node_set_iterator vsi;
1503
 
1504
  vnode = varpool_variable_node (vnode, NULL);
1505
 
1506
  /* If NODE is already there, we have nothing to do.  */
1507
  vsi = varpool_node_set_find (part->varpool_set, vnode);
1508
  if (!vsi_end_p (vsi))
1509
    return;
1510
 
1511
  varpool_node_set_add (part->varpool_set, vnode);
1512
 
1513
  if (vnode->aux)
1514
    {
1515
      vnode->in_other_partition = 1;
1516
      if (cgraph_dump_file)
1517
        fprintf (cgraph_dump_file, "Varpool node %s now used in multiple partitions\n",
1518
                 varpool_node_name (vnode));
1519
    }
1520
  vnode->aux = (void *)((size_t)vnode->aux + 1);
1521
 
1522
  add_references_to_partition (part, &vnode->ref_list);
1523
 
1524
  if (vnode->same_comdat_group
1525
      && !varpool_node_in_set_p (vnode->same_comdat_group, part->varpool_set))
1526
    add_varpool_node_to_partition (part, vnode->same_comdat_group);
1527
}
1528
 
1529
/* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
1530
   and number of varpool nodes is N_VARPOOL_NODES.  */
1531
 
1532
static void
1533
undo_partition (ltrans_partition partition, unsigned int n_cgraph_nodes,
1534
                unsigned int n_varpool_nodes)
1535
{
1536
  while (VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes) >
1537
         n_cgraph_nodes)
1538
    {
1539
      struct cgraph_node *node = VEC_index (cgraph_node_ptr,
1540
                                            partition->cgraph_set->nodes,
1541
                                            n_cgraph_nodes);
1542
      partition->insns -= inline_summary (node)->self_size;
1543
      cgraph_node_set_remove (partition->cgraph_set, node);
1544
      node->aux = (void *)((size_t)node->aux - 1);
1545
    }
1546
  while (VEC_length (varpool_node_ptr, partition->varpool_set->nodes) >
1547
         n_varpool_nodes)
1548
    {
1549
      struct varpool_node *node = VEC_index (varpool_node_ptr,
1550
                                             partition->varpool_set->nodes,
1551
                                             n_varpool_nodes);
1552
      varpool_node_set_remove (partition->varpool_set, node);
1553
      node->aux = (void *)((size_t)node->aux - 1);
1554
    }
1555
}
1556
 
1557
/* Return true if NODE should be partitioned.
1558
   This means that partitioning algorithm should put NODE into one of partitions.
1559
   This apply to most functions with bodies.  Functions that are not partitions
1560
   are put into every unit needing them.  This is the case of i.e. COMDATs.  */
1561
 
1562
static bool
1563
partition_cgraph_node_p (struct cgraph_node *node)
1564
{
1565
  /* We will get proper partition based on function they are inlined to.  */
1566
  if (node->global.inlined_to)
1567
    return false;
1568
  /* Nodes without a body do not need partitioning.  */
1569
  if (!node->analyzed)
1570
    return false;
1571
  /* Extern inlines and comdat are always only in partitions they are needed.  */
1572
  if (DECL_EXTERNAL (node->decl)
1573
      || (DECL_COMDAT (node->decl)
1574
          && !cgraph_used_from_object_file_p (node)))
1575
    return false;
1576
  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
1577
    return false;
1578
  return true;
1579
}
1580
 
1581
/* Return true if VNODE should be partitioned.
1582
   This means that partitioning algorithm should put VNODE into one of partitions. */
1583
 
1584
static bool
1585
partition_varpool_node_p (struct varpool_node *vnode)
1586
{
1587
  if (vnode->alias || !vnode->needed)
1588
    return false;
1589
  /* Constant pool and comdat are always only in partitions they are needed.  */
1590
  if (DECL_IN_CONSTANT_POOL (vnode->decl)
1591
      || (DECL_COMDAT (vnode->decl)
1592
          && !vnode->force_output
1593
          && !varpool_used_from_object_file_p (vnode)))
1594
    return false;
1595
  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->decl)))
1596
    return false;
1597
  return true;
1598
}
1599
 
1600
/* Group cgrah nodes by input files.  This is used mainly for testing
1601
   right now.  */
1602
 
1603
static void
1604
lto_1_to_1_map (void)
1605
{
1606
  struct cgraph_node *node;
1607
  struct varpool_node *vnode;
1608
  struct lto_file_decl_data *file_data;
1609
  struct pointer_map_t *pmap;
1610
  ltrans_partition partition;
1611
  void **slot;
1612
  int npartitions = 0;
1613
 
1614
  timevar_push (TV_WHOPR_WPA);
1615
 
1616
  pmap = pointer_map_create ();
1617
 
1618
  for (node = cgraph_nodes; node; node = node->next)
1619
    {
1620
      if (!partition_cgraph_node_p (node)
1621
          || node->aux)
1622
        continue;
1623
 
1624
      file_data = node->local.lto_file_data;
1625
 
1626
      if (file_data)
1627
        {
1628
          slot = pointer_map_contains (pmap, file_data);
1629
          if (slot)
1630
            partition = (ltrans_partition) *slot;
1631
          else
1632
            {
1633
              partition = new_partition (file_data->file_name);
1634
              slot = pointer_map_insert (pmap, file_data);
1635
              *slot = partition;
1636
              npartitions++;
1637
            }
1638
        }
1639
      else if (!file_data
1640
               && VEC_length (ltrans_partition, ltrans_partitions))
1641
        partition = VEC_index (ltrans_partition, ltrans_partitions, 0);
1642
      else
1643
        {
1644
          partition = new_partition ("");
1645
          slot = pointer_map_insert (pmap, NULL);
1646
          *slot = partition;
1647
          npartitions++;
1648
        }
1649
 
1650
      add_cgraph_node_to_partition (partition, node);
1651
    }
1652
 
1653
  for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1654
    {
1655
      if (!partition_varpool_node_p (vnode)
1656
          || vnode->aux)
1657
        continue;
1658
      file_data = vnode->lto_file_data;
1659
      slot = pointer_map_contains (pmap, file_data);
1660
      if (slot)
1661
        partition = (ltrans_partition) *slot;
1662
      else
1663
        {
1664
          partition = new_partition (file_data->file_name);
1665
          slot = pointer_map_insert (pmap, file_data);
1666
          *slot = partition;
1667
          npartitions++;
1668
        }
1669
 
1670
      add_varpool_node_to_partition (partition, vnode);
1671
    }
1672
  for (node = cgraph_nodes; node; node = node->next)
1673
    node->aux = NULL;
1674
  for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1675
    vnode->aux = NULL;
1676
 
1677
  /* If the cgraph is empty, create one cgraph node set so that there is still
1678
     an output file for any variables that need to be exported in a DSO.  */
1679
  if (!npartitions)
1680
    new_partition ("empty");
1681
 
1682
  pointer_map_destroy (pmap);
1683
 
1684
  timevar_pop (TV_WHOPR_WPA);
1685
 
1686
  lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
1687
                                                 ltrans_partitions);
1688
}
1689
 
1690
/* Helper function for qsort; sort nodes by order.  */
1691
static int
1692
node_cmp (const void *pa, const void *pb)
1693
{
1694
  const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
1695
  const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
1696
  return b->order - a->order;
1697
}
1698
 
1699
/* Helper function for qsort; sort nodes by order.  */
1700
static int
1701
varpool_node_cmp (const void *pa, const void *pb)
1702
{
1703
  const struct varpool_node *a = *(const struct varpool_node * const *) pa;
1704
  const struct varpool_node *b = *(const struct varpool_node * const *) pb;
1705
  return b->order - a->order;
1706
}
1707
 
1708
/* Group cgraph nodes into equally-sized partitions.
1709
 
1710
   The partitioning algorithm is simple: nodes are taken in predefined order.
1711
   The order corresponds to the order we want functions to have in the final
1712
   output.  In the future this will be given by function reordering pass, but
1713
   at the moment we use the topological order, which is a good approximation.
1714
 
1715
   The goal is to partition this linear order into intervals (partitions) so
1716
   that all the partitions have approximately the same size and the number of
1717
   callgraph or IPA reference edges crossing boundaries is minimal.
1718
 
1719
   This is a lot faster (O(n) in size of callgraph) than algorithms doing
1720
   priority-based graph clustering that are generally O(n^2) and, since
1721
   WHOPR is designed to make things go well across partitions, it leads
1722
   to good results.
1723
 
1724
   We compute the expected size of a partition as:
1725
 
1726
     max (total_size / lto_partitions, min_partition_size)
1727
 
1728
   We use dynamic expected size of partition so small programs are partitioned
1729
   into enough partitions to allow use of multiple CPUs, while large programs
1730
   are not partitioned too much.  Creating too many partitions significantly
1731
   increases the streaming overhead.
1732
 
1733
   In the future, we would like to bound the maximal size of partitions so as
1734
   to prevent the LTRANS stage from consuming too much memory.  At the moment,
1735
   however, the WPA stage is the most memory intensive for large benchmarks,
1736
   since too many types and declarations are read into memory.
1737
 
1738
   The function implements a simple greedy algorithm.  Nodes are being added
1739
   to the current partition until after 3/4 of the expected partition size is
1740
   reached.  Past this threshold, we keep track of boundary size (number of
1741
   edges going to other partitions) and continue adding functions until after
1742
   the current partition has grown to twice the expected partition size.  Then
1743
   the process is undone to the point where the minimal ratio of boundary size
1744
   and in-partition calls was reached.  */
1745
 
1746
static void
1747
lto_balanced_map (void)
1748
{
1749
  int n_nodes = 0;
1750
  int n_varpool_nodes = 0, varpool_pos = 0;
1751
  struct cgraph_node **postorder =
1752
    XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1753
  struct cgraph_node **order = XNEWVEC (struct cgraph_node *, cgraph_max_uid);
1754
  struct varpool_node **varpool_order = NULL;
1755
  int i, postorder_len;
1756
  struct cgraph_node *node;
1757
  int total_size = 0, best_total_size = 0;
1758
  int partition_size;
1759
  ltrans_partition partition;
1760
  unsigned int last_visited_cgraph_node = 0, last_visited_varpool_node = 0;
1761
  struct varpool_node *vnode;
1762
  int cost = 0, internal = 0;
1763
  int best_n_nodes = 0, best_n_varpool_nodes = 0, best_i = 0, best_cost =
1764
    INT_MAX, best_internal = 0;
1765
  int npartitions;
1766
  int current_order = -1;
1767
 
1768
  for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1769
    gcc_assert (!vnode->aux);
1770
  /* Until we have better ordering facility, use toplogical order.
1771
     Include only nodes we will partition and compute estimate of program
1772
     size.  Note that since nodes that are not partitioned might be put into
1773
     multiple partitions, this is just an estimate of real size.  This is why
1774
     we keep partition_size updated after every partition is finalized.  */
1775
  postorder_len = ipa_reverse_postorder (postorder);
1776
 
1777
  for (i = 0; i < postorder_len; i++)
1778
    {
1779
      node = postorder[i];
1780
      if (partition_cgraph_node_p (node))
1781
        {
1782
          order[n_nodes++] = node;
1783
          total_size += inline_summary (node)->size;
1784
        }
1785
    }
1786
  free (postorder);
1787
 
1788
  if (!flag_toplevel_reorder)
1789
    {
1790
      qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
1791
 
1792
      for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1793
        if (partition_varpool_node_p (vnode))
1794
          n_varpool_nodes++;
1795
      varpool_order = XNEWVEC (struct varpool_node *, n_varpool_nodes);
1796
 
1797
      n_varpool_nodes = 0;
1798
      for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1799
        if (partition_varpool_node_p (vnode))
1800
          varpool_order[n_varpool_nodes++] = vnode;
1801
      qsort (varpool_order, n_varpool_nodes, sizeof (struct varpool_node *),
1802
             varpool_node_cmp);
1803
    }
1804
 
1805
  /* Compute partition size and create the first partition.  */
1806
  partition_size = total_size / PARAM_VALUE (PARAM_LTO_PARTITIONS);
1807
  if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1808
    partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1809
  npartitions = 1;
1810
  partition = new_partition ("");
1811
  if (cgraph_dump_file)
1812
    fprintf (cgraph_dump_file, "Total unit size: %i, partition size: %i\n",
1813
             total_size, partition_size);
1814
 
1815
  for (i = 0; i < n_nodes; i++)
1816
    {
1817
      if (order[i]->aux)
1818
        continue;
1819
 
1820
      current_order = order[i]->order;
1821
 
1822
      if (!flag_toplevel_reorder)
1823
        while (varpool_pos < n_varpool_nodes && varpool_order[varpool_pos]->order < current_order)
1824
          {
1825
            if (!varpool_order[varpool_pos]->aux)
1826
              add_varpool_node_to_partition (partition, varpool_order[varpool_pos]);
1827
            varpool_pos++;
1828
          }
1829
 
1830
      add_cgraph_node_to_partition (partition, order[i]);
1831
      total_size -= inline_summary (order[i])->size;
1832
 
1833
 
1834
      /* Once we added a new node to the partition, we also want to add
1835
         all referenced variables unless they was already added into some
1836
         earlier partition.
1837
         add_cgraph_node_to_partition adds possibly multiple nodes and
1838
         variables that are needed to satisfy needs of ORDER[i].
1839
         We remember last visited cgraph and varpool node from last iteration
1840
         of outer loop that allows us to process every new addition.
1841
 
1842
         At the same time we compute size of the boundary into COST.  Every
1843
         callgraph or IPA reference edge leaving the partition contributes into
1844
         COST.  Every edge inside partition was earlier computed as one leaving
1845
         it and thus we need to subtract it from COST.  */
1846
      while (last_visited_cgraph_node <
1847
             VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes)
1848
             || last_visited_varpool_node < VEC_length (varpool_node_ptr,
1849
                                                        partition->varpool_set->
1850
                                                        nodes))
1851
        {
1852
          struct ipa_ref_list *refs;
1853
          int j;
1854
          struct ipa_ref *ref;
1855
          bool cgraph_p = false;
1856
 
1857
          if (last_visited_cgraph_node <
1858
              VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes))
1859
            {
1860
              struct cgraph_edge *edge;
1861
 
1862
              cgraph_p = true;
1863
              node = VEC_index (cgraph_node_ptr, partition->cgraph_set->nodes,
1864
                                last_visited_cgraph_node);
1865
              refs = &node->ref_list;
1866
 
1867
              last_visited_cgraph_node++;
1868
 
1869
              gcc_assert (node->analyzed);
1870
 
1871
              /* Compute boundary cost of callgraph edges.  */
1872
              for (edge = node->callees; edge; edge = edge->next_callee)
1873
                if (edge->callee->analyzed)
1874
                  {
1875
                    int edge_cost = edge->frequency;
1876
                    cgraph_node_set_iterator csi;
1877
 
1878
                    if (!edge_cost)
1879
                      edge_cost = 1;
1880
                    gcc_assert (edge_cost > 0);
1881
                    csi = cgraph_node_set_find (partition->cgraph_set, edge->callee);
1882
                    if (!csi_end_p (csi)
1883
                        && csi.index < last_visited_cgraph_node - 1)
1884
                      cost -= edge_cost, internal+= edge_cost;
1885
                    else
1886
                      cost += edge_cost;
1887
                  }
1888
              for (edge = node->callers; edge; edge = edge->next_caller)
1889
                {
1890
                  int edge_cost = edge->frequency;
1891
                  cgraph_node_set_iterator csi;
1892
 
1893
                  gcc_assert (edge->caller->analyzed);
1894
                  if (!edge_cost)
1895
                    edge_cost = 1;
1896
                  gcc_assert (edge_cost > 0);
1897
                  csi = cgraph_node_set_find (partition->cgraph_set, edge->caller);
1898
                  if (!csi_end_p (csi)
1899
                      && csi.index < last_visited_cgraph_node)
1900
                    cost -= edge_cost;
1901
                  else
1902
                    cost += edge_cost;
1903
                }
1904
            }
1905
          else
1906
            {
1907
              refs =
1908
                &VEC_index (varpool_node_ptr, partition->varpool_set->nodes,
1909
                            last_visited_varpool_node)->ref_list;
1910
              last_visited_varpool_node++;
1911
            }
1912
 
1913
          /* Compute boundary cost of IPA REF edges and at the same time look into
1914
             variables referenced from current partition and try to add them.  */
1915
          for (j = 0; ipa_ref_list_reference_iterate (refs, j, ref); j++)
1916
            if (ref->refered_type == IPA_REF_VARPOOL)
1917
              {
1918
                varpool_node_set_iterator vsi;
1919
 
1920
                vnode = ipa_ref_varpool_node (ref);
1921
                if (!vnode->finalized)
1922
                  continue;
1923
                if (!vnode->aux && flag_toplevel_reorder
1924
                    && partition_varpool_node_p (vnode))
1925
                  add_varpool_node_to_partition (partition, vnode);
1926
                vsi = varpool_node_set_find (partition->varpool_set, vnode);
1927
                if (!vsi_end_p (vsi)
1928
                    && vsi.index < last_visited_varpool_node - !cgraph_p)
1929
                  cost--, internal++;
1930
                else
1931
                  cost++;
1932
              }
1933
            else
1934
              {
1935
                cgraph_node_set_iterator csi;
1936
 
1937
                node = ipa_ref_node (ref);
1938
                if (!node->analyzed)
1939
                  continue;
1940
                csi = cgraph_node_set_find (partition->cgraph_set, node);
1941
                if (!csi_end_p (csi)
1942
                    && csi.index < last_visited_cgraph_node - cgraph_p)
1943
                  cost--, internal++;
1944
                else
1945
                  cost++;
1946
              }
1947
          for (j = 0; ipa_ref_list_refering_iterate (refs, j, ref); j++)
1948
            if (ref->refering_type == IPA_REF_VARPOOL)
1949
              {
1950
                varpool_node_set_iterator vsi;
1951
 
1952
                vnode = ipa_ref_refering_varpool_node (ref);
1953
                gcc_assert (vnode->finalized);
1954
                if (!vnode->aux && flag_toplevel_reorder
1955
                    && partition_varpool_node_p (vnode))
1956
                  add_varpool_node_to_partition (partition, vnode);
1957
                vsi = varpool_node_set_find (partition->varpool_set, vnode);
1958
                if (!vsi_end_p (vsi)
1959
                    && vsi.index < last_visited_varpool_node)
1960
                  cost--;
1961
                else
1962
                  cost++;
1963
              }
1964
            else
1965
              {
1966
                cgraph_node_set_iterator csi;
1967
 
1968
                node = ipa_ref_refering_node (ref);
1969
                gcc_assert (node->analyzed);
1970
                csi = cgraph_node_set_find (partition->cgraph_set, node);
1971
                if (!csi_end_p (csi)
1972
                    && csi.index < last_visited_cgraph_node)
1973
                  cost--;
1974
                else
1975
                  cost++;
1976
              }
1977
        }
1978
 
1979
      /* If the partition is large enough, start looking for smallest boundary cost.  */
1980
      if (partition->insns < partition_size * 3 / 4
1981
          || best_cost == INT_MAX
1982
          || ((!cost
1983
               || (best_internal * (HOST_WIDE_INT) cost
1984
                   > (internal * (HOST_WIDE_INT)best_cost)))
1985
              && partition->insns < partition_size * 5 / 4))
1986
        {
1987
          best_cost = cost;
1988
          best_internal = internal;
1989
          best_i = i;
1990
          best_n_nodes = VEC_length (cgraph_node_ptr,
1991
                                     partition->cgraph_set->nodes);
1992
          best_n_varpool_nodes = VEC_length (varpool_node_ptr,
1993
                                             partition->varpool_set->nodes);
1994
          best_total_size = total_size;
1995
        }
1996
      if (cgraph_dump_file)
1997
        fprintf (cgraph_dump_file, "Step %i: added %s/%i, size %i, cost %i/%i best %i/%i, step %i\n", i,
1998
                 cgraph_node_name (order[i]), order[i]->uid, partition->insns, cost, internal,
1999
                 best_cost, best_internal, best_i);
2000
      /* Partition is too large, unwind into step when best cost was reached and
2001
         start new partition.  */
2002
      if (partition->insns > 2 * partition_size)
2003
        {
2004
          if (best_i != i)
2005
            {
2006
              if (cgraph_dump_file)
2007
                fprintf (cgraph_dump_file, "Unwinding %i insertions to step %i\n",
2008
                         i - best_i, best_i);
2009
              undo_partition (partition, best_n_nodes, best_n_varpool_nodes);
2010
            }
2011
          i = best_i;
2012
          /* When we are finished, avoid creating empty partition.  */
2013
          while (i < n_nodes - 1 && order[i + 1]->aux)
2014
            i++;
2015
          if (i == n_nodes - 1)
2016
            break;
2017
          partition = new_partition ("");
2018
          last_visited_cgraph_node = 0;
2019
          last_visited_varpool_node = 0;
2020
          total_size = best_total_size;
2021
          cost = 0;
2022
 
2023
          if (cgraph_dump_file)
2024
            fprintf (cgraph_dump_file, "New partition\n");
2025
          best_n_nodes = 0;
2026
          best_n_varpool_nodes = 0;
2027
          best_cost = INT_MAX;
2028
 
2029
          /* Since the size of partitions is just approximate, update the size after
2030
             we finished current one.  */
2031
          if (npartitions < PARAM_VALUE (PARAM_LTO_PARTITIONS))
2032
            partition_size = total_size
2033
              / (PARAM_VALUE (PARAM_LTO_PARTITIONS) - npartitions);
2034
          else
2035
            partition_size = INT_MAX;
2036
 
2037
          if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
2038
            partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
2039
          npartitions ++;
2040
        }
2041
    }
2042
 
2043
  /* Varables that are not reachable from the code go into last partition.  */
2044
  if (flag_toplevel_reorder)
2045
    {
2046
      for (vnode = varpool_nodes; vnode; vnode = vnode->next)
2047
        if (partition_varpool_node_p (vnode) && !vnode->aux)
2048
          add_varpool_node_to_partition (partition, vnode);
2049
    }
2050
  else
2051
    {
2052
      while (varpool_pos < n_varpool_nodes)
2053
        {
2054
          if (!varpool_order[varpool_pos]->aux)
2055
            add_varpool_node_to_partition (partition, varpool_order[varpool_pos]);
2056
          varpool_pos++;
2057
        }
2058
      free (varpool_order);
2059
    }
2060
  free (order);
2061
}
2062
 
2063
/* Promote variable VNODE to be static.  */
2064
 
2065
static bool
2066
promote_var (struct varpool_node *vnode)
2067
{
2068
  if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
2069
    return false;
2070
  gcc_assert (flag_wpa);
2071
  TREE_PUBLIC (vnode->decl) = 1;
2072
  DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
2073
  DECL_VISIBILITY_SPECIFIED (vnode->decl) = true;
2074
  if (cgraph_dump_file)
2075
    fprintf (cgraph_dump_file,
2076
            "Promoting var as hidden: %s\n", varpool_node_name (vnode));
2077
  return true;
2078
}
2079
 
2080
/* Promote function NODE to be static.  */
2081
 
2082
static bool
2083
promote_fn (struct cgraph_node *node)
2084
{
2085
  gcc_assert (flag_wpa);
2086
  if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2087
    return false;
2088
  TREE_PUBLIC (node->decl) = 1;
2089
  DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
2090
  DECL_VISIBILITY_SPECIFIED (node->decl) = true;
2091
  if (cgraph_dump_file)
2092
    fprintf (cgraph_dump_file,
2093
             "Promoting function as hidden: %s/%i\n",
2094
             cgraph_node_name (node), node->uid);
2095
  return true;
2096
}
2097
 
2098
/* Find out all static decls that need to be promoted to global because
2099
   of cross file sharing.  This function must be run in the WPA mode after
2100
   all inlinees are added.  */
2101
 
2102
static void
2103
lto_promote_cross_file_statics (void)
2104
{
2105
  struct varpool_node *vnode;
2106
  unsigned i, n_sets;
2107
  cgraph_node_set set;
2108
  varpool_node_set vset;
2109
  cgraph_node_set_iterator csi;
2110
  varpool_node_set_iterator vsi;
2111
  VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
2112
  struct pointer_set_t *inserted = pointer_set_create ();
2113
 
2114
  gcc_assert (flag_wpa);
2115
 
2116
  n_sets = VEC_length (ltrans_partition, ltrans_partitions);
2117
  for (i = 0; i < n_sets; i++)
2118
    {
2119
      ltrans_partition part
2120
        = VEC_index (ltrans_partition, ltrans_partitions, i);
2121
      set = part->cgraph_set;
2122
      vset = part->varpool_set;
2123
 
2124
      /* If node called or referred to from other partition, it needs to be
2125
         globalized.  */
2126
      for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2127
        {
2128
          struct cgraph_node *node = csi_node (csi);
2129
          if (node->local.externally_visible)
2130
            continue;
2131
          if (node->global.inlined_to)
2132
            continue;
2133
          if ((!DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
2134
              && (referenced_from_other_partition_p (&node->ref_list, set, vset)
2135
                  || reachable_from_other_partition_p (node, set)))
2136
            promote_fn (node);
2137
        }
2138
      for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
2139
        {
2140
          vnode = vsi_node (vsi);
2141
          /* Constant pool references use internal labels and thus can not
2142
             be made global.  It is sensible to keep those ltrans local to
2143
             allow better optimization.  */
2144
          if (!DECL_IN_CONSTANT_POOL (vnode->decl) && !DECL_COMDAT (vnode->decl)
2145
              && !vnode->externally_visible && vnode->analyzed
2146
              && referenced_from_other_partition_p (&vnode->ref_list,
2147
                                                    set, vset))
2148
            promote_var (vnode);
2149
        }
2150
 
2151
      /* We export the initializer of a read-only var into each partition
2152
         referencing the var.  Folding might take declarations from the
2153
         initializer and use them, so everything referenced from the
2154
         initializer can be accessed from this partition after folding.
2155
 
2156
         This means that we need to promote all variables and functions
2157
         referenced from all initializers of read-only vars referenced
2158
         from this partition that are not in this partition.  This needs
2159
         to be done recursively.  */
2160
      for (vnode = varpool_nodes; vnode; vnode = vnode->next)
2161
        if (const_value_known_p (vnode->decl)
2162
            && DECL_INITIAL (vnode->decl)
2163
            && !varpool_node_in_set_p (vnode, vset)
2164
            && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
2165
            && !pointer_set_insert (inserted, vnode))
2166
        VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
2167
 
2168
      while (!VEC_empty (varpool_node_ptr, promoted_initializers))
2169
        {
2170
          int i;
2171
          struct ipa_ref *ref;
2172
 
2173
          vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
2174
          for (i = 0;
2175
               ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref);
2176
               i++)
2177
            {
2178
              if (ref->refered_type == IPA_REF_CGRAPH)
2179
                {
2180
                  struct cgraph_node *n = ipa_ref_node (ref);
2181
                  gcc_assert (!n->global.inlined_to);
2182
                  if (!n->local.externally_visible
2183
                      && !cgraph_node_in_set_p (n, set))
2184
                    promote_fn (n);
2185
                }
2186
              else
2187
                {
2188
                  struct varpool_node *v = ipa_ref_varpool_node (ref);
2189
                  if (varpool_node_in_set_p (v, vset))
2190
                    continue;
2191
 
2192
                  /* Constant pool references use internal labels and thus
2193
                     cannot be made global.  It is sensible to keep those
2194
                     ltrans local to allow better optimization.  */
2195
                  if (DECL_IN_CONSTANT_POOL (v->decl))
2196
                    {
2197
                      if (!pointer_set_insert (inserted, vnode))
2198
                        VEC_safe_push (varpool_node_ptr, heap,
2199
                                       promoted_initializers, v);
2200
                    }
2201
                  else if (!v->externally_visible && v->analyzed)
2202
                    {
2203
                      if (promote_var (v)
2204
                          && DECL_INITIAL (v->decl)
2205
                          && const_value_known_p (v->decl)
2206
                          && !pointer_set_insert (inserted, vnode))
2207
                        VEC_safe_push (varpool_node_ptr, heap,
2208
                                       promoted_initializers, v);
2209
                    }
2210
                }
2211
            }
2212
        }
2213
    }
2214
  pointer_set_destroy (inserted);
2215
}
2216
 
2217
static lto_file *current_lto_file;
2218
 
2219
/* Helper for qsort; compare partitions and return one with smaller size.
2220
   We sort from greatest to smallest so parallel build doesn't stale on the
2221
   longest compilation being executed too late.  */
2222
 
2223
static int
2224
cmp_partitions_size (const void *a, const void *b)
2225
{
2226
  const struct ltrans_partition_def *pa
2227
     = *(struct ltrans_partition_def *const *)a;
2228
  const struct ltrans_partition_def *pb
2229
     = *(struct ltrans_partition_def *const *)b;
2230
  return pb->insns - pa->insns;
2231
}
2232
 
2233
/* Helper for qsort; compare partitions and return one with smaller order.  */
2234
 
2235
static int
2236
cmp_partitions_order (const void *a, const void *b)
2237
{
2238
  const struct ltrans_partition_def *pa
2239
     = *(struct ltrans_partition_def *const *)a;
2240
  const struct ltrans_partition_def *pb
2241
     = *(struct ltrans_partition_def *const *)b;
2242
  int ordera = -1, orderb = -1;
2243
 
2244
  if (VEC_length (cgraph_node_ptr, pa->cgraph_set->nodes))
2245
    ordera = VEC_index (cgraph_node_ptr, pa->cgraph_set->nodes, 0)->order;
2246
  else if (VEC_length (varpool_node_ptr, pa->varpool_set->nodes))
2247
    ordera = VEC_index (varpool_node_ptr, pa->varpool_set->nodes, 0)->order;
2248
  if (VEC_length (cgraph_node_ptr, pb->cgraph_set->nodes))
2249
    orderb = VEC_index (cgraph_node_ptr, pb->cgraph_set->nodes, 0)->order;
2250
  else if (VEC_length (varpool_node_ptr, pb->varpool_set->nodes))
2251
    orderb = VEC_index (varpool_node_ptr, pb->varpool_set->nodes, 0)->order;
2252
  return orderb - ordera;
2253
}
2254
 
2255
/* Write all output files in WPA mode and the file with the list of
2256
   LTRANS units.  */
2257
 
2258
static void
2259
lto_wpa_write_files (void)
2260
{
2261
  unsigned i, n_sets;
2262
  lto_file *file;
2263
  cgraph_node_set set;
2264
  varpool_node_set vset;
2265
  ltrans_partition part;
2266
  FILE *ltrans_output_list_stream;
2267
  char *temp_filename;
2268
  size_t blen;
2269
 
2270
  /* Open the LTRANS output list.  */
2271
  if (!ltrans_output_list)
2272
    fatal_error ("no LTRANS output list filename provided");
2273
  ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2274
  if (ltrans_output_list_stream == NULL)
2275
    fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2276
 
2277
  timevar_push (TV_WHOPR_WPA);
2278
 
2279
  FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
2280
    lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
2281
                                                     part->cgraph_set->nodes);
2282
 
2283
  /* Find out statics that need to be promoted
2284
     to globals with hidden visibility because they are accessed from multiple
2285
     partitions.  */
2286
  lto_promote_cross_file_statics ();
2287
 
2288
  timevar_pop (TV_WHOPR_WPA);
2289
 
2290
  timevar_push (TV_WHOPR_WPA_IO);
2291
 
2292
  /* Generate a prefix for the LTRANS unit files.  */
2293
  blen = strlen (ltrans_output_list);
2294
  temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2295
  strcpy (temp_filename, ltrans_output_list);
2296
  if (blen > sizeof (".out")
2297
      && strcmp (temp_filename + blen - sizeof (".out") + 1,
2298
                 ".out") == 0)
2299
    temp_filename[blen - sizeof (".out") + 1] = '\0';
2300
  blen = strlen (temp_filename);
2301
 
2302
  n_sets = VEC_length (ltrans_partition, ltrans_partitions);
2303
 
2304
  /* Sort partitions by size so small ones are compiled last.
2305
     FIXME: Even when not reordering we may want to output one list for parallel make
2306
     and other for final link command.  */
2307
  VEC_qsort (ltrans_partition, ltrans_partitions,
2308
            flag_toplevel_reorder ? cmp_partitions_size : cmp_partitions_order);
2309
  for (i = 0; i < n_sets; i++)
2310
    {
2311
      size_t len;
2312
      ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
2313
 
2314
      set = part->cgraph_set;
2315
      vset = part->varpool_set;
2316
 
2317
      /* Write all the nodes in SET.  */
2318
      sprintf (temp_filename + blen, "%u.o", i);
2319
      file = lto_obj_file_open (temp_filename, true);
2320
      if (!file)
2321
        fatal_error ("lto_obj_file_open() failed");
2322
 
2323
      if (!quiet_flag)
2324
        fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2325
      if (cgraph_dump_file)
2326
        {
2327
          fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
2328
                   part->name, temp_filename, part->insns);
2329
          fprintf (cgraph_dump_file, "cgraph nodes:");
2330
          dump_cgraph_node_set (cgraph_dump_file, set);
2331
          fprintf (cgraph_dump_file, "varpool nodes:");
2332
          dump_varpool_node_set (cgraph_dump_file, vset);
2333
        }
2334
      gcc_checking_assert (cgraph_node_set_nonempty_p (set)
2335
                           || varpool_node_set_nonempty_p (vset) || !i);
2336
 
2337
      lto_set_current_out_file (file);
2338
 
2339
      ipa_write_optimization_summaries (set, vset);
2340
 
2341
      lto_set_current_out_file (NULL);
2342
      lto_obj_file_close (file);
2343
 
2344
      len = strlen (temp_filename);
2345
      if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
2346
          || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2347
        fatal_error ("writing to LTRANS output list %s: %m",
2348
                     ltrans_output_list);
2349
    }
2350
 
2351
  lto_stats.num_output_files += n_sets;
2352
 
2353
  /* Close the LTRANS output list.  */
2354
  if (fclose (ltrans_output_list_stream))
2355
    fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2356
 
2357
  free_ltrans_partitions();
2358
 
2359
  timevar_pop (TV_WHOPR_WPA_IO);
2360
}
2361
 
2362
 
2363
/* If TT is a variable or function decl replace it with its
2364
   prevailing variant.  */
2365
#define LTO_SET_PREVAIL(tt) \
2366
  do {\
2367
    if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
2368
      tt = lto_symtab_prevailing_decl (tt); \
2369
  } while (0)
2370
 
2371
/* Ensure that TT isn't a replacable var of function decl.  */
2372
#define LTO_NO_PREVAIL(tt) \
2373
  gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2374
 
2375
/* Given a tree T replace all fields referring to variables or functions
2376
   with their prevailing variant.  */
2377
static void
2378
lto_fixup_prevailing_decls (tree t)
2379
{
2380
  enum tree_code code = TREE_CODE (t);
2381
  LTO_NO_PREVAIL (TREE_TYPE (t));
2382
  if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2383
    LTO_NO_PREVAIL (TREE_CHAIN (t));
2384
  if (DECL_P (t))
2385
    {
2386
      LTO_NO_PREVAIL (DECL_NAME (t));
2387
      LTO_SET_PREVAIL (DECL_CONTEXT (t));
2388
      if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2389
        {
2390
          LTO_SET_PREVAIL (DECL_SIZE (t));
2391
          LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2392
          LTO_SET_PREVAIL (DECL_INITIAL (t));
2393
          LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2394
          LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2395
        }
2396
      if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2397
        {
2398
          LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2399
          LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
2400
        }
2401
      if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2402
        {
2403
          LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
2404
          LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2405
          LTO_NO_PREVAIL (DECL_VINDEX (t));
2406
        }
2407
      if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2408
        LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2409
      if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2410
        {
2411
          LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
2412
          LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2413
          LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2414
          LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2415
          LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2416
        }
2417
    }
2418
  else if (TYPE_P (t))
2419
    {
2420
      LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2421
      LTO_SET_PREVAIL (TYPE_SIZE (t));
2422
      LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2423
      LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2424
      LTO_NO_PREVAIL (TYPE_NAME (t));
2425
 
2426
      LTO_SET_PREVAIL (TYPE_MINVAL (t));
2427
      LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2428
      LTO_SET_PREVAIL (t->type_non_common.binfo);
2429
 
2430
      LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2431
 
2432
      LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2433
      LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2434
      LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2435
    }
2436
  else if (EXPR_P (t))
2437
    {
2438
      int i;
2439
      LTO_NO_PREVAIL (t->exp.block);
2440
      for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2441
        LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2442
    }
2443
  else
2444
    {
2445
      switch (code)
2446
        {
2447
        case TREE_LIST:
2448
          LTO_SET_PREVAIL (TREE_VALUE (t));
2449
          LTO_SET_PREVAIL (TREE_PURPOSE (t));
2450
          break;
2451
        default:
2452
          gcc_unreachable ();
2453
        }
2454
    }
2455
}
2456
#undef LTO_SET_PREVAIL
2457
#undef LTO_NO_PREVAIL
2458
 
2459
/* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2460
   replaces var and function decls with the corresponding prevailing def.  */
2461
 
2462
static void
2463
lto_fixup_state (struct lto_in_decl_state *state)
2464
{
2465
  unsigned i, si;
2466
  struct lto_tree_ref_table *table;
2467
 
2468
  /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2469
     we still need to walk from all DECLs to find the reachable
2470
     FUNCTION_DECLs and VAR_DECLs.  */
2471
  for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2472
    {
2473
      table = &state->streams[si];
2474
      for (i = 0; i < table->size; i++)
2475
        {
2476
          tree *tp = table->trees + i;
2477
          if (VAR_OR_FUNCTION_DECL_P (*tp))
2478
            *tp = lto_symtab_prevailing_decl (*tp);
2479
        }
2480
    }
2481
}
2482
 
2483
/* A callback of htab_traverse. Just extracts a state from SLOT
2484
   and calls lto_fixup_state. */
2485
 
2486
static int
2487
lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
2488
{
2489
  struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
2490
  lto_fixup_state (state);
2491
  return 1;
2492
}
2493
 
2494
/* Fix the decls from all FILES. Replaces each decl with the corresponding
2495
   prevailing one.  */
2496
 
2497
static void
2498
lto_fixup_decls (struct lto_file_decl_data **files)
2499
{
2500
  unsigned int i;
2501
  htab_iterator hi;
2502
  tree t;
2503
 
2504
  FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
2505
    lto_fixup_prevailing_decls (t);
2506
 
2507
  for (i = 0; files[i]; i++)
2508
    {
2509
      struct lto_file_decl_data *file = files[i];
2510
      struct lto_in_decl_state *state = file->global_decl_state;
2511
      lto_fixup_state (state);
2512
 
2513
      htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
2514
    }
2515
}
2516
 
2517
static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2518
 
2519
/* Turn file datas for sub files into a single array, so that they look
2520
   like separate files for further passes. */
2521
 
2522
static void
2523
lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2524
{
2525
  struct lto_file_decl_data *n, *next;
2526
  int i, k;
2527
 
2528
  lto_stats.num_input_files = count;
2529
  all_file_decl_data
2530
    = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
2531
  /* Set the hooks so that all of the ipa passes can read in their data.  */
2532
  lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2533
  for (i = 0, k = 0; i < last_file_ix; i++)
2534
    {
2535
      for (n = orig[i]; n != NULL; n = next)
2536
        {
2537
          all_file_decl_data[k++] = n;
2538
          next = n->next;
2539
          n->next = NULL;
2540
        }
2541
    }
2542
  all_file_decl_data[k] = NULL;
2543
  gcc_assert (k == count);
2544
}
2545
 
2546
/* Input file data before flattening (i.e. splitting them to subfiles to support
2547
   incremental linking.  */
2548
static int real_file_count;
2549
static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2550
 
2551
/* Read all the symbols from the input files FNAMES.  NFILES is the
2552
   number of files requested in the command line.  Instantiate a
2553
   global call graph by aggregating all the sub-graphs found in each
2554
   file.  */
2555
 
2556
static void
2557
read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2558
{
2559
  unsigned int i, last_file_ix;
2560
  FILE *resolution;
2561
  struct cgraph_node *node;
2562
  int count = 0;
2563
  struct lto_file_decl_data **decl_data;
2564
 
2565
  init_cgraph ();
2566
 
2567
  timevar_push (TV_IPA_LTO_DECL_IN);
2568
 
2569
  real_file_decl_data
2570
    = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
2571
  real_file_count = nfiles;
2572
 
2573
  /* Read the resolution file.  */
2574
  resolution = NULL;
2575
  if (resolution_file_name)
2576
    {
2577
      int t;
2578
      unsigned num_objects;
2579
 
2580
      resolution = fopen (resolution_file_name, "r");
2581
      if (resolution == NULL)
2582
        fatal_error ("could not open symbol resolution file: %m");
2583
 
2584
      t = fscanf (resolution, "%u", &num_objects);
2585
      gcc_assert (t == 1);
2586
 
2587
      /* True, since the plugin splits the archives.  */
2588
      gcc_assert (num_objects == nfiles);
2589
    }
2590
 
2591
  tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
2592
                                    NULL);
2593
 
2594
  if (!quiet_flag)
2595
    fprintf (stderr, "Reading object files:");
2596
 
2597
  /* Read all of the object files specified on the command line.  */
2598
  for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2599
    {
2600
      struct lto_file_decl_data *file_data = NULL;
2601
      if (!quiet_flag)
2602
        {
2603
          fprintf (stderr, " %s", fnames[i]);
2604
          fflush (stderr);
2605
        }
2606
 
2607
      current_lto_file = lto_obj_file_open (fnames[i], false);
2608
      if (!current_lto_file)
2609
        break;
2610
 
2611
      file_data = lto_file_read (current_lto_file, resolution, &count);
2612
      if (!file_data)
2613
        {
2614
          lto_obj_file_close (current_lto_file);
2615
          current_lto_file = NULL;
2616
          break;
2617
        }
2618
 
2619
      decl_data[last_file_ix++] = file_data;
2620
 
2621
      lto_obj_file_close (current_lto_file);
2622
      current_lto_file = NULL;
2623
      ggc_collect ();
2624
    }
2625
 
2626
  lto_flatten_files (decl_data, count, last_file_ix);
2627
  lto_stats.num_input_files = count;
2628
  ggc_free(decl_data);
2629
  real_file_decl_data = NULL;
2630
 
2631
  if (resolution_file_name)
2632
    fclose (resolution);
2633
 
2634
  /* Set the hooks so that all of the ipa passes can read in their data.  */
2635
  lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2636
 
2637
  timevar_pop (TV_IPA_LTO_DECL_IN);
2638
 
2639
  if (!quiet_flag)
2640
    fprintf (stderr, "\nReading the callgraph\n");
2641
 
2642
  timevar_push (TV_IPA_LTO_CGRAPH_IO);
2643
  /* Read the callgraph.  */
2644
  input_cgraph ();
2645
  timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2646
 
2647
  if (!quiet_flag)
2648
    fprintf (stderr, "Merging declarations\n");
2649
 
2650
  timevar_push (TV_IPA_LTO_DECL_MERGE);
2651
  /* Merge global decls.  */
2652
  lto_symtab_merge_decls ();
2653
 
2654
  /* If there were errors during symbol merging bail out, we have no
2655
     good way to recover here.  */
2656
  if (seen_error ())
2657
    fatal_error ("errors during merging of translation units");
2658
 
2659
  /* Fixup all decls and types and free the type hash tables.  */
2660
  lto_fixup_decls (all_file_decl_data);
2661
  htab_delete (tree_with_vars);
2662
  tree_with_vars = NULL;
2663
  free_gimple_type_tables ();
2664
  ggc_collect ();
2665
 
2666
  timevar_pop (TV_IPA_LTO_DECL_MERGE);
2667
  /* Each pass will set the appropriate timer.  */
2668
 
2669
  if (!quiet_flag)
2670
    fprintf (stderr, "Reading summaries\n");
2671
 
2672
  /* Read the IPA summary data.  */
2673
  if (flag_ltrans)
2674
    ipa_read_optimization_summaries ();
2675
  else
2676
    ipa_read_summaries ();
2677
 
2678
  /* Finally merge the cgraph according to the decl merging decisions.  */
2679
  timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2680
  if (cgraph_dump_file)
2681
    {
2682
      fprintf (cgraph_dump_file, "Before merging:\n");
2683
      dump_cgraph (cgraph_dump_file);
2684
      dump_varpool (cgraph_dump_file);
2685
    }
2686
  lto_symtab_merge_cgraph_nodes ();
2687
  ggc_collect ();
2688
 
2689
  if (flag_ltrans)
2690
    for (node = cgraph_nodes; node; node = node->next)
2691
      {
2692
        /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
2693
           summaries computed and needs to apply changes.  At the moment WHOPR only
2694
           supports inlining, so we can push it here by hand.  In future we need to stream
2695
           this field into ltrans compilation.  */
2696
        if (node->analyzed)
2697
          VEC_safe_push (ipa_opt_pass, heap,
2698
                         node->ipa_transforms_to_apply,
2699
                         (ipa_opt_pass)&pass_ipa_inline);
2700
      }
2701
  lto_symtab_free ();
2702
 
2703
  timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2704
 
2705
  timevar_push (TV_IPA_LTO_DECL_INIT_IO);
2706
 
2707
  /* FIXME lto. This loop needs to be changed to use the pass manager to
2708
     call the ipa passes directly.  */
2709
  if (!seen_error ())
2710
    for (i = 0; i < last_file_ix; i++)
2711
      {
2712
        struct lto_file_decl_data *file_data = all_file_decl_data [i];
2713
        lto_materialize_constructors_and_inits (file_data);
2714
      }
2715
 
2716
  /* Indicate that the cgraph is built and ready.  */
2717
  cgraph_function_flags_ready = true;
2718
 
2719
  timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
2720
  ggc_free (all_file_decl_data);
2721
  all_file_decl_data = NULL;
2722
}
2723
 
2724
 
2725
/* Materialize all the bodies for all the nodes in the callgraph.  */
2726
 
2727
static void
2728
materialize_cgraph (void)
2729
{
2730
  tree decl;
2731
  struct cgraph_node *node;
2732
  unsigned i;
2733
  timevar_id_t lto_timer;
2734
 
2735
  if (!quiet_flag)
2736
    fprintf (stderr,
2737
             flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2738
 
2739
 
2740
  /* Now that we have input the cgraph, we need to clear all of the aux
2741
     nodes and read the functions if we are not running in WPA mode.  */
2742
  timevar_push (TV_IPA_LTO_GIMPLE_IN);
2743
 
2744
  for (node = cgraph_nodes; node; node = node->next)
2745
    {
2746
      if (node->local.lto_file_data)
2747
        {
2748
          lto_materialize_function (node);
2749
          lto_stats.num_input_cgraph_nodes++;
2750
        }
2751
    }
2752
 
2753
  timevar_pop (TV_IPA_LTO_GIMPLE_IN);
2754
 
2755
  /* Start the appropriate timer depending on the mode that we are
2756
     operating in.  */
2757
  lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2758
              : (flag_ltrans) ? TV_WHOPR_LTRANS
2759
              : TV_LTO;
2760
  timevar_push (lto_timer);
2761
 
2762
  current_function_decl = NULL;
2763
  set_cfun (NULL);
2764
 
2765
  /* Inform the middle end about the global variables we have seen.  */
2766
  FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
2767
    rest_of_decl_compilation (decl, 1, 0);
2768
 
2769
  if (!quiet_flag)
2770
    fprintf (stderr, "\n");
2771
 
2772
  timevar_pop (lto_timer);
2773
}
2774
 
2775
 
2776
/* Perform whole program analysis (WPA) on the callgraph and write out the
2777
   optimization plan.  */
2778
 
2779
static void
2780
do_whole_program_analysis (void)
2781
{
2782
  /* Note that since we are in WPA mode, materialize_cgraph will not
2783
     actually read in all the function bodies.  It only materializes
2784
     the decls and cgraph nodes so that analysis can be performed.  */
2785
  materialize_cgraph ();
2786
 
2787
  /* Reading in the cgraph uses different timers, start timing WPA now.  */
2788
  timevar_push (TV_WHOPR_WPA);
2789
 
2790
  if (pre_ipa_mem_report)
2791
    {
2792
      fprintf (stderr, "Memory consumption before IPA\n");
2793
      dump_memory_report (false);
2794
    }
2795
 
2796
  cgraph_function_flags_ready = true;
2797
 
2798
  if (cgraph_dump_file)
2799
    {
2800
      dump_cgraph (cgraph_dump_file);
2801
      dump_varpool (cgraph_dump_file);
2802
    }
2803
  bitmap_obstack_initialize (NULL);
2804
  cgraph_state = CGRAPH_STATE_IPA_SSA;
2805
 
2806
  execute_ipa_pass_list (all_regular_ipa_passes);
2807
 
2808
  if (cgraph_dump_file)
2809
    {
2810
      fprintf (cgraph_dump_file, "Optimized ");
2811
      dump_cgraph (cgraph_dump_file);
2812
      dump_varpool (cgraph_dump_file);
2813
    }
2814
  verify_cgraph ();
2815
  bitmap_obstack_release (NULL);
2816
 
2817
  /* We are about to launch the final LTRANS phase, stop the WPA timer.  */
2818
  timevar_pop (TV_WHOPR_WPA);
2819
 
2820
  if (flag_lto_partition_1to1)
2821
    lto_1_to_1_map ();
2822
  else
2823
    lto_balanced_map ();
2824
 
2825
  if (!quiet_flag)
2826
    {
2827
      fprintf (stderr, "\nStreaming out");
2828
      fflush (stderr);
2829
    }
2830
  lto_wpa_write_files ();
2831
  ggc_collect ();
2832
  if (!quiet_flag)
2833
    fprintf (stderr, "\n");
2834
 
2835
  if (post_ipa_mem_report)
2836
    {
2837
      fprintf (stderr, "Memory consumption after IPA\n");
2838
      dump_memory_report (false);
2839
    }
2840
 
2841
  /* Show the LTO report before launching LTRANS.  */
2842
  if (flag_lto_report)
2843
    print_lto_report ();
2844
}
2845
 
2846
 
2847
static GTY(()) tree lto_eh_personality_decl;
2848
 
2849
/* Return the LTO personality function decl.  */
2850
 
2851
tree
2852
lto_eh_personality (void)
2853
{
2854
  if (!lto_eh_personality_decl)
2855
    {
2856
      /* Use the first personality DECL for our personality if we don't
2857
         support multiple ones.  This ensures that we don't artificially
2858
         create the need for them in a single-language program.  */
2859
      if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2860
        lto_eh_personality_decl = first_personality_decl;
2861
      else
2862
        lto_eh_personality_decl = lhd_gcc_personality ();
2863
    }
2864
 
2865
  return lto_eh_personality_decl;
2866
}
2867
 
2868
/* Set the process name based on the LTO mode. */
2869
 
2870
static void
2871
lto_process_name (void)
2872
{
2873
  if (flag_lto)
2874
    setproctitle ("lto1-lto");
2875
  if (flag_wpa)
2876
    setproctitle ("lto1-wpa");
2877
  if (flag_ltrans)
2878
    setproctitle ("lto1-ltrans");
2879
}
2880
 
2881
 
2882
/* Initialize the LTO front end.  */
2883
 
2884
static void
2885
lto_init (void)
2886
{
2887
  lto_process_name ();
2888
  lto_streamer_hooks_init ();
2889
  lto_reader_init ();
2890
  lto_set_in_hooks (NULL, get_section_data, free_section_data);
2891
  memset (&lto_stats, 0, sizeof (lto_stats));
2892
  bitmap_obstack_initialize (NULL);
2893
  gimple_register_cfg_hooks ();
2894
}
2895
 
2896
 
2897
/* Main entry point for the GIMPLE front end.  This front end has
2898
   three main personalities:
2899
 
2900
   - LTO (-flto).  All the object files on the command line are
2901
     loaded in memory and processed as a single translation unit.
2902
     This is the traditional link-time optimization behavior.
2903
 
2904
   - WPA (-fwpa).  Only the callgraph and summary information for
2905
     files in the command file are loaded.  A single callgraph
2906
     (without function bodies) is instantiated for the whole set of
2907
     files.  IPA passes are only allowed to analyze the call graph
2908
     and make transformation decisions.  The callgraph is
2909
     partitioned, each partition is written to a new object file
2910
     together with the transformation decisions.
2911
 
2912
   - LTRANS (-fltrans).  Similar to -flto but it prevents the IPA
2913
     summary files from running again.  Since WPA computed summary
2914
     information and decided what transformations to apply, LTRANS
2915
     simply applies them.  */
2916
 
2917
void
2918
lto_main (void)
2919
{
2920
  /* Initialize the LTO front end.  */
2921
  lto_init ();
2922
 
2923
  /* Read all the symbols and call graph from all the files in the
2924
     command line.  */
2925
  read_cgraph_and_symbols (num_in_fnames, in_fnames);
2926
 
2927
  if (!seen_error ())
2928
    {
2929
      /* If WPA is enabled analyze the whole call graph and create an
2930
         optimization plan.  Otherwise, read in all the function
2931
         bodies and continue with optimization.  */
2932
      if (flag_wpa)
2933
        do_whole_program_analysis ();
2934
      else
2935
        {
2936
          materialize_cgraph ();
2937
 
2938
          /* Let the middle end know that we have read and merged all of
2939
             the input files.  */
2940
          cgraph_optimize ();
2941
 
2942
          /* FIXME lto, if the processes spawned by WPA fail, we miss
2943
             the chance to print WPA's report, so WPA will call
2944
             print_lto_report before launching LTRANS.  If LTRANS was
2945
             launched directly by the driver we would not need to do
2946
             this.  */
2947
          if (flag_lto_report)
2948
            print_lto_report ();
2949
        }
2950
    }
2951
}
2952
 
2953
#include "gt-lto-lto.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.