lto-streamer-in.c 33.2 KB
Newer Older
1 2
/* Read the GIMPLE representation from a file stream.

3
   Copyright 2009, 2010 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
   Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
   Re-implemented by Diego Novillo <dnovillo@google.com>

This file is part of GCC.

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

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

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

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "toplev.h"
#include "tree.h"
#include "expr.h"
#include "flags.h"
#include "params.h"
#include "input.h"
#include "hashtab.h"
#include "basic-block.h"
#include "tree-flow.h"
#include "tree-pass.h"
#include "cgraph.h"
#include "function.h"
#include "ggc.h"
#include "diagnostic.h"
#include "except.h"
#include "debug.h"
#include "vec.h"
#include "ipa-utils.h"
Diego Novillo committed
45 46
#include "data-streamer.h"
#include "gimple-streamer.h"
47
#include "lto-streamer.h"
Diego Novillo committed
48
#include "tree-streamer.h"
49
#include "tree-pass.h"
50
#include "streamer-hooks.h"
51 52 53 54 55 56 57 58

/* The table to hold the file names.  */
static htab_t file_name_hash_table;


/* Check that tag ACTUAL has one of the given values.  NUM_TAGS is the
   number of valid tag values to check.  */

Diego Novillo committed
59
void
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
{
  va_list ap;
  int i;

  va_start (ap, ntags);
  for (i = 0; i < ntags; i++)
    if ((unsigned) actual == va_arg (ap, unsigned))
      {
	va_end (ap);
	return;
      }

  va_end (ap);
  internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
}


78 79 80 81 82 83 84 85 86
/* Read LENGTH bytes from STREAM to ADDR.  */

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

  for (i = 0; i < length; i++)
87
    buffer[i] = streamer_read_uchar (ib);
88 89 90
}


91 92 93 94 95 96 97 98
/* Lookup STRING in file_name_hash_table.  If found, return the existing
   string, otherwise insert STRING as the canonical version.  */

static const char *
canon_file_name (const char *string)
{
  void **slot;
  struct string_slot s_slot;
99 100
  size_t len = strlen (string);

101
  s_slot.s = string;
102
  s_slot.len = len;
103 104 105 106 107 108 109 110 111

  slot = htab_find_slot (file_name_hash_table, &s_slot, INSERT);
  if (*slot == NULL)
    {
      char *saved_string;
      struct string_slot *new_slot;

      saved_string = (char *) xmalloc (len + 1);
      new_slot = XCNEW (struct string_slot);
112
      memcpy (saved_string, string, len + 1);
113
      new_slot->s = saved_string;
114
      new_slot->len = len;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      *slot = new_slot;
      return saved_string;
    }
  else
    {
      struct string_slot *old_slot = (struct string_slot *) *slot;
      return old_slot->s;
    }
}


/* Clear the line info stored in DATA_IN.  */

static void
clear_line_info (struct data_in *data_in)
{
  if (data_in->current_file)
    linemap_add (line_table, LC_LEAVE, false, NULL, 0);
  data_in->current_file = NULL;
  data_in->current_line = 0;
  data_in->current_col = 0;
}


139
/* Read a location bitpack from input block IB.  */
140 141

static location_t
142
lto_input_location_bitpack (struct data_in *data_in, struct bitpack_d *bp)
143
{
144 145 146
  bool file_change, line_change, column_change;
  unsigned len;
  bool prev_file = data_in->current_file != NULL;
147

148
  if (bp_unpack_value (bp, 1))
149 150
    return UNKNOWN_LOCATION;

151 152 153 154 155 156 157 158 159 160
  file_change = bp_unpack_value (bp, 1);
  if (file_change)
    data_in->current_file = canon_file_name
			      (string_for_index (data_in,
						 bp_unpack_var_len_unsigned (bp),
					         &len));

  line_change = bp_unpack_value (bp, 1);
  if (line_change)
    data_in->current_line = bp_unpack_var_len_unsigned (bp);
161

162 163 164 165 166
  column_change = bp_unpack_value (bp, 1);
  if (column_change)
    data_in->current_col = bp_unpack_var_len_unsigned (bp);

  if (file_change)
167
    {
168
      if (prev_file)
169
	linemap_add (line_table, LC_LEAVE, false, NULL, 0);
170

171 172
      linemap_add (line_table, LC_ENTER, false, data_in->current_file,
		   data_in->current_line);
173
    }
174 175 176 177 178 179
  else if (line_change)
    linemap_line_start (line_table, data_in->current_line, data_in->current_col);

  return linemap_position_for_column (line_table, data_in->current_col);
}

180

