aboutsummaryrefslogtreecommitdiff
path: root/gcc/lto/lto-function-in.c
blob: e7f00ded12d5fc565d9116424910a3da33eded43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
/* Read the gimple representation of a function and it's local
   variables from the memory mapped representation of a a .o file.

   Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
   Contributed by Kenneth Zadeck <zadeck@naturalbridge.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 2, or (at your option)
any later version.

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

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */

#include "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 "varray.h"
#include "hashtab.h"
#include "langhooks.h"
#include "basic-block.h"
#include "tree-iterator.h"
#include "tree-pass.h"
#include "tree-flow.h"
#include "cgraph.h"
#include "function.h"
#include "ggc.h"
#include "diagnostic.h"
#include "except.h"
#include "debug.h"
#include "vec.h"
#include "timevar.h"
#include "dwarf2asm.h"
#include "dwarf2out.h"
#include "output.h"
#include "lto-tags.h"
#include "lto.h"
#include "lto-section-in.h"
#include <ctype.h>
#include "cpplib.h"

static enum tree_code tag_to_expr[LTO_last_tag];

/* The number of flags that are defined for each tree code.  */
static int flags_length_for_code[NUM_TREE_CODES];

struct data_in
{
  tree *field_decls;        /* The field decls.  */
  tree *fn_decls;           /* The function decls.  */
  tree *var_decls;          /* The global or static var_decls.  */
  tree *type_decls;         /* The type_decls.  */
  tree *types;              /* All of the types.  */
  int *local_decls_index;   /* The offsets to decode the local_decls.  */
  int *unexpanded_indexes;  /* A table to reconstruct the unexpanded_vars_list.  */
#ifdef LTO_STREAM_DEBUGGING
  int *local_decls_index_d; /* The offsets to decode the local_decls debug info.  */
#endif
  tree *local_decls;        /* The local var_decls and the parm_decls.  */
  tree *labels;             /* All of the labels.  */
  const char * strings;     /* The string table.  */
  unsigned int strings_len; /* The length of the string table.  */
  /* Number of named labels.  Used to find the index of unnamed labels
     since they share space with the named labels.  */
  unsigned int num_named_labels;  
  const char *current_file;
  int current_line;
#ifdef USE_MAPPED_LOCATION  
  int current_col;
#endif
};



/* This hash table is used to hash the file names in the
   source_location field.  Unlike other structures here, this is a
   persistent structure whose data lives for the entire
   compilation.  */

struct string_slot {
  const char *s;
  unsigned int slot_num;
};


/* Returns a hash code for P.  */

static hashval_t
hash_string_slot_node (const void *p)
{
  const struct string_slot *ds = (const struct string_slot *) p;
  return (hashval_t) htab_hash_string (ds->s);
}


/* Returns nonzero if P1 and P2 are equal.  */

static int
eq_string_slot_node (const void *p1, const void *p2)
{
  const struct string_slot *ds1 =
    (const struct string_slot *) p1;
  const struct string_slot *ds2 =
    (const struct string_slot *) p2;

  return strcmp (ds1->s, ds2->s) == 0;
}

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

static tree
input_expr_operand (struct lto_input_block *, struct data_in *, struct function *, 
                    enum LTO_tags);
static tree
input_local_var (struct lto_input_block *, struct data_in *, struct function *, unsigned int i);


/* Return the next character of input from IB.  Abort if you
   overrun.  */

/* Read the string at LOC from the string table in DATA_IN.  */

static const char * 
input_string_internal (struct data_in *data_in, unsigned int loc, 
		       unsigned int *rlen)
{
  struct lto_input_block str_tab 
    = {data_in->strings, loc, data_in->strings_len};
  unsigned int len = lto_input_uleb128 (&str_tab);
  const char * result;

  *rlen = len;
  gcc_assert (str_tab.p + len <= data_in->strings_len);

  result = (const char *)(data_in->strings + str_tab.p);
  LTO_DEBUG_STRING (result, len);
  return result;
}


/* Read a STRING_CST at LOC from the string table in DATA_IN.  */

static tree
input_string (struct data_in *data_in, unsigned int loc)
{
  unsigned int len;
  const char * ptr = input_string_internal (data_in, loc, &len);
  return build_string (len, ptr);
}


/* Input a real constant of TYPE at LOC.  */

static tree
input_real (struct lto_input_block *ib, struct data_in *data_in, tree type)
{
  unsigned int loc;
  unsigned int len;
  const char * str;
  REAL_VALUE_TYPE value;
  static char buffer[1000];

  LTO_DEBUG_TOKEN ("real");
  loc = lto_input_uleb128 (ib);
  str = input_string_internal (data_in, loc, &len);
  /* Copy over to make sure real_from_string doesn't see peculiar
     trailing characters in the exponent.  */
  memcpy (buffer, str, len);
  buffer[len] = '\0';
  real_from_string (&value, buffer);
  return build_real (type, value);
}


/* Return the next tag in the input block IB.  */

static enum LTO_tags
input_record_start (struct lto_input_block *ib)
{
  enum LTO_tags tag = lto_input_1_unsigned (ib);

#ifdef LTO_STREAM_DEBUGGING
  if (tag)
    LTO_DEBUG_INDENT (tag);
  else
    LTO_DEBUG_WIDE ("U", 0);
#endif    
  return tag;
} 


/* Get the label referenced by the next token in IB.  */

static tree 
get_label_decl (struct data_in *data_in, struct lto_input_block *ib)
{
  int index = lto_input_sleb128 (ib);
  if (index >= 0)
    return data_in->labels[index];
  else
    return data_in->labels[data_in->num_named_labels - index];
}


/* Get the type referenced by the next token in IB.  */

static tree
input_type_ref (struct data_in *data_in, struct lto_input_block *ib)
{
  int index;

  LTO_DEBUG_TOKEN ("type");
  index = lto_input_uleb128 (ib);
  return data_in->types[index];
}

/* Set all of the FLAGS for NODE.  */
#define CLEAROUT (BITS_PER_LTO_FLAGS_TYPE - 1)


/* Read the tree flags for CODE from IB.  */

static lto_flags_type
input_tree_flags (struct lto_input_block *ib, enum tree_code code, bool force)
{
  lto_flags_type flags;

  if (force || TEST_BIT (lto_flags_needed_for, code))
    {
      LTO_DEBUG_TOKEN ("flags");
      flags = lto_input_widest_uint_uleb128 (ib);
      LTO_DEBUG_TREE_FLAGS (code, flags);
    }
  else
    flags = 0;
  return flags;
}


/* Set all of the flag bits inside EXPR by unpacking FLAGS.  */

