summaryrefslogtreecommitdiff
path: root/gcc/ipa-type-escape-analysis.c
blob: a9deb05cc5f2f597a169040b097d0125992c54d8 (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
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
/* IPA Type Escape Analysis and Dead Field Elimination
   Copyright (C) 2019-2020 Free Software Foundation, Inc.

  Contributed by Erick Ochoa <erick.ochoa@theobroma-systems.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/>.  */

/* Interprocedural dead field analysis (IPA-DFA)

   The goal of this analysis is to

   1) discover RECORD_TYPEs which do not escape the current linking unit.

   2) discover fields in RECORD_TYPEs that are never read.

   3) merge the results from 1 and 2 to determine which fields are not needed.

   The algorithm basically consists of the following stages:

   1) Partition all TYPE_P trees into two sets: those trees which reach a
   tree of RECORD_TYPE.

   2.a) Analyze callsites to determine if arguments and return types are
   escaping.
   2.b) Analyze casts to determine if it would be safe to mark a field as dead.
   2.c) Analyze for constructors and static initialization and mark this as
   TYPE_P trees as unable to be modified
   2.d) Analyze if FIELD_DECL are accessed via pointer arithmetic and mark
   FIELD_DECLs before as unable to be modified.
   2.e) Analyze if an address of a FIELD_DECL is taken and mark the whole
   RECORD_TYPE as unable to be modified.
   2.f) Propagate this information to nested TYPE_P trees.
   2.g) Propagate this information across different TYPE_P trees that represent
   equivalent TYPE_P types.

   3.a) Analyze FIELD_DECL to determine whether they are read,
   written or neither.
   3.b) Unify this information across different RECORD_TYPE trees that
   represent equivalent types
   3.c) Determine which FIELD_DECL can be deleted.

   4) Calculate the intersection of non-escaping RECORD_TYPEs with RECORD_TYPEs
   that have a field that can be deleted.

   First stage - Determining if a TYPE_P points to a RECORD_TYPE
   =============================================================

   This stage is computed through the *Collector classes.  Those are
   TypeCollector, ExprCollector and GimpleTypeCollector which walk up and down
   types, expressions, and gimple respectively and propagate information about
   TYPE_P trees and mantain information on the type partitions.

   Second stage - Determining if a TYPE_P escapes
   ==============================================

   This stage is computed through the *Escaper classes.  Those are
   TypeEscaper, ExprEscaper, GimpleEscaper, GimpleCaster classes.  These
   classes walk up and down types, expressions and gimple respectively and
   propagate reasons why a TYPE_P tree might be escaping.
   Reasons are always ORed and equivalent TYPE_P trees might hold different
   results up to when equivalence is computed for all TYPE_P trees and reasons
   are propagated until a fixedpoint is achieved.

   Third stage - Calculating FIELD_DECL accesses
   =============================================

   This stage is computed through the *Accessor classes.  Those are
   TypeAccessor, ExprAccessor, and GimpleAccessor.  These classes walk up and
   down TYPE_P, expressions, and gimple respectively and propagate access
   information.  If an expression occurs in the LHS, it is marked as written
   and if it occurs on the RHS, it is marked as read.  Special cases where
   addresses of a FIELD_DECLs are taken mark all FIELD_DECL in a RECORD_TYPE
   as read.

   Fourth stage - Intersection of accesses and non_escaping
   ========================================================

   This stage happens in the function obtain_unescaped_and_unaccessed_fields.
   First all FIELD_DECLs are translated to their respective field offset.
   Then all field offsets for FIELD_DECLs which are READ are stored
   in a set.  We then compute the complement of this set and these are the
   offsets of FIELD_DECLs which are never read.

   Offsets are needed if we are to find dead fields for anonymous fields.
*/



#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple-expr.h"
#include "predict.h"
#include "alloc-pool.h"
#include "tree-pass.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "fold-const.h"
#include "gimple-fold.h"
#include "symbol-summary.h"
#include "tree-vrp.h"
#include "ipa-prop.h"
#include "tree-pretty-print.h"
#include "tree-inline.h"
#include "ipa-fnsummary.h"
#include "ipa-utils.h"
#include "tree-ssa-ccp.h"
#include "stringpool.h"
#include "attribs.h"
#include "basic-block.h" //needed for gimple.h
#include "function.h"    //needed for gimple.h
#include "gimple.h"
#include "stor-layout.h"
#include "cfg.h" // needed for gimple-iterator.h
#include "gimple-iterator.h"
#include "gimplify.h"      //unshare_expr
#include "value-range.h"   // make_ssa_name dependency
#include "tree-ssanames.h" // make_ssa_name
#include "ssa.h"
#include "tree-into-ssa.h"
#include "gimple-ssa.h" // update_stmt
#include "tree.h"
#include "gimple-expr.h"
#include "predict.h"
#include "alloc-pool.h"
#include "tree-pass.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "fold-const.h"
#include "gimple-fold.h"
#include "symbol-summary.h"
#include "tree-vrp.h"
#include "ipa-prop.h"
#include "tree-pretty-print.h"
#include "tree-inline.h"
#include "ipa-fnsummary.h"
#include "ipa-utils.h"
#include "tree-ssa-ccp.h"
#include "stringpool.h"
#include "attribs.h"
#include "tree-ssa-alias.h"
#include "tree-ssanames.h"
#include "gimple.h"
#include "cfg.h"
#include "gimple-iterator.h"
#include "gimple-ssa.h"
#include "gimple-pretty-print.h"
#include <vector>
#include <set>
#include <map>
#include <stack>

#include "ipa-type-escape-analysis.h"
#include "ipa-dfe.h"

// Main function that drives dfe.
static unsigned int
lto_dfe_execute ();

// Partition types into reching record or non reaching record sets.
static tpartitions_t
partition_types_into_record_reaching_or_non_record_reaching ();

// Partition types into escaping or non escaping sets.
static tpartitions_t
partition_types_into_escaping_nonescaping ();

// Perform dead field elimination.
static void
lto_dead_field_elimination ();

// Fixed point calculating to determine escaping types.
static void
fix_escaping_types_in_set (tpartitions_t &types);

// Find which fields are accessed.
static record_field_map_t
find_fields_accessed ();

// Obtain intersection of unaccessed and non escaping types.
static record_field_offset_map_t
obtain_nonescaping_unaccessed_fields (tpartitions_t casting,
				      record_field_map_t record_field_map);

// TODO:
// This was copy pasted from tree-ssa-structalias.c
// Maybe delete this and make the function visible?
static HOST_WIDE_INT
bitpos_of_field (const tree fdecl)
{
  if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
      || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
    return -1;

  return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
	  + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
}


namespace {
const pass_data pass_data_ipa_type_escape_analysis = {
  SIMPLE_IPA_PASS,
  "type-escape-analysis",
  OPTGROUP_NONE,
  TV_NONE,
  (PROP_cfg | PROP_ssa),
  0,
  0,
  0,
  0,
};

class pass_ipa_type_escape_analysis : public simple_ipa_opt_pass
{
public:
  pass_ipa_type_escape_analysis (gcc::context *ctx)
    : simple_ipa_opt_pass (pass_data_ipa_type_escape_analysis, ctx)
  {}

  virtual bool gate (function *)
  {
    return in_lto_p && flag_ipa_type_escape_analysis;
  }
  virtual unsigned execute (function *)
  {
    return lto_dfe_execute ();
  }
};
} // namespace

/* Top level function.  */
static unsigned int
lto_dfe_execute ()
{
  lto_dead_field_elimination ();
  log ("finished!\n");
  return 0;
}

/*
 * Perform dead field elimination at link-time.
 * This transformation is composed of two main stages:
 *   * Finding out which fields are non escaping and unaccessed.
 *   * Creating new types and substituting their mention throughout the
 *   program.
 */
static void
lto_dead_field_elimination ()
{
  // Analysis.
  tpartitions_t escaping_nonescaping_sets
    = partition_types_into_escaping_nonescaping ();
  record_field_map_t record_field_map = find_fields_accessed ();
  record_field_offset_map_t record_field_offset_map
    = obtain_nonescaping_unaccessed_fields (escaping_nonescaping_sets,
					    record_field_map);
  if (record_field_offset_map.empty ())
    return;

    // Prepare for transformation.
  std::set<const_tree> to_modify
    = get_all_types_pointing_to (record_field_offset_map,
				 escaping_nonescaping_sets);
  reorg_maps_t replacements
    = get_types_replacement (record_field_offset_map, to_modify);
  reorg_record_map_t map = replacements.first;
  reorg_field_map_t field_map = replacements.second;
  // Transformation.
  substitute_types_in_program (map, field_map);

}

/* Iterate all gimple bodies and collect trees
 * which are themselves RECORD_TYPE or which
 * somehow reach a RECORD_TYPE tree (e.g., via a
 * pointer, array, reference, union, field, etc...).
 * Let's call these trees record_reaching_trees.
 */
static tpartitions_t
partition_types_into_record_reaching_or_non_record_reaching ()
{
  GimpleTypeCollector collector;
  collector.walk ();
  tpartitions_t partitions = collector.get_record_reaching_trees ();
  return partitions;
}

/* Iterate over all gimple bodies and find out
 * which types are escaping AND are being casted.
 */
static tpartitions_t
partition_types_into_escaping_nonescaping ()
{
  tpartitions_t partitions
    = partition_types_into_record_reaching_or_non_record_reaching ();
  GimpleCaster caster (partitions);
  caster.walk ();

  // Dump for debugging.
  if (flag_print_cast_analysis)
    caster.print_reasons ();

  partitions = caster.get_sets ();
  // Unify results from different trees representing the same type
  // until a fixed point is reached.
  fix_escaping_types_in_set (partitions);
  return partitions;
}

/* Iterate over all gimple bodies and find out
 * which fields are accessed for all RECORD_TYPE
 * types.
 */
static record_field_map_t
find_fields_accessed ()
{
  GimpleAccesser accesser;
  accesser.walk ();

  // Dump for debugging.
  if (flag_print_access_analysis)
    accesser.print_accesses ();

  // This record_field_map holds
  // RECORD_TYPE -> (FIELD_DECL -> how field is accessed)
  record_field_map_t record_field_map = accesser.get_map ();
  return record_field_map;
}

/* Find equivalent RECORD_TYPE trees to const_tree r_i.
 * This equivalence will be used for merging the results of field accesses
 * across all equivalent RECORD_TYPE trees.

 * r_i should exists in the points_to_record set
 * and it is a tree for which this method is going to find the rest of
 * equivalent trees found in record_field_map.
 */
static std::vector<const_tree>
find_equivalent_trees (const_tree r_i, record_field_map_t record_field_map,
		       tpartitions_t casting)
{
  TypeIncompleteEquality equality;
  std::vector<const_tree> equivalence;
  bool is_rin_record = casting.in_points_to_record (r_i);
  if (!is_rin_record)
    return equivalence;

  for (std::map<const_tree, field_access_map_t>::const_iterator j
       = record_field_map.begin (),
       f = record_field_map.end ();
       j != f; j++)
    {
      const_tree r_j = j->first;
      const bool pointer_equal = r_i == r_j;
      if (pointer_equal)
	continue;

      bool is_p_record = casting.in_points_to_record (r_i)
			 && casting.in_points_to_record (r_j);
      if (!is_p_record)
	continue;

      const bool are_equal = equality.equal (r_i, r_j);
      if (!are_equal)
	continue;

      equivalence.push_back (r_j);
    }
  return equivalence;
}

/*
 * FIELD is a FIELD_DECL tree, ACCESSED is a a bitflag that marks whether the
 * field is read, written or neither.  FIELD_OFFSET will hold the following map:
 * tree (RECORD_TYPE) -> unsigned (bitpos_of_field for read fields).
 */
static void
add_offset_only_if_read (const_tree field, unsigned access,
			 field_offsets_t &field_offset)
{
  assert_is_type (field, FIELD_DECL);
  const bool is_read = access & Read;
  if (!is_read)
    return;

  tree _field = const_tree_to_tree (field);
  unsigned f_offset = bitpos_of_field (_field);
  field_offset.insert (f_offset);
}

/*
 * FIELD_MAP holds the following map:
 * tree (FIELD_DECL) -> access type
 * FIELD_OFFSET is being built here.
 * It should hold
 * tree (RECORD_TYPE) -> bitpos_of_field for read fields).
 */
static void
keep_only_read_fields_from_field_map (field_access_map_t &field_map,
				      field_offsets_t &field_offset)
{
  for (std::map<const_tree, unsigned>::iterator j = field_map.begin (),
						f = field_map.end ();
       j != f; ++j)
    {
      add_offset_only_if_read (j->first, j->second, field_offset);
    }
}

/*
 * EQUIVALENT holds equivalent trees of RECORD_TYPE
 * Update FIELD_OFFSET as the union of all READ FIELDS for the equivalent trees.
 */
static void
keep_only_read_fields_from_equivalent_field_maps (
  std::vector<const_tree> equivalent, record_field_map_t &record_field_map,
  field_offsets_t &field_offset)
{
  for (std::vector<const_tree>::iterator j = equivalent.begin (),
					 f = equivalent.end ();
       j != f; j++)
    {
      const_tree r_j = *j;
      field_access_map_t equivalent_field_map = record_field_map[r_j];
      keep_only_read_fields_from_field_map (equivalent_field_map, field_offset);
    }
}

/*
 * Whether RECORDS are escaping or can't be modified,
 * delete them from the set of candidate RECORDS to be modified.
 */
static void
erase_if_no_fields_can_be_deleted (
  record_field_offset_map_t &record_field_offset_map,
  std::set<const_tree> &to_keep, std::set<const_tree> &to_erase)
{
  for (std::map<const_tree, field_offsets_t>::iterator i
       = record_field_offset_map.begin (),
       e = record_field_offset_map.end ();
       i != e; ++i)
    {
      const_tree record = i->first;
      const bool keep = to_keep.find (record) != to_keep.end ();
      if (keep)
	continue;

      to_erase.insert (record);
    }

  for (std::set<const_tree>::iterator i = to_erase.begin (),
				      e = to_erase.end ();
       i != e; ++i)
    {
      const_tree record = *i;
      record_field_offset_map.erase (record);
    }
}

/*
 * Mark escaping RECORD_TYPEs as ready to be deleted from the
 * set of candidates to be modified.
 */
static void
mark_escaping_types_to_be_deleted (
  record_field_offset_map_t &record_field_offset_map,
  std::set<const_tree> &to_erase, tpartitions_t casting)
{
  const tset_t &non_escaping = casting.non_escaping;
  for (std::map<const_tree, field_offsets_t>::iterator i
       = record_field_offset_map.begin (),
       e = record_field_offset_map.end ();
       i != e; ++i)
    {
      const_tree record = i->first;
      const bool in_set = non_escaping.find (record) != non_escaping.end ();
      if (in_set)
	continue;

      to_erase.insert (record);
    }
}

// Obtain nonescaping unaccessed fields
static record_field_offset_map_t
obtain_nonescaping_unaccessed_fields (tpartitions_t casting,
				      record_field_map_t record_field_map)
{
  bool has_fields_that_can_be_deleted = false;
  record_field_offset_map_t record_field_offset_map;
  for (std::map<const_tree, field_access_map_t>::iterator i
       = record_field_map.begin (),
       e = record_field_map.end ();
       i != e; ++i)
    {
      const_tree r_i = i->first;
      std::vector<const_tree> equivalence
	= find_equivalent_trees (r_i, record_field_map, casting);
      field_offsets_t field_offset;
      field_access_map_t original_field_map = record_field_map[r_i];
      keep_only_read_fields_from_field_map (original_field_map, field_offset);
      keep_only_read_fields_from_equivalent_field_maps (equivalence,
							record_field_map,
							field_offset);
      // These map holds the following:
      // RECORD_TYPE -> unsigned (bit_pos_offset which has been read)
      record_field_offset_map[r_i] = field_offset;
    }

  // So now that we only have the FIELDS which are read,
  // we need to compute the complement...

  // Improve: This is tightly coupled, I need to decouple it...
  std::set<const_tree> to_erase;
  std::set<const_tree> to_keep;
  mark_escaping_types_to_be_deleted (record_field_offset_map, to_erase,
				     casting);
  for (std::map<const_tree, field_offsets_t>::iterator i
       = record_field_offset_map.begin (),
       e = record_field_offset_map.end ();
       i != e; ++i)
    {
      const_tree record = i->first;
      const bool will_be_erased = to_erase.find (record) != to_erase.end ();
      // No need to compute which fields can be deleted if type is escaping
      if (will_be_erased)
	continue;

      field_offsets_t field_offset = i->second;
      for (tree field = TYPE_FIELDS (record); field; field = DECL_CHAIN (field))
	{
	  unsigned f_offset = bitpos_of_field (field);
	  bool in_set2 = field_offset.find (f_offset) != field_offset.end ();
	  if (in_set2)
	    {
	      field_offset.erase (f_offset);
	      continue;
	    }
	  to_keep.insert (record);
	  field_offset.insert (f_offset);
	  has_fields_that_can_be_deleted = true;
	  // NOTE: With anonymous fields this might be weird to print.
	  log ("%s.%s may be deleted\n",
	       TypeStringifier::get_type_identifier (record).c_str (),
	       TypeStringifier::get_field_identifier (field).c_str ());

	  if (OPT_Wdfa == 0) continue;
	  // Anonymous fields? (Which the record can be!).
	    warning (OPT_Wdfa, "RECORD_TYPE %qE has dead field %qE in LTO.\n",
		record, field);
	}
      record_field_offset_map[record] = field_offset;
    }

  // Improve: Make this more elegant.
  if (!has_fields_that_can_be_deleted)
    {
      record_field_offset_map_t empty;
      return empty;
    }

  erase_if_no_fields_can_be_deleted (record_field_offset_map, to_keep,
				     to_erase);

  return record_field_offset_map;
}

// Main interface to TypeWalker
// Start recursive walk
void
TypeWalker::walk (const_tree t)
{
  gcc_assert (t);
  this->tset.clear ();
  this->_walk (t);
}

void
TypeWalker::_walk (const_tree type)
{
  // Improve, verify that having a type is an invariant.
  // I think there was a specific example which didn't
  // allow for it
  if (!type)
    return;

  gcc_assert (type);

  // This is an optimization.
  const bool _is_memoized = is_memoized (type);
  if (_is_memoized)
    return;

  // This is for correctness
  // Some types are represented as a graph
  // of trees and therefore we need a way to
  // avoid loops in this graph.
  // Imrpove: Outline finding if it is recursive?
  const bool is_recursing = tset.find (type) != tset.end ();
  if (is_recursing)
    return;

  tset.insert (type);
  const enum tree_code code = TREE_CODE (type);
  switch (code)
    {
    case VOID_TYPE:
      this->walk_VOID_TYPE (type);
      break;
    case INTEGER_TYPE:
      this->walk_INTEGER_TYPE (type);
      break;
    case REAL_TYPE:
      this->walk_REAL_TYPE (type);
      break;
    case FIXED_POINT_TYPE:
      this->walk_FIXED_POINT_TYPE (type);
      break;
    case COMPLEX_TYPE:
      this->walk_COMPLEX_TYPE (type);
      break;
    case ENUMERAL_TYPE:
      this->walk_ENUMERAL_TYPE (type);
      break;
    case BOOLEAN_TYPE:
      this->walk_BOOLEAN_TYPE (type);
      break;
    case OFFSET_TYPE:
      this->walk_OFFSET_TYPE (type);
      break;
    case RECORD_TYPE:
      this->walk_RECORD_TYPE (type);
      break;
    case POINTER_TYPE:
      this->walk_POINTER_TYPE (type);
      break;
    case REFERENCE_TYPE:
      this->walk_REFERENCE_TYPE (type);
      break;
    case ARRAY_TYPE:
      this->walk_ARRAY_TYPE (type);
      break;
    case UNION_TYPE:
      this->walk_UNION_TYPE (type);
      break;
    case FUNCTION_TYPE:
      this->walk_FUNCTION_TYPE (type);
      break;
    case METHOD_TYPE:
      this->walk_METHOD_TYPE (type);
      break;
    // Since we are dealing only with C at the moment,
    // we don't care about QUAL_UNION_TYPE nor LANG_TYPEs
    // So fail early.
    case QUAL_UNION_TYPE:
    case LANG_TYPE:
    default:
      {
	log ("missing %s\n", get_tree_code_name (code));
	gcc_unreachable ();
      }
      break;
    }
  tset.erase (type);
}

// This is used to walk over subtrees.
// But before walking subtrees, we need to
// call the pre-order callback
// and after we need to
// call the post-order callback.
#define TypeWalkerFuncDef(code)						\
  void TypeWalker::walk_##code (const_tree t)				\
  {									\
    assert_is_type (t, code);						\
    _walk_##code##_pre (t);						\
    _walk_##code (t);							\
    _walk_##code##_post (t);						\
  }

#define TypeWalkerFuncDefInternal(code)					\
  void TypeWalker::_walk_##code (__attribute__ ((unused)) const_tree t) \
  {}

TypeWalkerFuncDef (VOID_TYPE)
TypeWalkerFuncDefInternal (VOID_TYPE)
TypeWalkerFuncDef (INTEGER_TYPE)
TypeWalkerFuncDefInternal (INTEGER_TYPE)
TypeWalkerFuncDef (REAL_TYPE)
TypeWalkerFuncDefInternal (REAL_TYPE)
TypeWalkerFuncDef (BOOLEAN_TYPE)
TypeWalkerFuncDefInternal (BOOLEAN_TYPE)
TypeWalkerFuncDef (OFFSET_TYPE)
TypeWalkerFuncDefInternal (OFFSET_TYPE)
TypeWalkerFuncDef (FIXED_POINT_TYPE)
TypeWalkerFuncDefInternal (FIXED_POINT_TYPE)
TypeWalkerFuncDef (COMPLEX_TYPE)
TypeWalkerFuncDefInternal (COMPLEX_TYPE)
TypeWalkerFuncDef (ENUMERAL_TYPE)
TypeWalkerFuncDefInternal (ENUMERAL_TYPE)

/* walk wrapper is used for unwrapping
 * REFERENCE_TYPE, POINTER_TYPE, ARRAY_TYPE.
 */
void TypeWalker::_walk_wrapper (const_tree t)
{
  const_tree inner_type = TREE_TYPE (t);
  // I think I encountered this code:
  // FIXME: Do we really need this?
  if (!inner_type)
    return;

  gcc_assert (inner_type);
  _walk (inner_type);
}

#define TypeWalkerFuncDefWrapper(code)		\
  void TypeWalker::_walk_##code (const_tree t)  \
  { _walk_wrapper (t); }

TypeWalkerFuncDef (POINTER_TYPE)
TypeWalkerFuncDefWrapper (POINTER_TYPE)
TypeWalkerFuncDefWrapper (REFERENCE_TYPE)
TypeWalkerFuncDef (REFERENCE_TYPE)
TypeWalkerFuncDef (ARRAY_TYPE)
TypeWalkerFuncDefWrapper (ARRAY_TYPE)
TypeWalkerFuncDef (RECORD_TYPE)

void
TypeWalker::_walk_RECORD_TYPE (const_tree t)
{
  _walk_record_or_union (t);
}

TypeWalkerFuncDef (UNION_TYPE)

void
TypeWalker::_walk_UNION_TYPE (const_tree t)
{
  _walk_record_or_union (t);
}

void
TypeWalker::_walk_record_or_union (const_tree t)
{
  for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
    {
      gcc_assert (field);
      walk_field (field);
    }
}

void
TypeWalker::walk_field (const_tree t)
{
  _walk_field_pre (t);
  _walk_field (t);
  _walk_field_post (t);
}

void
TypeWalker::_walk_field (const_tree t)
{
  const_tree inner_type = TREE_TYPE (t);
  gcc_assert (inner_type);
  _walk (inner_type);
}

TypeWalkerFuncDef (FUNCTION_TYPE)

  void TypeWalker::_walk_FUNCTION_TYPE (const_tree t)
{
  _walk_function_or_method (t);
}

TypeWalkerFuncDef (METHOD_TYPE)

  void TypeWalker::_walk_METHOD_TYPE (const_tree t)
{
  _walk_function_or_method (t);
}

void
TypeWalker::_walk_function_or_method (const_tree t)
{
  const_tree ret_type = TREE_TYPE (t);
  walk_return (ret_type);
  walk_args (t);
}

void
TypeWalker::walk_return (const_tree t)
{
  _walk_return_pre (t);
  _walk_return (t);
  _walk_return_post (t);
}

void
TypeWalker::_walk_return (const_tree t)
{
  _walk (t);
}

void
TypeWalker::walk_args (const_tree t)
{
  _walk_args_pre (t);
  _walk_args (t);
  _walk_args_post (t);
}

void
TypeWalker::_walk_args (const_tree t)
{
  for (tree arg_node = TYPE_ARG_TYPES (t); NULL_TREE != arg_node;
       arg_node = TREE_CHAIN (arg_node))
    {
      const_tree arg_node_type = TREE_VALUE (arg_node);
      gcc_assert (arg_node_type);
      walk_arg (arg_node_type);
    }
}

void
TypeWalker::walk_arg (const_tree t)
{
  _walk_arg_pre (t);
  _walk_arg (t);
  _walk_arg_post (t);
}

void
TypeWalker::_walk_arg (const_tree t)
{
  _walk (t);
}

/* Main interface for the ExprWalker... */
void
ExprWalker::walk (const_tree e)
{
  _walk_pre (e);
  _walk (e);
  _walk_post (e);
}

void
ExprWalker::_walk (const_tree e)
{
  gcc_assert (e);
  const enum tree_code code = TREE_CODE (e);
  switch (code)
    {
    case INTEGER_CST:
      walk_INTEGER_CST (e);
      break;
    case REAL_CST:
      walk_REAL_CST (e);
      break;
    case STRING_CST:
      walk_STRING_CST (e);
      break;
    case BIT_FIELD_REF:
      walk_BIT_FIELD_REF (e);
      break;
    case ARRAY_REF:
      walk_ARRAY_REF (e);
      break;
    case MEM_REF:
      walk_MEM_REF (e);
      break;
    case COMPONENT_REF:
      walk_COMPONENT_REF (e);
      break;
    case SSA_NAME:
      walk_SSA_NAME (e);
      break;
    case ADDR_EXPR:
      walk_ADDR_EXPR (e);
      break;
    case VIEW_CONVERT_EXPR:
      walk_VIEW_CONVERT_EXPR (e);
      break;
    case IMAGPART_EXPR:
      walk_IMAGPART_EXPR (e);
      break;
    case VAR_DECL:
      walk_VAR_DECL (e);
      break;
    case FIELD_DECL:
      walk_FIELD_DECL (e);
      break;
    case RESULT_DECL:
      walk_RESULT_DECL (e);
      break;
    case PARM_DECL:
      walk_PARM_DECL (e);
      break;
    case FUNCTION_DECL:
      walk_FUNCTION_DECL (e);
      break;
    case CONSTRUCTOR:
      walk_CONSTRUCTOR (e);
      break;
    case LE_EXPR:
      walk_LE_EXPR (e);
      break;
    case LT_EXPR:
      walk_LT_EXPR (e);
      break;
    case EQ_EXPR:
      walk_EQ_EXPR (e);
      break;
    case GT_EXPR:
      walk_GT_EXPR (e);
      break;
    case GE_EXPR:
      walk_GE_EXPR (e);
      break;
    case NE_EXPR:
      walk_NE_EXPR (e);
      break;
    default:
      {
	log ("missing %s\n", get_tree_code_name (code));
	gcc_unreachable ();
      }
      break;
    }
}

/* call pre-order callback for everything
 * call pre-order callback for specific code
 * walk subtrees
 * call post-order callback for specific code
 * call post-order callback for everything.
 */
#define ExprWalkerFuncDef(code) 			\
  void ExprWalker::walk_##code (const_tree e)		\
  {							\
    assert_is_type (e, code);				\
    _walk_pre (e);					\
    _walk_##code##_pre (e);				\
    _walk_##code (e);					\
    _walk_##code##_post (e);				\
    _walk_post (e);					\
  }

ExprWalkerFuncDef (CONSTRUCTOR)
ExprWalkerFuncDef (INTEGER_CST)
ExprWalkerFuncDef (REAL_CST)
ExprWalkerFuncDef (STRING_CST)
ExprWalkerFuncDef (BIT_FIELD_REF)
ExprWalkerFuncDef (ARRAY_REF)
ExprWalkerFuncDef (MEM_REF)
ExprWalkerFuncDef (COMPONENT_REF)
ExprWalkerFuncDef (SSA_NAME)
ExprWalkerFuncDef (ADDR_EXPR)
ExprWalkerFuncDef (VIEW_CONVERT_EXPR)
ExprWalkerFuncDef (IMAGPART_EXPR)
ExprWalkerFuncDef (FIELD_DECL)
ExprWalkerFuncDef (VAR_DECL)
ExprWalkerFuncDef (RESULT_DECL)
ExprWalkerFuncDef (PARM_DECL)
ExprWalkerFuncDef (FUNCTION_DECL)
ExprWalkerFuncDef (LE_EXPR)
ExprWalkerFuncDef (LT_EXPR)
ExprWalkerFuncDef (EQ_EXPR)
ExprWalkerFuncDef (GT_EXPR)
ExprWalkerFuncDef (GE_EXPR)
ExprWalkerFuncDef (NE_EXPR)

void ExprWalker::_walk_leaf (const_tree e, const enum tree_code c)
{
  assert_is_type (e, c);
}

void
ExprWalker::_walk_op_n (const_tree e, unsigned n)
{
  gcc_assert (e);
  const_tree op_n = TREE_OPERAND (e, n);
  gcc_assert (op_n);
  walk (op_n);
}

void
ExprWalker::_walk_op_0 (const_tree e, const enum tree_code c)
{
  assert_is_type (e, c);
  _walk_op_n (e, 0);
}

void
ExprWalker::_walk_op_1 (const_tree e, const enum tree_code c)
{
  assert_is_type (e, c);
  _walk_op_n (e, 0);
  _walk_op_n (e, 1);
}

void
ExprWalker::_walk_CONSTRUCTOR (__attribute__ ((unused)) const_tree e)
{
  // Future-work: If we want to support rewriting CONSTRUCTORs
  // we will have to walk them
}

void
ExprWalker::_walk_LE_EXPR (const_tree e)
{
  _walk_op_1 (e, LE_EXPR);
}

void
ExprWalker::_walk_LT_EXPR (const_tree e)
{
  _walk_op_1 (e, LT_EXPR);
}

void
ExprWalker::_walk_EQ_EXPR (const_tree e)
{
  _walk_op_1 (e, EQ_EXPR);
}

void
ExprWalker::_walk_GT_EXPR (const_tree e)
{
  _walk_op_1 (e, GT_EXPR);
}

void
ExprWalker::_walk_GE_EXPR (const_tree e)
{
  _walk_op_1 (e, GE_EXPR);
}

void
ExprWalker::_walk_NE_EXPR (const_tree e)
{
  _walk_op_1 (e, NE_EXPR);
}

void
ExprWalker::_walk_INTEGER_CST (const_tree e)
{
  _walk_leaf (e, INTEGER_CST);
}

void
ExprWalker::_walk_REAL_CST (const_tree e)
{
  _walk_leaf (e, REAL_CST);
}

void
ExprWalker::_walk_STRING_CST (const_tree e)
{
  _walk_leaf (e, STRING_CST);
}

void
ExprWalker::_walk_BIT_FIELD_REF (__attribute__ ((unused)) const_tree e)
{
  // TODO:
  // We currently don't support bit_field_ref
  // but maybe we need to do something here?
}

void
ExprWalker::_walk_ARRAY_REF (const_tree e)
{
  _walk_op_1 (e, ARRAY_REF);
}

void
ExprWalker::_walk_MEM_REF (const_tree e)
{
  _walk_op_1 (e, MEM_REF);
}

void
ExprWalker::_walk_COMPONENT_REF (const_tree e)
{
  _walk_op_1 (e, COMPONENT_REF);
}

void
ExprWalker::_walk_SSA_NAME (const_tree e)
{
  _walk_leaf (e, SSA_NAME);
}

void
ExprWalker::_walk_ADDR_EXPR (const_tree e)
{
  _walk_op_0 (e, ADDR_EXPR);
}

void
ExprWalker::_walk_VIEW_CONVERT_EXPR (__attribute__ ((unused)) const_tree e)
{
  // TODO: I don't think we need to do anything here
}

void
ExprWalker::_walk_IMAGPART_EXPR (__attribute__ ((unused)) const_tree e)
{
  // TODO: I don't think we need to do anything here
}

void
ExprWalker::_walk_FIELD_DECL (const_tree e)
{
  _walk_leaf (e, FIELD_DECL);
}

void
ExprWalker::_walk_VAR_DECL (const_tree e)
{
  _walk_leaf (e, VAR_DECL);
}

void
ExprWalker::_walk_RESULT_DECL (const_tree e)
{
  _walk_leaf (e, RESULT_DECL);
}

void
ExprWalker::_walk_PARM_DECL (const_tree e)
{
  _walk_leaf (e, PARM_DECL);
}

void
ExprWalker::_walk_FUNCTION_DECL (const_tree e)
{
  _walk_leaf (e, FUNCTION_DECL);
  for (tree parm = DECL_ARGUMENTS (e); parm; parm = DECL_CHAIN (parm))
    {
      walk (parm);
    }
}

/* Main interface for GimpleWalker:
 * iterate over global variables and then for all functions
 * with gimple body.
 */
void
GimpleWalker::walk ()
{
  _walk_globals ();
  std::set<tree> fndecls;
  cgraph_node *node = NULL;
  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    {
      node->get_untransformed_body ();
      tree decl = node->decl;
      gcc_assert (decl);
      const bool already_in_set = fndecls.find (decl) != fndecls.end ();
      // I think it is possible for different nodes to point to the same
      // declaration.
      if (already_in_set)
	continue;

      _walk_cnode (node);
      fndecls.insert (decl);
    }
}

/* For each global variable.  */
void
GimpleWalker::_walk_globals ()
{
  varpool_node *vnode = NULL;
  FOR_EACH_VARIABLE (vnode)
    {
      _walk_global (vnode);
    }
}

/* Walk over global variable VNODE.  */
void
GimpleWalker::_walk_global (varpool_node *vnode)
{
  gcc_assert (vnode);
  struct ipa_ref *ref = NULL;
  for (unsigned i = 0; vnode->iterate_referring (i, ref); i++)
    {
      tree var_decl = vnode->decl;
      walk_tree2 (var_decl);
    }
}

/* Walk over SSA_NAMEs in CNODE.  */
void
GimpleWalker::_walk_ssa_names (cgraph_node *cnode)
{
  const_tree decl = cnode->decl;
  gcc_assert (decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert (func);
  size_t i = 0;
  tree ssa_name = NULL;
  push_cfun (func);
  FOR_EACH_SSA_NAME (i, ssa_name, cfun)
  {
    gcc_assert (ssa_name);
    walk_tree2 (ssa_name);
    tree ssa_name_var = SSA_NAME_VAR (ssa_name);
    if (!ssa_name_var)
      continue;
    walk_tree2 (ssa_name_var);
  }
  pop_cfun ();
}

/* Walk over declaration, locals, ssa_names, and basic blocks
 * in CNODE.  */
void
GimpleWalker::_walk_cnode (cgraph_node *cnode)
{
  gcc_assert (cnode);
  _walk_decl (cnode);
  _walk_locals (cnode);
  _walk_ssa_names (cnode);
  _walk_bb (cnode);
}

/* Walk over each local declaration in CNODE.  */
void
GimpleWalker::_walk_locals (cgraph_node *cnode)
{
  const_tree decl = cnode->decl;
  gcc_assert (decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert (func);
  int i = 0;
  tree var_decl = NULL;
  FOR_EACH_LOCAL_DECL (func, i, var_decl)
    {
      gcc_assert (var_decl);
      walk_tree2 (var_decl);
    }
}

/* Walk over all basic blocks in CNODE.  */
void
GimpleWalker::_walk_bb (cgraph_node *cnode)
{
  gcc_assert (cnode);
  cnode->get_untransformed_body ();
  const_tree decl = cnode->decl;
  gcc_assert (decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert (func);
  basic_block bb = NULL;
  push_cfun (func);
  FOR_EACH_BB_FN (bb, func)
    {
      _walk (bb);
    }
  pop_cfun ();
}

/* Walk over CNODE->decl.  */
void
GimpleWalker::_walk_decl (cgraph_node *cnode)
{
  const_tree decl = cnode->decl;
  gcc_assert (decl);
  walk_tree2 (decl);
}

/* Walk over each gimple statement in BB.  */
void
GimpleWalker::_walk (basic_block bb)
{
  gcc_assert (bb);
  gimple_stmt_iterator gsi = gsi_start_bb (bb);
  while (!gsi_end_p (gsi))
    {
      this->_deleted = false;
      gimple *stmt = gsi_stmt (gsi);
      walk_gimple (stmt);
      // If it is not deleted just continue.
      if (!this->_deleted)
	{
	  gsi_next (&gsi);
	  continue;
	}

      // Otherwise remove statement.
      unlink_stmt_vdef (stmt);
      gsi_remove (&gsi, true);
    }

  // TODO: Maybe outline to its own function?
  for (gimple_stmt_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
       gsi_next (&gsi))
    {
      gimple *stmt = gsi_stmt (gsi);
      walk_gimple (stmt);
    }
}

// call preorder callback
// walk subtrees
// call postorder callback
void
GimpleWalker::walk_gimple (gimple *stmt)
{
  _walk_pre_gimple (stmt);
  _walk_gimple (stmt);
  _walk_post_gimple (stmt);
}

/* Switch for different gimple instruction types.  */
void
GimpleWalker::_walk_gimple (gimple *stmt)
{
// Do not use _walk_pre (s)
// Subtle but important distinction,
// we want to differentiate calling here stamtent from s
// TODO: Maybe delete though?
// This could also be the source for the double insertion to stack bug?
  if (gassign *_gassign = dyn_cast<gassign*>(stmt))
  {
    _walk_pre_gimple(stmt);
    walk_gassign(_gassign);
    _walk_post_gimple(stmt);
    return;
  }

  if (greturn *_greturn = dyn_cast<greturn*>(stmt))
  {
    _walk_pre_gimple(stmt);
    walk_greturn(_greturn);
    _walk_post_gimple(stmt);
    return;
  }

  if (gcond *_gcond = dyn_cast<gcond*>(stmt))
  {
    _walk_pre_gimple(stmt);
    walk_gcond(_gcond);
    _walk_post_gimple(stmt);
    return;
  }

  if (gcall *_gcall = dyn_cast<gcall*>(stmt))
  {
    _walk_pre_gimple(stmt);
    walk_gcall(_gcall);
    _walk_post_gimple(stmt);
    return;
  }

  if (glabel *_glabel = dyn_cast<glabel*>(stmt))
  {
    _walk_pre_gimple(stmt);
    walk_glabel(_glabel);
    _walk_post_gimple(stmt);
    return;
  }

  if (gswitch *_gswitch = dyn_cast<gswitch*>(stmt))
  {
    _walk_pre_gimple(stmt);
    walk_gswitch(_gswitch);
    _walk_post_gimple(stmt);
    return;
  }


  if (gphi *_gphi = dyn_cast<gphi*>(stmt))
  {
    _walk_pre_gimple(stmt);
    walk_gphi(_gphi);
    _walk_post_gimple(stmt);
    return;
  }

  const enum gimple_code code = gimple_code (stmt);
  switch (code)
    {
    case GIMPLE_PREDICT:
    case GIMPLE_DEBUG:
    case GIMPLE_NOP:
      // I think it is safe to skip these
      // but it would also be nice to walk their sub-trees
      return;
      break;
    default:
      break;
  }

  // Break if something is unexpected.
  const char *name = gimple_code_name[code];
  log ("gimple code name %s\n", name);
  gcc_unreachable ();
}

void
GimpleWalker::walk_tree2 (const_tree t)
{
  _walk_pre_tree (t);
  _walk_tree (t);
  _walk_post_tree (t);
}

void
GimpleWalker::_walk_tree (const_tree t)
{}

void
GimpleWalker::walk_gassign (gassign *g)
{
  _walk_pre_gassign (g);
  _walk_gassign (g);
  _walk_post_gassign (g);
}

void
GimpleWalker::_walk_gassign (gassign *g)
{}

void
GimpleWalker::walk_greturn (greturn *g)
{
  _walk_pre_greturn (g);
  _walk_greturn (g);
  _walk_post_greturn (g);
}

void
GimpleWalker::_walk_greturn (greturn *g)
{}

void
GimpleWalker::walk_gcond (gcond *g)
{
  _walk_pre_gcond (g);
  _walk_gcond (g);
  _walk_post_gcond (g);
}

void
GimpleWalker::_walk_gcond (gcond *g)
{}

void
GimpleWalker::walk_gcall (gcall *g)
{
  _walk_pre_gcall (g);
  _walk_gcall (g);
  _walk_post_gcall (g);
}

void
GimpleWalker::_walk_gcall (gcall *g)
{}

void
GimpleWalker::walk_glabel (glabel *g)
{
  _walk_pre_glabel (g);
  _walk_glabel (g);
  _walk_post_glabel (g);
}

void
GimpleWalker::_walk_glabel (glabel *g)
{}

void
GimpleWalker::walk_gswitch (gswitch *g)
{
  _walk_pre_gswitch (g);
  _walk_gswitch (g);
  _walk_post_gswitch (g);
}

void
GimpleWalker::_walk_gswitch (gswitch *g)
{
}

void
GimpleWalker::walk_gphi (gphi *g)
{
  _walk_pre_gphi (g);
  _walk_gphi (g);
  _walk_post_gphi (g);
}

void
GimpleWalker::_walk_gphi (gphi *g)
{
}


void
TypeCollector::collect (const_tree t)
{
  const bool in_set = ptrset.in_universe (t);
  // Early memoization...

  if (in_set)
    return;

  // TODO: Can we move this to the beginning
  // of the function.
  gcc_assert (t);

  // This is the map that holds the types
  // we will encounter in this walk.
  // It should be empty at the beginning.
  // It maps from tree -> bool.
  // The boolean will be updated to show
  // whether a record is reachable from
  // the type.
  gcc_assert (ptr.empty ());
  walk (t);
}

// Make sure partitions are mutually exclusive.
void
TypeCollector::_sanity_check ()
{
  for (tset_t::iterator i = ptrset.points_to_record.begin (),
	    e = ptrset.points_to_record.end ();
       i != e; ++i)
    {
      for (tset_t::iterator j = ptrset.complement.begin (), f = ptrset.complement.end ();
	   j != f; ++j)
	{
	  const_tree type_ptr = *i;
	  gcc_assert (type_ptr);
	  const_tree type_com = *j;
	  gcc_assert (type_com);
	  const bool valid_sets = type_ptr != type_com;
	  if (valid_sets)
	    continue;

	  // Normally, we want a stronger type comparison
	  // that is not just the pointer address
	  // but this is the first sanity check and then we will need to
	  // determine the stronger type comparison.  But first we will need to
	  // fix the types...
	  gcc_unreachable ();
	}
    }
}

/* Determine if T is already memoized in the TypeCollector.  */
bool
TypeCollector::is_memoized (const_tree t)
{
  /* If we haven't seen it then no.  */
  const bool in_set = ptrset.in_universe (t);
  if (!in_set)
    return false;

  // If the memoized type points to a record
  // we must update all types that can refer
  // to memoized type.
  const bool points_to_record = ptrset.in_points_to_record (t);
  for (std::map<const_tree, bool>::iterator i = ptr.begin (), e = ptr.end (); i != e; ++i)
    {
      i->second |= points_to_record;
    }
  return true;
}

void
TypeCollector::_walk_VOID_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_VOID_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_INTEGER_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_INTEGER_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_REAL_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_REAL_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_FIXED_POINT_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_FIXED_POINT_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_COMPLEX_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_COMPLEX_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_ENUMERAL_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_ENUMERAL_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_BOOLEAN_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_BOOLEAN_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_collect_simple (const_tree t)
{
  // Insert into persistent set.
  ptrset.insert (t, ptr[t]);
  // erase from current working set.
  ptr.erase (t);
}

void
TypeCollector::_walk_ARRAY_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_ARRAY_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_POINTER_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_POINTER_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_REFERENCE_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_REFERENCE_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_RECORD_TYPE_post (const_tree t)
{
  // All in ptr point to record
  for (std::map<const_tree, bool>::iterator i = ptr.begin (), e = ptr.end (); i != e; ++i)
    {
      i->second = true;
    }
  _collect_simple (t);
}

void
TypeCollector::_walk_RECORD_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_UNION_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_UNION_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_FUNCTION_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_FUNCTION_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

void
TypeCollector::_walk_METHOD_TYPE_post (const_tree t)
{
  _collect_simple (t);
}

void
TypeCollector::_walk_METHOD_TYPE_pre (const_tree t)
{
  ptr[t] = false;
}

inline void
ExprCollector::_walk_pre (const_tree e)
{
  const_tree t = TREE_TYPE (e);
  gcc_assert (t);
  typeCollector.collect (t);
}

/*
 * For global variables T, this method will collect
 * and partition trees corresponding to types
 * into std::sets.  Concretely speaking, this class
 * partitions trees into two sets:
 * * reach a RECORD_TYPE
 * * does not reach a RECORD_TYPE.
 */
void
GimpleTypeCollector::_walk_pre_tree (const_tree t)
{
  exprCollector.walk (t);
}

/*
 * For assignment statements S, this method will collect
 * and partition trees corresponding to types
 * into std::sets.  Concretely speaking, this class
 * partitions trees into two sets:
 * * reach a RECORD_TYPE
 * * does not reach a RECORD_TYPE
 * The types reachable from the lhs and rhs are placed
 * in these sets.
 */
void
GimpleTypeCollector::_walk_pre_gassign (gassign *s)
{
  const_tree lhs = gimple_assign_lhs (s);
  exprCollector.walk (lhs);

  const enum gimple_rhs_class gclass = gimple_assign_rhs_class (s);
  switch (gclass)
    {
    case GIMPLE_TERNARY_RHS:
      {
	const_tree rhs = gimple_assign_rhs3 (s);
	exprCollector.walk (rhs);
      }
    /* fall-through */
    case GIMPLE_BINARY_RHS:
      {
	const_tree rhs = gimple_assign_rhs2 (s);
	exprCollector.walk (rhs);
      }
    /* fall-through */
    case GIMPLE_UNARY_RHS:
    case GIMPLE_SINGLE_RHS:
      {
	const_tree rhs = gimple_assign_rhs1 (s);
	exprCollector.walk (rhs);
      }
      break;
    default:
      gcc_unreachable ();
      break;
    }
}

void
GimpleTypeCollector::_walk_pre_greturn (greturn *s)
{
  const_tree retval = gimple_return_retval (s);
  if (!retval)
    return;

  exprCollector.walk (retval);
}

void
GimpleTypeCollector::_walk_pre_gcall (gcall *s)
{
  // Walk over the arguments.
  unsigned n = gimple_call_num_args (s);
  for (unsigned i = 0; i < n; i++)
    {
      const_tree a = gimple_call_arg (s, i);
      exprCollector.walk (a);
    }

  // Walk over the return type if it exists.
  const_tree lhs = gimple_call_lhs (s);
  if (!lhs)
    return;

  exprCollector.walk (lhs);
}

// Print working set.
void
GimpleTypeCollector::print_collected ()
{
  tpartitions_t sets = get_record_reaching_trees ();
}

/* Walk over LHS and RHS of conditions.  */
void
GimpleTypeCollector::_walk_pre_gcond (gcond *s)
{
  const_tree lhs = gimple_cond_lhs (s);
  exprCollector.walk (lhs);
  const_tree rhs = gimple_cond_rhs (s);
  exprCollector.walk (rhs);
}

bool
TypeEscaper::is_memoized (__attribute__ ((unused)) const_tree t)
{
  // Can't memoize here because
  // information is propagated up and down
  // the type.
  return false;
}

tpartitions_t
TypeEscaper::get_sets ()
{
  place_escaping_types_in_set ();
  return _ptrset;
}

/* From a map of TREE -> BOOL, the key represents a tree type
 * and the value represents whether the tree escapes.
 * Partition this map into sets.
 */
void
TypeEscaper::place_escaping_types_in_set ()
{
  TypeStringifier stringifier;
  for (typemap::iterator i = calc.begin (), e = calc.end (); i != e; ++i)
    {
      const_tree type = i->first;

      // We should only track interesting types
      // Types which are not in points_to_record are the ones
      // that are pointed to by records.
      // I think it is possible to prune them ahead of time...
      if (!_ptrset.in_points_to_record (type))
	continue;

      const Reason reason = i->second;
      // std::string name = stringifier.stringify(type);
      reason.is_escaping () ? _ptrset.escaping.insert (type)
			    : _ptrset.non_escaping.insert (type);
    }
}

void
TypeEscaper::update (const_tree t, Reason r)
{
  gcc_assert (t);
  _reason = r;
  walk (t);
}

void
TypeEscaper::_update (const_tree t)
{
  gcc_assert (t);
  const bool already_in_typemap = calc.find (t) != calc.end ();
  // Do we have to invalidate all types which point to a volatile type?
  // Or do we have to invalidate all types pointed to by a volatile type?
  // Or do we only invalidate all types which are volatile.
  // This is only the third option.
  const bool is_volatile = TYPE_VOLATILE (t);
  Reason _is_volatile;
  _is_volatile.type_is_volatile = is_volatile;
  Reason _inner = _reason | _is_volatile;
  // always OR
  already_in_typemap ? calc[t] |= _inner : calc[t] = _inner;
}

void
TypeEscaper::_walk_ARRAY_TYPE_pre (const_tree t)
{
  _update (t);
}

void
TypeEscaper::_walk_ARRAY_TYPE_post (const_tree t)
{
  _update (t);
}

void
TypeEscaper::_walk_POINTER_TYPE_pre (const_tree t)
{
  _update (t);
}

void
TypeEscaper::_walk_POINTER_TYPE_post (const_tree t)
{
  _update (t);
}

void
TypeEscaper::_walk_REFERENCE_TYPE_pre (const_tree t)
{
  _update (t);
}

void
TypeEscaper::_walk_RECORD_TYPE_pre (const_tree t)
{
  // we are entering a record
  _inside_record++;
  _update (t);
}

void
TypeEscaper::_walk_RECORD_TYPE_post (const_tree t)
{
  _update (t); // This is in case there was a union
  // we are exiting a record
  _inside_record--;
}

/* Mark as escaping because of union
 * and propagate up and down.
 */
void
TypeEscaper::_walk_UNION_TYPE_pre (const_tree t)
{
  _inside_union++;
  bool is_escaping = _inside_union > 0;
  _reason.type_is_in_union |= is_escaping;
  _update (t);
}

/* Mark bit fields as escaping and propagate up
 * and down.
 */
void
TypeEscaper::_walk_field_pre (const_tree t)
{
  _reason.type_is_in_union |= DECL_BIT_FIELD (t);
}

void
TypeEscaper::_walk_UNION_TYPE_post (const_tree t)
{
  _inside_union--;
  _update (t);
}

/* Mark as escaping because RECORD has a function pointer
 * propagate up and down.
 */
void
TypeEscaper::_walk_FUNCTION_TYPE_pre (__attribute__ ((unused)) const_tree t)
{
  _reason.type_is_in_union |= _inside_record > 0;
}

/* Mark as escaping because RECORD has a function pointer
 * propagate up and down.
 */
void
TypeEscaper::_walk_METHOD_TYPE_pre (__attribute__ ((unused)) const_tree t)
{
  _reason.type_is_in_union |= _inside_record > 0;
}

/* Print escaping reasons.  */
void
TypeEscaper::print_reasons ()
{
  TypeStringifier stringifier;
  for (typemap::iterator i = calc.begin (), e = calc.end (); i != e; ++i)
    {
      const_tree t = i->first;
      std::string name = stringifier.stringify (t);
      Reason r = i->second;
      log ("%s reason: ", name.c_str ());
      r.print ();
    }
}

tpartitions_t
ExprEscaper::get_sets ()
{
  return typeEscaper.get_sets ();
}

void
ExprEscaper::print_reasons ()
{
  typeEscaper.print_reasons ();
}

/* Propagate reason to subexpressions.  */
void
ExprEscaper::update (const_tree t, Reason r)
{
  gcc_assert (t);
  _r = r;
  walk (t);
}

/* Propagate reason to type of subexpressions.  */
void
ExprEscaper::_walk_pre (const_tree e)
{
  _stack.push (e);
  const_tree t = TREE_TYPE (e);

  gcc_assert (t);
  typeEscaper.update (t, _r);
}

void
ExprEscaper::_walk_post (__attribute__ ((unused)) const_tree e)
{
  _stack.pop ();
}

/* Capture casting on LHS.  */
void
ExprEscaper::_walk_SSA_NAME_pre (const_tree e)
{
  const_tree ssa_type = TREE_TYPE (e);

  if (_stack.size () < 4)
    return;

  // TODO:
  // It appears that we have a bug, where we are
  // storing expressions twice on the stack
  const_tree this_expr = _stack.top ();
  _stack.pop ();
  const_tree twice = _stack.top ();
  _stack.pop ();
  const_tree prev_expr = _stack.top ();
  _stack.push (twice);
  _stack.push (this_expr);
  if (TREE_CODE (prev_expr) != MEM_REF)
    return;

  tree op1 = TREE_OPERAND (prev_expr, 1);
  gcc_assert (TREE_CODE (op1) == INTEGER_CST);
  const_tree mref_type = TREE_TYPE (op1);

  Reason old_reason = _r;
  TypeIncompleteEquality structuralEquality;
  // we need to make sure that both of them point to structs?
  if (TREE_CODE (TREE_TYPE (mref_type)) == INTEGER_TYPE)
    return;

  _r.type_is_casted = !structuralEquality.equal (mref_type, ssa_type);
  typeEscaper.update (mref_type, _r);
  typeEscaper.update (ssa_type, _r);
  _r = old_reason;
}

/* Mark constructors as escaping.  */
void
ExprEscaper::_walk_CONSTRUCTOR_pre (const_tree e)
{
  if (TREE_CLOBBER_P (e))
    return;

  // TODO: Instead of overloading global_is_visible field
  // with has a constructor, make a field that denotes that
  // a this has a constructor.
  // Or better yet... modify the constructors!
  _r.global_is_visible = true;
  const_tree t = TREE_TYPE (e);
  typeEscaper.update (t, _r);
}

tpartitions_t
GimpleEscaper::get_sets ()
{
  return exprEscaper.get_sets ();
}

void
GimpleEscaper::print_reasons ()
{
  exprEscaper.print_reasons ();
}

/* Initialize undefined set of functions.  */
void
GimpleEscaper::_init ()
{
  cgraph_node *cnode = NULL;
  FOR_EACH_FUNCTION (cnode)
    {
      gcc_assert (cnode);
      const bool filter = GimpleEscaper::filter_known_function (cnode);
      if (filter)
	continue;

      const_tree decl = cnode->decl;
      gcc_assert (decl);
      undefined.insert (decl);
    }

  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
    {
      gcc_assert (cnode);
      cnode->get_untransformed_body ();
      const_tree decl = cnode->decl;
      gcc_assert (decl);
      undefined.erase (decl);
    }
}

/* Mark cnode graphs escaping if they are externally visible.  */
bool
GimpleEscaper::is_function_escaping (cgraph_node *cnode)
{
  const bool filter = GimpleEscaper::filter_known_function (cnode);
  if (filter)
    return false;

  return cnode->externally_visible;
}

/* Mark fndecl as escaping is they are externally visible or
 * there is no fndecl.  */
bool
GimpleEscaper::is_function_escaping (const_tree fndecl)
{
  if (!fndecl)
    return true;

  if (!TREE_PUBLIC (fndecl) && !DECL_EXTERNAL (fndecl))
    return false;

  return true;
}

/* Mark variable as escaping if it is externally visible.  */
bool
GimpleEscaper::is_variable_escaping (varpool_node *vnode)
{
  gcc_assert (vnode);
  const bool retval = vnode->externally_visible;
  const bool retval2 = vnode->externally_visible_p();
  log("%s externally_visible = %d %d\n", vnode->name(), retval, retval2);
  return retval;
}

/* Walk global variable VNODE.  */
void
GimpleEscaper::_walk_global (varpool_node *vnode)
{
  gcc_assert (vnode);
  const_tree var_decl = vnode->decl;
  Reason reason;
  const bool is_escaping = is_variable_escaping (vnode);
  reason.global_is_visible = is_escaping;

  // TODO: Instead of overloading the semantic meaning of global is visible
  // make different fields for CONSTRUCTOR and for CONSTRUCTOR is not in linking
  // unit
  // TODO: Once we are able to rewrite the CONSTRUCTOR we can get rid of marking
  // types with bracket constructors as illegal.
  tree initial = DECL_INITIAL (var_decl);
  const bool constructor = initial ? TREE_CODE (initial) == CONSTRUCTOR : false;
  const bool error_mark = initial ? TREE_CODE (initial) == ERROR_MARK : false;
  reason.global_is_visible
    |= constructor || error_mark; // static initialization...

  exprEscaper.update (var_decl, reason);
  GimpleWalker::_walk_global (vnode);
}

/* Return true if FNDECL is a known function.  */
bool
GimpleEscaper::filter_known_function (const_tree fndecl)
{
  assert_is_type (fndecl, FUNCTION_DECL);
  if (fndecl_built_in_p (fndecl))
    {
      switch (DECL_FUNCTION_CODE (fndecl))
	{
	case BUILT_IN_FREE:
	case BUILT_IN_MALLOC:
	case BUILT_IN_REALLOC:
	case BUILT_IN_CALLOC:
	case BUILT_IN_MEMSET:
	  return true;
	  break;
	default:
	  break;
	}
    }

  return false;
}

/* Return True if NODE points to a known FUNCTION_DECL.  */
bool
GimpleEscaper::filter_known_function (cgraph_node *node)
{
  if (!node)
    return false;
  return filter_known_function (node->decl);
}

/* Mark Variable declaration of unknown location as escaping.  */
void
GimpleEscaper::_walk_pre_tree (const_tree t)
{
  // Is any global variable escaping?
  Reason reason;
  if (TREE_CODE (t) == VAR_DECL)
    {
      if (DECL_SOURCE_LOCATION (t) == UNKNOWN_LOCATION)
	// Detecting some builtin types
	// I think fprofile-generate inserts some builtin types which
	// cannot be detected in any other way...
	// TODO: Maybe add a new reason instead of overloading is_indirect.
	reason.is_indirect = true;
    }
  exprEscaper.update (t, reason);
}

void
GimpleEscaper::_walk_pre_gassign (gassign *s)
{
  Reason reason;
  const enum gimple_rhs_class code = gimple_assign_rhs_class (s);
  switch (code)
    {
    case GIMPLE_TERNARY_RHS:
      {
	const_tree rhs3 = gimple_assign_rhs3 (s);
	exprEscaper.update (rhs3, reason);
      }
    /* fall-through */
    case GIMPLE_BINARY_RHS:
      {
	const_tree rhs2 = gimple_assign_rhs2 (s);
	exprEscaper.update (rhs2, reason);
      }
    /* fall-through */
    case GIMPLE_UNARY_RHS:
    case GIMPLE_SINGLE_RHS:
      {
	const_tree rhs1 = gimple_assign_rhs1 (s);
	exprEscaper.update (rhs1, reason);
	const_tree lhs = gimple_assign_lhs (s);
	if (!lhs)
	  break;
	exprEscaper.update (lhs, reason);
      }
      break;
    default:
      gcc_unreachable ();
      break;
    }
}

void
GimpleEscaper::_walk_pre_greturn (greturn *s)
{
  Reason reason;
  const_tree val = gimple_return_retval (s);
  if (!val)
    return;
  exprEscaper.update (val, reason);
}

void
GimpleEscaper::_walk_pre_gcond (gcond *s)
{
  Reason reason;
  const_tree lhs = gimple_cond_lhs (s);
  const_tree rhs = gimple_cond_rhs (s);
  gcc_assert (lhs && rhs);
  exprEscaper.update (lhs, reason);
  exprEscaper.update (rhs, reason);
}

void
GimpleEscaper::_walk_pre_gcall (gcall *s)
{
  const_tree fn = gimple_call_fndecl (s);
  // gcc_assert (fn);
  // The above will not always be true
  // It will be false when we have an indirect function
  cgraph_node *node = fn ? cgraph_node::get (fn) : NULL;
  TypeStringifier stringifier;
  const bool _is_function_escaping
    = node ? is_function_escaping (node) : is_function_escaping (fn);
  const bool is_undefined = undefined.find (fn) != undefined.end ();
  log ("is undefined %s\n", is_undefined ? "t" : "f");
  const bool _is_escaping = is_undefined || _is_function_escaping;

  Reason arg_reason;
  arg_reason.parameter_is_visible = _is_escaping;
  arg_reason.is_indirect = !fn;
  unsigned n = gimple_call_num_args (s);
  for (unsigned i = 0; i < n; i++)
    {
      const_tree a = gimple_call_arg (s, i);
      gcc_assert (a);
      if (arg_reason.is_escaping ())
	{
	  std::string name = stringifier.stringify (TREE_TYPE (a));
	  log ("escaping parameter %s\n", name.c_str ());
	}
      exprEscaper.typeEscaper.update (TREE_TYPE (a), arg_reason);
    }

  const_tree lhs = gimple_call_lhs (s);
  if (!lhs)
    return;
  Reason return_reason;
  return_reason.return_is_visible = _is_escaping;
  return_reason.is_indirect = !fn;
  exprEscaper.update (lhs, return_reason);
}

/* Determine if cast comes from a known function.
 * Do this by following the use-def chain.  */
bool
GimpleCaster::follow_def_to_find_if_really_cast (const_tree rhs)
{
  gimple *def_for_rhs = SSA_NAME_DEF_STMT (rhs);
  gcall *is_call = dyn_cast<gcall *> (def_for_rhs);
  if (!is_call)
    return true;

  const_tree fn = gimple_call_fndecl (is_call);
  if (!fn)
    return true;

  bool known_function = GimpleEscaper::filter_known_function (fn);
  return !known_function;
}

void
GimpleCaster::_walk_pre_gassign (gassign *s)
{
  const enum gimple_rhs_class code = gimple_assign_rhs_class (s);
  const bool valid_input = GIMPLE_SINGLE_RHS == code;
  if (!valid_input)
    return;

  // I originally was using gimple_assign_cast_p
  // but that proved to be insufficient...
  // So we have to use our equality comparison...
  TypeIncompleteEquality equality;
  const_tree lhs = gimple_assign_lhs (s);
  const_tree rhs = gimple_assign_rhs1 (s);
  gcc_assert (lhs && rhs);
  Reason reason;
  const_tree t_lhs = TREE_TYPE (lhs);
  const_tree t_rhs = TREE_TYPE (rhs);
  gcc_assert (t_lhs && t_rhs);
  bool is_cast = !equality.equal (t_lhs, t_rhs);
  // If it is cast, we might need to look at the definition of rhs
  // If the definition comes from a known function... then we are good...
  bool is_ssa = TREE_CODE (rhs) == SSA_NAME;
  is_cast = is_ssa ? follow_def_to_find_if_really_cast (rhs) : is_cast;

  reason.type_is_casted = is_cast;
  if (!flag_ipa_type_escape_analysis_keep_casts)
    exprEscaper.typeEscaper.update (TREE_TYPE (lhs), reason);
  if (!flag_ipa_type_escape_analysis_keep_casts)
    exprEscaper.typeEscaper.update (TREE_TYPE (rhs), reason);

  GimpleEscaper::_walk_pre_gassign (s);
}

/* Check to see if arguments are casted.  */
void
GimpleCaster::_walk_pre_gcall (gcall *s)
{
  GimpleEscaper::_walk_pre_gcall (s);
  if (flag_ipa_type_escape_analysis_keep_casts)
    return;

  const_tree fn = gimple_call_fndecl (s);
  // If there's no function declaration, how do we
  // know the argument types?
  if (!fn)
    return;

  cgraph_node *node = cgraph_node::get (fn);
  const bool known_function = GimpleEscaper::filter_known_function (node)
			      || GimpleEscaper::filter_known_function (fn);
  if (known_function)
    return;

  const_tree f_t = TREE_TYPE (fn);
  TypeIncompleteEquality equality;

  unsigned i = 0;
  for (tree a = TYPE_ARG_TYPES (f_t); NULL_TREE != a; a = TREE_CHAIN (a))
    {
      const_tree formal_t = TREE_VALUE (a);
      // There seems to be a final VOID_TYPE at the end of some functions?
      const enum tree_code code = TREE_CODE (formal_t);
      const bool is_void = VOID_TYPE == code;
      if (is_void)
	continue;

      const_tree real = gimple_call_arg (s, i);
      const_tree real_t = TREE_TYPE (real);
      const bool is_casted = !equality.equal (formal_t, real_t);
      Reason arg_reason;
      arg_reason.type_is_casted = is_casted;
      exprEscaper.typeEscaper.update (TREE_TYPE (real), arg_reason);
      i++;
    }

  const_tree lhs = gimple_call_lhs (s);
  if (!lhs)
    return;

  const_tree r_t = TREE_TYPE (f_t);
  const_tree l_t TREE_TYPE (lhs);
  const bool is_casted = !equality.equal (r_t, l_t);
  Reason ret_reason;
  ret_reason.type_is_casted = is_casted;
  exprEscaper.update (lhs, ret_reason);
}

bool
TypeAccessor::is_memoized (const_tree t)
{
  return memoized_map.find (t) != memoized_map.end ();
}

/* Add all fields in struct to memoized map.  */
void
TypeAccessor::_walk_RECORD_TYPE_pre (const_tree t)
{
  add_all_fields_in_struct (t);
  memoized_map.insert (t);
}

/* Initialize all fields as neither read nor written.  */
void
TypeAccessor::add_all_fields_in_struct (const_tree t)
{
  const enum tree_code c = TREE_CODE (t);
  const bool is_record = RECORD_TYPE == c;
  if (!is_record)
    return;

  const bool record_already_in_map = _map.find (t) != _map.end ();
  field_access_map_t field_map;
  field_map = record_already_in_map ? _map[t] : field_map;

  // Let's add all fields to the field map as empty.
  for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
    {
      const bool field_already_in_map_2
	= field_map.find (field) != field_map.end ();
      if (field_already_in_map_2)
	continue;
      field_map[field] = Empty;
    }

  _map[t] = field_map;
}

record_field_map_t
ExprAccessor::get_map ()
{
  return record_field_map;
}

void
ExprAccessor::add_all_fields_in_struct (const_tree t)
{
  _typeAccessor.walk (t);
}

void
ExprAccessor::_walk_pre (const_tree e)
{
  _stack.push (e);
  const_tree t = TREE_TYPE (e);
  add_all_fields_in_struct (t);
}

void
ExprAccessor::_walk_post (__attribute__ ((unused)) const_tree e)
{
  _stack.pop ();
}

void
ExprAccessor::update (const_tree e, unsigned access)
{
  _access = access;
  walk (e);
}

/* Check if we are accessing a field
 * through pointer arithmetic.  If this is happening
 * it is likely the result of an optimization.
 * Since we are not modifying these types of expressions yet
 * we must mark all fields leading to the accessed fields
 * as possibly READ.

 * TODO: If we modify this expression later on, we could
 * make our transformation more powerful and precise by
 * not marking all fields leading up to this one as possibly
 * read.
 */
void
ExprAccessor::_walk_ADDR_EXPR_pre (__attribute__ ((unused)) const_tree e)
{
  log ("expr accessor mem ref\n");
  log ("stack size = %d\n", _stack.size ());

  if (_stack.size () < 4)
    return;

  // TODO: Fix error with double pushing
  const_tree addr_expr = _stack.top ();
  _stack.pop ();
  const_tree twice = _stack.top ();
  _stack.pop ();
  const_tree prev_expr = _stack.top ();
  _stack.push (addr_expr);
  _stack.push (twice);
  log ("prev_expr code = %s\n", get_tree_code_name (TREE_CODE (prev_expr)));
  if (TREE_CODE (prev_expr) != MEM_REF)
    return;

  tree op_0 = TREE_OPERAND (addr_expr, 0);
  const_tree addr_expr_t = TREE_TYPE (op_0);

  TypeStringifier stringifier;
  std::string name = stringifier.stringify (addr_expr_t);
  log ("accessing a field through memref...? %s\n", name.c_str ());
  if (TREE_CODE (addr_expr_t) != RECORD_TYPE)
    return;

  // We are accessing a field of a record through pointer arithmetic...
  // So what field offset are we computing...
  const_tree mref_expr = prev_expr;
  tree offset = TREE_OPERAND (mref_expr, 1);
  gcc_assert (TREE_CODE (offset) == INTEGER_CST);
  tree type_size_tree = TYPE_SIZE_UNIT (addr_expr_t);
  int type_size_int = tree_to_shwi (type_size_tree);
  // TODO: Very recently inserted this assertion so we need to test this
  gcc_assert (type_size_int > 0);
  unsigned offset_int = tree_to_uhwi (offset) % type_size_int;
  // We need to get the field that corresponds to the offset_int
  const bool record_already_in_map
    = record_field_map.find (addr_expr_t) != record_field_map.end ();
  field_access_map_t field_map;
  field_map = record_already_in_map ? record_field_map[addr_expr_t] : field_map;

  tree field = NULL;
  for (field = TYPE_FIELDS (addr_expr_t); field; field = DECL_CHAIN (field))
    {
      log ("ever inside?\n");
      unsigned f_byte_offset = tree_to_uhwi (DECL_FIELD_OFFSET (field));
      unsigned f_offset = f_byte_offset;
      log ("offset field %d, offset by pointer %d\n", f_offset, offset_int);

      // NOTE: ALL FIELDS BEFORE THIS ONE NEED TO EXIST
      // Otherwise, this pointer arithmetic is invalid...
      // After the transformation
      const bool field_already_in_map
	= field_map.find (field) != field_map.end ();
      unsigned prev_access = field_already_in_map ? field_map[field] : Empty;

      prev_access |= Read;
      field_map[field] = prev_access;
      add_all_fields_in_struct (addr_expr_t);
      record_field_map[addr_expr_t] = field_map;

      if (f_offset == offset_int)
	break;
    }
}

/* Find out if we are taking the address of a FIELD_DECL.
 * If this is the case, it means that all FIELDS in this
 * RECORD_TYPE should be marked as READ for safety.
 */
void
ExprAccessor::_walk_COMPONENT_REF_pre (const_tree e)
{
  log ("in component_ref pre\n");
  assert_is_type (e, COMPONENT_REF);
  const_tree op0 = TREE_OPERAND (e, 0);
  gcc_assert (op0);
  const_tree op0_t = TREE_TYPE (op0);
  gcc_assert (op0_t);
  // op0_t can either be a RECORD_TYPE or a UNION_TYPE.
  const enum tree_code code = TREE_CODE (op0_t);
  const bool is_record = RECORD_TYPE == code;
  const bool is_union = UNION_TYPE == code;
  const bool valid = is_record != is_union;
  gcc_assert (valid);

  const_tree op1 = TREE_OPERAND (e, 1);
  assert_is_type (op1, FIELD_DECL);
  log ("%s.%s\n", TypeStringifier::get_type_identifier (op0_t),
       TypeStringifier::get_field_identifier (op1));
  const bool record_already_in_map
    = record_field_map.find (op0_t) != record_field_map.end ();
  field_access_map_t field_map;
  field_map = record_already_in_map ? record_field_map[op0_t] : field_map;
  const bool field_already_in_map = field_map.find (op1) != field_map.end ();
  unsigned prev_access = field_already_in_map ? field_map[op1] : Empty;

  prev_access |= _access;
  field_map[op1] = prev_access;
  add_all_fields_in_struct (op0_t);
  record_field_map[op0_t] = field_map;

  if (_stack.size () < 4)
    return;

  const_tree this_expr = _stack.top ();
  _stack.pop ();
  const_tree twice = _stack.top ();
  _stack.pop ();
  const_tree prev_expr = _stack.top ();
  _stack.push (twice);
  _stack.push (this_expr);
  if (TREE_CODE (prev_expr) != ADDR_EXPR)
    return;

  log ("we are taking address of a component?\n");
  const_tree t = op0_t;
  if (TREE_CODE (t) != RECORD_TYPE)
    return;

  /* If we are taking the address of a component, we need to mark the whole
   * RECORD_TYPE as escaping due to pointer arithmetic.
   * TODO: Maybe add a flag to enable and disable this for debugging and
   * testing.
   */
  for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  {
      log ("ever inside?\n");
      const bool field_already_in_map
	= field_map.find (field) != field_map.end ();
      unsigned prev_access = field_already_in_map ? field_map[field] : Empty;

      prev_access |= Read;
      field_map[field] = prev_access;
      add_all_fields_in_struct (t);
      record_field_map[t] = field_map;
  }
}

/* Print results.  */
void
ExprAccessor::print_accesses ()
{
  for (std::map<const_tree, field_access_map_t>::const_iterator i
       = record_field_map.begin (),
       e = record_field_map.end ();
       i != e; ++i)
    {
      const_tree record = i->first;
      field_access_map_t field_map = i->second;
      for (std::map<const_tree, unsigned>::const_iterator j
	   = field_map.begin (),
	   f = field_map.end ();
	   j != f; ++j)
	{
	  const_tree field = j->first;
	  const std::string name_r
	    = TypeStringifier::get_type_identifier (record);
	  const std::string name_f
	    = TypeStringifier::get_field_identifier (field);
	  unsigned access = j->second;
	  log ("%s.%s = 0x%04x\n", name_r.c_str (), name_f.c_str (), access);
	}
    }
}

/* RECORD_TYPE -> (FIELD_DECL -> bitflag)
 * bitflag specifies if field is read, written or neither.
 */
record_field_map_t
GimpleAccesser::get_map ()
{
  return exprAccessor.get_map ();
}

void
GimpleAccesser::print_accesses ()
{
  exprAccessor.print_accesses ();
}

/* Mark RHS as Read and LHS as Write.  */
void
GimpleAccesser::_walk_pre_gassign (gassign *s)
{
  // There seems to be quite a bit of code duplication here...
  const enum gimple_rhs_class code = gimple_assign_rhs_class (s);
  switch (code)
    {
    case GIMPLE_TERNARY_RHS:
      {
	const_tree rhs3 = gimple_assign_rhs3 (s);
	gcc_assert (rhs3);
	exprAccessor.update (rhs3, Read);
      }
    /* fall-through */
    case GIMPLE_BINARY_RHS:
      {
	const_tree rhs2 = gimple_assign_rhs2 (s);
	gcc_assert (rhs2);
	exprAccessor.update (rhs2, Read);
      }
    /* fall-through */
    case GIMPLE_UNARY_RHS:
    case GIMPLE_SINGLE_RHS:
      {
	const_tree rhs1 = gimple_assign_rhs1 (s);
	exprAccessor.update (rhs1, Read);
	const_tree lhs = gimple_assign_lhs (s);
	if (!lhs)
	  break;
	exprAccessor.update (lhs, Write);
	break;
      }
    default:
      gcc_unreachable ();
      break;
    }
}

/* Mark RHS as Read and LHS as Written.  */
void
GimpleAccesser::_walk_pre_gcall (gcall *s)
{
  unsigned n = gimple_call_num_args (s);
  for (unsigned i = 0; i < n; i++)
    {
      const_tree a = gimple_call_arg (s, i);
      gcc_assert (a);
      exprAccessor.update (a, Read);
    }

  const_tree lhs = gimple_call_lhs (s);
  if (!lhs)
    return;
  exprAccessor.update (lhs, Write);
}

/* Mark as Read.  */
void
GimpleAccesser::_walk_pre_greturn (greturn *s)
{
  const_tree val = gimple_return_retval (s);
  if (!val)
    return;
  exprAccessor.update (val, Read);
}

/* Mark as Read.  */
void
GimpleAccesser::_walk_pre_gcond (gcond *s)
{
  const_tree lhs = gimple_cond_lhs (s);
  const_tree rhs = gimple_cond_rhs (s);
  gcc_assert (lhs && rhs);
  exprAccessor.update (lhs, Read);
  exprAccessor.update (rhs, Read);
}

/* Print Reasons why a type might be escaping.  */
void
Reason::print () const
{
  log ("e=%d g=%d p=%d r=%d c=%d v=%d u=%d i=%d\n", this->is_escaping (),
       this->global_is_visible, this->parameter_is_visible,
       this->return_is_visible, this->type_is_casted, this->type_is_volatile,
       this->type_is_in_union, this->is_indirect);
}

/* Or an escaping Reason.  */
Reason
Reason::operator| (const Reason &other)
{
  Reason retval;
  retval.global_is_visible = this->global_is_visible | other.global_is_visible;
  retval.parameter_is_visible
    = this->parameter_is_visible | other.parameter_is_visible;
  retval.return_is_visible = this->return_is_visible | other.return_is_visible;
  retval.type_is_casted = this->type_is_casted | other.type_is_casted;
  retval.type_is_volatile = this->type_is_volatile | other.type_is_volatile;
  retval.type_is_in_union = this->type_is_in_union | other.type_is_in_union;
  retval.is_indirect = this->is_indirect | other.is_indirect;
  return retval;
}

/* Or an escaping Reason.  */
Reason &
Reason::operator|= (const Reason &other)
{
  this->global_is_visible |= other.global_is_visible;
  this->parameter_is_visible |= other.parameter_is_visible;
  this->return_is_visible |= other.return_is_visible;
  this->type_is_casted |= other.type_is_casted;
  this->type_is_volatile |= other.type_is_volatile;
  this->type_is_in_union |= other.type_is_in_union;
  this->is_indirect |= other.is_indirect;
  return *this;
}

/* Insert TYPE into a partition depending on IN_POINTS_TO_RECORD.  */
void
type_partitions_s::insert (const_tree type, bool in_points_to_record)
{
  gcc_assert (type);
  this->universe.insert (type);
  in_points_to_record ? this->points_to_record.insert (type)
		      : this->complement.insert (type);
  const bool in_points_to_set = this->in_points_to_record (type);
  const bool in_complement = this->in_complement (type);
  const bool _xor = in_points_to_set != in_complement;
  // sanity check...
  gcc_assert (_xor);
}

/* Find out whether TYPE is already in universe.  */
bool
type_partitions_s::in_universe (const_tree type) const
{
  gcc_assert (type);
  const bool seen_before = this->universe.find (type) != this->universe.end ();
  return seen_before;
}

/* Find out whether TYPE is in points_to_record partition.  */
bool
type_partitions_s::in_points_to_record (const_tree type) const
{
  gcc_assert (type);
  const bool seen_before
    = this->points_to_record.find (type) != this->points_to_record.end ();
  return seen_before;
}

/* Find out whether TYPE is not in points to record partition.  */
bool
type_partitions_s::in_complement (const_tree type) const
{
  gcc_assert (type);
  const bool seen_before
    = this->complement.find (type) != this->complement.end ();
  return seen_before;
}

/* Stringify a type.  */
std::string
TypeStringifier::stringify (const_tree t)
{
  if (!dump_file)
    return std::string ("");
  _stringification.clear ();
  gcc_assert (t);
  walk (t);
  return _stringification;
}

void
TypeStringifier::_walk_VOID_TYPE_pre (const_tree t)
{
  _stringify_simple (t);
}

void
TypeStringifier::_walk_INTEGER_TYPE_pre (const_tree t)
{
  _stringify_simple (t);
}

void
TypeStringifier::_walk_REAL_TYPE_pre (const_tree t)
{
  _stringify_simple (t);
}

void
TypeStringifier::_walk_FIXED_POINT_TYPE_pre (const_tree t)
{
  _stringify_simple (t);
}

void
TypeStringifier::_walk_COMPLEX_TYPE_pre (const_tree t)
{
  _stringify_simple (t);
}

void
TypeStringifier::_walk_OFFSET_TYPE_pre (const_tree t)
{
  _stringify_simple (t);
}

void
TypeStringifier::_walk_BOOLEAN_TYPE_pre (const_tree t)
{
  _stringify_simple (t);
}

void
TypeStringifier::_stringify_simple (const_tree t)
{
  gcc_assert (t);
  const enum tree_code code = TREE_CODE (t);
  this->_stringification += std::string (get_tree_code_name (code));
}

void
TypeStringifier::_walk_POINTER_TYPE_post (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string ("*");
}

void
TypeStringifier::_walk_ARRAY_TYPE_post (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string ("[]");
}

void
TypeStringifier::_walk_REFERENCE_TYPE_post (__attribute__ ((unused))
					    const_tree t)
{
  this->_stringification += std::string ("&");
}

void
TypeStringifier::_walk_UNION_TYPE_pre (const_tree t)
{
  this->_stringification += std::string (" union ");
  _stringify_aggregate_pre (t);
}

void
TypeStringifier::_walk_UNION_TYPE_post (const_tree t)
{
  _stringify_aggregate_post (t);
}

void
TypeStringifier::_walk_RECORD_TYPE_pre (const_tree t)
{
  this->_stringification += std::string (" record ");
  _stringify_aggregate_pre (t);
}

void
TypeStringifier::_walk_RECORD_TYPE_post (const_tree t)
{
  _stringify_aggregate_post (t);
}

void
TypeStringifier::_stringify_aggregate_pre (const_tree t)
{
  this->_stringification
    += TypeStringifier::get_type_identifier (t) + std::string (" {");
}

void
TypeStringifier::_stringify_aggregate_post (__attribute__ ((unused))
					    const_tree t)
{
  this->_stringification += std::string ("}");
}

void
TypeStringifier::_walk_field_post (const_tree t)
{
  this->_stringification += std::string (" ")
			    + TypeStringifier::get_field_identifier (t)
			    + std::string (";");
}

void
TypeStringifier::_walk_METHOD_TYPE_pre (const_tree t)
{
  _stringify_fm_pre (t);
}

void
TypeStringifier::_walk_METHOD_TYPE_post (const_tree t)
{
  _stringify_fm_post (t);
}

void
TypeStringifier::_walk_FUNCTION_TYPE_pre (const_tree t)
{
  _stringify_fm_pre (t);
}

void
TypeStringifier::_walk_FUNCTION_TYPE_post (const_tree t)
{
  _stringify_fm_post (t);
}

void
TypeStringifier::_stringify_fm_pre (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string ("function { ");
}

void
TypeStringifier::_stringify_fm_post (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string ("}");
}

void
TypeStringifier::_walk_return_pre (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string ("(");
}

void
TypeStringifier::_walk_return_post (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string (")");
}

void
TypeStringifier::_walk_args_pre (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string ("(");
}

void
TypeStringifier::_walk_args_post (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string (")");
}

void
TypeStringifier::_walk_arg_post (__attribute__ ((unused)) const_tree t)
{
  this->_stringification += std::string (", ");
}

std::string
TypeStringifier::get_type_identifier (const_tree t)
{
  tree name = TYPE_NAME (t);
  const bool no_name = NULL_TREE == name;
  if (no_name)
    return std::string ("");

  const enum tree_code name_code = TREE_CODE (name);
  const bool is_name_type_decl = TYPE_DECL == name_code;
  name = is_name_type_decl ? DECL_NAME (name) : name;
  const char *identifier_ptr = IDENTIFIER_POINTER (name);
  gcc_assert (identifier_ptr);
  return std::string (identifier_ptr);
}

std::string
TypeStringifier::get_field_identifier (const_tree t)
{
  assert_is_type (t, FIELD_DECL);
  const_tree decl_name = DECL_NAME (t);
  if (!decl_name)
    return std::string ("");

  const char *identifier = IDENTIFIER_POINTER (decl_name);
  return std::string (identifier);
}

/* Return true if L and R have equal structural equalities.  */
bool
TypeStructuralEquality::equal (const_tree l, const_tree r)
{
  return _equal (l, r);
}

bool
TypeStructuralEquality::_equal (const_tree l, const_tree r)
{
  bool valid_inputs = l && r;
  if (!valid_inputs)
    return l == r;

  bool equal_codes = _equal_code (l, r);
  if (!equal_codes)
    return equal_codes;

  bool recurse_l = set_l.find (l) != set_l.end ();
  bool recurse_r = set_r.find (r) != set_r.end ();
  // Verify that this the case every time.
  bool recurse = recurse_l || recurse_r;
  if (recurse)
    return recurse;

  set_l.insert (l);
  set_r.insert (r);
  const enum tree_code code = TREE_CODE (l);
  bool equal_children = false;
  switch (code)
    {
#define TSE_CASE(code)				\
  case code:					\
    equal_children = _walk_##code (l, r);	\
    break

      TSE_CASE (VOID_TYPE);
      TSE_CASE (INTEGER_TYPE);
      TSE_CASE (REAL_TYPE);
      TSE_CASE (FIXED_POINT_TYPE);
      TSE_CASE (COMPLEX_TYPE);
      TSE_CASE (ENUMERAL_TYPE);
      TSE_CASE (BOOLEAN_TYPE);
      TSE_CASE (OFFSET_TYPE);
      TSE_CASE (RECORD_TYPE);
      TSE_CASE (POINTER_TYPE);
      TSE_CASE (REFERENCE_TYPE);
      TSE_CASE (ARRAY_TYPE);
      TSE_CASE (UNION_TYPE);
      TSE_CASE (FUNCTION_TYPE);
      TSE_CASE (METHOD_TYPE);
    default:
      gcc_unreachable ();
      break;
    }

  set_l.erase (l);
  set_r.erase (r);
  return equal_children;
}

bool
TypeStructuralEquality::_equal_code (const_tree l, const_tree r)
{
  const enum tree_code code_l = TREE_CODE (l);
  const enum tree_code code_r = TREE_CODE (r);
  const bool equal = code_l == code_r;
  return equal;
}

#define TSE_FUNC_DEF_SIMPLE(code) 					  \
  bool TypeStructuralEquality::_walk_##code (const_tree l, const_tree r)  \
  {									  \
    return _equal_code (l, r);						  \
  }

TSE_FUNC_DEF_SIMPLE (VOID_TYPE)
TSE_FUNC_DEF_SIMPLE (INTEGER_TYPE)
TSE_FUNC_DEF_SIMPLE (REAL_TYPE)
TSE_FUNC_DEF_SIMPLE (FIXED_POINT_TYPE)
TSE_FUNC_DEF_SIMPLE (ENUMERAL_TYPE)
TSE_FUNC_DEF_SIMPLE (BOOLEAN_TYPE)
TSE_FUNC_DEF_SIMPLE (OFFSET_TYPE)
TSE_FUNC_DEF_SIMPLE (COMPLEX_TYPE)

bool
TypeStructuralEquality::_equal_wrapper (const_tree l, const_tree r)
{
  const_tree inner_l = TREE_TYPE (l);
  const_tree inner_r = TREE_TYPE (r);
  return _equal (inner_l, inner_r);
}

#define TSE_FUNC_DEF_WRAPPER(code)					 \
  bool TypeStructuralEquality::_walk_##code (const_tree l, const_tree r) \
  {									 \
    return _equal_wrapper (l, r);					 \
  }

TSE_FUNC_DEF_WRAPPER (REFERENCE_TYPE)
TSE_FUNC_DEF_WRAPPER (ARRAY_TYPE)
TSE_FUNC_DEF_WRAPPER (POINTER_TYPE)

#define TSE_FUNC_DEF_CONTAINER(code)					 \
  bool TypeStructuralEquality::_walk_##code (const_tree l, const_tree r) \
  {									 \
    const_tree field_l = TYPE_FIELDS (l);				 \
    const_tree field_r = TYPE_FIELDS (r);				 \
    bool efield_l = field_l;						 \
    bool efield_r = field_r;						 \
    bool still_equal = efield_l == efield_r;				 \
    if (!still_equal) 							 \
      return still_equal; 						 \
									 \
    while (field_l && field_r && still_equal) 				 \
      {									 \
	const_tree tfield_l = TREE_TYPE (field_l);			 \
	const_tree tfield_r = TREE_TYPE (field_r);			 \
	still_equal &= _equal (tfield_l, tfield_r);			 \
	field_l = DECL_CHAIN (field_l);					 \
	field_r = DECL_CHAIN (field_r);					 \
	efield_l = field_l;						 \
	efield_r = field_r;						 \
	still_equal &= efield_l == efield_r;				 \
      }									 \
    return still_equal;							 \
  }

TSE_FUNC_DEF_CONTAINER (RECORD_TYPE)
TSE_FUNC_DEF_CONTAINER (UNION_TYPE)

#define TSE_FUNC_DEF_FUNC(code)						 \
  bool TypeStructuralEquality::_walk_##code (const_tree l, const_tree r) \
  {									 \
    const_tree tret_l = TREE_TYPE (l);					 \
    const_tree tret_r = TREE_TYPE (r);					 \
    bool still_equal = _equal (tret_l, tret_r);				 \
    if (!still_equal)							 \
      return still_equal;						 \
									 \
    const_tree arg_l = TYPE_ARG_TYPES (l);				 \
    const_tree arg_r = TYPE_ARG_TYPES (r);				 \
    bool earg_l = arg_l;						 \
    bool earg_r = arg_r;						 \
    still_equal &= earg_l == earg_r;					 \
    while (arg_l && arg_r && still_equal)				 \
      {									 \
	const_tree targ_l = TREE_VALUE (arg_l);				 \
	const_tree targ_r = TREE_VALUE (arg_r);				 \
	still_equal &= _equal (targ_l, targ_r);				 \
	arg_l = TREE_CHAIN (arg_l);					 \
	arg_r = TREE_CHAIN (arg_r);					 \
	earg_l = arg_l;							 \
	earg_r = arg_r;							 \
	still_equal &= earg_l == earg_r;				 \
      }									 \
    return still_equal;							 \
  }

TSE_FUNC_DEF_FUNC (FUNCTION_TYPE)
TSE_FUNC_DEF_FUNC (METHOD_TYPE)

/* Used for comparing incomplete types.  */
bool
TypeIncompleteEquality::_equal (const_tree l, const_tree r)
{
  bool valid_inputs = l && r;
  if (!valid_inputs)
    return l == r;

  // If any of these are incomplete, then we can only compare using
  // identifiers...
  const bool complete_l = is_complete (l);
  const bool complete_r = is_complete (r);
  bool can_compare_structurally = complete_l && complete_r;
  if (can_compare_structurally)
    return TypeStructuralEquality::_equal (l, r);

  // Before comparing with identifiers
  // make last attempt to compare using main variants.
  const_tree m_l = TYPE_MAIN_VARIANT (l);
  const_tree m_r = TYPE_MAIN_VARIANT (r);
  gcc_assert (m_l && m_r);
  can_compare_structurally = m_l == m_r;
  if (can_compare_structurally)
    return true;

  const std::string n_l = TypeStringifier::get_type_identifier (m_l);
  const std::string n_r = TypeStringifier::get_type_identifier (m_r);
  return n_l.compare (n_r) == 0;
}

/* Remove non-escaping types and place in escaping types if
 * there is a tree in escaping partition which is equivalent to
 * another tree in non-escaping partition.
 * Perform this until a fixed point is reached.
 */
static void
fix_escaping_types_in_set (tpartitions_t &types)
{
  bool fixed_point_reached = false;
  TypeIncompleteEquality structuralEquality;
  do
    {
      std::vector<const_tree> fixes;
      fixed_point_reached = true;
      for (std::set<const_tree>::const_iterator i = types.escaping.begin (),
						e = types.escaping.end ();
	   i != e; ++i)
	{
	  for (std::set<const_tree>::const_iterator j
	       = types.non_escaping.begin (),
	       f = types.non_escaping.end ();
	       j != f; ++j)
	    {
	      const_tree type_esc = *i;
	      gcc_assert (type_esc);
	      const_tree type_non = *j;
	      gcc_assert (type_non);
	      // There can be cases where incomplete types are marked as
	      // non-escaping and complete types counter parts are marked as
	      // escaping.
	      const bool equal = structuralEquality.equal (type_esc, type_non);
	      if (!equal)
		continue;

	      fixed_point_reached = false;
	      // Add incomplete to escaping
	      // delete incomplete from non_escaping
	      // We shouldn't do that inside our iteration loop.
	      fixes.push_back (type_non);
	    }
	}

      for (std::vector<const_tree>::const_iterator i = fixes.begin (),
						   e = fixes.end ();
	   i != e; ++i)
	{
	  const_tree escaping_type = *i;
	  types.escaping.insert (escaping_type);
	  types.non_escaping.erase (escaping_type);
	}
    }
  while (!fixed_point_reached);
}

simple_ipa_opt_pass *
make_pass_ipa_type_escape_analysis (gcc::context *ctx)
{
  return new pass_ipa_type_escape_analysis (ctx);
}