181 182 183 184
/* Read a location from input block IB.
   If the input_location streamer hook exists, call it.
   Otherwise, proceed with reading the location from the
   expanded location bitpack.  */
185

Diego Novillo committed
186
location_t
187 188
lto_input_location (struct lto_input_block *ib, struct data_in *data_in)
{
189 190 191 192 193
  if (streamer_hooks.input_location)
    return streamer_hooks.input_location (ib, data_in);
  else
    {
      struct bitpack_d bp;
194

195 196 197
      bp = streamer_read_bitpack (ib);
      return lto_input_location_bitpack (data_in, &bp);
    }
198 199 200 201 202 203 204
}


/* Read a reference to a tree node from DATA_IN using input block IB.
   TAG is the expected node that should be found in IB, if TAG belongs
   to one of the indexable trees, expect to read a reference index to
   be looked up in one of the symbol tables, otherwise read the pysical
205
   representation of the tree using stream_read_tree.  FN is the
206 207
   function scope for the read tree.  */

Diego Novillo committed
208
tree
H.J. Lu committed
209
lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
210 211 212 213 214 215 216 217 218 219
		    struct function *fn, enum LTO_tags tag)
{
  unsigned HOST_WIDE_INT ix_u;
  tree result = NULL_TREE;

  lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);

  switch (tag)
    {
    case LTO_type_ref:
220
      ix_u = streamer_read_uhwi (ib);
221 222 223 224
      result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
      break;

    case LTO_ssa_name_ref:
225
      ix_u = streamer_read_uhwi (ib);
226 227 228 229
      result = VEC_index (tree, SSANAMES (fn), ix_u);
      break;

    case LTO_field_decl_ref:
230
      ix_u = streamer_read_uhwi (ib);
231 232 233 234
      result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
      break;

    case LTO_function_decl_ref:
235
      ix_u = streamer_read_uhwi (ib);
236 237 238 239
      result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
      break;

    case LTO_type_decl_ref:
240
      ix_u = streamer_read_uhwi (ib);
241 242 243 244
      result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
      break;

    case LTO_namespace_decl_ref:
245
      ix_u = streamer_read_uhwi (ib);
246 247 248 249 250 251 252 253
      result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
      break;

    case LTO_global_decl_ref:
    case LTO_result_decl_ref:
    case LTO_const_decl_ref:
    case LTO_imported_decl_ref:
    case LTO_label_decl_ref:
254
    case LTO_translation_unit_decl_ref:
255
      ix_u = streamer_read_uhwi (ib);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
      break;

    default:
      gcc_unreachable ();
    }

  gcc_assert (result);

  return result;
}


/* Read and return a double-linked list of catch handlers from input
   block IB, using descriptors in DATA_IN.  */

static struct eh_catch_d *
lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
			 eh_catch *last_p)
{
  eh_catch first;
  enum LTO_tags tag;

  *last_p = first = NULL;
280
  tag = streamer_read_record_start (ib);
281 282 283 284 285 286 287 288
  while (tag)
    {
      tree list;
      eh_catch n;

      lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);

      /* Read the catch node.  */
289
      n = ggc_alloc_cleared_eh_catch_d ();
290 291 292
      n->type_list = stream_read_tree (ib, data_in);
      n->filter_list = stream_read_tree (ib, data_in);
      n->label = stream_read_tree (ib, data_in);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

      /* Register all the types in N->FILTER_LIST.  */
      for (list = n->filter_list; list; list = TREE_CHAIN (list))
	add_type_for_runtime (TREE_VALUE (list));

      /* Chain N to the end of the list.  */
      if (*last_p)
	(*last_p)->next_catch = n;
      n->prev_catch = *last_p;
      *last_p = n;

      /* Set the head of the list the first time through the loop.  */
      if (first == NULL)
	first = n;

308
      tag = streamer_read_record_start (ib);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    }

  return first;
}


/* Read and return EH region IX from input block IB, using descriptors
   in DATA_IN.  */