static void
process_tree_flags (tree expr, lto_flags_type flags)
{
  enum tree_code code = TREE_CODE (expr);
  /* Shift the flags up so that the first flag is at the top of the
     flag word.  */
  flags <<= BITS_PER_LTO_FLAGS_TYPE - flags_length_for_code[code];

#define START_CLASS_SWITCH()              \
  {                                       \
                                          \
    switch (TREE_CODE_CLASS (code))       \
    {

#define START_CLASS_CASE(class)    case class:
#define ADD_CLASS_DECL_FLAG(flag_name)    \
  { expr->decl_common. flag_name = flags >> CLEAROUT; flags <<= 1; }
#define ADD_CLASS_EXPR_FLAG(flag_name)    \
  { expr->base. flag_name = flags >> CLEAROUT; flags <<= 1; }
#define END_CLASS_CASE(class)      break;
#define END_CLASS_SWITCH()                \
    default:                              \
      gcc_unreachable ();                 \
    }


#define START_EXPR_SWITCH()               \
    switch (code)			  \
    {
#define START_EXPR_CASE(code)    case code:
#define ADD_EXPR_FLAG(flag_name) \
  { expr->base. flag_name = (flags >> CLEAROUT); flags <<= 1; }
#define ADD_DECL_FLAG(flag_name) \
  { expr->decl_common. flag_name = flags >> CLEAROUT; flags <<= 1; }
#define ADD_VIS_FLAG(flag_name)  \
  { expr->decl_with_vis. flag_name = (flags >> CLEAROUT); flags <<= 1; }
#define ADD_VIS_FLAG_SIZE(flag_name,size)					\
  { expr->decl_with_vis. flag_name = (flags >> (BITS_PER_LTO_FLAGS_TYPE - size)); flags <<= size; }
#define END_EXPR_CASE(class)      break;
#define END_EXPR_SWITCH()                 \
    default:                              \
      gcc_unreachable ();                 \
    }                                     \
  }

#include "lto-tree-flags.def"

#undef START_CLASS_SWITCH
#undef START_CLASS_CASE
#undef ADD_CLASS_DECL_FLAG
#undef ADD_CLASS_EXPR_FLAG
#undef END_CLASS_CASE
#undef END_CLASS_SWITCH
#undef START_EXPR_SWITCH
#undef START_EXPR_CASE
#undef ADD_EXPR_FLAG
#undef ADD_DECL_FLAG
#undef ADD_VIS_FLAG
#undef ADD_VIS_FLAG_SIZE
#undef END_EXPR_CASE
#undef END_EXPR_SWITCH
}


/* Return the one true copy of STRING.  */

static const char *
canon_file_name (const char *string)
{
  void **slot;
  struct string_slot s_slot;
  s_slot.s = string;

  slot = htab_find_slot (file_name_hash_table, &s_slot, INSERT);
  if (*slot == NULL)
    {
      size_t len = strlen (string);
      char * saved_string = xmalloc (len + 1);
      strcpy (saved_string, string);

      struct string_slot *new_slot
	= xmalloc (sizeof (struct string_slot));

      new_slot->s = saved_string;
      *slot = new_slot;
      return saved_string;
    }
  else
    {
      struct string_slot *old_slot = (struct string_slot *)*slot;
      return old_slot->s;
    }
}


/* Based on the FLAGS, read in a file, a line and a col into the
   fields in DATA_IN.  */

static bool
input_line_info (struct lto_input_block *ib, struct data_in *data_in, 
		 lto_flags_type flags)
{
#ifdef USE_MAPPED_LOCATION  
  if (flags & LTO_SOURCE_FILE)
    {
      unsigned int len;
      if (data_in->current_file)
	linemap_add (line_table, LC_LEAVE, false, NULL, 0);

      LTO_DEBUG_TOKEN ("file");
      data_in->current_file 
	= canon_file_name (input_string_internal (data_in, lto_input_uleb128 (ib), &len));
    }
  if (flags & LTO_SOURCE_LINE)
    {
      LTO_DEBUG_TOKEN ("line");
      data_in->current_line = lto_input_uleb128 (ib);

      if (!(flags & LTO_SOURCE_FILE))
	linemap_line_start (line_table, data_in->current_line, 80);
    }
  if (flags & LTO_SOURCE_FILE)
    linemap_add (line_table, LC_ENTER, false, data_in->current_file, data_in->current_line);

  if (flags & LTO_SOURCE_COL)
    {
      LTO_DEBUG_TOKEN ("col");
      data_in->current_col = lto_input_uleb128 (ib);
    }
#else
  if (flags & LTO_SOURCE_FILE)
    {
      unsigned int len;
      LTO_DEBUG_TOKEN ("file");
      data_in->current_file 
	= input_string_internal (data_in, lto_input_uleb128 (ib), &len);
    }
  if (flags & LTO_SOURCE_LINE)
    {
      LTO_DEBUG_TOKEN ("line");
      data_in->current_line = lto_input_uleb128 (ib);
    }
#endif
  return (flags & LTO_SOURCE_HAS_LOC) != 0;
}


/* Set the line info stored in DATA_IN for NODE.  */

static void
set_line_info (struct data_in *data_in, tree node)
{
#ifdef USE_MAPPED_LOCATION
  if (EXPR_P (node))
    LINEMAP_POSITION_FOR_COLUMN (EXPR_CHECK (node)->exp.locus, line_table, data_in->current_col)
  else if (GIMPLE_STMT_P (node))
    LINEMAP_POSITION_FOR_COLUMN (GIMPLE_STMT_LOCUS (node), line_table, data_in->current_col)
  else if (DECL_P (node))
    LINEMAP_POSITION_FOR_COLUMN (DECL_SOURCE_LOCATION (node), line_table, data_in->current_col)
#else
  if (EXPR_P (node) || GIMPLE_STMT_P (node))
    annotate_with_file_line (node, 
			     canon_file_name (data_in->current_file), 
			     data_in->current_line);
  else if (DECL_P (node))
    {
      DECL_SOURCE_LOCATION (node).file = canon_file_name (data_in->current_file);
      DECL_SOURCE_LOCATION (node).line = data_in->current_line;
    }
#endif
}


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

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


/* Read a node in the gimple tree from IB.  The TAG has already been
   read.  */

static tree
input_expr_operand (struct lto_input_block *ib, struct data_in *data_in, 
		    struct function *fn, enum LTO_tags tag)
{
  enum tree_code code = tag_to_expr[tag];
  tree type = NULL_TREE;
  lto_flags_type flags;
  gcc_assert (code);
  tree result = NULL_TREE;
  bool needs_line_set = false;
  
  if (TEST_BIT (lto_types_needed_for, code))
    type = input_type_ref (data_in, ib);

  flags = input_tree_flags (ib, code, false);

