aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/l-ipo.c
blob: c9e1f492f161a4b486bf159929fb0dfaa156a3b7 (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
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
/* Copyright (C) 2009. Free Software Foundation, Inc.
   Contributed by Xinliang David Li (davidxl@google.com) and
                  Raksit Ashok  (raksit@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 "tree.h"
#include "is-a.h"
#include "predict.h"
#include "function.h"
#include "basic-block.h"
#include "stor-layout.h"
#include "pointer-set.h"
#include "stringpool.h"
#include "c-family/c-common.h"
#include "toplev.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include "diagnostic.h"
#include "debug.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "gimple-expr.h"
#include "gimple.h"
#include "gimple-iterator.h"
#include "cgraph.h"
#include "l-ipo.h"
#include "coverage.h"
#include "gcov-io.h"
#include "timevar.h"
#include "vec.h"
#include "params.h"
#include "rtl.h"
#include "varasm.h"

unsigned ggc_total_memory; /* in KB */

struct GTY(()) saved_module_scope
{
  vec<tree, va_gc> *module_decls;
  unsigned module_id;
};

static GTY (()) struct saved_module_scope *current_module_scope;
static GTY ((param_is (saved_module_scope))) htab_t saved_module_scope_map;
static int primary_module_last_funcdef_no = 0;
/* Function id space for each module are qualified by the module id. After all the files
   are parsed, we need to reset the funcdef_no to the max value from all module so that
   the function clones do not assigned with ids colliding with some other orignal function
   in the same module.  */
static int max_funcdef_no = 0;
static location_t primary_module_last_loc;
/* Primary module pending templates.  */
/* Referenced asm ids in primary module.  */
static GTY (()) vec<tree, va_gc> *referenced_asm_ids = NULL;
bool parser_parsing_start = false;
/* Nonzero if we're done parsing and into end-of-file activities.  */
int at_eof;

static int aggr_has_equiv_id (tree t1, tree t2);

/* Module scope hash function.  */

static hashval_t
htab_module_scope_hash (const void *ent)
{
  const struct saved_module_scope *const entry
      = (const struct saved_module_scope *) ent;
  return (hashval_t) entry->module_id;
}

/* Module scope equality function.  */

static int
htab_module_scope_eq (const void *ent1, const void *ent2)
{
  const struct saved_module_scope *const entry1
      = (const struct saved_module_scope *) ent1;
  const struct saved_module_scope *const entry2
      = (const struct saved_module_scope *) ent2;

  return entry1->module_id == entry2->module_id;
}

/* Returns the module scope given a module id MOD_ID.  */

static struct saved_module_scope *
get_module_scope (unsigned mod_id)
{
  struct saved_module_scope **slot, key, *module_scope;

  gcc_assert (mod_id);

  if (saved_module_scope_map == NULL)
    saved_module_scope_map = htab_create_ggc (10, htab_module_scope_hash,
                                              htab_module_scope_eq, NULL);
  key.module_id = mod_id;
  slot = (struct saved_module_scope **)
      htab_find_slot (saved_module_scope_map, &key, INSERT);
  module_scope = *slot;
  if (!module_scope)
    {
      module_scope = ggc_alloc_cleared_saved_module_scope ();
      module_scope->module_id = mod_id;
      *slot = module_scope;
    }
  return module_scope;
}

/* Allocate memory for struct lang_decl for tree T.  */

static struct lang_decl *
alloc_lang_decl (tree t)
{
  size_t size;
  size = lang_hooks.l_ipo.get_lang_decl_size (t);
  return ggc_alloc_cleared_lang_decl (size);
}

/* Return a cloned copy of tree SRC.  */

tree
lipo_save_decl (tree src)
{
  tree saved = copy_node (src);
  enum tree_code tc = TREE_CODE (src);
  if (TREE_CODE_CLASS (tc) == tcc_declaration)
    {
      struct lang_decl *ls = NULL;
      struct function *func = NULL;
      DECL_CONTEXT (saved) = DECL_CONTEXT (src);
      if (DECL_LANG_SPECIFIC (src))
        {
          ls = alloc_lang_decl (src);
          memcpy (ls, DECL_LANG_SPECIFIC (src),
                  lang_hooks.l_ipo.get_lang_decl_size (src));
        }
      DECL_LANG_SPECIFIC (saved) = ls;
      if (tc == FUNCTION_DECL && DECL_STRUCT_FUNCTION (src))
        {
          func = ggc_alloc_cleared_function ();
          *func = *(DECL_STRUCT_FUNCTION (src));
          DECL_STRUCT_FUNCTION (saved) = func;
        }
    }
  else
    {
      gcc_assert (TREE_CODE_CLASS (tc) == tcc_type &&
                  TYPE_MAIN_VARIANT (src) == src);
      TYPE_CONTEXT (saved) = TYPE_CONTEXT (src);
      lang_hooks.l_ipo.dup_lang_type (src, saved);
    }

  return saved;
}

/* Copy tree SAVED to tree DEST.  */

void
lipo_restore_decl (tree dest, tree saved)
{
  enum tree_code tc;
  unsigned old_uid;
  struct lang_decl *oldls;

  tc = TREE_CODE (saved);
  if (TREE_CODE_CLASS (tc) == tcc_declaration)
    {
      struct function *oldfunc = NULL;
      old_uid = DECL_UID (dest);
      oldls = DECL_LANG_SPECIFIC (dest);
      oldfunc
          = (tc == FUNCTION_DECL ? DECL_STRUCT_FUNCTION (dest) : NULL);

      memcpy ((char *) dest + sizeof (struct tree_common),
              (char *) saved + sizeof (struct tree_common),
              sizeof (struct tree_decl_common) - sizeof (struct tree_common));

      if (tc == FUNCTION_DECL)
        memcpy ((char *) dest + sizeof (struct tree_decl_common),
                (char *) saved + sizeof (struct tree_decl_common),
                sizeof (struct tree_function_decl)
                - sizeof (struct tree_decl_common));

      DECL_UID (dest) = old_uid;
      if (DECL_LANG_SPECIFIC (saved))
        {
          if (!oldls)
            oldls = alloc_lang_decl (dest);
          memcpy (oldls, DECL_LANG_SPECIFIC (saved),
                  lang_hooks.l_ipo.get_lang_decl_size (saved));
          DECL_LANG_SPECIFIC (dest) = oldls;
        }
      else
        DECL_LANG_SPECIFIC (dest) = NULL;

      if (tc == FUNCTION_DECL)
        {
          if (DECL_STRUCT_FUNCTION (saved))
            {
              if (!oldfunc)
                oldfunc = ggc_alloc_cleared_function ();
              *oldfunc = *(DECL_STRUCT_FUNCTION (saved));
              DECL_STRUCT_FUNCTION (dest) = oldfunc;
            }
          else
            DECL_STRUCT_FUNCTION (dest) = NULL;
        }
    }
  else
    {
      gcc_assert (TREE_CODE_CLASS (tc) == tcc_type);
      lang_hooks.l_ipo.copy_lang_type (saved, dest);
    }
}


/* Return the name for tree TD which is either a decl or type.  */

tree
get_type_or_decl_name (tree td)
{
  tree id;

  if (DECL_P (td))
    id = DECL_NAME (td);
  else
    {
      id = TYPE_NAME (td);
      if (DECL_P (id))
        id = DECL_NAME (id);
    }
  return id;
}

/* For a DECL (a type or a decl) in SCOPE, check to see if it is in
   global or namespace scope. If yes, add it to the current module scope.  */

void
add_decl_to_current_module_scope (tree decl, void *scope)
{
  struct saved_module_scope *module_scope;
  tree id;

  if (!flag_dyn_ipa)
    return;

  if (!parser_parsing_start)
    {
      /* The source file may contains only global variable declations
         -- there is no module grouping data associated with it, so
         neither primary_module_id nor current_module_id is set.  */
      lang_hooks.l_ipo.add_built_in_decl (decl);
      return;
    }

  if (!L_IPO_COMP_MODE)
    return;

  if (!lang_hooks.l_ipo.has_global_name (decl, scope))
    return;

  /* Unlike C++ where names are attached to type decls, for C, the type name
     is identifier node. Thus we need to track type names as well.  */
  id = get_type_or_decl_name (decl);
  if (!id)
    return;

  module_scope = current_module_scope;
  gcc_assert (module_scope && module_scope->module_id == current_module_id);
  vec_safe_push (module_scope->module_decls, decl);
}

/* Clear name bindings for all decls created in MODULE_SCOPE.  */

static void
clear_module_scope_bindings (struct saved_module_scope *module_scope)
{
  size_t i;
  tree decl;

  for (i = 0;
       vec_safe_iterate (module_scope->module_decls, i, &decl);
       ++i)
    {
      lang_hooks.l_ipo.clear_global_name_bindings (
        get_type_or_decl_name (decl));
      /* Now force creating assembly name. */
      if (VAR_OR_FUNCTION_DECL_P (decl))
        {
          tree assembler_name;

          if (HAS_DECL_ASSEMBLER_NAME_P (decl)
              && DECL_ASSEMBLER_NAME_SET_P (decl))
            {
              assembler_name = DECL_ASSEMBLER_NAME (decl);
              lang_hooks.l_ipo.clear_global_name_bindings (assembler_name);
            }
        }
    }
}

/* The referenced attribute of a decl is not associated with the
   decl itself but with the assembler name. Remember the referenced
   bits before clearing them.  */

static void
save_assembler_name_reference_bit (void)
{
  varpool_get_referenced_asm_ids (&referenced_asm_ids);
}

/* Clear the reference bits for assembler names before closing the
   module scope.  */

static void
clear_assembler_name_reference_bit (void)
{
  varpool_clear_asm_id_reference_bit ();
}

/* Restore the reference bits for assembler names.  */

static void
restore_assembler_name_reference_bit (void)
{
  size_t i;
  tree nm;
  for (i = 0;
       vec_safe_iterate (referenced_asm_ids, i, &nm);
       ++i)
    TREE_SYMBOL_REFERENCED (nm) = 1;
}

/* Set up the module scope before the parsing of the
   associated source file.  */

void
push_module_scope (void)
{
  struct saved_module_scope *prev_module_scope;

  if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
    {
      parser_parsing_start = true;
      return;
    }

  prev_module_scope = current_module_scope;
  if (L_IPO_IS_PRIMARY_MODULE)
    {
      gcc_assert (!prev_module_scope);
      lang_hooks.l_ipo.save_built_in_decl_pre_parsing ();
      parser_parsing_start = true;
    }

  gcc_assert (current_module_id);

  /* Set up the module scope.  */
  current_module_scope = get_module_scope (current_module_id);
  return;
}

/* Restore the shared decls to their post parsing states.  */

static void
restore_post_parsing_states (void)
{
  current_module_id = primary_module_id;
  current_module_scope = get_module_scope (primary_module_id);
  set_funcdef_no (max_funcdef_no);
  input_location = primary_module_last_loc;

  restore_assembler_name_reference_bit ();
  lang_hooks.l_ipo.restore_built_in_decl_post_module_parsing ();
}

/* Pop the current module scope (by clearing name bindings etc.)
   and prepare for parsing of the next module.  In particular,
   built-in decls need to be restored to the state before file
   parsing starts.  */

void
pop_module_scope (void)
{
  bool is_last = false;
  int  last_funcdef_no;

  if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
    return;

  gcc_assert (current_module_id && current_module_scope);

  if (L_IPO_IS_PRIMARY_MODULE)
    primary_module_last_loc = input_location;

  at_eof = 1;
  cgraph_process_same_body_aliases ();
  lang_hooks.l_ipo.process_pending_decls (input_location);
  lang_hooks.l_ipo.reset_parsing_state ();
  at_eof = 0;

  is_last = is_last_module (current_module_id);

  last_funcdef_no = get_last_funcdef_no ();
  if (last_funcdef_no > max_funcdef_no)
    max_funcdef_no = last_funcdef_no;

  lang_hooks.l_ipo.save_built_in_decl_post_module_parsing ();
  /* Save primary module state if needed (when module group
     size > 1)  */
  if (L_IPO_IS_PRIMARY_MODULE && num_in_fnames > 1)
    {
      save_assembler_name_reference_bit ();
      primary_module_last_funcdef_no = last_funcdef_no;
    }

  if (!is_last)
    {
      /* More aux modules are anticipated, clear
         the parsing state.  */
      gcc_assert (num_in_fnames > 1);
      clear_assembler_name_reference_bit ();
      clear_module_scope_bindings (current_module_scope);
      /* Restore symtab bindings for builtins  */
      lang_hooks.l_ipo.restore_built_in_decl_pre_parsing ();
      /* The map can not be cleared because the names of operator
         decls are used to store the information about the conversion
         target type. This forces the coversion operator ids to be
         incremented across different modules, and assember id must
         be used for checksum computation.  */
      /* cp_clear_conv_type_map (); */
    }
  else if (num_in_fnames > 1)
   {
     clear_module_scope_bindings (current_module_scope);
     restore_post_parsing_states ();
   }
  else
    gcc_assert (L_IPO_IS_PRIMARY_MODULE && num_in_fnames == 1);
}


/* Type merging support for LIPO  */

struct type_ec
{
  tree rep_type;
  vec<tree> *eq_types;
};

static vec<tree> *pending_types = NULL;
static struct pointer_set_t *type_set = NULL;
static htab_t type_hash_tab = NULL;

/* Hash function for the type table.  */

static hashval_t
type_hash_hash (const void *ent)
{
  tree type, name;
  const struct type_ec *const entry
      = (const struct type_ec *) ent;

  type = entry->rep_type;
  name = TYPE_NAME (type);
  if (DECL_P (name))
    name = DECL_NAME (name);

  return htab_hash_string (IDENTIFIER_POINTER (name));
}

/* Equality function for type hash table.  */

static int
type_hash_eq (const void *ent1, const void *ent2)
{
  tree type1, type2;
  const struct type_ec *const entry1
      = (const struct type_ec *) ent1;
  const struct type_ec *const entry2
      = (const struct type_ec *) ent2;

  type1 = entry1->rep_type;
  type2 = entry2->rep_type;

  return aggr_has_equiv_id (type1, type2);
}

/* Function to delete type hash entries.  */

static void
type_hash_del (void *ent)
{
  struct type_ec *const entry
      = (struct type_ec *) ent;

  vec_free (entry->eq_types);
  free (entry);
}

struct GTY(()) type_ent
{
  tree type;
  unsigned eq_id;
};

static GTY ((param_is (type_ent))) htab_t l_ipo_type_tab = 0;
static unsigned l_ipo_eq_id = 0;

/* Address hash function for struct type_ent.  */

static hashval_t
type_addr_hash (const void *ent)
{
  const struct type_ent *const entry
      = (const struct type_ent *) ent;
  return (hashval_t) (uintptr_t) entry->type;
}

/* Address equality function for type_ent.  */

static int
type_addr_eq (const void *ent1, const void *ent2)
{
  const struct type_ent *const entry1
      = (const struct type_ent *) ent1;
  const struct type_ent *const entry2
      = (const struct type_ent *) ent2;
  return entry1->type == entry2->type;
}

/* Returns 1 if NS1 and NS2 refer to the same namespace.  */

static int
is_ns_equiv (tree ns1, tree ns2)
{
  tree n1, n2;
  if (ns1 == NULL && ns2 == NULL)
    return 1;

  if ((!ns1 && ns2) || (ns1 && !ns2))
    return 0;

  gcc_assert (DECL_P (ns1) && DECL_P (ns2));

  if (!is_ns_equiv (DECL_CONTEXT (ns1),
                    DECL_CONTEXT (ns2)))
      return 0;

  n1 = DECL_NAME (ns1);
  n2 = DECL_NAME (ns2);
  if (n1 == 0 && n2 == 0)
    /* Conservative (which can happen when two NSes are from
       different modules but with same UID) quivalence is allowed.  */
    return DECL_UID (ns1) == DECL_UID (ns2);
  if (!n1 || !n2)
    return 0;

  if (!strcmp (IDENTIFIER_POINTER (n1),
               IDENTIFIER_POINTER (n2)))
    return 1;

  return 0;
}

/* Returns 1 if aggregate type T1 and T2 have equivalent qualified
   ids.  */

static int
aggr_has_equiv_id (tree t1, tree t2)
{
  int ctx_match;
  tree ctx1, ctx2, tn1, tn2;
  gcc_assert (TYPE_P (t1) && TYPE_P (t2));

  ctx1 = TYPE_CONTEXT (t1);
  ctx2 = TYPE_CONTEXT (t2);

  if ((ctx1 && !ctx2) || (!ctx1 && ctx2))
    return 0;

  if (ctx1 && TREE_CODE (ctx1) != TREE_CODE (ctx2))
    return 0;

  if (ctx1 && (TREE_CODE (ctx1) == FUNCTION_DECL
               || TREE_CODE (ctx1) == BLOCK))
    return 0;

  if (!ctx1)
    {
      ctx_match = 1;
      gcc_assert (!ctx2);
    }
  else if (TREE_CODE (ctx1) == NAMESPACE_DECL)
    ctx_match = is_ns_equiv (ctx1, ctx2);
  else if (TYPE_P (ctx1))
    ctx_match = aggr_has_equiv_id (ctx1, ctx2);
  else
    {
      gcc_assert (TREE_CODE (ctx1) == TRANSLATION_UNIT_DECL);
      ctx_match = 1;
    }

  if (!ctx_match)
    return 0;

  /* Now compare the name of the types.  */
  tn1 = TYPE_NAME (t1);
  tn2 = TYPE_NAME (t2);
  if ((tn1 && !tn2) || !(tn1 && tn2))
    return 0;
  else if (!tn1 && !tn2)
    /* Be conservative on unamed types.  */
    return 1;

  if (DECL_P (tn1))
    tn1 = DECL_NAME (tn1);
  if (DECL_P (tn2))
    tn2 = DECL_NAME (tn2);
  if (strcmp (IDENTIFIER_POINTER (tn1),
              IDENTIFIER_POINTER (tn2)))
    return 0;

  return lang_hooks.l_ipo.cmp_lang_type (t1, t2);
}

/* Return the canonical type of the type's main variant.  */
static inline tree
get_norm_type (const_tree type)
{
  tree cano_type = TYPE_MAIN_VARIANT (type);
  if (TYPE_CANONICAL (cano_type))
    cano_type = TYPE_CANONICAL (cano_type);

  return cano_type;
}

/* Return 1 if type T1 and T2 are equivalent. Struct/union/class
   types are compared using qualified name ids.  Alias sets of
   equivalent types will be merged. Client code may choose to do
   structural equivalence check for sanity.  Note the difference
   between the types_compatible_p (and its langhooks subroutines)
   and this interface. The former is mainly used to remove useless
   type conversion and value numbering computation. It returns 1
   only when it is sure and should not be used in contexts where
   erroneously returning 0 causes problems. This interface
   lipo_cmp_type behaves differently - it returns 1 when it is not
   sure -- as the primary purpose of the interface is for alias
   set computation.  */

int
lipo_cmp_type (tree t1, tree t2)
{
  if (TREE_CODE (t1) != TREE_CODE (t2))
    return 0;
  if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
    return 0;
  if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
    return 0;

  t1 = get_norm_type (t1);
  t2 = get_norm_type (t2);

  switch (TREE_CODE (t1))
    {
    case RECORD_TYPE:
    case UNION_TYPE:
    case QUAL_UNION_TYPE:
      return aggr_has_equiv_id (t1, t2);

    case POINTER_TYPE:
    case REFERENCE_TYPE:
    case COMPLEX_TYPE:
    case TYPE_PACK_EXPANSION:
      return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
    case ARRAY_TYPE:
      return (TYPE_DOMAIN (t1) == NULL || TYPE_DOMAIN (t2) == NULL
              || (lipo_cmp_type (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2))
                  && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2))));
    case METHOD_TYPE:
      return lipo_cmp_type (TYPE_METHOD_BASETYPE (t1),
                            TYPE_METHOD_BASETYPE (t2));
    case FUNCTION_TYPE:
      {
        tree arg1, arg2;
        for (arg1 = TYPE_ARG_TYPES (t1), arg2 = TYPE_ARG_TYPES (t2);
             arg1 && arg2;
             arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
          if (!lipo_cmp_type (TREE_VALUE (arg1),
                              TREE_VALUE (arg2)))
            return 0;
        if (arg1 || arg2)
          return 0;
        return 1;
      }
    case OFFSET_TYPE:
      return lipo_cmp_type (TYPE_OFFSET_BASETYPE (t1),
                            TYPE_OFFSET_BASETYPE (t2));
    case ENUMERAL_TYPE:
      return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
    case REAL_TYPE:
    case FIXED_POINT_TYPE:
    case INTEGER_TYPE:
      return (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
              && TYPE_MODE (t1) == TYPE_MODE (t2)
              && TYPE_MIN_VALUE (t1) == TYPE_MIN_VALUE (t2)
              && TYPE_MAX_VALUE (t1) == TYPE_MAX_VALUE (t2));
    case VECTOR_TYPE:
      return (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
              && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2)));
    case VOID_TYPE:
    case BOOLEAN_TYPE:
    case NULLPTR_TYPE:
      return 1;
    case TEMPLATE_TYPE_PARM:
      return 1;
    default:
      gcc_unreachable ();
    }
}