static eh_region
input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
{
  enum LTO_tags tag;
  eh_region r;

  /* Read the region header.  */
325
  tag = streamer_read_record_start (ib);
326 327 328
  if (tag == LTO_null)
    return NULL;

329
  r = ggc_alloc_cleared_eh_region_d ();
330
  r->index = streamer_read_hwi (ib);
331 332 333 334 335

  gcc_assert (r->index == ix);

  /* Read all the region pointers as region numbers.  We'll fix up
     the pointers once the whole array has been read.  */
336 337 338
  r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
  r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
  r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

  switch (tag)
    {
      case LTO_ert_cleanup:
	r->type = ERT_CLEANUP;
	break;

      case LTO_ert_try:
	{
	  struct eh_catch_d *last_catch;
	  r->type = ERT_TRY;
	  r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
							     &last_catch);
	  r->u.eh_try.last_catch = last_catch;
	  break;
	}

      case LTO_ert_allowed_exceptions:
	{
	  tree l;

	  r->type = ERT_ALLOWED_EXCEPTIONS;
361 362
	  r->u.allowed.type_list = stream_read_tree (ib, data_in);
	  r->u.allowed.label = stream_read_tree (ib, data_in);
363
	  r->u.allowed.filter = streamer_read_uhwi (ib);
364 365 366 367 368 369 370 371

	  for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
	    add_type_for_runtime (TREE_VALUE (l));
	}
	break;

      case LTO_ert_must_not_throw:
	r->type = ERT_MUST_NOT_THROW;
372
	r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
373 374 375 376 377 378 379
	r->u.must_not_throw.failure_loc = lto_input_location (ib, data_in);
	break;

      default:
	gcc_unreachable ();
    }

380
  r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

  return r;
}


/* Read and return EH landing pad IX from input block IB, using descriptors
   in DATA_IN.  */

static eh_landing_pad
input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
{
  enum LTO_tags tag;
  eh_landing_pad lp;

  /* Read the landing pad header.  */
396
  tag = streamer_read_record_start (ib);
397 398 399 400 401
  if (tag == LTO_null)
    return NULL;

  lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);

402
  lp = ggc_alloc_cleared_eh_landing_pad_d ();
403
  lp->index = streamer_read_hwi (ib);
404
  gcc_assert (lp->index == ix);
405 406
  lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
  lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
407
  lp->post_landing_pad = stream_read_tree (ib, data_in);
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

  return lp;
}


/* After reading the EH regions, pointers to peer and children regions
   are region numbers.  This converts all these region numbers into
   real pointers into the rematerialized regions for FN.  ROOT_REGION
   is the region number for the root EH region in FN.  */

static void
fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
{
  unsigned i;
  VEC(eh_region,gc) *eh_array = fn->eh->region_array;
  VEC(eh_landing_pad,gc) *lp_array = fn->eh->lp_array;
  eh_region r;
  eh_landing_pad lp;

  gcc_assert (eh_array && lp_array);

  gcc_assert (root_region >= 0);
  fn->eh->region_tree = VEC_index (eh_region, eh_array, root_region);

#define FIXUP_EH_REGION(r) (r) = VEC_index (eh_region, eh_array, \
					    (HOST_WIDE_INT) (intptr_t) (r))
#define FIXUP_EH_LP(p) (p) = VEC_index (eh_landing_pad, lp_array, \
					(HOST_WIDE_INT) (intptr_t) (p))

  /* Convert all the index numbers stored in pointer fields into
     pointers to the corresponding slots in the EH region array.  */
439
  FOR_EACH_VEC_ELT (eh_region, eh_array, i, r)
440 441 442 443 444 445 446 447 448 449 450 451 452 453
    {
      /* The array may contain NULL regions.  */
      if (r == NULL)
	continue;

      gcc_assert (i == (unsigned) r->index);
      FIXUP_EH_REGION (r->outer);
      FIXUP_EH_REGION (r->inner);
      FIXUP_EH_REGION (r->next_peer);
      FIXUP_EH_LP (r->landing_pads);
    }

  /* Convert all the index numbers stored in pointer fields into
     pointers to the corresponding slots in the EH landing pad array.  */
454
  FOR_EACH_VEC_ELT (eh_landing_pad, lp_array, i, lp)
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    {
      /* The array may contain NULL landing pads.  */
      if (lp == NULL)
	continue;

      gcc_assert (i == (unsigned) lp->index);
      FIXUP_EH_LP (lp->next_lp);
      FIXUP_EH_REGION (lp->region);
    }

#undef FIXUP_EH_REGION
#undef FIXUP_EH_LP
}


/* Initialize EH support.  */

Diego Novillo committed
472
void
473 474
lto_init_eh (void)
{
475 476 477 478 479
  static bool eh_initialized_p = false;

  if (eh_initialized_p)
    return;

480 481 482 483 484 485 486 487
  /* Contrary to most other FEs, we only initialize EH support when at
     least one of the files in the set contains exception regions in
     it.  Since this happens much later than the call to init_eh in
     lang_dependent_init, we have to set flag_exceptions and call
     init_eh again to initialize the EH tables.  */
  flag_exceptions = 1;
  init_eh ();

488
  eh_initialized_p = true;
489 490 491 492 493 494 495 496 497 498 499 500
}