  if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
      || IS_GIMPLE_STMT_CODE_CLASS(TREE_CODE_CLASS (code)))
    needs_line_set = input_line_info (ib, data_in, flags);

  switch (code)
    {
    case COMPLEX_CST:
      {
	tree elt_type = input_type_ref (data_in, ib);

	result = build0 (code, type);
	if (tag == LTO_complex_cst1)
	  {
	    TREE_REALPART (result) = input_real (ib, data_in, elt_type);
	    TREE_IMAGPART (result) = input_real (ib, data_in, elt_type);
	  }
	else
	  {
	    TREE_REALPART (result) = lto_input_integer (ib, elt_type);
	    TREE_IMAGPART (result) = lto_input_integer (ib, elt_type);
	  }
      }
      break;

    case INTEGER_CST:
      result = lto_input_integer (ib, type);
      break;

    case REAL_CST:
      result = input_real (ib, data_in, type);
      break;

    case STRING_CST:
      result = input_string (data_in, lto_input_uleb128 (ib));
      TREE_TYPE (result) = type;
      break;

    case IDENTIFIER_NODE:
      {
	unsigned int len;
	const char * ptr = input_string_internal (data_in, lto_input_uleb128 (ib), &len);
	result = get_identifier_with_length (ptr, len);
      }
      break;

    case VECTOR_CST:
      {
	tree chain = NULL_TREE;
	int len = lto_input_uleb128 (ib);
	tree elt_type = input_type_ref (data_in, ib);

	if (len && tag == LTO_vector_cst1)
	  {
	    int i;
	    tree last 
	      = build_tree_list (NULL_TREE, input_real (ib, data_in, elt_type));
	    chain = last; 
	    for (i = 1; i < len; i++)
	      {
		tree t 
		  = build_tree_list (NULL_TREE, input_real (ib, data_in, elt_type));
		TREE_CHAIN (last) = t;
		last = t;
	      }
	  }
	else
	  {
	    int i;
	    tree last = build_tree_list (NULL_TREE, lto_input_integer (ib, elt_type));
	    chain = last; 
	    for (i = 1; i < len; i++)
	      {
		tree t 
		  = build_tree_list (NULL_TREE, lto_input_integer (ib, elt_type));
		TREE_CHAIN (last) = t;
		last = t;
	      }
	  }
	result = build_vector (type, chain);
      }
      break;

    case CASE_LABEL_EXPR:
      {
	int variant = tag - LTO_case_label_expr0;
	tree op0 = NULL_TREE;
	tree op1 = NULL_TREE;
	
	if (variant & 0x1)
	  op0 = input_expr_operand (ib, data_in, fn, 
				    input_record_start (ib));

	if (variant & 0x2)
	  op1 = input_expr_operand (ib, data_in, fn, 
				    input_record_start (ib));

	result = build3 (code, void_type_node, 
			 op0, op1, get_label_decl (data_in, ib));
      }
      break;

    case CONSTRUCTOR:
      {
	VEC(constructor_elt,gc) *vec = NULL;
	unsigned int len = lto_input_uleb128 (ib);
	
	if (len)
	  {
	    unsigned int i = 0;
	    vec = VEC_alloc (constructor_elt, gc, len);
	    for (i = 0; i < len; i++)
	      {
		tree purpose = NULL_TREE;
		tree value;
		constructor_elt *elt; 
		enum LTO_tags ctag = input_record_start (ib);
		
		if (ctag)
		  purpose = input_expr_operand (ib, data_in, fn, ctag);
		
		value = input_expr_operand (ib, data_in, fn, input_record_start (ib));
		elt = VEC_quick_push (constructor_elt, vec, NULL);
		elt->index = purpose;
		elt->value = value;
	      }
	  }
	result = build_constructor (type, vec);
      }
      break;

    case SSA_NAME:
      result = VEC_index (tree, SSANAMES (fn), lto_input_uleb128 (ib));
      add_referenced_var (SSA_NAME_VAR (result));
      break;

    case CONST_DECL:
      /* Just ignore these, Mark will make them disappear.  */
      break;

    case FIELD_DECL:
      result = data_in->field_decls [lto_input_uleb128 (ib)];
      break;

    case FUNCTION_DECL:
      result = data_in->fn_decls [lto_input_uleb128 (ib)];
      gcc_assert (result);
      break;

    case TYPE_DECL:
      result = data_in->type_decls [lto_input_uleb128 (ib)];
      gcc_assert (result);
      break;

    case VAR_DECL:
    case PARM_DECL:
      if (tag == LTO_var_decl1)
        {
          /* Static or externs are here.  */
          result = data_in->var_decls [lto_input_uleb128 (ib)];
	  varpool_mark_needed_node (varpool_node (result));
        }
      else 
	{
	  /* Locals are here.  */
	  int lv_index = lto_input_uleb128 (ib);
	  result = data_in->local_decls [lv_index];
	  if (result == NULL)
	    {
	      /* Create a context to read the local variable so that
		 it does not disturb the position of the code that is
		 calling for the local variable.  This allows locals
		 to refer to other locals.  */
	      struct lto_input_block lib;

#ifdef LTO_STREAM_DEBUGGING
	      struct lto_input_block *current = lto_debug_context.current_data;
	      struct lto_input_block debug;
	      int current_indent = lto_debug_context.indent;

	      debug.data = current->data;
	      debug.len = current->len;
	      debug.p = data_in->local_decls_index_d[lv_index];

	      lto_debug_context.indent = 0;
	      lto_debug_context.current_data = &debug;
#endif
	      lib.data = ib->data;
	      lib.len = ib->len;
	      lib.p = data_in->local_decls_index[lv_index];

	      result = input_local_var (&lib, data_in, fn, lv_index); 
	      data_in->local_decls [lv_index] = result;

#ifdef LTO_STREAM_DEBUGGING
	      lto_debug_context.indent = current_indent;
	      lto_debug_context.current_data = current;
#endif

	    }
	}
      break;

    case LABEL_DECL:
      result = get_label_decl (data_in, ib);
      break;

    case LABEL_EXPR:
      result = build1 (code, void_type_node, get_label_decl (data_in, ib));
      if (!DECL_CONTEXT (LABEL_EXPR_LABEL (result)))
	DECL_CONTEXT (LABEL_EXPR_LABEL (result)) = fn->decl;
      break;

    case COND_EXPR:
      if (tag == LTO_cond_expr0)
	{
	  tree op0;
	  tree op1;
	  tree op2;
	  op0 = input_expr_operand (ib, data_in, fn, 
				    input_record_start (ib));
	  op1 = input_expr_operand (ib, data_in, fn, 
				    input_record_start (ib));
	  op2 = input_expr_operand (ib, data_in, fn, 
				    input_record_start (ib));
	  result = build3 (code, type, op0, op1, op2);
	}
      else
	{
	  tree op0;
	  op0 = input_expr_operand (ib, data_in, fn, 
				    input_record_start (ib));
	  result = build3 (code, type, op0, NULL, NULL);
	}
      break;
      

    case RESULT_DECL:
      result = DECL_RESULT (current_function_decl);
      add_referenced_var (result);
      break;

    case COMPONENT_REF:
      {
	tree op0;
	tree op1;
	op0 = input_expr_operand (ib, data_in, fn, 
				  input_record_start (ib));
	op1 = input_expr_operand (ib, data_in, fn,
				  input_record_start (ib));
  
	/* Ignore 3 because it can be recomputed.  */
	result = build3 (code, type, op0, op1, NULL_TREE);
      }
      break;

    case CALL_EXPR:
      {
	unsigned int i;
	unsigned int count = lto_input_uleb128 (ib);
	tree op1;
	tree op2 = NULL_TREE;

	/* The call chain.  */
	if (tag == LTO_call_expr1)
	  op2 = input_expr_operand (ib, data_in, fn, 
				    input_record_start (ib));

	/* The callee.  */
	op1 = input_expr_operand (ib, data_in, fn, 
				  input_record_start (ib));

	result = build_vl_exp (code, count);
	CALL_EXPR_FN (result) = op1;
	CALL_EXPR_STATIC_CHAIN (result) = op2;
	for (i = 3; i < count; i++)
	  TREE_OPERAND (result, i) 
	    = input_expr_operand (ib, data_in, fn, 
				  input_record_start (ib));
        TREE_TYPE (result) = type;
      }
      break;

    case BIT_FIELD_REF:
      {
	tree op0;
	tree op1;
	tree op2;
	if (tag == LTO_bit_field_ref1)
	  {
	    op1 = build_int_cst_wide (sizetype, lto_input_uleb128 (ib), 0);
	    op2 = build_int_cst_wide (bitsizetype, lto_input_uleb128 (ib), 0);
	    op0 = input_expr_operand (ib, data_in, fn,
				      input_record_start (ib));
	  }
	else
	  {
	    op0 = input_expr_operand (ib, data_in, fn,
				      input_record_start (ib));
	    op1 = input_expr_operand (ib, data_in, fn,
				      input_record_start (ib));
	    op2 = input_expr_operand (ib, data_in, fn,
				      input_record_start (ib));
	  }
	result = build3 (code, type, op0, op1, op2);
      }
      break;

    case ARRAY_REF:
    case ARRAY_RANGE_REF:
      /* Ignore operands 2 and 3 for ARRAY_REF and ARRAY_RANGE REF
	 because they can be recomputed.  */
      {
	tree op0 = input_expr_operand (ib, data_in, fn, 
				       input_record_start (ib));
	tree op1 = input_expr_operand (ib, data_in, fn,
				       input_record_start (ib));
	result = build4 (code, type, op0, op1, NULL_TREE, NULL_TREE);
      }
      break;

    case ASM_EXPR:
      {
	tree str = input_string (data_in, lto_input_uleb128 (ib));
	tree ins = NULL_TREE;
	tree outs = NULL_TREE;
	tree clobbers = NULL_TREE;
	tree tl;

	tag = input_record_start (ib);
	if (tag)
	  ins = input_expr_operand (ib, data_in, fn, tag); 
	tag = input_record_start (ib);
	if (tag)
	  outs = input_expr_operand (ib, data_in, fn, tag); 
	tag = input_record_start (ib);
	if (tag)
	  clobbers = input_expr_operand (ib, data_in, fn, tag);

	result = build4 (code, void_type_node, str, outs, ins, clobbers);

	for (tl = ASM_OUTPUTS (result); tl; tl = TREE_CHAIN (tl))
	  if (TREE_CODE (TREE_VALUE (tl)) == SSA_NAME)
	    SSA_NAME_DEF_STMT (TREE_VALUE (tl)) = result;
      }
      break;

    case RESX_EXPR:
      result = build1 (code, void_type_node, lto_input_integer (ib, NULL_TREE));
      break;

    case RETURN_EXPR:
      switch (tag) 
	{
	case LTO_return_expr0:
	  result = build1 (code, type, NULL_TREE);
	  break;
	  
	case LTO_return_expr1:
          {
            enum LTO_tags tag = input_record_start (ib);
            tree op0;

            if (tag)
              op0 = input_expr_operand (ib, data_in, fn, tag);
            else
	      {
		op0 = DECL_RESULT (current_function_decl);
		add_referenced_var (op0);
	      }

            result = build1 (code, type, op0);

	    if ((TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
		&& (TREE_CODE (GIMPLE_STMT_OPERAND (op0, 0)) == SSA_NAME))
		SSA_NAME_DEF_STMT (GIMPLE_STMT_OPERAND (op0, 0)) = result;
          }
	  break;
	  
	case LTO_return_expr2:
	  {
	    tree op0 = input_expr_operand (ib, data_in, fn,
					   input_record_start (ib));
	    tree op1 = input_expr_operand (ib, data_in, fn,
					   input_record_start (ib));
	    result = build1 (code, type, 
			     build2 (MODIFY_EXPR, NULL_TREE, op0, op1));
	  }
	  break;

        default:
          gcc_unreachable ();
	}
      break;

    case RANGE_EXPR:
      {
	tree op0 = lto_input_integer (ib, input_type_ref (data_in, ib));
	tree op1 = lto_input_integer (ib, input_type_ref (data_in, ib));
	result = build2 (RANGE_EXPR, sizetype, op0, op1);
      }
      break;

    case GIMPLE_MODIFY_STMT:
      {
	tree op0 = input_expr_operand (ib, data_in, fn, 
				       input_record_start (ib));
	tree op1 = input_expr_operand (ib, data_in, fn,
				       input_record_start (ib));

	result = build_gimple_modify_stmt (op0, op1);
	if (TREE_CODE (op0) == SSA_NAME)
	  SSA_NAME_DEF_STMT (op0) = result;
      }
      break;

    case SWITCH_EXPR:
      {
	unsigned int len = lto_input_uleb128 (ib);
	unsigned int i;
	tree op0 = input_expr_operand (ib, data_in, fn, 
				       input_record_start (ib));
	tree op2 = make_tree_vec (len);
	
	for (i = 0; i < len; ++i)
	  TREE_VEC_ELT (op2, i) 
	    = input_expr_operand (ib, data_in, fn,
				  input_record_start (ib));
	result = build3 (code, type, op0, NULL_TREE, op2);
      }
      break;

    case TREE_LIST:
      {
	unsigned int count = lto_input_uleb128 (ib);
	tree next = NULL;

	result = NULL_TREE;
	while (count--)
	  {
	    tree value;
	    tree purpose;
	    tree elt;
	    enum LTO_tags tag = input_record_start (ib);

	    if (tag)
	      value = input_expr_operand (ib, data_in, fn, tag);
	    else 
	      value = NULL_TREE;
	    tag = input_record_start (ib);
	    if (tag)
	      purpose = input_expr_operand (ib, data_in, fn, tag);
	    else 
	      purpose = NULL_TREE;

	    elt = build_tree_list (purpose, value);
	    if (result)
	      TREE_CHAIN (next) = elt;
	    else
	      /* Save the first one.  */
	      result = elt;
	    next = elt;
	  }
      }
      break;

      /* This is the default case. All of the cases that can be done
	 completely mechanically are done here.  */
#define SET_NAME(a,b)
#define TREE_SINGLE_MECHANICAL_TRUE
#define MAP_EXPR_TAG(expr,tag) case expr:
#include "lto-tree-tags.def"
#undef MAP_EXPR_TAG
#undef TREE_SINGLE_MECHANICAL_TRUE
#undef SET_NAME
      {
	tree ops[7];
	int len = TREE_CODE_LENGTH (code);
	int i;
	for (i = 0; i<len; i++)
	  ops[i] = input_expr_operand (ib, data_in, fn, 
				       input_record_start (ib));
	switch (len)
	  {
	  case 0:
	    result = build0 (code, type);
	    break;
	  case 1:
	    result = build1 (code, type, ops[0]);
	    break;
	  case 2:
	    result = build2 (code, type, ops[0], ops[1]);
	    break;
	  case 3:
	    result = build3 (code, type, ops[0], ops[1], ops[2]);
	    break;
	  case 4:
	    result = build4 (code, type, ops[0], ops[1], ops[2], ops[3]);
	    break;
	  case 5:
	    result = build5 (code, type, ops[0], ops[1], ops[2], ops[3], 
			     ops[4]);
	    break;
            /* No 'case 6'.  */
	  case 7:
	    result = build7 (code, type, ops[0], ops[1], ops[2], ops[3], 
			     ops[4], ops[5], ops[6]);
	    break;
	  default:
	    gcc_unreachable ();
	  }
      }
      break;
      /* This is the error case, these are type codes that will either
	 never happen or that we have not gotten around to dealing
	 with are here.  */
    case BIND_EXPR:
    case BLOCK:
    case CATCH_EXPR:
    case EH_FILTER_EXPR:
    case NAME_MEMORY_TAG:
    case OMP_CONTINUE:
    case OMP_CRITICAL:
    case OMP_FOR:
    case OMP_MASTER:
    case OMP_ORDERED:
    case OMP_PARALLEL:
    case OMP_RETURN:
    case OMP_SECTIONS:
    case OMP_SINGLE:
    case STRUCT_FIELD_TAG:
    case SYMBOL_MEMORY_TAG:
    case TARGET_MEM_REF:
    case TRY_CATCH_EXPR:
    case TRY_FINALLY_EXPR:
    default:
      /* We cannot have forms that are not explicity handled.  So when
	 this is triggered, there is some form that is not being
	 output.  */
      gcc_unreachable ();
    }

  LTO_DEBUG_UNDENT();
  if (flags)
    process_tree_flags (result, flags);

  if (needs_line_set)
    set_line_info (data_in, result);

  /* It is not enought to just put the flags back as we serialized
     them.  There are side effects to the buildN functions which play
     with the flags to the point that we just have to call this here
     to get it right.  */
  if (code == ADDR_EXPR)
    {
      tree x;

      /* Following tree-cfg.c:verify_expr: skip any references and
	 ensure that any variable used as a prefix is marked
	 addressable.  */
      for (x = TREE_OPERAND (result, 0);
	   handled_component_p (x);
	   x = TREE_OPERAND (x, 0))
	;

      if (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL)
	TREE_ADDRESSABLE (x) = 1;
      else if (TREE_CODE (x) == FUNCTION_DECL)
	cgraph_mark_needed_node (cgraph_node (x));

      recompute_tree_invariant_for_addr_expr (result);
    }
  return result;
}


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