#ifndef ANON_AGGRNAME_PREFIX
#define ANON_AGGRNAME_PREFIX "__anon_"
#endif
#ifndef ANON_AGGRNAME_P
#define ANON_AGGRNAME_P(ID_NODE) \
  (!strncmp (IDENTIFIER_POINTER (ID_NODE), ANON_AGGRNAME_PREFIX, \
	     sizeof (ANON_AGGRNAME_PREFIX) - 1))
#endif

/* Callback function used in tree walk to find referenced struct types.  */

static tree
find_struct_types (tree *tp,
                   int *walk_subtrees ATTRIBUTE_UNUSED,
                   void *data ATTRIBUTE_UNUSED)
{
  if (!(*tp))
    return NULL_TREE;

  if (TYPE_P (*tp))
    {
      if (lang_hooks.l_ipo.is_compiler_generated_type (*tp))
        return NULL_TREE;

      switch (TREE_CODE (*tp))
        {
        case RECORD_TYPE:
        case UNION_TYPE:
        case QUAL_UNION_TYPE:
          {
            tree cano_type, name;
            tree context;
            tree field;

            cano_type = get_norm_type (*tp);
            name = TYPE_NAME (cano_type);
            if (!name)
              {
                /* the main variant of typedef of unnamed struct
                   has no name, use the orignal type for equivalence.  */
                cano_type = *tp;
                name = TYPE_NAME (cano_type);
              }
            if (!name)
              return NULL_TREE;
            if (DECL_P (name)
                && (DECL_IGNORED_P (name)
                    || ANON_AGGRNAME_P (DECL_NAME (name))))
              return NULL_TREE;

            if (!pointer_set_insert (type_set, cano_type))
              pending_types->safe_push (cano_type);
            else
              return NULL_TREE; /* Or use walk tree without dups.  */

            context = TYPE_CONTEXT (cano_type);
            if (context && TYPE_P (context))
              walk_tree (&context, find_struct_types, NULL, NULL);

            /* Instantiate a nested work as the tree walker does not
               get to the fields.  */
            if (TYPE_BINFO (cano_type))
	      {
                int i;
                tree binfo, base_binfo;

                for (binfo = TYPE_BINFO (cano_type), i = 0;
                     BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
                  walk_tree (&BINFO_TYPE (base_binfo), find_struct_types,
                             NULL, NULL);
              }
            for (field = TYPE_FIELDS (cano_type);
                 field != 0;
                 field = TREE_CHAIN (field))
              walk_tree (&TREE_TYPE (field), find_struct_types,
                         NULL, NULL);
            return NULL_TREE;
          }
        default:
          return NULL_TREE;
        }
    }
  else if (DECL_P (*tp))
    /* walk tree does not walk down decls, so do a nested walk here.  */
    walk_tree (&(TREE_TYPE (*tp)), find_struct_types, NULL, NULL);

  return NULL_TREE;
}

/* Collect referenced struct types.  */

static void
cgraph_collect_type_referenced (void)
{
  basic_block bb;
  gimple_stmt_iterator gi;

  FOR_EACH_BB_FN (bb, cfun)
    {
      for (gi = gsi_start_bb (bb); !gsi_end_p (gi); gsi_next (&gi))
        {
          unsigned i;
	  gimple stmt = gsi_stmt (gi);
	  for (i = 0; i < gimple_num_ops (stmt); i++)
	    walk_tree (gimple_op_ptr (stmt, i), find_struct_types, NULL, NULL);
	}
    }
}

/* Check type equivalence. Returns 1 if T1 and T2 are equivalent
   for tbaa; return 0 if not. -1 is returned if it is unknown.  */

int
equivalent_struct_types_for_tbaa (const_tree t1, const_tree t2)
{
  struct type_ent key, *tent1, *tent2,  **slot;

  if (!l_ipo_type_tab)
    return -1;

  t1 = get_norm_type (t1);
  t2 = get_norm_type (t2);

  key.type = (tree) (uintptr_t) t1;
  slot = (struct type_ent **)
      htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
  if (!slot || !*slot)
    return -1;
  tent1 = *slot;

  key.type = (tree) (uintptr_t) t2;
  slot = (struct type_ent **)
      htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
  if (!slot || !*slot)
    return -1;
  tent2 = *slot;

  return tent1->eq_id == tent2->eq_id;
}

/* Build type hash table.  */

static void
cgraph_build_type_equivalent_classes (void)
{
  unsigned n, i;
  n = pending_types->length ();
  for (i = 0; i < n; i++)
    {
      struct type_ec **slot;
      struct type_ec te;
      te.rep_type  = (*pending_types)[i];
      te.eq_types = NULL;
      slot = (struct type_ec **) htab_find_slot (type_hash_tab,
                                                 &te, INSERT);
      if (!*slot)
        {
          *slot = XCNEW (struct type_ec);
          (*slot)->rep_type = te.rep_type;
          vec_alloc ((*slot)->eq_types, 10);
        }
      (*slot)->eq_types->safe_push (te.rep_type);
    }
}

/* Re-propagate component types's alias set to that of TYPE. PROCESSED
   is the pointer set of processed types.  */

static void
re_record_component_aliases (tree type,
                             struct pointer_set_t *processed)
{
  alias_set_type superset = get_alias_set (type);
  tree field;

  if (superset == 0)
    return;

  if (pointer_set_insert (processed, type))
    return;

  switch (TREE_CODE (type))
    {
    case RECORD_TYPE:
    case UNION_TYPE:
    case QUAL_UNION_TYPE:
      /* Recursively record aliases for the base classes, if there are any.  */
      if (TYPE_BINFO (type))
	{
	  int i;
	  tree binfo, base_binfo;

	  for (binfo = TYPE_BINFO (type), i = 0;
	       BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
            {
              re_record_component_aliases (BINFO_TYPE (base_binfo),
                                           processed);
              record_alias_subset (superset,
                                   get_alias_set (BINFO_TYPE (base_binfo)));
            }
	}
      for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
	if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
          {
            re_record_component_aliases (TREE_TYPE (field), processed);
            record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
          }
      break;

    case COMPLEX_TYPE:
      re_record_component_aliases (TREE_TYPE (type), processed);
      record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
      break;

    /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
       element type.  */

    default:
      break;
    }
}

/* The callback function to merge alias sets of equivalent types.  */

static int
type_eq_process (void **slot, void *data ATTRIBUTE_UNUSED)
{
  unsigned i;
  alias_set_type alias_set, ptr_alias_set = -1;
  tree rep_type, type;
  vec<tree> *eq_types;
  struct type_ec ** te = (struct type_ec **)slot;
  bool zero_set = false, ptr_zero_set = false;
  struct type_ent **slot2, key, *tent;


  rep_type = (*te)->rep_type;
  eq_types = (*te)->eq_types;
  alias_set = get_alias_set (rep_type);

  for (i = 0; eq_types->iterate (i, &type); ++i)
    {
      alias_set_type als, ptr_als = -1;
      tree type_ptr = TYPE_POINTER_TO (type);;

      als = get_alias_set (type);
      if (als == 0)
        zero_set = true;

      if (alias_set && als && alias_set != als)
        record_alias_subset (alias_set, als);

      if (type_ptr)
        {
          ptr_als = get_alias_set (type_ptr);
          if (ptr_als == 0)
            ptr_zero_set = true;

          if (ptr_alias_set == -1)
            ptr_alias_set = ptr_als;
          else
            {
              if (!ptr_zero_set && ptr_alias_set != ptr_als)
                record_alias_subset (ptr_alias_set, ptr_als);
            }
        }
    }

  /* Now propagate back.  */
  for (i = 0; eq_types->iterate (i, &type); ++i)
    {
      alias_set_type als, ptr_als;
      tree ptr_type = TYPE_POINTER_TO (type);

      als = get_alias_set (type);

      if (zero_set)
        TYPE_ALIAS_SET (type) = 0;
      else if (alias_set != als)
        record_alias_subset (als, alias_set);

      if (ptr_type)
        {
          ptr_als = get_alias_set (ptr_type);
          if (ptr_zero_set)
            TYPE_ALIAS_SET (ptr_type) = 0;
          else if (ptr_alias_set != ptr_als)
            record_alias_subset (ptr_als, ptr_alias_set);
        }
    }


  /* Now populate the type table.  */
  l_ipo_eq_id++;
  for (i = 0; eq_types->iterate (i, &type); ++i)
    {
      key.type = type;
      slot2 = (struct type_ent **)
          htab_find_slot (l_ipo_type_tab, &key, INSERT);
      tent = *slot2;
      gcc_assert (!tent);
      tent = ggc_alloc_cleared_type_ent ();
      tent->type = key.type;
      tent->eq_id = l_ipo_eq_id;
      *slot2 = tent;
    }

  return 1;
}

/* Regenerate alias set for aggregate types.  */

static void
record_components_for_parent_types (void)
{
  unsigned n, i;
  struct pointer_set_t *processed_types;

  processed_types = pointer_set_create ();
  n = pending_types->length ();
  for (i = 0; i < n; i++)
    {
      tree type = (*pending_types)[i];
      re_record_component_aliases (type, processed_types);
    }

  pointer_set_destroy (processed_types);
}

/* Unify type alias sets for equivalent types.  */

void
cgraph_unify_type_alias_sets (void)
{
  struct cgraph_node *node;
  struct varpool_node *pv;

  /* Only need to do type unification when we are in LIPO mode
     and have a non-trivial module group (size is >1). However,
     override the size check under non-zero PARAM_LIPO_RANDOM_GROUP_SIZE,
     which indicates that we are stress-testing LIPO. In that case
     try to flush out problems with type unification by always
     performing it.  */
  if (!L_IPO_COMP_MODE
      || (num_in_fnames == 1
          && PARAM_VALUE (PARAM_LIPO_RANDOM_GROUP_SIZE) == 0))
    return;

  vec_alloc (pending_types, 100);
  type_set = pointer_set_create ();
  type_hash_tab = htab_create (10, type_hash_hash,
                               type_hash_eq, type_hash_del);
  l_ipo_type_tab = htab_create_ggc (10, type_addr_hash,
                                    type_addr_eq, NULL);

  FOR_EACH_DEFINED_FUNCTION (node)
    {
      if (!gimple_has_body_p (node->decl))
	continue;
      push_cfun (DECL_STRUCT_FUNCTION (node->decl));
      current_function_decl = node->decl;
      if (gimple_has_body_p (current_function_decl))
        cgraph_collect_type_referenced ();
      current_function_decl = NULL;
      set_cfun (NULL);
      pop_cfun ();
    }

  FOR_EACH_VARIABLE (pv)
    walk_tree (&pv->decl, find_struct_types, NULL, NULL);

  /* Compute type equivalent classes.  */
  cgraph_build_type_equivalent_classes ();
  /* Now unify alias sets of equivelent types.  */
  htab_traverse (type_hash_tab, type_eq_process, NULL);
  /* Finally re-populating parent's alias set.  */
  record_components_for_parent_types ();

  pointer_set_destroy (type_set);
  vec_free (pending_types);
  htab_delete (type_hash_tab);
}

/* Return true if DECL is an artificial function that we do not want
   to promote and which may not be available in the primary module.
   The sole exception is currently __tls_init.  */

static bool
decl_artificial_nopromote (tree decl)
{
  if (!DECL_ARTIFICIAL (decl))
    return false;

  /* Handle the __tls_init function specially as we do want to promote it and
     allow the aux module to be resolved to the version in the primary module.
     We check if it is prefixed by __tls_init to catch it after promotion
     as well from cgraph_is_aux_decl_external.  */
  if (!strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "__tls_init",
                10))
    return false;

  return true;
}

/* Return true if NODE->decl from an auxiliary module has external
   definition (and therefore is not needed for expansion).  */

bool
cgraph_is_aux_decl_external (struct cgraph_node *node)
{
  tree decl = node->decl;

  if (!L_IPO_COMP_MODE)
    return false;

  if (!cgraph_is_auxiliary (decl))
    return false;

  /* Versioned clones from auxiliary moduels are not external.  */
  if (node->is_versioned_clone)
    return false;

  /* Comdat or weak functions in aux modules are not external --
     there is no guarantee that the definitition will be emitted
     in the primary compilation of this auxiliary module.
     Functions marked artificial (e.g. an implicitly instantiated virtual
     destructor) are also not guaranteed to be available in the primary module,
     as they are not promoted by process_module_scope_static_func.  */
  if (DECL_COMDAT (decl) || DECL_WEAK (decl) || decl_artificial_nopromote (decl))
    return false;

  /* virtual functions won't be deleted in the primary module.  */
  if (DECL_VIRTUAL_P (decl))
    return true;

  if (!TREE_PUBLIC (decl))
    return false;

  /* The others from aux modules are external. */
  return true;
}

/* Linked function symbol (cgraph node)  table.  */
static GTY((param_is (cgraph_sym))) htab_t cgraph_symtab;

/* This is true when global linking is needed and performed (for C++).
   For C, symbol linking is performed on the fly during parsing, and
   the cgraph_symtab is used only for keeping additional information
   for any already merged symbol if needed.  */

static bool global_link_performed = 0;

/* For an external (non-defined) function DECL, return the primary
   module id (even though when the declaration is declared in an aux
   module). For a defined function DECL, return the module id in which
   it is defined.  */

unsigned
cgraph_get_module_id (tree decl)
{
  struct function *func = DECL_STRUCT_FUNCTION (decl);
  /* Not defined.  */
  if (!func)
    return primary_module_id;
  return FUNC_DECL_MODULE_ID (func);
}

/* Return true if function decl is defined in an auxiliary module.  */

bool
cgraph_is_auxiliary (tree decl)
{
  return (cgraph_get_module_id (decl) != primary_module_id);
}

/* Return the hash value for cgraph_sym pointed to by P. The
   hash value is computed using function's assembler name.  */

static hashval_t
hash_sym_by_assembler_name (const void *p)
{
  const struct cgraph_sym *n = (const struct cgraph_sym *) p;
  return (hashval_t) decl_assembler_name_hash (n->assembler_name);
}

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

static int
eq_assembler_name (const void *p1, const void *p2)
{
  const struct cgraph_sym *n1 = (const struct cgraph_sym *) p1;
  const_tree name = (const_tree) p2;
  return (decl_assembler_name_equal (n1->rep_decl, name));
}

/* Return the cgraph_sym for function declaration DECL.  */

static struct cgraph_sym **
cgraph_sym (tree decl)
{
  struct cgraph_sym **slot;
  tree name;

  if (!cgraph_symtab)
    {
      gcc_assert (!global_link_performed);
      return NULL;
    }

  name = DECL_ASSEMBLER_NAME (decl);
  slot = (struct cgraph_sym **)
      htab_find_slot_with_hash (cgraph_symtab, name,
                                decl_assembler_name_hash (name),
                                NO_INSERT);
  return slot;
}

/* Return the representative declaration for assembler name
   ASM_NAME.  */

tree
cgraph_find_decl (tree asm_name)
{
  struct cgraph_sym **slot;
  if (!L_IPO_COMP_MODE)
    return NULL;
  if (!cgraph_symtab || !global_link_performed)
    return NULL;

  slot = (struct cgraph_sym **)
      htab_find_slot_with_hash (cgraph_symtab, asm_name,
                                decl_assembler_name_hash (asm_name),
                                NO_INSERT);
  if (!slot || !*slot)
    return NULL;

  return (*slot)->rep_node->decl;
}

/* Return true if function declaration DECL is originally file scope
   static, which is promoted to global scope.  */

bool
cgraph_is_promoted_static_func (tree decl)
{
  struct cgraph_sym ** sym;
  gcc_assert (L_IPO_COMP_MODE);

  /* cgraph_symtab will be created when any symbol got
     promoted.  */
  if (!cgraph_symtab)
    return false;

  sym = cgraph_sym (decl);
  if (!sym)
    return false;
  return (*sym)->is_promoted_static;
}

/* Hash function for module information table. ENT
   is a pointer to a cgraph_module_info.  */

static hashval_t
htab_sym_hash (const void *ent)
{
  const struct cgraph_mod_info * const mi
      = (const struct cgraph_mod_info * const ) ent;
  return (hashval_t) mi->module_id;
}

/* Hash equality function for module information table.  */

static int
htab_sym_eq (const void *ent1, const void *ent2)
{
  const struct cgraph_mod_info * const mi1
      = (const struct cgraph_mod_info * const ) ent1;
  const struct cgraph_mod_info * const mi2
      = (const struct cgraph_mod_info * const ) ent2;
  return (mi1->module_id == mi2->module_id);
}

/* cgraph_sym SYM may be defined in more than one source modules.
   Add declaration DECL's definiting module to SYM.  */

static void
add_define_module (struct cgraph_sym *sym, tree decl)
{
  unsigned module_id;
  struct cgraph_mod_info **slot;
  struct cgraph_mod_info mi;

  struct function *f = DECL_STRUCT_FUNCTION (decl);
  if (!f)
    return;
  module_id = FUNC_DECL_MODULE_ID (f);

  if (!sym->def_module_hash)
    sym->def_module_hash
        = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);

  mi.module_id = module_id;
  slot = (struct cgraph_mod_info **)htab_find_slot (sym->def_module_hash,
                                                    &mi, INSERT);
  if (!*slot)
    {
      *slot = ggc_alloc_cleared_cgraph_mod_info ();
      (*slot)->module_id = module_id;
    }
  else
    gcc_assert ((*slot)->module_id == module_id);
}