/* Read the exception table for FN from IB using the data descriptors
   in DATA_IN.  */

static void
input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
		  struct function *fn)
{
  HOST_WIDE_INT i, root_region, len;
  enum LTO_tags tag;
H.J. Lu committed
501

502
  tag = streamer_read_record_start (ib);
503 504 505 506 507 508 509 510
  if (tag == LTO_null)
    return;

  lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);

  /* If the file contains EH regions, then it was compiled with
     -fexceptions.  In that case, initialize the backend EH
     machinery.  */
511
  lto_init_eh ();
512 513 514

  gcc_assert (fn->eh);

515
  root_region = streamer_read_hwi (ib);
516 517 518
  gcc_assert (root_region == (int) root_region);

  /* Read the EH region array.  */
519
  len = streamer_read_hwi (ib);
520 521 522 523 524 525 526 527 528 529 530 531
  gcc_assert (len == (int) len);
  if (len > 0)
    {
      VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
      for (i = 0; i < len; i++)
	{
	  eh_region r = input_eh_region (ib, data_in, i);
	  VEC_replace (eh_region, fn->eh->region_array, i, r);
	}
    }

  /* Read the landing pads.  */
532
  len = streamer_read_hwi (ib);
533 534 535 536 537 538 539 540 541 542 543 544
  gcc_assert (len == (int) len);
  if (len > 0)
    {
      VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
      for (i = 0; i < len; i++)
	{
	  eh_landing_pad lp = input_eh_lp (ib, data_in, i);
	  VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
	}
    }

  /* Read the runtime type data.  */
545
  len = streamer_read_hwi (ib);
546 547 548 549 550 551
  gcc_assert (len == (int) len);
  if (len > 0)
    {
      VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
      for (i = 0; i < len; i++)
	{
552
	  tree ttype = stream_read_tree (ib, data_in);
553 554 555 556 557
	  VEC_replace (tree, fn->eh->ttype_data, i, ttype);
	}
    }

  /* Read the table of action chains.  */
558
  len = streamer_read_hwi (ib);
559 560 561 562 563 564 565 566
  gcc_assert (len == (int) len);
  if (len > 0)
    {
      if (targetm.arm_eabi_unwinder)
	{
	  VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
	  for (i = 0; i < len; i++)
	    {
567
	      tree t = stream_read_tree (ib, data_in);
568 569 570 571 572 573 574 575
	      VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
	    }
	}
      else
	{
	  VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
	  for (i = 0; i < len; i++)
	    {
576
	      uchar c = streamer_read_uchar (ib);
577 578 579 580 581 582 583 584 585
	      VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
	    }
	}
    }

  /* Reconstruct the EH region tree by fixing up the peer/children
     pointers.  */
  fixup_eh_region_pointers (fn, root_region);

586
  tag = streamer_read_record_start (ib);
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
  lto_tag_check_range (tag, LTO_null, LTO_null);
}


/* Make a new basic block with index INDEX in function FN.  */

static basic_block
make_new_block (struct function *fn, unsigned int index)
{
  basic_block bb = alloc_block ();
  bb->index = index;
  SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
  n_basic_blocks_for_function (fn)++;
  return bb;
}


/* Read the CFG for function FN from input block IB.  */

H.J. Lu committed
606
static void
607 608
input_cfg (struct lto_input_block *ib, struct function *fn,
	   int count_materialization_scale)
609 610 611 612 613 614 615
{
  unsigned int bb_count;
  basic_block p_bb;
  unsigned int i;
  int index;

  init_empty_tree_cfg_for_function (fn);
616
  init_ssa_operands (fn);
617

618 619
  profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
							 PROFILE_LAST);
620

621
  bb_count = streamer_read_uhwi (ib);
622 623 624 625 626 627 628

  last_basic_block_for_function (fn) = bb_count;
  if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
    VEC_safe_grow_cleared (basic_block, gc,
			   basic_block_info_for_function (fn), bb_count);

  if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
H.J. Lu committed
629
    VEC_safe_grow_cleared (basic_block, gc,
630 631
			   label_to_block_map_for_function (fn), bb_count);

632
  index = streamer_read_hwi (ib);