static void
input_globals (struct lto_header * header,
	      lto_info_fd *fd,
	      lto_context *context,	
	      struct data_in *data_in, 
	      lto_ref in_field_decls[],
	      lto_ref in_fn_decls[],
	      lto_ref in_var_decls[],
	      lto_ref in_type_decls[],
	      lto_ref in_types[])
{
  int i;
  data_in->field_decls = xcalloc (header->num_field_decls, sizeof (tree*));
  data_in->fn_decls    = xcalloc (header->num_fn_decls, sizeof (tree*));
  data_in->var_decls   = xcalloc (header->num_var_decls, sizeof (tree*));
  data_in->type_decls  = xcalloc (header->num_type_decls, sizeof (tree*));
  data_in->types       = xcalloc (header->num_types, sizeof (tree*));

  for (i=0; i<header->num_field_decls; i++)
    data_in->field_decls[i] 
      = lto_resolve_field_ref (fd, context, &in_field_decls[i]);

  for (i=0; i<header->num_fn_decls; i++)
    data_in->fn_decls[i] 
      = lto_resolve_fn_ref (fd, context, &in_fn_decls[i]);

  for (i=0; i<header->num_var_decls; i++)
    data_in->var_decls[i] 
      = lto_resolve_var_ref (fd, context, &in_var_decls[i]);

  for (i=0; i<header->num_type_decls; i++)
    data_in->type_decls[i] 
      = lto_resolve_typedecl_ref (fd, context, &in_type_decls[i]);

  for (i=0; i<header->num_types; i++)
    data_in->types[i] = lto_resolve_type_ref (fd, context, &in_types[i]);
}