static int
add_def_module (void **slot, void *data)
{
  struct cgraph_mod_info **m = (struct cgraph_mod_info **)slot;
  htab_t mod_set = (htab_t) data;
  struct cgraph_mod_info **new_slot;

  new_slot = (struct cgraph_mod_info **)htab_find_slot (mod_set, *m, INSERT);
  if (!*new_slot)
    {
      *new_slot = ggc_alloc_cleared_cgraph_mod_info ();
      (*new_slot)->module_id = (*m)->module_id;
    }
  else
    gcc_assert ((*new_slot)->module_id == (*m)->module_id);
  return 1;
}

/* Clone defined module hash table from ORIG to CLONE.  */

void
copy_defined_module_set (tree clone, tree orig)
{
  struct cgraph_sym **orig_sym, **clone_sym;

  orig_sym = cgraph_sym (orig);
  clone_sym = cgraph_sym (clone);
  if (!orig_sym || !(*orig_sym)->def_module_hash)
    return;
  if (!(*clone_sym)->def_module_hash)
    (*clone_sym)->def_module_hash
      = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
  htab_traverse ((*orig_sym)->def_module_hash, add_def_module, (*clone_sym)->def_module_hash);
}

/* Return true if the symbol associated with DECL is defined in module
   MODULE_ID.  This interface is used by the inliner to make sure profile-gen
   and profile-use pass (L-IPO mode) make consistent inline decision.  */