633 634 635 636 637 638 639 640
  while (index != -1)
    {
      basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
      unsigned int edge_count;

      if (bb == NULL)
	bb = make_new_block (fn, index);

641
      edge_count = streamer_read_uhwi (ib);
642 643 644 645 646 647 648 649 650 651 652

      /* Connect up the CFG.  */
      for (i = 0; i < edge_count; i++)
	{
	  unsigned int dest_index;
	  unsigned int edge_flags;
	  basic_block dest;
	  int probability;
	  gcov_type count;
	  edge e;

653 654 655
	  dest_index = streamer_read_uhwi (ib);
	  probability = (int) streamer_read_hwi (ib);
	  count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
656
		   + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
657
	  edge_flags = streamer_read_uhwi (ib);
658 659 660

	  dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);

H.J. Lu committed
661
	  if (dest == NULL)
662 663 664 665 666 667 668
	    dest = make_new_block (fn, dest_index);

	  e = make_edge (bb, dest, edge_flags);
	  e->probability = probability;
	  e->count = count;
	}

669
      index = streamer_read_hwi (ib);
670 671 672
    }

  p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
673
  index = streamer_read_hwi (ib);
674 675 676 677 678 679
  while (index != -1)
    {
      basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
      bb->prev_bb = p_bb;
      p_bb->next_bb = bb;
      p_bb = bb;
680
      index = streamer_read_hwi (ib);
681 682 683 684 685 686 687 688 689 690 691 692 693
    }
}


/* Read the SSA names array for function FN from DATA_IN using input
   block IB.  */

static void
input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
		 struct function *fn)
{
  unsigned int i, size;

694
  size = streamer_read_uhwi (ib);
695 696
  init_ssanames (fn, size);

697
  i = streamer_read_uhwi (ib);
698 699 700 701 702 703 704 705 706
  while (i)
    {
      tree ssa_name, name;
      bool is_default_def;

      /* Skip over the elements that had been freed.  */
      while (VEC_length (tree, SSANAMES (fn)) < i)
	VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);

707
      is_default_def = (streamer_read_uchar (ib) != 0);
708
      name = stream_read_tree (ib, data_in);
709 710 711
      ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());

      if (is_default_def)
712
	set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
713

714
      i = streamer_read_uhwi (ib);
H.J. Lu committed
715
    }
716 717 718 719 720 721 722 723 724 725 726 727
}


/* Go through all NODE edges and fixup call_stmt pointers
   so they point to STMTS.  */

static void
fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
{
  struct cgraph_edge *cedge;
  for (cedge = node->callees; cedge; cedge = cedge->next_callee)
    cedge->call_stmt = stmts[cedge->lto_stmt_uid];
728 729
  for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
    cedge->call_stmt = stmts[cedge->lto_stmt_uid];
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
}

/* Fixup call_stmt pointers in NODE and all clones.  */

static void
fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
{
  struct cgraph_node *node;

  while (orig->clone_of)
    orig = orig->clone_of;

  fixup_call_stmt_edges_1 (orig, stmts);
  if (orig->clones)
    for (node = orig->clones; node != orig;)
      {
	fixup_call_stmt_edges_1 (node, stmts);
	if (node->clones)
	  node = node->clones;
	else if (node->next_sibling_clone)
	  node = node->next_sibling_clone;
	else
	  {
	    while (node != orig && !node->next_sibling_clone)
	      node = node->clone_of;
	    if (node != orig)
	      node = node->next_sibling_clone;
	  }
      }
}

761 762 763

/* Input the base body of struct function FN from DATA_IN
   using input block IB.  */
764 765

static void
766 767
input_struct_function_base (struct function *fn, struct data_in *data_in,
                            struct lto_input_block *ib)
768
{
769
  struct bitpack_d bp;
770
  int len;
771

772 773 774
  /* Read the static chain and non-local goto save area.  */
  fn->static_chain_decl = stream_read_tree (ib, data_in);
  fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
775

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  /* Read all the local symbols.  */
  len = streamer_read_hwi (ib);
  if (len > 0)
    {
      int i;
      VEC_safe_grow (tree, gc, fn->local_decls, len);
      for (i = 0; i < len; i++)
	{
	  tree t = stream_read_tree (ib, data_in);
	  VEC_replace (tree, fn->local_decls, i, t);
	}
    }

  /* Input the function start and end loci.  */
  fn->function_start_locus = lto_input_location (ib, data_in);
  fn->function_end_locus = lto_input_location (ib, data_in);

  /* Input the current IL state of the function.  */
  fn->curr_properties = streamer_read_uhwi (ib);
795 796

  /* Read all the attributes for FN.  */
797
  bp = streamer_read_bitpack (ib);
798 799 800 801 802
  fn->is_thunk = bp_unpack_value (&bp, 1);
  fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
  fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
  fn->returns_struct = bp_unpack_value (&bp, 1);
  fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
803
  fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
804 805 806 807 808 809 810 811
  fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
  fn->after_inlining = bp_unpack_value (&bp, 1);
  fn->stdarg = bp_unpack_value (&bp, 1);
  fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
  fn->calls_alloca = bp_unpack_value (&bp, 1);
  fn->calls_setjmp = bp_unpack_value (&bp, 1);
  fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
  fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
812
}
813