/* Load NAMED_COUNT named labels and constuct UNNAMED_COUNT unnamed
   labels from DATA segment SIZE bytes long using DATA_IN.  */

static void 
input_labels (struct lto_input_block *ib, struct data_in *data_in, 
	      unsigned int named_count, unsigned int unnamed_count)
{
  unsigned int i;

  clear_line_info (data_in);
  /* The named and unnamed labels share the same array.  In the lto
     code, the unnamed labels have a negative index.  Their position
     in the array can be found by subtracting that index from the
     number of named labels.  */
  data_in->labels = xcalloc (named_count + unnamed_count, sizeof (tree*));
  for (i = 0; i < named_count; i++)
    {
      unsigned int name_index = lto_input_uleb128 (ib);
      unsigned int len;
      const char *s = input_string_internal (data_in, name_index, &len);
      tree name = get_identifier_with_length (s, len);
      data_in->labels[i] = build_decl (LABEL_DECL, name, void_type_node);
    }

  for (i = 0; i < unnamed_count; i++)
    data_in->labels[i + named_count] 
      = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
 }


/* Input the local var index table.  */


static void
input_local_vars_index (struct lto_input_block *ib, struct data_in *data_in, 
			unsigned int count)
{
  unsigned int i;
  data_in->local_decls_index = xcalloc (count, sizeof (unsigned int));
#ifdef LTO_STREAM_DEBUGGING
  data_in->local_decls_index_d = xcalloc (count, sizeof (unsigned int));
#endif

  for (i = 0; i < count; i++)
    {
      data_in->local_decls_index[i] = lto_input_uleb128 (ib); 
#ifdef LTO_STREAM_DEBUGGING
      data_in->local_decls_index_d[i] = lto_input_uleb128 (ib); 
#endif
    }
}


/* Input local var I for FN from IB.  */

static tree
input_local_var (struct lto_input_block *ib, struct data_in *data_in, 
		 struct function *fn, unsigned int i)
{
  enum LTO_tags tag;
  unsigned int variant;
  bool is_var;
  unsigned int name_index;
  tree name;
  tree type;
  lto_flags_type flags;
  tree result;
  tree context;

  /* The line number info needs to be reset for each local var since
     they are read in random order.  */
  clear_line_info (data_in);

  tag = input_record_start (ib);
  variant = tag & 0xF;
  is_var = ((tag & 0xFFF0) == LTO_local_var_decl_body0);
  
  name_index = lto_input_uleb128 (ib);
  if (name_index)
    {
      unsigned int len;
      const char *s = input_string_internal (data_in, name_index, &len);
      name = get_identifier_with_length (s, len);
    }
  else 
    name = NULL_TREE;
  
  type = input_type_ref (data_in, ib);
  gcc_assert (type);
  
  if (is_var)
    result = build_decl (VAR_DECL, name, type);
  else
    result = build_decl (PARM_DECL, name, type);

  data_in->local_decls[i] = result;
  
  if (is_var)
    {
      int index;

      LTO_DEBUG_INDENT_TOKEN ("init");
      tag = input_record_start (ib);
      if (tag)
	DECL_INITIAL (result) = input_expr_operand (ib, data_in, fn, tag);

      LTO_DEBUG_INDENT_TOKEN ("unexpanded index");
      index = lto_input_sleb128 (ib);
      if (index != -1)
	data_in->unexpanded_indexes[index] = i;
    }
  else
    {
      DECL_ARG_TYPE (result) = input_type_ref (data_in, ib);
      LTO_DEBUG_TOKEN ("chain");
      tag = input_record_start (ib);
      if (tag)
	TREE_CHAIN (result) = input_expr_operand (ib, data_in, fn, tag);
      else 
	TREE_CHAIN (result) = NULL_TREE;
    }


  flags = input_tree_flags (ib, 0, true);
  if (input_line_info (ib, data_in, flags))
    set_line_info (data_in, result);


  LTO_DEBUG_TOKEN ("context");
  context = input_expr_operand (ib, data_in, fn, input_record_start (ib));
  if (TYPE_P (context))
    DECL_CONTEXT (result) = TYPE_NAME (context);
  else
    DECL_CONTEXT (result) = context;
  