bool
cgraph_is_inline_body_available_in_module (tree decl, unsigned module_id)
{
  struct cgraph_sym **sym;
  void **slot;
  struct cgraph_mod_info mi;

  gcc_assert (L_IPO_COMP_MODE);

  if (DECL_BUILT_IN (decl))
    return true;

  /* TODO: revisit this.  */
  if (DECL_IN_SYSTEM_HEADER (decl) && DECL_DECLARED_INLINE_P (decl))
    return true;

  gcc_assert (TREE_STATIC (decl) || DECL_DECLARED_INLINE_P (decl));

  if (cgraph_get_module_id (decl) == module_id)
    return true;

  sym = cgraph_sym (decl);
  if (!sym || !(*sym)->def_module_hash)
    return false;

  mi.module_id = module_id;
  slot = htab_find_slot ((*sym)->def_module_hash, &mi, NO_INSERT);
  if (slot)
    {
      gcc_assert (((struct cgraph_mod_info*)*slot)->module_id == module_id);
      return true;
    }
  return false;
}

/* Return the linked cgraph node using DECL's assembler name.  DO_ASSERT
   is a flag indicating that a non null link target must be returned.  */

struct cgraph_node *
cgraph_lipo_get_resolved_node_1 (tree decl, bool do_assert)
{
  struct cgraph_sym **slot;

  /* Handle alias decl. */
  slot = cgraph_sym (decl);

  if (!slot || !*slot)
    {
      if (!do_assert)
        return NULL;
      else
        {
          /* Nodes that are indirectly called are not 'reachable' in
             the callgraph. If they are not needed (comdat, inline
             extern etc), they may be removed from the link table
             before direct calls to them are exposed (via indirect
             call promtion by const folding etc). When this happens,
             the node will need to be relinked. A probably better fix
             is to modify the callgraph so that they are not eliminated
             in the first place -- this will allow inlining to happen.  */

          struct cgraph_node *n = cgraph_get_create_node (decl);
          if (!n->analyzed)
            {
              gcc_assert (DECL_EXTERNAL (decl)
                          || cgraph_is_aux_decl_external (n)
                          || DECL_VIRTUAL_P (decl));
              gcc_assert (/* This is the case for explicit extern instantiation,
                             when cgraph node is not created before link.  */
                          DECL_EXTERNAL (decl));
              cgraph_link_node (n);
              return n;
            }
          else
            gcc_unreachable ();
        }
    }
  else
    {
      struct cgraph_sym *sym = *slot;
      return sym->rep_node;
    }
}

