OSDN Git Service

ChangeLog:
[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 /* Return true when node is reachable from other partition.  */
168
169 static bool
170 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
171 {
172   struct cgraph_edge *e;
173   if (node->needed)
174     return true;
175   if (!node->analyzed)
176     return false;
177   if (node->global.inlined_to)
178     return false;
179   for (e = node->callers; e; e = e->next_caller)
180     if (!cgraph_node_in_set_p (e->caller, set))
181       return true;
182   return false;
183 }
184
185 /* Output the cgraph NODE to OB.  ENCODER is used to find the
186    reference number of NODE->inlined_to.  SET is the set of nodes we
187    are writing to the current file.  If NODE is not in SET, then NODE
188    is a boundary of a cgraph_node_set and we pretend NODE just has a
189    decl and no callees.  WRITTEN_DECLS is the set of FUNCTION_DECLs
190    that have had their callgraph node written so far.  This is used to
191    determine if NODE is a clone of a previously written node.  */
192
193 static void
194 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
195                  lto_cgraph_encoder_t encoder, cgraph_node_set set,
196                  bitmap written_decls)
197 {
198   unsigned int tag;
199   struct bitpack_d *bp;
200   unsigned local, externally_visible, inlinable, analyzed;
201   bool boundary_p, wrote_decl_p;
202   intptr_t ref;
203   bool in_other_partition = false;
204
205   boundary_p = !cgraph_node_in_set_p (node, set);
206   wrote_decl_p = bitmap_bit_p (written_decls, DECL_UID (node->decl));
207
208   switch (cgraph_function_body_availability (node))
209     {
210     case AVAIL_NOT_AVAILABLE:
211       tag = LTO_cgraph_unavail_node;
212       break;
213
214     case AVAIL_AVAILABLE:
215     case AVAIL_LOCAL:
216       tag = LTO_cgraph_avail_node;
217       break;
218
219     case AVAIL_OVERWRITABLE:
220       tag = LTO_cgraph_overwritable_node;
221       break;
222
223     default:
224       gcc_unreachable ();
225     }
226
227   if (boundary_p)
228     tag = LTO_cgraph_unavail_node;
229
230   lto_output_uleb128_stream (ob->main_stream, tag);
231
232   local = node->local.local;
233   externally_visible = node->local.externally_visible;
234   inlinable = node->local.inlinable;
235   analyzed = node->analyzed;
236
237   /* In WPA mode, we only output part of the call-graph.  Also, we
238      fake cgraph node attributes.  There are two cases that we care.
239
240      Boundary nodes: There are nodes that are not part of SET but are
241      called from within SET.  We artificially make them look like
242      externally visible nodes with no function body.
243
244      Cherry-picked nodes:  These are nodes we pulled from other
245      translation units into SET during IPA-inlining.  We make them as
246      local static nodes to prevent clashes with other local statics.  */
247   if (boundary_p)
248     {
249       /* Inline clones can not be part of boundary.  
250          gcc_assert (!node->global.inlined_to);  
251
252          FIXME: At the moment they can be, when partition contains an inline
253          clone that is clone of inline clone from outside partition.  We can
254          reshape the clone tree and make other tree to be the root, but it
255          needs a bit extra work and will be promplty done by cgraph_remove_node
256          after reading back.  */
257       in_other_partition = 1;
258       analyzed = 0;
259     }
260
261   lto_output_uleb128_stream (ob->main_stream, wrote_decl_p);
262
263   if (!wrote_decl_p)
264     bitmap_set_bit (written_decls, DECL_UID (node->decl));
265
266   lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
267   lto_output_sleb128_stream (ob->main_stream, node->count);
268
269   bp = bitpack_create ();
270   bp_pack_value (bp, local, 1);
271   bp_pack_value (bp, externally_visible, 1);
272   bp_pack_value (bp, node->local.finalized, 1);
273   bp_pack_value (bp, inlinable, 1);
274   bp_pack_value (bp, node->local.disregard_inline_limits, 1);
275   bp_pack_value (bp, node->local.redefined_extern_inline, 1);
276   bp_pack_value (bp, node->local.for_functions_valid, 1);
277   bp_pack_value (bp, node->local.vtable_method, 1);
278   bp_pack_value (bp, node->needed, 1);
279   bp_pack_value (bp, node->address_taken, 1);
280   bp_pack_value (bp, node->abstract_and_needed, 1);
281   bp_pack_value (bp, node->reachable, 1);
282   bp_pack_value (bp, analyzed && reachable_from_other_partition_p (node, set), 1);
283   bp_pack_value (bp, node->lowered, 1);
284   bp_pack_value (bp, analyzed, 1);
285   bp_pack_value (bp, in_other_partition, 1);
286   bp_pack_value (bp, node->process, 1);
287   bp_pack_value (bp, node->alias, 1);
288   bp_pack_value (bp, node->finalized_by_frontend, 1);
289   bp_pack_value (bp, node->frequency, 2);
290   lto_output_bitpack (ob->main_stream, bp);
291   bitpack_delete (bp);
292
293   if (tag != LTO_cgraph_unavail_node)
294     {
295       lto_output_sleb128_stream (ob->main_stream,
296                                  node->local.inline_summary.estimated_self_stack_size);
297       lto_output_sleb128_stream (ob->main_stream,
298                                  node->local.inline_summary.self_size);
299       lto_output_sleb128_stream (ob->main_stream,
300                                  node->local.inline_summary.size_inlining_benefit);
301       lto_output_sleb128_stream (ob->main_stream,
302                                  node->local.inline_summary.self_time);
303       lto_output_sleb128_stream (ob->main_stream,
304                                  node->local.inline_summary.time_inlining_benefit);
305     }
306
307   /* FIXME lto: Outputting global info is not neccesary until after
308      inliner was run.  Global structure holds results of propagation
309      done by inliner.  */
310   lto_output_sleb128_stream (ob->main_stream,
311                              node->global.estimated_stack_size);
312   lto_output_sleb128_stream (ob->main_stream,
313                              node->global.stack_frame_offset);
314   if (node->global.inlined_to && !boundary_p)
315     {
316       ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
317       gcc_assert (ref != LCC_NOT_FOUND);
318     }
319   else
320     ref = LCC_NOT_FOUND;
321   lto_output_sleb128_stream (ob->main_stream, ref);
322
323   lto_output_sleb128_stream (ob->main_stream, node->global.time);
324   lto_output_sleb128_stream (ob->main_stream, node->global.size);
325   lto_output_sleb128_stream (ob->main_stream,
326                              node->global.estimated_growth);
327   lto_output_uleb128_stream (ob->main_stream, node->global.inlined);
328   if (node->same_comdat_group && !boundary_p)
329     {
330       ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
331       gcc_assert (ref != LCC_NOT_FOUND);
332     }
333   else
334     ref = LCC_NOT_FOUND;
335   lto_output_sleb128_stream (ob->main_stream, ref);
336
337   if (node->same_body)
338     {
339       struct cgraph_node *alias;
340       unsigned long alias_count = 1;
341       for (alias = node->same_body; alias->next; alias = alias->next)
342         alias_count++;
343       lto_output_uleb128_stream (ob->main_stream, alias_count);
344       do
345         {
346           lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
347                                     alias->decl);
348           if (alias->thunk.thunk_p)
349             {
350               lto_output_uleb128_stream
351                  (ob->main_stream,
352                   1 + (alias->thunk.this_adjusting != 0) * 2
353                   + (alias->thunk.virtual_offset_p != 0) * 4);
354               lto_output_uleb128_stream (ob->main_stream,
355                                          alias->thunk.fixed_offset);
356               lto_output_uleb128_stream (ob->main_stream,
357                                          alias->thunk.virtual_value);
358               lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
359                                         alias->thunk.alias);
360             }
361           else
362             {
363               lto_output_uleb128_stream (ob->main_stream, 0);
364               lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
365                                         alias->thunk.alias);
366             }
367           alias = alias->previous;
368         }
369       while (alias);
370     }
371   else
372     lto_output_uleb128_stream (ob->main_stream, 0);
373 }
374
375 /* Stream out profile_summary to OB.  */
376
377 static void
378 output_profile_summary (struct lto_simple_output_block *ob)
379 {
380   if (profile_info)
381     {
382       /* We do not output num, it is not terribly useful.  */
383       gcc_assert (profile_info->runs);
384       lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
385       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
386       lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
387       lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
388     }
389   else
390     lto_output_uleb128_stream (ob->main_stream, 0);
391 }
392
393 /* Add NODE into encoder as well as nodes it is cloned from.
394    Do it in a way so clones appear first.  */
395 static void
396 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node)
397 {
398   if (node->clone_of)
399     add_node_to (encoder, node->clone_of);
400   lto_cgraph_encoder_encode (encoder, node);
401 }
402
403 /* Output the part of the cgraph in SET.  */
404
405 void
406 output_cgraph (cgraph_node_set set)
407 {
408   struct cgraph_node *node;
409   struct lto_simple_output_block *ob;
410   cgraph_node_set_iterator csi;
411   struct cgraph_edge *edge;
412   int i, n_nodes;
413   bitmap written_decls;
414   lto_cgraph_encoder_t encoder;
415   struct cgraph_asm_node *can;
416
417   ob = lto_create_simple_output_block (LTO_section_cgraph);
418
419   output_profile_summary (ob);
420
421   /* An encoder for cgraph nodes should have been created by
422      ipa_write_summaries_1.  */
423   gcc_assert (ob->decl_state->cgraph_node_encoder);
424   encoder = ob->decl_state->cgraph_node_encoder;
425
426   /* The FUNCTION_DECLs for which we have written a node.  The first
427      node found is written as the "original" node, the remaining nodes
428      are considered its clones.  */
429   written_decls = lto_bitmap_alloc ();
430
431   /* Go over all the nodes in SET and assign references.  */
432   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
433     {
434       node = csi_node (csi);
435       add_node_to (encoder, node);
436     }
437
438   /* Go over all the nodes again to include callees that are not in
439      SET.  */
440   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
441     {
442       node = csi_node (csi);
443       for (edge = node->callees; edge; edge = edge->next_callee)
444         {
445           struct cgraph_node *callee = edge->callee;
446           if (!cgraph_node_in_set_p (callee, set))
447             {
448               /* We should have moved all the inlines.  */
449               gcc_assert (!callee->global.inlined_to);
450               add_node_to (encoder, callee);
451             }
452         }
453     }
454
455   /* Write out the nodes.  We must first output a node and then its clones,
456      otherwise at a time reading back the node there would be nothing to clone
457      from.  */
458   n_nodes = lto_cgraph_encoder_size (encoder);
459   for (i = 0; i < n_nodes; i++)
460     {
461       node = lto_cgraph_encoder_deref (encoder, i);
462       lto_output_node (ob, node, encoder, set, written_decls);
463     }
464
465   lto_bitmap_free (written_decls);
466
467   /* Go over the nodes in SET again to write edges.  */
468   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
469     {
470       node = csi_node (csi);
471       if (node->callees)
472         {
473           /* Output edges in backward direction, so the reconstructed callgraph
474              match and it is easy to associate call sites in the IPA pass summaries.  */
475           edge = node->callees;
476           while (edge->next_callee)
477             edge = edge->next_callee;
478           for (; edge; edge = edge->prev_callee)
479             lto_output_edge (ob, edge, encoder);
480         }
481     }
482
483   lto_output_uleb128_stream (ob->main_stream, 0);
484
485   /* Emit toplevel asms.  */
486   for (can = cgraph_asm_nodes; can; can = can->next)
487     {
488       int len = TREE_STRING_LENGTH (can->asm_str);
489       lto_output_uleb128_stream (ob->main_stream, len);
490       for (i = 0; i < len; ++i)
491         lto_output_1_stream (ob->main_stream,
492                              TREE_STRING_POINTER (can->asm_str)[i]);
493     }
494
495   lto_output_uleb128_stream (ob->main_stream, 0);
496
497   lto_destroy_simple_output_block (ob);
498 }
499
500
501 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
502    STACK_SIZE, SELF_TIME and SELF_SIZE.  This is called either to initialize
503    NODE or to replace the values in it, for instance because the first
504    time we saw it, the function body was not available but now it
505    is.  BP is a bitpack with all the bitflags for NODE read from the
506    stream.  */
507
508 static void
509 input_overwrite_node (struct lto_file_decl_data *file_data,
510                       struct cgraph_node *node,
511                       enum LTO_cgraph_tags tag,
512                       struct bitpack_d *bp,
513                       unsigned int stack_size,
514                       unsigned int self_time,
515                       unsigned int time_inlining_benefit,
516                       unsigned int self_size,
517                       unsigned int size_inlining_benefit)
518 {
519   node->aux = (void *) tag;
520   node->local.inline_summary.estimated_self_stack_size = stack_size;
521   node->local.inline_summary.self_time = self_time;
522   node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
523   node->local.inline_summary.self_size = self_size;
524   node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
525   node->global.time = self_time;
526   node->global.size = self_size;
527   node->local.lto_file_data = file_data;
528
529   node->local.local = bp_unpack_value (bp, 1);
530   node->local.externally_visible = bp_unpack_value (bp, 1);
531   node->local.finalized = bp_unpack_value (bp, 1);
532   node->local.inlinable = bp_unpack_value (bp, 1);
533   node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
534   node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
535   node->local.for_functions_valid = bp_unpack_value (bp, 1);
536   node->local.vtable_method = bp_unpack_value (bp, 1);
537   node->needed = bp_unpack_value (bp, 1);
538   node->address_taken = bp_unpack_value (bp, 1);
539   node->abstract_and_needed = bp_unpack_value (bp, 1);
540   node->reachable = bp_unpack_value (bp, 1);
541   node->reachable_from_other_partition = bp_unpack_value (bp, 1);
542   node->lowered = bp_unpack_value (bp, 1);
543   node->analyzed = bp_unpack_value (bp, 1);
544   node->in_other_partition = bp_unpack_value (bp, 1);
545   node->process = bp_unpack_value (bp, 1);
546   node->alias = bp_unpack_value (bp, 1);
547   node->finalized_by_frontend = bp_unpack_value (bp, 1);
548   node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
549 }
550
551
552 /* Read a node from input_block IB.  TAG is the node's tag just read.
553    Return the node read or overwriten.  */
554
555 static struct cgraph_node *
556 input_node (struct lto_file_decl_data *file_data,
557             struct lto_input_block *ib,
558             enum LTO_cgraph_tags tag)
559 {
560   tree fn_decl;
561   struct cgraph_node *node;
562   struct bitpack_d *bp;
563   int stack_size = 0;
564   unsigned decl_index;
565   bool clone_p;
566   int estimated_stack_size = 0;
567   int stack_frame_offset = 0;
568   int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
569   int estimated_growth = 0;
570   int time = 0;
571   int size = 0;
572   int self_time = 0;
573   int self_size = 0;
574   int time_inlining_benefit = 0;
575   int size_inlining_benefit = 0;
576   unsigned long same_body_count = 0;
577   bool inlined = false;
578
579   clone_p = (lto_input_uleb128 (ib) != 0);
580
581   decl_index = lto_input_uleb128 (ib);
582   fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
583
584   if (clone_p)
585     node = cgraph_clone_node (cgraph_node (fn_decl), 0,
586                               CGRAPH_FREQ_BASE, 0, false, NULL);
587
588   else
589     node = cgraph_node (fn_decl);
590
591   node->count = lto_input_sleb128 (ib);
592   bp = lto_input_bitpack (ib);
593
594   if (tag != LTO_cgraph_unavail_node)
595     {
596       stack_size = lto_input_sleb128 (ib);
597       self_size = lto_input_sleb128 (ib);
598       size_inlining_benefit = lto_input_sleb128 (ib);
599       self_time = lto_input_sleb128 (ib);
600       time_inlining_benefit = lto_input_sleb128 (ib);
601     }
602
603   estimated_stack_size = lto_input_sleb128 (ib);
604   stack_frame_offset = lto_input_sleb128 (ib);
605   ref = lto_input_sleb128 (ib);
606   time = lto_input_sleb128 (ib);
607   size = lto_input_sleb128 (ib);
608   estimated_growth = lto_input_sleb128 (ib);
609   inlined = lto_input_uleb128 (ib);
610   ref2 = lto_input_sleb128 (ib);
611   same_body_count = lto_input_uleb128 (ib);
612
613   /* Make sure that we have not read this node before.  Nodes that
614      have already been read will have their tag stored in the 'aux'
615      field.  Since built-in functions can be referenced in multiple
616      functions, they are expected to be read more than once.  */
617   if (node->aux && !DECL_IS_BUILTIN (node->decl))
618     internal_error ("bytecode stream: found multiple instances of cgraph "
619                     "node %d", node->uid);
620
621   input_overwrite_node (file_data, node, tag, bp, stack_size, self_time,
622                         time_inlining_benefit, self_size,
623                         size_inlining_benefit);
624   bitpack_delete (bp);
625
626   node->global.estimated_stack_size = estimated_stack_size;
627   node->global.stack_frame_offset = stack_frame_offset;
628   node->global.time = time;
629   node->global.size = size;
630
631   /* Store a reference for now, and fix up later to be a pointer.  */
632   node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
633
634   node->global.estimated_growth = estimated_growth;
635   node->global.inlined = inlined;
636
637   /* Store a reference for now, and fix up later to be a pointer.  */
638   node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
639
640   while (same_body_count-- > 0)
641     {
642       tree alias_decl;
643       int type;
644       decl_index = lto_input_uleb128 (ib);
645       alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
646       type = lto_input_uleb128 (ib);
647       if (!type)
648         {
649           tree real_alias;
650           decl_index = lto_input_uleb128 (ib);
651           real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
652           cgraph_same_body_alias (alias_decl, real_alias);
653         }
654       else
655         {
656           HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
657           HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
658           tree real_alias;
659           decl_index = lto_input_uleb128 (ib);
660           real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
661           cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
662                             virtual_value,
663                             (type & 4) ? size_int (virtual_value) : NULL_TREE,
664                             real_alias);
665         }
666     }
667   return node;
668 }
669
670
671 /* Read an edge from IB.  NODES points to a vector of previously read
672    nodes for decoding caller and callee of the edge to be read.  */
673
674 static void
675 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes)
676 {
677   struct cgraph_node *caller, *callee;
678   struct cgraph_edge *edge;
679   unsigned int stmt_id;
680   gcov_type count;
681   int freq;
682   unsigned int nest;
683   cgraph_inline_failed_t inline_failed;
684   struct bitpack_d *bp;
685   enum ld_plugin_symbol_resolution caller_resolution;
686
687   caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
688   if (caller == NULL || caller->decl == NULL_TREE)
689     internal_error ("bytecode stream: no caller found while reading edge");
690
691   callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
692   if (callee == NULL || callee->decl == NULL_TREE)
693     internal_error ("bytecode stream: no callee found while reading edge");
694
695   count = (gcov_type) lto_input_sleb128 (ib);
696
697   bp = lto_input_bitpack (ib);
698   stmt_id = (unsigned int) bp_unpack_value (bp, HOST_BITS_PER_INT);
699   inline_failed = (cgraph_inline_failed_t) bp_unpack_value (bp,
700                                                             HOST_BITS_PER_INT);
701   freq = (int) bp_unpack_value (bp, HOST_BITS_PER_INT);
702   nest = (unsigned) bp_unpack_value (bp, 30);
703
704   /* If the caller was preempted, don't create the edge.
705      ???  Should we ever have edges from a preempted caller?  */
706   caller_resolution = lto_symtab_get_resolution (caller->decl);
707   if (caller_resolution == LDPR_PREEMPTED_REG
708       || caller_resolution == LDPR_PREEMPTED_IR)
709     return;
710
711   edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
712   edge->lto_stmt_uid = stmt_id;
713   edge->inline_failed = inline_failed;
714   edge->indirect_call = bp_unpack_value (bp, 1);
715   edge->call_stmt_cannot_inline_p = bp_unpack_value (bp, 1);
716   edge->can_throw_external = bp_unpack_value (bp, 1);
717   bitpack_delete (bp);
718 }
719
720
721 /* Read a cgraph from IB using the info in FILE_DATA.  */
722
723 static void
724 input_cgraph_1 (struct lto_file_decl_data *file_data,
725                 struct lto_input_block *ib)
726 {
727   enum LTO_cgraph_tags tag;
728   VEC(cgraph_node_ptr, heap) *nodes = NULL;
729   struct cgraph_node *node;
730   unsigned i;
731   unsigned HOST_WIDE_INT len;
732
733   tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
734   while (tag)
735     {
736       if (tag == LTO_cgraph_edge)
737         input_edge (ib, nodes);
738       else
739         {
740           node = input_node (file_data, ib, tag);
741           if (node == NULL || node->decl == NULL_TREE)
742             internal_error ("bytecode stream: found empty cgraph node");
743           VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
744           lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
745         }
746
747       tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
748     }
749
750   /* Input toplevel asms.  */
751   len = lto_input_uleb128 (ib);
752   while (len)
753     {
754       char *str = (char *)xmalloc (len + 1);
755       for (i = 0; i < len; ++i)
756         str[i] = lto_input_1_unsigned (ib);
757       cgraph_add_asm_node (build_string (len, str));
758       free (str);
759
760       len = lto_input_uleb128 (ib);
761     }
762
763   for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
764     {
765       int ref = (int) (intptr_t) node->global.inlined_to;
766
767       /* Fixup inlined_to from reference to pointer.  */
768       if (ref != LCC_NOT_FOUND)
769         node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
770       else
771         node->global.inlined_to = NULL;
772
773       ref = (int) (intptr_t) node->same_comdat_group;
774
775       /* Fixup same_comdat_group from reference to pointer.  */
776       if (ref != LCC_NOT_FOUND)
777         node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
778       else
779         node->same_comdat_group = NULL;
780     }
781
782   VEC_free (cgraph_node_ptr, heap, nodes);
783 }
784
785 static struct gcov_ctr_summary lto_gcov_summary;
786
787 /* Input profile_info from IB.  */
788 static void
789 input_profile_summary (struct lto_input_block *ib)
790 {
791   unsigned int runs = lto_input_uleb128 (ib);
792   if (runs)
793     {
794       if (!profile_info)
795         {
796           profile_info = &lto_gcov_summary;
797           lto_gcov_summary.runs = runs;
798           lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
799           lto_gcov_summary.run_max = lto_input_sleb128 (ib);
800           lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
801         }
802       /* We can support this by scaling all counts to nearest common multiple
803          of all different runs, but it is perhaps not worth the effort.  */
804       else if (profile_info->runs != runs
805                || profile_info->sum_all != lto_input_sleb128 (ib)
806                || profile_info->run_max != lto_input_sleb128 (ib)
807                || profile_info->sum_max != lto_input_sleb128 (ib))
808         sorry ("Combining units with different profiles is not supported.");
809       /* We allow some units to have profile and other to not have one.  This will
810          just make unprofiled units to be size optimized that is sane.  */
811     }
812
813 }
814
815 /* Input and merge the cgraph from each of the .o files passed to
816    lto1.  */
817
818 void
819 input_cgraph (void)
820 {
821   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
822   struct lto_file_decl_data *file_data;
823   unsigned int j = 0;
824   struct cgraph_node *node;
825
826   while ((file_data = file_data_vec[j++]))
827     {
828       const char *data;
829       size_t len;
830       struct lto_input_block *ib;
831
832       ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
833                                           &data, &len);
834       input_profile_summary (ib);
835       file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
836       input_cgraph_1 (file_data, ib);
837       lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
838                                       ib, data, len);
839
840       /* Assume that every file read needs to be processed by LTRANS.  */
841       if (flag_wpa)
842         lto_mark_file_for_ltrans (file_data);
843     }
844
845   /* Clear out the aux field that was used to store enough state to
846      tell which nodes should be overwritten.  */
847   for (node = cgraph_nodes; node; node = node->next)
848     {
849       /* Some nodes may have been created by cgraph_node.  This
850          happens when the callgraph contains nested functions.  If the
851          node for the parent function was never emitted to the gimple
852          file, cgraph_node will create a node for it when setting the
853          context of the nested function.  */
854       if (node->local.lto_file_data)
855         node->aux = NULL;
856     }
857 }