  LTO_DEBUG_TOKEN ("align");
  DECL_ALIGN (result) = lto_input_uleb128 (ib);
  LTO_DEBUG_TOKEN ("size");
  DECL_SIZE (result) = input_expr_operand (ib, data_in, fn, input_record_start (ib));

  if (variant & 0x1)
    {
      LTO_DEBUG_TOKEN ("attributes");
      DECL_ATTRIBUTES (result) 
	= input_expr_operand (ib, data_in, fn, input_record_start (ib));
    }
  if (variant & 0x2)
    DECL_SIZE_UNIT (result) 
      = input_expr_operand (ib, data_in, fn, input_record_start (ib));
  if (variant & 0x4)
    SET_DECL_DEBUG_EXPR (result, 
			 input_expr_operand (ib, data_in, fn, 
					     input_record_start (ib)));
  if (variant & 0x8)
    DECL_ABSTRACT_ORIGIN (result) 
      = input_expr_operand (ib, data_in, fn, input_record_start (ib));
  
  process_tree_flags (result, flags);
  LTO_DEBUG_UNDENT();

  return result;
}


/* Load COUNT local var_decls and parm_decls from a DATA segment SIZE
   bytes long using DATA_IN.  */

static void 
input_local_vars (struct lto_input_block *ib, struct data_in *data_in, 
		  struct function *fn, unsigned int count)
{
  int i;
  unsigned int tag;

  data_in->unexpanded_indexes = xcalloc (count, sizeof (int));
  data_in->local_decls = xcalloc (count, sizeof (tree*));

  memset (data_in->unexpanded_indexes, -1, count * sizeof (int));

  /* Recreate the unexpanded_var_list.  Put the statics at the end.*/
  fn->unexpanded_var_list = NULL;
  LTO_DEBUG_TOKEN ("local statics");
  tag = input_record_start (ib);
  
  while (tag)
    {
      tree var = input_expr_operand (ib, data_in, fn, tag);
      fn->unexpanded_var_list 
	= tree_cons (NULL_TREE, var, fn->unexpanded_var_list);

      if (lto_input_uleb128 (ib))
	DECL_CONTEXT (var) = fn->decl;
	
      /* DECL_INITIAL.  */
      tag = input_record_start (ib);
      if (tag)
	DECL_INITIAL (var) = input_expr_operand (ib, data_in, fn, tag);

      /* Statics never have external visibility.  */
      DECL_EXTERNAL (var) = 0;

      /* Next static.  */
      tag = input_record_start (ib);
    }

  LTO_DEBUG_TOKEN ("local vars");
  for (i = 0; i < (int)count; i++)
    /* Some local decls may have already been read in if they are used
       as part of a previous local_decl.  */
    if (!data_in->local_decls[i])
      {
#ifdef LTO_STREAM_DEBUGGING
	((struct lto_input_block *)lto_debug_context.current_data)->p 
	  = data_in->local_decls_index_d[i]; 
#endif
	ib->p = data_in->local_decls_index[i];
	input_local_var (ib, data_in, fn, i);
      }

  /* Add the regular locals in the proper order.  */
  for (i = count - 1; i >= 0; i--)
    if (data_in->unexpanded_indexes[i] != -1)
      fn->unexpanded_var_list 
	= tree_cons (NULL_TREE, 
		     data_in->local_decls[data_in->unexpanded_indexes[i]],
		     fn->unexpanded_var_list);

  free (data_in->unexpanded_indexes);
  data_in->unexpanded_indexes = NULL;
}


/* Read the exception table.  */

static void
input_eh_regions (struct lto_input_block *ib, 
		  struct function *fn ATTRIBUTE_UNUSED, 
		  struct data_in *data_in ATTRIBUTE_UNUSED)
{
  /* Not ready to read exception records yet.  */
  lto_input_uleb128 (ib);
}


/* Make a new basic block at INDEX in 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);
  bb->il.tree = GGC_CNEW (struct tree_bb_info);
  n_basic_blocks_for_function (fn)++;
  bb->flags = 0;
  set_bb_stmt_list (bb, alloc_stmt_list ());
  return bb;
}


/* Set up the cfg for THIS_FUN.  */

static void 
input_cfg (struct lto_input_block *ib, struct function *fn)
{
  unsigned int bb_count;
  basic_block p_bb;
  unsigned int i;
  int index;

  init_empty_tree_cfg_for_function (fn);
  init_ssa_operands ();

  LTO_DEBUG_TOKEN ("lastbb");
  bb_count = lto_input_uleb128 (ib);

  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)))
    VEC_safe_grow_cleared (basic_block, gc, 
			   label_to_block_map_for_function (fn), bb_count);

  LTO_DEBUG_TOKEN ("bbindex");
  index = lto_input_sleb128 (ib);
  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);

      LTO_DEBUG_TOKEN ("edgecount");
      edge_count = lto_input_uleb128 (ib);

      /* Connect up the cfg.  */
      for (i = 0; i < edge_count; i++)
	{
	  unsigned int dest_index;
	  unsigned int edge_flags;
	  basic_block dest;

	  LTO_DEBUG_TOKEN ("dest");
	  dest_index = lto_input_uleb128 (ib);
	  LTO_DEBUG_TOKEN ("eflags");
	  edge_flags = lto_input_uleb128 (ib);
	  dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);

	  if (dest == NULL) 
	    dest = make_new_block (fn, dest_index);
	  make_edge (bb, dest, edge_flags);
	}

      LTO_DEBUG_TOKEN ("bbindex");
      index = lto_input_sleb128 (ib);
    }

  p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
  LTO_DEBUG_TOKEN ("bbchain");
  index = lto_input_sleb128 (ib);
  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;
      LTO_DEBUG_TOKEN ("bbchain");
      index = lto_input_sleb128 (ib);
    }
}


/* Input the next phi function for BB.  */

static tree
input_phi (struct lto_input_block *ib, basic_block bb, 
	   struct data_in *data_in, struct function *fn)
{
  lto_flags_type flags = input_tree_flags (ib, PHI_NODE, false);

  tree phi_result = VEC_index (tree, SSANAMES (fn), lto_input_uleb128 (ib));
  int len = EDGE_COUNT (bb->preds);
  int i;
  tree result = create_phi_node (phi_result, bb);

  SSA_NAME_DEF_STMT (phi_result) = result;

  /* We have to go thru a lookup process here because the preds in the
     reconstructed graph are generally in a different order than they
     were in the original program.  */
  for (i = 0; i < len; i++)
    {
      tree def = input_expr_operand (ib, data_in, fn, input_record_start (ib));
      int src_index = lto_input_uleb128 (ib);
      basic_block sbb = BASIC_BLOCK_FOR_FUNCTION (fn, src_index);
      
      edge e = NULL;
      int j;
      
      for (j = 0; j < len; j++)
	if (EDGE_PRED (bb, j)->src == sbb)
	  {
	    e = EDGE_PRED (bb, j);
	    break;
	  }

      add_phi_arg (result, def, e); 
    }

  if (flags)
    process_tree_flags (result, flags);

  LTO_DEBUG_UNDENT();

  return result;
}