/* Return the cgraph_node of DECL if decl has definition; otherwise return
   the cgraph node of the representative decl, which is the declaration DECL
   is resolved to after linking/symbol resolution.  */

struct cgraph_node *
cgraph_lipo_get_resolved_node (tree decl)
{
  struct cgraph_node *node = NULL;

  gcc_assert (L_IPO_COMP_MODE && global_link_performed);
  gcc_assert (cgraph_symtab);

  /* Never merged.  */
  if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl)
      /* builtin function decls are shared across modules, but 'linking'
         is still performed for them to keep track of the set of defining
         modules. Skip the real resolution here to avoid merging '__builtin_xxx'
         with 'xxx'.  */
      || DECL_BUILT_IN (decl))
    return cgraph_get_create_node (decl);

  node = cgraph_lipo_get_resolved_node_1 (decl, true);
  return node;
}

/* When NODE->decl is dead function eliminated,
   remove the entry in the link table.  */

void
cgraph_remove_link_node (struct cgraph_node *node)
{
  tree name, decl;

  if (!L_IPO_COMP_MODE || !cgraph_symtab)
    return;

  decl = node->decl;

  /* Skip nodes that are not in the link table.  */
  if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
    return;

  /* Skip if node is an inline clone or if the node has
     defintion that is not really resolved to the merged node.  */
  if (cgraph_lipo_get_resolved_node_1 (decl, false) != node)
    return;

  name = DECL_ASSEMBLER_NAME (decl);
  htab_remove_elt_with_hash (cgraph_symtab, name,
                             decl_assembler_name_hash (name));
}

