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
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
|
/* Classes for modeling the state of memory.
Copyright (C) 2019-2020 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "function.h"
#include "basic-block.h"
#include "gimple.h"
#include "gimple-iterator.h"
#include "diagnostic-core.h"
#include "graphviz.h"
#include "options.h"
#include "cgraph.h"
#include "tree-dfa.h"
#include "stringpool.h"
#include "convert.h"
#include "target.h"
#include "fold-const.h"
#include "tree-pretty-print.h"
#include "diagnostic-color.h"
#include "diagnostic-metadata.h"
#include "tristate.h"
#include "bitmap.h"
#include "selftest.h"
#include "function.h"
#include "analyzer/analyzer.h"
#include "analyzer/analyzer-logging.h"
#include "ordered-hash-map.h"
#include "options.h"
#include "cgraph.h"
#include "cfg.h"
#include "digraph.h"
#include "analyzer/supergraph.h"
#include "sbitmap.h"
#include "analyzer/region-model.h"
#include "analyzer/constraint-manager.h"
#include "diagnostic-event-id.h"
#include "analyzer/sm.h"
#include "diagnostic-event-id.h"
#include "analyzer/sm.h"
#include "analyzer/pending-diagnostic.h"
#include "analyzer/analyzer-selftests.h"
#include "stor-layout.h"
#if ENABLE_ANALYZER
namespace ana {
/* Dump T to PP in language-independent form, for debugging/logging/dumping
purposes. */
static void
dump_tree (pretty_printer *pp, tree t)
{
dump_generic_node (pp, t, 0, TDF_SLIM, 0);
}
/* Dump T to PP in language-independent form in quotes, for
debugging/logging/dumping purposes. */
void
dump_quoted_tree (pretty_printer *pp, tree t)
{
pp_begin_quote (pp, pp_show_color (pp));
dump_tree (pp, t);
pp_end_quote (pp, pp_show_color (pp));
}
/* Equivalent to pp_printf (pp, "%qT", t), to avoid nesting pp_printf
calls within other pp_printf calls.
default_tree_printer handles 'T' and some other codes by calling
dump_generic_node (pp, t, 0, TDF_SLIM, 0);
dump_generic_node calls pp_printf in various places, leading to
garbled output.
Ideally pp_printf could be made to be reentrant, but in the meantime
this function provides a workaround. */
static void
print_quoted_type (pretty_printer *pp, tree t)
{
pp_begin_quote (pp, pp_show_color (pp));
dump_generic_node (pp, t, 0, TDF_SLIM, 0);
pp_end_quote (pp, pp_show_color (pp));
}
/* Dump this path_var to PP (which must support %E for trees).
Express the stack depth using an "@DEPTH" suffix, so e.g. given
void foo (int j);
void bar (int i)
{
foo (i);
}
then:
- the "i" in "bar" would be "(i @ 0)"
- the "j" in "foo" would be "(j @ 1)". */
void
path_var::dump (pretty_printer *pp) const
{
if (m_tree == NULL_TREE)
pp_string (pp, "NULL");
if (CONSTANT_CLASS_P (m_tree))
pp_printf (pp, "%qE", m_tree);
else
pp_printf (pp, "(%qE @ %i)", m_tree, m_stack_depth);
}
/* For use in printing a comma-separated list. */
static void
dump_separator (pretty_printer *pp, bool *is_first)
{
if (!*is_first)
pp_string (pp, ", ");
*is_first = false;
}
/* Concrete subclass of constraint_manager that wires it up to a region_model
(whilst allowing the constraint_manager and region_model to be somewhat
at arms length).
TODO: revisit this; maybe put the region_model * into the constraint_manager
base class. */
class impl_constraint_manager : public constraint_manager
{
public:
impl_constraint_manager (region_model *model)
: constraint_manager (),
m_model (model)
{}
impl_constraint_manager (const impl_constraint_manager &other,
region_model *model)
: constraint_manager (other),
m_model (model)
{}
constraint_manager *clone (region_model *model) const
{
return new impl_constraint_manager (*this, model);
}
tree maybe_get_constant (svalue_id sid) const FINAL OVERRIDE
{
svalue *svalue = m_model->get_svalue (sid);
return svalue->maybe_get_constant ();
}
svalue_id get_sid_for_constant (tree cst) const FINAL OVERRIDE
{
gcc_assert (CONSTANT_CLASS_P (cst));
return m_model->get_rvalue (cst, NULL);
}
int get_num_svalues () const FINAL OVERRIDE
{
return m_model->get_num_svalues ();
}
private:
region_model *m_model;
};
/* class svalue_id. */
/* Print this svalue_id to PP. */
void
svalue_id::print (pretty_printer *pp) const
{
if (null_p ())
pp_printf (pp, "null");
else
pp_printf (pp, "sv%i", m_idx);
}
/* Print this svalue_id in .dot format to PP. */
void
svalue_id::dump_node_name_to_pp (pretty_printer *pp) const
{
gcc_assert (!null_p ());
pp_printf (pp, "svalue_%i", m_idx);
}
/* Assert that this object is valid (w.r.t. MODEL). */
void
svalue_id::validate (const region_model &model) const
{
gcc_assert (null_p () || m_idx < (int)model.get_num_svalues ());
}
/* class region_id. */
/* Print this region_id to PP. */
void
region_id::print (pretty_printer *pp) const
{
if (null_p ())
pp_printf (pp, "null");
else
pp_printf (pp, "r%i", m_idx);
}
/* Print this region_id in .dot format to PP. */
void
region_id::dump_node_name_to_pp (pretty_printer *pp) const
{
gcc_assert (!null_p ());
pp_printf (pp, "region_%i", m_idx);
}
/* Assert that this object is valid (w.r.t. MODEL). */
void
region_id::validate (const region_model &model) const
{
gcc_assert (null_p () || m_idx < (int)model.get_num_regions ());
}
/* class region_id_set. */
region_id_set::region_id_set (const region_model *model)
: m_bitmap (model->get_num_regions ())
{
bitmap_clear (m_bitmap);
}
/* class svalue_id_set. */
svalue_id_set::svalue_id_set ()
: m_bitmap (NULL)
{
bitmap_clear (m_bitmap);
}
/* class svalue and its various subclasses. */
/* class svalue. */
/* svalue's equality operator. Most of the work is done by the
a "compare_fields" implementation on each subclass. */
bool
svalue::operator== (const svalue &other) const
{
enum svalue_kind this_kind = get_kind ();
enum svalue_kind other_kind = other.get_kind ();
if (this_kind != other_kind)
return false;
if (m_type != other.m_type)
return false;
switch (this_kind)
{
default:
gcc_unreachable ();
case SK_REGION:
{
const region_svalue &this_sub
= (const region_svalue &)*this;
const region_svalue &other_sub
= (const region_svalue &)other;
return this_sub.compare_fields (other_sub);
}
break;
case SK_CONSTANT:
{
const constant_svalue &this_sub
= (const constant_svalue &)*this;
const constant_svalue &other_sub
= (const constant_svalue &)other;
return this_sub.compare_fields (other_sub);
}
break;
case SK_UNKNOWN:
{
const unknown_svalue &this_sub
= (const unknown_svalue &)*this;
const unknown_svalue &other_sub
= (const unknown_svalue &)other;
return this_sub.compare_fields (other_sub);
}
break;
case SK_POISONED:
{
const poisoned_svalue &this_sub
= (const poisoned_svalue &)*this;
const poisoned_svalue &other_sub
= (const poisoned_svalue &)other;
return this_sub.compare_fields (other_sub);
}
break;
case SK_SETJMP:
{
const setjmp_svalue &this_sub
= (const setjmp_svalue &)*this;
const setjmp_svalue &other_sub
= (const setjmp_svalue &)other;
return this_sub.compare_fields (other_sub);
}
break;
}
}
/* Generate a hash value for this svalue. Most of the work is done by the
add_to_hash vfunc. */
hashval_t
svalue::hash () const
{
inchash::hash hstate;
if (m_type)
hstate.add_int (TYPE_UID (m_type));
add_to_hash (hstate);
return hstate.end ();
}
/* Print this svalue and its ID to PP. */
void
svalue::print (const region_model &model,
svalue_id this_sid,
pretty_printer *pp) const
{
this_sid.print (pp);
pp_string (pp, ": {");
if (m_type)
{
gcc_assert (TYPE_P (m_type));
pp_string (pp, "type: ");
print_quoted_type (pp, m_type);
pp_string (pp, ", ");
}
/* vfunc. */
print_details (model, this_sid, pp);
pp_string (pp, "}");
}
/* Dump this svalue in the form of a .dot record to PP. */
void
svalue::dump_dot_to_pp (const region_model &model,
svalue_id this_sid,
pretty_printer *pp) const
{
this_sid.dump_node_name_to_pp (pp);
pp_printf (pp, " [label=\"");
pp_write_text_to_stream (pp);
this_sid.print (pp);
pp_string (pp, ": {");
print (model, this_sid, pp);
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
pp_string (pp, "}\"];");
pp_newline (pp);
}
/* Base implementation of svalue::remap_region_ids vfunc. */
void
svalue::remap_region_ids (const region_id_map &)
{
/* Empty. */
}
/* Base implementation of svalue::walk_for_canonicalization vfunc. */
void
svalue::walk_for_canonicalization (canonicalization *) const
{
/* Empty. */
}
/* Base implementation of svalue::get_child_sid vfunc. */
svalue_id
svalue::get_child_sid (region *parent ATTRIBUTE_UNUSED,
region *child,
region_model &model,
region_model_context *ctxt ATTRIBUTE_UNUSED)
{
svalue *new_child_value = clone ();
if (child->get_type ())
new_child_value->m_type = child->get_type ();
svalue_id new_child_sid = model.add_svalue (new_child_value);
return new_child_sid;
}
/* If this svalue is a constant_svalue, return the underlying tree constant.
Otherwise return NULL_TREE. */
tree
svalue::maybe_get_constant () const
{
if (const constant_svalue *cst_sval = dyn_cast_constant_svalue ())
return cst_sval->get_constant ();
else
return NULL_TREE;
}
/* class region_svalue : public svalue. */
/* Compare the fields of this region_svalue with OTHER, returning true
if they are equal.
For use by svalue::operator==. */
bool
region_svalue::compare_fields (const region_svalue &other) const
{
return m_rid == other.m_rid;
}
/* Implementation of svalue::add_to_hash vfunc for region_svalue. */
void
region_svalue::add_to_hash (inchash::hash &hstate) const
{
inchash::add (m_rid, hstate);
}
/* Implementation of svalue::print_details vfunc for region_svalue. */
void
region_svalue::print_details (const region_model &model ATTRIBUTE_UNUSED,
svalue_id this_sid ATTRIBUTE_UNUSED,
pretty_printer *pp) const
{
if (m_rid.null_p ())
pp_string (pp, "NULL");
else
{
pp_string (pp, "&");
m_rid.print (pp);
}
}
/* Implementation of svalue::dump_dot_to_pp for region_svalue. */
void
region_svalue::dump_dot_to_pp (const region_model &model,
svalue_id this_sid,
pretty_printer *pp) const
{
svalue::dump_dot_to_pp (model, this_sid, pp);
/* If non-NULL, add an edge to the pointed-to region. */
if (!m_rid.null_p ())
{
this_sid.dump_node_name_to_pp (pp);
pp_string (pp, " -> ");
m_rid.dump_node_name_to_pp (pp);
pp_string (pp, ";");
pp_newline (pp);
}
}
/* Implementation of svalue::remap_region_ids vfunc for region_svalue. */
void
region_svalue::remap_region_ids (const region_id_map &map)
{
map.update (&m_rid);
}
/* Merge REGION_SVAL_A and REGION_SVAL_B using MERGER, writing the result
into *MERGED_SID. */
void
region_svalue::merge_values (const region_svalue ®ion_sval_a,
const region_svalue ®ion_sval_b,
svalue_id *merged_sid,
tree type,
model_merger *merger)
{
region_id a_rid = region_sval_a.get_pointee ();
region_id b_rid = region_sval_b.get_pointee ();
/* Both are non-NULL. */
gcc_assert (!a_rid.null_p () && !b_rid.null_p ());
/* Have these ptr-values already been merged? */
region_id a_rid_in_m
= merger->m_map_regions_from_a_to_m.get_dst_for_src (a_rid);
region_id b_rid_in_m
= merger->m_map_regions_from_b_to_m.get_dst_for_src (b_rid);
/* "null_p" here means "we haven't seen this ptr-value before".
If we've seen one but not the other, or we have different
regions, then the merged ptr has to be "unknown". */
if (a_rid_in_m != b_rid_in_m)
{
svalue *merged_sval = new unknown_svalue (type);
*merged_sid = merger->m_merged_model->add_svalue (merged_sval);
return;
}
/* Have we seen this yet? If so, reuse the value. */
if (!a_rid_in_m.null_p ())
{
*merged_sid
= merger->m_merged_model->get_or_create_ptr_svalue (type, a_rid_in_m);
return;
}
/* Otherwise we have A/B regions that haven't been referenced yet. */
/* Are the regions the "same", when seen from the tree point-of-view.
If so, create a merged pointer to it. */
path_var pv_a = merger->m_model_a->get_representative_path_var (a_rid);
path_var pv_b = merger->m_model_b->get_representative_path_var (b_rid);
if (pv_a.m_tree
&& pv_a == pv_b)
{
region_id merged_pointee_rid
= merger->m_merged_model->get_lvalue (pv_a, NULL);
*merged_sid
= merger->m_merged_model->get_or_create_ptr_svalue (type,
merged_pointee_rid);
merger->record_regions (a_rid, b_rid, merged_pointee_rid);
return;
}
/* Handle an A/B pair of ptrs that both point at heap regions.
If they both have a heap region in the merger model, merge them. */
region *region_a = merger->m_model_a->get_region (a_rid);
region *region_b = merger->m_model_b->get_region (b_rid);
region_id a_parent_rid = region_a->get_parent ();
region_id b_parent_rid = region_b->get_parent ();
region *parent_region_a = merger->m_model_a->get_region (a_parent_rid);
region *parent_region_b = merger->m_model_b->get_region (b_parent_rid);
if (parent_region_a
&& parent_region_b
&& parent_region_a->get_kind () == RK_HEAP
&& parent_region_b->get_kind () == RK_HEAP)
{
/* We have an A/B pair of ptrs that both point at heap regions. */
/* presumably we want to see if each A/B heap region already
has a merged region, and, if so, is it the same one. */
// This check is above
region_id merged_pointee_rid
= merger->m_merged_model->add_new_malloc_region ();
*merged_sid
= merger->m_merged_model->get_or_create_ptr_svalue
(type, merged_pointee_rid);
merger->record_regions (a_rid, b_rid, merged_pointee_rid);
return;
}
/* Two different non-NULL pointers? Merge to unknown. */
svalue *merged_sval = new unknown_svalue (type);
*merged_sid = merger->m_merged_model->add_svalue (merged_sval);
return;
}
/* Implementation of svalue::walk_for_canonicalization vfunc for
region_svalue. */
void
region_svalue::walk_for_canonicalization (canonicalization *c) const
{
c->walk_rid (m_rid);
}
/* Evaluate the condition LHS OP RHS.
Subroutine of region_model::eval_condition for when we have a pair of
pointers. */
tristate
region_svalue::eval_condition (region_svalue *lhs,
enum tree_code op,
region_svalue *rhs)
{
/* See if they point to the same region. */
/* TODO: what about child regions where the child is the first child
(or descendent)? */
region_id lhs_rid = lhs->get_pointee ();
region_id rhs_rid = rhs->get_pointee ();
switch (op)
{
default:
gcc_unreachable ();
case EQ_EXPR:
if (lhs_rid == rhs_rid)
return tristate::TS_TRUE;
else
return tristate::TS_FALSE;
break;
case NE_EXPR:
if (lhs_rid != rhs_rid)
return tristate::TS_TRUE;
else
return tristate::TS_FALSE;
break;
case GE_EXPR:
case LE_EXPR:
if (lhs_rid == rhs_rid)
return tristate::TS_TRUE;
break;
case GT_EXPR:
case LT_EXPR:
if (lhs_rid == rhs_rid)
return tristate::TS_FALSE;
break;
}
return tristate::TS_UNKNOWN;
}
/* class constant_svalue : public svalue. */
/* Compare the fields of this constant_svalue with OTHER, returning true
if they are equal.
For use by svalue::operator==. */
bool
constant_svalue::compare_fields (const constant_svalue &other) const
{
return m_cst_expr == other.m_cst_expr;
}
/* Implementation of svalue::add_to_hash vfunc for constant_svalue. */
void
constant_svalue::add_to_hash (inchash::hash &hstate) const
{
inchash::add_expr (m_cst_expr, hstate);
}
/* Merge the CST_SVAL_A and CST_SVAL_B using MERGER, writing the id of
the resulting svalue into *MERGED_SID. */
void
constant_svalue::merge_values (const constant_svalue &cst_sval_a,
const constant_svalue &cst_sval_b,
svalue_id *merged_sid,
model_merger *merger)
{
tree cst_a = cst_sval_a.get_constant ();
tree cst_b = cst_sval_b.get_constant ();
svalue *merged_sval;
if (cst_a == cst_b)
{
/* If they are the same constant, merge as that constant value. */
merged_sval = new constant_svalue (cst_a);
}
else
{
/* Otherwise, we have two different constant values.
Merge as an unknown value.
TODO: impose constraints on the value?
(maybe just based on A, to avoid infinite chains) */
merged_sval = new unknown_svalue (TREE_TYPE (cst_a));
}
*merged_sid = merger->m_merged_model->add_svalue (merged_sval);
}
/* Evaluate the condition LHS OP RHS.
Subroutine of region_model::eval_condition for when we have a pair of
constants. */
tristate
constant_svalue::eval_condition (constant_svalue *lhs,
enum tree_code op,
constant_svalue *rhs)
{
tree lhs_const = lhs->get_constant ();
tree rhs_const = rhs->get_constant ();
gcc_assert (CONSTANT_CLASS_P (lhs_const));
gcc_assert (CONSTANT_CLASS_P (rhs_const));
/* Check for comparable types. */
if (types_compatible_p (TREE_TYPE (lhs_const), TREE_TYPE (rhs_const)))
{
tree comparison
= fold_binary (op, boolean_type_node, lhs_const, rhs_const);
if (comparison == boolean_true_node)
return tristate (tristate::TS_TRUE);
if (comparison == boolean_false_node)
return tristate (tristate::TS_FALSE);
}
return tristate::TS_UNKNOWN;
}
/* Implementation of svalue::print_details vfunc for constant_svalue. */
void
constant_svalue::print_details (const region_model &model ATTRIBUTE_UNUSED,
svalue_id this_sid ATTRIBUTE_UNUSED,
pretty_printer *pp) const
{
pp_printf (pp, "%qE", m_cst_expr);
}
/* Implementation of svalue::get_child_sid vfunc for constant_svalue. */
svalue_id
constant_svalue::get_child_sid (region *parent ATTRIBUTE_UNUSED,
region *child,
region_model &model,
region_model_context *ctxt ATTRIBUTE_UNUSED)
{
/* TODO: handle the all-zeroes case by returning an all-zeroes of the
child type. */
/* Otherwise, we don't have a good way to get a child value out of a
constant.
Handle this case by using an unknown value. */
svalue *unknown_sval = new unknown_svalue (child->get_type ());
return model.add_svalue (unknown_sval);
}
/* class unknown_svalue : public svalue. */
/* Compare the fields of this unknown_svalue with OTHER, returning true
if they are equal.
For use by svalue::operator==. */
bool
unknown_svalue::compare_fields (const unknown_svalue &) const
{
/* I *think* we want to return true here, in that when comparing
two region models, we want two peer unknown_svalue instances
to be the "same". */
return true;
}
/* Implementation of svalue::add_to_hash vfunc for unknown_svalue. */
void
unknown_svalue::add_to_hash (inchash::hash &) const
{
/* Empty. */
}
/* Implementation of svalue::print_details vfunc for unknown_svalue. */
void
unknown_svalue::print_details (const region_model &model ATTRIBUTE_UNUSED,
svalue_id this_sid ATTRIBUTE_UNUSED,
pretty_printer *pp) const
{
pp_string (pp, "unknown");
}
/* Get a string for KIND for use in debug dumps. */
const char *
poison_kind_to_str (enum poison_kind kind)
{
switch (kind)
{
default:
gcc_unreachable ();
case POISON_KIND_FREED:
return "freed";
case POISON_KIND_POPPED_STACK:
return "popped stack";
}
}
/* class poisoned_svalue : public svalue. */
/* Compare the fields of this poisoned_svalue with OTHER, returning true
if they are equal.
For use by svalue::operator==. */
bool
poisoned_svalue::compare_fields (const poisoned_svalue &other) const
{
return m_kind == other.m_kind;
}
/* Implementation of svalue::add_to_hash vfunc for poisoned_svalue. */
void
poisoned_svalue::add_to_hash (inchash::hash &hstate) const
{
hstate.add_int (m_kind);
}
/* Implementation of svalue::print_details vfunc for poisoned_svalue. */
void
poisoned_svalue::print_details (const region_model &model ATTRIBUTE_UNUSED,
svalue_id this_sid ATTRIBUTE_UNUSED,
pretty_printer *pp) const
{
pp_printf (pp, "poisoned: %s", poison_kind_to_str (m_kind));
}
/* class setjmp_svalue's implementation is in engine.cc, so that it can use
the declaration of exploded_node. */
/* class region and its various subclasses. */
/* Get a string for KIND for use in debug dumps. */
const char *
region_kind_to_str (enum region_kind kind)
{
switch (kind)
{
default:
gcc_unreachable ();
case RK_PRIMITIVE:
return "primitive";
case RK_STRUCT:
return "struct";
case RK_UNION:
return "union";
case RK_ARRAY:
return "array";
case RK_FRAME:
return "frame";
case RK_GLOBALS:
return "globals";
case RK_CODE:
return "code";
case RK_FUNCTION:
return "function";
case RK_STACK:
return "stack";
case RK_HEAP:
return "heap";
case RK_ROOT:
return "root";
case RK_SYMBOLIC:
return "symbolic";
}
}
/* class region. */
/* Equality operator for region.
After comparing base class fields and kind, the rest of the
comparison is handled off to a "compare_fields" member function
specific to the appropriate subclass. */
bool
region::operator== (const region &other) const
{
if (m_parent_rid != other.m_parent_rid)
return false;
if (m_sval_id != other.m_sval_id)
return false;
if (m_type != other.m_type)
return false;
enum region_kind this_kind = get_kind ();
enum region_kind other_kind = other.get_kind ();
if (this_kind != other_kind)
return false;
/* Compare views. */
if (m_view_rids.length () != other.m_view_rids.length ())
return false;
int i;
region_id *rid;
FOR_EACH_VEC_ELT (m_view_rids, i, rid)
if (! (*rid == other.m_view_rids[i]))
return false;
switch (this_kind)
{
default:
gcc_unreachable ();
case RK_PRIMITIVE:
{
#if 1
return true;
#else
const primitive_region &this_sub
= (const primitive_region &)*this;
const primitive_region &other_sub
= (const primitive_region &)other;
return this_sub.compare_fields (other_sub);
#endif
}
case RK_STRUCT:
{
const struct_region &this_sub
= (const struct_region &)*this;
const struct_region &other_sub
= (const struct_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_UNION:
{
const union_region &this_sub
= (const union_region &)*this;
const union_region &other_sub
= (const union_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_ARRAY:
{
const array_region &this_sub
= (const array_region &)*this;
const array_region &other_sub
= (const array_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_FRAME:
{
const frame_region &this_sub
= (const frame_region &)*this;
const frame_region &other_sub
= (const frame_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_GLOBALS:
{
const globals_region &this_sub
= (const globals_region &)*this;
const globals_region &other_sub
= (const globals_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_CODE:
{
const code_region &this_sub
= (const code_region &)*this;
const code_region &other_sub
= (const code_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_FUNCTION:
{
const function_region &this_sub
= (const function_region &)*this;
const function_region &other_sub
= (const function_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_STACK:
{
const stack_region &this_sub
= (const stack_region &)*this;
const stack_region &other_sub
= (const stack_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_ROOT:
{
const root_region &this_sub
= (const root_region &)*this;
const root_region &other_sub
= (const root_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_SYMBOLIC:
{
const symbolic_region &this_sub
= (const symbolic_region &)*this;
const symbolic_region &other_sub
= (const symbolic_region &)other;
return this_sub.compare_fields (other_sub);
}
case RK_HEAP:
{
const heap_region &this_sub
= (const heap_region &)*this;
const heap_region &other_sub
= (const heap_region &)other;
return this_sub.compare_fields (other_sub);
}
}
}
/* Get the parent region of this region. */
region *
region::get_parent_region (const region_model &model) const
{
return model.get_region (m_parent_rid);
}
/* Set this region's value to RHS_SID (or potentially a variant of it,
for some kinds of casts). */
void
region::set_value (region_model &model, region_id this_rid, svalue_id rhs_sid,
region_model_context *ctxt)
{
/* Handle some kinds of casting. */
if (m_type)
{
svalue *sval = model.get_svalue (rhs_sid);
if (sval->get_type ())
rhs_sid = model.maybe_cast (m_type, rhs_sid, ctxt);
sval = model.get_svalue (rhs_sid);
if (sval->get_type ())
gcc_assert (m_type == sval->get_type ());
}
m_sval_id = rhs_sid;
/* Update views.
If this is a view, it becomes its parent's active view.
If there was already an active views, invalidate its value; otherwise
if the parent itself had a value, invalidate it.
If it's not a view, then deactivate any view that is active on this
region. */
{
if (m_is_view)
become_active_view (model, this_rid);
else
{
deactivate_any_active_view (model);
gcc_assert (m_active_view_rid.null_p ());
}
}
}
/* Make this region (with id THIS_RID) the "active" view of its parent.
Any other active view has its value set to "unknown" and descendent values
cleared.
If there wasn't an active view, then set the parent's value to unknown, and
clear its descendent values (apart from this view). */
void
region::become_active_view (region_model &model, region_id this_rid)
{
gcc_assert (m_is_view);
region *parent_reg = model.get_region (m_parent_rid);
gcc_assert (parent_reg);
region_id old_active_view_rid = parent_reg->m_active_view_rid;
if (old_active_view_rid == this_rid)
{
/* Already the active view: do nothing. */
return;
}
/* We have a change of active view. */
parent_reg->m_active_view_rid = this_rid;
if (old_active_view_rid.null_p ())
{
/* No previous active view, but the parent and its other children
might have values.
If so, invalidate those values - but not that of the new view. */
region_id_set below_region (&model);
model.get_descendents (m_parent_rid, &below_region, this_rid);
for (unsigned i = 0; i < model.get_num_regions (); i++)
{
region_id rid (region_id::from_int (i));
if (below_region.region_p (rid))
{
region *other_reg = model.get_region (rid);
other_reg->m_sval_id = svalue_id::null ();
}
}
region *parent = model.get_region (m_parent_rid);
parent->m_sval_id
= model.add_svalue (new unknown_svalue (parent->get_type ()));
}
else
{
/* If there was an active view, invalidate it. */
region *old_active_view = model.get_region (old_active_view_rid);
old_active_view->deactivate_view (model, old_active_view_rid);
}
}
/* If this region (with id THIS_RID) has an active view, deactivate it,
clearing m_active_view_rid. */
void
region::deactivate_any_active_view (region_model &model)
{
if (m_active_view_rid.null_p ())
return;
region *view = model.get_region (m_active_view_rid);
view->deactivate_view (model, m_active_view_rid);
m_active_view_rid = region_id::null ();
}
/* Clear any values for regions below THIS_RID.
Set the view's value to unknown. */
void
region::deactivate_view (region_model &model, region_id this_view_rid)
{
gcc_assert (is_view_p ());
/* Purge values from old_active_this_view_rid and all its
descendents. Potentially we could use a poison value
for this, but let's use unknown for now. */
region_id_set below_view (&model);
model.get_descendents (this_view_rid, &below_view, region_id::null ());
for (unsigned i = 0; i < model.get_num_regions (); i++)
{
region_id rid (region_id::from_int (i));
if (below_view.region_p (rid))
{
region *other_reg = model.get_region (rid);
other_reg->m_sval_id = svalue_id::null ();
}
}
m_sval_id = model.add_svalue (new unknown_svalue (get_type ()));
}
/* Get a value for this region, either its value if it has one,
or, failing that, "inherit" a value from first ancestor with a
non-null value.
For example, when getting the value for a local variable within
a stack frame that doesn't have one, the frame doesn't have a value
either, but the stack as a whole will have an "uninitialized" poison
value, so inherit that. */
svalue_id
region::get_value (region_model &model, bool non_null,
region_model_context *ctxt)
{
/* If this region has a value, use it. */
if (!m_sval_id.null_p ())
return m_sval_id;
/* Otherwise, "inherit" value from first ancestor with a
non-null value. */
region *parent = model.get_region (m_parent_rid);
if (parent)
{
svalue_id inherited_sid
= parent->get_inherited_child_sid (this, model, ctxt);
if (!inherited_sid.null_p ())
return inherited_sid;
}
/* If a non-null value has been requested, then generate
a new unknown value. Store it, so that repeated reads from this
region will yield the same unknown value. */
if (non_null)
{
svalue_id unknown_sid = model.add_svalue (new unknown_svalue (m_type));
m_sval_id = unknown_sid;
return unknown_sid;
}
return svalue_id::null ();
}
/* Get a value for CHILD, inheriting from this region.
Recurse, so this region will inherit a value if it doesn't already
have one. */
svalue_id
region::get_inherited_child_sid (region *child,
region_model &model,
region_model_context *ctxt)
{
if (m_sval_id.null_p ())
{
/* Recurse. */
if (!m_parent_rid.null_p ())
{
region *parent = model.get_region (m_parent_rid);
m_sval_id = parent->get_inherited_child_sid (this, model, ctxt);
}
}
if (!m_sval_id.null_p ())
{
/* Clone the parent's value, so that attempts to update it
(e.g giving a specific value to an inherited "uninitialized"
value) touch the child, and not the parent. */
svalue *this_value = model.get_svalue (m_sval_id);
svalue_id new_child_sid
= this_value->get_child_sid (this, child, model, ctxt);
if (ctxt)
ctxt->on_inherited_svalue (m_sval_id, new_child_sid);
child->m_sval_id = new_child_sid;
return new_child_sid;
}
return svalue_id::null ();
}
/* Copy from SRC_RID to DST_RID, using CTXT for any issues that occur.
Copy across any value for the region, and handle structs, unions
and arrays recursively. */
void
region_model::copy_region (region_id dst_rid, region_id src_rid,
region_model_context *ctxt)
{
gcc_assert (!dst_rid.null_p ());
gcc_assert (!src_rid.null_p ());
if (dst_rid == src_rid)
return;
region *dst_reg = get_region (dst_rid);
region *src_reg = get_region (src_rid);
/* Copy across any value for the src region itself. */
svalue_id sid = src_reg->get_value (*this, true, ctxt);
set_value (dst_rid, sid, ctxt);
if (dst_reg->get_kind () != src_reg->get_kind ())
return;
/* Copy across child regions for structs, unions, and arrays. */
switch (dst_reg->get_kind ())
{
case RK_PRIMITIVE:
return;
case RK_STRUCT:
{
struct_region *dst_sub = as_a <struct_region *> (dst_reg);
struct_region *src_sub = as_a <struct_region *> (src_reg);
copy_struct_region (dst_rid, dst_sub, src_sub, ctxt);
}
return;
case RK_UNION:
{
union_region *src_sub = as_a <union_region *> (src_reg);
copy_union_region (dst_rid, src_sub, ctxt);
}
return;
case RK_FRAME:
case RK_GLOBALS:
case RK_CODE:
case RK_FUNCTION:
return;
case RK_ARRAY:
{
array_region *dst_sub = as_a <array_region *> (dst_reg);
array_region *src_sub = as_a <array_region *> (src_reg);
copy_array_region (dst_rid, dst_sub, src_sub, ctxt);
}
return;
case RK_STACK:
case RK_HEAP:
case RK_ROOT:
case RK_SYMBOLIC:
return;
}
}
/* Subroutine of region_model::copy_region for copying the child
regions for a struct. */
void
region_model::copy_struct_region (region_id dst_rid,
struct_region *dst_reg,
struct_region *src_reg,
region_model_context *ctxt)
{
for (map_region::iterator_t iter = src_reg->begin ();
iter != src_reg->end (); ++iter)
{
tree src_key = (*iter).first;
region_id src_field_rid = (*iter).second;
region *src_field_reg = get_region (src_field_rid);
region_id dst_field_rid
= dst_reg->get_or_create (this, dst_rid, src_key,
src_field_reg->get_type (), ctxt);
copy_region (dst_field_rid, src_field_rid, ctxt);
}
}
/* Subroutine of region_model::copy_region for copying the active
child region for a union. */
void
region_model::copy_union_region (region_id dst_rid,
union_region *src_reg,
region_model_context *ctxt)
{
region_id src_active_view_rid = src_reg->get_active_view ();
if (src_active_view_rid.null_p ())
return;
region *src_active_view = get_region (src_active_view_rid);
tree type = src_active_view->get_type ();
region_id dst_active_view_rid = get_or_create_view (dst_rid, type, ctxt);
copy_region (dst_active_view_rid, src_active_view_rid, ctxt);
}
/* Subroutine of region_model::copy_region for copying the child
regions for an array. */
void
region_model::copy_array_region (region_id dst_rid,
array_region *dst_reg,
array_region *src_reg,
region_model_context *ctxt)
{
for (array_region::iterator_t iter = src_reg->begin ();
iter != src_reg->end (); ++iter)
{
array_region::key_t src_key = (*iter).first;
region_id src_field_rid = (*iter).second;
region *src_field_reg = get_region (src_field_rid);
region_id dst_field_rid
= dst_reg->get_or_create (this, dst_rid, src_key,
src_field_reg->get_type (), ctxt);
copy_region (dst_field_rid, src_field_rid, ctxt);
}
}
/* Generate a hash value for this region. The work is done by the
add_to_hash vfunc. */
hashval_t
region::hash () const
{
inchash::hash hstate;
add_to_hash (hstate);
return hstate.end ();
}
/* Print a one-liner representation of this region to PP, assuming
that this region is within MODEL and its id is THIS_RID. */
void
region::print (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
this_rid.print (pp);
pp_string (pp, ": {");
/* vfunc. */
print_fields (model, this_rid, pp);
pp_string (pp, "}");
}
/* Base class implementation of region::dump_dot_to_pp vfunc. */
void
region::dump_dot_to_pp (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
this_rid.dump_node_name_to_pp (pp);
pp_printf (pp, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"",
"lightgrey");
pp_write_text_to_stream (pp);
print (model, this_rid, pp);
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
pp_string (pp, "\"];");
pp_newline (pp);
/* Add edge to svalue. */
if (!m_sval_id.null_p ())
{
this_rid.dump_node_name_to_pp (pp);
pp_string (pp, " -> ");
m_sval_id.dump_node_name_to_pp (pp);
pp_string (pp, ";");
pp_newline (pp);
}
/* Add edge to parent. */
if (!m_parent_rid.null_p ())
{
this_rid.dump_node_name_to_pp (pp);
pp_string (pp, " -> ");
m_parent_rid.dump_node_name_to_pp (pp);
pp_string (pp, ";");
pp_newline (pp);
}
}
/* Dump a tree-like ASCII-art representation of this region to PP. */
void
region::dump_to_pp (const region_model &model,
region_id this_rid,
pretty_printer *pp,
const char *prefix,
bool is_last_child) const
{
print (model, this_rid, pp);
pp_newline (pp);
const char *new_prefix;
if (!m_parent_rid.null_p ())
new_prefix = ACONCAT ((prefix, is_last_child ? " " : "| ", NULL));
else
new_prefix = prefix;
const char *begin_color = colorize_start (pp_show_color (pp), "note");
const char *end_color = colorize_stop (pp_show_color (pp));
char *field_prefix
= ACONCAT ((begin_color, new_prefix, "|:", end_color, NULL));
if (!m_sval_id.null_p ())
{
pp_printf (pp, "%s sval: ", field_prefix);
model.get_svalue (m_sval_id)->print (model, m_sval_id, pp);
pp_newline (pp);
}
if (m_type)
{
pp_printf (pp, "%s type: ", field_prefix);
print_quoted_type (pp, m_type);
pp_newline (pp);
}
/* Find the children. */
auto_vec<region_id> child_rids;
unsigned i;
for (unsigned i = 0; i < model.get_num_regions (); ++i)
{
region_id rid = region_id::from_int (i);
region *child = model.get_region (rid);
if (child->m_parent_rid == this_rid)
child_rids.safe_push (rid);
}
/* Print the children, using dump_child_label to label them. */
region_id *child_rid;
FOR_EACH_VEC_ELT (child_rids, i, child_rid)
{
is_last_child = (i == child_rids.length () - 1);
if (!this_rid.null_p ())
{
const char *tail = is_last_child ? "`-" : "|-";
pp_printf (pp, "%r%s%s%R", "note", new_prefix, tail);
}
dump_child_label (model, this_rid, *child_rid, pp);
model.get_region (*child_rid)->dump_to_pp (model, *child_rid, pp,
new_prefix,
is_last_child);
}
}
/* Base implementation of region::dump_child_label vfunc. */
void
region::dump_child_label (const region_model &model,
region_id this_rid ATTRIBUTE_UNUSED,
region_id child_rid,
pretty_printer *pp) const
{
region *child = model.get_region (child_rid);
if (child->m_is_view)
{
gcc_assert (TYPE_P (child->get_type ()));
if (m_active_view_rid == child_rid)
pp_string (pp, "active ");
else
pp_string (pp, "inactive ");
pp_string (pp, "view as ");
print_quoted_type (pp, child->get_type ());
pp_string (pp, ": ");
}
}
/* Base implementation of region::validate vfunc.
Assert that the fields of "region" are valid; subclasses should
chain up their implementation to this one. */
void
region::validate (const region_model &model) const
{
m_parent_rid.validate (model);
m_sval_id.validate (model);
unsigned i;
region_id *view_rid;
FOR_EACH_VEC_ELT (m_view_rids, i, view_rid)
{
gcc_assert (!view_rid->null_p ());
view_rid->validate (model);
}
m_active_view_rid.validate (model);
}
/* Apply MAP to svalue_ids to this region. This updates the value
for the region (if any). */
void
region::remap_svalue_ids (const svalue_id_map &map)
{
map.update (&m_sval_id);
}
/* Base implementation of region::remap_region_ids vfunc; subclasses should
chain up to this, updating any region_id data. */
void
region::remap_region_ids (const region_id_map &map)
{
map.update (&m_parent_rid);
unsigned i;
region_id *view_rid;
FOR_EACH_VEC_ELT (m_view_rids, i, view_rid)
map.update (view_rid);
map.update (&m_active_view_rid);
}
/* Add a new region with id VIEW_RID as a view of this region. */
void
region::add_view (region_id view_rid, region_model *model)
{
gcc_assert (!view_rid.null_p ());
region *new_view = model->get_region (view_rid);
new_view->m_is_view = true;
gcc_assert (!new_view->m_parent_rid.null_p ());
gcc_assert (new_view->m_sval_id.null_p ());
//gcc_assert (new_view->get_type () != NULL_TREE);
// TODO: this can sometimes be NULL, when viewing through a (void *)
// TODO: the type ought to not be present yet
m_view_rids.safe_push (view_rid);
}
/* Look for a view of type TYPE of this region, returning its id if found,
or null otherwise. */
region_id
region::get_view (tree type, region_model *model) const
{
unsigned i;
region_id *view_rid;
FOR_EACH_VEC_ELT (m_view_rids, i, view_rid)
{
region *view = model->get_region (*view_rid);
gcc_assert (view->m_is_view);
if (view->get_type () == type)
return *view_rid;
}
return region_id::null ();
}
/* region's ctor. */
region::region (region_id parent_rid, svalue_id sval_id, tree type)
: m_parent_rid (parent_rid), m_sval_id (sval_id), m_type (type),
m_view_rids (), m_is_view (false), m_active_view_rid (region_id::null ())
{
gcc_assert (type == NULL_TREE || TYPE_P (type));
}
/* region's copy ctor. */
region::region (const region &other)
: m_parent_rid (other.m_parent_rid), m_sval_id (other.m_sval_id),
m_type (other.m_type), m_view_rids (other.m_view_rids.length ()),
m_is_view (other.m_is_view), m_active_view_rid (other.m_active_view_rid)
{
int i;
region_id *rid;
FOR_EACH_VEC_ELT (other.m_view_rids, i, rid)
m_view_rids.quick_push (*rid);
}
/* Base implementation of region::add_to_hash vfunc; subclasses should
chain up to this. */
void
region::add_to_hash (inchash::hash &hstate) const
{
inchash::add (m_parent_rid, hstate);
inchash::add (m_sval_id, hstate);
hstate.add_ptr (m_type);
// TODO: views
}
/* Base implementation of region::print_fields vfunc. */
void
region::print_fields (const region_model &model ATTRIBUTE_UNUSED,
region_id this_rid ATTRIBUTE_UNUSED,
pretty_printer *pp) const
{
pp_printf (pp, "kind: %qs", region_kind_to_str (get_kind ()));
pp_string (pp, ", parent: ");
m_parent_rid.print (pp);
pp_printf (pp, ", sval: ");
m_sval_id.print (pp);
if (m_type)
{
pp_printf (pp, ", type: ");
print_quoted_type (pp, m_type);
}
}
/* Determine if a pointer to this region must be non-NULL.
Generally, pointers to regions must be non-NULL, but pointers
to symbolic_regions might, in fact, be NULL.
This allows us to simulate functions like malloc and calloc with:
- only one "outcome" from each statement,
- the idea that the pointer is on the heap if non-NULL
- the possibility that the pointer could be NULL
- the idea that successive values returned from malloc are non-equal
- to be able to zero-fill for calloc. */
bool
region::non_null_p (const region_model &model) const
{
/* Look through views to get at the underlying region. */
if (is_view_p ())
return model.get_region (m_parent_rid)->non_null_p (model);
/* Are we within a symbolic_region? If so, it could be NULL. */
if (const symbolic_region *sym_reg = dyn_cast_symbolic_region ())
{
if (sym_reg->m_possibly_null)
return false;
}
return true;
}
/* class primitive_region : public region. */
/* Implementation of region::clone vfunc for primitive_region. */
region *
primitive_region::clone () const
{
return new primitive_region (*this);
}
/* Implementation of region::walk_for_canonicalization vfunc for
primitive_region. */
void
primitive_region::walk_for_canonicalization (canonicalization *) const
{
/* Empty. */
}
/* class map_region : public region. */
/* map_region's copy ctor. */
map_region::map_region (const map_region &other)
: region (other),
m_map (other.m_map)
{
}
/* Compare the fields of this map_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
map_region::compare_fields (const map_region &other) const
{
if (m_map.elements () != other.m_map.elements ())
return false;
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
tree key = (*iter).first;
region_id e = (*iter).second;
region_id *other_slot = const_cast <map_t &> (other.m_map).get (key);
if (other_slot == NULL)
return false;
if (e != *other_slot)
return false;
}
return true;
}
/* Implementation of region::print_fields vfunc for map_region. */
void
map_region::print_fields (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
region::print_fields (model, this_rid, pp);
pp_string (pp, ", map: {");
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
if (iter != m_map.begin ())
pp_string (pp, ", ");
tree expr = (*iter).first;
region_id child_rid = (*iter).second;
dump_quoted_tree (pp, expr);
pp_string (pp, ": ");
child_rid.print (pp);
}
pp_string (pp, "}");
}
/* Implementation of region::validate vfunc for map_region. */
void
map_region::validate (const region_model &model) const
{
region::validate (model);
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
region_id child_rid = (*iter).second;
child_rid.validate (model);
}
}
/* Implementation of region::dump_dot_to_pp vfunc for map_region. */
void
map_region::dump_dot_to_pp (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
region::dump_dot_to_pp (model, this_rid, pp);
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
// TODO: add nodes/edges to label things
tree expr = (*iter).first;
region_id child_rid = (*iter).second;
pp_printf (pp, "rid_label_%i [label=\"", child_rid.as_int ());
pp_write_text_to_stream (pp);
pp_printf (pp, "%qE", expr);
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
pp_string (pp, "\"];");
pp_newline (pp);
pp_printf (pp, "rid_label_%i", child_rid.as_int ());
pp_string (pp, " -> ");
child_rid.dump_node_name_to_pp (pp);
pp_string (pp, ";");
pp_newline (pp);
}
}
/* Implementation of region::dump_child_label vfunc for map_region. */
void
map_region::dump_child_label (const region_model &model,
region_id this_rid,
region_id child_rid,
pretty_printer *pp) const
{
region::dump_child_label (model, this_rid, child_rid, pp);
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
if (child_rid == (*iter).second)
{
tree key = (*iter).first;
dump_quoted_tree (pp, key);
pp_string (pp, ": ");
}
}
}
/* Look for a child region for KEY within this map_region.
If it doesn't already exist, create a child map_region, using TYPE for
its type.
Return the region_id of the child (whether pre-existing, or
newly-created).
Notify CTXT if we don't know how to handle TYPE. */
region_id
map_region::get_or_create (region_model *model,
region_id this_rid,
tree key,
tree type,
region_model_context *ctxt)
{
gcc_assert (key);
gcc_assert (valid_key_p (key));
region_id *slot = m_map.get (key);
if (slot)
return *slot;
region_id child_rid = model->add_region_for_type (this_rid, type, ctxt);
m_map.put (key, child_rid);
return child_rid;
}
/* Get the region_id for the child region for KEY within this
MAP_REGION, or NULL if there is no such child region. */
region_id *
map_region::get (tree key)
{
gcc_assert (key);
gcc_assert (valid_key_p (key));
region_id *slot = m_map.get (key);
return slot;
}
/* Implementation of region::add_to_hash vfunc for map_region. */
void
map_region::add_to_hash (inchash::hash &hstate) const
{
region::add_to_hash (hstate);
// TODO
}
/* Implementation of region::remap_region_ids vfunc for map_region. */
void
map_region::remap_region_ids (const region_id_map &map)
{
region::remap_region_ids (map);
/* Remap the region ids within the map entries. */
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end (); ++iter)
map.update (&(*iter).second);
}
/* Remove the binding of KEY to its child region (but not the
child region itself).
For use when purging unneeded SSA names. */
void
map_region::unbind (tree key)
{
gcc_assert (key);
gcc_assert (valid_key_p (key));
m_map.remove (key);
}
/* Look for a child region with id CHILD_RID within this map_region.
If one is found, return its tree key, otherwise return NULL_TREE. */
tree
map_region::get_tree_for_child_region (region_id child_rid) const
{
// TODO: do we want to store an inverse map?
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
tree key = (*iter).first;
region_id r = (*iter).second;
if (r == child_rid)
return key;
}
return NULL_TREE;
}
/* Look for a child region CHILD within this map_region.
If one is found, return its tree key, otherwise return NULL_TREE. */
tree
map_region::get_tree_for_child_region (region *child,
const region_model &model) const
{
// TODO: do we want to store an inverse map?
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
tree key = (*iter).first;
region_id r = (*iter).second;
if (model.get_region (r) == child)
return key;
}
return NULL_TREE;
}
/* Comparator for trees to impose a deterministic ordering on
T1 and T2. */
static int
tree_cmp (const_tree t1, const_tree t2)
{
gcc_assert (t1);
gcc_assert (t2);
/* Test tree codes first. */
if (TREE_CODE (t1) != TREE_CODE (t2))
return TREE_CODE (t1) - TREE_CODE (t2);
/* From this point on, we know T1 and T2 have the same tree code. */
if (DECL_P (t1))
{
if (DECL_NAME (t1) && DECL_NAME (t2))
return strcmp (IDENTIFIER_POINTER (DECL_NAME (t1)),
IDENTIFIER_POINTER (DECL_NAME (t2)));
else
{
if (DECL_NAME (t1))
return -1;
else if (DECL_NAME (t2))
return 1;
else
return DECL_UID (t1) - DECL_UID (t2);
}
}
switch (TREE_CODE (t1))
{
case SSA_NAME:
{
if (SSA_NAME_VAR (t1) && SSA_NAME_VAR (t2))
{
int var_cmp = tree_cmp (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
if (var_cmp)
return var_cmp;
return SSA_NAME_VERSION (t1) - SSA_NAME_VERSION (t2);
}
else
{
if (SSA_NAME_VAR (t1))
return -1;
else if (SSA_NAME_VAR (t2))
return 1;
else
return SSA_NAME_VERSION (t1) - SSA_NAME_VERSION (t2);
}
}
break;
case INTEGER_CST:
return tree_int_cst_compare (t1, t2);
case REAL_CST:
{
const real_value *rv1 = TREE_REAL_CST_PTR (t1);
const real_value *rv2 = TREE_REAL_CST_PTR (t2);
if (real_compare (UNORDERED_EXPR, rv1, rv2))
{
/* Impose an arbitrary order on NaNs relative to other NaNs
and to non-NaNs. */
if (int cmp_isnan = real_isnan (rv1) - real_isnan (rv2))
return cmp_isnan;
if (int cmp_issignaling_nan
= real_issignaling_nan (rv1) - real_issignaling_nan (rv2))
return cmp_issignaling_nan;
return real_isneg (rv1) - real_isneg (rv2);
}
if (real_compare (LT_EXPR, rv1, rv2))
return -1;
if (real_compare (GT_EXPR, rv1, rv2))
return 1;
return 0;
}
case STRING_CST:
return strcmp (TREE_STRING_POINTER (t1),
TREE_STRING_POINTER (t2));
default:
gcc_unreachable ();
break;
}
gcc_unreachable ();
return 0;
}
/* qsort comparator for trees to impose a deterministic ordering on
P1 and P2. */
static int
tree_cmp (const void *p1, const void *p2)
{
const_tree t1 = *(const_tree const *)p1;
const_tree t2 = *(const_tree const *)p2;
return tree_cmp (t1, t2);
}
/* Attempt to merge MAP_REGION_A and MAP_REGION_B into MERGED_MAP_REGION,
which has region_id MERGED_RID, using MERGER.
Return true if the merger is possible, false otherwise. */
bool
map_region::can_merge_p (const map_region *map_region_a,
const map_region *map_region_b,
map_region *merged_map_region,
region_id merged_rid,
model_merger *merger)
{
for (map_t::iterator iter = map_region_a->m_map.begin ();
iter != map_region_a->m_map.end ();
++iter)
{
tree key_a = (*iter).first;
region_id rid_a = (*iter).second;
if (const region_id *slot_b
= const_cast<map_region *>(map_region_b)->m_map.get (key_a))
{
region_id rid_b = *slot_b;
region *child_region_a = merger->get_region_a <region> (rid_a);
region *child_region_b = merger->get_region_b <region> (rid_b);
gcc_assert (child_region_a->get_type ()
== child_region_b->get_type ());
gcc_assert (child_region_a->get_kind ()
== child_region_b->get_kind ());
region_id child_merged_rid
= merged_map_region->get_or_create (merger->m_merged_model,
merged_rid,
key_a,
child_region_a->get_type (),
NULL);
region *child_merged_region
= merger->m_merged_model->get_region (child_merged_rid);
/* Consider values. */
svalue_id child_a_sid = child_region_a->get_value_direct ();
svalue_id child_b_sid = child_region_b->get_value_direct ();
svalue_id child_merged_sid;
if (!merger->can_merge_values_p (child_a_sid, child_b_sid,
&child_merged_sid))
return false;
if (!child_merged_sid.null_p ())
child_merged_region->set_value (*merger->m_merged_model,
child_merged_rid,
child_merged_sid,
NULL);
if (map_region *map_region_a = child_region_a->dyn_cast_map_region ())
{
/* Recurse. */
if (!can_merge_p (map_region_a,
as_a <map_region *> (child_region_b),
as_a <map_region *> (child_merged_region),
child_merged_rid,
merger))
return false;
}
}
else
{
/* TODO: region is present in A, but absent in B. */
}
}
/* TODO: check for keys in B that aren't in A. */
return true;
}
/* Implementation of region::walk_for_canonicalization vfunc for
map_region. */
void
map_region::walk_for_canonicalization (canonicalization *c) const
{
auto_vec<tree> keys (m_map.elements ());
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
tree key_a = (*iter).first;
keys.quick_push (key_a);
}
keys.qsort (tree_cmp);
unsigned i;
tree key;
FOR_EACH_VEC_ELT (keys, i, key)
{
region_id rid = *const_cast<map_region *>(this)->m_map.get (key);
c->walk_rid (rid);
}
}
/* For debugging purposes: look for a child region for a decl named
IDENTIFIER (or an SSA_NAME for such a decl), returning its value,
or svalue_id::null if none are found. */
svalue_id
map_region::get_value_by_name (tree identifier,
const region_model &model) const
{
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
tree key = (*iter).first;
if (TREE_CODE (key) == SSA_NAME)
if (SSA_NAME_VAR (key))
key = SSA_NAME_VAR (key);
if (DECL_P (key))
if (DECL_NAME (key) == identifier)
{
region_id rid = (*iter).second;
region *region = model.get_region (rid);
return region->get_value (const_cast<region_model &>(model),
false, NULL);
}
}
return svalue_id::null ();
}
/* class struct_or_union_region : public map_region. */
/* Implementation of map_region::valid_key_p vfunc for
struct_or_union_region. */
bool
struct_or_union_region::valid_key_p (tree key) const
{
return TREE_CODE (key) == FIELD_DECL;
}
/* Compare the fields of this struct_or_union_region with OTHER, returning
true if they are equal.
For use by region::operator==. */
bool
struct_or_union_region::compare_fields (const struct_or_union_region &other)
const
{
return map_region::compare_fields (other);
}
/* class struct_region : public struct_or_union_region. */
/* Implementation of region::clone vfunc for struct_region. */
region *
struct_region::clone () const
{
return new struct_region (*this);
}
/* Compare the fields of this struct_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
struct_region::compare_fields (const struct_region &other) const
{
return struct_or_union_region::compare_fields (other);
}
/* class union_region : public struct_or_union_region. */
/* Implementation of region::clone vfunc for union_region. */
region *
union_region::clone () const
{
return new union_region (*this);
}
/* Compare the fields of this union_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
union_region::compare_fields (const union_region &other) const
{
return struct_or_union_region::compare_fields (other);
}
/* class frame_region : public map_region. */
/* Compare the fields of this frame_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
frame_region::compare_fields (const frame_region &other) const
{
if (!map_region::compare_fields (other))
return false;
if (m_fun != other.m_fun)
return false;
if (m_depth != other.m_depth)
return false;
return true;
}
/* Implementation of region::clone vfunc for frame_region. */
region *
frame_region::clone () const
{
return new frame_region (*this);
}
/* Implementation of map_region::valid_key_p vfunc for frame_region. */
bool
frame_region::valid_key_p (tree key) const
{
// TODO: could also check that VAR_DECLs are locals
return (TREE_CODE (key) == PARM_DECL
|| TREE_CODE (key) == VAR_DECL
|| TREE_CODE (key) == SSA_NAME
|| TREE_CODE (key) == RESULT_DECL);
}
/* Implementation of region::print_fields vfunc for frame_region. */
void
frame_region::print_fields (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
map_region::print_fields (model, this_rid, pp);
pp_printf (pp, ", function: %qs, depth: %i", function_name (m_fun), m_depth);
}
/* Implementation of region::add_to_hash vfunc for frame_region. */
void
frame_region::add_to_hash (inchash::hash &hstate) const
{
map_region::add_to_hash (hstate);
hstate.add_ptr (m_fun);
hstate.add_int (m_depth);
}
/* class globals_region : public scope_region. */
/* Compare the fields of this globals_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
globals_region::compare_fields (const globals_region &other) const
{
return map_region::compare_fields (other);
}
/* Implementation of region::clone vfunc for globals_region. */
region *
globals_region::clone () const
{
return new globals_region (*this);
}
/* Implementation of map_region::valid_key_p vfunc for globals_region. */
bool
globals_region::valid_key_p (tree key) const
{
return TREE_CODE (key) == VAR_DECL;
}
/* class code_region : public map_region. */
/* Compare the fields of this code_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
code_region::compare_fields (const code_region &other) const
{
return map_region::compare_fields (other);
}
/* Implementation of region::clone vfunc for code_region. */
region *
code_region::clone () const
{
return new code_region (*this);
}
/* Implementation of map_region::valid_key_p vfunc for code_region. */
bool
code_region::valid_key_p (tree key) const
{
return TREE_CODE (key) == FUNCTION_DECL;
}
/* class array_region : public region. */
/* array_region's copy ctor. */
array_region::array_region (const array_region &other)
: region (other),
m_map (other.m_map)
{
}
/* Get a child region for the element with index INDEX_SID. */
region_id
array_region::get_element (region_model *model,
region_id this_rid,
svalue_id index_sid,
region_model_context *ctxt)
{
tree element_type = TREE_TYPE (get_type ());
svalue *index_sval = model->get_svalue (index_sid);
if (tree cst_index = index_sval->maybe_get_constant ())
{
key_t key = key_from_constant (cst_index);
region_id element_rid
= get_or_create (model, this_rid, key, element_type, ctxt);
return element_rid;
}
return model->get_or_create_view (this_rid, element_type, ctxt);
}
/* Implementation of region::clone vfunc for array_region. */
region *
array_region::clone () const
{
return new array_region (*this);
}
/* Compare the fields of this array_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
array_region::compare_fields (const array_region &other) const
{
if (m_map.elements () != other.m_map.elements ())
return false;
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
int key = (*iter).first;
region_id e = (*iter).second;
region_id *other_slot = const_cast <map_t &> (other.m_map).get (key);
if (other_slot == NULL)
return false;
if (e != *other_slot)
return false;
}
return true;
}
/* Implementation of region::print_fields vfunc for array_region. */
void
array_region::print_fields (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
region::print_fields (model, this_rid, pp);
pp_string (pp, ", array: {");
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
if (iter != m_map.begin ())
pp_string (pp, ", ");
int key = (*iter).first;
region_id child_rid = (*iter).second;
pp_printf (pp, "[%i]: ", key);
child_rid.print (pp);
}
pp_string (pp, "}");
}
/* Implementation of region::validate vfunc for array_region. */
void
array_region::validate (const region_model &model) const
{
region::validate (model);
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
region_id child_rid = (*iter).second;
child_rid.validate (model);
}
}
/* Implementation of region::dump_dot_to_pp vfunc for array_region. */
void
array_region::dump_dot_to_pp (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
region::dump_dot_to_pp (model, this_rid, pp);
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
// TODO: add nodes/edges to label things
int key = (*iter).first;
region_id child_rid = (*iter).second;
pp_printf (pp, "rid_label_%i [label=\"", child_rid.as_int ());
pp_write_text_to_stream (pp);
pp_printf (pp, "%qi", key);
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
pp_string (pp, "\"];");
pp_newline (pp);
pp_printf (pp, "rid_label_%i", child_rid.as_int ());
pp_string (pp, " -> ");
child_rid.dump_node_name_to_pp (pp);
pp_string (pp, ";");
pp_newline (pp);
}
}
/* Implementation of region::dump_child_label vfunc for array_region. */
void
array_region::dump_child_label (const region_model &model,
region_id this_rid,
region_id child_rid,
pretty_printer *pp) const
{
region::dump_child_label (model, this_rid, child_rid, pp);
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
if (child_rid == (*iter).second)
{
int key = (*iter).first;
pp_printf (pp, "[%i]: ", key);
}
}
}
/* Look for a child region for KEY within this array_region.
If it doesn't already exist, create a child array_region, using TYPE for
its type.
Return the region_id of the child (whether pre-existing, or
newly-created).
Notify CTXT if we don't know how to handle TYPE. */
region_id
array_region::get_or_create (region_model *model,
region_id this_rid,
key_t key,
tree type,
region_model_context *ctxt)
{
region_id *slot = m_map.get (key);
if (slot)
return *slot;
region_id child_rid = model->add_region_for_type (this_rid, type, ctxt);
m_map.put (key, child_rid);
return child_rid;
}
/* Get the region_id for the child region for KEY within this
ARRAY_REGION, or NULL if there is no such child region. */
region_id *
array_region::get (key_t key)
{
region_id *slot = m_map.get (key);
return slot;
}
/* Implementation of region::add_to_hash vfunc for array_region. */
void
array_region::add_to_hash (inchash::hash &hstate) const
{
region::add_to_hash (hstate);
// TODO
}
/* Implementation of region::remap_region_ids vfunc for array_region. */
void
array_region::remap_region_ids (const region_id_map &map)
{
region::remap_region_ids (map);
/* Remap the region ids within the map entries. */
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end (); ++iter)
map.update (&(*iter).second);
}
/* Look for a child region with id CHILD_RID within this array_region.
If one is found, write its key to *OUT and return true,
otherwise return false. */
bool
array_region::get_key_for_child_region (region_id child_rid, key_t *out) const
{
// TODO: do we want to store an inverse map?
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
key_t key = (*iter).first;
region_id r = (*iter).second;
if (r == child_rid)
{
*out = key;
return true;
}
}
return false;
}
/* qsort comparator for array_region's keys. */
int
array_region::key_cmp (const void *p1, const void *p2)
{
key_t i1 = *(const key_t *)p1;
key_t i2 = *(const key_t *)p2;
if (i1 > i2)
return 1;
else if (i1 < i2)
return -1;
else
return 0;
}
/* Implementation of region::walk_for_canonicalization vfunc for
array_region. */
void
array_region::walk_for_canonicalization (canonicalization *c) const
{
auto_vec<int> keys (m_map.elements ());
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
{
int key_a = (*iter).first;
keys.quick_push (key_a);
}
keys.qsort (key_cmp);
unsigned i;
int key;
FOR_EACH_VEC_ELT (keys, i, key)
{
region_id rid = *const_cast<array_region *>(this)->m_map.get (key);
c->walk_rid (rid);
}
}
/* Convert constant CST into an array_region::key_t. */
array_region::key_t
array_region::key_from_constant (tree cst)
{
gcc_assert (CONSTANT_CLASS_P (cst));
wide_int w = wi::to_wide (cst);
key_t result = w.to_shwi ();
return result;
}
/* Convert array_region::key_t KEY into a tree constant. */
tree
array_region::constant_from_key (key_t key)
{
tree array_type = get_type ();
tree index_type = TYPE_DOMAIN (array_type);
return build_int_cst (index_type, key);
}
/* class function_region : public map_region. */
/* Compare the fields of this function_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
function_region::compare_fields (const function_region &other) const
{
return map_region::compare_fields (other);
}
/* Implementation of region::clone vfunc for function_region. */
region *
function_region::clone () const
{
return new function_region (*this);
}
/* Implementation of map_region::valid_key_p vfunc for function_region. */
bool
function_region::valid_key_p (tree key) const
{
return TREE_CODE (key) == LABEL_DECL;
}
/* class stack_region : public region. */
/* stack_region's copy ctor. */
stack_region::stack_region (const stack_region &other)
: region (other),
m_frame_rids (other.m_frame_rids.length ())
{
int i;
region_id *frame_rid;
FOR_EACH_VEC_ELT (other.m_frame_rids, i, frame_rid)
m_frame_rids.quick_push (*frame_rid);
}
/* Compare the fields of this stack_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
stack_region::compare_fields (const stack_region &other) const
{
if (m_frame_rids.length () != other.m_frame_rids.length ())
return false;
int i;
region_id *frame_rid;
FOR_EACH_VEC_ELT (m_frame_rids, i, frame_rid)
if (m_frame_rids[i] != other.m_frame_rids[i])
return false;
return true;
}
/* Implementation of region::clone vfunc for stack_region. */
region *
stack_region::clone () const
{
return new stack_region (*this);
}
/* Implementation of region::print_fields vfunc for stack_region. */
void
stack_region::print_fields (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
region::print_fields (model, this_rid, pp);
// TODO
}
/* Implementation of region::dump_child_label vfunc for stack_region. */
void
stack_region::dump_child_label (const region_model &model,
region_id this_rid ATTRIBUTE_UNUSED,
region_id child_rid,
pretty_printer *pp) const
{
function *fun = model.get_region<frame_region> (child_rid)->get_function ();
pp_printf (pp, "frame for %qs: ", function_name (fun));
}
/* Implementation of region::validate vfunc for stack_region. */
void
stack_region::validate (const region_model &model) const
{
region::validate (model);
int i;
region_id *frame_rid;
FOR_EACH_VEC_ELT (m_frame_rids, i, frame_rid)
m_frame_rids[i].validate (model);
}
/* Push FRAME_RID (for a frame_region) onto this stack. */
void
stack_region::push_frame (region_id frame_rid)
{
m_frame_rids.safe_push (frame_rid);
}
/* Get the region_id of the top-most frame in this stack, if any. */
region_id
stack_region::get_current_frame_id () const
{
if (m_frame_rids.length () > 0)
return m_frame_rids[m_frame_rids.length () - 1];
else
return region_id::null ();
}
/* Pop the topmost frame_region from this stack.
If RESULT_DST_RID is non-null, copy any return value from the frame
into RESULT_DST_RID's region.
Purge the frame region and all its descendent regions.
Convert any pointers that point into such regions into
POISON_KIND_POPPED_STACK svalues.
If PURGE, then purge all unused svalues, with the exception of any
returned values.
Accumulate stats on purged entities into STATS. */
void
stack_region::pop_frame (region_model *model, region_id result_dst_rid,
bool purge, purge_stats *stats,
region_model_context *ctxt)
{
gcc_assert (m_frame_rids.length () > 0);
region_id frame_rid = get_current_frame_id ();
frame_region *frame = model->get_region<frame_region> (frame_rid);
/* Evaluate the result, within the callee frame. */
svalue_id_set returned_sids;
tree fndecl = frame->get_function ()->decl;
tree result = DECL_RESULT (fndecl);
if (result && TREE_TYPE (result) != void_type_node)
{
if (!result_dst_rid.null_p ())
{
/* Copy the result to RESULT_DST_RID. */
model->copy_region (result_dst_rid, model->get_lvalue (result, ctxt),
ctxt);
}
if (purge)
{
/* Populate returned_sids, to avoid purging them. */
region_id return_rid = model->get_lvalue (result, NULL);
region_id_set returned_rids (model);
model->get_descendents (return_rid, &returned_rids,
region_id::null ());
for (unsigned i = 0; i < model->get_num_regions (); i++)
{
region_id rid = region_id::from_int (i);
if (returned_rids.region_p (rid))
{
svalue_id sid = model->get_region (rid)->get_value_direct ();
returned_sids.add_svalue (sid);
}
}
}
}
/* Pop the frame RID. */
m_frame_rids.pop ();
model->delete_region_and_descendents (frame_rid,
POISON_KIND_POPPED_STACK,
stats,
ctxt ? ctxt->get_logger () : NULL);
/* Delete unused svalues, but don't delete the return value(s). */
if (purge)
model->purge_unused_svalues (stats, ctxt, &returned_sids);
model->validate ();
}
/* Implementation of region::add_to_hash vfunc for stack_region. */
void
stack_region::add_to_hash (inchash::hash &hstate) const
{
region::add_to_hash (hstate);
int i;
region_id *frame_rid;
FOR_EACH_VEC_ELT (m_frame_rids, i, frame_rid)
inchash::add (*frame_rid, hstate);
}
/* Implementation of region::remap_region_ids vfunc for stack_region. */
void
stack_region::remap_region_ids (const region_id_map &map)
{
region::remap_region_ids (map);
int i;
region_id *frame_rid;
FOR_EACH_VEC_ELT (m_frame_rids, i, frame_rid)
map.update (&m_frame_rids[i]);
}
/* Attempt to merge STACK_REGION_A and STACK_REGION_B using MERGER.
Return true if the merger is possible, false otherwise. */
bool
stack_region::can_merge_p (const stack_region *stack_region_a,
const stack_region *stack_region_b,
model_merger *merger)
{
if (stack_region_a->get_num_frames ()
!= stack_region_b->get_num_frames ())
return false;
region_model *merged_model = merger->m_merged_model;
region_id rid_merged_stack
= merged_model->get_root_region ()->ensure_stack_region (merged_model);
stack_region *merged_stack
= merged_model->get_region <stack_region> (rid_merged_stack);
/* First, create all frames in the merged model, without populating them.
The merging code assumes that all frames in the merged model already exist,
so we have to do this first to handle the case in which a local in an
older frame points at a local in a more recent frame. */
for (unsigned i = 0; i < stack_region_a->get_num_frames (); i++)
{
region_id rid_a = stack_region_a->get_frame_rid (i);
frame_region *frame_a = merger->get_region_a <frame_region> (rid_a);
region_id rid_b = stack_region_b->get_frame_rid (i);
frame_region *frame_b = merger->get_region_b <frame_region> (rid_b);
if (frame_a->get_function () != frame_b->get_function ())
return false;
frame_region *merged_frame = new frame_region (rid_merged_stack,
frame_a->get_function (),
frame_a->get_depth ());
region_id rid_merged_frame = merged_model->add_region (merged_frame);
merged_stack->push_frame (rid_merged_frame);
}
/* Now populate the frames we created. */
for (unsigned i = 0; i < stack_region_a->get_num_frames (); i++)
{
region_id rid_a = stack_region_a->get_frame_rid (i);
frame_region *frame_a = merger->get_region_a <frame_region> (rid_a);
region_id rid_b = stack_region_b->get_frame_rid (i);
frame_region *frame_b = merger->get_region_b <frame_region> (rid_b);
region_id rid_merged_frame = merged_stack->get_frame_rid (i);
frame_region *merged_frame
= merged_model->get_region <frame_region> (rid_merged_frame);
if (!map_region::can_merge_p (frame_a, frame_b,
merged_frame, rid_merged_frame,
merger))
return false;
}
return true;
}
/* Implementation of region::walk_for_canonicalization vfunc for
stack_region. */
void
stack_region::walk_for_canonicalization (canonicalization *c) const
{
int i;
region_id *frame_rid;
FOR_EACH_VEC_ELT (m_frame_rids, i, frame_rid)
c->walk_rid (*frame_rid);
}
/* For debugging purposes: look for a grandchild region within one of
the child frame regions, where the grandchild is for a decl named
IDENTIFIER (or an SSA_NAME for such a decl):
stack_region
`-frame_region
`-region for decl named IDENTIFIER
returning its value, or svalue_id::null if none are found. */
svalue_id
stack_region::get_value_by_name (tree identifier,
const region_model &model) const
{
int i;
region_id *frame_rid;
FOR_EACH_VEC_ELT (m_frame_rids, i, frame_rid)
{
frame_region *frame = model.get_region<frame_region> (*frame_rid);
svalue_id sid = frame->get_value_by_name (identifier, model);
if (!sid.null_p ())
return sid;
}
return svalue_id::null ();
}
/* class heap_region : public region. */
/* heap_region's copy ctor. */
heap_region::heap_region (const heap_region &other)
: region (other)
{
}
/* Compare the fields of this heap_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
heap_region::compare_fields (const heap_region &) const
{
/* Empty. */
return true;
}
/* Implementation of region::clone vfunc for heap_region. */
region *
heap_region::clone () const
{
return new heap_region (*this);
}
/* Implementation of region::walk_for_canonicalization vfunc for
heap_region. */
void
heap_region::walk_for_canonicalization (canonicalization *) const
{
/* Empty. */
}
/* class root_region : public region. */
/* root_region's default ctor. */
root_region::root_region ()
: region (region_id::null (),
svalue_id::null (),
NULL_TREE)
{
}
/* root_region's copy ctor. */
root_region::root_region (const root_region &other)
: region (other),
m_stack_rid (other.m_stack_rid),
m_globals_rid (other.m_globals_rid),
m_code_rid (other.m_code_rid),
m_heap_rid (other.m_heap_rid)
{
}
/* Compare the fields of this root_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
root_region::compare_fields (const root_region &other) const
{
if (m_stack_rid != other.m_stack_rid)
return false;
if (m_globals_rid != other.m_globals_rid)
return false;
if (m_code_rid != other.m_code_rid)
return false;
if (m_heap_rid != other.m_heap_rid)
return false;
return true;
}
/* Implementation of region::clone vfunc for root_region. */
region *
root_region::clone () const
{
return new root_region (*this);
}
/* Implementation of region::print_fields vfunc for root_region. */
void
root_region::print_fields (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
region::print_fields (model, this_rid, pp);
// TODO
}
/* Implementation of region::validate vfunc for root_region. */
void
root_region::validate (const region_model &model) const
{
region::validate (model);
m_stack_rid.validate (model);
m_globals_rid.validate (model);
m_code_rid.validate (model);
m_heap_rid.validate (model);
}
/* Implementation of region::dump_child_label vfunc for root_region. */
void
root_region::dump_child_label (const region_model &model ATTRIBUTE_UNUSED,
region_id this_rid ATTRIBUTE_UNUSED,
region_id child_rid,
pretty_printer *pp) const
{
if (child_rid == m_stack_rid)
pp_printf (pp, "stack: ");
else if (child_rid == m_globals_rid)
pp_printf (pp, "globals: ");
else if (child_rid == m_code_rid)
pp_printf (pp, "code: ");
else if (child_rid == m_heap_rid)
pp_printf (pp, "heap: ");
}
/* Create a new frame_region for a call to FUN and push it onto
the stack.
If ARG_SIDS is non-NULL, use it to populate the parameters
in the new frame.
Otherwise, populate them with unknown values.
Return the region_id of the new frame. */
region_id
root_region::push_frame (region_model *model, function *fun,
vec<svalue_id> *arg_sids,
region_model_context *ctxt)
{
gcc_assert (fun);
/* arg_sids can be NULL. */
ensure_stack_region (model);
stack_region *stack = model->get_region <stack_region> (m_stack_rid);
frame_region *region = new frame_region (m_stack_rid, fun,
stack->get_num_frames ());
region_id frame_rid = model->add_region (region);
// TODO: unify these cases by building a vec of unknown?
if (arg_sids)
{
/* Arguments supplied from a caller frame. */
tree fndecl = fun->decl;
unsigned idx = 0;
for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm;
iter_parm = DECL_CHAIN (iter_parm), ++idx)
{
/* If there's a mismatching declaration, the call stmt might
not have enough args. Handle this case by leaving the
rest of the params as uninitialized. */
if (idx >= arg_sids->length ())
break;
svalue_id arg_sid = (*arg_sids)[idx];
region_id parm_rid
= region->get_or_create (model, frame_rid, iter_parm,
TREE_TYPE (iter_parm), ctxt);
model->set_value (parm_rid, arg_sid, ctxt);
/* Also do it for default SSA name (sharing the same unknown
value). */
tree parm_default_ssa = ssa_default_def (fun, iter_parm);
if (parm_default_ssa)
{
region_id defssa_rid
= region->get_or_create (model, frame_rid, parm_default_ssa,
TREE_TYPE (iter_parm), ctxt);
model->set_value (defssa_rid, arg_sid, ctxt);
}
}
}
else
{
/* No known arguments (a top-level call within the analysis). */
/* Params have a defined, unknown value; they should not inherit
from the poisoned uninit value. */
tree fndecl = fun->decl;
for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm;
iter_parm = DECL_CHAIN (iter_parm))
{
region_id parm_rid
= region->get_or_create (model, frame_rid, iter_parm,
TREE_TYPE (iter_parm), ctxt);
svalue_id parm_sid
= model->set_to_new_unknown_value (parm_rid, TREE_TYPE (iter_parm),
ctxt);
/* Also do it for default SSA name (sharing the same unknown
value). */
tree parm_default_ssa = ssa_default_def (fun, iter_parm);
if (parm_default_ssa)
{
region_id defssa_rid
= region->get_or_create (model, frame_rid, parm_default_ssa,
TREE_TYPE (iter_parm), ctxt);
model->get_region (defssa_rid)->set_value (*model, defssa_rid,
parm_sid, ctxt);
}
}
}
stack->push_frame (frame_rid);
return frame_rid;
}
/* Get the region_id of the top-most frame in this root_region's stack,
if any. */
region_id
root_region::get_current_frame_id (const region_model &model) const
{
stack_region *stack = model.get_region <stack_region> (m_stack_rid);
if (stack)
return stack->get_current_frame_id ();
else
return region_id::null ();
}
/* Pop the topmost frame_region from this root_region's stack;
see the comment for stack_region::pop_frame. */
void
root_region::pop_frame (region_model *model, region_id result_dst_rid,
bool purge, purge_stats *out,
region_model_context *ctxt)
{
stack_region *stack = model->get_region <stack_region> (m_stack_rid);
stack->pop_frame (model, result_dst_rid, purge, out, ctxt);
}
/* Return the region_id of the stack region, creating it if doesn't
already exist. */
region_id
root_region::ensure_stack_region (region_model *model)
{
if (m_stack_rid.null_p ())
{
m_stack_rid
= model->add_region (new stack_region (model->get_root_rid (),
svalue_id::null ()));
}
return m_stack_rid;
}
/* Return the stack region (which could be NULL). */
stack_region *
root_region::get_stack_region (const region_model *model) const
{
return model->get_region <stack_region> (m_stack_rid);
}
/* Return the region_id of the globals region, creating it if doesn't
already exist. */
region_id
root_region::ensure_globals_region (region_model *model)
{
if (m_globals_rid.null_p ())
m_globals_rid
= model->add_region (new globals_region (model->get_root_rid ()));
return m_globals_rid;
}
/* Return the code region (which could be NULL). */
code_region *
root_region::get_code_region (const region_model *model) const
{
return model->get_region <code_region> (m_code_rid);
}
/* Return the region_id of the code region, creating it if doesn't
already exist. */
region_id
root_region::ensure_code_region (region_model *model)
{
if (m_code_rid.null_p ())
m_code_rid
= model->add_region (new code_region (model->get_root_rid ()));
return m_code_rid;
}
/* Return the globals region (which could be NULL). */
globals_region *
root_region::get_globals_region (const region_model *model) const
{
return model->get_region <globals_region> (m_globals_rid);
}
/* Return the region_id of the heap region, creating it if doesn't
already exist. */
region_id
root_region::ensure_heap_region (region_model *model)
{
if (m_heap_rid.null_p ())
{
m_heap_rid
= model->add_region (new heap_region (model->get_root_rid (),
svalue_id::null ()));
}
return m_heap_rid;
}
/* Return the heap region (which could be NULL). */
heap_region *
root_region::get_heap_region (const region_model *model) const
{
return model->get_region <heap_region> (m_heap_rid);
}
/* Implementation of region::remap_region_ids vfunc for root_region. */
void
root_region::remap_region_ids (const region_id_map &map)
{
map.update (&m_stack_rid);
map.update (&m_globals_rid);
map.update (&m_code_rid);
map.update (&m_heap_rid);
}
/* Attempt to merge ROOT_REGION_A and ROOT_REGION_B into
MERGED_ROOT_REGION using MERGER.
Return true if the merger is possible, false otherwise. */
bool
root_region::can_merge_p (const root_region *root_region_a,
const root_region *root_region_b,
root_region *merged_root_region,
model_merger *merger)
{
/* We can only merge if the stacks are sufficiently similar. */
stack_region *stack_a = root_region_a->get_stack_region (merger->m_model_a);
stack_region *stack_b = root_region_b->get_stack_region (merger->m_model_b);
if (stack_a && stack_b)
{
/* If the two models both have a stack, attempt to merge them. */
merged_root_region->ensure_stack_region (merger->m_merged_model);
if (!stack_region::can_merge_p (stack_a, stack_b, merger))
return false;
}
else if (stack_a || stack_b)
/* Don't attempt to merge if one model has a stack and the other
doesn't. */
return false;
map_region *globals_a = root_region_a->get_globals_region (merger->m_model_a);
map_region *globals_b = root_region_b->get_globals_region (merger->m_model_b);
if (globals_a && globals_b)
{
/* If both models have globals regions, attempt to merge them. */
region_id merged_globals_rid
= merged_root_region->ensure_globals_region (merger->m_merged_model);
map_region *merged_globals
= merged_root_region->get_globals_region (merger->m_merged_model);
if (!map_region::can_merge_p (globals_a, globals_b,
merged_globals, merged_globals_rid,
merger))
return false;
}
/* otherwise, merge as "no globals". */
map_region *code_a = root_region_a->get_code_region (merger->m_model_a);
map_region *code_b = root_region_b->get_code_region (merger->m_model_b);
if (code_a && code_b)
{
/* If both models have code regions, attempt to merge them. */
region_id merged_code_rid
= merged_root_region->ensure_code_region (merger->m_merged_model);
map_region *merged_code
= merged_root_region->get_code_region (merger->m_merged_model);
if (!map_region::can_merge_p (code_a, code_b,
merged_code, merged_code_rid,
merger))
return false;
}
/* otherwise, merge as "no code". */
heap_region *heap_a = root_region_a->get_heap_region (merger->m_model_a);
heap_region *heap_b = root_region_b->get_heap_region (merger->m_model_b);
if (heap_a && heap_b)
{
/* If both have a heap, create a "merged" heap.
Actually merging the heap contents happens via the region_svalue
instances, as needed, when seeing pairs of region_svalue instances. */
merged_root_region->ensure_heap_region (merger->m_merged_model);
}
/* otherwise, merge as "no heap". */
return true;
}
/* Implementation of region::add_to_hash vfunc for root_region. */
void
root_region::add_to_hash (inchash::hash &hstate) const
{
region::add_to_hash (hstate);
inchash::add (m_stack_rid, hstate);
inchash::add (m_globals_rid, hstate);
inchash::add (m_code_rid, hstate);
inchash::add (m_heap_rid, hstate);
}
/* Implementation of region::walk_for_canonicalization vfunc for
root_region. */
void
root_region::walk_for_canonicalization (canonicalization *c) const
{
c->walk_rid (m_stack_rid);
c->walk_rid (m_globals_rid);
c->walk_rid (m_code_rid);
c->walk_rid (m_heap_rid);
}
/* For debugging purposes: look for a descendant region for a local
or global decl named IDENTIFIER (or an SSA_NAME for such a decl),
returning its value, or svalue_id::null if none are found. */
svalue_id
root_region::get_value_by_name (tree identifier,
const region_model &model) const
{
if (stack_region *stack = get_stack_region (&model))
{
svalue_id sid = stack->get_value_by_name (identifier, model);
if (!sid.null_p ())
return sid;
}
if (map_region *globals = get_globals_region (&model))
{
svalue_id sid = globals->get_value_by_name (identifier, model);
if (!sid.null_p ())
return sid;
}
return svalue_id::null ();
}
/* class symbolic_region : public map_region. */
/* symbolic_region's copy ctor. */
symbolic_region::symbolic_region (const symbolic_region &other)
: region (other),
m_possibly_null (other.m_possibly_null)
{
}
/* Compare the fields of this symbolic_region with OTHER, returning true
if they are equal.
For use by region::operator==. */
bool
symbolic_region::compare_fields (const symbolic_region &other) const
{
return m_possibly_null == other.m_possibly_null;
}
/* Implementation of region::clone vfunc for symbolic_region. */
region *
symbolic_region::clone () const
{
return new symbolic_region (*this);
}
/* Implementation of region::walk_for_canonicalization vfunc for
symbolic_region. */
void
symbolic_region::walk_for_canonicalization (canonicalization *) const
{
/* Empty. */
}
/* Implementation of region::print_fields vfunc for symbolic_region. */
void
symbolic_region::print_fields (const region_model &model,
region_id this_rid,
pretty_printer *pp) const
{
region::print_fields (model, this_rid, pp);
pp_printf (pp, ", possibly_null: %s", m_possibly_null ? "true" : "false");
}
/* class region_model. */
/* region_model's default ctor. */
region_model::region_model ()
{
m_root_rid = add_region (new root_region ());
m_constraints = new impl_constraint_manager (this);
// TODO
}
/* region_model's copy ctor. */
region_model::region_model (const region_model &other)
: m_svalues (other.m_svalues.length ()),
m_regions (other.m_regions.length ()),
m_root_rid (other.m_root_rid)
{
/* Clone the svalues and regions. */
int i;
svalue *svalue;
FOR_EACH_VEC_ELT (other.m_svalues, i, svalue)
m_svalues.quick_push (svalue->clone ());
region *region;
FOR_EACH_VEC_ELT (other.m_regions, i, region)
m_regions.quick_push (region->clone ());
m_constraints = other.m_constraints->clone (this);
}
/* region_model's dtor. */
region_model::~region_model ()
{
delete m_constraints;
}
/* region_model's assignment operator. */
region_model &
region_model::operator= (const region_model &other)
{
unsigned i;
svalue *svalue;
region *region;
/* Delete existing content. */
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
delete svalue;
m_svalues.truncate (0);
FOR_EACH_VEC_ELT (m_regions, i, region)
delete region;
m_regions.truncate (0);
delete m_constraints;
/* Clone the svalues and regions. */
m_svalues.reserve (other.m_svalues.length (), true);
FOR_EACH_VEC_ELT (other.m_svalues, i, svalue)
m_svalues.quick_push (svalue->clone ());
m_regions.reserve (other.m_regions.length (), true);
FOR_EACH_VEC_ELT (other.m_regions, i, region)
m_regions.quick_push (region->clone ());
m_root_rid = other.m_root_rid;
m_constraints = other.m_constraints->clone (this);
return *this;
}
/* Equality operator for region_model.
Amongst other things this directly compares the svalue and region
vectors and so for this to be meaningful both this and OTHER should
have been canonicalized. */
bool
region_model::operator== (const region_model &other) const
{
if (m_root_rid != other.m_root_rid)
return false;
if (m_svalues.length () != other.m_svalues.length ())
return false;
if (m_regions.length () != other.m_regions.length ())
return false;
if (*m_constraints != *other.m_constraints)
return false;
unsigned i;
svalue *svalue;
FOR_EACH_VEC_ELT (other.m_svalues, i, svalue)
if (!(*m_svalues[i] == *other.m_svalues[i]))
return false;
region *region;
FOR_EACH_VEC_ELT (other.m_regions, i, region)
if (!(*m_regions[i] == *other.m_regions[i]))
return false;
gcc_checking_assert (hash () == other.hash ());
return true;
}
/* Generate a hash value for this region_model. */
hashval_t
region_model::hash () const
{
hashval_t result = 0;
int i;
svalue *svalue;
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
result ^= svalue->hash ();
region *region;
FOR_EACH_VEC_ELT (m_regions, i, region)
result ^= region->hash ();
result ^= m_constraints->hash ();
return result;
}
/* Print an all-on-one-line representation of this region_model to PP,
which must support %E for trees. */
void
region_model::print (pretty_printer *pp) const
{
int i;
pp_string (pp, "svalues: [");
svalue *svalue;
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
{
if (i > 0)
pp_string (pp, ", ");
print_svalue (svalue_id::from_int (i), pp);
}
pp_string (pp, "], regions: [");
region *region;
FOR_EACH_VEC_ELT (m_regions, i, region)
{
if (i > 0)
pp_string (pp, ", ");
region->print (*this, region_id::from_int (i), pp);
}
pp_string (pp, "], constraints: ");
m_constraints->print (pp);
}
/* Print the svalue with id SID to PP. */
void
region_model::print_svalue (svalue_id sid, pretty_printer *pp) const
{
get_svalue (sid)->print (*this, sid, pp);
}
/* Dump a .dot representation of this region_model to PP, showing
the values and the hierarchy of regions. */
void
region_model::dump_dot_to_pp (pretty_printer *pp) const
{
graphviz_out gv (pp);
pp_string (pp, "digraph \"");
pp_write_text_to_stream (pp);
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
pp_string (pp, "\" {\n");
gv.indent ();
pp_string (pp, "overlap=false;\n");
pp_string (pp, "compound=true;\n");
int i;
svalue *svalue;
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
svalue->dump_dot_to_pp (*this, svalue_id::from_int (i), pp);
region *region;
FOR_EACH_VEC_ELT (m_regions, i, region)
region->dump_dot_to_pp (*this, region_id::from_int (i), pp);
/* TODO: constraints. */
/* Terminate "digraph" */
gv.outdent ();
pp_string (pp, "}");
pp_newline (pp);
}
/* Dump a .dot representation of this region_model to FP. */
void
region_model::dump_dot_to_file (FILE *fp) const
{
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
pp.buffer->stream = fp;
dump_dot_to_pp (&pp);
pp_flush (&pp);
}
/* Dump a .dot representation of this region_model to PATH. */
void
region_model::dump_dot (const char *path) const
{
FILE *fp = fopen (path, "w");
dump_dot_to_file (fp);
fclose (fp);
}
/* Dump a multiline representation of this model to PP, showing the
region hierarchy, the svalues, and any constraints.
If SUMMARIZE is true, show only the most pertinent information,
in a form that attempts to be less verbose.
Otherwise, show all information. */
void
region_model::dump_to_pp (pretty_printer *pp, bool summarize) const
{
if (summarize)
{
auto_vec<path_var> rep_path_vars;
unsigned i;
region *reg;
FOR_EACH_VEC_ELT (m_regions, i, reg)
{
region_id rid = region_id::from_int (i);
path_var pv = get_representative_path_var (rid);
if (pv.m_tree)
rep_path_vars.safe_push (pv);
}
bool is_first = true;
/* Work with a copy in case the get_lvalue calls change anything
(they shouldn't). */
region_model copy (*this);
copy.dump_summary_of_rep_path_vars (pp, &rep_path_vars, &is_first);
equiv_class *ec;
FOR_EACH_VEC_ELT (m_constraints->m_equiv_classes, i, ec)
{
for (unsigned j = 0; j < ec->m_vars.length (); j++)
{
svalue_id lhs_sid = ec->m_vars[j];
tree lhs_tree = get_representative_tree (lhs_sid);
if (lhs_tree == NULL_TREE)
continue;
for (unsigned k = j + 1; k < ec->m_vars.length (); k++)
{
svalue_id rhs_sid = ec->m_vars[k];
tree rhs_tree = get_representative_tree (rhs_sid);
if (rhs_tree
&& !(CONSTANT_CLASS_P (lhs_tree)
&& CONSTANT_CLASS_P (rhs_tree)))
{
dump_separator (pp, &is_first);
dump_tree (pp, lhs_tree);
pp_string (pp, " == ");
dump_tree (pp, rhs_tree);
}
}
}
}
constraint *c;
FOR_EACH_VEC_ELT (m_constraints->m_constraints, i, c)
{
const equiv_class &lhs = c->m_lhs.get_obj (*m_constraints);
const equiv_class &rhs = c->m_rhs.get_obj (*m_constraints);
svalue_id lhs_sid = lhs.get_representative ();
svalue_id rhs_sid = rhs.get_representative ();
tree lhs_tree = get_representative_tree (lhs_sid);
tree rhs_tree = get_representative_tree (rhs_sid);
if (lhs_tree && rhs_tree
&& !(CONSTANT_CLASS_P (lhs_tree) && CONSTANT_CLASS_P (rhs_tree)))
{
dump_separator (pp, &is_first);
dump_tree (pp, lhs_tree);
pp_printf (pp, " %s ", constraint_op_code (c->m_op));
dump_tree (pp, rhs_tree);
}
}
return;
}
get_region (m_root_rid)->dump_to_pp (*this, m_root_rid, pp, "", true);
pp_string (pp, "svalues:");
pp_newline (pp);
int i;
svalue *svalue;
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
{
pp_string (pp, " ");
svalue_id sid = svalue_id::from_int (i);
print_svalue (sid, pp);
pp_newline (pp);
}
pp_string (pp, "constraint manager:");
pp_newline (pp);
m_constraints->dump_to_pp (pp);
}
/* Dump a multiline representation of this model to FILE. */
void
region_model::dump (FILE *fp, bool summarize) const
{
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
pp_show_color (&pp) = pp_show_color (global_dc->printer);
pp.buffer->stream = fp;
dump_to_pp (&pp, summarize);
pp_flush (&pp);
}
/* Dump a multiline representation of this model to stderr. */
DEBUG_FUNCTION void
region_model::dump (bool summarize) const
{
dump (stderr, summarize);
}
/* Dump RMODEL fully to stderr (i.e. without summarization). */
DEBUG_FUNCTION void
region_model::debug () const
{
dump (false);
}
/* Dump VEC to PP, in the form "{VEC elements}: LABEL". */
static void
dump_vec_of_tree (pretty_printer *pp,
bool *is_first,
const auto_vec<tree> &vec,
const char *label)
{
if (vec.length () == 0)
return;
dump_separator (pp, is_first);
pp_printf (pp, "{");
unsigned i;
tree key;
FOR_EACH_VEC_ELT (vec, i, key)
{
if (i > 0)
pp_string (pp, ", ");
dump_tree (pp, key);
}
pp_printf (pp, "}: %s", label);
}
/* Dump all *REP_PATH_VARS to PP in compact form, updating *IS_FIRST.
Subroutine of region_model::dump_to_pp. */
void
region_model::dump_summary_of_rep_path_vars (pretty_printer *pp,
auto_vec<path_var> *rep_path_vars,
bool *is_first)
{
/* Print pointers, constants, and poisoned values that aren't "uninit";
gather keys for unknown and uninit values. */
unsigned i;
path_var *pv;
auto_vec<tree> unknown_trees;
FOR_EACH_VEC_ELT (*rep_path_vars, i, pv)
{
if (TREE_CODE (pv->m_tree) == STRING_CST)
continue;
tentative_region_model_context ctxt;
region_id child_rid = get_lvalue (*pv, &ctxt);
if (ctxt.had_errors_p ())
continue;
region *child_region = get_region (child_rid);
if (!child_region)
continue;
svalue_id sid = child_region->get_value_direct ();
if (sid.null_p ())
continue;
svalue *sval = get_svalue (sid);
switch (sval->get_kind ())
{
default:
gcc_unreachable ();
case SK_REGION:
{
region_svalue *region_sval = as_a <region_svalue *> (sval);
region_id pointee_rid = region_sval->get_pointee ();
gcc_assert (!pointee_rid.null_p ());
tree pointee = get_representative_path_var (pointee_rid).m_tree;
dump_separator (pp, is_first);
dump_tree (pp, pv->m_tree);
pp_string (pp, ": ");
pp_character (pp, '&');
if (pointee)
dump_tree (pp, pointee);
else
pointee_rid.print (pp);
}
break;
case SK_CONSTANT:
dump_separator (pp, is_first);
dump_tree (pp, pv->m_tree);
pp_string (pp, ": ");
dump_tree (pp, sval->dyn_cast_constant_svalue ()->get_constant ());
break;
case SK_UNKNOWN:
unknown_trees.safe_push (pv->m_tree);
break;
case SK_POISONED:
{
poisoned_svalue *poisoned_sval = as_a <poisoned_svalue *> (sval);
enum poison_kind pkind = poisoned_sval->get_poison_kind ();
dump_separator (pp, is_first);
dump_tree (pp, pv->m_tree);
pp_printf (pp, ": %s", poison_kind_to_str (pkind));
}
break;
case SK_SETJMP:
dump_separator (pp, is_first);
pp_printf (pp, "setjmp: EN: %i",
sval->dyn_cast_setjmp_svalue ()->get_enode_index ());
break;
}
}
/* Print unknown and uninitialized values in consolidated form. */
dump_vec_of_tree (pp, is_first, unknown_trees, "unknown");
}
/* Assert that this object is valid. */
void
region_model::validate () const
{
/* Skip this in a release build. */
#if !CHECKING_P
return;
#endif
m_constraints->validate ();
unsigned i;
region *r;
FOR_EACH_VEC_ELT (m_regions, i, r)
r->validate (*this);
// TODO: anything else?
}
/* Global data for use by svalue_id_cmp_by_constant_svalue. */
static region_model *svalue_id_cmp_by_constant_svalue_model = NULL;
/* Comparator for use by region_model::canonicalize. */
static int
svalue_id_cmp_by_constant_svalue (const void *p1, const void *p2)
{
const svalue_id *sid1 = (const svalue_id *)p1;
const svalue_id *sid2 = (const svalue_id *)p2;
gcc_assert (!sid1->null_p ());
gcc_assert (!sid2->null_p ());
gcc_assert (svalue_id_cmp_by_constant_svalue_model);
const svalue &sval1
= *svalue_id_cmp_by_constant_svalue_model->get_svalue (*sid1);
const svalue &sval2
= *svalue_id_cmp_by_constant_svalue_model->get_svalue (*sid2);
gcc_assert (sval1.get_kind () == SK_CONSTANT);
gcc_assert (sval2.get_kind () == SK_CONSTANT);
tree cst1 = ((const constant_svalue &)sval1).get_constant ();
tree cst2 = ((const constant_svalue &)sval2).get_constant ();
return tree_cmp (cst1, cst2);
}
/* Reorder the regions and svalues into a deterministic "canonical" order,
to maximize the chance of equality.
If non-NULL, notify CTXT about the svalue id remapping. */
void
region_model::canonicalize (region_model_context *ctxt)
{
/* Walk all regions and values in a deterministic order, visiting
rids and sids, generating a rid and sid map. */
canonicalization c (*this);
/* (1): Walk all svalues, putting constants first, sorting the constants
(thus imposing an ordering on any constants that are purely referenced
by constraints).
Ignore other svalues for now. */
{
unsigned i;
auto_vec<svalue_id> sids;
svalue *sval;
FOR_EACH_VEC_ELT (m_svalues, i, sval)
{
if (sval->get_kind () == SK_CONSTANT)
sids.safe_push (svalue_id::from_int (i));
}
svalue_id_cmp_by_constant_svalue_model = this;
sids.qsort (svalue_id_cmp_by_constant_svalue);
svalue_id_cmp_by_constant_svalue_model = NULL;
svalue_id *sid;
FOR_EACH_VEC_ELT (sids, i, sid)
c.walk_sid (*sid);
}
/* (2): Walk all regions (and thus their values) in a deterministic
order. */
c.walk_rid (m_root_rid);
/* (3): Ensure we've visited everything, as we don't want to purge
at this stage. Anything we visit for the first time here has
arbitrary order. */
{
unsigned i;
region *region;
FOR_EACH_VEC_ELT (m_regions, i, region)
c.walk_rid (region_id::from_int (i));
svalue *sval;
FOR_EACH_VEC_ELT (m_svalues, i, sval)
c.walk_sid (svalue_id::from_int (i));
}
/* (4): We now have a reordering of the regions and values.
Apply it. */
remap_svalue_ids (c.m_sid_map);
remap_region_ids (c.m_rid_map);
if (ctxt)
ctxt->remap_svalue_ids (c.m_sid_map);
/* (5): Canonicalize the constraint_manager (it has already had its
svalue_ids remapped above). This makes use of the new svalue_id
values, and so must happen last. */
m_constraints->canonicalize (get_num_svalues ());
validate ();
}
/* Return true if this region_model is in canonical form. */
bool
region_model::canonicalized_p () const
{
region_model copy (*this);
copy.canonicalize (NULL);
return *this == copy;
}
/* A subclass of pending_diagnostic for complaining about uses of
poisoned values. */
class poisoned_value_diagnostic
: public pending_diagnostic_subclass<poisoned_value_diagnostic>
{
public:
poisoned_value_diagnostic (tree expr, enum poison_kind pkind)
: m_expr (expr), m_pkind (pkind)
{}
const char *get_kind () const FINAL OVERRIDE { return "poisoned_value_diagnostic"; }
bool operator== (const poisoned_value_diagnostic &other) const
{
return m_expr == other.m_expr;
}
bool emit (rich_location *rich_loc) FINAL OVERRIDE
{
switch (m_pkind)
{
default:
gcc_unreachable ();
case POISON_KIND_FREED:
{
diagnostic_metadata m;
m.add_cwe (416); /* "CWE-416: Use After Free". */
return warning_meta (rich_loc, m,
OPT_Wanalyzer_use_after_free,
"use after %<free%> of %qE",
m_expr);
}
break;
case POISON_KIND_POPPED_STACK:
{
/* TODO: which CWE? */
return warning_at (rich_loc,
OPT_Wanalyzer_use_of_pointer_in_stale_stack_frame,
"use of pointer %qE within stale stack frame",
m_expr);
}
break;
}
}
label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
{
switch (m_pkind)
{
default:
gcc_unreachable ();
case POISON_KIND_FREED:
return ev.formatted_print ("use after %<free%> of %qE here",
m_expr);
case POISON_KIND_POPPED_STACK:
return ev.formatted_print
("use of pointer %qE within stale stack frame here",
m_expr);
}
}
private:
tree m_expr;
enum poison_kind m_pkind;
};
/* Determine if EXPR is poisoned, and if so, queue a diagnostic to CTXT. */
void
region_model::check_for_poison (tree expr, region_model_context *ctxt)
{
if (!ctxt)
return;
// TODO: this is disabled for now (too many false positives)
return;
svalue_id expr_sid = get_rvalue (expr, ctxt);
gcc_assert (!expr_sid.null_p ());
svalue *expr_svalue = get_svalue (expr_sid);
gcc_assert (expr_svalue);
if (const poisoned_svalue *poisoned_sval
= expr_svalue->dyn_cast_poisoned_svalue ())
{
enum poison_kind pkind = poisoned_sval->get_poison_kind ();
ctxt->warn (new poisoned_value_diagnostic (expr, pkind));
}
}
/* Update this model for the ASSIGN stmt, using CTXT to report any
diagnostics. */
void
region_model::on_assignment (const gassign *assign, region_model_context *ctxt)
{
tree lhs = gimple_assign_lhs (assign);
tree rhs1 = gimple_assign_rhs1 (assign);
region_id lhs_rid = get_lvalue (lhs, ctxt);
/* Check for uses of poisoned values. */
switch (get_gimple_rhs_class (gimple_expr_code (assign)))
{
case GIMPLE_INVALID_RHS:
gcc_unreachable ();
break;
case GIMPLE_TERNARY_RHS:
check_for_poison (gimple_assign_rhs3 (assign), ctxt);
/* Fallthru */
case GIMPLE_BINARY_RHS:
check_for_poison (gimple_assign_rhs2 (assign), ctxt);
/* Fallthru */
case GIMPLE_UNARY_RHS:
case GIMPLE_SINGLE_RHS:
check_for_poison (gimple_assign_rhs1 (assign), ctxt);
}
if (lhs_rid.null_p ())
return;
// TODO: issue a warning for this case
enum tree_code op = gimple_assign_rhs_code (assign);
switch (op)
{
default:
{
if (0)
sorry_at (assign->location, "unhandled assignment op: %qs",
get_tree_code_name (op));
set_to_new_unknown_value (lhs_rid, TREE_TYPE (lhs), ctxt);
}
break;
case BIT_FIELD_REF:
{
// TODO
}
break;
case CONSTRUCTOR:
{
/* e.g. "x ={v} {CLOBBER};" */
// TODO
}
break;
case POINTER_PLUS_EXPR:
{
/* e.g. "_1 = a_10(D) + 12;" */
tree ptr = rhs1;
tree offset = gimple_assign_rhs2 (assign);
svalue_id ptr_sid = get_rvalue (ptr, ctxt);
svalue_id offset_sid = get_rvalue (offset, ctxt);
region_id element_rid
= get_or_create_pointer_plus_expr (TREE_TYPE (TREE_TYPE (ptr)),
ptr_sid, offset_sid,
ctxt);
svalue_id element_ptr_sid
= get_or_create_ptr_svalue (TREE_TYPE (ptr), element_rid);
set_value (lhs_rid, element_ptr_sid, ctxt);
}
break;
case POINTER_DIFF_EXPR:
{
/* e.g. "_1 = p_2(D) - q_3(D);". */
/* TODO. */
set_to_new_unknown_value (lhs_rid, TREE_TYPE (lhs), ctxt);
}
break;
case ADDR_EXPR:
{
/* LHS = &RHS; */
svalue_id ptr_sid = get_rvalue (rhs1, ctxt);
set_value (lhs_rid, ptr_sid, ctxt);
}
break;
case MEM_REF:
{
region_id rhs_rid = get_lvalue (rhs1, ctxt);
svalue_id rhs_sid
= get_region (rhs_rid)->get_value (*this, true, ctxt);
set_value (lhs_rid, rhs_sid, ctxt);
}
break;
case REAL_CST:
case INTEGER_CST:
case ARRAY_REF:
{
/* LHS = RHS; */
svalue_id cst_sid = get_rvalue (rhs1, ctxt);
set_value (lhs_rid, cst_sid, ctxt);
}
break;
case FIX_TRUNC_EXPR:
case FLOAT_EXPR:
case NOP_EXPR:
// cast: TODO
// fall though for now
case SSA_NAME:
case VAR_DECL:
case PARM_DECL:
{
/* LHS = VAR; */
region_id rhs_rid = get_lvalue (rhs1, ctxt);
copy_region (lhs_rid, rhs_rid, ctxt);
}
break;
case EQ_EXPR:
case GE_EXPR:
case LE_EXPR:
case NE_EXPR:
case GT_EXPR:
case LT_EXPR:
{
tree rhs2 = gimple_assign_rhs2 (assign);
// TODO: constraints between svalues
svalue_id rhs1_sid = get_rvalue (rhs1, ctxt);
svalue_id rhs2_sid = get_rvalue (rhs2, ctxt);
tristate t = eval_condition (rhs1_sid, op, rhs2_sid);
if (t.is_known ())
set_value (lhs_rid,
get_rvalue (t.is_true ()
? boolean_true_node
: boolean_false_node,
ctxt),
ctxt);
else
set_to_new_unknown_value (lhs_rid, TREE_TYPE (lhs), ctxt);
}
break;
case NEGATE_EXPR:
case BIT_NOT_EXPR:
{
// TODO: unary ops
// TODO: constant?
set_to_new_unknown_value (lhs_rid, TREE_TYPE (lhs), ctxt);
}
break;
case PLUS_EXPR:
case MINUS_EXPR:
case MULT_EXPR:
case TRUNC_DIV_EXPR:
case TRUNC_MOD_EXPR:
case LSHIFT_EXPR:
case RSHIFT_EXPR:
case BIT_IOR_EXPR:
case BIT_XOR_EXPR:
case BIT_AND_EXPR:
case MIN_EXPR:
case MAX_EXPR:
{
/* Binary ops. */
tree rhs2 = gimple_assign_rhs2 (assign);
svalue_id rhs1_sid = get_rvalue (rhs1, ctxt);
svalue_id rhs2_sid = get_rvalue (rhs2, ctxt);
if (tree rhs1_cst = maybe_get_constant (rhs1_sid))
if (tree rhs2_cst = maybe_get_constant (rhs2_sid))
{
tree result = fold_binary (op, TREE_TYPE (lhs),
rhs1_cst, rhs2_cst);
if (result && CONSTANT_CLASS_P (result))
{
svalue_id result_sid
= get_or_create_constant_svalue (result);
set_value (lhs_rid, result_sid, ctxt);
return;
}
}
set_to_new_unknown_value (lhs_rid, TREE_TYPE (lhs), ctxt);
}
break;
case COMPONENT_REF:
{
/* LHS = op0.op1; */
region_id child_rid = get_lvalue (rhs1, ctxt);
svalue_id child_sid
= get_region (child_rid)->get_value (*this, true, ctxt);
set_value (lhs_rid, child_sid, ctxt);
}
break;
}
}
/* Update this model for the CALL stmt, using CTXT to report any
diagnostics - the first half.
Updates to the region_model that should be made *before* sm-states
are updated are done here; other updates to the region_model are done
in region_model::on_call_post.
Return true if the function call has unknown side effects (it wasn't
recognized and we don't have a body for it, or are unable to tell which
fndecl it is). */
bool
region_model::on_call_pre (const gcall *call, region_model_context *ctxt)
{
region_id lhs_rid;
tree lhs_type = NULL_TREE;
if (tree lhs = gimple_call_lhs (call))
{
lhs_rid = get_lvalue (lhs, ctxt);
lhs_type = TREE_TYPE (lhs);
}
/* Check for uses of poisoned values.
For now, special-case "free", to avoid warning about "use-after-free"
when "double free" would be more precise. */
if (!is_special_named_call_p (call, "free", 1))
for (unsigned i = 0; i < gimple_call_num_args (call); i++)
check_for_poison (gimple_call_arg (call, i), ctxt);
bool unknown_side_effects = false;
if (tree callee_fndecl = get_fndecl_for_call (call, ctxt))
{
if (is_named_call_p (callee_fndecl, "malloc", call, 1))
{
// TODO: capture size as a svalue?
region_id new_rid = add_new_malloc_region ();
if (!lhs_rid.null_p ())
{
svalue_id ptr_sid
= get_or_create_ptr_svalue (lhs_type, new_rid);
set_value (lhs_rid, ptr_sid, ctxt);
}
return false;
}
else if (is_named_call_p (callee_fndecl, "__builtin_alloca", call, 1))
{
region_id frame_rid = get_current_frame_id ();
region_id new_rid
= add_region (new symbolic_region (frame_rid, NULL_TREE, false));
if (!lhs_rid.null_p ())
{
svalue_id ptr_sid
= get_or_create_ptr_svalue (lhs_type, new_rid);
set_value (lhs_rid, ptr_sid, ctxt);
}
return false;
}
else if (gimple_call_builtin_p (call, BUILT_IN_EXPECT)
|| gimple_call_builtin_p (call, BUILT_IN_EXPECT_WITH_PROBABILITY)
|| gimple_call_internal_p (call, IFN_BUILTIN_EXPECT))
{
/* __builtin_expect's return value is its initial argument. */
if (!lhs_rid.null_p ())
{
tree initial_arg = gimple_call_arg (call, 0);
svalue_id sid = get_rvalue (initial_arg, ctxt);
set_value (lhs_rid, sid, ctxt);
}
return false;
}
else if (is_named_call_p (callee_fndecl, "strlen", call, 1))
{
region_id buf_rid = deref_rvalue (gimple_call_arg (call, 0), ctxt);
svalue_id buf_sid
= get_region (buf_rid)->get_value (*this, true, ctxt);
if (tree cst_expr = maybe_get_constant (buf_sid))
{
if (TREE_CODE (cst_expr) == STRING_CST
&& !lhs_rid.null_p ())
{
/* TREE_STRING_LENGTH is sizeof, not strlen. */
int sizeof_cst = TREE_STRING_LENGTH (cst_expr);
int strlen_cst = sizeof_cst - 1;
tree t_cst = build_int_cst (lhs_type, strlen_cst);
svalue_id result_sid
= get_or_create_constant_svalue (t_cst);
set_value (lhs_rid, result_sid, ctxt);
return false;
}
}
/* Otherwise an unknown value. */
}
else if (is_named_call_p (callee_fndecl,
"__analyzer_dump_num_heap_regions", call, 0))
{
/* Handle the builtin "__analyzer_dump_num_heap_regions" by emitting
a warning (for use in DejaGnu tests). */
int num_heap_regions = 0;
region_id heap_rid = get_root_region ()->ensure_heap_region (this);
unsigned i;
region *region;
FOR_EACH_VEC_ELT (m_regions, i, region)
if (region->get_parent () == heap_rid)
num_heap_regions++;
/* Use quotes to ensure the output isn't truncated. */
warning_at (call->location, 0,
"num heap regions: %qi", num_heap_regions);
return false;
}
else if (!fndecl_has_gimple_body_p (callee_fndecl)
&& !DECL_PURE_P (callee_fndecl))
unknown_side_effects = true;
}
else
unknown_side_effects = true;
/* Unknown return value. */
if (!lhs_rid.null_p ())
set_to_new_unknown_value (lhs_rid, lhs_type, ctxt);
return unknown_side_effects;
}
/* Update this model for the CALL stmt, using CTXT to report any
diagnostics - the second half.
Updates to the region_model that should be made *after* sm-states
are updated are done here; other updates to the region_model are done
in region_model::on_call_pre.
If UNKNOWN_SIDE_EFFECTS is true, also call handle_unrecognized_call
to purge state. */
void
region_model::on_call_post (const gcall *call,
bool unknown_side_effects,
region_model_context *ctxt)
{
/* Update for "free" here, after sm-handling.
If the ptr points to an underlying heap region, delete the region,
poisoning pointers to it and regions within it.
We delay this until after sm-state has been updated so that the
sm-handling can transition all of the various casts of the pointer
to a "freed" state *before* we delete the related region here.
This has to be done here so that the sm-handling can use the fact
that they point to the same region to establish that they are equal
(in region_model::eval_condition_without_cm), and thus transition
all pointers to the region to the "freed" state together, regardless
of casts. */
if (tree callee_fndecl = get_fndecl_for_call (call, ctxt))
if (is_named_call_p (callee_fndecl, "free", call, 1))
{
tree ptr = gimple_call_arg (call, 0);
svalue_id ptr_sid = get_rvalue (ptr, ctxt);
svalue *ptr_sval = get_svalue (ptr_sid);
if (region_svalue *ptr_to_region_sval
= ptr_sval->dyn_cast_region_svalue ())
{
/* If the ptr points to an underlying heap region, delete it,
poisoning pointers. */
region_id pointee_rid = ptr_to_region_sval->get_pointee ();
region_id heap_rid = get_root_region ()->ensure_heap_region (this);
if (!pointee_rid.null_p ()
&& get_region (pointee_rid)->get_parent () == heap_rid)
{
purge_stats stats;
delete_region_and_descendents (pointee_rid,
POISON_KIND_FREED,
&stats, ctxt->get_logger ());
purge_unused_svalues (&stats, ctxt);
validate ();
// TODO: do anything with stats?
}
}
return;
}
if (unknown_side_effects)
handle_unrecognized_call (call, ctxt);
}
/* Helper class for region_model::handle_unrecognized_call, for keeping
track of all regions that are reachable, and, of those, which are
mutable. */
class reachable_regions
{
public:
reachable_regions (region_model *model)
: m_model (model), m_reachable_rids (), m_mutable_rids ()
{}
/* Lazily mark RID as being reachable, recursively adding regions
reachable from RID. */
void add (region_id rid, bool is_mutable)
{
gcc_assert (!rid.null_p ());
unsigned idx = rid.as_int ();
/* Bail out if this region is already in the sets at the IS_MUTABLE
level of mutability. */
if (!is_mutable && bitmap_bit_p (m_reachable_rids, idx))
return;
bitmap_set_bit (m_reachable_rids, idx);
if (is_mutable)
{
if (bitmap_bit_p (m_mutable_rids, idx))
return;
else
bitmap_set_bit (m_mutable_rids, idx);
}
/* If this region's value is a pointer, add the pointee. */
region *reg = m_model->get_region (rid);
svalue_id sid = reg->get_value_direct ();
svalue *sval = m_model->get_svalue (sid);
if (sval)
if (region_svalue *ptr = sval->dyn_cast_region_svalue ())
{
region_id pointee_rid = ptr->get_pointee ();
/* Use const-ness of pointer type to affect mutability. */
bool ptr_is_mutable = true;
if (ptr->get_type ()
&& TREE_CODE (ptr->get_type ()) == POINTER_TYPE
&& TYPE_READONLY (TREE_TYPE (ptr->get_type ())))
ptr_is_mutable = false;
add (pointee_rid, ptr_is_mutable);
}
/* Add descendents of this region. */
region_id_set descendents (m_model);
m_model->get_descendents (rid, &descendents, region_id::null ());
for (unsigned i = 0; i < m_model->get_num_regions (); i++)
{
region_id iter_rid = region_id::from_int (i);
if (descendents.region_p (iter_rid))
add (iter_rid, is_mutable);
}
}
bool mutable_p (region_id rid)
{
gcc_assert (!rid.null_p ());
return bitmap_bit_p (m_mutable_rids, rid.as_int ());
}
private:
region_model *m_model;
/* The region ids already seen. This has to be an auto_bitmap rather than
an auto_sbitmap as new regions can be created within the model during
the traversal. */
auto_bitmap m_reachable_rids;
/* The region_ids that can be changed (accessed via non-const pointers). */
auto_bitmap m_mutable_rids;
};
/* Handle a call CALL to a function with unknown behavior.
Traverse the regions in this model, determining what regions are
reachable from pointer arguments to CALL and from global variables,
recursively.
Set all reachable regions to new unknown values and purge sm-state
from their values, and from values that point to them. */
void
region_model::handle_unrecognized_call (const gcall *call,
region_model_context *ctxt)
{
tree fndecl = get_fndecl_for_call (call, ctxt);
reachable_regions reachable_regions (this);
/* Determine the reachable regions and their mutability. */
{
/* Globals. */
region_id globals_rid = get_globals_region_id ();
if (!globals_rid.null_p ())
reachable_regions.add (globals_rid, true);
/* Params that are pointers. */
tree iter_param_types = NULL_TREE;
if (fndecl)
iter_param_types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
for (unsigned arg_idx = 0; arg_idx < gimple_call_num_args (call); arg_idx++)
{
/* Track expected param type, where available. */
tree param_type = NULL_TREE;
if (iter_param_types)
{
param_type = TREE_VALUE (iter_param_types);
gcc_assert (param_type);
iter_param_types = TREE_CHAIN (iter_param_types);
}
tree parm = gimple_call_arg (call, arg_idx);
svalue_id parm_sid = get_rvalue (parm, ctxt);
svalue *parm_sval = get_svalue (parm_sid);
if (parm_sval)
if (region_svalue *parm_ptr = parm_sval->dyn_cast_region_svalue ())
{
region_id pointee_rid = parm_ptr->get_pointee ();
bool is_mutable = true;
if (param_type
&& TREE_CODE (param_type) == POINTER_TYPE
&& TYPE_READONLY (TREE_TYPE (param_type)))
is_mutable = false;
reachable_regions.add (pointee_rid, is_mutable);
}
// FIXME: what about compound parms that contain ptrs?
}
}
/* OK: we now have all reachable regions.
Set them all to new unknown values. */
for (unsigned i = 0; i < get_num_regions (); i++)
{
region_id iter_rid = region_id::from_int (i);
if (reachable_regions.mutable_p (iter_rid))
{
region *reg = get_region (iter_rid);
/* Purge any sm-state for any underlying svalue. */
svalue_id curr_sid = reg->get_value_direct ();
if (!curr_sid.null_p ())
ctxt->on_unknown_change (curr_sid);
set_to_new_unknown_value (iter_rid,
reg->get_type (),
ctxt);
}
}
/* Purge sm-state for any remaining svalues that point to regions that
were reachable. This helps suppress leak false-positives.
For example, if we had a malloc call that was cast to a "foo *" type,
we could have a temporary void * for the result of malloc which has its
own svalue, not reachable from the function call, but for which the
"foo *" svalue was reachable. If we don't purge it, the temporary will
be reported as a leak. */
int i;
svalue *svalue;
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
if (region_svalue *ptr = svalue->dyn_cast_region_svalue ())
{
region_id pointee_rid = ptr->get_pointee ();
if (reachable_regions.mutable_p (pointee_rid))
ctxt->on_unknown_change (svalue_id::from_int (i));
}
validate ();
}
/* Update this model for the RETURN_STMT, using CTXT to report any
diagnostics. */
void
region_model::on_return (const greturn *return_stmt, region_model_context *ctxt)
{
tree callee = get_current_function ()->decl;
tree lhs = DECL_RESULT (callee);
tree rhs = gimple_return_retval (return_stmt);
if (lhs && rhs)
copy_region (get_lvalue (lhs, ctxt), get_lvalue (rhs, ctxt), ctxt);
}
/* Update this model for a call and return of setjmp/sigsetjmp at CALL within
ENODE, using CTXT to report any diagnostics.
This is for the initial direct invocation of setjmp/sigsetjmp (which returns
0), as opposed to any second return due to longjmp/sigsetjmp. */
void
region_model::on_setjmp (const gcall *call, const exploded_node *enode,
region_model_context *ctxt)
{
region_id buf_rid = deref_rvalue (gimple_call_arg (call, 0), ctxt);
region *buf = get_region (buf_rid);
/* Create a setjmp_svalue for this call and store it in BUF_RID's region. */
if (buf)
{
setjmp_record r (enode, call);
svalue *sval = new setjmp_svalue (r, buf->get_type ());
svalue_id new_sid = add_svalue (sval);
set_value (buf_rid, new_sid, ctxt);
}
/* Direct calls to setjmp return 0. */
if (tree lhs = gimple_call_lhs (call))
{
tree zero = build_int_cst (TREE_TYPE (lhs), 0);
svalue_id new_sid = get_or_create_constant_svalue (zero);
region_id lhs_rid = get_lvalue (lhs, ctxt);
set_value (lhs_rid, new_sid, ctxt);
}
}
/* Update this region_model for rewinding from a "longjmp" at LONGJMP_CALL
to a "setjmp" at SETJMP_CALL where the final stack depth should be
SETJMP_STACK_DEPTH. Purge any stack frames, potentially reporting on
leaks to CTXT. */
void
region_model::on_longjmp (const gcall *longjmp_call, const gcall *setjmp_call,
int setjmp_stack_depth,
region_model_context *ctxt)
{
/* Evaluate the val, using the frame of the "longjmp". */
tree fake_retval = gimple_call_arg (longjmp_call, 1);
svalue_id fake_retval_sid = get_rvalue (fake_retval, ctxt);
/* Pop any frames until we reach the stack depth of the function where
setjmp was called. */
gcc_assert (get_stack_depth () >= setjmp_stack_depth);
while (get_stack_depth () > setjmp_stack_depth)
{
/* Don't purge unused svalues yet, as we're using fake_retval_sid. */
pop_frame (region_id::null (), false, NULL, ctxt);
}
gcc_assert (get_stack_depth () == setjmp_stack_depth);
/* Assign to LHS of "setjmp" in new_state. */
if (tree lhs = gimple_call_lhs (setjmp_call))
{
/* Passing 0 as the val to longjmp leads to setjmp returning 1. */
tree t_zero = build_int_cst (TREE_TYPE (fake_retval), 0);
svalue_id zero_sid = get_or_create_constant_svalue (t_zero);
tristate eq_zero = eval_condition (fake_retval_sid, EQ_EXPR, zero_sid);
/* If we have 0, use 1. */
if (eq_zero.is_true ())
{
tree t_one = build_int_cst (TREE_TYPE (fake_retval), 1);
svalue_id one_sid = get_or_create_constant_svalue (t_one);
fake_retval_sid = one_sid;
}
else
{
/* Otherwise note that the value is nonzero. */
m_constraints->add_constraint (fake_retval_sid, NE_EXPR, zero_sid);
}
region_id lhs_rid = get_lvalue (lhs, ctxt);
set_value (lhs_rid, fake_retval_sid, ctxt);
}
/* Now that we've assigned the fake_retval, we can purge the unused
svalues, which could detect leaks. */
purge_unused_svalues (NULL, ctxt, NULL);
validate ();
}
/* Update this region_model for a phi stmt of the form
LHS = PHI <...RHS...>.
where RHS is for the appropriate edge. */
void
region_model::handle_phi (const gphi *phi,
tree lhs, tree rhs, bool is_back_edge,
region_model_context *ctxt)
{
/* For now, don't bother tracking the .MEM SSA names. */
if (tree var = SSA_NAME_VAR (lhs))
if (TREE_CODE (var) == VAR_DECL)
if (VAR_DECL_IS_VIRTUAL_OPERAND (var))
return;
svalue_id rhs_sid = get_rvalue (rhs, ctxt);
if (is_back_edge && get_svalue (rhs_sid)->get_kind () != SK_UNKNOWN)
{
/* If we have a back edge, we probably have a loop.
Use an unknown value, to avoid effectively unrolling the
loop.
To terminate, we need to avoid generating a series of
models with an unbounded monotonically increasing number of
redundant unknown values; hence we need to purge svalues
before inserting the state into the exploded graph, to
collect unused svalues. */
set_to_new_unknown_value (get_lvalue (lhs, ctxt), TREE_TYPE (lhs), ctxt);
}
else
set_value (get_lvalue (lhs, ctxt), rhs_sid, ctxt);
if (ctxt)
ctxt->on_phi (phi, rhs);
}
/* Implementation of region_model::get_lvalue; the latter adds type-checking.
Get the id of the region for PV within this region_model,
emitting any diagnostics to CTXT. */
region_id
region_model::get_lvalue_1 (path_var pv, region_model_context *ctxt)
{
tree expr = pv.m_tree;
gcc_assert (expr);
switch (TREE_CODE (expr))
{
default:
return make_region_for_unexpected_tree_code (ctxt, expr,
dump_location_t ());
case ARRAY_REF:
{
tree array = TREE_OPERAND (expr, 0);
tree index = TREE_OPERAND (expr, 1);
#if 0
// TODO: operands 2 and 3, if present:
gcc_assert (TREE_OPERAND (expr, 2) == NULL_TREE);
gcc_assert (TREE_OPERAND (expr, 3) == NULL_TREE);
#endif
region_id array_rid = get_lvalue (array, ctxt);
svalue_id index_sid = get_rvalue (index, ctxt);
region *base_array_reg = get_region (array_rid);
array_region *array_reg = base_array_reg->dyn_cast_array_region ();
if (!array_reg)
{
/* Normally, array_rid ought to refer to an array_region, since
array's type will be ARRAY_TYPE. However, if we have an
unexpected tree code for array, we could have a
symbolic_region here. If so, we're in error-handling. */
gcc_assert (base_array_reg->get_type () == NULL_TREE);
return make_region_for_unexpected_tree_code (ctxt, expr,
dump_location_t ());
}
return array_reg->get_element (this, array_rid, index_sid, ctxt);
}
break;
case BIT_FIELD_REF:
{
/* For now, create a view, as if a cast, ignoring the bit positions. */
tree obj = TREE_OPERAND (expr, 0);
return get_or_create_view (get_lvalue (obj, ctxt), TREE_TYPE (expr),
ctxt);
};
break;
case MEM_REF:
{
tree ptr = TREE_OPERAND (expr, 0);
tree offset = TREE_OPERAND (expr, 1);
svalue_id ptr_sid = get_rvalue (ptr, ctxt);
svalue_id offset_sid = get_rvalue (offset, ctxt);
return get_or_create_mem_ref (TREE_TYPE (expr), ptr_sid,
offset_sid, ctxt);
}
break;
case VAR_DECL:
/* Handle globals. */
if (is_global_var (expr))
{
region_id globals_rid
= get_root_region ()->ensure_globals_region (this);
map_region *globals = get_region<map_region> (globals_rid);
region_id var_rid = globals->get_or_create (this, globals_rid, expr,
TREE_TYPE (expr), ctxt);
return var_rid;
}
/* Fall through. */
case SSA_NAME:
case PARM_DECL:
case RESULT_DECL:
{
gcc_assert (TREE_CODE (expr) == SSA_NAME
|| TREE_CODE (expr) == PARM_DECL
|| TREE_CODE (expr) == VAR_DECL
|| TREE_CODE (expr) == RESULT_DECL);
int stack_depth = pv.m_stack_depth;
stack_region *stack = get_root_region ()->get_stack_region (this);
gcc_assert (stack);
region_id frame_rid = stack->get_frame_rid (stack_depth);
frame_region *frame = get_region <frame_region> (frame_rid);
gcc_assert (frame);
region_id child_rid = frame->get_or_create (this, frame_rid, expr,
TREE_TYPE (expr), ctxt);
return child_rid;
}
case COMPONENT_REF:
{
/* obj.field */
tree obj = TREE_OPERAND (expr, 0);
tree field = TREE_OPERAND (expr, 1);
tree obj_type = TREE_TYPE (obj);
if (TREE_CODE (obj_type) != RECORD_TYPE
&& TREE_CODE (obj_type) != UNION_TYPE)
return make_region_for_unexpected_tree_code (ctxt, obj_type,
dump_location_t ());
region_id obj_rid = get_lvalue (obj, ctxt);
region_id struct_or_union_rid
= get_or_create_view (obj_rid, TREE_TYPE (obj), ctxt);
return get_field_region (struct_or_union_rid, field, ctxt);
}
break;
case CONST_DECL:
{
tree cst_type = TREE_TYPE (expr);
region_id cst_rid = add_region_for_type (m_root_rid, cst_type, ctxt);
if (tree value = DECL_INITIAL (expr))
{
svalue_id sid = get_rvalue (value, ctxt);
get_region (cst_rid)->set_value (*this, cst_rid, sid, ctxt);
}
return cst_rid;
}
break;
case STRING_CST:
{
tree cst_type = TREE_TYPE (expr);
array_region *cst_region = new array_region (m_root_rid, cst_type);
region_id cst_rid = add_region (cst_region);
svalue_id cst_sid = get_or_create_constant_svalue (expr);
cst_region->set_value (*this, cst_rid, cst_sid, ctxt);
return cst_rid;
}
break;
case NOP_EXPR:
case VIEW_CONVERT_EXPR:
{
tree obj = TREE_OPERAND (expr, 0);
return get_or_create_view (get_lvalue (obj, ctxt), TREE_TYPE (expr),
ctxt);
};
break;
}
}
/* If we see a tree code we don't know how to handle, rather than
ICE or generate bogus results, create a dummy region, and notify
CTXT so that it can mark the new state as being not properly
modelled. The exploded graph can then stop exploring that path,
since any diagnostics we might issue will have questionable
validity. */
region_id
region_model::make_region_for_unexpected_tree_code (region_model_context *ctxt,
tree t,
const dump_location_t &loc)
{
gcc_assert (ctxt);
region_id new_rid
= add_region (new symbolic_region (m_root_rid, NULL_TREE, false));
ctxt->on_unexpected_tree_code (t, loc);
return new_rid;
}
/* Assert that SRC_TYPE can be converted to DST_TYPE as a no-op. */
static void
assert_compat_types (tree src_type, tree dst_type)
{
if (src_type && dst_type && !VOID_TYPE_P (dst_type))
gcc_checking_assert (useless_type_conversion_p (src_type, dst_type));
}
/* Get the id of the region for PV within this region_model,
emitting any diagnostics to CTXT. */
region_id
region_model::get_lvalue (path_var pv, region_model_context *ctxt)
{
if (pv.m_tree == NULL_TREE)
return region_id::null ();
region_id result_rid = get_lvalue_1 (pv, ctxt);
assert_compat_types (get_region (result_rid)->get_type (),
TREE_TYPE (pv.m_tree));
return result_rid;
}
/* Get the region_id for EXPR within this region_model (assuming the most
recent stack frame if it's a local). */
region_id
region_model::get_lvalue (tree expr, region_model_context *ctxt)
{
return get_lvalue (path_var (expr, get_stack_depth () - 1), ctxt);
}
/* Implementation of region_model::get_rvalue; the latter adds type-checking.
Get the value of PV within this region_model,
emitting any diagnostics to CTXT. */
svalue_id
region_model::get_rvalue_1 (path_var pv, region_model_context *ctxt)
{
gcc_assert (pv.m_tree);
switch (TREE_CODE (pv.m_tree))
{
default:
{
svalue *unknown_sval = new unknown_svalue (TREE_TYPE (pv.m_tree));
return add_svalue (unknown_sval);
}
break;
case ADDR_EXPR:
{
/* "&EXPR". */
tree expr = pv.m_tree;
tree op0 = TREE_OPERAND (expr, 0);
if (TREE_CODE (op0) == FUNCTION_DECL)
return get_svalue_for_fndecl (TREE_TYPE (expr), op0, ctxt);
else if (TREE_CODE (op0) == LABEL_DECL)
return get_svalue_for_label (TREE_TYPE (expr), op0, ctxt);
region_id expr_rid = get_lvalue (op0, ctxt);
return get_or_create_ptr_svalue (TREE_TYPE (expr), expr_rid);
}
break;
case ARRAY_REF:
{
region_id element_rid = get_lvalue (pv, ctxt);
return get_region (element_rid)->get_value (*this, true, ctxt);
}
case INTEGER_CST:
case REAL_CST:
case STRING_CST:
return get_or_create_constant_svalue (pv.m_tree);
case COMPONENT_REF:
case MEM_REF:
case SSA_NAME:
case VAR_DECL:
case PARM_DECL:
case RESULT_DECL:
{
region_id var_rid = get_lvalue (pv, ctxt);
return get_region (var_rid)->get_value (*this, true, ctxt);
}
}
}
/* Get the value of PV within this region_model,
emitting any diagnostics to CTXT. */
svalue_id
region_model::get_rvalue (path_var pv, region_model_context *ctxt)
{
if (pv.m_tree == NULL_TREE)
return svalue_id::null ();
svalue_id result_sid = get_rvalue_1 (pv, ctxt);
assert_compat_types (get_svalue (result_sid)->get_type (),
TREE_TYPE (pv.m_tree));
return result_sid;
}
/* Get the value of EXPR within this region_model (assuming the most
recent stack frame if it's a local). */
svalue_id
region_model::get_rvalue (tree expr, region_model_context *ctxt)
{
return get_rvalue (path_var (expr, get_stack_depth () - 1), ctxt);
}
/* Return an svalue_id for a pointer to RID of type PTR_TYPE, reusing
existing pointer values if one is available. */
svalue_id
region_model::get_or_create_ptr_svalue (tree ptr_type, region_id rid)
{
/* Reuse existing region_svalue, if one of the right type is
available. */
/* In theory we could stash a svalue_id in "region", but differing
pointer types muddles things.
For now, just do a linear search through all existing svalues. */
int i;
svalue *svalue;
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
if (region_svalue *ptr_svalue = svalue->dyn_cast_region_svalue ())
if (ptr_svalue->get_pointee () == rid
&& ptr_svalue->get_type () == ptr_type)
return svalue_id::from_int (i);
return add_svalue (new region_svalue (ptr_type, rid));
}
/* Return an svalue_id for a constant_svalue for CST_EXPR,
creating the constant_svalue if necessary.
The constant_svalue instances are reused, based on pointer equality
of trees */
svalue_id
region_model::get_or_create_constant_svalue (tree cst_expr)
{
gcc_assert (cst_expr);
/* Reuse one if it already exists. */
// TODO: maybe store a map, rather than do linear search?
int i;
svalue *svalue;
FOR_EACH_VEC_ELT (m_svalues, i, svalue)
if (svalue->maybe_get_constant () == cst_expr)
return svalue_id::from_int (i);
svalue_id cst_sid = add_svalue (new constant_svalue (cst_expr));
return cst_sid;
}
/* Return an svalue_id for a region_svalue for FNDECL,
creating the function_region if necessary. */
svalue_id
region_model::get_svalue_for_fndecl (tree ptr_type, tree fndecl,
region_model_context *ctxt)
{
gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
region_id function_rid = get_region_for_fndecl (fndecl, ctxt);
return get_or_create_ptr_svalue (ptr_type, function_rid);
}
/* Return a region_id for a function_region for FNDECL,
creating it if necessary. */
region_id
region_model::get_region_for_fndecl (tree fndecl,
region_model_context *ctxt)
{
gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
region_id code_rid = get_root_region ()->ensure_code_region (this);
code_region *code = get_root_region ()->get_code_region (this);
return code->get_or_create (this, code_rid, fndecl, TREE_TYPE (fndecl),
ctxt);
}
/* Return an svalue_id for a region_svalue for LABEL,
creating the label_region if necessary. */
svalue_id
region_model::get_svalue_for_label (tree ptr_type, tree label,
region_model_context *ctxt)
{
gcc_assert (TREE_CODE (label) == LABEL_DECL);
region_id label_rid = get_region_for_label (label, ctxt);
return get_or_create_ptr_svalue (ptr_type, label_rid);
}
/* Return a region_id for a label_region for LABEL,
creating it if necessary. */
region_id
region_model::get_region_for_label (tree label,
region_model_context *ctxt)
{
gcc_assert (TREE_CODE (label) == LABEL_DECL);
tree fndecl = DECL_CONTEXT (label);
gcc_assert (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL);
region_id func_rid = get_region_for_fndecl (fndecl, ctxt);
function_region *func_reg = get_region <function_region> (func_rid);
return func_reg->get_or_create (this, func_rid, label, TREE_TYPE (label),
ctxt);
}
/* Build a cast of SRC_EXPR to DST_TYPE, or return NULL_TREE.
Adapted from gcc::jit::playback::context::build_cast, which in turn is
adapted from
- c/c-typeck.c:build_c_cast
- c/c-convert.c: convert
- convert.h
Only some kinds of cast are currently supported here. */
static tree
build_cast (tree dst_type, tree src_expr)
{
tree result = targetm.convert_to_type (dst_type, src_expr);
if (result)
return result;
enum tree_code dst_code = TREE_CODE (dst_type);
switch (dst_code)
{
case INTEGER_TYPE:
case ENUMERAL_TYPE:
result = convert_to_integer (dst_type, src_expr);
goto maybe_fold;
case BOOLEAN_TYPE:
/* Compare with c_objc_common_truthvalue_conversion and
c_common_truthvalue_conversion. */
/* For now, convert to: (src_expr != 0) */
result = build2 (NE_EXPR, dst_type,
src_expr,
build_int_cst (TREE_TYPE (src_expr), 0));
goto maybe_fold;
case REAL_TYPE:
result = convert_to_real (dst_type, src_expr);
goto maybe_fold;
case POINTER_TYPE:
result = build1 (NOP_EXPR, dst_type, src_expr);
goto maybe_fold;
default:
return NULL_TREE;
maybe_fold:
if (TREE_CODE (result) != C_MAYBE_CONST_EXPR)
result = fold (result);
return result;
}
}
/* If the type of SID's underlying value is DST_TYPE, return SID.
Otherwise, attempt to create (or reuse) an svalue representing an access
of SID as a DST_TYPE and return that value's svalue_id. */
svalue_id
region_model::maybe_cast_1 (tree dst_type, svalue_id sid)
{
svalue *sval = get_svalue (sid);
tree src_type = sval->get_type ();
if (src_type == dst_type)
return sid;
if (POINTER_TYPE_P (dst_type)
|| POINTER_TYPE_P (src_type))
{
/* Pointer to region. */
if (region_svalue *ptr_sval = sval->dyn_cast_region_svalue ())
return get_or_create_ptr_svalue (dst_type, ptr_sval->get_pointee ());
/* Unknown pointer? Get or create a new unknown pointer of the
correct type, preserving the equality between the pointers. */
if (sval->dyn_cast_unknown_svalue ())
{
equiv_class &ec = m_constraints->get_equiv_class (sid);
/* Look for an existing pointer of the correct type within the EC. */
int i;
svalue_id *equiv_sid;
FOR_EACH_VEC_ELT (ec.m_vars, i, equiv_sid)
{
svalue *equiv_val = get_svalue (*equiv_sid);
if (equiv_val->get_type () == dst_type)
return *equiv_sid;
}
/* Otherwise, create a new unknown pointer of the correct type. */
svalue *unknown_sval = new unknown_svalue (dst_type);
svalue_id new_ptr_sid = add_svalue (unknown_sval);
m_constraints->add_constraint (sid, EQ_EXPR, new_ptr_sid);
return new_ptr_sid;
}
}
/* Attempt to cast constants. */
if (tree src_cst = sval->maybe_get_constant ())
{
if (tree dst = build_cast (dst_type, src_cst))
if (CONSTANT_CLASS_P (dst))
return get_or_create_constant_svalue (dst);
}
/* Otherwise, return a new unknown value. */
svalue *unknown_sval = new unknown_svalue (dst_type);
return add_svalue (unknown_sval);
}
/* If the type of SID's underlying value is DST_TYPE, return SID.
Otherwise, attempt to create (or reuse) an svalue representing an access
of SID as a DST_TYPE and return that value's svalue_id.
If the result != SID, then call CTXT's on_cast vfunc (if CTXT is non-NULL),
so that sm-state can be propagated from SID to the result. */
svalue_id
region_model::maybe_cast (tree dst_type, svalue_id sid,
region_model_context *ctxt)
{
svalue_id result = maybe_cast_1 (dst_type, sid);
if (result != sid)
if (ctxt)
{
/* Notify ctxt about a cast, so any sm-state can be copied. */
ctxt->on_cast (sid, result);
}
return result;
}
/* Ensure that the region for OBJ_RID has a child region for FIELD;
return the child region's region_id. */
region_id
region_model::get_field_region (region_id struct_or_union_rid, tree field,
region_model_context *ctxt)
{
struct_or_union_region *sou_reg
= get_region<struct_or_union_region> (struct_or_union_rid);
/* Inherit constness from parent type. */
const int qual_mask = TYPE_QUAL_CONST;
int sou_quals = TYPE_QUALS (sou_reg->get_type ()) & qual_mask;
tree field_type = TREE_TYPE (field);
tree field_type_with_quals = build_qualified_type (field_type, sou_quals);
// TODO: maybe convert to a vfunc?
if (sou_reg->get_kind () == RK_UNION)
{
/* Union.
Get a view of the union as a whole, with the type of the field. */
region_id view_rid
= get_or_create_view (struct_or_union_rid, field_type_with_quals, ctxt);
return view_rid;
}
else
{
/* Struct. */
region_id child_rid
= sou_reg->get_or_create (this, struct_or_union_rid, field,
field_type_with_quals, ctxt);
return child_rid;
}
}
/* Get a region_id for referencing PTR_SID, creating a region if need be, and
potentially generating warnings via CTXT. */
region_id
region_model::deref_rvalue (svalue_id ptr_sid, region_model_context *ctxt)
{
gcc_assert (!ptr_sid.null_p ());
svalue *ptr_svalue = get_svalue (ptr_sid);
gcc_assert (ptr_svalue);
switch (ptr_svalue->get_kind ())
{
case SK_REGION:
{
region_svalue *region_sval = as_a <region_svalue *> (ptr_svalue);
return region_sval->get_pointee ();
}
case SK_CONSTANT:
goto create_symbolic_region;
case SK_POISONED:
{
if (ctxt)
if (tree ptr = get_representative_tree (ptr_sid))
{
poisoned_svalue *poisoned_sval
= as_a <poisoned_svalue *> (ptr_svalue);
enum poison_kind pkind = poisoned_sval->get_poison_kind ();
ctxt->warn (new poisoned_value_diagnostic (ptr, pkind));
}
goto create_symbolic_region;
}
case SK_UNKNOWN:
{
create_symbolic_region:
/* We need a symbolic_region to represent this unknown region.
We don't know if it on the heap, stack, or a global,
so use the root region as parent. */
region_id new_rid
= add_region (new symbolic_region (m_root_rid, NULL_TREE, false));
/* We need to write the region back into the pointer,
or we'll get a new, different region each time.
We do this by changing the meaning of ptr_sid, replacing
the unknown value with the ptr to the new region.
We replace the meaning of the ID rather than simply writing
to PTR's lvalue since there could be several places sharing
the same unknown ptr value. */
svalue *ptr_val
= new region_svalue (ptr_svalue->get_type (), new_rid);
replace_svalue (ptr_sid, ptr_val);
return new_rid;
}
case SK_SETJMP:
goto create_symbolic_region;
}
gcc_unreachable ();
}
/* Get a region_id for referencing PTR, creating a region if need be, and
potentially generating warnings via CTXT. */
region_id
region_model::deref_rvalue (tree ptr, region_model_context *ctxt)
{
svalue_id ptr_sid = get_rvalue (ptr, ctxt);
return deref_rvalue (ptr_sid, ctxt);
}
/* Set the value of the region given by LHS_RID to the value given
by RHS_SID. */
void
region_model::set_value (region_id lhs_rid, svalue_id rhs_sid,
region_model_context *ctxt)
{
gcc_assert (!lhs_rid.null_p ());
gcc_assert (!rhs_sid.null_p ());
get_region (lhs_rid)->set_value (*this, lhs_rid, rhs_sid, ctxt);
}
/* Set the value of the region given by LHS to the value given
by RHS. */
void
region_model::set_value (tree lhs, tree rhs, region_model_context *ctxt)
{
region_id lhs_rid = get_lvalue (lhs, ctxt);
svalue_id rhs_sid = get_rvalue (rhs, ctxt);
gcc_assert (!lhs_rid.null_p ());
gcc_assert (!rhs_sid.null_p ());
set_value (lhs_rid, rhs_sid, ctxt);
}
/* Determine what is known about the condition "LHS_SID OP RHS_SID" within
this model. */
tristate
region_model::eval_condition (svalue_id lhs_sid,
enum tree_code op,
svalue_id rhs_sid) const
{
svalue *lhs = get_svalue (lhs_sid);
svalue *rhs = get_svalue (rhs_sid);
/* For now, make no attempt to capture constraints on floating-point
values. */
if ((lhs->get_type () && FLOAT_TYPE_P (lhs->get_type ()))
|| (rhs->get_type () && FLOAT_TYPE_P (rhs->get_type ())))
return tristate::unknown ();
tristate ts = eval_condition_without_cm (lhs_sid, op, rhs_sid);
if (ts.is_known ())
return ts;
/* Otherwise, try constraints. */
return m_constraints->eval_condition (lhs_sid, op, rhs_sid);
}
/* Determine what is known about the condition "LHS_SID OP RHS_SID" within
this model, without resorting to the constraint_manager.
This is exposed so that impl_region_model_context::on_state_leak can
check for equality part-way through region_model::purge_unused_svalues
without risking creating new ECs. */
tristate
region_model::eval_condition_without_cm (svalue_id lhs_sid,
enum tree_code op,
svalue_id rhs_sid) const
{
svalue *lhs = get_svalue (lhs_sid);
svalue *rhs = get_svalue (rhs_sid);
gcc_assert (lhs);
gcc_assert (rhs);
/* See what we know based on the values. */
if (lhs && rhs)
{
/* For now, make no attempt to capture constraints on floating-point
values. */
if ((lhs->get_type () && FLOAT_TYPE_P (lhs->get_type ()))
|| (rhs->get_type () && FLOAT_TYPE_P (rhs->get_type ())))
return tristate::unknown ();
if (lhs == rhs)
{
/* If we have the same svalue, then we have equality
(apart from NaN-handling).
TODO: should this definitely be the case for poisoned values? */
switch (op)
{
case EQ_EXPR:
case GE_EXPR:
case LE_EXPR:
return tristate::TS_TRUE;
case NE_EXPR:
case GT_EXPR:
case LT_EXPR:
return tristate::TS_FALSE;
default:
/* For other ops, use the logic below. */
break;
}
}
/* If we have a pair of region_svalues, compare them. */
if (region_svalue *lhs_ptr = lhs->dyn_cast_region_svalue ())
if (region_svalue *rhs_ptr = rhs->dyn_cast_region_svalue ())
{
tristate res = region_svalue::eval_condition (lhs_ptr, op, rhs_ptr);
if (res.is_known ())
return res;
/* Otherwise, only known through constraints. */
}
/* If we have a pair of constants, compare them. */
if (constant_svalue *cst_lhs = lhs->dyn_cast_constant_svalue ())
if (constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ())
return constant_svalue::eval_condition (cst_lhs, op, cst_rhs);
/* Handle comparison of a region_svalue against zero. */
if (region_svalue *ptr = lhs->dyn_cast_region_svalue ())
if (constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ())
if (zerop (cst_rhs->get_constant ()))
{
/* A region_svalue is a non-NULL pointer, except in certain
special cases (see the comment for region::non_null_p. */
region *pointee = get_region (ptr->get_pointee ());
if (pointee->non_null_p (*this))
{
switch (op)
{
default:
gcc_unreachable ();
case EQ_EXPR:
case GE_EXPR:
case LE_EXPR:
return tristate::TS_FALSE;
case NE_EXPR:
case GT_EXPR:
case LT_EXPR:
return tristate::TS_TRUE;
}
}
}
}
return tristate::TS_UNKNOWN;
}
/* Attempt to add the constraint "LHS OP RHS" to this region_model.
If it is consistent with existing constraints, add it, and return true.
Return false if it contradicts existing constraints.
Use CTXT for reporting any diagnostics associated with the accesses. */
bool
region_model::add_constraint (tree lhs, enum tree_code op, tree rhs,
region_model_context *ctxt)
{
/* For now, make no attempt to capture constraints on floating-point
values. */
if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
return true;
svalue_id lhs_sid = get_rvalue (lhs, ctxt);
svalue_id rhs_sid = get_rvalue (rhs, ctxt);
tristate t_cond = eval_condition (lhs_sid, op, rhs_sid);
/* If we already have the condition, do nothing. */
if (t_cond.is_true ())
return true;
/* Reject a constraint that would contradict existing knowledge, as
unsatisfiable. */
if (t_cond.is_false ())
return false;
/* Store the constraint. */
m_constraints->add_constraint (lhs_sid, op, rhs_sid);
add_any_constraints_from_ssa_def_stmt (lhs, op, rhs, ctxt);
/* If we now know a symbolic_region is non-NULL, clear its
m_possibly_null. */
if (zerop (rhs) && op == NE_EXPR)
if (region_svalue *ptr = get_svalue (lhs_sid)->dyn_cast_region_svalue ())
{
region *pointee = get_region (ptr->get_pointee ());
if (symbolic_region *sym_reg = pointee->dyn_cast_symbolic_region ())
sym_reg->m_possibly_null = false;
}
/* Notify the context, if any. This exists so that the state machines
in a program_state can be notified about the condition, and so can
set sm-state for e.g. unchecked->checked, both for cfg-edges, and
when synthesizing constraints as above. */
if (ctxt)
ctxt->on_condition (lhs, op, rhs);
return true;
}
/* Subroutine of region_model::add_constraint for handling optimized
&& and || conditionals.
If we have an SSA_NAME for a boolean compared against 0,
look at anything implied by the def stmt and call add_constraint
for it (which could recurse).
For example, if we have
_1 = p_6 == 0B;
_2 = p_8 == 0B
_3 = _1 | _2
and add the constraint
(_3 == 0),
then the def stmt for _3 implies that _1 and _2 are both false,
and hence we can add the constraints:
p_6 != 0B
p_8 != 0B. */
void
region_model::add_any_constraints_from_ssa_def_stmt (tree lhs,
enum tree_code op,
tree rhs,
region_model_context *ctxt)
{
if (TREE_CODE (lhs) != SSA_NAME)
return;
if (!zerop (rhs))
return;
if (op != NE_EXPR && op != EQ_EXPR)
return;
gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
if (const gassign *assign = dyn_cast<gassign *> (def_stmt))
add_any_constraints_from_gassign (op, rhs, assign, ctxt);
else if (gcall *call = dyn_cast<gcall *> (def_stmt))
add_any_constraints_from_gcall (op, rhs, call, ctxt);
}
/* Add any constraints for an SSA_NAME defined by ASSIGN
where the result OP RHS. */
void
region_model::add_any_constraints_from_gassign (enum tree_code op,
tree rhs,
const gassign *assign,
region_model_context *ctxt)
{
/* We have either
- "LHS != false" (i.e. LHS is true), or
- "LHS == false" (i.e. LHS is false). */
bool is_true = op == NE_EXPR;
enum tree_code rhs_code = gimple_assign_rhs_code (assign);
switch (rhs_code)
{
default:
break;
case NOP_EXPR:
{
add_constraint (gimple_assign_rhs1 (assign), op, rhs, ctxt);
}
break;
case BIT_AND_EXPR:
{
if (is_true)
{
/* ...and "LHS == (rhs1 & rhs2) i.e. "(rhs1 & rhs2)" is true
then both rhs1 and rhs2 must be true. */
tree rhs1 = gimple_assign_rhs1 (assign);
tree rhs2 = gimple_assign_rhs2 (assign);
add_constraint (rhs1, NE_EXPR, boolean_false_node, ctxt);
add_constraint (rhs2, NE_EXPR, boolean_false_node, ctxt);
}
}
break;
case BIT_IOR_EXPR:
{
if (!is_true)
{
/* ...and "LHS == (rhs1 | rhs2)
i.e. "(rhs1 | rhs2)" is false
then both rhs1 and rhs2 must be false. */
tree rhs1 = gimple_assign_rhs1 (assign);
tree rhs2 = gimple_assign_rhs2 (assign);
add_constraint (rhs1, EQ_EXPR, boolean_false_node, ctxt);
add_constraint (rhs2, EQ_EXPR, boolean_false_node, ctxt);
}
}
break;
case EQ_EXPR:
case NE_EXPR:
{
/* ...and "LHS == (rhs1 OP rhs2)"
then rhs1 OP rhs2 must have the same logical value as LHS. */
tree rhs1 = gimple_assign_rhs1 (assign);
tree rhs2 = gimple_assign_rhs2 (assign);
if (!is_true)
rhs_code
= invert_tree_comparison (rhs_code, false /* honor_nans */);
add_constraint (rhs1, rhs_code, rhs2, ctxt);
}
break;
}
}
/* Add any constraints for an SSA_NAME defined by CALL
where the result OP RHS. */
void
region_model::add_any_constraints_from_gcall (enum tree_code op,
tree rhs,
const gcall *call,
region_model_context *ctxt)
{
if (gimple_call_builtin_p (call, BUILT_IN_EXPECT)
|| gimple_call_builtin_p (call, BUILT_IN_EXPECT_WITH_PROBABILITY)
|| gimple_call_internal_p (call, IFN_BUILTIN_EXPECT))
{
/* __builtin_expect's return value is its initial argument. */
add_constraint (gimple_call_arg (call, 0), op, rhs, ctxt);
}
}
/* Determine what is known about the condition "LHS OP RHS" within
this model.
Use CTXT for reporting any diagnostics associated with the accesses. */
tristate
region_model::eval_condition (tree lhs,
enum tree_code op,
tree rhs,
region_model_context *ctxt)
{
/* For now, make no attempt to model constraints on floating-point
values. */
if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
return tristate::unknown ();
return eval_condition (get_rvalue (lhs, ctxt), op, get_rvalue (rhs, ctxt));
}
/* If SID is a constant value, return the underlying tree constant.
Otherwise, return NULL_TREE. */
tree
region_model::maybe_get_constant (svalue_id sid) const
{
gcc_assert (!sid.null_p ());
svalue *sval = get_svalue (sid);
return sval->maybe_get_constant ();
}
/* Create a new child region of the heap (creating the heap region if
necessary).
Return the region_id of the new child region. */
region_id
region_model::add_new_malloc_region ()
{
region_id heap_rid
= get_root_region ()->ensure_heap_region (this);
return add_region (new symbolic_region (heap_rid, NULL_TREE, true));
}
/* Attempt to return a tree that represents SID, or return NULL_TREE. */
tree
region_model::get_representative_tree (svalue_id sid) const
{
if (sid.null_p ())
return NULL_TREE;
/* Find the first region that stores the value (e.g. a local) and
generate a representative tree for it. */
unsigned i;
region *region;
FOR_EACH_VEC_ELT (m_regions, i, region)
if (sid == region->get_value_direct ())
{
path_var pv = get_representative_path_var (region_id::from_int (i));
if (pv.m_tree)
return pv.m_tree;
}
/* Handle string literals and various other pointers. */
svalue *sval = get_svalue (sid);
if (region_svalue *ptr_sval = sval->dyn_cast_region_svalue ())
{
region_id rid = ptr_sval->get_pointee ();
path_var pv = get_representative_path_var (rid);
if (pv.m_tree)
return build1 (ADDR_EXPR,
TREE_TYPE (sval->get_type ()),
pv.m_tree);
}
return maybe_get_constant (sid);
}
/* Attempt to return a path_var that represents the region, or return
the NULL path_var.
For example, a region for a field of a local would be a path_var
wrapping a COMPONENT_REF. */
path_var
region_model::get_representative_path_var (region_id rid) const
{
region *reg = get_region (rid);
region *parent_reg = get_region (reg->get_parent ());
region_id stack_rid = get_stack_region_id ();
if (!stack_rid.null_p ())
if (parent_reg && parent_reg->get_parent () == stack_rid)
{
frame_region *parent_frame = (frame_region *)parent_reg;
tree t = parent_frame->get_tree_for_child_region (rid);
return path_var (t, parent_frame->get_depth ());
}
if (reg->get_parent () == get_globals_region_id ())
{
map_region *globals = get_root_region ()->get_globals_region (this);
if (globals)
return path_var (globals->get_tree_for_child_region (rid), -1);
}
/* Handle e.g. fields of a local by recursing. */
region_id parent_rid = reg->get_parent ();
if (parent_reg)
{
if (reg->is_view_p ())
{
path_var parent_pv = get_representative_path_var (parent_rid);
if (parent_pv.m_tree && reg->get_type ())
return path_var (build1 (NOP_EXPR,
reg->get_type (),
parent_pv.m_tree),
parent_pv.m_stack_depth);
}
if (parent_reg->get_kind () == RK_STRUCT)
{
map_region *parent_map_region = (map_region *)parent_reg;
/* This can fail if we have a view, rather than a field. */
if (tree child_key
= parent_map_region->get_tree_for_child_region (rid))
{
path_var parent_pv = get_representative_path_var (parent_rid);
if (parent_pv.m_tree && TREE_CODE (child_key) == FIELD_DECL)
return path_var (build3 (COMPONENT_REF,
TREE_TYPE (child_key),
parent_pv.m_tree, child_key,
NULL_TREE),
parent_pv.m_stack_depth);
}
}
/* Handle elements within an array. */
if (array_region *array_reg = parent_reg->dyn_cast_array_region ())
{
array_region::key_t key;
if (array_reg->get_key_for_child_region (rid, &key))
{
path_var parent_pv = get_representative_path_var (parent_rid);
if (parent_pv.m_tree && reg->get_type ())
{
tree index = array_reg->constant_from_key (key);
return path_var (build4 (ARRAY_REF,
reg->get_type (),
parent_pv.m_tree, index,
NULL_TREE, NULL_TREE),
parent_pv.m_stack_depth);
}
}
}
}
/* Handle string literals. */
svalue_id sid = reg->get_value_direct ();
if (svalue *sval = get_svalue (sid))
if (tree cst = sval->maybe_get_constant ())
if (TREE_CODE (cst) == STRING_CST)
return path_var (cst, 0);
return path_var (NULL_TREE, 0);
}
/* Locate all regions that directly have value SID and append representative
path_var instances for them into *OUT. */
void
region_model::get_path_vars_for_svalue (svalue_id sid, vec<path_var> *out) const
{
unsigned i;
region *region;
FOR_EACH_VEC_ELT (m_regions, i, region)
if (sid == region->get_value_direct ())
{
path_var pv = get_representative_path_var (region_id::from_int (i));
if (pv.m_tree)
out->safe_push (pv);
}
}
/* Set DST_RID value to be a new unknown value of type TYPE. */
svalue_id
region_model::set_to_new_unknown_value (region_id dst_rid, tree type,
region_model_context *ctxt)
{
gcc_assert (!dst_rid.null_p ());
svalue_id new_sid = add_svalue (new unknown_svalue (type));
set_value (dst_rid, new_sid, ctxt);
// TODO: presumably purge all child regions too (but do this in set_value?)
return new_sid;
}
/* Update this model for any phis in SNODE, assuming we came from
LAST_CFG_SUPEREDGE. */
void
region_model::update_for_phis (const supernode *snode,
const cfg_superedge *last_cfg_superedge,
region_model_context *ctxt)
{
gcc_assert (last_cfg_superedge);
for (gphi_iterator gpi = const_cast<supernode *>(snode)->start_phis ();
!gsi_end_p (gpi); gsi_next (&gpi))
{
gphi *phi = gpi.phi ();
tree src = last_cfg_superedge->get_phi_arg (phi);
tree lhs = gimple_phi_result (phi);
/* Update next_state based on phi. */
bool is_back_edge = last_cfg_superedge->back_edge_p ();
handle_phi (phi, lhs, src, is_back_edge, ctxt);
}
}
/* Attempt to update this model for taking EDGE (where the last statement
was LAST_STMT), returning true if the edge can be taken, false
otherwise.
For CFG superedges where LAST_STMT is a conditional or a switch
statement, attempt to add the relevant conditions for EDGE to this
model, returning true if they are feasible, or false if they are
impossible.
For call superedges, push frame information and store arguments
into parameters.
For return superedges, pop frame information and store return
values into any lhs.
Rejection of call/return superedges happens elsewhere, in
program_point::on_edge (i.e. based on program point, rather
than program state). */
bool
region_model::maybe_update_for_edge (const superedge &edge,
const gimple *last_stmt,
region_model_context *ctxt)
{
/* Handle frame updates for interprocedural edges. */
switch (edge.m_kind)
{
default:
break;
case SUPEREDGE_CALL:
{
const call_superedge *call_edge = as_a <const call_superedge *> (&edge);
update_for_call_superedge (*call_edge, ctxt);
}
break;
case SUPEREDGE_RETURN:
{
const return_superedge *return_edge
= as_a <const return_superedge *> (&edge);
update_for_return_superedge (*return_edge, ctxt);
}
break;
case SUPEREDGE_INTRAPROCEDURAL_CALL:
{
const callgraph_superedge *cg_sedge
= as_a <const callgraph_superedge *> (&edge);
update_for_call_summary (*cg_sedge, ctxt);
}
break;
}
if (last_stmt == NULL)
return true;
/* Apply any constraints for conditionals/switch statements. */
if (const gcond *cond_stmt = dyn_cast <const gcond *> (last_stmt))
{
const cfg_superedge *cfg_sedge = as_a <const cfg_superedge *> (&edge);
return apply_constraints_for_gcond (*cfg_sedge, cond_stmt, ctxt);
}
if (const gswitch *switch_stmt = dyn_cast <const gswitch *> (last_stmt))
{
const switch_cfg_superedge *switch_sedge
= as_a <const switch_cfg_superedge *> (&edge);
return apply_constraints_for_gswitch (*switch_sedge, switch_stmt, ctxt);
}
return true;
}
/* Push a new frame_region on to the stack region.
Populate the frame_region with child regions for the function call's
parameters, using values from the arguments at the callsite in the
caller's frame. */
void
region_model::update_for_call_superedge (const call_superedge &call_edge,
region_model_context *ctxt)
{
/* Build a vec of argument svalue_id, using the current top
frame for resolving tree expressions. */
const gcall *call_stmt = call_edge.get_call_stmt ();
auto_vec<svalue_id> arg_sids (gimple_call_num_args (call_stmt));
for (unsigned i = 0; i < gimple_call_num_args (call_stmt); i++)
{
tree arg = gimple_call_arg (call_stmt, i);
arg_sids.quick_push (get_rvalue (arg, ctxt));
}
push_frame (call_edge.get_callee_function (), &arg_sids, ctxt);
}
/* Pop the top-most frame_region from the stack, and copy the return
region's values (if any) into the region for the lvalue of the LHS of
the call (if any). */
void
region_model::update_for_return_superedge (const return_superedge &return_edge,
region_model_context *ctxt)
{
region_id stack_rid = get_stack_region_id ();
stack_region *stack = get_region <stack_region> (stack_rid);
/* Get the region for the result of the call, within the caller frame. */
region_id result_dst_rid;
const gcall *call_stmt = return_edge.get_call_stmt ();
tree lhs = gimple_call_lhs (call_stmt);
if (lhs)
{
/* Normally we access the top-level frame, which is:
path_var (expr, stack->get_num_frames () - 1)
whereas here we need the caller frame, hence "- 2" here. */
gcc_assert (stack->get_num_frames () >= 2);
result_dst_rid = get_lvalue (path_var (lhs, stack->get_num_frames () - 2),
ctxt);
}
purge_stats stats;
stack->pop_frame (this, result_dst_rid, true, &stats, ctxt);
// TODO: do something with the stats?
if (!lhs)
{
/* This could be a leak; try purging again, but this time,
don't special-case the result sids (as was done in pop_frame). */
purge_unused_svalues (&stats, ctxt);
}
}
/* Update this region_model with a summary of the effect of calling
and returning from CG_SEDGE.
TODO: Currently this is extremely simplistic: we merely set the
return value to "unknown". A proper implementation would e.g. update
sm-state, and presumably be reworked to support multiple outcomes. */
void
region_model::update_for_call_summary (const callgraph_superedge &cg_sedge,
region_model_context *ctxt)
{
/* For now, set any return value to "unknown". */
const gcall *call_stmt = cg_sedge.get_call_stmt ();
tree lhs = gimple_call_lhs (call_stmt);
if (lhs)
set_to_new_unknown_value (get_lvalue (lhs, ctxt), TREE_TYPE (lhs), ctxt);
// TODO: actually implement some kind of summary here
}
/* Given a true or false edge guarded by conditional statement COND_STMT,
determine appropriate constraints for the edge to be taken.
If they are feasible, add the constraints and return true.
Return false if the constraints contradict existing knowledge
(and so the edge should not be taken). */
bool
region_model::apply_constraints_for_gcond (const cfg_superedge &sedge,
const gcond *cond_stmt,
region_model_context *ctxt)
{
::edge cfg_edge = sedge.get_cfg_edge ();
gcc_assert (cfg_edge != NULL);
gcc_assert (cfg_edge->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE));
enum tree_code op = gimple_cond_code (cond_stmt);
tree lhs = gimple_cond_lhs (cond_stmt);
tree rhs = gimple_cond_rhs (cond_stmt);
if (cfg_edge->flags & EDGE_FALSE_VALUE)
op = invert_tree_comparison (op, false /* honor_nans */);
return add_constraint (lhs, op, rhs, ctxt);
}
/* Given an EDGE guarded by SWITCH_STMT, determine appropriate constraints
for the edge to be taken.
If they are feasible, add the constraints and return true.
Return false if the constraints contradict existing knowledge
(and so the edge should not be taken). */
bool
region_model::apply_constraints_for_gswitch (const switch_cfg_superedge &edge,
const gswitch *switch_stmt,
region_model_context *ctxt)
{
tree index = gimple_switch_index (switch_stmt);
tree case_label = edge.get_case_label ();
gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
tree lower_bound = CASE_LOW (case_label);
tree upper_bound = CASE_HIGH (case_label);
if (lower_bound)
{
if (upper_bound)
{
/* Range. */
if (!add_constraint (index, GE_EXPR, lower_bound, ctxt))
return false;
return add_constraint (index, LE_EXPR, upper_bound, ctxt);
}
else
/* Single-value. */
return add_constraint (index, EQ_EXPR, lower_bound, ctxt);
}
else
{
/* The default case.
Add exclusions based on the other cases. */
for (unsigned other_idx = 1;
other_idx < gimple_switch_num_labels (switch_stmt);
other_idx++)
{
tree other_label = gimple_switch_label (switch_stmt,
other_idx);
tree other_lower_bound = CASE_LOW (other_label);
tree other_upper_bound = CASE_HIGH (other_label);
gcc_assert (other_lower_bound);
if (other_upper_bound)
{
/* Exclude this range-valued case.
For now, we just exclude the boundary values.
TODO: exclude the values within the region. */
if (!add_constraint (index, NE_EXPR, other_lower_bound, ctxt))
return false;
if (!add_constraint (index, NE_EXPR, other_upper_bound, ctxt))
return false;
}
else
/* Exclude this single-valued case. */
if (!add_constraint (index, NE_EXPR, other_lower_bound, ctxt))
return false;
}
return true;
}
}
/* Get the root_region within this model (guaranteed to be non-null). */
root_region *
region_model::get_root_region () const
{
return get_region<root_region> (m_root_rid);
}
/* Get the region_id of this model's stack region (if any). */
region_id
region_model::get_stack_region_id () const
{
return get_root_region ()->get_stack_region_id ();
}
/* Create a new frame_region for a call to FUN and push it onto
the stack.
If ARG_SIDS is non-NULL, use it to populate the parameters
in the new frame.
Otherwise, populate them with unknown values.
Return the region_id of the new frame_region. */
region_id
region_model::push_frame (function *fun, vec<svalue_id> *arg_sids,
region_model_context *ctxt)
{
return get_root_region ()->push_frame (this, fun, arg_sids, ctxt);
}
/* Get the region_id of the top-most frame in this region_model's stack,
if any. */
region_id
region_model::get_current_frame_id () const
{
return get_root_region ()->get_current_frame_id (*this);
}
/* Get the function of the top-most frame in this region_model's stack.
There must be such a frame. */
function *
region_model::get_current_function () const
{
region_id frame_id = get_current_frame_id ();
frame_region *frame = get_region<frame_region> (frame_id);
return frame->get_function ();
}
/* Pop the topmost frame_region from this region_model's stack;
see the comment for stack_region::pop_frame. */
void
region_model::pop_frame (region_id result_dst_rid,
bool purge, purge_stats *out,
region_model_context *ctxt)
{
get_root_region ()->pop_frame (this, result_dst_rid, purge, out, ctxt);
}
/* Get the number of frames in this region_model's stack. */
int
region_model::get_stack_depth () const
{
stack_region *stack = get_root_region ()->get_stack_region (this);
if (stack)
return stack->get_num_frames ();
else
return 0;
}
/* Get the function * at DEPTH within the call stack. */
function *
region_model::get_function_at_depth (unsigned depth) const
{
stack_region *stack = get_root_region ()->get_stack_region (this);
gcc_assert (stack);
region_id frame_rid = stack->get_frame_rid (depth);
frame_region *frame = get_region <frame_region> (frame_rid);
return frame->get_function ();
}
/* Get the region_id of this model's globals region (if any). */
region_id
region_model::get_globals_region_id () const
{
return get_root_region ()->get_globals_region_id ();
}
/* Add SVAL to this model, taking ownership, and returning its new
svalue_id. */
svalue_id
region_model::add_svalue (svalue *sval)
{
gcc_assert (sval);
m_svalues.safe_push (sval);
return svalue_id::from_int (m_svalues.length () - 1);
}
/* Change the meaning of SID to be NEW_SVAL
(e.g. when deferencing an unknown pointer, the pointer
becomes a pointer to a symbolic region, so that all users
of the former unknown pointer are now effectively pointing
at the same region). */
void
region_model::replace_svalue (svalue_id sid, svalue *new_sval)
{
gcc_assert (!sid.null_p ());
int idx = sid.as_int ();
gcc_assert (m_svalues[idx]);
gcc_assert (m_svalues[idx]->get_type () == new_sval->get_type ());
delete m_svalues[idx];
m_svalues[idx] = new_sval;
}
/* Add region R to this model, taking ownership, and returning its new
region_id. */
region_id
region_model::add_region (region *r)
{
gcc_assert (r);
m_regions.safe_push (r);
return region_id::from_int (m_regions.length () - 1);
}
/* Return the svalue with id SVAL_ID, or NULL for a null id. */
svalue *
region_model::get_svalue (svalue_id sval_id) const
{
if (sval_id.null_p ())
return NULL;
return m_svalues[sval_id.as_int ()];
}
/* Return the region with id RID, or NULL for a null id. */
region *
region_model::get_region (region_id rid) const
{
if (rid.null_p ())
return NULL;
return m_regions[rid.as_int ()];
}
/* Make a region of an appropriate subclass for TYPE,
with parent PARENT_RID, or return NULL for types we don't yet know
how to handle. */
static region *
make_region_for_type (region_id parent_rid, tree type)
{
gcc_assert (TYPE_P (type));
if (INTEGRAL_TYPE_P (type)
|| SCALAR_FLOAT_TYPE_P (type)
|| POINTER_TYPE_P (type)
|| TREE_CODE (type) == COMPLEX_TYPE
|| TREE_CODE (type) == VECTOR_TYPE)
return new primitive_region (parent_rid, type);
if (TREE_CODE (type) == RECORD_TYPE)
return new struct_region (parent_rid, type);
if (TREE_CODE (type) == ARRAY_TYPE)
return new array_region (parent_rid, type);
if (TREE_CODE (type) == UNION_TYPE)
return new union_region (parent_rid, type);
if (FUNC_OR_METHOD_TYPE_P (type))
return new function_region (parent_rid, type);
/* If we have a void *, make a new symbolic region. */
if (VOID_TYPE_P (type))
return new symbolic_region (parent_rid, type, false);
return NULL;
}
/* Add a region with type TYPE and parent PARENT_RID. */
region_id
region_model::add_region_for_type (region_id parent_rid, tree type,
region_model_context *ctxt)
{
if (type)
{
gcc_assert (TYPE_P (type));
if (region *new_region = make_region_for_type (parent_rid, type))
return add_region (new_region);
}
/* If we can't handle TYPE, return a placeholder region, and stop
exploring this path. */
return make_region_for_unexpected_tree_code (ctxt, type,
dump_location_t ());
}
/* Helper class for region_model::purge_unused_svalues. */
class restrict_to_used_svalues : public purge_criteria
{
public:
restrict_to_used_svalues (const auto_sbitmap &used) : m_used (used) {}
bool should_purge_p (svalue_id sid) const FINAL OVERRIDE
{
gcc_assert (!sid.null_p ());
return !bitmap_bit_p (m_used, sid.as_int ());
}
private:
const auto_sbitmap &m_used;
};
/* Remove unused svalues from this model, accumulating stats into STATS.
Unused svalues are deleted. Doing so could reorder the svalues, and
thus change the meaning of svalue_ids.
If CTXT is non-NULL, then it is notified about svalue_id remappings,
and about svalue_ids that are about to be deleted. This allows e.g.
for warning about resource leaks, for the case where the svalue
represents a resource handle in the user code (e.g. a FILE * or a malloc
buffer).
Amongst other things, removing unused svalues is important for ensuring
that the analysis of loops terminates. Otherwise, we could generate a
succession of models with unreferenced "unknown" values, where the
number of redundant unknown values could grow without bounds, and each
such model would be treated as distinct.
If KNOWN_USED_SIDS is non-NULL, treat *KNOWN_USED_SIDS as used (this is for
handling values being returned from functions as their frame is popped,
since otherwise we'd have to simultaneously determine both the rvalue
of the return expr in the callee frame and the lvalue for the gcall's
assignment in the caller frame, and it seems cleaner to express all
lvalue and rvalue lookups implicitly relative to a "current" frame).
The svalue_ids in *KNOWN_USED_SIDS are not remapped and hence this
call makes it invalid. */
void
region_model::purge_unused_svalues (purge_stats *stats,
region_model_context *ctxt,
svalue_id_set *known_used_sids)
{
// TODO: might want to avoid a vfunc call just to do logging here:
logger *logger = ctxt ? ctxt->get_logger () : NULL;
LOG_SCOPE (logger);
auto_sbitmap used (m_svalues.length ());
bitmap_clear (used);
if (known_used_sids)
{
/* We can't use an sbitmap for known_used_sids as the number of
svalues could have grown since it was created. */
for (unsigned i = 0; i < get_num_svalues (); i++)
if (known_used_sids->svalue_p (svalue_id::from_int (i)))
bitmap_set_bit (used, i);
}
/* Walk the regions, marking sids that are used. */
unsigned i;
region *r;
FOR_EACH_VEC_ELT (m_regions, i, r)
{
svalue_id sid = r->get_value_direct ();
if (!sid.null_p ())
bitmap_set_bit (used, sid.as_int ());
}
/* Now purge any constraints involving svalues we don't care about. */
restrict_to_used_svalues criterion (used);
m_constraints->purge (criterion, stats);
/* Mark any sids that are in constraints that survived. */
{
equiv_class *ec;
FOR_EACH_VEC_ELT (m_constraints->m_equiv_classes, i, ec)
{
int j;
svalue_id *sid;
FOR_EACH_VEC_ELT (ec->m_vars, j, sid)
{
gcc_assert (!sid->null_p ());
bitmap_set_bit (used, sid->as_int ());
}
}
}
/* Build a mapping from old-sid to new-sid so that we can preserve
order of the used IDs and move all redundant ones to the end.
Iterate though svalue IDs, adding used ones to the front of
the new list, and unused ones to the back. */
svalue_id_map map (m_svalues.length ());
int next_used_new_sid = 0;
int after_next_unused_new_sid = m_svalues.length ();
for (unsigned i = 0; i < m_svalues.length (); i++)
{
svalue_id src (svalue_id::from_int (i));
if (bitmap_bit_p (used, i))
{
if (logger)
logger->log ("sv%i is used", i);
map.put (src, svalue_id::from_int (next_used_new_sid++));
}
else
{
if (logger)
logger->log ("sv%i is unused", i);
map.put (src, svalue_id::from_int (--after_next_unused_new_sid));
}
}
/* The two insertion points should have met. */
gcc_assert (next_used_new_sid == after_next_unused_new_sid);
/* Now walk the regions and the constraints, remapping sids,
so that all the redundant svalues are at the end. */
remap_svalue_ids (map);
if (logger)
{
logger->start_log_line ();
logger->log_partial ("map: ");
map.dump_to_pp (logger->get_printer ());
logger->end_log_line ();
}
/* Notify any client about the remapping and pending deletion.
Potentially this could trigger leak warnings. */
if (ctxt)
{
ctxt->remap_svalue_ids (map);
int num_client_items_purged
= ctxt->on_svalue_purge (svalue_id::from_int (next_used_new_sid), map);
if (stats)
stats->m_num_client_items += num_client_items_purged;
}
/* Drop the redundant svalues from the end of the vector. */
while ((signed)m_svalues.length () > next_used_new_sid)
{
if (logger)
{
svalue_id victim = svalue_id::from_int (m_svalues.length () - 1);
logger->log ("deleting sv%i (was sv%i)",
victim.as_int (),
map.get_src_for_dst (victim).as_int ());
}
delete m_svalues.pop ();
if (stats)
stats->m_num_svalues++;
}
validate ();
}
/* Renumber the svalues within this model according to MAP. */
void
region_model::remap_svalue_ids (const svalue_id_map &map)
{
/* Update IDs within regions. */
unsigned i;
region *r;
FOR_EACH_VEC_ELT (m_regions, i, r)
r->remap_svalue_ids (map);
/* Update IDs within ECs within constraints. */
m_constraints->remap_svalue_ids (map);
/* Build a reordered svalues vector. */
auto_vec<svalue *> new_svalues (m_svalues.length ());
for (unsigned i = 0; i < m_svalues.length (); i++)
{
svalue_id dst (svalue_id::from_int (i));
svalue_id src = map.get_src_for_dst (dst);
new_svalues.quick_push (get_svalue (src));
}
/* Copy over the reordered vec to m_svalues. */
m_svalues.truncate (0);
gcc_assert (m_svalues.space (new_svalues.length ()));
svalue *sval;
FOR_EACH_VEC_ELT (new_svalues, i, sval)
m_svalues.quick_push (sval);
}
/* Renumber the regions within this model according to MAP. */
void
region_model::remap_region_ids (const region_id_map &map)
{
/* Update IDs within regions. */
unsigned i;
region *r;
FOR_EACH_VEC_ELT (m_regions, i, r)
r->remap_region_ids (map);
/* Update IDs within svalues. */
svalue *sval;
FOR_EACH_VEC_ELT (m_svalues, i, sval)
sval->remap_region_ids (map);
/* Build a reordered regions vector. */
auto_vec<region *> new_regions (m_regions.length ());
for (unsigned i = 0; i < m_regions.length (); i++)
{
region_id dst (region_id::from_int (i));
region_id src = map.get_src_for_dst (dst);
new_regions.quick_push (get_region (src));
}
/* Copy over the reordered vec to m_regions. */
m_regions.truncate (0);
gcc_assert (m_regions.space (new_regions.length ()));
FOR_EACH_VEC_ELT (new_regions, i, r)
m_regions.quick_push (r);
}
/* Delete all regions within SET_TO_PURGE, remapping region IDs for
other regions. It's required that there are no uses of the
regions within the set (or the region IDs will become invalid).
Accumulate stats to STATS. */
void
region_model::purge_regions (const region_id_set &set_to_purge,
purge_stats *stats,
logger *)
{
/* Build a mapping from old-rid to new-rid so that we can preserve
order of the used IDs and move all redundant ones to the end.
Iterate though region IDs, adding used ones to the front of
the new list, and unused ones to the back. */
region_id_map map (m_regions.length ());
int next_used_new_rid = 0;
int after_next_unused_new_rid = m_regions.length ();
for (unsigned i = 0; i < m_regions.length (); i++)
{
region_id src (region_id::from_int (i));
if (set_to_purge.region_p (src))
map.put (src, region_id::from_int (--after_next_unused_new_rid));
else
map.put (src, region_id::from_int (next_used_new_rid++));
}
/* The two insertion points should have met. */
gcc_assert (next_used_new_rid == after_next_unused_new_rid);
/* Now walk the regions and svalues, remapping rids,
so that all the redundant regions are at the end. */
remap_region_ids (map);
/* Drop the redundant regions from the end of the vector. */
while ((signed)m_regions.length () > next_used_new_rid)
{
delete m_regions.pop ();
if (stats)
stats->m_num_regions++;
}
}
/* Populate *OUT with RID and all of its descendents.
If EXCLUDE_RID is non-null, then don't add it or its descendents. */
void
region_model::get_descendents (region_id rid, region_id_set *out,
region_id exclude_rid) const
{
out->add_region (rid);
bool changed = true;
while (changed)
{
changed = false;
unsigned i;
region *r;
FOR_EACH_VEC_ELT (m_regions, i, r)
{
region_id iter_rid = region_id::from_int (i);
if (iter_rid == exclude_rid)
continue;
if (!out->region_p (iter_rid))
{
region_id parent_rid = r->get_parent ();
if (!parent_rid.null_p ())
if (out->region_p (parent_rid))
{
out->add_region (iter_rid);
changed = true;
}
}
}
}
}
/* Delete RID and all descendent regions.
Find any pointers to such regions; convert them to
poisoned values of kind PKIND.
Accumulate stats on purged entities into STATS. */
void
region_model::delete_region_and_descendents (region_id rid,
enum poison_kind pkind,
purge_stats *stats,
logger *logger)
{
/* Find all child and descendent regions. */
region_id_set descendents (this);
get_descendents (rid, &descendents, region_id::null ());
/* Find any pointers to such regions; convert to poisoned. */
poison_any_pointers_to_bad_regions (descendents, pkind);
/* Delete all such regions. */
purge_regions (descendents, stats, logger);
}
/* Find any pointers to regions within BAD_REGIONS; convert them to
poisoned values of kind PKIND. */
void
region_model::poison_any_pointers_to_bad_regions (const region_id_set &
bad_regions,
enum poison_kind pkind)
{
int i;
svalue *sval;
FOR_EACH_VEC_ELT (m_svalues, i, sval)
if (region_svalue *ptr_sval = sval->dyn_cast_region_svalue ())
{
region_id ptr_dst = ptr_sval->get_pointee ();
if (!ptr_dst.null_p ())
if (bad_regions.region_p (ptr_dst))
replace_svalue
(svalue_id::from_int (i),
new poisoned_svalue (pkind, sval->get_type ()));
}
}
/* Attempt to merge THIS with OTHER_MODEL, writing the result
to OUT_MODEL, and populating SID_MAPPING. */
bool
region_model::can_merge_with_p (const region_model &other_model,
region_model *out_model,
svalue_id_merger_mapping *sid_mapping) const
{
gcc_assert (m_root_rid == other_model.m_root_rid);
gcc_assert (m_root_rid.as_int () == 0);
gcc_assert (sid_mapping);
gcc_assert (out_model);
model_merger merger (this, &other_model, out_model, sid_mapping);
if (!root_region::can_merge_p (get_root_region (),
other_model.get_root_region (),
out_model->get_root_region (),
&merger))
return false;
/* Merge constraints. */
constraint_manager::merge (*m_constraints,
*other_model.m_constraints,
out_model->m_constraints,
merger);
out_model->validate ();
/* The merged model should be simpler (or as simple) as the inputs. */
#if 0
gcc_assert (out_model->m_svalues.length () <= m_svalues.length ());
gcc_assert (out_model->m_svalues.length ()
<= other_model.m_svalues.length ());
#endif
gcc_assert (out_model->m_regions.length () <= m_regions.length ());
gcc_assert (out_model->m_regions.length ()
<= other_model.m_regions.length ());
// TODO: same, for constraints
return true;
}
/* As above, but supply a placeholder svalue_id_merger_mapping
instance to be used and receive output. For use in selftests. */
bool
region_model::can_merge_with_p (const region_model &other_model,
region_model *out_model) const
{
svalue_id_merger_mapping sid_mapping (*this, other_model);
return can_merge_with_p (other_model, out_model, &sid_mapping);
}
/* For debugging purposes: look for a region within this region_model
for a decl named NAME (or an SSA_NAME for such a decl),
returning its value, or svalue_id::null if none are found. */
svalue_id
region_model::get_value_by_name (const char *name) const
{
gcc_assert (name);
tree identifier = get_identifier (name);
return get_root_region ()->get_value_by_name (identifier, *this);
}
/* Generate or reuse an svalue_id within this model for an index
into an array of type PTR_TYPE, based on OFFSET_SID. */
svalue_id
region_model::convert_byte_offset_to_array_index (tree ptr_type,
svalue_id offset_sid)
{
gcc_assert (POINTER_TYPE_P (ptr_type));
if (tree offset_cst = maybe_get_constant (offset_sid))
{
tree elem_type = TREE_TYPE (ptr_type);
/* Arithmetic on void-pointers is a GNU C extension, treating the size
of a void as 1.
https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html */
if (TREE_CODE (elem_type) == VOID_TYPE)
return offset_sid;
/* First, use int_size_in_bytes, to reject the case where we have an
incomplete type, or a non-constant value. */
HOST_WIDE_INT hwi_byte_size = int_size_in_bytes (elem_type);
if (hwi_byte_size > 0)
{
/* Now call size_in_bytes to get the answer in tree form. */
tree byte_size = size_in_bytes (elem_type);
gcc_assert (byte_size);
/* Try to get a constant by dividing, ensuring that we're in a
signed representation first. */
tree index
= fold_binary (TRUNC_DIV_EXPR, ssizetype,
fold_convert (ssizetype, offset_cst),
fold_convert (ssizetype, byte_size));
if (index && TREE_CODE (index) == INTEGER_CST)
return get_or_create_constant_svalue (index);
}
}
/* Otherwise, we don't know the array index; generate a new unknown value.
TODO: do we need to capture the relationship between two unknown
values (the offset and the index)? */
return add_svalue (new unknown_svalue (integer_type_node));
}
/* Get a region of type TYPE for PTR_SID[OFFSET_SID/sizeof (*PTR_SID)].
If OFFSET_SID is known to be zero, then dereference PTR_SID.
Otherwise, impose a view of "typeof(*PTR_SID)[]" on *PTR_SID,
and then get a view of type TYPE on the relevant array element. */
region_id
region_model::get_or_create_mem_ref (tree type,
svalue_id ptr_sid,
svalue_id offset_sid,
region_model_context *ctxt)
{
svalue *ptr_sval = get_svalue (ptr_sid);
tree ptr_type = ptr_sval->get_type ();
gcc_assert (ptr_type);
region_id raw_rid = deref_rvalue (ptr_sid, ctxt);
svalue *offset_sval = get_svalue (offset_sid);
tree offset_type = offset_sval->get_type ();
gcc_assert (offset_type);
if (constant_svalue *cst_sval = offset_sval->dyn_cast_constant_svalue ())
{
if (zerop (cst_sval->get_constant ()))
{
/* Handle the zero offset case. */
return get_or_create_view (raw_rid, type, ctxt);
}
/* If we're already within an array of the correct type,
then we want to reuse that array, rather than starting
a new view.
If so, figure out our raw_rid's offset from its parent,
if we can, and use that to offset OFFSET_SID, and create
the element within the parent region. */
region *raw_reg = get_region (raw_rid);
region_id parent_rid = raw_reg->get_parent ();
tree parent_type = get_region (parent_rid)->get_type ();
if (parent_type
&& TREE_CODE (parent_type) == ARRAY_TYPE)
{
// TODO: check we have the correct parent type
array_region *parent_array = get_region <array_region> (parent_rid);
array_region::key_t key_for_raw_rid;
if (parent_array->get_key_for_child_region (raw_rid,
&key_for_raw_rid))
{
/* Convert from offset to index. */
svalue_id index_sid
= convert_byte_offset_to_array_index (ptr_type, offset_sid);
if (tree index_cst
= get_svalue (index_sid)->maybe_get_constant ())
{
array_region::key_t index_offset
= array_region::key_from_constant (index_cst);
array_region::key_t index_rel_to_parent
= key_for_raw_rid + index_offset;
tree index_rel_to_parent_cst
= wide_int_to_tree (integer_type_node,
index_rel_to_parent);
svalue_id index_sid
= get_or_create_constant_svalue (index_rel_to_parent_cst);
/* Carry on, using the parent region and adjusted index. */
region_id element_rid
= parent_array->get_element (this, raw_rid, index_sid,
ctxt);
return get_or_create_view (element_rid, type, ctxt);
}
}
}
}
tree array_type = build_array_type (TREE_TYPE (ptr_type),
integer_type_node);
region_id array_view_rid = get_or_create_view (raw_rid, array_type, ctxt);
array_region *array_reg = get_region <array_region> (array_view_rid);
svalue_id index_sid
= convert_byte_offset_to_array_index (ptr_type, offset_sid);
region_id element_rid
= array_reg->get_element (this, array_view_rid, index_sid, ctxt);
return get_or_create_view (element_rid, type, ctxt);
}
/* Get a region of type TYPE for PTR_SID + OFFSET_SID.
If OFFSET_SID is known to be zero, then dereference PTR_SID.
Otherwise, impose a view of "typeof(*PTR_SID)[]" on *PTR_SID,
and then get a view of type TYPE on the relevant array element. */
region_id
region_model::get_or_create_pointer_plus_expr (tree type,
svalue_id ptr_sid,
svalue_id offset_in_bytes_sid,
region_model_context *ctxt)
{
return get_or_create_mem_ref (type,
ptr_sid,
offset_in_bytes_sid,
ctxt);
}
/* Get or create a view of type TYPE of the region with id RAW_ID.
Return the id of the view (or RAW_ID if it of the same type). */
region_id
region_model::get_or_create_view (region_id raw_rid, tree type,
region_model_context *ctxt)
{
region *raw_region = get_region (raw_rid);
gcc_assert (TYPE_P (type));
if (type != raw_region->get_type ())
{
/* If the region already has a view of the requested type,
reuse it. */
region_id existing_view_rid = raw_region->get_view (type, this);
if (!existing_view_rid.null_p ())
return existing_view_rid;
/* Otherwise, make one (adding it to the region_model and
to the viewed region). */
region_id view_rid = add_region_for_type (raw_rid, type, ctxt);
raw_region->add_view (view_rid, this);
// TODO: something to signify that this is a "view"
return view_rid;
}
return raw_rid;
}
/* Attempt to get the fndecl used at CALL, if known, or NULL_TREE
otherwise. */
tree
region_model::get_fndecl_for_call (const gcall *call,
region_model_context *ctxt)
{
tree fn_ptr = gimple_call_fn (call);
if (fn_ptr == NULL_TREE)
return NULL_TREE;
svalue_id fn_ptr_sid = get_rvalue (fn_ptr, ctxt);
svalue *fn_ptr_sval = get_svalue (fn_ptr_sid);
if (region_svalue *fn_ptr_ptr = fn_ptr_sval->dyn_cast_region_svalue ())
{
region_id fn_rid = fn_ptr_ptr->get_pointee ();
code_region *code = get_root_region ()->get_code_region (this);
if (code)
{
tree fn_decl = code->get_tree_for_child_region (fn_rid);
if (!fn_decl)
return NULL_TREE;
cgraph_node *node = cgraph_node::get (fn_decl);
if (!node)
return NULL_TREE;
const cgraph_node *ultimate_node = node->ultimate_alias_target ();
if (ultimate_node)
return ultimate_node->decl;
}
}
return NULL_TREE;
}
/* struct model_merger. */
/* Dump a multiline representation of this merger to PP. */
void
model_merger::dump_to_pp (pretty_printer *pp) const
{
pp_string (pp, "model A:");
pp_newline (pp);
m_model_a->dump_to_pp (pp, false);
pp_newline (pp);
pp_string (pp, "model B:");
pp_newline (pp);
m_model_b->dump_to_pp (pp, false);
pp_newline (pp);
pp_string (pp, "merged model:");
pp_newline (pp);
m_merged_model->dump_to_pp (pp, false);
pp_newline (pp);
pp_string (pp, "region map: model A to merged model:");
pp_newline (pp);
m_map_regions_from_a_to_m.dump_to_pp (pp);
pp_newline (pp);
pp_string (pp, "region map: model B to merged model:");
pp_newline (pp);
m_map_regions_from_b_to_m.dump_to_pp (pp);
pp_newline (pp);
m_sid_mapping->dump_to_pp (pp);
}
/* Dump a multiline representation of this merger to FILE. */
void
model_merger::dump (FILE *fp) const
{
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
pp_show_color (&pp) = pp_show_color (global_dc->printer);
pp.buffer->stream = fp;
dump_to_pp (&pp);
pp_flush (&pp);
}
/* Dump a multiline representation of this merger to stderr. */
DEBUG_FUNCTION void
model_merger::dump () const
{
dump (stderr);
}
/* Attempt to merge the svalues of SID_A and SID_B (from their
respective models), writing the id of the resulting svalue
into *MERGED_SID.
Return true if the merger is possible, false otherwise. */
bool
model_merger::can_merge_values_p (svalue_id sid_a,
svalue_id sid_b,
svalue_id *merged_sid)
{
gcc_assert (merged_sid);
svalue *sval_a = m_model_a->get_svalue (sid_a);
svalue *sval_b = m_model_b->get_svalue (sid_b);
/* If both are NULL, then the "values" are trivially mergeable. */
if (!sval_a && !sval_b)
return true;
/* If one is NULL and the other non-NULL, then the "values"
are not mergeable. */
if (!(sval_a && sval_b))
return false;
/* Have they both already been mapped to the same new svalue_id?
If so, use it. */
svalue_id sid_a_in_m
= m_sid_mapping->m_map_from_a_to_m.get_dst_for_src (sid_a);
svalue_id sid_b_in_m
= m_sid_mapping->m_map_from_b_to_m.get_dst_for_src (sid_b);
if (!sid_a_in_m.null_p ()
&& !sid_b_in_m.null_p ()
&& sid_a_in_m == sid_b_in_m)
{
*merged_sid = sid_a_in_m;
return true;
}
tree type = sval_a->get_type ();
if (type == NULL_TREE)
type = sval_b->get_type ();
/* If the values have different kinds, or are both unknown,
then merge as "unknown". */
if (sval_a->get_kind () != sval_b->get_kind ()
|| sval_a->get_kind () == SK_UNKNOWN)
{
svalue *merged_sval = new unknown_svalue (type);
*merged_sid = m_merged_model->add_svalue (merged_sval);
record_svalues (sid_a, sid_b, *merged_sid);
return true;
}
gcc_assert (sval_a->get_kind () == sval_b->get_kind ());
switch (sval_a->get_kind ())
{
default:
case SK_UNKNOWN: /* SK_UNKNOWN handled above. */
gcc_unreachable ();
case SK_REGION:
{
/* If we have two region pointers, then we can merge (possibly to
"unknown"). */
const region_svalue ®ion_sval_a = *as_a <region_svalue *> (sval_a);
const region_svalue ®ion_sval_b = *as_a <region_svalue *> (sval_b);
region_svalue::merge_values (region_sval_a, region_sval_b,
merged_sid, type,
this);
record_svalues (sid_a, sid_b, *merged_sid);
return true;
}
break;
case SK_CONSTANT:
{
/* If we have two constants, then we can merge. */
const constant_svalue &cst_sval_a = *as_a <constant_svalue *> (sval_a);
const constant_svalue &cst_sval_b = *as_a <constant_svalue *> (sval_b);
constant_svalue::merge_values (cst_sval_a, cst_sval_b,
merged_sid, this);
record_svalues (sid_a, sid_b, *merged_sid);
return true;
}
break;
case SK_POISONED:
case SK_SETJMP:
return false;
}
}
/* Record that A_RID in model A and B_RID in model B
correspond to MERGED_RID in the merged model, so
that pointers can be accurately merged. */
void
model_merger::record_regions (region_id a_rid,
region_id b_rid,
region_id merged_rid)
{
m_map_regions_from_a_to_m.put (a_rid, merged_rid);
m_map_regions_from_b_to_m.put (b_rid, merged_rid);
}
/* Record that A_SID in model A and B_SID in model B
correspond to MERGED_SID in the merged model. */
void
model_merger::record_svalues (svalue_id a_sid,
svalue_id b_sid,
svalue_id merged_sid)
{
gcc_assert (m_sid_mapping);
m_sid_mapping->m_map_from_a_to_m.put (a_sid, merged_sid);
m_sid_mapping->m_map_from_b_to_m.put (b_sid, merged_sid);
}
/* struct svalue_id_merger_mapping. */
/* svalue_id_merger_mapping's ctor. */
svalue_id_merger_mapping::svalue_id_merger_mapping (const region_model &a,
const region_model &b)
: m_map_from_a_to_m (a.get_num_svalues ()),
m_map_from_b_to_m (b.get_num_svalues ())
{
}
/* Dump a multiline representation of this to PP. */
void
svalue_id_merger_mapping::dump_to_pp (pretty_printer *pp) const
{
pp_string (pp, "svalue_id map: model A to merged model:");
pp_newline (pp);
m_map_from_a_to_m.dump_to_pp (pp);
pp_newline (pp);
pp_string (pp, "svalue_id map: model B to merged model:");
pp_newline (pp);
m_map_from_b_to_m.dump_to_pp (pp);
pp_newline (pp);
}
/* Dump a multiline representation of this to FILE. */
void
svalue_id_merger_mapping::dump (FILE *fp) const
{
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
pp_show_color (&pp) = pp_show_color (global_dc->printer);
pp.buffer->stream = fp;
dump_to_pp (&pp);
pp_flush (&pp);
}
/* Dump a multiline representation of this to stderr. */
DEBUG_FUNCTION void
svalue_id_merger_mapping::dump () const
{
dump (stderr);
}
/* struct canonicalization. */
/* canonicalization's ctor. */
canonicalization::canonicalization (const region_model &model)
: m_model (model),
m_rid_map (model.get_num_regions ()),
m_sid_map (model.get_num_svalues ()),
m_next_rid_int (0),
m_next_sid_int (0)
{
}
/* If we've not seen RID yet, assign it a canonicalized region_id,
and walk the region's svalue and then the region. */
void
canonicalization::walk_rid (region_id rid)
{
/* Stop if we've already seen RID. */
if (!m_rid_map.get_dst_for_src (rid).null_p ())
return;
region *region = m_model.get_region (rid);
if (region)
{
m_rid_map.put (rid, region_id::from_int (m_next_rid_int++));
walk_sid (region->get_value_direct ());
region->walk_for_canonicalization (this);
}
}
/* If we've not seen SID yet, assign it a canonicalized svalue_id,
and walk the svalue (and potentially regions e.g. for ptr values). */
void
canonicalization::walk_sid (svalue_id sid)
{
/* Stop if we've already seen SID. */
if (!m_sid_map.get_dst_for_src (sid).null_p ())
return;
svalue *sval = m_model.get_svalue (sid);
if (sval)
{
m_sid_map.put (sid, svalue_id::from_int (m_next_sid_int++));
/* Potentially walk regions e.g. for ptrs. */
sval->walk_for_canonicalization (this);
}
}
/* Dump a multiline representation of this to PP. */
void
canonicalization::dump_to_pp (pretty_printer *pp) const
{
pp_string (pp, "region_id map:");
pp_newline (pp);
m_rid_map.dump_to_pp (pp);
pp_newline (pp);
pp_string (pp, "svalue_id map:");
pp_newline (pp);
m_sid_map.dump_to_pp (pp);
pp_newline (pp);
}
/* Dump a multiline representation of this to FILE. */
void
canonicalization::dump (FILE *fp) const
{
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
pp_show_color (&pp) = pp_show_color (global_dc->printer);
pp.buffer->stream = fp;
dump_to_pp (&pp);
pp_flush (&pp);
}
/* Dump a multiline representation of this to stderr. */
DEBUG_FUNCTION void
canonicalization::dump () const
{
dump (stderr);
}
} // namespace ana
/* Update HSTATE with a hash of SID. */
void
inchash::add (svalue_id sid, inchash::hash &hstate)
{
hstate.add_int (sid.as_int ());
}
/* Update HSTATE with a hash of RID. */
void
inchash::add (region_id rid, inchash::hash &hstate)
{
hstate.add_int (rid.as_int ());
}
/* Dump RMODEL fully to stderr (i.e. without summarization). */
DEBUG_FUNCTION void
debug (const region_model &rmodel)
{
rmodel.dump (false);
}
namespace ana {
#if CHECKING_P
namespace selftest {
/* Build a constant tree of the given type from STR. */
static tree
build_real_cst_from_string (tree type, const char *str)
{
REAL_VALUE_TYPE real;
real_from_string (&real, str);
return build_real (type, real);
}
/* Append various "interesting" constants to OUT (e.g. NaN). */
static void
append_interesting_constants (auto_vec<tree> *out)
{
out->safe_push (build_int_cst (integer_type_node, 0));
out->safe_push (build_int_cst (integer_type_node, 42));
out->safe_push (build_int_cst (unsigned_type_node, 0));
out->safe_push (build_int_cst (unsigned_type_node, 42));
out->safe_push (build_real_cst_from_string (float_type_node, "QNaN"));
out->safe_push (build_real_cst_from_string (float_type_node, "-QNaN"));
out->safe_push (build_real_cst_from_string (float_type_node, "SNaN"));
out->safe_push (build_real_cst_from_string (float_type_node, "-SNaN"));
out->safe_push (build_real_cst_from_string (float_type_node, "0.0"));
out->safe_push (build_real_cst_from_string (float_type_node, "-0.0"));
out->safe_push (build_real_cst_from_string (float_type_node, "Inf"));
out->safe_push (build_real_cst_from_string (float_type_node, "-Inf"));
}
/* Verify that tree_cmp is a well-behaved comparator for qsort, even
if the underlying constants aren't comparable. */
static void
test_tree_cmp_on_constants ()
{
auto_vec<tree> csts;
append_interesting_constants (&csts);
/* Try sorting every triple. */
const unsigned num = csts.length ();
for (unsigned i = 0; i < num; i++)
for (unsigned j = 0; j < num; j++)
for (unsigned k = 0; k < num; k++)
{
auto_vec<tree> v (3);
v.quick_push (csts[i]);
v.quick_push (csts[j]);
v.quick_push (csts[k]);
v.qsort (tree_cmp);
}
}
/* Implementation detail of the ASSERT_CONDITION_* macros. */
void
assert_condition (const location &loc,
region_model &model,
tree lhs, tree_code op, tree rhs,
tristate expected)
{
tristate actual = model.eval_condition (lhs, op, rhs, NULL);
ASSERT_EQ_AT (loc, actual, expected);
}
/* Implementation detail of ASSERT_DUMP_TREE_EQ. */
static void
assert_dump_tree_eq (const location &loc, tree t, const char *expected)
{
auto_fix_quotes sentinel;
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
dump_tree (&pp, t);
ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
}
/* Assert that dump_tree (T) is EXPECTED. */
#define ASSERT_DUMP_TREE_EQ(T, EXPECTED) \
SELFTEST_BEGIN_STMT \
assert_dump_tree_eq ((SELFTEST_LOCATION), (T), (EXPECTED)); \
SELFTEST_END_STMT
/* Implementation detail of ASSERT_DUMP_EQ. */
static void
assert_dump_eq (const location &loc,
const region_model &model,
bool summarize,
const char *expected)
{
auto_fix_quotes sentinel;
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
model.dump_to_pp (&pp, summarize);
ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
}
/* Assert that MODEL.dump_to_pp (SUMMARIZE) is EXPECTED. */
#define ASSERT_DUMP_EQ(MODEL, SUMMARIZE, EXPECTED) \
SELFTEST_BEGIN_STMT \
assert_dump_eq ((SELFTEST_LOCATION), (MODEL), (SUMMARIZE), (EXPECTED)); \
SELFTEST_END_STMT
/* Smoketest for region_model::dump_to_pp. */
static void
test_dump ()
{
region_model model;
model.get_root_region ()->ensure_stack_region (&model);
model.get_root_region ()->ensure_globals_region (&model);
model.get_root_region ()->ensure_heap_region (&model);
ASSERT_DUMP_EQ (model, false,
"r0: {kind: `root', parent: null, sval: null}\n"
"|-stack: r1: {kind: `stack', parent: r0, sval: null}\n"
"|-globals: r2: {kind: `globals', parent: r0, sval: null, map: {}}\n"
"`-heap: r3: {kind: `heap', parent: r0, sval: null}\n"
"svalues:\n"
"constraint manager:\n"
" equiv classes:\n"
" constraints:\n");
ASSERT_DUMP_EQ (model, true, "");
}
/* Helper function for selftests. Create a struct or union type named NAME,
with the fields given by the FIELD_DECLS in FIELDS.
If IS_STRUCT is true create a RECORD_TYPE (aka a struct), otherwise
create a UNION_TYPE. */
static tree
make_test_compound_type (const char *name, bool is_struct,
const auto_vec<tree> *fields)
{
tree t = make_node (is_struct ? RECORD_TYPE : UNION_TYPE);
TYPE_NAME (t) = get_identifier (name);
TYPE_SIZE (t) = 0;
tree fieldlist = NULL;
int i;
tree field;
FOR_EACH_VEC_ELT (*fields, i, field)
{
gcc_assert (TREE_CODE (field) == FIELD_DECL);
DECL_CONTEXT (field) = t;
fieldlist = chainon (field, fieldlist);
}
fieldlist = nreverse (fieldlist);
TYPE_FIELDS (t) = fieldlist;
layout_type (t);
return t;
}
/* Selftest fixture for creating the type "struct coord {int x; int y; };". */
struct coord_test
{
coord_test ()
{
auto_vec<tree> fields;
m_x_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
get_identifier ("x"), integer_type_node);
fields.safe_push (m_x_field);
m_y_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
get_identifier ("y"), integer_type_node);
fields.safe_push (m_y_field);
m_coord_type = make_test_compound_type ("coord", true, &fields);
}
tree m_x_field;
tree m_y_field;
tree m_coord_type;
};
/* Verify that dumps can show struct fields. */
static void
test_dump_2 ()
{
coord_test ct;
tree c = build_global_decl ("c", ct.m_coord_type);
tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
c, ct.m_x_field, NULL_TREE);
tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
c, ct.m_y_field, NULL_TREE);
tree int_17 = build_int_cst (integer_type_node, 17);
tree int_m3 = build_int_cst (integer_type_node, -3);
region_model model;
model.set_value (c_x, int_17, NULL);
model.set_value (c_y, int_m3, NULL);
/* Simplified dump. */
ASSERT_DUMP_EQ (model, true, "c.x: 17, c.y: -3");
/* Full dump. */
ASSERT_DUMP_EQ
(model, false,
"r0: {kind: `root', parent: null, sval: null}\n"
"`-globals: r1: {kind: `globals', parent: r0, sval: null, map: {`c': r2}}\n"
" `-`c': r2: {kind: `struct', parent: r1, sval: null, type: `struct coord', map: {`x': r3, `y': r4}}\n"
" |: type: `struct coord'\n"
" |-`x': r3: {kind: `primitive', parent: r2, sval: sv0, type: `int'}\n"
" | |: sval: sv0: {type: `int', `17'}\n"
" | |: type: `int'\n"
" `-`y': r4: {kind: `primitive', parent: r2, sval: sv1, type: `int'}\n"
" |: sval: sv1: {type: `int', `-3'}\n"
" |: type: `int'\n"
"svalues:\n"
" sv0: {type: `int', `17'}\n"
" sv1: {type: `int', `-3'}\n"
"constraint manager:\n"
" equiv classes:\n"
" constraints:\n");
}
/* Verify that dumps can show array elements. */
static void
test_dump_3 ()
{
tree tlen = size_int (10);
tree arr_type = build_array_type (char_type_node, build_index_type (tlen));
tree a = build_global_decl ("a", arr_type);
region_model model;
tree int_0 = build_int_cst (integer_type_node, 0);
tree a_0 = build4 (ARRAY_REF, char_type_node,
a, int_0, NULL_TREE, NULL_TREE);
tree char_A = build_int_cst (char_type_node, 'A');
model.set_value (a_0, char_A, NULL);
/* Simplified dump. */
ASSERT_DUMP_EQ (model, true, "a[0]: 65");
/* Full dump. */
ASSERT_DUMP_EQ
(model, false,
"r0: {kind: `root', parent: null, sval: null}\n"
"`-globals: r1: {kind: `globals', parent: r0, sval: null, map: {`a': r2}}\n"
" `-`a': r2: {kind: `array', parent: r1, sval: null, type: `char[11]', array: {[0]: r3}}\n"
" |: type: `char[11]'\n"
" `-[0]: r3: {kind: `primitive', parent: r2, sval: sv1, type: `char'}\n"
" |: sval: sv1: {type: `char', `65'}\n"
" |: type: `char'\n"
"svalues:\n"
" sv0: {type: `int', `0'}\n"
" sv1: {type: `char', `65'}\n"
"constraint manager:\n"
" equiv classes:\n"
" constraints:\n");
}
/* Verify that region_model::get_representative_tree works as expected. */
static void
test_get_representative_tree ()
{
/* STRING_CST. */
{
tree string_cst = build_string (4, "foo");
region_model m;
svalue_id str_sid = m.get_rvalue (string_cst, NULL);
tree rep = m.get_representative_tree (str_sid);
ASSERT_EQ (rep, string_cst);
}
/* String literal. */
{
tree string_cst_ptr = build_string_literal (4, "foo");
region_model m;
svalue_id str_sid = m.get_rvalue (string_cst_ptr, NULL);
tree rep = m.get_representative_tree (str_sid);
ASSERT_DUMP_TREE_EQ (rep, "&\"foo\"[0]");
}
}
/* Verify that calling region_model::get_rvalue repeatedly on the same
tree constant retrieves the same svalue_id. */
static void
test_unique_constants ()
{
tree int_0 = build_int_cst (integer_type_node, 0);
tree int_42 = build_int_cst (integer_type_node, 42);
test_region_model_context ctxt;
region_model model;
ASSERT_EQ (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_0, &ctxt));
ASSERT_EQ (model.get_rvalue (int_42, &ctxt),
model.get_rvalue (int_42, &ctxt));
ASSERT_NE (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_42, &ctxt));
ASSERT_EQ (ctxt.get_num_diagnostics (), 0);
}
/* Check that operator== and hashing works as expected for the
various svalue subclasses. */
static void
test_svalue_equality ()
{
tree int_42 = build_int_cst (integer_type_node, 42);
tree int_0 = build_int_cst (integer_type_node, 0);
/* Create pairs instances of the various subclasses of svalue,
testing for hash and equality between (this, this) and
(this, other of same subclass). */
svalue *ptr_to_r0
= new region_svalue (ptr_type_node, region_id::from_int (0));
svalue *ptr_to_r1
= new region_svalue (ptr_type_node, region_id::from_int (1));
ASSERT_EQ (ptr_to_r0->hash (), ptr_to_r0->hash ());
ASSERT_EQ (*ptr_to_r0, *ptr_to_r0);
ASSERT_NE (ptr_to_r0->hash (), ptr_to_r1->hash ());
ASSERT_NE (*ptr_to_r0, *ptr_to_r1);
svalue *cst_int_42 = new constant_svalue (int_42);
svalue *cst_int_0 = new constant_svalue (int_0);
ASSERT_EQ (cst_int_42->hash (), cst_int_42->hash ());
ASSERT_EQ (*cst_int_42, *cst_int_42);
ASSERT_NE (cst_int_42->hash (), cst_int_0->hash ());
ASSERT_NE (*cst_int_42, *cst_int_0);
svalue *unknown_0 = new unknown_svalue (ptr_type_node);
svalue *unknown_1 = new unknown_svalue (ptr_type_node);
ASSERT_EQ (unknown_0->hash (), unknown_0->hash ());
ASSERT_EQ (*unknown_0, *unknown_0);
ASSERT_EQ (*unknown_1, *unknown_1);
/* Comparisons between different kinds of svalue. */
ASSERT_NE (*ptr_to_r0, *cst_int_42);
ASSERT_NE (*ptr_to_r0, *unknown_0);
ASSERT_NE (*cst_int_42, *ptr_to_r0);
ASSERT_NE (*cst_int_42, *unknown_0);
ASSERT_NE (*unknown_0, *ptr_to_r0);
ASSERT_NE (*unknown_0, *cst_int_42);
delete ptr_to_r0;
delete ptr_to_r1;
delete cst_int_42;
delete cst_int_0;
delete unknown_0;
delete unknown_1;
}
/* Check that operator== and hashing works as expected for the
various region subclasses. */
static void
test_region_equality ()
{
region *r0
= new primitive_region (region_id::from_int (3), integer_type_node);
region *r1
= new primitive_region (region_id::from_int (4), integer_type_node);
ASSERT_EQ (*r0, *r0);
ASSERT_EQ (r0->hash (), r0->hash ());
ASSERT_NE (*r0, *r1);
ASSERT_NE (r0->hash (), r1->hash ());
delete r0;
delete r1;
// TODO: test coverage for the map within a map_region
}
/* A subclass of purge_criteria for selftests: purge all svalue_id instances. */
class purge_all_svalue_ids : public purge_criteria
{
public:
bool should_purge_p (svalue_id) const FINAL OVERRIDE
{
return true;
}
};
/* A subclass of purge_criteria: purge a specific svalue_id. */
class purge_one_svalue_id : public purge_criteria
{
public:
purge_one_svalue_id (svalue_id victim) : m_victim (victim) {}
purge_one_svalue_id (region_model model, tree expr)
: m_victim (model.get_rvalue (expr, NULL)) {}
bool should_purge_p (svalue_id sid) const FINAL OVERRIDE
{
return sid == m_victim;
}
private:
svalue_id m_victim;
};
/* Check that constraint_manager::purge works for individual svalue_ids. */
static void
test_purging_by_criteria ()
{
tree int_42 = build_int_cst (integer_type_node, 42);
tree int_0 = build_int_cst (integer_type_node, 0);
tree x = build_global_decl ("x", integer_type_node);
tree y = build_global_decl ("y", integer_type_node);
{
region_model model0;
region_model model1;
ADD_SAT_CONSTRAINT (model1, x, EQ_EXPR, y);
ASSERT_NE (model0, model1);
purge_stats stats_for_px;
purge_one_svalue_id px (model1, x);
model1.get_constraints ()->purge (px, &stats_for_px);
ASSERT_EQ (stats_for_px.m_num_equiv_classes, 0);
purge_stats stats_for_py;
purge_one_svalue_id py (model1.get_rvalue (y, NULL));
model1.get_constraints ()->purge (py, &stats_for_py);
ASSERT_EQ (stats_for_py.m_num_equiv_classes, 1);
ASSERT_EQ (*model0.get_constraints (), *model1.get_constraints ());
}
{
region_model model0;
region_model model1;
ADD_SAT_CONSTRAINT (model1, x, EQ_EXPR, int_42);
ASSERT_NE (model0, model1);
ASSERT_CONDITION_TRUE (model1, x, EQ_EXPR, int_42);
purge_stats stats;
model1.get_constraints ()->purge (purge_one_svalue_id (model1, x), &stats);
ASSERT_CONDITION_UNKNOWN (model1, x, EQ_EXPR, int_42);
}
{
region_model model0;
region_model model1;
ADD_SAT_CONSTRAINT (model1, x, GE_EXPR, int_0);
ADD_SAT_CONSTRAINT (model1, x, LE_EXPR, int_42);
ASSERT_NE (model0, model1);
ASSERT_CONDITION_TRUE (model1, x, GE_EXPR, int_0);
ASSERT_CONDITION_TRUE (model1, x, LE_EXPR, int_42);
purge_stats stats;
model1.get_constraints ()->purge (purge_one_svalue_id (model1, x), &stats);
ASSERT_CONDITION_UNKNOWN (model1, x, GE_EXPR, int_0);
ASSERT_CONDITION_UNKNOWN (model1, x, LE_EXPR, int_42);
}
{
region_model model0;
region_model model1;
ADD_SAT_CONSTRAINT (model1, x, NE_EXPR, int_42);
ADD_SAT_CONSTRAINT (model1, y, NE_EXPR, int_0);
ASSERT_NE (model0, model1);
ASSERT_CONDITION_TRUE (model1, x, NE_EXPR, int_42);
ASSERT_CONDITION_TRUE (model1, y, NE_EXPR, int_0);
purge_stats stats;
model1.get_constraints ()->purge (purge_one_svalue_id (model1, x), &stats);
ASSERT_NE (model0, model1);
ASSERT_CONDITION_UNKNOWN (model1, x, NE_EXPR, int_42);
ASSERT_CONDITION_TRUE (model1, y, NE_EXPR, int_0);
}
{
region_model model0;
region_model model1;
ADD_SAT_CONSTRAINT (model1, x, NE_EXPR, int_42);
ADD_SAT_CONSTRAINT (model1, y, NE_EXPR, int_0);
ASSERT_NE (model0, model1);
ASSERT_CONDITION_TRUE (model1, x, NE_EXPR, int_42);
ASSERT_CONDITION_TRUE (model1, y, NE_EXPR, int_0);
purge_stats stats;
model1.get_constraints ()->purge (purge_all_svalue_ids (), &stats);
ASSERT_CONDITION_UNKNOWN (model1, x, NE_EXPR, int_42);
ASSERT_CONDITION_UNKNOWN (model1, y, NE_EXPR, int_0);
}
}
/* Test that region_model::purge_unused_svalues works as expected. */
static void
test_purge_unused_svalues ()
{
tree int_42 = build_int_cst (integer_type_node, 42);
tree int_0 = build_int_cst (integer_type_node, 0);
tree x = build_global_decl ("x", integer_type_node);
tree y = build_global_decl ("y", integer_type_node);
test_region_model_context ctxt;
region_model model;
model.set_to_new_unknown_value (model.get_lvalue (x, &ctxt), TREE_TYPE (x),
&ctxt);
model.set_to_new_unknown_value (model.get_lvalue (x, &ctxt), TREE_TYPE (x),
&ctxt);
model.set_to_new_unknown_value (model.get_lvalue (x, &ctxt), TREE_TYPE (x),
&ctxt);
model.add_constraint (x, NE_EXPR, int_42, &ctxt);
model.set_value (model.get_lvalue (x, &ctxt),
model.get_rvalue (int_42, &ctxt),
&ctxt);
model.add_constraint (y, GT_EXPR, int_0, &ctxt);
/* The redundant unknown values should have been purged. */
purge_stats purged;
model.purge_unused_svalues (&purged, NULL);
ASSERT_EQ (purged.m_num_svalues, 3);
/* and the redundant constraint on an old, unknown value for x should
have been purged. */
ASSERT_EQ (purged.m_num_equiv_classes, 1);
ASSERT_EQ (purged.m_num_constraints, 1);
ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 2);
/* ...but we should still have x == 42. */
ASSERT_EQ (model.eval_condition (x, EQ_EXPR, int_42, &ctxt),
tristate::TS_TRUE);
/* ...and we should still have the constraint on y. */
ASSERT_EQ (model.eval_condition (y, GT_EXPR, int_0, &ctxt),
tristate::TS_TRUE);
ASSERT_EQ (ctxt.get_num_diagnostics (), 0);
}
/* Verify that simple assignments work as expected. */
static void
test_assignment ()
{
tree int_0 = build_int_cst (integer_type_node, 0);
tree x = build_global_decl ("x", integer_type_node);
tree y = build_global_decl ("y", integer_type_node);
/* "x == 0", then use of y, then "y = 0;". */
region_model model;
ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
ASSERT_CONDITION_UNKNOWN (model, y, EQ_EXPR, int_0);
model.set_value (model.get_lvalue (y, NULL),
model.get_rvalue (int_0, NULL),
NULL);
ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, int_0);
ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, x);
ASSERT_DUMP_EQ (model, true, "y: 0, {x}: unknown, x == y");
}
/* Verify that compound assignments work as expected. */
static void
test_compound_assignment ()
{
coord_test ct;
tree c = build_global_decl ("c", ct.m_coord_type);
tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
c, ct.m_x_field, NULL_TREE);
tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
c, ct.m_y_field, NULL_TREE);
tree d = build_global_decl ("d", ct.m_coord_type);
tree d_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
d, ct.m_x_field, NULL_TREE);
tree d_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
d, ct.m_y_field, NULL_TREE);
tree int_17 = build_int_cst (integer_type_node, 17);
tree int_m3 = build_int_cst (integer_type_node, -3);
region_model model;
model.set_value (c_x, int_17, NULL);
model.set_value (c_y, int_m3, NULL);
ASSERT_DUMP_EQ (model, true, "c.x: 17, c.y: -3");
/* Copy c to d. */
model.copy_region (model.get_lvalue (d, NULL), model.get_lvalue (c, NULL),
NULL);
/* Check that the fields have the same svalues. */
ASSERT_EQ (model.get_rvalue (c_x, NULL), model.get_rvalue (d_x, NULL));
ASSERT_EQ (model.get_rvalue (c_y, NULL), model.get_rvalue (d_y, NULL));
}
/* Verify the details of pushing and popping stack frames. */
static void
test_stack_frames ()
{
tree int_42 = build_int_cst (integer_type_node, 42);
tree int_10 = build_int_cst (integer_type_node, 10);
tree int_5 = build_int_cst (integer_type_node, 5);
tree int_0 = build_int_cst (integer_type_node, 0);
auto_vec <tree> param_types;
tree parent_fndecl = make_fndecl (integer_type_node,
"parent_fn",
param_types);
allocate_struct_function (parent_fndecl, true);
tree child_fndecl = make_fndecl (integer_type_node,
"child_fn",
param_types);
allocate_struct_function (child_fndecl, true);
/* "a" and "b" in the parent frame. */
tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier ("a"),
integer_type_node);
tree b = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier ("b"),
integer_type_node);
/* "x" and "y" in a child frame. */
tree x = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier ("x"),
integer_type_node);
tree y = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier ("y"),
integer_type_node);
/* "p" global. */
tree p = build_global_decl ("p", ptr_type_node);
/* "q" global. */
tree q = build_global_decl ("q", ptr_type_node);
test_region_model_context ctxt;
region_model model;
/* Push stack frame for "parent_fn". */
region_id parent_frame_rid
= model.push_frame (DECL_STRUCT_FUNCTION (parent_fndecl), NULL, &ctxt);
ASSERT_EQ (model.get_current_frame_id (), parent_frame_rid);
region_id a_in_parent_rid = model.get_lvalue (a, &ctxt);
model.set_value (a_in_parent_rid, model.get_rvalue (int_42, &ctxt), &ctxt);
model.set_to_new_unknown_value (model.get_lvalue (b, &ctxt),
integer_type_node, &ctxt);
model.add_constraint (b, LT_EXPR, int_10, &ctxt);
ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt),
tristate (tristate::TS_TRUE));
/* Push stack frame for "child_fn". */
region_id child_frame_rid
= model.push_frame (DECL_STRUCT_FUNCTION (child_fndecl), NULL, &ctxt);
ASSERT_EQ (model.get_current_frame_id (), child_frame_rid);
region_id x_in_child_rid = model.get_lvalue (x, &ctxt);
model.set_value (x_in_child_rid, model.get_rvalue (int_0, &ctxt), &ctxt);
model.set_to_new_unknown_value (model.get_lvalue (y, &ctxt),
integer_type_node, &ctxt);
model.add_constraint (y, NE_EXPR, int_5, &ctxt);
ASSERT_EQ (model.eval_condition (y, NE_EXPR, int_5, &ctxt),
tristate (tristate::TS_TRUE));
/* Point a global pointer at a local in the child frame: p = &x. */
region_id p_in_globals_rid = model.get_lvalue (p, &ctxt);
model.set_value (p_in_globals_rid,
model.get_or_create_ptr_svalue (ptr_type_node,
x_in_child_rid),
&ctxt);
/* Point another global pointer at p: q = &p. */
region_id q_in_globals_rid = model.get_lvalue (q, &ctxt);
model.set_value (q_in_globals_rid,
model.get_or_create_ptr_svalue (ptr_type_node,
p_in_globals_rid),
&ctxt);
/* Test get_descendents. */
region_id_set descendents (&model);
model.get_descendents (child_frame_rid, &descendents, region_id::null ());
ASSERT_TRUE (descendents.region_p (child_frame_rid));
ASSERT_TRUE (descendents.region_p (x_in_child_rid));
ASSERT_FALSE (descendents.region_p (a_in_parent_rid));
ASSERT_EQ (descendents.num_regions (), 3);
#if 0
auto_vec<region_id> test_vec;
for (region_id_set::iterator_t iter = descendents.begin ();
iter != descendents.end ();
++iter)
test_vec.safe_push (*iter);
gcc_unreachable (); // TODO
//ASSERT_EQ ();
#endif
ASSERT_DUMP_EQ (model, true,
"a: 42, x: 0, p: &x, q: &p, {b, y}: unknown, b < 10, y != 5");
/* Pop the "child_fn" frame from the stack. */
purge_stats purged;
model.pop_frame (region_id::null (), true, &purged, &ctxt);
/* We should have purged the unknown values for x and y. */
ASSERT_EQ (purged.m_num_svalues, 2);
/* We should have purged the frame region and the regions for x and y. */
ASSERT_EQ (purged.m_num_regions, 3);
/* We should have purged the constraint on y. */
ASSERT_EQ (purged.m_num_equiv_classes, 1);
ASSERT_EQ (purged.m_num_constraints, 1);
/* Verify that p (which was pointing at the local "x" in the popped
frame) has been poisoned. */
svalue *new_p_sval = model.get_svalue (model.get_rvalue (p, &ctxt));
ASSERT_EQ (new_p_sval->get_kind (), SK_POISONED);
ASSERT_EQ (new_p_sval->dyn_cast_poisoned_svalue ()->get_poison_kind (),
POISON_KIND_POPPED_STACK);
/* Verify that q still points to p, in spite of the region
renumbering. */
svalue *new_q_sval = model.get_svalue (model.get_rvalue (q, &ctxt));
ASSERT_EQ (new_q_sval->get_kind (), SK_REGION);
ASSERT_EQ (new_q_sval->dyn_cast_region_svalue ()->get_pointee (),
model.get_lvalue (p, &ctxt));
/* Verify that top of stack has been updated. */
ASSERT_EQ (model.get_current_frame_id (), parent_frame_rid);
/* Verify locals in parent frame. */
/* Verify "a" still has its value. */
svalue *new_a_sval = model.get_svalue (model.get_rvalue (a, &ctxt));
ASSERT_EQ (new_a_sval->get_kind (), SK_CONSTANT);
ASSERT_EQ (new_a_sval->dyn_cast_constant_svalue ()->get_constant (),
int_42);
/* Verify "b" still has its constraint. */
ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt),
tristate (tristate::TS_TRUE));
}
/* Verify that get_representative_path_var works as expected, that
we can map from region ids to parms and back within a recursive call
stack. */
static void
test_get_representative_path_var ()
{
auto_vec <tree> param_types;
tree fndecl = make_fndecl (integer_type_node,
"factorial",
param_types);
allocate_struct_function (fndecl, true);
/* Parm "n". */
tree n = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier ("n"),
integer_type_node);
region_model model;
/* Push 5 stack frames for "factorial", each with a param */
auto_vec<region_id> parm_rids;
auto_vec<svalue_id> parm_sids;
for (int depth = 0; depth < 5; depth++)
{
region_id frame_rid
= model.push_frame (DECL_STRUCT_FUNCTION (fndecl), NULL, NULL);
region_id rid_n = model.get_lvalue (path_var (n, depth), NULL);
parm_rids.safe_push (rid_n);
ASSERT_EQ (model.get_region (rid_n)->get_parent (), frame_rid);
svalue_id sid_n
= model.set_to_new_unknown_value (rid_n, integer_type_node, NULL);
parm_sids.safe_push (sid_n);
}
/* Verify that we can recognize that the regions are the parms,
at every depth. */
for (int depth = 0; depth < 5; depth++)
{
ASSERT_EQ (model.get_representative_path_var (parm_rids[depth]),
path_var (n, depth));
/* ...and that we can lookup lvalues for locals for all frames,
not just the top. */
ASSERT_EQ (model.get_lvalue (path_var (n, depth), NULL),
parm_rids[depth]);
/* ...and that we can locate the svalues. */
auto_vec<path_var> pvs;
model.get_path_vars_for_svalue (parm_sids[depth], &pvs);
ASSERT_EQ (pvs.length (), 1);
ASSERT_EQ (pvs[0], path_var (n, depth));
}
}
/* Verify that the core regions within a region_model are in a consistent
order after canonicalization. */
static void
test_canonicalization_1 ()
{
region_model model0;
model0.get_root_region ()->ensure_stack_region (&model0);
model0.get_root_region ()->ensure_globals_region (&model0);
region_model model1;
model1.get_root_region ()->ensure_globals_region (&model1);
model1.get_root_region ()->ensure_stack_region (&model1);
model0.canonicalize (NULL);
model1.canonicalize (NULL);
ASSERT_EQ (model0, model1);
}
/* Verify that region models for
x = 42; y = 113;
and
y = 113; x = 42;
are equal after canonicalization. */
static void
test_canonicalization_2 ()
{
tree int_42 = build_int_cst (integer_type_node, 42);
tree int_113 = build_int_cst (integer_type_node, 113);
tree x = build_global_decl ("x", integer_type_node);
tree y = build_global_decl ("y", integer_type_node);
region_model model0;
model0.set_value (model0.get_lvalue (x, NULL),
model0.get_rvalue (int_42, NULL),
NULL);
model0.set_value (model0.get_lvalue (y, NULL),
model0.get_rvalue (int_113, NULL),
NULL);
region_model model1;
model1.set_value (model1.get_lvalue (y, NULL),
model1.get_rvalue (int_113, NULL),
NULL);
model1.set_value (model1.get_lvalue (x, NULL),
model1.get_rvalue (int_42, NULL),
NULL);
model0.canonicalize (NULL);
model1.canonicalize (NULL);
ASSERT_EQ (model0, model1);
}
/* Verify that constraints for
x > 3 && y > 42
and
y > 42 && x > 3
are equal after canonicalization. */
static void
test_canonicalization_3 ()
{
tree int_3 = build_int_cst (integer_type_node, 3);
tree int_42 = build_int_cst (integer_type_node, 42);
tree x = build_global_decl ("x", integer_type_node);
tree y = build_global_decl ("y", integer_type_node);
region_model model0;
model0.add_constraint (x, GT_EXPR, int_3, NULL);
model0.add_constraint (y, GT_EXPR, int_42, NULL);
region_model model1;
model1.add_constraint (y, GT_EXPR, int_42, NULL);
model1.add_constraint (x, GT_EXPR, int_3, NULL);
model0.canonicalize (NULL);
model1.canonicalize (NULL);
ASSERT_EQ (model0, model1);
}
/* Verify that we can canonicalize a model containing NaN and other real
constants. */
static void
test_canonicalization_4 ()
{
auto_vec<tree> csts;
append_interesting_constants (&csts);
region_model model;
unsigned i;
tree cst;
FOR_EACH_VEC_ELT (csts, i, cst)
model.get_rvalue (cst, NULL);
model.canonicalize (NULL);
}
/* Assert that if we have two region_model instances
with values VAL_A and VAL_B for EXPR that they are
mergable. Write the merged model to *OUT_MERGED_MODEL,
and the merged svalue ptr to *OUT_MERGED_SVALUE.
If VAL_A or VAL_B are NULL_TREE, don't populate EXPR
for that region_model. */
static void
assert_region_models_merge (tree expr, tree val_a, tree val_b,
region_model *out_merged_model,
svalue **out_merged_svalue)
{
test_region_model_context ctxt;
region_model model0;
region_model model1;
if (val_a)
model0.set_value (model0.get_lvalue (expr, &ctxt),
model0.get_rvalue (val_a, &ctxt),
&ctxt);
if (val_b)
model1.set_value (model1.get_lvalue (expr, &ctxt),
model1.get_rvalue (val_b, &ctxt),
&ctxt);
/* They should be mergeable. */
ASSERT_TRUE (model0.can_merge_with_p (model1, out_merged_model));
svalue_id merged_svalue_sid = out_merged_model->get_rvalue (expr, &ctxt);
*out_merged_svalue = out_merged_model->get_svalue (merged_svalue_sid);
}
/* Verify that we can merge region_model instances. */
static void
test_state_merging ()
{
tree int_42 = build_int_cst (integer_type_node, 42);
tree int_113 = build_int_cst (integer_type_node, 113);
tree x = build_global_decl ("x", integer_type_node);
tree y = build_global_decl ("y", integer_type_node);
tree z = build_global_decl ("z", integer_type_node);
tree p = build_global_decl ("p", ptr_type_node);
tree addr_of_y = build1 (ADDR_EXPR, ptr_type_node, y);
tree addr_of_z = build1 (ADDR_EXPR, ptr_type_node, z);
auto_vec <tree> param_types;
tree test_fndecl = make_fndecl (integer_type_node, "test_fn", param_types);
allocate_struct_function (test_fndecl, true);
/* Param "a". */
tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier ("a"),
integer_type_node);
tree addr_of_a = build1 (ADDR_EXPR, ptr_type_node, a);
/* Param "q", a pointer. */
tree q = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier ("q"),
ptr_type_node);
{
region_model model0;
region_model model1;
region_model merged;
/* Verify empty models can be merged. */
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
ASSERT_EQ (model0, merged);
}
/* Verify that we can merge two contradictory constraints on the
value for a global. */
/* TODO: verify that the merged model doesn't have a value for
the global */
{
region_model model0;
region_model model1;
region_model merged;
test_region_model_context ctxt;
model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
model1.add_constraint (x, EQ_EXPR, int_113, &ctxt);
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
ASSERT_NE (model0, merged);
ASSERT_NE (model1, merged);
}
/* Verify handling of a PARM_DECL. */
{
test_region_model_context ctxt;
region_model model0;
region_model model1;
ASSERT_EQ (model0.get_stack_depth (), 0);
model0.push_frame (DECL_STRUCT_FUNCTION (test_fndecl), NULL, &ctxt);
ASSERT_EQ (model0.get_stack_depth (), 1);
ASSERT_EQ (model0.get_function_at_depth (0),
DECL_STRUCT_FUNCTION (test_fndecl));
model1.push_frame (DECL_STRUCT_FUNCTION (test_fndecl), NULL, &ctxt);
svalue_id sid_a
= model0.set_to_new_unknown_value (model0.get_lvalue (a, &ctxt),
integer_type_node, &ctxt);
model1.set_to_new_unknown_value (model1.get_lvalue (a, &ctxt),
integer_type_node, &ctxt);
ASSERT_EQ (model0, model1);
/* Check that get_value_by_name works for locals. */
ASSERT_EQ (model0.get_value_by_name ("a"), sid_a);
/* They should be mergeable, and the result should be the same. */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
ASSERT_EQ (model0, merged);
/* In particular, there should be an unknown value for "a". */
svalue *merged_a_sval = merged.get_svalue (merged.get_rvalue (a, &ctxt));
ASSERT_EQ (merged_a_sval->get_kind (), SK_UNKNOWN);
}
/* Verify handling of a global. */
{
test_region_model_context ctxt;
region_model model0;
region_model model1;
svalue_id sid_x
= model0.set_to_new_unknown_value (model0.get_lvalue (x, &ctxt),
integer_type_node, &ctxt);
model1.set_to_new_unknown_value (model1.get_lvalue (x, &ctxt),
integer_type_node, &ctxt);
ASSERT_EQ (model0, model1);
/* Check that get_value_by_name works for globals. */
ASSERT_EQ (model0.get_value_by_name ("x"), sid_x);
/* They should be mergeable, and the result should be the same. */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
ASSERT_EQ (model0, merged);
/* In particular, there should be an unknown value for "x". */
svalue *merged_x_sval = merged.get_svalue (merged.get_rvalue (x, &ctxt));
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
}
/* Use global-handling to verify various combinations of values. */
/* Two equal constant values. */
{
region_model merged;
svalue *merged_x_sval;
assert_region_models_merge (x, int_42, int_42, &merged, &merged_x_sval);
/* In particular, there should be a constant value for "x". */
ASSERT_EQ (merged_x_sval->get_kind (), SK_CONSTANT);
ASSERT_EQ (merged_x_sval->dyn_cast_constant_svalue ()->get_constant (),
int_42);
}
/* Two non-equal constant values. */
{
region_model merged;
svalue *merged_x_sval;
assert_region_models_merge (x, int_42, int_113, &merged, &merged_x_sval);
/* In particular, there should be an unknown value for "x". */
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
}
/* Uninit and constant. */
{
region_model merged;
svalue *merged_x_sval;
assert_region_models_merge (x, NULL_TREE, int_113, &merged, &merged_x_sval);
/* In particular, there should be an unknown value for "x". */
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
}
/* Constant and uninit. */
{
region_model merged;
svalue *merged_x_sval;
assert_region_models_merge (x, int_42, NULL_TREE, &merged, &merged_x_sval);
/* In particular, there should be an unknown value for "x". */
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
}
/* Unknown and constant. */
// TODO
/* Pointers: NULL and NULL. */
// TODO
/* Pointers: NULL and non-NULL. */
// TODO
/* Pointers: non-NULL and non-NULL: ptr to a local. */
{
region_model model0;
model0.push_frame (DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL);
model0.set_to_new_unknown_value (model0.get_lvalue (a, NULL),
integer_type_node, NULL);
model0.set_value (model0.get_lvalue (p, NULL),
model0.get_rvalue (addr_of_a, NULL), NULL);
region_model model1 (model0);
ASSERT_EQ (model0, model1);
/* They should be mergeable, and the result should be the same. */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
ASSERT_EQ (model0, merged);
}
/* Pointers: non-NULL and non-NULL: ptr to a global. */
{
region_model merged;
/* p == &y in both input models. */
svalue *merged_p_sval;
assert_region_models_merge (p, addr_of_y, addr_of_y, &merged,
&merged_p_sval);
/* We should get p == &y in the merged model. */
ASSERT_EQ (merged_p_sval->get_kind (), SK_REGION);
region_svalue *merged_p_ptr = merged_p_sval->dyn_cast_region_svalue ();
region_id merged_p_star_rid = merged_p_ptr->get_pointee ();
ASSERT_EQ (merged_p_star_rid, merged.get_lvalue (y, NULL));
}
/* Pointers: non-NULL ptrs to different globals: should be unknown. */
{
region_model merged;
/* x == &y vs x == &z in the input models. */
svalue *merged_x_sval;
assert_region_models_merge (x, addr_of_y, addr_of_z, &merged,
&merged_x_sval);
/* We should get x == unknown in the merged model. */
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
}
/* Pointers: non-NULL and non-NULL: ptr to a heap region. */
{
test_region_model_context ctxt;
region_model model0;
region_id new_rid = model0.add_new_malloc_region ();
svalue_id ptr_sid
= model0.get_or_create_ptr_svalue (ptr_type_node, new_rid);
model0.set_value (model0.get_lvalue (p, &ctxt),
ptr_sid, &ctxt);
model0.canonicalize (&ctxt);
region_model model1 (model0);
ASSERT_EQ (model0, model1);
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
merged.canonicalize (&ctxt);
/* The merged model ought to be identical (after canonicalization,
at least). */
ASSERT_EQ (model0, merged);
}
/* Two regions sharing the same unknown svalue should continue sharing
an unknown svalue after self-merger. */
{
test_region_model_context ctxt;
region_model model0;
svalue_id sid
= model0.set_to_new_unknown_value (model0.get_lvalue (x, &ctxt),
integer_type_node, &ctxt);
model0.set_value (model0.get_lvalue (y, &ctxt), sid, &ctxt);
region_model model1 (model0);
/* They should be mergeable, and the result should be the same. */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
ASSERT_EQ (model0, merged);
/* In particular, we should have x == y. */
ASSERT_EQ (merged.eval_condition (x, EQ_EXPR, y, &ctxt),
tristate (tristate::TS_TRUE));
}
#if 0
{
region_model model0;
region_model model1;
test_region_model_context ctxt;
model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
model1.add_constraint (x, NE_EXPR, int_42, &ctxt);
ASSERT_TRUE (model0.can_merge_with_p (model1));
}
{
region_model model0;
region_model model1;
test_region_model_context ctxt;
model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
model1.add_constraint (x, NE_EXPR, int_42, &ctxt);
model1.add_constraint (x, EQ_EXPR, int_113, &ctxt);
ASSERT_TRUE (model0.can_merge_with_p (model1));
}
#endif
// TODO: what can't we merge? need at least one such test
/* TODO: various things
- heap regions
- value merging:
- every combination, but in particular
- pairs of regions
*/
/* Views. */
{
test_region_model_context ctxt;
region_model model0;
region_id x_rid = model0.get_lvalue (x, &ctxt);
region_id x_as_ptr = model0.get_or_create_view (x_rid, ptr_type_node,
&ctxt);
model0.set_value (x_as_ptr, model0.get_rvalue (addr_of_y, &ctxt), &ctxt);
region_model model1 (model0);
ASSERT_EQ (model1, model0);
/* They should be mergeable, and the result should be the same. */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
}
/* Verify that we can merge a model in which a local in an older stack
frame points to a local in a more recent stack frame. */
{
region_model model0;
model0.push_frame (DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL);
region_id q_in_first_frame = model0.get_lvalue (q, NULL);
/* Push a second frame. */
region_id rid_2nd_frame
= model0.push_frame (DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL);
/* Have a pointer in the older frame point to a local in the
more recent frame. */
svalue_id sid_ptr = model0.get_rvalue (addr_of_a, NULL);
model0.set_value (q_in_first_frame, sid_ptr, NULL);
/* Verify that it's pointing at the newer frame. */
region_id rid_pointee
= model0.get_svalue (sid_ptr)->dyn_cast_region_svalue ()->get_pointee ();
ASSERT_EQ (model0.get_region (rid_pointee)->get_parent (), rid_2nd_frame);
model0.canonicalize (NULL);
region_model model1 (model0);
ASSERT_EQ (model0, model1);
/* They should be mergeable, and the result should be the same
(after canonicalization, at least). */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
merged.canonicalize (NULL);
ASSERT_EQ (model0, merged);
}
/* Verify that we can merge a model in which a local points to a global. */
{
region_model model0;
model0.push_frame (DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL);
model0.set_value (model0.get_lvalue (q, NULL),
model0.get_rvalue (addr_of_y, NULL), NULL);
model0.canonicalize (NULL);
region_model model1 (model0);
ASSERT_EQ (model0, model1);
/* They should be mergeable, and the result should be the same
(after canonicalization, at least). */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
merged.canonicalize (NULL);
ASSERT_EQ (model0, merged);
}
}
/* Verify that constraints are correctly merged when merging region_model
instances. */
static void
test_constraint_merging ()
{
tree int_0 = build_int_cst (integer_type_node, 0);
tree int_5 = build_int_cst (integer_type_node, 5);
tree x = build_global_decl ("x", integer_type_node);
tree y = build_global_decl ("y", integer_type_node);
tree z = build_global_decl ("z", integer_type_node);
tree n = build_global_decl ("n", integer_type_node);
test_region_model_context ctxt;
/* model0: 0 <= (x == y) < n. */
region_model model0;
model0.set_to_new_unknown_value (model0.get_lvalue (x, &ctxt),
integer_type_node, &ctxt);
model0.add_constraint (x, EQ_EXPR, y, &ctxt);
model0.add_constraint (x, GE_EXPR, int_0, NULL);
model0.add_constraint (x, LT_EXPR, n, NULL);
/* model1: z != 5 && (0 <= x < n). */
region_model model1;
model1.set_to_new_unknown_value (model1.get_lvalue (x, &ctxt),
integer_type_node, &ctxt);
model1.add_constraint (z, NE_EXPR, int_5, NULL);
model1.add_constraint (x, GE_EXPR, int_0, NULL);
model1.add_constraint (x, LT_EXPR, n, NULL);
/* They should be mergeable; the merged constraints should
be: (0 <= x < n). */
region_model merged;
ASSERT_TRUE (model0.can_merge_with_p (model1, &merged));
ASSERT_EQ (merged.eval_condition (x, GE_EXPR, int_0, &ctxt),
tristate (tristate::TS_TRUE));
ASSERT_EQ (merged.eval_condition (x, LT_EXPR, n, &ctxt),
tristate (tristate::TS_TRUE));
ASSERT_EQ (merged.eval_condition (z, NE_EXPR, int_5, &ctxt),
tristate (tristate::TS_UNKNOWN));
ASSERT_EQ (merged.eval_condition (x, LT_EXPR, y, &ctxt),
tristate (tristate::TS_UNKNOWN));
}
/* Verify that if we mark a pointer to a malloc-ed region as non-NULL,
all cast pointers to that region are also known to be non-NULL. */
static void
test_malloc_constraints ()
{
region_model model;
tree p = build_global_decl ("p", ptr_type_node);
tree char_star = build_pointer_type (char_type_node);
tree q = build_global_decl ("q", char_star);
tree null_ptr = build_int_cst (ptr_type_node, 0);
region_id rid = model.add_new_malloc_region ();
svalue_id sid = model.get_or_create_ptr_svalue (ptr_type_node, rid);
model.set_value (model.get_lvalue (p, NULL), sid, NULL);
model.set_value (q, p, NULL);
/* We should have a symbolic_region with m_possibly_null: true. */
region *pointee = model.get_region (rid);
symbolic_region *sym_reg = pointee->dyn_cast_symbolic_region ();
ASSERT_NE (sym_reg, NULL);
ASSERT_TRUE (sym_reg->m_possibly_null);
ASSERT_CONDITION_UNKNOWN (model, p, NE_EXPR, null_ptr);
ASSERT_CONDITION_UNKNOWN (model, p, EQ_EXPR, null_ptr);
ASSERT_CONDITION_UNKNOWN (model, q, NE_EXPR, null_ptr);
ASSERT_CONDITION_UNKNOWN (model, q, EQ_EXPR, null_ptr);
model.add_constraint (p, NE_EXPR, null_ptr, NULL);
/* Adding the constraint should have cleared m_possibly_null. */
ASSERT_FALSE (sym_reg->m_possibly_null);
ASSERT_CONDITION_TRUE (model, p, NE_EXPR, null_ptr);
ASSERT_CONDITION_FALSE (model, p, EQ_EXPR, null_ptr);
ASSERT_CONDITION_TRUE (model, q, NE_EXPR, null_ptr);
ASSERT_CONDITION_FALSE (model, q, EQ_EXPR, null_ptr);
}
/* Run all of the selftests within this file. */
void
analyzer_region_model_cc_tests ()
{
test_tree_cmp_on_constants ();
test_dump ();
test_dump_2 ();
test_dump_3 ();
test_get_representative_tree ();
test_unique_constants ();
test_svalue_equality ();
test_region_equality ();
test_purging_by_criteria ();
test_purge_unused_svalues ();
test_assignment ();
test_compound_assignment ();
test_stack_frames ();
test_get_representative_path_var ();
test_canonicalization_1 ();
test_canonicalization_2 ();
test_canonicalization_3 ();
test_canonicalization_4 ();
test_state_merging ();
test_constraint_merging ();
test_malloc_constraints ();
}
} // namespace selftest
#endif /* CHECKING_P */
} // namespace ana
#endif /* #if ENABLE_ANALYZER */
|