/* Read in the ssa_names array from IB.  */

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

  init_ssanames (fn, size);
  i = lto_input_uleb128 (ib);

  while (i)
    {
      tree ssa_name;
      tree name;
      lto_flags_type flags;

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

      name = input_expr_operand (ib, data_in, fn, input_record_start (ib));
      ssa_name = make_ssa_name (fn, name, build_empty_stmt ());

      flags = input_tree_flags (ib, 0, true);
      process_tree_flags (ssa_name, flags);
      if (SSA_NAME_IS_DEFAULT_DEF (ssa_name))
	set_default_def (SSA_NAME_VAR (ssa_name), ssa_name);
      i = lto_input_uleb128 (ib);
    } 
}

 
/* Read in the next basic block.  */

static void
input_bb (struct lto_input_block *ib, enum LTO_tags tag, 
	  struct data_in *data_in, struct function *fn)
{
  unsigned int index;
  basic_block bb;
  block_stmt_iterator bsi;
  unsigned int stmt_num;

  LTO_DEBUG_TOKEN ("bbindex");
  index = lto_input_uleb128 (ib);
  bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);

  /* LTO_bb1 has stmts, LTO_bb0 does not.  */
  if (tag == LTO_bb0)
    {
      LTO_DEBUG_UNDENT();
      return;
    }

  bsi = bsi_start (bb);
  LTO_DEBUG_INDENT_TOKEN ("stmt");
  stmt_num = lto_input_uleb128 (ib);
  while (stmt_num)
    {
      tree stmt;

      tag = input_record_start (ib);
      stmt = input_expr_operand (ib, data_in, fn, tag);
      bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
      LTO_DEBUG_INDENT_TOKEN ("stmt");
      stmt_num = lto_input_uleb128 (ib);
      /* FIXME, add code to handle the exception.  */
    }

  LTO_DEBUG_INDENT_TOKEN ("phi");  
  stmt_num = lto_input_uleb128 (ib);
  while (stmt_num)
    {
      tag = input_record_start (ib);
      input_phi (ib, bb, data_in, fn);
      LTO_DEBUG_INDENT_TOKEN ("phi");
      stmt_num = lto_input_uleb128 (ib);
    }

  LTO_DEBUG_UNDENT();
}


/* Fill in the body of FN_DECL.  */

static void
input_function (tree fn_decl, struct data_in *data_in, 
		struct lto_input_block *ib)
{
  struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
  enum LTO_tags tag = input_record_start (ib);

  DECL_INITIAL (fn_decl) = DECL_SAVED_TREE (fn_decl) = make_node (BLOCK);
  BLOCK_ABSTRACT_ORIGIN (DECL_SAVED_TREE (fn_decl)) = fn_decl;
  clear_line_info (data_in);

  tree_register_cfg_hooks ();
  gcc_assert (tag == LTO_function);

  input_eh_regions (ib, fn, data_in);

  LTO_DEBUG_INDENT_TOKEN ("decl_arguments");
  tag = input_record_start (ib);
  if (tag)
    DECL_ARGUMENTS (fn_decl) = input_expr_operand (ib, data_in, fn, tag); 

  LTO_DEBUG_INDENT_TOKEN ("decl_context");
  tag = input_record_start (ib);
  if (tag)
    DECL_CONTEXT (fn_decl) = input_expr_operand (ib, data_in, fn, tag); 

  tag = input_record_start (ib);
  while (tag)
    {
      input_bb (ib, tag, data_in, fn);
      tag = input_record_start (ib);
    }

  LTO_DEBUG_UNDENT();
}


/* Fill in the body of VAR.  */

static void
input_constructor (tree var, struct data_in *data_in, 
		   struct lto_input_block *ib)
{
  enum LTO_tags tag;

  clear_line_info (data_in);
  LTO_DEBUG_INDENT_TOKEN ("init");
  tag = input_record_start (ib);
  DECL_INITIAL (var) = input_expr_operand (ib, data_in, NULL, tag);
}


static bool initialized_local = false;


/* Static initialization for the lto reader.  */

static void
lto_static_init_local (void)
{
  if (initialized_local)
    return;

  initialized_local = true;

  /* Initialize the expression to tag mapping.  */
#define MAP_EXPR_TAG(expr,tag)   tag_to_expr [tag] = expr;
#define MAP_EXPR_TAGS(expr,tag,count) \
  {                                   \
    int i;                            \
    for (i=0; i<count; i++)           \
      tag_to_expr [tag + i] = expr;   \
  }
#define TREE_MULTIPLE
#define TREE_SINGLE_MECHANICAL_TRUE
#define TREE_SINGLE_MECHANICAL_FALSE
#define SET_NAME(a,b)
#include "lto-tree-tags.def"

#undef MAP_EXPR_TAG
#undef MAP_EXPR_TAGS
#undef TREE_MULTIPLE
#undef TREE_SINGLE_MECHANICAL_TRUE
#undef TREE_SINGLE_MECHANICAL_FALSE
#undef SET_NAME
  /* Initialize flags_length_for_code.  */


#define START_CLASS_SWITCH()                  \
  {                                           \
    int code;				      \
    for (code=0; code<NUM_TREE_CODES; code++) \
      {                                       \
	/* The LTO_SOURCE_LOC_BITS leaves room for file and line number for exprs.  */ \
        flags_length_for_code[code] = LTO_SOURCE_LOC_BITS; \
                                              \
        switch (TREE_CODE_CLASS (code))       \
          {

#define START_CLASS_CASE(class)    case class:
#define ADD_CLASS_DECL_FLAG(flag_name)    flags_length_for_code[code]++;
#define ADD_CLASS_EXPR_FLAG(flag_name)    flags_length_for_code[code]++;
#define END_CLASS_CASE(class)      break;
#define END_CLASS_SWITCH()                    \
          default:                            \
	    fprintf (stderr, "no declaration for TREE CODE CLASS for = %s(%d)\n", \
                     tree_code_name[code], code);                                 \
            gcc_unreachable ();               \
          }


#define START_EXPR_SWITCH()                   \
        switch (code)			      \
          {
#define START_EXPR_CASE(code)    case code:
#define ADD_EXPR_FLAG(flag_name)           flags_length_for_code[code]++;
#define ADD_DECL_FLAG(flag_name)           flags_length_for_code[code]++;
#define ADD_VIS_FLAG(flag_name)            flags_length_for_code[code]++;
#define ADD_VIS_FLAG_SIZE(flag_name,size)  flags_length_for_code[code] += size;
#define END_EXPR_CASE(class)      break;
#define END_EXPR_SWITCH()                     \
          default:                            \
	    fprintf (stderr, "no declaration for TREE CODE = %s(%d)\n", \
                     tree_code_name[code], code);		        \
            gcc_unreachable ();               \
          }                                   \
      }					      \
  }

#include "lto-tree-flags.def"

#undef START_CLASS_SWITCH
#undef START_CLASS_CASE
#undef ADD_CLASS_DECL_FLAG
#undef ADD_CLASS_EXPR_FLAG
#undef END_CLASS_CASE
#undef END_CLASS_SWITCH
#undef START_EXPR_SWITCH
#undef START_EXPR_CASE
#undef ADD_EXPR_FLAG
#undef ADD_DECL_FLAG
#undef ADD_VIS_FLAG
#undef ADD_VIS_FLAG_SIZE
#undef END_EXPR_CASE
#undef END_EXPR_SWITCH

  /* Verify that lto_flags_type is wide enough.  */
  {
    int code;
    for (code = 0; code < NUM_TREE_CODES; code++)
      gcc_assert (flags_length_for_code[code] <= BITS_PER_LTO_FLAGS_TYPE);
  }
  
  lto_static_init ();
  tree_register_cfg_hooks ();

  file_name_hash_table
    = htab_create (37, hash_string_slot_node, eq_string_slot_node, free);
}