/* Return true if the function body for DECL has profile information.  */

static bool
has_profile_info (tree decl)
{
  gcov_type *ctrs = NULL;
  unsigned n;
  struct function* f = DECL_STRUCT_FUNCTION (decl);

  ctrs = get_coverage_counts_no_warn (f, GCOV_COUNTER_ARCS, &n);
  if (ctrs)
    {
      unsigned i;
      for (i = 0; i < n; i++)
        if (ctrs[i])
          return true;
    }

  return false;
}

/* Resolve delaration NODE->decl for function symbol *SLOT.  */

static void
resolve_cgraph_node (struct cgraph_sym **slot, struct cgraph_node *node)
{
  tree decl1, decl2;
  int decl1_defined = 0;
  int decl2_defined = 0;

  decl1 = (*slot)->rep_decl;
  decl2 = node->decl;

  decl1_defined = gimple_has_body_p (decl1);
  decl2_defined = gimple_has_body_p (decl2);

  if (decl1_defined && !decl2_defined)
    return;

  if (!decl1_defined && decl2_defined)
    {
      (*slot)->rep_node = node;
      (*slot)->rep_decl = decl2;
      add_define_module (*slot, decl2);
      return;
    }

  if (decl2_defined)
    {
      bool has_prof1 = false;
      bool has_prof2 = false;
      gcc_assert (decl1_defined);
      add_define_module (*slot, decl2);

      /* Pick the node that cannot be removed, to avoid a situation
         where we remove the resolved node and later try to access
         it for the remaining non-removable copy.  E.g. one may be
         extern and the other weak, only the extern copy can be removed.  */
      if (cgraph_can_remove_if_no_direct_calls_and_refs_p ((*slot)->rep_node)
          && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
        {
          (*slot)->rep_node = node;
          (*slot)->rep_decl = decl2;
          return;
        }
      /* Similarly, pick the non-external symbol, since external
         symbols may be eliminated by symtab_remove_unreachable_nodes
         after ipa inlining (see process_references).  */
      if (DECL_EXTERNAL (decl1) && !DECL_EXTERNAL (decl2))
        {
          (*slot)->rep_node = node;
          (*slot)->rep_decl = decl2;
          return;
        }

      has_prof1 = has_profile_info (decl1);
      bool is_aux1 = cgraph_is_auxiliary (decl1);
      bool is_aux2 = cgraph_is_auxiliary (decl2);
      /* Pick the copy from the primary module if multiple copies
         have profile.  */
      if (has_prof1 && (!is_aux1 || is_aux2))
        return;
      has_prof2 = has_profile_info (decl2);
      if (has_prof2)
        {
          (*slot)->rep_node = node;
          (*slot)->rep_decl = decl2;
        }
      return;
    }

  /* Handle aliases properly. Make sure the alias symbol resolution
     is consistent with alias target  */
  if (node->alias && !node->thunk.thunk_p)
    {
      struct cgraph_node *decl2_tgt = cgraph_function_or_thunk_node (node, NULL);
      if (cgraph_lipo_get_resolved_node_1 (decl2_tgt->decl, false) == decl2_tgt)
        {
          (*slot)->rep_node = node;
          (*slot)->rep_decl = decl2;
        }
    }
  return;
}


/* Resolve NODE->decl in the function symbol table.  */

struct cgraph_sym *
cgraph_link_node (struct cgraph_node *node)
{
  void **slot;
  tree name;

  if (!L_IPO_COMP_MODE)
    return NULL;

  if (!cgraph_symtab)
    return NULL;

  /* Skip the cases when the  defintion can be locally resolved, and
     when we do not need to keep track of defining modules.  */
  if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
    return NULL;

  name = DECL_ASSEMBLER_NAME (node->decl);
  slot = htab_find_slot_with_hash (cgraph_symtab, name,
                                   decl_assembler_name_hash (name),
                                   INSERT);
  if (*slot)
    resolve_cgraph_node ((struct cgraph_sym **) slot, node);
  else
    {
      struct cgraph_sym *sym = ggc_alloc_cleared_cgraph_sym ();
      sym->rep_node = node;
      sym->rep_decl = node->decl;
      sym->assembler_name = name;
      add_define_module (sym, node->decl);
      *slot = sym;
    }
  return (struct cgraph_sym *) *slot;
}

/* Perform cross module linking of function declarations.  */

void
cgraph_do_link (void)
{
  struct cgraph_node *node;

  if (!L_IPO_COMP_MODE)
    return;

  global_link_performed = 1;
  gcc_assert (cgraph_pre_profiling_inlining_done);

  if (!cgraph_symtab)
    cgraph_symtab
        = htab_create_ggc (10, hash_sym_by_assembler_name,
                           eq_assembler_name, NULL);

  FOR_EACH_FUNCTION (node)
    {
      gcc_assert (!node->global.inlined_to);
      /* Delay aliases  */
      if (node->alias && !node->thunk.thunk_p)
        continue;
      cgraph_link_node (node);
    }

  /* Now handle aliases   */
  FOR_EACH_FUNCTION (node)
    {
      if (node->alias && !node->thunk.thunk_p)
        cgraph_link_node (node);
    }
}

struct promo_ent
{
  char* assemb_name;
  tree decl;
  int seq;
};

/* Hash function for promo_ent table.  */

static hashval_t
promo_ent_hash (const void *ent)
{
  const struct promo_ent *const entry
      = (const struct promo_ent *) ent;

  return htab_hash_string (entry->assemb_name);
}

/* Hash_eq function for promo_ent table.  */

static int
promo_ent_eq (const void *ent1, const void *ent2)
{
  const struct promo_ent *const entry1
      = (const struct promo_ent *) ent1;
  const struct promo_ent *const entry2
      = (const struct promo_ent *) ent2;
  if (!strcmp (entry1->assemb_name, entry2->assemb_name))
    return 1;
  return 0;
}

/* Delete function for promo_ent hash table.  */

static void
promo_ent_del (void *ent)
{
  struct promo_ent *const entry
      = (struct promo_ent *) ent;

  free (entry->assemb_name);
  free (entry);
}

static htab_t promo_ent_hash_tab = NULL;

/* Make the var decl for weak symbol as extern.  */
 
static inline void
externalize_weak_decl (tree decl)
{
  gcc_assert (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl));

  DECL_EXTERNAL (decl) = 1;
  TREE_STATIC (decl) = 0;
  DECL_INITIAL (decl) = NULL;

  /* Keep the context so that devirt_variable_node_removal_hook
     can do cleanup properly for vtables.
  DECL_CONTEXT (decl) = NULL; */
}

/* Return a unique sequence number for NAME. This is needed to avoid
   name conflict -- function scope statics may have identical names.

   When DECL is NULL, 
   this function returns a zero sequence number if it is called with
   a particular NAME for the first time, and non-zero otherwise.
   This fact is used to keep track of unseen weak variables.  
   
   When DECL is not NULL, this function is supposed to be called by
   varpool_remove_duplicate_weak_decls.  */

static int
get_name_seq_num (const char *name, tree decl)
{
  struct promo_ent **slot;
  struct promo_ent ent;
  int ret = 0;

  gcc_assert (!decl || TREE_CODE (decl) == VAR_DECL);
  ent.assemb_name = xstrdup (name);
  ent.seq = 0;

  slot = (struct promo_ent **)
      htab_find_slot (promo_ent_hash_tab, &ent, INSERT);

  if (!*slot)
    {
      *slot = XCNEW (struct promo_ent);
      (*slot)->assemb_name = ent.assemb_name;
      (*slot)->decl = decl;
    }
  else
    {
      /* During output, the previously selected weak decl may not be
         referenced by any function that is expanded thus they do not have
         DECL_RTL_SET_P to be true and therefore can be eliminated by
         varpool_remove_unreferenced_decls later. To avoid that, logic is
         added to replace previously selected decl when needed.  */ 
      if (decl && DECL_RTL_SET_P (decl)
          && !DECL_RTL_SET_P ((*slot)->decl))
        {
          externalize_weak_decl ((*slot)->decl);
          (*slot)->decl = decl;
          ret = 0;
        }
      else 
        ret = ++(*slot)->seq;
      free (ent.assemb_name);
    }
  return ret;
}

/* Returns a unique assembler name for DECL.  */

