OSDN Git Service

2009-12-11 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
[pf3gnuchains/gcc-fork.git] / gcc / lto-cgraph.c
1 /* Write and read the cgraph to the memory mapped representation of a
2    .o file.
3
4    Copyright 2009 Free Software Foundation, Inc.
5    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "varray.h"
34 #include "hashtab.h"
35 #include "langhooks.h"
36 #include "basic-block.h"
37 #include "tree-flow.h"
38 #include "cgraph.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "diagnostic.h"
42 #include "except.h"
43 #include "vec.h"
44 #include "timevar.h"
45 #include "output.h"
46 #include "pointer-set.h"
47 #include "lto-streamer.h"
48 #include "gcov-io.h"
49
50 /* Create a new cgraph encoder.  */
51
52 lto_cgraph_encoder_t
53 lto_cgraph_encoder_new (void)
54 {
55   lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
56   encoder->map = pointer_map_create ();
57   encoder->nodes = NULL;
58   return encoder;
59 }
60
61
62 /* Delete ENCODER and its components.  */
63
64 void
65 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
66 {
67    VEC_free (cgraph_node_ptr, heap, encoder->nodes);
68    pointer_map_destroy (encoder->map);
69    free (encoder);
70 }
71
72
73 /* Return the existing reference number of NODE in the cgraph encoder in
74    output block OB.  Assign a new reference if this is the first time
75    NODE is encoded.  */
76
77 int
78 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
79                            struct cgraph_node *node)
80 {
81   int ref;
82   void **slot;
83
84   slot = pointer_map_contains (encoder->map, node);
85   if (!slot)
86     {
87       ref = VEC_length (cgraph_node_ptr, encoder->nodes);
88       slot = pointer_map_insert (encoder->map, node);
89       *slot = (void *) (intptr_t) ref;
90       VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
91     }
92   else
93     ref = (int) (intptr_t) *slot;
94
95   return ref;
96 }
97
98
99 /* Look up NODE in encoder.  Return NODE's reference if it has been encoded
100    or LCC_NOT_FOUND if it is not there.  */
101
102 int
103 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
104                            struct cgraph_node *node)
105 {
106   void **slot = pointer_map_contains (encoder->map, node);
107   return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
108 }
109
110
111 /* Return the cgraph node corresponding to REF using ENCODER.  */
112
113 struct cgraph_node *
114 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
115 {
116   if (ref == LCC_NOT_FOUND)
117     return NULL;
118
119   return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
120 }
121
122
123 /* Return number of encoded nodes in ENCODER.  */
124
125 static int
126 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
127 {
128   return VEC_length (cgraph_node_ptr, encoder->nodes);
129 }
130
131
132 /* Output the cgraph EDGE to OB using ENCODER.  */
133
134 static void
135 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
136                  lto_cgraph_encoder_t encoder)
137 {
138   unsigned int uid;
139   intptr_t ref;
140   struct bitpack_d *bp;
141
142   lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
143
144   ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
145   gcc_assert (ref != LCC_NOT_FOUND);
146   lto_output_sleb128_stream (ob->main_stream, ref);
147
148   ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
149   gcc_assert (ref != LCC_NOT_FOUND);
150   lto_output_sleb128_stream (ob->main_stream, ref);
151
152   lto_output_sleb128_stream (ob->main_stream, edge->count);
153
154   bp = bitpack_create ();
155   uid = flag_wpa ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt);
156   bp_pack_value (bp, uid, HOST_BITS_PER_INT);
157   bp_pack_value (bp, edge->inline_failed, HOST_BITS_PER_INT);
158   bp_pack_value (bp, edge->frequency, HOST_BITS_PER_INT);
159   bp_pack_value (bp, edge->loop_nest, 30);
160   bp_pack_value (bp, edge->indirect_call, 1);
161   bp_pack_value (bp, edge->call_stmt_cannot_inline_p, 1);
162   bp_pack_value (bp, edge->can_throw_external, 1);
163   lto_output_bitpack (ob->main_stream, bp);
164   bitpack_delete (bp);
165 }
166
167
168 /* Output the cgraph NODE to OB.  ENCODER is used to find the
169    reference number of NODE->inlined_to.  SET is the set of nodes we
170    are writing to the current file.  If NODE is not in SET, then NODE
171    is a boundary of a cgraph_node_set and we pretend NODE just has a
172    decl and no callees.  WRITTEN_DECLS is the set of FUNCTION_DECLs
173    that have had their callgraph node written so far.  This is used to
174    determine if NODE is a clone of a previously written node.  */
175
176 static void
177 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
178                  lto_cgraph_encoder_t encoder, cgraph_node_set set,
179                  bitmap written_decls)
180 {
181   unsigned int tag;
182   struct bitpack_d *bp;
183   unsigned local, externally_visible, inlinable, analyzed;
184   bool boundary_p, wrote_decl_p;
185   intptr_t ref;
186
187   boundary_p = !cgraph_node_in_set_p (node, set);
188   wrote_decl_p = bitmap_bit_p (written_decls, DECL_UID (node->decl));
189
190   switch (cgraph_function_body_availability (node))
191     {
192     case AVAIL_NOT_AVAILABLE:
193       tag = LTO_cgraph_unavail_node;
194       break;
195
196     case AVAIL_AVAILABLE:
197     case AVAIL_LOCAL:
198       tag = LTO_cgraph_avail_node;
199       break;
200
201     case AVAIL_OVERWRITABLE:
202       tag = LTO_cgraph_overwritable_node;
203       break;
204
205     default:
206       gcc_unreachable ();
207     }
208
209   if (boundary_p)
210     tag = LTO_cgraph_unavail_node;
211
212   lto_output_uleb128_stream (ob->main_stream, tag);
213
214   local = node->local.local;
215   externally_visible = node->local.externally_visible;
216   inlinable = node->local.inlinable;
217   analyzed = node->analyzed;
218
219   /* In WPA mode, we only output part of the call-graph.  Also, we
220      fake cgraph node attributes.  There are two cases that we care.
221
222      Boundary nodes: There are nodes that are not part of SET but are
223      called from within SET.  We artificially make them look like
224      externally visible nodes with no function body.
225
226      Cherry-picked nodes:  These are nodes we pulled from other
227      translation units into SET during IPA-inlining.  We make them as
228      local static nodes to prevent clashes with other local statics.  */
229   if (boundary_p)
230     {
231       /* Inline clones can not be part of boundary.  */
232       gcc_assert (!node->global.inlined_to);
233       local = 0;
234       externally_visible = 1;
235       inlinable = 0;
236       analyzed = 0;
237     }
238   else if (lto_forced_extern_inline_p (node->decl))
239     {
240       local = 1;
241       externally_visible = 0;
242       inlinable = 1;
243     }
244
245   lto_output_uleb128_stream (ob->main_stream, wrote_decl_p);
246
247   if (!wrote_decl_p)
248     bitmap_set_bit (written_decls, DECL_UID (node->decl));
249
250   lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
251   lto_output_sleb128_stream (ob->main_stream, node->count);
252
253   bp = bitpack_create ();
254   bp_pack_value (bp, local, 1);
255   bp_pack_value (bp, externally_visible, 1);
256   bp_pack_value (bp, node->local.finalized, 1);
257   bp_pack_value (bp, inlinable, 1);
258   bp_pack_value (bp, node->local.disregard_inline_limits, 1);
259   bp_pack_value (bp, node->local.redefined_extern_inline, 1);
260   bp_pack_value (bp, node->local.for_functions_valid, 1);
261   bp_pack_value (bp, node->local.vtable_method, 1);
262   bp_pack_value (bp, node->needed, 1);
263   bp_pack_value (bp, node->address_taken, 1);
264   bp_pack_value (bp, node->abstract_and_needed, 1);
265   bp_pack_value (bp, node->reachable, 1);
266   bp_pack_value (bp, node->lowered, 1);
267   bp_pack_value (bp, analyzed, 1);
268   bp_pack_value (bp, node->process, 1);
269   bp_pack_value (bp, node->alias, 1);
270   bp_pack_value (bp, node->finalized_by_frontend, 1);
271   lto_output_bitpack (ob->main_stream, bp);
272   bitpack_delete (bp);
273
274   if (tag != LTO_cgraph_unavail_node)
275     {
276       lto_output_sleb128_stream (ob->main_stream,
277                                  node->local.inline_summary.estimated_self_stack_size);
278       lto_output_sleb128_stream (ob->main_stream,
279                                  node->local.inline_summary.self_size);
280       lto_output_sleb128_stream (ob->main_stream,
281                                  node->local.inline_summary.size_inlining_benefit);
282       lto_output_sleb128_stream (ob->main_stream,
283                                  node->local.inline_summary.self_time);
284       lto_output_sleb128_stream (ob->main_stream,
285                                  node->local.inline_summary.time_inlining_benefit);
286     }
287
288   /* FIXME lto: Outputting global info is not neccesary until after
289      inliner was run.  Global structure holds results of propagation
290      done by inliner.  */
291   lto_output_sleb128_stream (ob->main_stream,
292                              node->global.estimated_stack_size);
293   lto_output_sleb128_stream (ob->main_stream,
294                              node->global.stack_frame_offset);
295   if (node->global.inlined_to && !boundary_p)
296     {
297       ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
298       gcc_assert (ref != LCC_NOT_FOUND);
299     }
300   else
301     ref = LCC_NOT_FOUND;
302   lto_output_sleb128_stream (ob->main_stream, ref);
303
304   lto_output_sleb128_stream (ob->main_stream, node->global.time);
305   lto_output_sleb128_stream (ob->main_stream, node->global.size);
306   lto_output_sleb128_stream (ob->main_stream,
307                              node->global.estimated_growth);
308   lto_output_uleb128_stream (ob->main_stream, node->global.inlined);
309   if (node->same_comdat_group)
310     {
311       ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
312       gcc_assert (ref != LCC_NOT_FOUND);
313     }
314   else
315     ref = LCC_NOT_FOUND;
316   lto_output_sleb128_stream (ob->main_stream, ref);
317
318   if (node->same_body)
319     {
320       struct cgraph_node *alias;
321       unsigned long alias_count = 1;
322       for (alias = node->same_body; alias->next; alias = alias->next)
323         alias_count++;
324       lto_output_uleb128_stream (ob->main_stream, alias_count);
325       do
326         {
327           lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
328                                     alias->decl);
329           if (alias->thunk.thunk_p)
330             {
331               lto_output_uleb128_stream
332                  (ob->main_stream,
333                   1 + (alias->thunk.this_adjusting != 0) * 2
334                   + (alias->thunk.virtual_offset_p != 0) * 4);
335               lto_output_uleb128_stream (ob->main_stream,
336                                          alias->thunk.fixed_offset);
337               lto_output_uleb128_stream (ob->main_stream,
338                                          alias->thunk.virtual_value);
339               lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
340                                         alias->thunk.alias);
341             }
342           else
343             lto_output_uleb128_stream (ob->main_stream, 0);
344           alias = alias->previous;
345         }
346       while (alias);
347     }
348   else
349     lto_output_uleb128_stream (ob->main_stream, 0);
350 }
351
352 /* Stream out profile_summary to OB.  */
353
354 static void
355 output_profile_summary (struct lto_simple_output_block *ob)
356 {
357   if (profile_info)
358     {
359       /* We do not output num, it is not terribly useful.  */
360       gcc_assert (profile_info->runs);
361       lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
362       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
363       lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
364       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
365     }
366   else
367     lto_output_uleb128_stream (ob->main_stream, 0);
368 }
369
370
371 /* Output the part of the cgraph in SET.  */
372
373 void
374 output_cgraph (cgraph_node_set set)
375 {
376   struct cgraph_node *node;
377   struct lto_simple_output_block *ob;
378   cgraph_node_set_iterator csi;
379   struct cgraph_edge *edge;
380   int i, n_nodes;
381   bitmap written_decls;
382   lto_cgraph_encoder_t encoder;
383   struct cgraph_asm_node *can;
384
385   ob = lto_create_simple_output_block (LTO_section_cgraph);
386
387   output_profile_summary (ob);
388
389   /* An encoder for cgraph nodes should have been created by
390      ipa_write_summaries_1.  */
391   gcc_assert (ob->decl_state->cgraph_node_encoder);
392   encoder = ob->decl_state->cgraph_node_encoder;
393
394   /* The FUNCTION_DECLs for which we have written a node.  The first
395      node found is written as the "original" node, the remaining nodes
396      are considered its clones.  */
397   written_decls = lto_bitmap_alloc ();
398
399   /* Go over all the nodes in SET and assign references.  */
400   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
401     {
402       node = csi_node (csi);
403       lto_cgraph_encoder_encode (encoder, node);
404     }
405
406   /* Go over all the nodes again to include callees that are not in
407      SET.  */
408   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
409     {
410       node = csi_node (csi);
411       for (edge = node->callees; edge; edge = edge->next_callee)
412         {
413           struct cgraph_node *callee = edge->callee;
414           if (!cgraph_node_in_set_p (callee, set))
415             {
416               /* We should have moved all the inlines.  */
417               gcc_assert (!callee->global.inlined_to);
418               lto_cgraph_encoder_encode (encoder, callee);
419               /* Also with each included function include all other functions
420                  in the same comdat group.  */
421               if (callee->same_comdat_group)
422                 {
423                   struct cgraph_node *next;
424                   for (next = callee->same_comdat_group;
425                        next != callee;
426                        next = next->same_comdat_group)
427                     if (!cgraph_node_in_set_p (next, set))
428                       lto_cgraph_encoder_encode (encoder, next);
429                 }
430             }
431         }
432       /* Also with each included function include all other functions
433          in the same comdat group.  */
434       if (node->same_comdat_group)
435         {
436           struct cgraph_node *next;
437           for (next = node->same_comdat_group;
438                next != node;
439                next = next->same_comdat_group)
440             if (!cgraph_node_in_set_p (next, set))
441               lto_cgraph_encoder_encode (encoder, next);
442         }
443     }
444
445   /* Write out the nodes.  */
446   n_nodes = lto_cgraph_encoder_size (encoder);
447   for (i = 0; i < n_nodes; i++)
448     {
449       node = lto_cgraph_encoder_deref (encoder, i);
450       lto_output_node (ob, node, encoder, set, written_decls);
451     }
452
453   lto_bitmap_free (written_decls);
454
455   /* Go over the nodes in SET again to write edges.  */
456   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
457     {
458       node = csi_node (csi);
459       if (node->callees)
460         {
461           /* Output edges in backward direction, so the reconstructed callgraph
462              match and it is easy to associate call sites in the IPA pass summaries.  */
463           edge = node->callees;
464           while (edge->next_callee)
465             edge = edge->next_callee;
466           for (; edge; edge = edge->prev_callee)
467             lto_output_edge (ob, edge, encoder);
468         }
469     }
470
471   lto_output_uleb128_stream (ob->main_stream, 0);
472
473   /* Emit toplevel asms.  */
474   for (can = cgraph_asm_nodes; can; can = can->next)
475     {
476       int len = TREE_STRING_LENGTH (can->asm_str);
477       lto_output_uleb128_stream (ob->main_stream, len);
478       for (i = 0; i < len; ++i)
479         lto_output_1_stream (ob->main_stream,
480                              TREE_STRING_POINTER (can->asm_str)[i]);
481     }
482
483   lto_output_uleb128_stream (ob->main_stream, 0);
484
485   lto_destroy_simple_output_block (ob);
486 }
487
488
489 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
490    STACK_SIZE, SELF_TIME and SELF_SIZE.  This is called either to initialize
491    NODE or to replace the values in it, for instance because the first
492    time we saw it, the function body was not available but now it
493    is.  BP is a bitpack with all the bitflags for NODE read from the
494    stream.  */
495
496 static void
497 input_overwrite_node (struct lto_file_decl_data *file_data,
498                       struct cgraph_node *node,
499                       enum LTO_cgraph_tags tag,
500                       struct bitpack_d *bp,
501                       unsigned int stack_size,
502                       unsigned int self_time,
503                       unsigned int time_inlining_benefit,
504                       unsigned int self_size,
505                       unsigned int size_inlining_benefit)
506 {
507   node->aux = (void *) tag;
508   node->local.inline_summary.estimated_self_stack_size = stack_size;
509   node->local.inline_summary.self_time = self_time;
510   node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
511   node->local.inline_summary.self_size = self_size;
512   node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
513   node->global.time = self_time;
514   node->global.size = self_size;
515   node->local.lto_file_data = file_data;
516
517   node->local.local = bp_unpack_value (bp, 1);
518   node->local.externally_visible = bp_unpack_value (bp, 1);
519   node->local.finalized = bp_unpack_value (bp, 1);
520   node->local.inlinable = bp_unpack_value (bp, 1);
521   node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
522   node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
523   node->local.for_functions_valid = bp_unpack_value (bp, 1);
524   node->local.vtable_method = bp_unpack_value (bp, 1);
525   node->needed = bp_unpack_value (bp, 1);
526   node->address_taken = bp_unpack_value (bp, 1);
527   node->abstract_and_needed = bp_unpack_value (bp, 1);
528   node->reachable = bp_unpack_value (bp, 1);
529   node->lowered = bp_unpack_value (bp, 1);
530   node->analyzed = bp_unpack_value (bp, 1);
531   node->process = bp_unpack_value (bp, 1);
532   node->alias = bp_unpack_value (bp, 1);
533   node->finalized_by_frontend = bp_unpack_value (bp, 1);
534 }
535
536
537 /* Read a node from input_block IB.  TAG is the node's tag just read.
538    Return the node read or overwriten.  */
539
540 static struct cgraph_node *
541 input_node (struct lto_file_decl_data *file_data,
542             struct lto_input_block *ib,
543             enum LTO_cgraph_tags tag)
544 {
545   tree fn_decl;
546   struct cgraph_node *node;
547   struct bitpack_d *bp;
548   int stack_size = 0;
549   unsigned decl_index;
550   bool clone_p;
551   int estimated_stack_size = 0;
552   int stack_frame_offset = 0;
553   int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
554   int estimated_growth = 0;
555   int time = 0;
556   int size = 0;
557   int self_time = 0;
558   int self_size = 0;
559   int time_inlining_benefit = 0;
560   int size_inlining_benefit = 0;
561   unsigned long same_body_count = 0;
562   bool inlined = false;
563
564   clone_p = (lto_input_uleb128 (ib) != 0);
565
566   decl_index = lto_input_uleb128 (ib);
567   fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
568
569   if (clone_p)
570     node = cgraph_clone_node (cgraph_node (fn_decl), 0,
571                               CGRAPH_FREQ_BASE, 0, false, NULL);
572
573   else
574     node = cgraph_node (fn_decl);
575
576   node->count = lto_input_sleb128 (ib);
577   bp = lto_input_bitpack (ib);
578
579   if (tag != LTO_cgraph_unavail_node)
580     {
581       stack_size = lto_input_sleb128 (ib);
582       self_size = lto_input_sleb128 (ib);
583       size_inlining_benefit = lto_input_sleb128 (ib);
584       self_time = lto_input_sleb128 (ib);
585       time_inlining_benefit = lto_input_sleb128 (ib);
586     }
587
588   estimated_stack_size = lto_input_sleb128 (ib);
589   stack_frame_offset = lto_input_sleb128 (ib);
590   ref = lto_input_sleb128 (ib);
591   time = lto_input_sleb128 (ib);
592   size = lto_input_sleb128 (ib);
593   estimated_growth = lto_input_sleb128 (ib);
594   inlined = lto_input_uleb128 (ib);
595   ref2 = lto_input_sleb128 (ib);
596   same_body_count = lto_input_uleb128 (ib);
597
598   /* Make sure that we have not read this node before.  Nodes that
599      have already been read will have their tag stored in the 'aux'
600      field.  Since built-in functions can be referenced in multiple
601      functions, they are expected to be read more than once.  */
602   if (node->aux && !DECL_IS_BUILTIN (node->decl))
603     internal_error ("bytecode stream: found multiple instances of cgraph "
604                     "node %d", node->uid);
605
606   input_overwrite_node (file_data, node, tag, bp, stack_size, self_time,
607                         time_inlining_benefit, self_size,
608                         size_inlining_benefit);
609   bitpack_delete (bp);
610
611   node->global.estimated_stack_size = estimated_stack_size;
612   node->global.stack_frame_offset = stack_frame_offset;
613   node->global.time = time;
614   node->global.size = size;
615
616   /* Store a reference for now, and fix up later to be a pointer.  */
617   node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
618
619   node->global.estimated_growth = estimated_growth;
620   node->global.inlined = inlined;
621
622   /* Store a reference for now, and fix up later to be a pointer.  */
623   node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
624
625   while (same_body_count-- > 0)
626     {
627       tree alias_decl;
628       int type;
629       decl_index = lto_input_uleb128 (ib);
630       alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
631       type = lto_input_uleb128 (ib);
632       if (!type)
633         cgraph_same_body_alias (alias_decl, fn_decl);
634       else
635         {
636           HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
637           HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
638           tree real_alias;
639           decl_index = lto_input_uleb128 (ib);
640           real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
641           cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
642                             virtual_value,
643                             (type & 4) ? size_int (virtual_value) : NULL_TREE,
644                             real_alias);
645         }
646     }
647   return node;
648 }
649
650
651 /* Read an edge from IB.  NODES points to a vector of previously read
652    nodes for decoding caller and callee of the edge to be read.  */
653
654 static void
655 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes)
656 {
657   struct cgraph_node *caller, *callee;
658   struct cgraph_edge *edge;
659   unsigned int stmt_id;
660   gcov_type count;
661   int freq;
662   unsigned int nest;
663   cgraph_inline_failed_t inline_failed;
664   struct bitpack_d *bp;
665   enum ld_plugin_symbol_resolution caller_resolution;
666
667   caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
668   if (caller == NULL || caller->decl == NULL_TREE)
669     internal_error ("bytecode stream: no caller found while reading edge");
670
671   callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
672   if (callee == NULL || callee->decl == NULL_TREE)
673     internal_error ("bytecode stream: no callee found while reading edge");
674
675   count = (gcov_type) lto_input_sleb128 (ib);
676
677   bp = lto_input_bitpack (ib);
678   stmt_id = (unsigned int) bp_unpack_value (bp, HOST_BITS_PER_INT);
679   inline_failed = (cgraph_inline_failed_t) bp_unpack_value (bp,
680                                                             HOST_BITS_PER_INT);
681   freq = (int) bp_unpack_value (bp, HOST_BITS_PER_INT);
682   nest = (unsigned) bp_unpack_value (bp, 30);
683
684   /* If the caller was preempted, don't create the edge.
685      ???  Should we ever have edges from a preempted caller?  */
686   caller_resolution = lto_symtab_get_resolution (caller->decl);
687   if (caller_resolution == LDPR_PREEMPTED_REG
688       || caller_resolution == LDPR_PREEMPTED_IR)
689     return;
690
691   edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
692   edge->lto_stmt_uid = stmt_id;
693   edge->inline_failed = inline_failed;
694   edge->indirect_call = bp_unpack_value (bp, 1);
695   edge->call_stmt_cannot_inline_p = bp_unpack_value (bp, 1);
696   edge->can_throw_external = bp_unpack_value (bp, 1);
697   bitpack_delete (bp);
698 }
699
700
701 /* Read a cgraph from IB using the info in FILE_DATA.  */
702
703 static void
704 input_cgraph_1 (struct lto_file_decl_data *file_data,
705                 struct lto_input_block *ib)
706 {
707   enum LTO_cgraph_tags tag;
708   VEC(cgraph_node_ptr, heap) *nodes = NULL;
709   struct cgraph_node *node;
710   unsigned i;
711   unsigned HOST_WIDE_INT len;
712
713   tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
714   while (tag)
715     {
716       if (tag == LTO_cgraph_edge)
717         input_edge (ib, nodes);
718       else
719         {
720           node = input_node (file_data, ib, tag);
721           if (node == NULL || node->decl == NULL_TREE)
722             internal_error ("bytecode stream: found empty cgraph node");
723           VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
724           lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
725         }
726
727       tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
728     }
729
730   /* Input toplevel asms.  */
731   len = lto_input_uleb128 (ib);
732   while (len)
733     {
734       char *str = (char *)xmalloc (len + 1);
735       for (i = 0; i < len; ++i)
736         str[i] = lto_input_1_unsigned (ib);
737       cgraph_add_asm_node (build_string (len, str));
738       free (str);
739
740       len = lto_input_uleb128 (ib);
741     }
742
743   for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
744     {
745       int ref = (int) (intptr_t) node->global.inlined_to;
746
747       /* Fixup inlined_to from reference to pointer.  */
748       if (ref != LCC_NOT_FOUND)
749         node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
750       else
751         node->global.inlined_to = NULL;
752
753       ref = (int) (intptr_t) node->same_comdat_group;
754
755       /* Fixup same_comdat_group from reference to pointer.  */
756       if (ref != LCC_NOT_FOUND)
757         node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
758       else
759         node->same_comdat_group = NULL;
760     }
761
762   VEC_free (cgraph_node_ptr, heap, nodes);
763 }
764
765 static struct gcov_ctr_summary lto_gcov_summary;
766
767 /* Input profile_info from IB.  */
768 static void
769 input_profile_summary (struct lto_input_block *ib)
770 {
771   unsigned int runs = lto_input_uleb128 (ib);
772   if (runs)
773     {
774       if (!profile_info)
775         {
776           profile_info = &lto_gcov_summary;
777           lto_gcov_summary.runs = runs;
778           lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
779           lto_gcov_summary.run_max = lto_input_sleb128 (ib);
780           lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
781         }
782       /* We can support this by scaling all counts to nearest common multiple
783          of all different runs, but it is perhaps not worth the effort.  */
784       else if (profile_info->runs != runs
785                || profile_info->sum_all != lto_input_sleb128 (ib)
786                || profile_info->run_max != lto_input_sleb128 (ib)
787                || profile_info->sum_max != lto_input_sleb128 (ib))
788         sorry ("Combining units with different profiles is not supported.");
789       /* We allow some units to have profile and other to not have one.  This will
790          just make unprofiled units to be size optimized that is sane.  */
791     }
792
793 }
794
795 /* Input and merge the cgraph from each of the .o files passed to
796    lto1.  */
797
798 void
799 input_cgraph (void)
800 {
801   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
802   struct lto_file_decl_data *file_data;
803   unsigned int j = 0;
804   struct cgraph_node *node;
805
806   while ((file_data = file_data_vec[j++]))
807     {
808       const char *data;
809       size_t len;
810       struct lto_input_block *ib;
811
812       ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
813                                           &data, &len);
814       input_profile_summary (ib);
815       file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
816       input_cgraph_1 (file_data, ib);
817       lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
818                                       ib, data, len);
819
820       /* Assume that every file read needs to be processed by LTRANS.  */
821       if (flag_wpa)
822         lto_mark_file_for_ltrans (file_data);
823     }
824
825   /* Clear out the aux field that was used to store enough state to
826      tell which nodes should be overwritten.  */
827   for (node = cgraph_nodes; node; node = node->next)
828     {
829       /* Some nodes may have been created by cgraph_node.  This
830          happens when the callgraph contains nested functions.  If the
831          node for the parent function was never emitted to the gimple
832          file, cgraph_node will create a node for it when setting the
833          context of the nested function.  */
834       if (node->local.lto_file_data)
835         node->aux = NULL;
836     }
837 }