814

815
/* Read the body of function FN_DECL from DATA_IN using input block IB.  */
816

817 818 819 820 821 822 823 824 825 826
static void
input_function (tree fn_decl, struct data_in *data_in,
		struct lto_input_block *ib)
{
  struct function *fn;
  enum LTO_tags tag;
  gimple *stmts;
  basic_block bb;
  struct cgraph_node *node;
  tree args, narg, oarg;
827

828 829 830 831 832 833 834 835
  fn = DECL_STRUCT_FUNCTION (fn_decl);
  tag = streamer_read_record_start (ib);
  clear_line_info (data_in);

  gimple_register_cfg_hooks ();
  lto_tag_check (tag, LTO_function);

  input_struct_function_base (fn, data_in, ib);
836

837 838
  /* Read all function arguments.  We need to re-map them here to the
     arguments of the merged function declaration.  */
839
  args = stream_read_tree (ib, data_in);
840 841 842 843
  for (oarg = args, narg = DECL_ARGUMENTS (fn_decl);
       oarg && narg;
       oarg = TREE_CHAIN (oarg), narg = TREE_CHAIN (narg))
    {
844
      unsigned ix;
845
      bool res;
846
      res = streamer_tree_cache_lookup (data_in->reader_cache, oarg, &ix);
847 848
      gcc_assert (res);
      /* Replace the argument in the streamer cache.  */
849
      streamer_tree_cache_insert_at (data_in->reader_cache, narg, ix);
850 851 852
    }
  gcc_assert (!oarg && !narg);

853 854 855 856 857 858 859
  /* Read all the SSA names.  */
  input_ssa_names (ib, data_in, fn);

  /* Read the exception handling regions in the function.  */
  input_eh_regions (ib, data_in, fn);

  /* Read the tree of lexical scopes for the function.  */
860
  DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
861 862
  gcc_assert (DECL_INITIAL (fn_decl));
  DECL_SAVED_TREE (fn_decl) = NULL_TREE;
863
  node = cgraph_get_create_node (fn_decl);
864 865

  /* Read all the basic blocks.  */
866
  tag = streamer_read_record_start (ib);
867 868
  while (tag)
    {
869 870
      input_bb (ib, tag, data_in, fn,
		node->count_materialization_scale);
871
      tag = streamer_read_record_start (ib);
872 873 874 875
    }

  /* Fix up the call statements that are mentioned in the callgraph
     edges.  */
876 877 878 879 880 881 882 883 884 885
  set_gimple_stmt_max_uid (cfun, 0);
  FOR_ALL_BB (bb)
    {
      gimple_stmt_iterator gsi;
      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
	{
	  gimple stmt = gsi_stmt (gsi);
	  gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
	}
    }
886 887 888
  stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
  FOR_ALL_BB (bb)
    {
889 890
      gimple_stmt_iterator bsi = gsi_start_bb (bb);
      while (!gsi_end_p (bsi))
891 892
	{
	  gimple stmt = gsi_stmt (bsi);
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
	  /* If we're recompiling LTO objects with debug stmts but
	     we're not supposed to have debug stmts, remove them now.
	     We can't remove them earlier because this would cause uid
	     mismatches in fixups, but we can do it at this point, as
	     long as debug stmts don't require fixups.  */
	  if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
	    {
	      gimple_stmt_iterator gsi = bsi;
	      gsi_next (&bsi);
	      gsi_remove (&gsi, true);
	    }
	  else
	    {
	      gsi_next (&bsi);
	      stmts[gimple_uid (stmt)] = stmt;
	    }
909 910 911 912 913 914 915 916 917 918 919 920
	}
    }

  /* Set the gimple body to the statement sequence in the entry
     basic block.  FIXME lto, this is fairly hacky.  The existence
     of a gimple body is used by the cgraph routines, but we should
     really use the presence of the CFG.  */
  {
    edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
    gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
  }

921 922
  fixup_call_stmt_edges (node, stmts);
  execute_all_ipa_stmt_fixups (node, stmts);
923

H.J. Lu committed
924
  update_ssa (TODO_update_ssa_only_virtuals);
925 926
  free_dominance_info (CDI_DOMINATORS);
  free_dominance_info (CDI_POST_DOMINATORS);
927 928 929 930 931 932 933 934 935 936
  free (stmts);
}