static tree
create_unique_name (tree decl, unsigned module_id)
{
  tree id, assemb_id;
  char *assembler_name;
  const char *name;
  struct  function *context = NULL;
  int seq = 0;

  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
      if (!DECL_CONTEXT (decl)
          || TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
        {
          id = DECL_NAME (decl);
          /* if (IDENTIFIER_OPNAME_P (id))  */
          if (TREE_LANG_FLAG_2 (id))
            id = DECL_ASSEMBLER_NAME (decl);
        }
      else
        id = DECL_ASSEMBLER_NAME (decl);
    }
  else
    {
      if (!DECL_CONTEXT (decl))
        id = DECL_NAME (decl);
      else if (TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
        id = DECL_ASSEMBLER_NAME (decl);
      else if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
        {
          id = DECL_NAME (decl);
          context = DECL_STRUCT_FUNCTION (DECL_CONTEXT (decl));
        }
      else
        /* file scope context */
        id = DECL_NAME (decl);
    }

  name = IDENTIFIER_POINTER (id);
  if (context)
    {
      char *n;
      unsigned fno =  FUNC_DECL_FUNC_ID (context);
      n = (char *)alloca (strlen (name) + 15);
      sprintf (n, "%s.%u", name, fno);
      name = n;
    }

  assembler_name = (char*) alloca (strlen (name) + 30);
  sprintf (assembler_name, "%s.cmo.%u", name, module_id);
  seq = get_name_seq_num (assembler_name, NULL);
  if (seq)
    sprintf (assembler_name, "%s.%d", assembler_name, seq);

  assemb_id = get_identifier (assembler_name);

  return assemb_id;
}

/* Promote DECL to be global. MODULE_ID is the id of the module where
   DECL is defined. IS_EXTERN is a flag indicating if externalization
   is needed.  */

static void
promote_static_var_func (unsigned module_id, tree decl, bool is_extern)
{
  tree assemb_id;
  tree alias;

  /* No need to promote symbol alias.  */
  alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
  if (alias)
    return;

  /* Function decls in C++ may contain characters not taken by assembler.
     Similarly, function scope static variable has UID as the assembler name
     suffix which is not consistent across modules.  */
  assemb_id = create_unique_name (decl, module_id);

  if (DECL_ASSEMBLER_NAME_SET_P (decl))
    {
      if (TREE_CODE (decl) == FUNCTION_DECL)
        unlink_from_assembler_name_hash (cgraph_get_create_node (decl),
                                         false);
      else
        unlink_from_assembler_name_hash (varpool_get_node (decl), false);
    }

  SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
  TREE_PUBLIC (decl) = 1;
  DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
  DECL_VISIBILITY_SPECIFIED (decl) = 1;

  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
      struct cgraph_node *node = cgraph_get_create_node (decl);

      node->resolution = LDPR_UNKNOWN;
      insert_to_assembler_name_hash (node, false);
    }
  else
    {
      struct varpool_node *node = varpool_get_node (decl);
      node->resolution = LDPR_UNKNOWN;
      /* Statics from exported primary module are very likely
         referenced by other modules, so they should be made
         externally visible (to be avoided to be localized again).
         Another way to do this is to set force_output bit or
         change the logic in varpool_externally_visible in ipa.c.  */
      if (!is_extern)
        {
          node->resolution = LDPR_PREVAILING_DEF;
          node->externally_visible = true;
        }
      varpool_link_node (node);
      insert_to_assembler_name_hash (node, false);
      /* Possibly update the RTL name as well. */
      if (DECL_RTL_SET_P (decl))
        XSTR (XEXP (DECL_RTL (decl), 0), 0) = IDENTIFIER_POINTER (assemb_id);
    }

  if (is_extern)
    {
      if (TREE_CODE (decl) == VAR_DECL)
        {
          TREE_STATIC (decl) = 0;
          DECL_EXTERNAL (decl) = 1;
          /* Keep the initializer to allow const prop.  */
          /* DECL_INITIAL (decl) = 0; */
          /* Keep the context so that devirt_variable_node_removal_hook
             can do cleanup properly for vtables.
          DECL_CONTEXT (decl) = 0; */
        }
      /* else
         Function body will be deleted later before expansion.  */
    }
  else
    TREE_STATIC (decl) = 1;
}

/* Externalize global variables from aux modules and promote
   static variables.
   WEAK variables are treated especially in
   varpool_remove_duplicate_weak_decls.  */

static void
process_module_scope_static_var (struct varpool_node *vnode)
{
  tree decl = vnode->decl;

  if (varpool_is_auxiliary (vnode))
    {
      gcc_assert (vnode->module_id != primary_module_id);
      if (TREE_PUBLIC (decl))
        {
          /* Externalize non-weak variables.  */
	  if (!DECL_WEAK (decl))
	    {
	      DECL_EXTERNAL (decl) = 1;
	      TREE_STATIC (decl) = 0;
              /* Keep the initializer to allow const prop.  */
	      /* DECL_INITIAL (decl) = NULL; */
	      if (DECL_CONTEXT (decl))
                {
                  DECL_ASSEMBLER_NAME (decl);
                }
              /* Keep the context so that devirt_variable_node_removal_hook
                 can do cleanup properly for vtables.
              DECL_CONTEXT (decl) = NULL; */
	    }
        }
      else
        {
          /* Promote static vars to global.  */
          if (vnode->module_id)
            promote_static_var_func (vnode->module_id, decl,
                                     varpool_is_auxiliary (vnode));
        }
    }
  else
    {
      if (PRIMARY_MODULE_EXPORTED && !TREE_PUBLIC (decl))
        promote_static_var_func (vnode->module_id, decl,
                                 varpool_is_auxiliary (vnode));
    }
}

/* Promote all aliases of CNODE.  */

static void
promote_function_aliases (struct cgraph_node *cnode, unsigned mod_id,
                          bool is_extern)
{
  int i;
  struct ipa_ref *ref;

  for (i = 0; ipa_ref_list_referring_iterate (&cnode->ref_list, i, ref);
      i++)
    {
      if (ref->use == IPA_REF_ALIAS)
        {
          struct cgraph_node *alias = ipa_ref_referring_node (ref);
          tree alias_decl = alias->decl;
          /* Should assert  */
          if (cgraph_get_module_id (alias_decl) == mod_id)
            promote_static_var_func (mod_id, alias_decl, is_extern);
        }
    }
}

/* Promote static function CNODE->decl to be global.  */

static void
process_module_scope_static_func (struct cgraph_node *cnode)
{
  tree decl = cnode->decl;
  bool addr_taken;
  unsigned mod_id;
  struct ipa_ref *ref;
  int i;

  if (TREE_PUBLIC (decl)
      || !TREE_STATIC (decl)
      || DECL_EXTERNAL (decl)
      || decl_artificial_nopromote (decl))
    return;

  if (flag_ripa_no_promote_always_inline
      && lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)) != NULL)
    return;

  /* Can be local -- the promotion pass need to be done after
     callgraph build when address taken bit is set.  */
  addr_taken = cnode->address_taken;
  if (!addr_taken)
    {
      for (i = 0; ipa_ref_list_referring_iterate (&cnode->ref_list, i, ref);
          i++)
        if (ref->use == IPA_REF_ALIAS)
          {
	    struct cgraph_node *alias = ipa_ref_referring_node (ref);
	    if (alias->address_taken)
	      addr_taken = true;
          }
    }
  if (!addr_taken)
    {
      tree assemb_id = create_unique_name (decl, cgraph_get_module_id (decl));

      if (DECL_ASSEMBLER_NAME_SET_P (decl))
        unlink_from_assembler_name_hash (cnode, false);
      SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
      insert_to_assembler_name_hash (cnode, false);
      return;
    }

  mod_id = cgraph_get_module_id (decl);
  if (cgraph_is_auxiliary (decl))
    {
      gcc_assert (mod_id != primary_module_id);
      /* Promote static function to global.  */
      if (mod_id)
        {
          promote_static_var_func (mod_id, decl, 1);
          promote_function_aliases (cnode, mod_id, 1);
        }
    }
  else
    {
      if (PRIMARY_MODULE_EXPORTED
          /* skip static_init routines.  */
          && !DECL_ARTIFICIAL (decl))
        {
          promote_static_var_func (mod_id, decl, 0);

          promote_function_aliases (cnode, mod_id, 0);
        }
    }
}

/* Process var_decls, func_decls with static storage.  */

void
cgraph_process_module_scope_statics (void)
{
  struct cgraph_node *pf;
  struct varpool_node *pv;

  if (!L_IPO_COMP_MODE)
    return;

  promo_ent_hash_tab = htab_create (10, promo_ent_hash,
                                    promo_ent_eq, promo_ent_del);

  /* Process variable first.  */
  FOR_EACH_DEFINED_VARIABLE (pv)
    process_module_scope_static_var (pv);

  FOR_EACH_FUNCTION (pf)
    process_module_scope_static_func (pf);

  htab_delete (promo_ent_hash_tab);
}

