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
|
From b73462757734c62f64e7a4379340679ec6f19669 Mon Sep 17 00:00:00 2001
From: Diachkov Ilia <diachkov.ilia1@huawei-partners.com>
Date: Tue, 27 Feb 2024 07:28:12 +0800
Subject: [PATCH 06/18] Port icp patch to GCC 12
---
gcc/common.opt | 8 +
gcc/dbgcnt.def | 1 +
gcc/ipa-devirt.cc | 1855 +++++++++++++++++++++++++++++++++++
gcc/passes.def | 1 +
gcc/testsuite/gcc.dg/icp1.c | 40 +
gcc/testsuite/gcc.dg/icp2.c | 38 +
gcc/testsuite/gcc.dg/icp3.c | 52 +
gcc/testsuite/gcc.dg/icp4.c | 55 ++
gcc/testsuite/gcc.dg/icp5.c | 66 ++
gcc/testsuite/gcc.dg/icp6.c | 66 ++
gcc/testsuite/gcc.dg/icp7.c | 48 +
gcc/timevar.def | 1 +
gcc/tree-pass.h | 1 +
13 files changed, 2232 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/icp1.c
create mode 100644 gcc/testsuite/gcc.dg/icp2.c
create mode 100644 gcc/testsuite/gcc.dg/icp3.c
create mode 100644 gcc/testsuite/gcc.dg/icp4.c
create mode 100644 gcc/testsuite/gcc.dg/icp5.c
create mode 100644 gcc/testsuite/gcc.dg/icp6.c
create mode 100644 gcc/testsuite/gcc.dg/icp7.c
diff --git a/gcc/common.opt b/gcc/common.opt
index 39c90604e..16aadccf6 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1316,6 +1316,14 @@ fdevirtualize
Common Var(flag_devirtualize) Optimization
Try to convert virtual calls to direct ones.
+ficp
+Common Var(flag_icp) Optimization Init(0)
+Try to promote indirect calls to direct ones.
+
+ficp-speculatively
+Common Var(flag_icp_speculatively) Optimization
+Promote indirect calls speculatively.
+
fdiagnostics-show-location=
Common Joined RejectNegative Enum(diagnostic_prefixing_rule)
-fdiagnostics-show-location=[once|every-line] How often to emit source location at the beginning of line-wrapped diagnostics.
diff --git a/gcc/dbgcnt.def b/gcc/dbgcnt.def
index 3aa18cd0c..a00bbc31b 100644
--- a/gcc/dbgcnt.def
+++ b/gcc/dbgcnt.def
@@ -170,6 +170,7 @@ DEBUG_COUNTER (graphite_scop)
DEBUG_COUNTER (hoist)
DEBUG_COUNTER (hoist_insn)
DEBUG_COUNTER (ia64_sched2)
+DEBUG_COUNTER (icp)
DEBUG_COUNTER (if_after_combine)
DEBUG_COUNTER (if_after_reload)
DEBUG_COUNTER (if_conversion)
diff --git a/gcc/ipa-devirt.cc b/gcc/ipa-devirt.cc
index 74fe65608..383839189 100644
--- a/gcc/ipa-devirt.cc
+++ b/gcc/ipa-devirt.cc
@@ -103,9 +103,14 @@ along with GCC; see the file COPYING3. If not see
indirect polymorphic edge all possible polymorphic call targets of the call.
pass_ipa_devirt performs simple speculative devirtualization.
+ pass_ipa_icp performs simple indirect call promotion.
*/
#include "config.h"
+#define INCLUDE_ALGORITHM
+#define INCLUDE_SET
+#define INCLUDE_MAP
+#define INCLUDE_LIST
#include "system.h"
#include "coretypes.h"
#include "backend.h"
@@ -127,6 +132,7 @@ along with GCC; see the file COPYING3. If not see
#include "ipa-fnsummary.h"
#include "demangle.h"
#include "dbgcnt.h"
+#include "gimple-iterator.h"
#include "gimple-pretty-print.h"
#include "intl.h"
#include "stringpool.h"
@@ -4401,5 +4407,1854 @@ make_pass_ipa_odr (gcc::context *ctxt)
return new pass_ipa_odr (ctxt);
}
+/* Function signature map used to look up function decl which corresponds to
+ the given function type. */
+typedef std::set<unsigned> type_set;
+typedef std::set<tree> decl_set;
+typedef std::map<unsigned, type_set*> type_alias_map;
+typedef std::map<unsigned, decl_set*> type_decl_map;
+typedef std::map<unsigned, tree> uid_to_type_map;
+typedef std::map<tree, tree> type_map;
+
+static bool has_address_taken_functions_with_varargs = false;
+static type_set *unsafe_types = NULL;
+static type_alias_map *fta_map = NULL;
+static type_alias_map *ta_map = NULL;
+static type_map *ctype_map = NULL;
+static type_alias_map *cbase_to_ptype = NULL;
+static type_decl_map *fs_map = NULL;
+static uid_to_type_map *type_uid_map = NULL;
+
+static void
+print_type_set(unsigned ftype_uid, type_alias_map *map)
+{
+ if (!map->count (ftype_uid))
+ return;
+ type_set* s = (*map)[ftype_uid];
+ for (type_set::const_iterator it = s->begin (); it != s->end (); it++)
+ fprintf (dump_file, it == s->begin () ? "%d" : ", %d", *it);
+}
+
+static void
+dump_type_with_uid (const char *msg, tree type, dump_flags_t flags = TDF_NONE)
+{
+ fprintf (dump_file, msg);
+ print_generic_expr (dump_file, type, flags);
+ fprintf (dump_file, " (%d)\n", TYPE_UID (type));
+}
+
+/* Walk aggregate type and collect types of scalar elements. */
+
+static void
+collect_scalar_types (tree tp, std::list<tree> &types)
+{
+ /* TODO: take into account different field offsets.
+ Also support array casts. */
+ if (tp && dump_file && (dump_flags & TDF_DETAILS))
+ dump_type_with_uid ("Walk var's type: ", tp, TDF_UID);
+ if (RECORD_OR_UNION_TYPE_P (tp))
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Record's fields {\n");
+ for (tree field = TYPE_FIELDS (tp); field;
+ field = DECL_CHAIN (field))
+ {
+ if (TREE_CODE (field) != FIELD_DECL)
+ continue;
+ collect_scalar_types (TREE_TYPE (field), types);
+ }
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "}\n");
+ return;
+ }
+ if (TREE_CODE (tp) == ARRAY_TYPE)
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Array's innermost type:\n");
+ /* Take the innermost component type. */
+ tree elt;
+ for (elt = TREE_TYPE (tp); TREE_CODE (elt) == ARRAY_TYPE;
+ elt = TREE_TYPE (elt))
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ print_generic_expr (dump_file, elt);
+ collect_scalar_types (elt, types);
+ return;
+ }
+ types.push_back (tp);
+}
+
+static void maybe_register_aliases (tree type1, tree type2);
+
+/* Walk type lists and maybe register type aliases. */
+
+static void
+compare_type_lists (std::list<tree> tlist1, std::list<tree> tlist2)
+{
+ for (std::list<tree>::iterator ti1 = tlist1.begin (), ti2 = tlist2.begin ();
+ ti1 != tlist1.end (); ++ti1, ++ti2)
+ {
+ /* TODO: correct the analysis results if lists have different length. */
+ if (ti2 == tlist2.end ())
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Type lists with different length!\n");
+ break;
+ }
+ maybe_register_aliases (*ti1, *ti2);
+ }
+}
+
+/* For two given types collect scalar element types and
+ compare the result lists to find type aliases. */
+
+static void
+collect_scalar_types_and_find_aliases (tree t1, tree t2)
+{
+ std::list<tree> tlist1;
+ std::list<tree> tlist2;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "First type list: ");
+ collect_scalar_types (t1, tlist1);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Second type list: ");
+ collect_scalar_types (t2, tlist2);
+ compare_type_lists (tlist1, tlist2);
+}
+
+/* Dump type with the corresponding set from the map. */
+
+static void
+dump_type_uid_with_set (const char *msg, tree type, type_alias_map *map,
+ bool dump_type = true, bool with_newline = true)
+{
+ fprintf (dump_file, msg, TYPE_UID (type));
+ if (dump_type)
+ print_generic_expr (dump_file, type);
+ fprintf (dump_file, " (");
+ print_type_set (TYPE_UID (type), map);
+ fprintf (dump_file, ")");
+ fprintf (dump_file, with_newline ? "\n" : " ");
+}
+
+static void
+dump_two_types_uids_with_set (const char *msg, unsigned t1_uid,
+ unsigned t2_uid, type_alias_map *map)
+{
+ fprintf (dump_file, msg, t1_uid, t2_uid);
+ fprintf (dump_file, " (");
+ print_type_set (t1_uid, map);
+ fprintf (dump_file, ")\n");
+}
+
+/* Register type aliases in the map. Return true if new alias
+ is registered. */
+
+static bool
+register_ailas_type (tree type, tree alias_type, type_alias_map *map,
+ bool only_merge = false)
+{
+ /* TODO: maybe support the case with one missed type. */
+ if (!type || !alias_type)
+ return false;
+ unsigned type_uid = TYPE_UID (type);
+ unsigned alias_type_uid = TYPE_UID (alias_type);
+ if (type_uid_map->count (type_uid) == 0)
+ (*type_uid_map)[type_uid] = type;
+ if (type_uid_map->count (alias_type_uid) == 0)
+ (*type_uid_map)[alias_type_uid] = alias_type;
+
+ if (map->count (type_uid) == 0 && map->count (alias_type_uid) == 0)
+ {
+ (*map)[type_uid] = new type_set ();
+ (*map)[alias_type_uid] = (*map)[type_uid];
+ }
+ else if (map->count (type_uid) == 0)
+ (*map)[type_uid] = (*map)[alias_type_uid];
+ else if (map->count (alias_type_uid) == 0)
+ (*map)[alias_type_uid] = (*map)[type_uid];
+ else if (map->count (type_uid) && map->count (alias_type_uid))
+ {
+ if ((*map)[type_uid] == (*map)[alias_type_uid])
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_two_types_uids_with_set ("Types (%d) and (%d) are already in",
+ type_uid, alias_type_uid, map);
+ return false;
+ }
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ dump_type_uid_with_set ("T1 (%d) in set", type, map, false, true);
+ dump_type_uid_with_set ("T2 (%d) in set", alias_type, map,
+ false, true);
+ }
+ (*map)[type_uid]->insert ((*map)[alias_type_uid]->begin (),
+ (*map)[alias_type_uid]->end ());
+ type_set *type_set = (*map)[alias_type_uid];
+ for (type_set::const_iterator it1 = type_set->begin ();
+ it1 != type_set->end (); ++it1)
+ (*map)[*it1] = (*map)[type_uid];
+ delete type_set;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "MERGE: ");
+ }
+ if (!only_merge)
+ {
+ (*map)[type_uid]->insert (alias_type_uid);
+ (*map)[type_uid]->insert (type_uid);
+ }
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_two_types_uids_with_set ("Insert types (%d) and (%d) into set",
+ type_uid, alias_type_uid, map);
+ return true;
+}
+
+static void
+dump_two_types_with_uids (const char *msg, tree t1, tree t2)
+{
+ fprintf (dump_file, msg);
+ print_generic_expr (dump_file, t1, TDF_UID);
+ fprintf (dump_file, " (%d), ", TYPE_UID (t1));
+ print_generic_expr (dump_file, t2, TDF_UID);
+ fprintf (dump_file, " (%d)\n", TYPE_UID (t2));
+}
+
+static void
+analyze_pointees (tree type1, tree type2)
+{
+ gcc_assert (POINTER_TYPE_P (type1) && POINTER_TYPE_P (type2));
+ tree base1 = TREE_TYPE (type1);
+ tree base2 = TREE_TYPE (type2);
+ /* TODO: maybe analyze void pointers. */
+ if (VOID_TYPE_P(base1) || VOID_TYPE_P(base2))
+ return;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_two_types_with_uids ("Walk pointee types: ", base1, base2);
+ collect_scalar_types_and_find_aliases (base1, base2);
+}
+
+static void
+map_canonical_base_to_pointer (tree type, tree to_insert)
+{
+ type = TYPE_MAIN_VARIANT (type);
+ tree base_type = TREE_TYPE (type);
+ tree cbase_type = TYPE_CANONICAL (base_type);
+ if (!cbase_type)
+ return;
+ unsigned cbase_type_uid = TYPE_UID (cbase_type);
+ if (type_uid_map->count (cbase_type_uid) == 0)
+ (*type_uid_map)[cbase_type_uid] = cbase_type;
+
+ if (cbase_to_ptype->count (cbase_type_uid) == 0)
+ {
+ (*cbase_to_ptype)[cbase_type_uid] = new type_set ();
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "New map cb-to-p=(%d): ", cbase_type_uid);
+ }
+ else if (!(*cbase_to_ptype)[cbase_type_uid]->count (TYPE_UID (to_insert)))
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Found map cb-to-p=(%d): ", cbase_type_uid);
+ }
+ else
+ return;
+ /* Add all variants of 'to_insert' type. */
+ for (tree t = to_insert; t; t = TYPE_NEXT_VARIANT (t))
+ {
+ unsigned t_uid = TYPE_UID (t);
+ if (!(*cbase_to_ptype)[cbase_type_uid]->count (t_uid))
+ {
+ (*cbase_to_ptype)[cbase_type_uid]->insert (t_uid);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "(%d) ", t_uid);
+ }
+ if (type_uid_map->count (t_uid) == 0)
+ (*type_uid_map)[t_uid] = t;
+ }
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\n");
+}
+
+/* Analyse two types and maybe register them as aliases. Also collect
+ unsafe function types and map canonical base types to corresponding
+ pointer types. */
+
+static void
+maybe_register_aliases (tree type1, tree type2)
+{
+ if (type1 && POINTER_TYPE_P (type1) && !FUNCTION_POINTER_TYPE_P (type1))
+ map_canonical_base_to_pointer (type1, type1);
+ if (type2 && POINTER_TYPE_P (type2) && !FUNCTION_POINTER_TYPE_P (type2))
+ map_canonical_base_to_pointer (type2, type2);
+
+ if (type1 == type2 || !type1 || !type2)
+ return;
+
+ if (POINTER_TYPE_P (type1) && POINTER_TYPE_P (type2))
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_two_types_with_uids ("Pointer types: ", type1, type2);
+ if (register_ailas_type (type1, type2, ta_map))
+ analyze_pointees (type1, type2);
+ }
+ /* If function and non-function type pointers alias,
+ the function type is unsafe. */
+ if (FUNCTION_POINTER_TYPE_P (type1) && !FUNCTION_POINTER_TYPE_P (type2))
+ unsafe_types->insert (TYPE_UID (type1));
+ if (FUNCTION_POINTER_TYPE_P (type2) && !FUNCTION_POINTER_TYPE_P (type1))
+ unsafe_types->insert (TYPE_UID (type2));
+
+ /* Try to figure out with pointers to incomplete types. */
+ if (POINTER_TYPE_P (type1) && POINTER_TYPE_P (type2))
+ {
+ type1 = TYPE_MAIN_VARIANT (type1);
+ type2 = TYPE_MAIN_VARIANT (type2);
+ tree base1 = TREE_TYPE (type1);
+ tree base2 = TREE_TYPE (type2);
+ if (RECORD_OR_UNION_TYPE_P (base1) && RECORD_OR_UNION_TYPE_P (base2))
+ {
+ tree cb1 = TYPE_CANONICAL (base1);
+ tree cb2 = TYPE_CANONICAL (base2);
+ if (cb1 && !cb2)
+ map_canonical_base_to_pointer (type1, type2);
+ if (cb2 && !cb1)
+ map_canonical_base_to_pointer (type2, type1);
+ }
+ }
+}
+
+/* Maybe register non-void/equal type aliases. */
+
+static void
+maybe_register_non_void_aliases (tree t1, tree t2)
+{
+ gcc_assert (t1 && t2);
+ if (type_uid_map->count (TYPE_UID (t1)) == 0)
+ (*type_uid_map)[TYPE_UID (t1)] = t1;
+ if (type_uid_map->count (TYPE_UID (t2)) == 0)
+ (*type_uid_map)[TYPE_UID (t2)] = t2;
+
+ /* Skip equal and void types. */
+ if (t1 == t2 || VOID_TYPE_P (t1) || VOID_TYPE_P (t2))
+ return;
+ maybe_register_aliases (t1, t2);
+}
+
+/* Detect function type in call stmt. */
+
+static tree
+get_call_fntype (gcall *stmt)
+{
+ tree fntype = NULL;
+ if (gimple_call_fndecl (stmt) && TREE_TYPE (gimple_call_fndecl (stmt)))
+ fntype = TREE_TYPE (gimple_call_fndecl (stmt));
+ else
+ {
+ tree call_fn = gimple_call_fn (stmt);
+ tree ptype = TREE_TYPE (call_fn);
+ gcc_assert (ptype && TREE_TYPE (ptype));
+ fntype = TREE_TYPE (ptype);
+ }
+ gcc_assert (fntype && fntype != void_type_node
+ && (TREE_CODE (fntype) == FUNCTION_TYPE
+ || TREE_CODE (fntype) == METHOD_TYPE));
+ return fntype;
+}
+
+static void
+dump_global_var (tree decl)
+{
+ fprintf (dump_file, "Analyze global var: ");
+ print_generic_decl (dump_file, decl, TDF_NONE);
+ fprintf (dump_file, "\n");
+}
+
+static void
+collect_block_elt_types (tree tp, std::list<tree> &types, tree block)
+{
+ tree vt = TREE_TYPE (tp);
+ gcc_assert (vt);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ const char *msg = TREE_CODE (block) == BLOCK ? "VAR's block: " :
+ "VAR's ctor: ";
+ fprintf (dump_file, msg);
+ print_generic_expr (dump_file, tp);
+ dump_type_with_uid (" with type ", vt);
+ }
+ collect_scalar_types (vt, types);
+}
+
+/* Compare types of initialization block's or constructor's elements and
+ fields of the initializer type to find type aliases. */
+
+static void
+compare_block_and_init_type (tree block, tree t1)
+{
+ std::list<tree> tlist1;
+ std::list<tree> tlist2;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Init's type list: ");
+ collect_scalar_types (t1, tlist1);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Block's type list: ");
+ if (TREE_CODE (block) == CONSTRUCTOR)
+ {
+ unsigned HOST_WIDE_INT idx;
+ tree value;
+ FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (block), idx, value)
+ {
+ gcc_assert (value);
+ collect_block_elt_types (value, tlist2, block);
+ }
+ }
+ else if (TREE_CODE (block) == BLOCK)
+ for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
+ {
+ if (TREE_CODE (var) != VAR_DECL)
+ continue;
+ collect_block_elt_types (var, tlist2, block);
+ }
+ else
+ gcc_unreachable ();
+ compare_type_lists (tlist1, tlist2);
+}
+
+/* Analyze global var to find type aliases comparing types of var and
+ initializer elements. */
+
+static void
+analyze_global_var (varpool_node *var)
+{
+ var->get_constructor();
+ tree decl = var->decl;
+ if (TREE_CODE (decl) == SSA_NAME || !DECL_INITIAL (decl)
+ || integer_zerop (DECL_INITIAL (decl)))
+ return;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_global_var (decl);
+ tree var_type = TREE_TYPE (decl);
+ tree init_type = TREE_TYPE (DECL_INITIAL (decl));
+ gcc_assert (var_type && init_type);
+ if (RECORD_OR_UNION_TYPE_P (init_type)
+ && !initializer_zerop (DECL_INITIAL (decl)))
+ compare_block_and_init_type (DECL_INITIAL (decl), init_type);
+ else if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Is not a record with nonzero init\n");
+
+ if (var_type == init_type)
+ return;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_two_types_with_uids ("Mismatch of var and init types: ",
+ var_type, init_type);
+ collect_scalar_types_and_find_aliases (var_type, init_type);
+}
+
+static void
+dump_function_node_info (struct cgraph_node *n)
+{
+ fprintf (dump_file, "\nAnalyse function node: ");
+ print_generic_expr (dump_file, n->decl);
+ fprintf (dump_file, "\n");
+ tree fndecl_type = TREE_TYPE (n->decl);
+ dump_type_with_uid ("Function decl type: ", fndecl_type, TDF_UID);
+ if (TREE_TYPE (fndecl_type))
+ dump_type_with_uid ("Return type: ", TREE_TYPE (fndecl_type));
+ tree argt = TYPE_ARG_TYPES (fndecl_type);
+ for (unsigned i = 1; argt && argt != void_type_node
+ && !VOID_TYPE_P (TREE_VALUE (argt)); ++i, argt = TREE_CHAIN (argt))
+ {
+ tree atype = TREE_VALUE (argt);
+ fprintf (dump_file, "%d-arg type: ", i);
+ dump_type_with_uid ("", atype);
+ }
+ fprintf (dump_file, "\n");
+}
+
+static void
+dump_call_stmt_info (gcall *stmt, tree fntype)
+{
+ fprintf (dump_file, "\nAnalyse call stmt: ");
+ if (stmt)
+ print_gimple_stmt (dump_file, stmt, 3, TDF_DETAILS);
+ else
+ fprintf (dump_file, "(no stmt)\n");
+ dump_type_with_uid ("fntype=", fntype, TDF_UID);
+ if (gimple_call_fntype (stmt))
+ dump_type_with_uid ("fntype1=", gimple_call_fntype (stmt), TDF_UID);
+ if (gimple_call_fndecl (stmt) && TREE_TYPE (gimple_call_fndecl (stmt)))
+ dump_type_with_uid ("fntype2=", TREE_TYPE (gimple_call_fndecl (stmt)),
+ TDF_UID);
+}
+
+/* Dump actual and formal arg types. */
+
+static void
+dump_arg_types_with_uids (int i, tree t1, tree t2)
+{
+ if (i >= 0)
+ fprintf (dump_file, "Call's %d-arg types: ", i);
+ else
+ fprintf (dump_file, "Call's return types: ");
+ fprintf (dump_file, "(%d) and (%d) ", TYPE_UID (t1), TYPE_UID (t2));
+ print_generic_expr (dump_file, t1, TDF_UID);
+ fprintf (dump_file, " ");
+ print_generic_expr (dump_file, t2, TDF_UID);
+ fprintf (dump_file, "\n");
+}
+
+/* Analyze call graph edge with connected call stmt to find type aliases in
+ arguments and return value casts. */
+
+static void
+analyze_cgraph_edge (cgraph_edge *e)
+{
+ gcall *stmt = e->call_stmt;
+ gcc_assert (stmt != NULL);
+ tree fntype = get_call_fntype (stmt);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_call_stmt_info (stmt, fntype);
+ if (gimple_has_lhs (stmt))
+ {
+ tree t1 = TREE_TYPE (gimple_call_lhs (stmt));
+ tree t2 = TREE_TYPE (fntype);
+ const int is_return_arg = -1;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_arg_types_with_uids (is_return_arg, t1, t2);
+ maybe_register_non_void_aliases (t1, t2);
+ }
+
+ tree argt = TYPE_ARG_TYPES (fntype);
+ if (!argt)
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Finish call stmt analysis\n");
+ return;
+ }
+ gcc_assert (argt);
+ unsigned num_args = gimple_call_num_args (stmt);
+ for (unsigned i = 0; i < num_args && argt; ++i, argt = TREE_CHAIN (argt))
+ {
+ tree arg = gimple_call_arg (stmt, i);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_arg_types_with_uids (i, TREE_VALUE (argt), TREE_TYPE (arg));
+ if (TREE_VALUE (argt) == TREE_TYPE (arg)
+ || !POINTER_TYPE_P (TREE_VALUE (argt))
+ || !POINTER_TYPE_P (TREE_TYPE (arg)))
+ continue;
+ maybe_register_non_void_aliases (TREE_VALUE (argt), TREE_TYPE (arg));
+ tree t1 = TREE_TYPE (TREE_VALUE (argt));
+ tree t2 = TREE_TYPE (TREE_TYPE (arg));
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Call's %d-arg base types: (%d) and (%d)\n",
+ i, (t1 ? TYPE_UID (t1) : 0), (t2 ? TYPE_UID (t2) : 0));
+ maybe_register_non_void_aliases (t1, t2);
+ }
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "End list of args\n");
+ tree fndecl_type = NULL;
+ if (e->callee && e->callee->decl)
+ fndecl_type = TREE_TYPE (e->callee->decl);
+ if (fndecl_type && fndecl_type != fntype)
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Function decl and edge types mismatch:\n");
+ register_ailas_type (fntype, fndecl_type, fta_map);
+ }
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "End call stmt analysis\n");
+}
+
+static void
+dump_assign_info (gimple *stmt, tree rhs, tree lhs_type, tree rhs_type)
+{
+ fprintf (dump_file, "\nAnalyse assign cast/copy stmt, rhs=%s: ",
+ get_tree_code_name (TREE_CODE (rhs)));
+ print_gimple_stmt (dump_file, stmt, 3, TDF_DETAILS);
+ fprintf (dump_file, "Types: ");
+ print_generic_expr (dump_file, lhs_type);
+ fprintf (dump_file, ", ");
+ print_generic_expr (dump_file, rhs_type);
+ fprintf (dump_file, "\n");
+}
+
+/* Analyze cast/copy assign stmt to find type aliases. */
+
+static void
+analyze_assign_stmt (gimple *stmt)
+{
+ gcc_assert (is_gimple_assign (stmt));
+ tree rhs_type = NULL_TREE;
+ tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
+ tree rhs = gimple_assign_rhs1 (stmt);
+ if (TREE_CODE (rhs) == MEM_REF)
+ {
+ rhs = TREE_OPERAND (rhs, 0);
+ tree ptr_type = TREE_TYPE (rhs);
+ gcc_assert (POINTER_TYPE_P (ptr_type));
+ rhs_type = TREE_TYPE (ptr_type);
+ }
+ else if (TREE_CODE (rhs) == ADDR_EXPR)
+ {
+ rhs = TREE_OPERAND (rhs, 0);
+ if (VAR_OR_FUNCTION_DECL_P (rhs) || TREE_CODE (rhs) == STRING_CST
+ || TREE_CODE (rhs) == ARRAY_REF || TREE_CODE (rhs) == PARM_DECL)
+ rhs_type = build_pointer_type (TREE_TYPE (rhs));
+ else if (TREE_CODE (rhs) == COMPONENT_REF)
+ {
+ rhs = TREE_OPERAND (rhs, 1);
+ rhs_type = build_pointer_type (TREE_TYPE (rhs));
+ }
+ else if (TREE_CODE (rhs) == MEM_REF)
+ {
+ rhs = TREE_OPERAND (rhs, 0);
+ rhs_type = TREE_TYPE (rhs);
+ gcc_assert (POINTER_TYPE_P (rhs_type));
+ }
+ else
+ gcc_unreachable();
+ }
+ else
+ rhs_type = TREE_TYPE (rhs);
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_assign_info (stmt, rhs, lhs_type, rhs_type);
+ if (CONSTANT_CLASS_P (rhs) && !zerop (rhs)
+ && FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs)))
+ {
+ tree ftype = TREE_TYPE (rhs_type);
+ unsafe_types->insert (TYPE_UID (ftype));
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Function type (%d) is unsafe due to assign "
+ "non-zero cst to function pointer\n", TYPE_UID (ftype));
+ }
+ maybe_register_non_void_aliases (lhs_type, rhs_type);
+}
+
+/* Walk all fn's stmt to analyze assigns. */
+
+static void
+analyze_assigns (function* fn)
+{
+ push_cfun (fn);
+ basic_block bb;
+ gimple_stmt_iterator si;
+ FOR_EACH_BB_FN (bb, fn)
+ for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
+ {
+ gimple *stmt = gsi_stmt (si);
+ if (!gimple_assign_cast_p (stmt) && !gimple_assign_copy_p (stmt))
+ continue;
+ analyze_assign_stmt (stmt);
+ }
+ pop_cfun ();
+}
+
+/* Walk all functions to collect sets of type aliases. */
+
+static void
+collect_type_alias_sets ()
+{
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\n\nCollect type alias sets walking global vars.\n");
+
+ varpool_node *var;
+ FOR_EACH_VARIABLE (var)
+ if (var->real_symbol_p ())
+ analyze_global_var (var);
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\nCollect type alias sets walking functions.\n");
+
+ struct cgraph_node *n;
+ FOR_EACH_FUNCTION (n)
+ {
+ if (!n->has_gimple_body_p ())
+ continue;
+ n->get_body ();
+ function *fn = DECL_STRUCT_FUNCTION (n->decl);
+ if (!fn)
+ continue;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_function_node_info (n);
+ /* Analyze direct/indirect function calls. */
+ for (cgraph_edge *e = n->callees; e; e = e->next_callee)
+ analyze_cgraph_edge (e);
+ for (cgraph_edge *e = n->indirect_calls; e; e = e->next_callee)
+ analyze_cgraph_edge (e);
+ /* Analyze assign (with casts) statements. */
+ analyze_assigns (fn);
+ }
+}
+
+static void
+process_cbase_to_ptype_map ()
+{
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\nProcess types in cbase-to-ptypes map:\n");
+
+ for (type_alias_map::iterator it1 = cbase_to_ptype->begin ();
+ it1 != cbase_to_ptype->end (); ++it1)
+ {
+ type_set *set = it1->second;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_type_uid_with_set ("cb=(%d): ", (*type_uid_map)[it1->first],
+ cbase_to_ptype);
+ tree ctype = NULL;
+ for (type_set::const_iterator it2 = set->begin ();
+ it2 != set->end (); it2++)
+ {
+ tree t2 = (*type_uid_map)[*it2];
+ if (t2 == TYPE_MAIN_VARIANT (t2))
+ {
+ ctype = t2;
+ break;
+ }
+ }
+ if (!ctype)
+ continue;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_type_with_uid ("Select canonical type: ", ctype);
+ for (type_set::const_iterator it2 = set->begin ();
+ it2 != set->end (); it2++)
+ {
+ tree t = (*type_uid_map)[*it2];
+ if (!ctype_map->count (t))
+ {
+ (*ctype_map)[t] = ctype;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Set canonical type for (%d)->c(%d)\n",
+ *it2, TYPE_UID (ctype));
+ }
+ else if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Canonical type is already set (%d)->c(%d)\n",
+ *it2, TYPE_UID ((*ctype_map)[t]));
+ }
+ }
+}
+
+static void
+set_canonical_type_for_type_set (type_set *set)
+{
+ tree one_canonical = NULL;
+ for (type_set::const_iterator it = set->begin (); it != set->end (); it++)
+ {
+ tree t = (*type_uid_map)[*it];
+ gcc_assert (t);
+ if ((TYPE_CANONICAL (t) || ctype_map->count (t)))
+ {
+ one_canonical = TYPE_CANONICAL (t) ? TYPE_CANONICAL (t)
+ : (*ctype_map)[t];
+ gcc_assert (COMPLETE_TYPE_P (t));
+ break;
+ }
+ }
+ for (type_set::const_iterator it = set->begin (); it != set->end (); it++)
+ {
+ tree t = (*type_uid_map)[*it];
+ if (!ctype_map->count (t))
+ {
+ (*ctype_map)[t] = one_canonical;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ if (one_canonical)
+ fprintf (dump_file, "Set canonical type for (%d)->c(%d)\n",
+ TYPE_UID (t), TYPE_UID (one_canonical));
+ else
+ fprintf (dump_file, "Set NULL canonical for (%d)\n", *it);
+ }
+ }
+ else if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ tree ct = (*ctype_map)[t];
+ fprintf (dump_file, "Canonical type is already set (%d)->c(%d)\n",
+ TYPE_UID (t), ct ? TYPE_UID (ct) : -1);
+ }
+ }
+}
+
+static void
+dump_is_type_set_incomplete (type_set * set)
+{
+ bool has_complete_types = false;
+ for (type_set::const_iterator it = set->begin (); it != set->end (); it++)
+ if (COMPLETE_TYPE_P ((*type_uid_map)[*it]))
+ {
+ has_complete_types = true;
+ break;
+ }
+ if (!has_complete_types)
+ fprintf (dump_file, "Set of incomplete types\n");
+}
+
+static void
+process_alias_type_sets ()
+{
+ if (dump_file)
+ fprintf (dump_file, "\nProcess alias sets of types:\n");
+ /* Keep processed types to process each type set (in ta_map) only once. */
+ type_set processed_types;
+ for (type_alias_map::iterator it1 = ta_map->begin ();
+ it1 != ta_map->end (); ++it1)
+ {
+ tree type = (*type_uid_map)[it1->first];
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_type_uid_with_set ("(%d) ", type, ta_map);
+ if (processed_types.count (TYPE_UID (type)) != 0
+ || unsafe_types->count (TYPE_UID (type)) != 0)
+ continue;
+ type_set *set = it1->second;
+ for (type_set::const_iterator it2 = set->begin ();
+ it2 != set->end (); it2++)
+ processed_types.insert (*it2);
+ /* Check if this type set contains function pointers and
+ non-function pointers. */
+ bool has_no_fp = false, has_fp = false;
+ for (type_set::const_iterator it2 = set->begin ();
+ it2 != set->end (); it2++)
+ {
+ tree t2 = (*type_uid_map)[*it2];
+ if (FUNCTION_POINTER_TYPE_P (t2))
+ has_fp = true;
+ else
+ has_no_fp = true;
+ if (has_fp && has_no_fp)
+ break;
+ }
+ if (has_fp)
+ {
+ for (type_set::const_iterator it2 = set->begin ();
+ it2 != set->end (); it2++)
+ {
+ tree t2 = (*type_uid_map)[*it2];
+ /* If it's a type set with mixed function and not-function types,
+ mark all function pointer types in the set as unsafe. */
+ if (has_no_fp && FUNCTION_POINTER_TYPE_P (t2))
+ {
+ tree ftype = TREE_TYPE (t2);
+ unsafe_types->insert (TYPE_UID (ftype));
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Insert function type (%d) to unsafe "
+ "due to escape its pointer type (%d) to mixed "
+ "alias set (printed before)\n",
+ TYPE_UID (ftype), TYPE_UID (t2));
+ }
+ /* If it's a type set with only function pointer types,
+ mark all base function types in the set as aliases. */
+ if (!has_no_fp)
+ {
+ gcc_assert (FUNCTION_POINTER_TYPE_P (type)
+ && FUNCTION_POINTER_TYPE_P (t2));
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Insert function type aliases by "
+ "function pointer aliases:\n");
+ register_ailas_type (TREE_TYPE (type), TREE_TYPE (t2),
+ fta_map);
+ }
+ }
+ }
+ set_canonical_type_for_type_set (set);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_is_type_set_incomplete (set);
+ }
+}
+
+static void
+dump_unsafe_and_canonical_types ()
+{
+ fprintf (dump_file, "\nList of unsafe types:\n");
+ for (type_set::iterator it = unsafe_types->begin ();
+ it != unsafe_types->end (); ++it)
+ {
+ print_generic_expr (dump_file, (*type_uid_map)[*it]);
+ fprintf (dump_file, " (%d)\n", *it);
+ }
+ fprintf (dump_file, "\nList of alias canonical types:\n");
+ for (type_alias_map::iterator it = ta_map->begin ();
+ it != ta_map->end (); ++it)
+ {
+ tree type = (*type_uid_map)[it->first];
+ if (ctype_map->count (type) == 0)
+ continue;
+ print_generic_expr (dump_file, type);
+ fprintf (dump_file, " -> ");
+ tree ctype = (*ctype_map)[type];
+ if (ctype != NULL)
+ {
+ print_generic_expr (dump_file, ctype);
+ fprintf (dump_file, " (%d)->(%d)\n",
+ TYPE_UID (type), TYPE_UID (ctype));
+ }
+ else
+ fprintf (dump_file, " null\n");
+ }
+}
+
+static void
+init_function_type_alias_for_edge (cgraph_edge *e)
+{
+ gcall *stmt = e->call_stmt;
+ gcc_assert (stmt != NULL);
+ tree fntype = get_call_fntype (stmt);
+ if (fta_map->count (TYPE_UID (fntype)) == 0)
+ register_ailas_type (fntype, fntype, fta_map);
+}
+
+/* This pass over all function types makes each function type to have
+ at least one alias (itself). */
+
+static void
+init_function_type_aliases ()
+{
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\nInit aliases for all function types.\n");
+
+ struct cgraph_node *n;
+ FOR_EACH_FUNCTION (n)
+ {
+ tree fntype = TREE_TYPE (n->decl);
+ if (fta_map->count (TYPE_UID (fntype)) == 0)
+ register_ailas_type (fntype, fntype, fta_map);
+
+ if (!n->has_gimple_body_p ())
+ continue;
+ n->get_body ();
+ function *fn = DECL_STRUCT_FUNCTION (n->decl);
+ if (!fn)
+ continue;
+
+ /* Init for function types of direct/indirect callees. */
+ for (cgraph_edge *e = n->callees; e; e = e->next_callee)
+ init_function_type_alias_for_edge (e);
+ for (cgraph_edge *e = n->indirect_calls; e; e = e->next_callee)
+ init_function_type_alias_for_edge (e);
+ }
+}
+
+/* In lto-common.c there is the global canonical type table and the
+ corresponding machinery which detects the same types from differens
+ modules and joins them assigning the one canonical type. However
+ lto does not set the goal to do a complete and precise matching, so
+ sometimes a few types has no TYPE_CANONICAL set. Since ICP relies on
+ precise type matching, we create the similar table and register all
+ the required types in it. */
+
+static std::map<const_tree, hashval_t> *canonical_type_hash_cache = NULL;
+static std::map<hashval_t, tree> *icp_canonical_types = NULL;
+
+static hashval_t hash_canonical_type (tree type);
+
+/* Register canonical type in icp_canonical_types and ctype_map evaluating
+ its hash (using hash_canonical_type) if it's needed. */
+
+static hashval_t
+icp_register_canonical_type (tree t)
+{
+ hashval_t hash;
+ if (canonical_type_hash_cache->count ((const_tree) t) == 0)
+ {
+ tree t1 = TYPE_MAIN_VARIANT (t);
+ if (!COMPLETE_TYPE_P (t1) && TYPE_CANONICAL (t1)
+ && COMPLETE_TYPE_P (TYPE_CANONICAL (t1)))
+ {
+ t1 = TYPE_CANONICAL (t1);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Use complete canonical (%d) for (%d)\n",
+ TYPE_UID (t1), TYPE_UID (t));
+ }
+ hash = hash_canonical_type (t1);
+ /* Cache the just computed hash value. */
+ (*canonical_type_hash_cache)[(const_tree) t] = hash;
+ }
+ else
+ hash = (*canonical_type_hash_cache)[(const_tree) t];
+
+ tree new_type = t;
+ if (icp_canonical_types->count (hash))
+ {
+ new_type = (*icp_canonical_types)[hash];
+ gcc_checking_assert (new_type != t);
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Found canonical (%d) for (%d), h=%u\n",
+ TYPE_UID (new_type), TYPE_UID (t), (unsigned int) hash);
+ }
+ else
+ {
+ (*icp_canonical_types)[hash] = t;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Register canonical %d, h=%u\n", TYPE_UID (t),
+ (unsigned int) hash);
+ }
+ if (ctype_map->count (t) == 0)
+ (*ctype_map)[t] = new_type;
+ return hash;
+}
+
+/* Merge hstate with hash of the given type. If the type is not registered,
+ register it in the maps of the canonical types. */
+
+static void
+iterative_hash_canonical_type (tree type, inchash::hash &hstate)
+{
+ hashval_t v;
+ /* All type variants have same TYPE_CANONICAL. */
+ type = TYPE_MAIN_VARIANT (type);
+ if (canonical_type_hash_cache->count ((const_tree) type))
+ v = (*canonical_type_hash_cache)[(const_tree) type];
+ else
+ v = icp_register_canonical_type (type);
+ hstate.merge_hash (v);
+}
+
+/* Compute and return hash for the given type. It does not take into account
+ base types of pointer types. */
+
+static hashval_t
+hash_canonical_type (tree type)
+{
+ inchash::hash hstate;
+ enum tree_code code;
+ /* Combine a few common features of types so that types are grouped into
+ smaller sets; when searching for existing matching types to merge,
+ only existing types having the same features as the new type will be
+ checked. */
+ code = tree_code_for_canonical_type_merging (TREE_CODE (type));
+ hstate.add_int (code);
+ if (!RECORD_OR_UNION_TYPE_P (type))
+ hstate.add_int (TYPE_MODE (type));
+ /* Incorporate common features of numerical types. */
+ if (INTEGRAL_TYPE_P (type)
+ || SCALAR_FLOAT_TYPE_P (type)
+ || FIXED_POINT_TYPE_P (type)
+ || TREE_CODE (type) == OFFSET_TYPE
+ || POINTER_TYPE_P (type))
+ {
+ hstate.add_int (TYPE_PRECISION (type));
+ if (!type_with_interoperable_signedness (type))
+ hstate.add_int (TYPE_UNSIGNED (type));
+ }
+ if (VECTOR_TYPE_P (type))
+ {
+ hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
+ hstate.add_int (TYPE_UNSIGNED (type));
+ }
+ if (TREE_CODE (type) == COMPLEX_TYPE)
+ hstate.add_int (TYPE_UNSIGNED (type));
+ if (POINTER_TYPE_P (type))
+ hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
+ /* For array types hash the domain bounds and the string flag. */
+ if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
+ {
+ hstate.add_int (TYPE_STRING_FLAG (type));
+ /* OMP lowering can introduce error_mark_node in place of
+ random local decls in types. */
+ if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
+ inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
+ if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
+ inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
+ }
+ /* Recurse for aggregates with a single element type. */
+ if (TREE_CODE (type) == ARRAY_TYPE
+ || TREE_CODE (type) == COMPLEX_TYPE
+ || TREE_CODE (type) == VECTOR_TYPE)
+ iterative_hash_canonical_type (TREE_TYPE (type), hstate);
+ /* Incorporate function return and argument types. */
+ if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
+ {
+ unsigned nargs = 0;
+ iterative_hash_canonical_type (TREE_TYPE (type), hstate);
+ for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
+ {
+ iterative_hash_canonical_type (TREE_VALUE (p), hstate);
+ nargs++;
+ }
+ hstate.add_int (nargs);
+ }
+ if (RECORD_OR_UNION_TYPE_P (type))
+ {
+ unsigned nfields = 0;
+ for (tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
+ if (TREE_CODE (f) == FIELD_DECL)
+ {
+ iterative_hash_canonical_type (TREE_TYPE (f), hstate);
+ nfields++;
+ }
+ hstate.add_int (nfields);
+ }
+ return hstate.end ();
+}
+
+/* It finds canonical type in ctype_map and icp_canonical_types maps. */
+
+static tree
+find_canonical_type (tree type)
+{
+ if (ctype_map->count (type))
+ return (*ctype_map)[type];
+ if (canonical_type_hash_cache->count ((const_tree) type) == 0)
+ return NULL;
+ hashval_t h = (*canonical_type_hash_cache)[(const_tree) type];
+ if (icp_canonical_types->count (h))
+ return (*icp_canonical_types)[h];
+ return NULL;
+}
+
+/* It updates hash for the given type taking into account pointees in pointer
+ types. If the type is incomplete function type, it returns true. It's used
+ only for function type hash calculation. */
+
+static bool
+initial_hash_canonical_type (tree type, inchash::hash &hstate)
+{
+ /* All type variants have same TYPE_CANONICAL. */
+ type = TYPE_MAIN_VARIANT (type);
+ if (VOID_TYPE_P (type))
+ {
+ hstate.add_int (POINTER_TYPE);
+ return false;
+ }
+ hstate.add_int (TREE_CODE (type));
+ hstate.add_int (TYPE_MODE (type));
+ if (POINTER_TYPE_P (type))
+ {
+ tree base_type = TREE_TYPE (type);
+ hstate.add_int (TYPE_ADDR_SPACE (base_type));
+ return initial_hash_canonical_type (base_type, hstate);
+ }
+ tree ctype = find_canonical_type (type);
+ if (!ctype)
+ {
+ if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Due to ftype (%d)\n", TYPE_UID (type));
+ return true;
+ }
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_type_with_uid ("Has NO canonical type: ", type, TDF_UID);
+ icp_register_canonical_type (type);
+ if (ctype_map->count(type))
+ ctype = (*ctype_map)[type];
+ if (ctype && dump_file && (dump_flags & TDF_DETAILS))
+ dump_type_with_uid ("Found canonical type: ", ctype, TDF_UID);
+ }
+ else if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_type_with_uid ("Canonical type: ", ctype, TDF_UID);
+ hstate.add_int (TYPE_UID (ctype));
+ return false;
+}
+
+/* It returns hash value for the given function type. If the function type is
+ incomplete, insert it in the incomplete_hash_ftype set. */
+
+static hashval_t
+get_hash_for_ftype (tree type, type_set *incomplete_hash_ftype)
+{
+ bool incomplete = false;
+ inchash::hash hstate;
+ /* Function type is expected. */
+ gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
+ || TREE_CODE (type) == METHOD_TYPE);
+ /* Hash return type. */
+ tree rt = TREE_TYPE (type);
+ tree ct = rt ? find_canonical_type (rt) : void_type_node;
+ incomplete |= initial_hash_canonical_type (ct ? ct : rt, hstate);
+ /* Hash arg types. */
+ tree argt = TYPE_ARG_TYPES (type);
+ if (!argt)
+ incomplete |= initial_hash_canonical_type (void_type_node, hstate);
+ else
+ for (unsigned i = 1; argt; ++i, argt = TREE_CHAIN (argt))
+ {
+ tree ct = find_canonical_type (TREE_VALUE (argt));
+ ct = ct ? ct : TREE_VALUE (argt);
+ incomplete |= initial_hash_canonical_type (ct, hstate);
+ }
+ if (incomplete && incomplete_hash_ftype->count (TYPE_UID (type)) == 0)
+ incomplete_hash_ftype->insert (TYPE_UID (type));
+ else if (!incomplete && incomplete_hash_ftype->count (TYPE_UID (type)) != 0)
+ incomplete_hash_ftype->erase (TYPE_UID (type));
+ return hstate.end();
+}
+
+/* Find type aliases evaluating type hashes and connecting types with
+ the same hash values. */
+
+static void
+find_type_aliases_by_compatibility ()
+{
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\nFind type aliases checking their compatibility.\n");
+
+ std::map<hashval_t, tree> hash_to_ftype;
+ type_set *incomplete_hash_ftype = new type_set;
+ canonical_type_hash_cache = new std::map<const_tree, hashval_t>;
+ icp_canonical_types = new std::map<hashval_t, tree>;
+
+ bool changed;
+ int i = 0;
+ do
+ {
+ changed = false;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Iteration %d\n", i);
+ for (type_alias_map::iterator it = fta_map->begin ();
+ it != fta_map->end (); ++it)
+ {
+ tree type = (*type_uid_map)[it->first];
+ if (TYPE_CANONICAL (type))
+ continue;
+ hashval_t hash = get_hash_for_ftype (type, incomplete_hash_ftype);
+ if (incomplete_hash_ftype->count (TYPE_UID (type)) != 0)
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Incomplete (%d), h=%u\n", TYPE_UID (type),
+ (unsigned int) hash);
+ continue;
+ }
+ if (hash_to_ftype.count (hash) == 0)
+ hash_to_ftype[hash] = type;
+ TYPE_CANONICAL (type) = hash_to_ftype[hash];
+ changed = true;
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "(%d)->(%d), h=%u\n", TYPE_UID (type),
+ TYPE_UID (TYPE_CANONICAL (type)), (unsigned int) hash);
+ }
+ i++;
+ }
+ while (changed);
+
+ delete incomplete_hash_ftype;
+ delete icp_canonical_types;
+ delete canonical_type_hash_cache;
+}
+
+static void
+dump_function_type_aliases_list ()
+{
+ fprintf (dump_file, "\nList of function type aliases:\n");
+ for (type_alias_map::iterator it = fta_map->begin ();
+ it != fta_map->end (); ++it)
+ dump_type_uid_with_set ("(%d) ", (*type_uid_map)[it->first], fta_map);
+}
+
+/* Collect type aliases and find missed canonical types. */
+
+static void
+collect_function_type_aliases ()
+{
+ collect_type_alias_sets ();
+ process_cbase_to_ptype_map ();
+ process_alias_type_sets ();
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_unsafe_and_canonical_types ();
+
+ /* TODO: maybe remove this pass. */
+ init_function_type_aliases ();
+ for (type_alias_map::iterator it = fta_map->begin ();
+ it != fta_map->end (); ++it)
+ set_canonical_type_for_type_set (it->second);
+ find_type_aliases_by_compatibility ();
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ dump_function_type_aliases_list ();
+}
+
+static void
+dump_function_signature_info (struct cgraph_node *n, tree ftype, bool varargs)
+{
+ fprintf (dump_file, "Function decl: ");
+ print_generic_expr (dump_file, n->decl);
+ dump_type_uid_with_set (" with type (%d) ", ftype, fta_map, true, false);
+ if (varargs)
+ fprintf (dump_file, "has varargs, ");
+ if (TREE_CODE (ftype) == METHOD_TYPE)
+ fprintf (dump_file, "is method, ");
+ if (!n->address_taken)
+ fprintf (dump_file, "is not address taken, ");
+ if (unsafe_types->count (TYPE_UID (ftype)))
+ fprintf (dump_file, "is unsafe, ");
+ fprintf (dump_file, "\n");
+}
+
+/* Check if the function has variadic arguments.
+ It's corrected count_num_arguments (). */
+
+static bool
+has_varargs (tree decl)
+{
+ tree t;
+ unsigned int num = 0;
+ for (t = TYPE_ARG_TYPES (TREE_TYPE (decl));
+ t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
+ num++;
+ if (!t && num)
+ return true;
+ return false;
+}
+
+/* Join fs_map's sets for function type aliases. */
+
+static void
+merge_fs_map_for_ftype_aliases ()
+{
+ if (dump_file)
+ fprintf (dump_file, "\n\nMerge decl sets for function type aliases:\n");
+ type_set processed_types;
+ for (type_decl_map::iterator it1 = fs_map->begin ();
+ it1 != fs_map->end (); ++it1)
+ {
+ if (processed_types.count (it1->first) != 0)
+ continue;
+ decl_set *d_set = it1->second;
+ tree type = (*type_uid_map)[it1->first];
+ type_set *set = (*fta_map)[it1->first];
+ for (type_set::const_iterator it2 = set->begin ();
+ it2 != set->end (); it2++)
+ {
+ tree t2 = (*type_uid_map)[*it2];
+ processed_types.insert (*it2);
+ if (type == t2)
+ continue;
+ gcc_assert ((TREE_CODE (type) == FUNCTION_TYPE
+ || TREE_CODE (type) == METHOD_TYPE)
+ && (TREE_CODE (t2) == FUNCTION_TYPE
+ || TREE_CODE (t2) == METHOD_TYPE));
+ if (fs_map->count (*it2) == 0 || (*fs_map)[*it2] == NULL)
+ (*fs_map)[*it2] = d_set;
+ else
+ {
+ decl_set *t2_decl_set = (*fs_map)[*it2];
+ (*fs_map)[*it2] = d_set;
+ gcc_assert (t2_decl_set && t2_decl_set->size() > 0);
+ d_set->insert (t2_decl_set->begin (), t2_decl_set->end ());
+ delete t2_decl_set;
+ }
+ }
+ }
+}
+
+/* Dump function types with set of functions corresponding to it. */
+
+static void
+dump_function_signature_sets ()
+{
+ fprintf (dump_file, "\n\nUnique sets of function signatures:\n");
+ std::set<decl_set *> processed_sets;
+ for (type_decl_map::iterator it1 = fs_map->begin ();
+ it1 != fs_map->end (); ++it1)
+ {
+ decl_set *set = it1->second;
+ if (processed_sets.count (set) != 0)
+ continue;
+ processed_sets.insert (set);
+ fprintf (dump_file, "{ ");
+ print_type_set (it1->first, fta_map);
+ fprintf (dump_file, " : ");
+ for (decl_set::const_iterator it2 = set->begin ();
+ it2 != set->end (); it2++)
+ {
+ fprintf (dump_file, it2 == set->begin () ? "" : ", ");
+ print_generic_expr (dump_file, *it2);
+ fprintf (dump_file, "(%d)", DECL_UID (*it2));
+ }
+ fprintf (dump_file, "}\n");
+ }
+}
+
+/* Fill the map of function types to sets of function decls. */
+
+static void
+collect_function_signatures ()
+{
+ if (dump_file)
+ fprintf (dump_file, "\n\nCollect function signatures:\n");
+ struct cgraph_node *n;
+ FOR_EACH_FUNCTION (n)
+ {
+ gcc_assert (n->decl && TREE_TYPE (n->decl));
+ tree ftype = TREE_TYPE (n->decl);
+ bool varargs = has_varargs (n->decl);
+ if (varargs && n->address_taken)
+ has_address_taken_functions_with_varargs = true;
+ if (dump_file)
+ dump_function_signature_info (n, ftype, varargs);
+ if (!n->address_taken)
+ continue;
+ /* TODO: make a separate pass at the end to remove canonicals. */
+ tree ctype = TYPE_CANONICAL (ftype);
+ unsigned alias_type_fs = ctype ? TYPE_UID (ctype) : 0;
+ if (dump_file)
+ fprintf (dump_file, "canonical type: %d %ld\n",
+ alias_type_fs, fs_map->count (alias_type_fs));
+ if (alias_type_fs)
+ {
+ if (fs_map->count (TYPE_UID (ctype)) == 0)
+ (*fs_map)[TYPE_UID (ctype)] = new decl_set ();
+ if (dump_file)
+ fprintf (dump_file, "insert decl (%d) to set of map [%d]\n",
+ DECL_UID (n->decl), TYPE_UID (ctype));
+ (*fs_map)[TYPE_UID (ctype)]->insert (n->decl);
+ }
+ }
+ merge_fs_map_for_ftype_aliases ();
+ if (dump_file)
+ dump_function_signature_sets ();
+}
+
+#define MAX_TARG_STAT 4
+struct icp_stats
+{
+ int npolymorphic;
+ int nspeculated;
+ int nsubst;
+ int ncold;
+ int nmultiple;
+ int noverwritable;
+ int nnotdefined;
+ int nexternal;
+ int nartificial;
+ int nremove;
+ int nicp;
+ int nspec;
+ int nf;
+ int ncalls;
+ int nindir;
+ int nind_only;
+ int ntargs[MAX_TARG_STAT + 1];
+};
+
+static void
+dump_processing_function (struct cgraph_node *n, struct icp_stats &stats)
+{
+ fprintf (dump_file, "\n\nProcesing function %s\n", n->dump_name ());
+ print_generic_expr (dump_file, n->decl);
+ fprintf (dump_file, "\n");
+ dump_type_with_uid ("Func's type: ", TREE_TYPE (n->decl));
+ if (dump_file && (dump_flags & TDF_STATS))
+ {
+ struct cgraph_edge *e;
+ stats.nf++;
+ for (e = n->indirect_calls; e; e = e->next_callee)
+ stats.nindir++;
+ for (e = n->callees; e; e = e->next_callee)
+ stats.ncalls++;
+ stats.ncalls += stats.nindir;
+ if (n->callers == NULL)
+ {
+ fprintf (dump_file, "Function has NO callers\n");
+ stats.nind_only++;
+ }
+ }
+}
+
+static void
+dump_indirect_call_site (tree call_fn, tree call_fn_ty)
+{
+ fprintf (dump_file, "Indirect call site: ");
+ print_generic_expr (dump_file, call_fn);
+ dump_type_with_uid ("\nFunction pointer type: ", call_fn_ty);
+}
+
+static void
+erase_from_unreachable (unsigned type_uid, type_set &unreachable)
+{
+ unreachable.erase (type_uid);
+ if (!fta_map->count (type_uid))
+ return;
+ type_set *set = (*fta_map)[type_uid];
+ for (type_set::const_iterator it = set->begin (); it != set->end (); it++)
+ unreachable.erase (*it);
+}
+
+static void
+dump_found_fdecls (decl_set *decls, unsigned ctype_uid)
+{
+ fprintf (dump_file, "Signature analysis FOUND decls (%d):", ctype_uid);
+ for (decl_set::const_iterator it = decls->begin (); it != decls->end (); it++)
+ {
+ print_generic_expr (dump_file, *it);
+ fprintf (dump_file, "(%d), ", DECL_UID (*it));
+ }
+ if (unsafe_types->count (ctype_uid))
+ fprintf (dump_file, "type is UNSAFE");
+ fprintf (dump_file, "\n");
+}
+
+static void
+count_found_targets (struct icp_stats &stats, unsigned size)
+{
+ gcc_assert (size > 0);
+ stats.ntargs[size > MAX_TARG_STAT ? MAX_TARG_STAT : size - 1]++;
+}
+
+/* Promote the indirect call. */
+
+static void
+promote_call (struct cgraph_edge *e, struct cgraph_node *n,
+ struct cgraph_node *likely_target, struct icp_stats *stats)
+{
+ if (dump_enabled_p ())
+ {
+ dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
+ "promoting indirect call in %s to %s\n",
+ n->dump_name (), likely_target->dump_name ());
+ }
+ if (!likely_target->can_be_discarded_p ())
+ {
+ symtab_node *sn = likely_target->noninterposable_alias ();
+ cgraph_node *alias = dyn_cast<cgraph_node *> (sn);
+ if (alias)
+ likely_target = alias;
+ }
+ gimple *new_call;
+ if (flag_icp_speculatively)
+ {
+ e->make_speculative (likely_target, e->count.apply_scale (5, 10));
+ new_call = e->call_stmt;
+ stats->nspec++;
+ }
+ else
+ {
+ cgraph_edge *e2 = cgraph_edge::make_direct (e, likely_target);
+ new_call = cgraph_edge::redirect_call_stmt_to_callee (e2);
+ stats->nsubst++;
+ }
+ if (dump_file)
+ {
+ fprintf (dump_file, "The call is substituted by: ");
+ print_gimple_stmt (dump_file, new_call, 0);
+ fprintf (dump_file, "\n");
+ }
+}
+
+/* Find functions which are called only indirectly and if they are not in
+ fs_map, they can be removed. For now it is used only to print stats. */
+
+static int
+find_functions_can_be_removed (type_set &unreachable)
+{
+ int nremove = 0;
+ if (dump_file)
+ fprintf (dump_file, "\nRemove unused functions:\n");
+ struct cgraph_node *n;
+ FOR_EACH_FUNCTION (n)
+ {
+ gcc_assert (n->decl && TREE_TYPE (n->decl));
+ if (n->callers != NULL)
+ continue;
+ tree ftype = TREE_TYPE (n->decl);
+ tree ctype = TYPE_CANONICAL (ftype);
+ if (!ctype || !unreachable.count (TYPE_UID (ctype))
+ || unsafe_types->count (TYPE_UID (ftype))
+ || TREE_CODE (ftype) == METHOD_TYPE || n->callers != NULL
+ || !n->definition || n->alias || n->thunk || n->clones)
+ continue;
+ if (dump_file)
+ fprintf (dump_file, "%s is not used\n", n->dump_name ());
+ nremove++;
+ }
+ return nremove;
+}
+
+static void
+dump_stats (struct icp_stats &st)
+{
+ fprintf (dump_file, "\nSTATS: %i candidates for indirect call promotion,"
+ " %i substituted, %i speculatively promoted, %i cold\n"
+ "%i have multiple targets, %i already speculated, %i external,"
+ " %i not defined, %i artificial, %i polymorphic calls,"
+ " %i overwritable\n", st.nicp, st.nsubst, st.nspec, st.ncold,
+ st.nmultiple, st.nspeculated, st.nexternal, st.nnotdefined,
+ st.nartificial, st.npolymorphic, st.noverwritable);
+ if (!(dump_flags & TDF_STATS))
+ return;
+ fprintf (dump_file, "EXTRA STATS: %i functions, %i indirect calls,"
+ " %i total calls, %i called only indirectly, %i may be removed\n"
+ "Indirect call sites with found targets ", st.nf, st.nindir,
+ st.ncalls, st.nind_only, st.nremove);
+ for (unsigned i = 0; i < MAX_TARG_STAT; i++)
+ fprintf (dump_file, "%u:%i, ", i + 1, st.ntargs[i]);
+ fprintf (dump_file, "more:%i\n", st.ntargs[MAX_TARG_STAT]);
+}
+
+/* Optimize indirect calls. When an indirect call has only one target,
+ promote it into a direct call. */
+
+static bool
+optimize_indirect_calls ()
+{
+ /* TODO: maybe move to the top of ipa_icp. */
+ if (has_address_taken_functions_with_varargs)
+ {
+ if (dump_file)
+ fprintf (dump_file, "\n\nAddress taken function with varargs is found."
+ " Skip the optimization.\n");
+ return false;
+ }
+ struct icp_stats stats = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, {0, 0, 0, 0, 0}};
+ /* At first assume all function types are unreadchable. */
+ type_set unreachable_ftypes;
+ if (dump_file && (dump_flags & TDF_STATS))
+ for (type_decl_map::iterator it = fs_map->begin ();
+ it != fs_map->end (); ++it)
+ unreachable_ftypes.insert (it->first);
+
+ struct cgraph_node *n;
+ FOR_EACH_DEFINED_FUNCTION (n)
+ {
+ if (dump_file)
+ dump_processing_function (n, stats);
+ struct cgraph_edge *e;
+ bool update = false;
+ if (!opt_for_fn (n->decl, flag_icp) || !n->has_gimple_body_p ()
+ || n->inlined_to || !n->indirect_calls)
+ {
+ if (dump_file)
+ fprintf (dump_file, "Skip the function\n");
+ continue;
+ }
+ /* If the function has indirect calls which are not polymorphic,
+ process its body, otherwise continue. */
+ bool non_polymorphic_calls = false;
+ for (e = n->indirect_calls; e; e = e->next_callee)
+ if (!e->indirect_info->polymorphic)
+ {
+ non_polymorphic_calls = true;
+ break;
+ }
+ if (!non_polymorphic_calls)
+ {
+ if (dump_file)
+ fprintf (dump_file, "All indirect calls are polymorphic,"
+ "skip...\n");
+ continue;
+ }
+ /* Get the function body to operate with call statements. */
+ n->get_body ();
+ /* Walk indirect call sites and apply the optimization. */
+ cgraph_edge *next;
+ for (e = n->indirect_calls; e; e = next)
+ {
+ next = e->next_callee;
+ if (e->indirect_info->polymorphic)
+ {
+ if (dump_file)
+ fprintf (dump_file, "Target is polymorphic, skip...\n\n");
+ stats.npolymorphic++;
+ continue;
+ }
+ stats.nicp++;
+ struct cgraph_node *likely_target = NULL;
+ gcall *stmt = e->call_stmt;
+ gcc_assert (stmt != NULL);
+ tree call_fn = gimple_call_fn (stmt);
+ tree call_fn_ty = TREE_TYPE (call_fn);
+ if (dump_file)
+ dump_indirect_call_site (call_fn, call_fn_ty);
+ tree decl = NULL_TREE;
+ if (POINTER_TYPE_P (call_fn_ty))
+ {
+ if (dump_file)
+ dump_type_with_uid ("Pointee type: ", TREE_TYPE (call_fn_ty));
+ if (dump_file && (dump_flags & TDF_STATS))
+ erase_from_unreachable (TYPE_UID (TREE_TYPE (call_fn_ty)),
+ unreachable_ftypes);
+ /* Try to use the signature analysis results. */
+ tree ctype = TYPE_CANONICAL (TREE_TYPE (call_fn_ty));
+ unsigned ctype_uid = ctype ? TYPE_UID (ctype) : 0;
+ if (ctype_uid && fs_map->count (ctype_uid))
+ {
+ if (dump_flags && (dump_flags & TDF_STATS))
+ erase_from_unreachable (ctype_uid, unreachable_ftypes);
+ decl_set *decls = (*fs_map)[ctype_uid];
+ if (dump_file)
+ dump_found_fdecls (decls, ctype_uid);
+ /* TODO: optimize for multple targets. */
+ if (!unsafe_types->count (ctype_uid) && decls->size () == 1)
+ {
+ decl = *(decls->begin ());
+ likely_target = cgraph_node::get (decl);
+ }
+ if (!unsafe_types->count (ctype_uid)
+ && (dump_flags & TDF_STATS))
+ count_found_targets (stats, decls->size ());
+ }
+ }
+ if (!decl || !likely_target)
+ {
+ if (dump_file)
+ fprintf (dump_file, "Callee is unknown\n\n");
+ continue;
+ }
+ if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
+ {
+ if (dump_file)
+ fprintf (dump_file, "Callee is method\n\n");
+ continue;
+ }
+ if (e->speculative)
+ {
+ if (dump_file)
+ fprintf (dump_file, "Call is already speculated\n\n");
+ stats.nspeculated++;
+ continue;
+ }
+ if (!likely_target->definition)
+ {
+ if (dump_file)
+ fprintf (dump_file, "Target is not a definition\n\n");
+ stats.nnotdefined++;
+ continue;
+ }
+ /* Do not introduce new references to external symbols. While we
+ can handle these just well, it is common for programs to
+ incorrectly with headers defining methods they are linked
+ with. */
+ if (DECL_EXTERNAL (likely_target->decl))
+ {
+ if (dump_file)
+ fprintf (dump_file, "Target is external\n\n");
+ stats.nexternal++;
+ continue;
+ }
+ /* Don't use an implicitly-declared destructor (c++/58678). */
+ struct cgraph_node *non_thunk_target
+ = likely_target->function_symbol ();
+ if (DECL_ARTIFICIAL (non_thunk_target->decl))
+ {
+ if (dump_file)
+ fprintf (dump_file, "Target is artificial\n\n");
+ stats.nartificial++;
+ continue;
+ }
+ if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
+ && likely_target->can_be_discarded_p ())
+ {
+ if (dump_file)
+ fprintf (dump_file, "Target is overwritable\n\n");
+ stats.noverwritable++;
+ continue;
+ }
+ else if (dbg_cnt (icp))
+ {
+ promote_call (e, n, likely_target, &stats);
+ update = true;
+ }
+ }
+ if (update)
+ ipa_update_overall_fn_summary (n);
+ }
+
+ if (dump_file && (dump_flags & TDF_STATS))
+ stats.nremove = find_functions_can_be_removed (unreachable_ftypes);
+
+ if (dump_file)
+ dump_stats (stats);
+ return stats.nsubst || stats.nspec;
+}
+
+/* Delete the given MAP with allocated sets. One set may be associated with
+ more then one type/decl. */
+
+template <typename MAP>
+static void
+remove_type_alias_map (MAP *map)
+{
+ std::set<typename MAP::mapped_type> processed_sets;
+ for (typename MAP::iterator it = map->begin (); it != map->end (); it++)
+ {
+ typename MAP::mapped_type set = it->second;
+ if (processed_sets.count (set) != 0)
+ continue;
+ processed_sets.insert (set);
+ delete set;
+ }
+ delete map;
+}
+
+/* The ipa indirect call promotion pass. Run required analysis and optimize
+ indirect calls.
+ When indirect call has only one target, promote it into a direct call. */
+
+static unsigned int
+ipa_icp (void)
+{
+ ta_map = new type_alias_map;
+ fta_map = new type_alias_map;
+ cbase_to_ptype = new type_alias_map;
+ fs_map = new type_decl_map;
+ ctype_map = new type_map;
+ unsafe_types = new type_set;
+ type_uid_map = new uid_to_type_map;
+
+ /* Find type aliases, fill the function signature map and
+ optimize indirect calls. */
+ collect_function_type_aliases ();
+ collect_function_signatures ();
+ bool optimized = optimize_indirect_calls ();
+
+ remove_type_alias_map (ta_map);
+ remove_type_alias_map (fta_map);
+ remove_type_alias_map (cbase_to_ptype);
+ remove_type_alias_map (fs_map);
+ delete ctype_map;
+ delete unsafe_types;
+ delete type_uid_map;
+
+ return optimized ? TODO_remove_functions : 0;
+}
+
+namespace {
+
+const pass_data pass_data_ipa_icp =
+{
+ IPA_PASS, /* type */
+ "icp", /* name */
+ OPTGROUP_NONE, /* optinfo_flags */
+ TV_IPA_ICP, /* tv_id */
+ 0, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ 0, /* todo_flags_finish */
+};
+
+class pass_ipa_icp : public ipa_opt_pass_d
+{
+public:
+ pass_ipa_icp (gcc::context *ctxt)
+ : ipa_opt_pass_d (pass_data_ipa_icp, ctxt,
+ NULL, /* generate_summary */
+ NULL, /* write_summary */
+ NULL, /* read_summary */
+ NULL, /* write_optimization_summary */
+ NULL, /* read_optimization_summary */
+ NULL, /* stmt_fixup */
+ 0, /* function_transform_todo_flags_start */
+ NULL, /* function_transform */
+ NULL) /* variable_transform */
+ {}
+
+ /* opt_pass methods: */
+ virtual bool gate (function *)
+ {
+ return (optimize && flag_icp && !seen_error ()
+ && (in_lto_p || flag_whole_program));
+ }
+
+ virtual unsigned int execute (function *) { return ipa_icp (); }
+
+}; // class pass_ipa_icp
+
+} // anon namespace
+
+ipa_opt_pass_d *
+make_pass_ipa_icp (gcc::context *ctxt)
+{
+ return new pass_ipa_icp (ctxt);
+}
#include "gt-ipa-devirt.h"
diff --git a/gcc/passes.def b/gcc/passes.def
index 9692066e4..d6db9be6e 100644
--- a/gcc/passes.def
+++ b/gcc/passes.def
@@ -156,6 +156,7 @@ along with GCC; see the file COPYING3. If not see
NEXT_PASS (pass_ipa_profile);
NEXT_PASS (pass_ipa_icf);
NEXT_PASS (pass_ipa_devirt);
+ NEXT_PASS (pass_ipa_icp);
NEXT_PASS (pass_ipa_cp);
NEXT_PASS (pass_ipa_sra);
NEXT_PASS (pass_ipa_cdtor_merge);
diff --git a/gcc/testsuite/gcc.dg/icp1.c b/gcc/testsuite/gcc.dg/icp1.c
new file mode 100644
index 000000000..c2117f738
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/icp1.c
@@ -0,0 +1,40 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto -ficp -fdump-ipa-icp=./icp1.c.077i.icp" } */
+
+int dummy = 0;
+
+typedef int (*ftype1)(int a);
+typedef float (*ftype2)(int a);
+
+ftype1 func1;
+
+struct {
+ int a;
+ int* b;
+ ftype1 myf1;
+ ftype2 myf2;
+} my_str;
+
+int foo(int a) {
+ my_str.myf1 = func1;
+ if (a % 2 == 0)
+ dummy += dummy % (dummy - a);
+ return a + 1;
+}
+
+float bar(int a) {
+ my_str.myf2 = &bar;
+ func1 = &foo;
+ return foo(a);
+}
+
+int main() {
+ bar(1);
+ my_str.myf2(3);
+ return (my_str.myf1(2) + func1(4)) != 8;
+}
+
+/* { dg-final { scan-ipa-dump "The call is substituted by:.*= foo \\(4\\);" "icp" } } */
+/* { dg-final { scan-ipa-dump "The call is substituted by:.*= foo \\(2\\);" "icp" } } */
+/* { dg-final { scan-ipa-dump "The call is substituted by: bar \\(3\\);" "icp" } } */
+/* { dg-final { scan-ipa-dump "STATS: 3 candidates for indirect call promotion, 3 substituted, 0 speculatively promoted, 0 cold" "icp" } } */
diff --git a/gcc/testsuite/gcc.dg/icp2.c b/gcc/testsuite/gcc.dg/icp2.c
new file mode 100644
index 000000000..03d31d407
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/icp2.c
@@ -0,0 +1,38 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto -ficp -fdump-ipa-icp=./icp2.c.077i.icp" } */
+
+int dummy = 0;
+
+typedef int (*ftype1)(int a);
+typedef float (*ftype2)(int a);
+
+ftype1 func1;
+
+struct {
+ int a;
+ int* b;
+ ftype1 myf1;
+ ftype2 myf2;
+} my_str;
+
+int foo(int a) {
+ my_str.myf1 = func1;
+ if (a % 2 == 0)
+ dummy += dummy % (dummy - a);
+ return a + 1;
+}
+
+float bar(int a) {
+ my_str.myf2 = dummy ? (ftype2) &foo : &bar;
+ func1 = (ftype1) &bar;
+ return foo(a);
+}
+
+int main() {
+ bar(1);
+ my_str.myf2(3);
+ return (my_str.myf1(2) + func1(4)) != 8;
+}
+
+/* { dg-final { scan-ipa-dump-not "The call is substituted by.*" "icp" } } */
+/* { dg-final { scan-ipa-dump "STATS: 3 candidates for indirect call promotion, 0 substituted, 0 speculatively promoted, 0 cold" "icp" } } */
diff --git a/gcc/testsuite/gcc.dg/icp3.c b/gcc/testsuite/gcc.dg/icp3.c
new file mode 100644
index 000000000..2a7d1e6f5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/icp3.c
@@ -0,0 +1,52 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto -ficp -fdump-ipa-icp=./icp3.c.077i.icp" } */
+
+#include <stdio.h>
+
+int dummy = 0;
+
+typedef int (*ftype1)(int a);
+typedef float (*ftype2)(int a);
+typedef ftype1 (*ftype3) (ftype2);
+
+ftype1 func1;
+
+struct {
+ int a;
+ int* b;
+ ftype1 myf1;
+ ftype2 myf2;
+ ftype3 myf3;
+} my_str;
+
+ftype1 boo(ftype2 a) {
+ printf ("Call boo\n");
+ return (ftype1) a;
+}
+
+int foo(int a) {
+ printf ("Call foo\n");
+ my_str.myf1 = func1;
+ if (a % 2 == 0)
+ dummy += dummy % (dummy - a);
+ return a + 1;
+}
+
+float bar(int a) {
+ printf("Call bar\n");
+ my_str.myf2 = (ftype2) my_str.myf3((ftype2) foo);
+ func1 = &foo;
+ return foo(a);
+}
+
+int main() {
+ my_str.myf3 = &boo;
+ bar(1);
+ my_str.myf2(3);
+ return (my_str.myf1(2) + func1(4)) != 8;
+}
+
+/* { dg-final { scan-ipa-dump "The call is substituted by:.*= foo \\(4\\);" "icp" } } */
+/* { dg-final { scan-ipa-dump "The call is substituted by:.*= foo \\(2\\);" "icp" } } */
+/* { dg-final { scan-ipa-dump "The call is substituted by: foo \\(3\\);" "icp" } } */
+/* { dg-final { scan-ipa-dump "STATS: 4 candidates for indirect call promotion, 3 substituted, 0 speculatively promoted, 0 cold" "icp" } } */
diff --git a/gcc/testsuite/gcc.dg/icp4.c b/gcc/testsuite/gcc.dg/icp4.c
new file mode 100644
index 000000000..e3e1d5116
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/icp4.c
@@ -0,0 +1,55 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto -ficp -fdump-ipa-icp=./icp4.c.077i.icp" } */
+
+#include <stdio.h>
+
+int dummy = 0;
+
+typedef int (*ftype1)(int a);
+typedef float (*ftype2)(int a);
+typedef ftype1 (*ftype3) (ftype2);
+
+ftype1 func1;
+ftype1 boo(ftype2 a);
+int foo(int a);
+float bar(int a);
+
+typedef struct {
+ int a;
+ int* b;
+ ftype1 myf1;
+ ftype2 myf2;
+ ftype3 myf3;
+} T;
+
+T my_str = {0, (int*) &dummy, (ftype1) &boo, (ftype2) &foo, (ftype3) &bar};
+
+ftype1 boo(ftype2 a) {
+ printf ("Call boo\n");
+ return (ftype1) a;
+}
+
+int foo(int a) {
+ printf ("Call foo\n");
+ my_str.myf1 = func1;
+ if (a % 2 == 0)
+ dummy += dummy % (dummy - a);
+ return a + 1;
+}
+
+float bar(int a) {
+ printf("Call bar\n");
+ my_str.myf2 = (ftype2) my_str.myf3((ftype2) foo);
+ func1 = &foo;
+ return foo(a);
+}
+
+int main() {
+ my_str.myf3 = &boo;
+ bar(1);
+ my_str.myf2(3);
+ return (my_str.myf1(2) + func1(4)) != 8;
+}
+
+/* { dg-final { scan-ipa-dump-not "The call is substituted by.*" "icp" } } */
+/* { dg-final { scan-ipa-dump "STATS: 4 candidates for indirect call promotion, 0 substituted, 0 speculatively promoted, 0 cold" "icp" } } */
diff --git a/gcc/testsuite/gcc.dg/icp5.c b/gcc/testsuite/gcc.dg/icp5.c
new file mode 100644
index 000000000..c7709243c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/icp5.c
@@ -0,0 +1,66 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto -ficp -fdump-ipa-icp=./icp5.c.077i.icp" } */
+
+#include <stdio.h>
+
+int dummy = 0;
+
+typedef int (*ftype1)(int a);
+typedef float (*ftype2)(int a);
+typedef ftype1 (*ftype3) (ftype2);
+
+ftype1 func1;
+ftype1 boo(ftype2 a);
+int foo(int a);
+float bar(int a);
+
+typedef struct {
+ int a;
+ int* b;
+ ftype1 myf1;
+ ftype2 myf2;
+ ftype3 myf3;
+} T;
+
+T my_str;
+
+typedef struct {
+ int a;
+ int* b;
+ ftype3 myf1;
+ ftype2 myf2;
+ ftype1 myf3;
+} T1;
+
+T1 my1 = {0, &dummy, boo, &bar, &foo};
+
+ftype1 boo(ftype2 a) {
+ printf("Call boo\n");
+ return (ftype1) a;
+}
+
+int foo(int a) {
+ printf("Call foo\n");
+ my_str.myf1 = func1;
+ if (a % 2 == 0)
+ dummy += dummy % (dummy - a);
+ return a + 1;
+}
+
+float bar(int a) {
+ printf("Call bar\n");
+ my_str.myf2 = (ftype2) my_str.myf3((ftype2) foo);
+ func1 = &foo;
+ return foo(a);
+}
+
+int main() {
+ my_str = *(T*)&my1;
+ my_str.myf3 = &boo;
+ bar(1);
+ my_str.myf2(3);
+ return (my_str.myf1(2) + func1(4)) != 8;
+}
+
+/* { dg-final { scan-ipa-dump-not "The call is substituted by.*" "icp" } } */
+/* { dg-final { scan-ipa-dump "STATS: 4 candidates for indirect call promotion, 0 substituted, 0 speculatively promoted, 0 cold" "icp" } } */
diff --git a/gcc/testsuite/gcc.dg/icp6.c b/gcc/testsuite/gcc.dg/icp6.c
new file mode 100644
index 000000000..5a9f15045
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/icp6.c
@@ -0,0 +1,66 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto -ficp -fdump-ipa-icp=./icp6.c.077i.icp -Wno-int-conversion -Wno-incompatible-pointer-types" } */
+int dummy = 0;
+
+typedef int (*ftype1)(int a);
+typedef float (*ftype2)(int a);
+typedef int (*ftype3)();
+typedef int (*ftype4)(int a, int b);
+
+ftype1 func1;
+ftype4 func2;
+
+struct {
+ int a;
+ int* b;
+ ftype1 myf1;
+ ftype2 myf2;
+ ftype3 myf3;
+} my_str;
+
+int foo3(float a) {
+ return dummy;
+}
+
+int foo4(int a, int b) {
+ return a*b;
+}
+
+int foo(int a) {
+ my_str.myf1 = func1;
+ if (a % 2 == 0)
+ dummy += dummy % (dummy - a);
+ return a + 1;
+}
+
+int foo2(float a) {
+ func1 = (ftype1) &foo;
+ func2 = &foo4;
+ return dummy + foo3 (a);
+}
+
+float bar2(int a) {
+ my_str.myf2 = (ftype2)(0x864213);
+ func2 = 0x65378;
+ return foo(a);
+}
+
+float bar(int a) {
+ my_str.myf3 = &foo2;
+ my_str.myf2 = &bar;
+ func1 = (ftype1) &dummy;
+ func2 = (ftype4) &bar2;
+ return foo(a);
+}
+
+int main() {
+ bar(1);
+ bar2(1);
+ bar(0);
+ my_str.myf2(3);
+ ((ftype1) my_str.myf3)(0.0);
+ int sum = func1(4);
+ return (sum + my_str.myf1(2) + func2(5, 6)) != 38;
+}
+/* { dg-final { scan-ipa-dump "The call is substituted by.*foo2 \\(0\\);" "icp" } } */
+/* { dg-final { scan-ipa-dump "STATS: 5 candidates for indirect call promotion, 1 substituted, 0 speculatively promoted, 0 cold" "icp" } } */
diff --git a/gcc/testsuite/gcc.dg/icp7.c b/gcc/testsuite/gcc.dg/icp7.c
new file mode 100644
index 000000000..fa52197f4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/icp7.c
@@ -0,0 +1,48 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -flto -ficp -fdump-ipa-icp=./icp7.c.077i.icp" } */
+
+#include <stdarg.h>
+
+int dummy = 0;
+
+typedef int (*ftype1)(int a);
+typedef float (*ftype2)(int a);
+
+ftype1 func1;
+
+struct {
+ int a;
+ int* b;
+ ftype1 myf1;
+ ftype2 myf2;
+} my_str;
+
+int boo(int a, ...) {
+ va_list ap;
+ va_start(ap, a);
+ if (a == 0)
+ dummy += va_arg(ap, int);
+ va_end(ap);
+ return dummy;
+}
+
+int foo(int a) {
+ my_str.myf1 = func1;
+ if (a % 2 == 0)
+ dummy += dummy % (dummy - a);
+ return a + 1;
+}
+
+float bar(int a) {
+ my_str.myf2 = &bar;
+ func1 = (ftype1) &boo;
+ return foo(a);
+}
+
+int main() {
+ bar(1);
+ my_str.myf2(3);
+ return (my_str.myf1(2) + func1(4));
+}
+
+/* { dg-final { scan-ipa-dump "Address taken function with varargs is found. Skip the optimization." "icp" } } */
diff --git a/gcc/timevar.def b/gcc/timevar.def
index 98a5a490f..ca4156066 100644
--- a/gcc/timevar.def
+++ b/gcc/timevar.def
@@ -71,6 +71,7 @@ DEFTIMEVAR (TV_CGRAPHOPT , "callgraph optimization")
DEFTIMEVAR (TV_CGRAPH_FUNC_EXPANSION , "callgraph functions expansion")
DEFTIMEVAR (TV_CGRAPH_IPA_PASSES , "callgraph ipa passes")
DEFTIMEVAR (TV_IPA_ODR , "ipa ODR types")
+DEFTIMEVAR (TV_IPA_ICP , "ipa indirect call promotion")
DEFTIMEVAR (TV_IPA_FNSUMMARY , "ipa function summary")
DEFTIMEVAR (TV_IPA_UNREACHABLE , "ipa dead code removal")
DEFTIMEVAR (TV_IPA_INHERITANCE , "ipa inheritance graph")
diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h
index 56898e019..5f09e4f8b 100644
--- a/gcc/tree-pass.h
+++ b/gcc/tree-pass.h
@@ -524,6 +524,7 @@ extern ipa_opt_pass_d *make_pass_ipa_cp (gcc::context *ctxt);
extern ipa_opt_pass_d *make_pass_ipa_sra (gcc::context *ctxt);
extern ipa_opt_pass_d *make_pass_ipa_icf (gcc::context *ctxt);
extern ipa_opt_pass_d *make_pass_ipa_devirt (gcc::context *ctxt);
+extern ipa_opt_pass_d *make_pass_ipa_icp (gcc::context *ctxt);
extern ipa_opt_pass_d *make_pass_ipa_odr (gcc::context *ctxt);
extern ipa_opt_pass_d *make_pass_ipa_reference (gcc::context *ctxt);
extern ipa_opt_pass_d *make_pass_ipa_pure_const (gcc::context *ctxt);
--
2.33.0
|