/* Read the body from DATA for function FN_DECL and fill it in.
   FILE_DATA are the global decls and types.  SECTION_TYPE is either
   LTO_section_function_body or LTO_section_static_initializer.  If
   section type is LTO_section_function_body, FN must be the decl for
   that function.  */

H.J. Lu committed
937
static void
938 939 940 941 942
lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
	       const char *data, enum lto_section_type section_type)
{
  const struct lto_function_header *header;
  struct data_in *data_in;
943 944 945
  int cfg_offset;
  int main_offset;
  int string_offset;
946 947 948 949
  struct lto_input_block ib_cfg;
  struct lto_input_block ib_main;

  header = (const struct lto_function_header *) data;
H.J. Lu committed
950
  cfg_offset = sizeof (struct lto_function_header);
951 952 953 954 955 956 957 958 959 960 961 962
  main_offset = cfg_offset + header->cfg_size;
  string_offset = main_offset + header->main_size;

  LTO_INIT_INPUT_BLOCK (ib_cfg,
		        data + cfg_offset,
			0,
			header->cfg_size);

  LTO_INIT_INPUT_BLOCK (ib_main,
			data + main_offset,
			0,
			header->main_size);
H.J. Lu committed
963

964 965 966 967 968 969 970 971 972 973 974
  data_in = lto_data_in_create (file_data, data + string_offset,
				header->string_size, NULL);

  /* Make sure the file was generated by the exact same compiler.  */
  lto_check_version (header->lto_header.major_version,
		     header->lto_header.minor_version);

  if (section_type == LTO_section_function_body)
    {
      struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
      struct lto_in_decl_state *decl_state;
975
      struct cgraph_node *node = cgraph_get_node (fn_decl);
976
      unsigned from;
977

978
      gcc_checking_assert (node);
979 980 981
      push_cfun (fn);
      init_tree_ssa (fn);

982 983 984
      /* We input IL in SSA form.  */
      cfun->gimple_df->in_ssa_p = true;

985 986 987 988 989
      /* Use the function's decl state. */
      decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
      gcc_assert (decl_state);
      file_data->current_decl_state = decl_state;

990
      input_cfg (&ib_cfg, fn, node->count_materialization_scale);
991 992

      /* Set up the struct function.  */
993
      from = VEC_length (tree, data_in->reader_cache->nodes);
994
      input_function (fn_decl, data_in, &ib_main);
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
      /* And fixup types we streamed locally.  */
	{
	  struct streamer_tree_cache_d *cache = data_in->reader_cache;
	  unsigned len = VEC_length (tree, cache->nodes);
	  unsigned i;
	  for (i = len; i-- > from;)
	    {
	      tree t = VEC_index (tree, cache->nodes, i);
	      if (t == NULL_TREE)
		continue;

	      if (TYPE_P (t))
		{
		  gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
		  TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
		  if (TYPE_MAIN_VARIANT (t) != t)
		    {
		      gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
		      TYPE_NEXT_VARIANT (t)
			= TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
		      TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
		    }
		}
	    }
	}
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

      /* Restore decl state */
      file_data->current_decl_state = file_data->global_decl_state;

      pop_cfun ();
    }

  clear_line_info (data_in);
  lto_data_in_delete (data_in);
}


/* Read the body of FN_DECL using DATA.  FILE_DATA holds the global
   decls and types.  */

H.J. Lu committed
1035
void
1036 1037 1038 1039 1040 1041 1042
lto_input_function_body (struct lto_file_decl_data *file_data,
			 tree fn_decl, const char *data)
{
  lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
}


1043 1044
/* Read the physical representation of a tree node with tag TAG from
   input block IB using the per-file context in DATA_IN.  */
1045

1046 1047 1048
static tree
lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
	       enum LTO_tags tag)