/* There could be duplicate non-extern WEAK decls in the varpool queue,
   coming from different modules. All but one of these need to be externalized
   and removed from the varpool queue.
   Duplicate WEAK decls can be added to varpool queue as late as
   cgraph_expand_function, when a WEAK decl is marked referenced as assembler
   is being output. Therefore, a call to this function should be made after
   cgraph_expand_function.  */

void
varpool_remove_duplicate_weak_decls (void)
{
  struct varpool_node *node = NULL;

  if (!L_IPO_COMP_MODE)
    return;

  promo_ent_hash_tab = htab_create (10, promo_ent_hash,
                                    promo_ent_eq, promo_ent_del);

  FOR_EACH_VARIABLE (node)
    {
      tree decl = node->decl;

      if (TREE_PUBLIC (decl) && DECL_WEAK (decl) && !DECL_EXTERNAL (decl)
          && get_name_seq_num (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), decl))
        externalize_weak_decl (decl);
    }

  htab_delete (promo_ent_hash_tab);
}

static GTY((param_is (symtab_node))) htab_t varpool_symtab;

/* Hash function for varpool node.  */

static hashval_t
hash_node_by_assembler_name (const void *p)
{
  const struct varpool_node *n = (const struct varpool_node *) p;
  return (hashval_t) decl_assembler_name_hash (
        DECL_ASSEMBLER_NAME (n->decl));
}

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

static int
eq_node_assembler_name (const void *p1, const void *p2)
{
  const struct varpool_node *n1 = (const struct varpool_node *) p1;
  const_tree name = (const_tree)p2;
  return (decl_assembler_name_equal (n1->decl, name));
}

/* Return true if NODE's decl is declared in an auxiliary module.  */

bool
varpool_is_auxiliary (struct varpool_node *node)
{
  return (node->module_id
          && node->module_id != primary_module_id);
}

/* Return the varpool_node to which DECL is resolved to during linking.
   This method can not be used after static to global promotion happens.  */

static struct varpool_node *
real_varpool_node_1 (tree decl, bool assert)
{
  void **slot;
  tree name;

  if (!L_IPO_COMP_MODE || !varpool_symtab)
    return varpool_get_node (decl);

  if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
    return varpool_get_node (decl);

  name = DECL_ASSEMBLER_NAME (decl);
  slot = htab_find_slot_with_hash (varpool_symtab, name,
                                   decl_assembler_name_hash (name),
                                   NO_INSERT);
  if (!slot)
    {
      gcc_assert (!assert);
      return NULL;
    }

  gcc_assert (slot && *slot);
  return (struct varpool_node *)*slot;
}

struct varpool_node *
real_varpool_node (tree decl)
{
  return real_varpool_node_1 (decl, true);
}

/* Remove NODE from the link table.  */

void
varpool_remove_link_node (struct varpool_node *node)
{
  tree name;
  tree decl;

  if (!L_IPO_COMP_MODE || !varpool_symtab)
    return;

  decl = node->decl;

  if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
    return;

  if (real_varpool_node_1 (decl, false) != node)
    return;

  name = DECL_ASSEMBLER_NAME (decl);
  htab_remove_elt_with_hash (varpool_symtab, name,
                             decl_assembler_name_hash (name));
}

/* Merge the addressable attribute from DECL2 to DECL1.  */

static inline void
merge_addressable_attr (tree decl1, tree decl2)
{
  if (TREE_ADDRESSABLE (decl2))
    TREE_ADDRESSABLE (decl1) = 1;
}

/* Resolve NODE->decl to symbol table entry *SLOT.  */

static void
resolve_varpool_node (struct varpool_node **slot, struct varpool_node *node)
{
  tree decl1, decl2;

  decl1 = (*slot)->decl;
  decl2 = node->decl;

  /* Take the decl with the complete type. */
  if (COMPLETE_TYPE_P (TREE_TYPE (decl1))
      && !COMPLETE_TYPE_P (TREE_TYPE (decl2)))
    {
      merge_addressable_attr (decl1, decl2);
      return;
    }
  if (!COMPLETE_TYPE_P (TREE_TYPE (decl1))
      && COMPLETE_TYPE_P (TREE_TYPE (decl2)))
    {
      *slot = node;
      merge_addressable_attr (decl2, decl1);
      return;
    }

  if (DECL_INITIAL (decl1) && !DECL_INITIAL (decl2))
    {    
      merge_addressable_attr (decl1, decl2);
      return;
    }    

  if (!DECL_INITIAL (decl1) && DECL_INITIAL (decl2))
    {    
      *slot = node;
      merge_addressable_attr (decl2, decl1);
      return;
    }    

  /* Either all complete or neither's type is complete. Just
     pick the primary module's decl.  */
  if (!varpool_is_auxiliary (*slot))
    {
      merge_addressable_attr (decl1, decl2);
      return;
    }

  if (!varpool_is_auxiliary (node))
    {
      *slot = node;
      merge_addressable_attr (decl2, decl1);
      return;
    }

  merge_addressable_attr (decl1, decl2);
  return;
}

/* Link NODE into var_decl symbol table.  */

void
varpool_link_node (struct varpool_node *node)
{
  tree name;
  void **slot;

  if (!L_IPO_COMP_MODE || !varpool_symtab)
    return;

  if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
    return;

  name = DECL_ASSEMBLER_NAME (node->decl);
  slot = htab_find_slot_with_hash (varpool_symtab, name,
                                   decl_assembler_name_hash (name),
                                   INSERT);
  if (*slot)
    resolve_varpool_node ((struct varpool_node **) slot, node);
  else
    *slot = node;
}

/* Fixup references of VNODE.  */

static void
fixup_reference_list (struct varpool_node *node)
{
  int i;
  struct ipa_ref *ref;
  struct ipa_ref_list *list = &node->ref_list;
  vec<symtab_node *> new_refered;
  vec<int> new_refered_type;
  struct symtab_node *sym_node;
  enum ipa_ref_use use_type = IPA_REF_LOAD;

  new_refered.create (10);
  new_refered_type.create (10);
  for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
    {
      if (is_a <cgraph_node> (ref->referred))
        {
          struct cgraph_node *cnode = ipa_ref_node (ref);
          struct cgraph_node *r_cnode
            = cgraph_lipo_get_resolved_node (cnode->decl);
          new_refered.safe_push (r_cnode);
          use_type = ref->use;
          new_refered_type.safe_push ((int) use_type);
          gcc_assert (use_type != IPA_REF_ADDR
                      || cnode->global.inlined_to
                      || cnode->address_taken);
          if (use_type == IPA_REF_ADDR)
            cgraph_mark_address_taken_node (r_cnode);
        }
      else if (is_a <varpool_node> (ref->referred))
        {
          struct varpool_node *var = ipa_ref_varpool_node (ref);
          struct varpool_node *r_var = real_varpool_node (var->decl);
          new_refered.safe_push (r_var);
          use_type = ref->use;
          new_refered_type.safe_push ((int) use_type);
        }
      else
        gcc_assert (false);
    }
  ipa_remove_all_references (&node->ref_list);
  for (i = 0; new_refered.iterate (i, &sym_node); ++i)
    {
      ipa_record_reference (node, sym_node,
                            (enum ipa_ref_use) new_refered_type[i], NULL);
    }
}

/* Perform cross module linking for var_decls.  */

void
varpool_do_link (void)
{
  struct varpool_node *node;

  if (!L_IPO_COMP_MODE)
    return;

  varpool_symtab
      = htab_create_ggc (10, hash_node_by_assembler_name,
                         eq_node_assembler_name, NULL);
  FOR_EACH_VARIABLE (node)
    varpool_link_node (node);

  /* Merge the externally visible attribute.  */
  FOR_EACH_VARIABLE (node)
    {
      if (node->externally_visible)
        (real_varpool_node (node->decl))->externally_visible = true;
      fixup_reference_list (node);
    }
}

/* Get the list of assembler name ids with reference bit set.  */

void
varpool_get_referenced_asm_ids (vec<tree,va_gc> **ids)
{
  struct varpool_node *node;
  FOR_EACH_VARIABLE (node)
    {
      tree asm_id = NULL;
      tree decl = node->decl;
      if (DECL_ASSEMBLER_NAME_SET_P (decl))
        {
          asm_id = DECL_ASSEMBLER_NAME (decl);
          vec_safe_push (*ids, asm_id);
        }
    }
}

/* Clear the referenced bit in all assembler ids.  */

void
varpool_clear_asm_id_reference_bit (void)
{
  struct varpool_node *node;
  FOR_EACH_VARIABLE (node)
    {
      tree asm_id = NULL;
      tree decl = node->decl;
      if (DECL_ASSEMBLER_NAME_SET_P (decl))
        {
          asm_id = DECL_ASSEMBLER_NAME (decl);
          TREE_SYMBOL_REFERENCED (asm_id) = 0;
        }
    }
}


#include "gt-l-ipo.h"