/* Read the body form DATA for tree T and fill it in.
   FD and CONTEXT are magic cookies used to resolve global decls and
   types.  IN_FUNCTION should be true if DATA describes the
   body of a function, or false otherwise.  */
static void 
lto_read_body (lto_info_fd *fd,
	       lto_context *context,
	       tree t,
	       const void *data,
	       bool in_function)
{
  struct lto_header * header 
    = (struct lto_header *) data;
  struct data_in data_in;

  int32_t fields_offset = sizeof (struct lto_header); 
  int32_t fns_offset 
    = fields_offset + (header->num_field_decls * sizeof (lto_ref));
  int32_t vars_offset 
    = fns_offset + (header->num_fn_decls * sizeof (lto_ref));
  int32_t type_decls_offset 
    = vars_offset + (header->num_var_decls * sizeof (lto_ref));
  int32_t types_offset 
    = type_decls_offset + (header->num_type_decls * sizeof (lto_ref));
  int32_t named_label_offset 
    = types_offset + (header->num_types * sizeof (lto_ref));
  int32_t ssa_names_offset 
    = named_label_offset + header->named_label_size;
  int32_t cfg_offset 
    = ssa_names_offset + header->ssa_names_size;
  int32_t local_decls_index_offset = cfg_offset + header->cfg_size;
  int32_t local_decls_offset = local_decls_index_offset + header->local_decls_index_size;
  int32_t main_offset = local_decls_offset + header->local_decls_size;
  int32_t string_offset = main_offset + header->main_size;

#ifdef LTO_STREAM_DEBUGGING
  int32_t debug_decl_index_offset = string_offset + header->string_size;
  int32_t debug_decl_offset = debug_decl_index_offset + header->debug_decl_index_size;
  int32_t debug_label_offset = debug_decl_offset + header->debug_decl_size;
  int32_t debug_ssa_names_offset = debug_label_offset + header->debug_label_size;
  int32_t debug_cfg_offset = debug_ssa_names_offset + header->debug_ssa_names_size;
  int32_t debug_main_offset = debug_cfg_offset + header->debug_cfg_size;

  struct lto_input_block debug_decl_index 
    = {data + debug_decl_index_offset, 0, header->debug_decl_index_size};
  struct lto_input_block debug_decl 
    = {data + debug_decl_offset, 0, header->debug_decl_size};
  struct lto_input_block debug_label 
    = {data + debug_label_offset, 0, header->debug_label_size};
  struct lto_input_block debug_ssa_names 
    = {data + debug_ssa_names_offset, 0, header->debug_ssa_names_size};
  struct lto_input_block debug_cfg 
    = {data + debug_cfg_offset, 0, header->debug_cfg_size};
  struct lto_input_block debug_main 
    = {data + debug_main_offset, 0, header->debug_main_size};

  lto_debug_context.out = lto_debug_in_fun;
  lto_debug_context.indent = 0;
#endif

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

  struct lto_input_block ib_named_labels 
    = {data + named_label_offset, 0, header->named_label_size};
  struct lto_input_block ib_ssa_names 
    = {data + ssa_names_offset, 0, header->ssa_names_size};
  struct lto_input_block ib_cfg 
    = {data + cfg_offset, 0, header->cfg_size};
  struct lto_input_block ib_local_decls_index 
    = {data + local_decls_index_offset, 0, header->local_decls_index_size};
  struct lto_input_block ib_local_decls 
    = {data + local_decls_offset, 0, header->local_decls_size};
  struct lto_input_block ib_main 
    = {data + main_offset, 0, header->main_size};

  memset (&data_in, 0, sizeof (struct data_in));
  data_in.strings            = data + string_offset;
  data_in.strings_len        = header->string_size;

  lto_static_init_local ();

  /* No upward compatibility here.  */
  gcc_assert (header->major_version == LTO_major_version);
  gcc_assert (header->minor_version == LTO_minor_version);

  input_globals (header, fd, context, &data_in, 
		 in_field_decls, in_fn_decls, 
		 in_var_decls, in_type_decls, in_types);

  if (in_function)
    {
      struct function *fn = DECL_STRUCT_FUNCTION (t);
      push_cfun (fn);
      init_tree_ssa (fn);
      data_in.num_named_labels = header->num_named_labels;

#ifdef LTO_STREAM_DEBUGGING
      lto_debug_context.current_data = &debug_label;
#endif
      input_labels (&ib_named_labels, &data_in, 
		    header->num_named_labels, header->num_unnamed_labels);
      
#ifdef LTO_STREAM_DEBUGGING
      lto_debug_context.current_data = &debug_decl_index;
#endif
      input_local_vars_index (&ib_local_decls_index, &data_in, header->num_local_decls);
      
#ifdef LTO_STREAM_DEBUGGING
      lto_debug_context.current_data = &debug_decl;
#endif
      input_local_vars (&ib_local_decls, &data_in, fn, header->num_local_decls);
      
#ifdef LTO_STREAM_DEBUGGING
      lto_debug_context.current_data = &debug_ssa_names;
#endif
      input_ssa_names (&ib_ssa_names, &data_in, fn);
      
#ifdef LTO_STREAM_DEBUGGING
      lto_debug_context.current_data = &debug_cfg;
#endif
      input_cfg (&ib_cfg, fn);

      /* Ensure that all our variables have annotations attached to them
	 so building SSA doesn't choke.  */
      {
	int i;

	for (i = 0; i < header->num_var_decls; i++)
	  add_referenced_var (data_in.var_decls[i]);
	for (i =0; i < header->num_local_decls; i++)
	  add_referenced_var (data_in.local_decls[i]);
      }

#ifdef LTO_STREAM_DEBUGGING
      lto_debug_context.current_data = &debug_main;
#endif
      /* Set up the struct function.  */
      input_function (t, &data_in, &ib_main);

      /* We should now be in SSA.  */
      cfun->gimple_df->in_ssa_p = true;
      /* Fill in properties we know hold for the rebuilt CFG.  */
      cfun->curr_properties = PROP_ssa;

      pop_cfun ();
    }
  else 
    {
#ifdef LTO_STREAM_DEBUGGING
      lto_debug_context.current_data = &debug_main;
#endif
      input_constructor (t, &data_in, &ib_main);
    }

  clear_line_info (&data_in);
  free (data_in.field_decls);
  free (data_in.fn_decls);
  free (data_in.var_decls);
  free (data_in.type_decls);
  free (data_in.types);
  if (in_function)
    {
      free (data_in.labels);
      free (data_in.local_decls_index);
      free (data_in.local_decls_index_d);
    }
}


/* Read in FN_DECL using DATA.  FD and CONTEXT are magic cookies.  */

void 
lto_read_function_body (lto_info_fd *fd,
			lto_context *context,
			tree fn_decl,
			const void *data)
{
  current_function_decl = fn_decl;
  lto_read_body (fd, context, fn_decl, data, true);
}


/* Read in VAR_DECL using DATA.  FD and CONTEXT are magic cookies.  */

void 
lto_read_var_init (lto_info_fd *fd,
		   lto_context *context,
		   tree var_decl,
		   const void *data)
{
  lto_read_body (fd, context, var_decl, data, false);
}