1049
{
1050
  /* Instantiate a new tree node.  */
1051
  tree result = streamer_alloc_tree (ib, data_in, tag);
1052 1053 1054 1055

  /* Enter RESULT in the reader cache.  This will make RESULT
     available so that circular references in the rest of the tree
     structure can be resolved in subsequent calls to stream_read_tree.  */
1056
  streamer_tree_cache_append (data_in->reader_cache, result);
1057 1058 1059 1060

  /* Read all the bitfield values in RESULT.  Note that for LTO, we
     only write language-independent bitfields, so no more unpacking is
     needed.  */
1061
  streamer_read_tree_bitfields (ib, result);
1062 1063

  /* Read all the pointer fields in RESULT.  */
1064
  streamer_read_tree_body (ib, data_in, result);
1065 1066 1067 1068 1069 1070 1071 1072 1073

  /* Read any LTO-specific data not read by the tree streamer.  */
  if (DECL_P (result)
      && TREE_CODE (result) != FUNCTION_DECL
      && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
    DECL_INITIAL (result) = stream_read_tree (ib, data_in);

  /* We should never try to instantiate an MD or NORMAL builtin here.  */
  if (TREE_CODE (result) == FUNCTION_DECL)
1074
    gcc_assert (!streamer_handle_as_builtin_p (result));
1075

1076
  /* end_marker = */ streamer_read_uchar (ib);
1077 1078 1079

#ifdef LTO_STREAMER_DEBUG
  /* Remove the mapping to RESULT's original address set by
1080
     streamer_alloc_tree.  */
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  lto_orig_address_remove (result);
#endif

  return result;
}


/* Read a tree from input block IB using the per-file context in
   DATA_IN.  This context is used, for example, to resolve references
   to previously read nodes.  */

tree
lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
{
  enum LTO_tags tag;
  tree result;

1098
  tag = streamer_read_record_start (ib);
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
  gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);

  if (tag == LTO_null)
    result = NULL_TREE;
  else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
    {
      /* If TAG is a reference to an indexable tree, the next value
	 in IB is the index into the table where we expect to find
	 that tree.  */
      result = lto_input_tree_ref (ib, data_in, cfun, tag);
    }
  else if (tag == LTO_tree_pickle_reference)
    {
      /* If TAG is a reference to a previously read tree, look it up in
	 the reader cache.  */
1114
      result = streamer_get_pickled_tree (ib, data_in);
1115 1116 1117 1118 1119
    }
  else if (tag == LTO_builtin_decl)
    {
      /* If we are going to read a built-in function, all we need is
	 the code and class.  */
1120
      result = streamer_get_builtin_tree (ib, data_in);
1121 1122 1123 1124 1125
    }
  else if (tag == lto_tree_code_to_tag (INTEGER_CST))
    {
      /* For integer constants we only need the type and its hi/low
	 words.  */
1126
      result = streamer_read_integer_cst (ib, data_in);
1127 1128 1129 1130 1131 1132 1133 1134
    }
  else
    {
      /* Otherwise, materialize a new node from IB.  */
      result = lto_read_tree (ib, data_in, tag);
    }

  return result;
1135 1136 1137
}


1138 1139 1140
/* Input toplevel asms.  */

void
1141
lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1142 1143 1144 1145 1146
{
  size_t len;
  const char *data = lto_get_section_data (file_data, LTO_section_asm,
					   NULL, &len);
  const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1147
  int string_offset;
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
  struct data_in *data_in;
  struct lto_input_block ib;
  tree str;

  if (! data)
    return;

  string_offset = sizeof (*header) + header->main_size;

  LTO_INIT_INPUT_BLOCK (ib,
			data + sizeof (*header),
			0,
			header->main_size);

  data_in = lto_data_in_create (file_data, data + string_offset,
				header->string_size, NULL);

  /* Make sure the file was generated by the exact same compiler.  */
  lto_check_version (header->lto_header.major_version,
		     header->lto_header.minor_version);

  while ((str = streamer_read_string_cst (data_in, &ib)))
1170
    {
1171
      struct asm_node *node = add_asm_node (str);
1172
      node->order = streamer_read_hwi (&ib) + order_base;
1173 1174
      if (node->order >= symtab_order)
	symtab_order = node->order + 1;
1175
    }
1176 1177 1178 1179 1180 1181 1182 1183

  clear_line_info (data_in);
  lto_data_in_delete (data_in);

  lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
}


1184 1185 1186
/* Initialization for the LTO reader.  */

void
1187
lto_reader_init (void)
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
{
  lto_streamer_init ();
  file_name_hash_table = htab_create (37, hash_string_slot_node,
				      eq_string_slot_node, free);
}


/* Create a new data_in object for FILE_DATA. STRINGS is the string
   table to use with LEN strings.  RESOLUTIONS is the vector of linker
   resolutions (NULL if not using a linker plugin).  */

struct data_in *
lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
		    unsigned len,
		    VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
{
  struct data_in *data_in = XCNEW (struct data_in);
  data_in->file_data = file_data;
  data_in->strings = strings;
  data_in->strings_len = len;
  data_in->globals_resolution = resolutions;
1209
  data_in->reader_cache = streamer_tree_cache_create ();
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

  return data_in;
}


/* Remove DATA_IN.  */

void
lto_data_in_delete (struct data_in *data_in)
{
  VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
1221
  streamer_tree_cache_delete (data_in->reader_cache);
1222 1223 1224
  free (data_in->labels);
  free (data_in);
}