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
|
/* Passes for transactional memory support.
Copyright (C) 2008-2014 Free Software Foundation, Inc.
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 "hash-table.h"
#include "tree.h"
#include "basic-block.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "tree-eh.h"
#include "gimple-expr.h"
#include "is-a.h"
#include "gimple.h"
#include "calls.h"
#include "function.h"
#include "rtl.h"
#include "emit-rtl.h"
#include "gimplify.h"
#include "gimple-iterator.h"
#include "gimplify-me.h"
#include "gimple-walk.h"
#include "gimple-ssa.h"
#include "cgraph.h"
#include "tree-cfg.h"
#include "stringpool.h"
#include "tree-ssanames.h"
#include "tree-into-ssa.h"
#include "tree-pass.h"
#include "tree-inline.h"
#include "diagnostic-core.h"
#include "demangle.h"
#include "output.h"
#include "trans-mem.h"
#include "params.h"
#include "target.h"
#include "langhooks.h"
#include "gimple-pretty-print.h"
#include "cfgloop.h"
#include "tree-ssa-address.h"
#include "predict.h"
#define A_RUNINSTRUMENTEDCODE 0x0001
#define A_RUNUNINSTRUMENTEDCODE 0x0002
#define A_SAVELIVEVARIABLES 0x0004
#define A_RESTORELIVEVARIABLES 0x0008
#define A_ABORTTRANSACTION 0x0010
#define AR_USERABORT 0x0001
#define AR_USERRETRY 0x0002
#define AR_TMCONFLICT 0x0004
#define AR_EXCEPTIONBLOCKABORT 0x0008
#define AR_OUTERABORT 0x0010
#define MODE_SERIALIRREVOCABLE 0x0000
/* The representation of a transaction changes several times during the
lowering process. In the beginning, in the front-end we have the
GENERIC tree TRANSACTION_EXPR. For example,
__transaction {
local++;
if (++global == 10)
__tm_abort;
}
During initial gimplification (gimplify.c) the TRANSACTION_EXPR node is
trivially replaced with a GIMPLE_TRANSACTION node.
During pass_lower_tm, we examine the body of transactions looking
for aborts. Transactions that do not contain an abort may be
merged into an outer transaction. We also add a TRY-FINALLY node
to arrange for the transaction to be committed on any exit.
[??? Think about how this arrangement affects throw-with-commit
and throw-with-abort operations. In this case we want the TRY to
handle gotos, but not to catch any exceptions because the transaction
will already be closed.]
GIMPLE_TRANSACTION [label=NULL] {
try {
local = local + 1;
t0 = global;
t1 = t0 + 1;
global = t1;
if (t1 == 10)
__builtin___tm_abort ();
} finally {
__builtin___tm_commit ();
}
}
During pass_lower_eh, we create EH regions for the transactions,
intermixed with the regular EH stuff. This gives us a nice persistent
mapping (all the way through rtl) from transactional memory operation
back to the transaction, which allows us to get the abnormal edges
correct to model transaction aborts and restarts:
GIMPLE_TRANSACTION [label=over]
local = local + 1;
t0 = global;
t1 = t0 + 1;
global = t1;
if (t1 == 10)
__builtin___tm_abort ();
__builtin___tm_commit ();
over:
This is the end of all_lowering_passes, and so is what is present
during the IPA passes, and through all of the optimization passes.
During pass_ipa_tm, we examine all GIMPLE_TRANSACTION blocks in all
functions and mark functions for cloning.
At the end of gimple optimization, before exiting SSA form,
pass_tm_edges replaces statements that perform transactional
memory operations with the appropriate TM builtins, and swap
out function calls with their transactional clones. At this
point we introduce the abnormal transaction restart edges and
complete lowering of the GIMPLE_TRANSACTION node.
x = __builtin___tm_start (MAY_ABORT);
eh_label:
if (x & abort_transaction)
goto over;
local = local + 1;
t0 = __builtin___tm_load (global);
t1 = t0 + 1;
__builtin___tm_store (&global, t1);
if (t1 == 10)
__builtin___tm_abort ();
__builtin___tm_commit ();
over:
*/
static void *expand_regions (struct tm_region *,
void *(*callback)(struct tm_region *, void *),
void *, bool);
/* Return the attributes we want to examine for X, or NULL if it's not
something we examine. We look at function types, but allow pointers
to function types and function decls and peek through. */
static tree
get_attrs_for (const_tree x)
{
switch (TREE_CODE (x))
{
case FUNCTION_DECL:
return TYPE_ATTRIBUTES (TREE_TYPE (x));
break;
default:
if (TYPE_P (x))
return NULL;
x = TREE_TYPE (x);
if (TREE_CODE (x) != POINTER_TYPE)
return NULL;
/* FALLTHRU */
case POINTER_TYPE:
x = TREE_TYPE (x);
if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE)
return NULL;
/* FALLTHRU */
case FUNCTION_TYPE:
case METHOD_TYPE:
return TYPE_ATTRIBUTES (x);
}
}
/* Return true if X has been marked TM_PURE. */
bool
is_tm_pure (const_tree x)
{
unsigned flags;
switch (TREE_CODE (x))
{
case FUNCTION_DECL:
case FUNCTION_TYPE:
case METHOD_TYPE:
break;
default:
if (TYPE_P (x))
return false;
x = TREE_TYPE (x);
if (TREE_CODE (x) != POINTER_TYPE)
return false;
/* FALLTHRU */
case POINTER_TYPE:
x = TREE_TYPE (x);
if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE)
return false;
break;
}
flags = flags_from_decl_or_type (x);
return (flags & ECF_TM_PURE) != 0;
}
/* Return true if X has been marked TM_IRREVOCABLE. */
static bool
is_tm_irrevocable (tree x)
{
tree attrs = get_attrs_for (x);
if (attrs && lookup_attribute ("transaction_unsafe", attrs))
return true;
/* A call to the irrevocable builtin is by definition,
irrevocable. */
if (TREE_CODE (x) == ADDR_EXPR)
x = TREE_OPERAND (x, 0);
if (TREE_CODE (x) == FUNCTION_DECL
&& DECL_BUILT_IN_CLASS (x) == BUILT_IN_NORMAL
&& DECL_FUNCTION_CODE (x) == BUILT_IN_TM_IRREVOCABLE)
return true;
return false;
}
/* Return true if X has been marked TM_SAFE. */
bool
is_tm_safe (const_tree x)
{
if (flag_tm)
{
tree attrs = get_attrs_for (x);
if (attrs)
{
if (lookup_attribute ("transaction_safe", attrs))
return true;
if (lookup_attribute ("transaction_may_cancel_outer", attrs))
return true;
}
}
return false;
}
/* Return true if CALL is const, or tm_pure. */
static bool
is_tm_pure_call (gimple call)
{
tree fn = gimple_call_fn (call);
if (TREE_CODE (fn) == ADDR_EXPR)
{
fn = TREE_OPERAND (fn, 0);
gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
}
else
fn = TREE_TYPE (fn);
return is_tm_pure (fn);
}
/* Return true if X has been marked TM_CALLABLE. */
static bool
is_tm_callable (tree x)
{
tree attrs = get_attrs_for (x);
if (attrs)
{
if (lookup_attribute ("transaction_callable", attrs))
return true;
if (lookup_attribute ("transaction_safe", attrs))
return true;
if (lookup_attribute ("transaction_may_cancel_outer", attrs))
return true;
}
return false;
}
/* Return true if X has been marked TRANSACTION_MAY_CANCEL_OUTER. */
bool
is_tm_may_cancel_outer (tree x)
{
tree attrs = get_attrs_for (x);
if (attrs)
return lookup_attribute ("transaction_may_cancel_outer", attrs) != NULL;
return false;
}
/* Return true for built in functions that "end" a transaction. */
bool
is_tm_ending_fndecl (tree fndecl)
{
if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
switch (DECL_FUNCTION_CODE (fndecl))
{
case BUILT_IN_TM_COMMIT:
case BUILT_IN_TM_COMMIT_EH:
case BUILT_IN_TM_ABORT:
case BUILT_IN_TM_IRREVOCABLE:
return true;
default:
break;
}
return false;
}
/* Return true if STMT is a built in function call that "ends" a
transaction. */
bool
is_tm_ending (gimple stmt)
{
tree fndecl;
if (gimple_code (stmt) != GIMPLE_CALL)
return false;
fndecl = gimple_call_fndecl (stmt);
return (fndecl != NULL_TREE
&& is_tm_ending_fndecl (fndecl));
}
/* Return true if STMT is a TM load. */
static bool
is_tm_load (gimple stmt)
{
tree fndecl;
if (gimple_code (stmt) != GIMPLE_CALL)
return false;
fndecl = gimple_call_fndecl (stmt);
return (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
&& BUILTIN_TM_LOAD_P (DECL_FUNCTION_CODE (fndecl)));
}
/* Same as above, but for simple TM loads, that is, not the
after-write, after-read, etc optimized variants. */
static bool
is_tm_simple_load (gimple stmt)
{
tree fndecl;
if (gimple_code (stmt) != GIMPLE_CALL)
return false;
fndecl = gimple_call_fndecl (stmt);
if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
{
enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
return (fcode == BUILT_IN_TM_LOAD_1
|| fcode == BUILT_IN_TM_LOAD_2
|| fcode == BUILT_IN_TM_LOAD_4
|| fcode == BUILT_IN_TM_LOAD_8
|| fcode == BUILT_IN_TM_LOAD_FLOAT
|| fcode == BUILT_IN_TM_LOAD_DOUBLE
|| fcode == BUILT_IN_TM_LOAD_LDOUBLE
|| fcode == BUILT_IN_TM_LOAD_M64
|| fcode == BUILT_IN_TM_LOAD_M128
|| fcode == BUILT_IN_TM_LOAD_M256);
}
return false;
}
/* Return true if STMT is a TM store. */
static bool
is_tm_store (gimple stmt)
{
tree fndecl;
if (gimple_code (stmt) != GIMPLE_CALL)
return false;
fndecl = gimple_call_fndecl (stmt);
return (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
&& BUILTIN_TM_STORE_P (DECL_FUNCTION_CODE (fndecl)));
}
/* Same as above, but for simple TM stores, that is, not the
after-write, after-read, etc optimized variants. */
static bool
is_tm_simple_store (gimple stmt)
{
tree fndecl;
if (gimple_code (stmt) != GIMPLE_CALL)
return false;
fndecl = gimple_call_fndecl (stmt);
if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
{
enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
return (fcode == BUILT_IN_TM_STORE_1
|| fcode == BUILT_IN_TM_STORE_2
|| fcode == BUILT_IN_TM_STORE_4
|| fcode == BUILT_IN_TM_STORE_8
|| fcode == BUILT_IN_TM_STORE_FLOAT
|| fcode == BUILT_IN_TM_STORE_DOUBLE
|| fcode == BUILT_IN_TM_STORE_LDOUBLE
|| fcode == BUILT_IN_TM_STORE_M64
|| fcode == BUILT_IN_TM_STORE_M128
|| fcode == BUILT_IN_TM_STORE_M256);
}
return false;
}
/* Return true if FNDECL is BUILT_IN_TM_ABORT. */
static bool
is_tm_abort (tree fndecl)
{
return (fndecl
&& DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
&& DECL_FUNCTION_CODE (fndecl) == BUILT_IN_TM_ABORT);
}
/* Build a GENERIC tree for a user abort. This is called by front ends
while transforming the __tm_abort statement. */
tree
build_tm_abort_call (location_t loc, bool is_outer)
{
return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TM_ABORT), 1,
build_int_cst (integer_type_node,
AR_USERABORT
| (is_outer ? AR_OUTERABORT : 0)));
}
/* Map for aribtrary function replacement under TM, as created
by the tm_wrap attribute. */
static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
htab_t tm_wrap_map;
void
record_tm_replacement (tree from, tree to)
{
struct tree_map **slot, *h;
/* Do not inline wrapper functions that will get replaced in the TM
pass.
Suppose you have foo() that will get replaced into tmfoo(). Make
sure the inliner doesn't try to outsmart us and inline foo()
before we get a chance to do the TM replacement. */
DECL_UNINLINABLE (from) = 1;
if (tm_wrap_map == NULL)
tm_wrap_map = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
h = ggc_alloc<tree_map> ();
h->hash = htab_hash_pointer (from);
h->base.from = from;
h->to = to;
slot = (struct tree_map **)
htab_find_slot_with_hash (tm_wrap_map, h, h->hash, INSERT);
*slot = h;
}
/* Return a TM-aware replacement function for DECL. */
static tree
find_tm_replacement_function (tree fndecl)
{
if (tm_wrap_map)
{
struct tree_map *h, in;
in.base.from = fndecl;
in.hash = htab_hash_pointer (fndecl);
h = (struct tree_map *) htab_find_with_hash (tm_wrap_map, &in, in.hash);
if (h)
return h->to;
}
/* ??? We may well want TM versions of most of the common <string.h>
functions. For now, we've already these two defined. */
/* Adjust expand_call_tm() attributes as necessary for the cases
handled here: */
if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
switch (DECL_FUNCTION_CODE (fndecl))
{
case BUILT_IN_MEMCPY:
return builtin_decl_explicit (BUILT_IN_TM_MEMCPY);
case BUILT_IN_MEMMOVE:
return builtin_decl_explicit (BUILT_IN_TM_MEMMOVE);
case BUILT_IN_MEMSET:
return builtin_decl_explicit (BUILT_IN_TM_MEMSET);
default:
return NULL;
}
return NULL;
}
/* When appropriate, record TM replacement for memory allocation functions.
FROM is the FNDECL to wrap. */
void
tm_malloc_replacement (tree from)
{
const char *str;
tree to;
if (TREE_CODE (from) != FUNCTION_DECL)
return;
/* If we have a previous replacement, the user must be explicitly
wrapping malloc/calloc/free. They better know what they're
doing... */
if (find_tm_replacement_function (from))
return;
str = IDENTIFIER_POINTER (DECL_NAME (from));
if (!strcmp (str, "malloc"))
to = builtin_decl_explicit (BUILT_IN_TM_MALLOC);
else if (!strcmp (str, "calloc"))
to = builtin_decl_explicit (BUILT_IN_TM_CALLOC);
else if (!strcmp (str, "free"))
to = builtin_decl_explicit (BUILT_IN_TM_FREE);
else
return;
TREE_NOTHROW (to) = 0;
record_tm_replacement (from, to);
}
/* Diagnostics for tm_safe functions/regions. Called by the front end
once we've lowered the function to high-gimple. */
/* Subroutine of diagnose_tm_safe_errors, called through walk_gimple_seq.
Process exactly one statement. WI->INFO is set to non-null when in
the context of a tm_safe function, and null for a __transaction block. */
#define DIAG_TM_OUTER 1
#define DIAG_TM_SAFE 2
#define DIAG_TM_RELAXED 4
struct diagnose_tm
{
unsigned int summary_flags : 8;
unsigned int block_flags : 8;
unsigned int func_flags : 8;
unsigned int saw_volatile : 1;
gimple stmt;
};
/* Return true if T is a volatile variable of some kind. */
static bool
volatile_var_p (tree t)
{
return (SSA_VAR_P (t)
&& TREE_THIS_VOLATILE (TREE_TYPE (t)));
}
/* Tree callback function for diagnose_tm pass. */
static tree
diagnose_tm_1_op (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
void *data)
{
struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
struct diagnose_tm *d = (struct diagnose_tm *) wi->info;
if (volatile_var_p (*tp)
&& d->block_flags & DIAG_TM_SAFE
&& !d->saw_volatile)
{
d->saw_volatile = 1;
error_at (gimple_location (d->stmt),
"invalid volatile use of %qD inside transaction",
*tp);
}
return NULL_TREE;
}
static inline bool
is_tm_safe_or_pure (const_tree x)
{
return is_tm_safe (x) || is_tm_pure (x);
}
static tree
diagnose_tm_1 (gimple_stmt_iterator *gsi, bool *handled_ops_p,
struct walk_stmt_info *wi)
{
gimple stmt = gsi_stmt (*gsi);
struct diagnose_tm *d = (struct diagnose_tm *) wi->info;
/* Save stmt for use in leaf analysis. */
d->stmt = stmt;
switch (gimple_code (stmt))
{
case GIMPLE_CALL:
{
tree fn = gimple_call_fn (stmt);
if ((d->summary_flags & DIAG_TM_OUTER) == 0
&& is_tm_may_cancel_outer (fn))
error_at (gimple_location (stmt),
"%<transaction_may_cancel_outer%> function call not within"
" outer transaction or %<transaction_may_cancel_outer%>");
if (d->summary_flags & DIAG_TM_SAFE)
{
bool is_safe, direct_call_p;
tree replacement;
if (TREE_CODE (fn) == ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
{
direct_call_p = true;
replacement = TREE_OPERAND (fn, 0);
replacement = find_tm_replacement_function (replacement);
if (replacement)
fn = replacement;
}
else
{
direct_call_p = false;
replacement = NULL_TREE;
}
if (is_tm_safe_or_pure (fn))
is_safe = true;
else if (is_tm_callable (fn) || is_tm_irrevocable (fn))
{
/* A function explicitly marked transaction_callable as
opposed to transaction_safe is being defined to be
unsafe as part of its ABI, regardless of its contents. */
is_safe = false;
}
else if (direct_call_p)
{
if (IS_TYPE_OR_DECL_P (fn)
&& flags_from_decl_or_type (fn) & ECF_TM_BUILTIN)
is_safe = true;
else if (replacement)
{
/* ??? At present we've been considering replacements
merely transaction_callable, and therefore might
enter irrevocable. The tm_wrap attribute has not
yet made it into the new language spec. */
is_safe = false;
}
else
{
/* ??? Diagnostics for unmarked direct calls moved into
the IPA pass. Section 3.2 of the spec details how
functions not marked should be considered "implicitly
safe" based on having examined the function body. */
is_safe = true;
}
}
else
{
/* An unmarked indirect call. Consider it unsafe even
though optimization may yet figure out how to inline. */
is_safe = false;
}
if (!is_safe)
{
if (TREE_CODE (fn) == ADDR_EXPR)
fn = TREE_OPERAND (fn, 0);
if (d->block_flags & DIAG_TM_SAFE)
{
if (direct_call_p)
error_at (gimple_location (stmt),
"unsafe function call %qD within "
"atomic transaction", fn);
else
{
if (!DECL_P (fn) || DECL_NAME (fn))
error_at (gimple_location (stmt),
"unsafe function call %qE within "
"atomic transaction", fn);
else
error_at (gimple_location (stmt),
"unsafe indirect function call within "
"atomic transaction");
}
}
else
{
if (direct_call_p)
error_at (gimple_location (stmt),
"unsafe function call %qD within "
"%<transaction_safe%> function", fn);
else
{
if (!DECL_P (fn) || DECL_NAME (fn))
error_at (gimple_location (stmt),
"unsafe function call %qE within "
"%<transaction_safe%> function", fn);
else
error_at (gimple_location (stmt),
"unsafe indirect function call within "
"%<transaction_safe%> function");
}
}
}
}
}
break;
case GIMPLE_ASM:
/* ??? We ought to come up with a way to add attributes to
asm statements, and then add "transaction_safe" to it.
Either that or get the language spec to resurrect __tm_waiver. */
if (d->block_flags & DIAG_TM_SAFE)
error_at (gimple_location (stmt),
"asm not allowed in atomic transaction");
else if (d->func_flags & DIAG_TM_SAFE)
error_at (gimple_location (stmt),
"asm not allowed in %<transaction_safe%> function");
break;
case GIMPLE_TRANSACTION:
{
unsigned char inner_flags = DIAG_TM_SAFE;
if (gimple_transaction_subcode (stmt) & GTMA_IS_RELAXED)
{
if (d->block_flags & DIAG_TM_SAFE)
error_at (gimple_location (stmt),
"relaxed transaction in atomic transaction");
else if (d->func_flags & DIAG_TM_SAFE)
error_at (gimple_location (stmt),
"relaxed transaction in %<transaction_safe%> function");
inner_flags = DIAG_TM_RELAXED;
}
else if (gimple_transaction_subcode (stmt) & GTMA_IS_OUTER)
{
if (d->block_flags)
error_at (gimple_location (stmt),
"outer transaction in transaction");
else if (d->func_flags & DIAG_TM_OUTER)
error_at (gimple_location (stmt),
"outer transaction in "
"%<transaction_may_cancel_outer%> function");
else if (d->func_flags & DIAG_TM_SAFE)
error_at (gimple_location (stmt),
"outer transaction in %<transaction_safe%> function");
inner_flags |= DIAG_TM_OUTER;
}
*handled_ops_p = true;
if (gimple_transaction_body (stmt))
{
struct walk_stmt_info wi_inner;
struct diagnose_tm d_inner;
memset (&d_inner, 0, sizeof (d_inner));
d_inner.func_flags = d->func_flags;
d_inner.block_flags = d->block_flags | inner_flags;
d_inner.summary_flags = d_inner.func_flags | d_inner.block_flags;
memset (&wi_inner, 0, sizeof (wi_inner));
wi_inner.info = &d_inner;
walk_gimple_seq (gimple_transaction_body (stmt),
diagnose_tm_1, diagnose_tm_1_op, &wi_inner);
}
}
break;
default:
break;
}
return NULL_TREE;
}
static unsigned int
diagnose_tm_blocks (void)
{
struct walk_stmt_info wi;
struct diagnose_tm d;
memset (&d, 0, sizeof (d));
if (is_tm_may_cancel_outer (current_function_decl))
d.func_flags = DIAG_TM_OUTER | DIAG_TM_SAFE;
else if (is_tm_safe (current_function_decl))
d.func_flags = DIAG_TM_SAFE;
d.summary_flags = d.func_flags;
memset (&wi, 0, sizeof (wi));
wi.info = &d;
walk_gimple_seq (gimple_body (current_function_decl),
diagnose_tm_1, diagnose_tm_1_op, &wi);
return 0;
}
namespace {
const pass_data pass_data_diagnose_tm_blocks =
{
GIMPLE_PASS, /* type */
"*diagnose_tm_blocks", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_TRANS_MEM, /* tv_id */
PROP_gimple_any, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_diagnose_tm_blocks : public gimple_opt_pass
{
public:
pass_diagnose_tm_blocks (gcc::context *ctxt)
: gimple_opt_pass (pass_data_diagnose_tm_blocks, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *) { return flag_tm; }
virtual unsigned int execute (function *) { return diagnose_tm_blocks (); }
}; // class pass_diagnose_tm_blocks
} // anon namespace
gimple_opt_pass *
make_pass_diagnose_tm_blocks (gcc::context *ctxt)
{
return new pass_diagnose_tm_blocks (ctxt);
}
/* Instead of instrumenting thread private memory, we save the
addresses in a log which we later use to save/restore the addresses
upon transaction start/restart.
The log is keyed by address, where each element contains individual
statements among different code paths that perform the store.
This log is later used to generate either plain save/restore of the
addresses upon transaction start/restart, or calls to the ITM_L*
logging functions.
So for something like:
struct large { int x[1000]; };
struct large lala = { 0 };
__transaction {
lala.x[i] = 123;
...
}
We can either save/restore:
lala = { 0 };
trxn = _ITM_startTransaction ();
if (trxn & a_saveLiveVariables)
tmp_lala1 = lala.x[i];
else if (a & a_restoreLiveVariables)
lala.x[i] = tmp_lala1;
or use the logging functions:
lala = { 0 };
trxn = _ITM_startTransaction ();
_ITM_LU4 (&lala.x[i]);
Obviously, if we use _ITM_L* to log, we prefer to call _ITM_L* as
far up the dominator tree to shadow all of the writes to a given
location (thus reducing the total number of logging calls), but not
so high as to be called on a path that does not perform a
write. */
/* One individual log entry. We may have multiple statements for the
same location if neither dominate each other (on different
execution paths). */
typedef struct tm_log_entry
{
/* Address to save. */
tree addr;
/* Entry block for the transaction this address occurs in. */
basic_block entry_block;
/* Dominating statements the store occurs in. */
gimple_vec stmts;
/* Initially, while we are building the log, we place a nonzero
value here to mean that this address *will* be saved with a
save/restore sequence. Later, when generating the save sequence
we place the SSA temp generated here. */
tree save_var;
} *tm_log_entry_t;
/* Log entry hashtable helpers. */
struct log_entry_hasher
{
typedef tm_log_entry value_type;
typedef tm_log_entry compare_type;
static inline hashval_t hash (const value_type *);
static inline bool equal (const value_type *, const compare_type *);
static inline void remove (value_type *);
};
/* Htab support. Return hash value for a `tm_log_entry'. */
inline hashval_t
log_entry_hasher::hash (const value_type *log)
{
return iterative_hash_expr (log->addr, 0);
}
/* Htab support. Return true if two log entries are the same. */
inline bool
log_entry_hasher::equal (const value_type *log1, const compare_type *log2)
{
/* FIXME:
rth: I suggest that we get rid of the component refs etc.
I.e. resolve the reference to base + offset.
We may need to actually finish a merge with mainline for this,
since we'd like to be presented with Richi's MEM_REF_EXPRs more
often than not. But in the meantime your tm_log_entry could save
the results of get_inner_reference.
See: g++.dg/tm/pr46653.C
*/
/* Special case plain equality because operand_equal_p() below will
return FALSE if the addresses are equal but they have
side-effects (e.g. a volatile address). */
if (log1->addr == log2->addr)
return true;
return operand_equal_p (log1->addr, log2->addr, 0);
}
/* Htab support. Free one tm_log_entry. */
inline void
log_entry_hasher::remove (value_type *lp)
{
lp->stmts.release ();
free (lp);
}
/* The actual log. */
static hash_table<log_entry_hasher> *tm_log;
/* Addresses to log with a save/restore sequence. These should be in
dominator order. */
static vec<tree> tm_log_save_addresses;
enum thread_memory_type
{
mem_non_local = 0,
mem_thread_local,
mem_transaction_local,
mem_max
};
typedef struct tm_new_mem_map
{
/* SSA_NAME being dereferenced. */
tree val;
enum thread_memory_type local_new_memory;
} tm_new_mem_map_t;
/* Hashtable helpers. */
struct tm_mem_map_hasher : typed_free_remove <tm_new_mem_map_t>
{
typedef tm_new_mem_map_t value_type;
typedef tm_new_mem_map_t compare_type;
static inline hashval_t hash (const value_type *);
static inline bool equal (const value_type *, const compare_type *);
};
inline hashval_t
tm_mem_map_hasher::hash (const value_type *v)
{
return (intptr_t)v->val >> 4;
}
inline bool
tm_mem_map_hasher::equal (const value_type *v, const compare_type *c)
{
return v->val == c->val;
}
/* Map for an SSA_NAME originally pointing to a non aliased new piece
of memory (malloc, alloc, etc). */
static hash_table<tm_mem_map_hasher> *tm_new_mem_hash;
/* Initialize logging data structures. */
static void
tm_log_init (void)
{
tm_log = new hash_table<log_entry_hasher> (10);
tm_new_mem_hash = new hash_table<tm_mem_map_hasher> (5);
tm_log_save_addresses.create (5);
}
/* Free logging data structures. */
static void
tm_log_delete (void)
{
delete tm_log;
tm_log = NULL;
delete tm_new_mem_hash;
tm_new_mem_hash = NULL;
tm_log_save_addresses.release ();
}
/* Return true if MEM is a transaction invariant memory for the TM
region starting at REGION_ENTRY_BLOCK. */
static bool
transaction_invariant_address_p (const_tree mem, basic_block region_entry_block)
{
if ((TREE_CODE (mem) == INDIRECT_REF || TREE_CODE (mem) == MEM_REF)
&& TREE_CODE (TREE_OPERAND (mem, 0)) == SSA_NAME)
{
basic_block def_bb;
def_bb = gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (mem, 0)));
return def_bb != region_entry_block
&& dominated_by_p (CDI_DOMINATORS, region_entry_block, def_bb);
}
mem = strip_invariant_refs (mem);
return mem && (CONSTANT_CLASS_P (mem) || decl_address_invariant_p (mem));
}
/* Given an address ADDR in STMT, find it in the memory log or add it,
making sure to keep only the addresses highest in the dominator
tree.
ENTRY_BLOCK is the entry_block for the transaction.
If we find the address in the log, make sure it's either the same
address, or an equivalent one that dominates ADDR.
If we find the address, but neither ADDR dominates the found
address, nor the found one dominates ADDR, we're on different
execution paths. Add it.
If known, ENTRY_BLOCK is the entry block for the region, otherwise
NULL. */
static void
tm_log_add (basic_block entry_block, tree addr, gimple stmt)
{
tm_log_entry **slot;
struct tm_log_entry l, *lp;
l.addr = addr;
slot = tm_log->find_slot (&l, INSERT);
if (!*slot)
{
tree type = TREE_TYPE (addr);
lp = XNEW (struct tm_log_entry);
lp->addr = addr;
*slot = lp;
/* Small invariant addresses can be handled as save/restores. */
if (entry_block
&& transaction_invariant_address_p (lp->addr, entry_block)
&& TYPE_SIZE_UNIT (type) != NULL
&& tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
&& ((HOST_WIDE_INT) tree_to_uhwi (TYPE_SIZE_UNIT (type))
< PARAM_VALUE (PARAM_TM_MAX_AGGREGATE_SIZE))
/* We must be able to copy this type normally. I.e., no
special constructors and the like. */
&& !TREE_ADDRESSABLE (type))
{
lp->save_var = create_tmp_reg (TREE_TYPE (lp->addr), "tm_save");
lp->stmts.create (0);
lp->entry_block = entry_block;
/* Save addresses separately in dominator order so we don't
get confused by overlapping addresses in the save/restore
sequence. */
tm_log_save_addresses.safe_push (lp->addr);
}
else
{
/* Use the logging functions. */
lp->stmts.create (5);
lp->stmts.quick_push (stmt);
lp->save_var = NULL;
}
}
else
{
size_t i;
gimple oldstmt;
lp = *slot;
/* If we're generating a save/restore sequence, we don't care
about statements. */
if (lp->save_var)
return;
for (i = 0; lp->stmts.iterate (i, &oldstmt); ++i)
{
if (stmt == oldstmt)
return;
/* We already have a store to the same address, higher up the
dominator tree. Nothing to do. */
if (dominated_by_p (CDI_DOMINATORS,
gimple_bb (stmt), gimple_bb (oldstmt)))
return;
/* We should be processing blocks in dominator tree order. */
gcc_assert (!dominated_by_p (CDI_DOMINATORS,
gimple_bb (oldstmt), gimple_bb (stmt)));
}
/* Store is on a different code path. */
lp->stmts.safe_push (stmt);
}
}
/* Gimplify the address of a TARGET_MEM_REF. Return the SSA_NAME
result, insert the new statements before GSI. */
static tree
gimplify_addr (gimple_stmt_iterator *gsi, tree x)
{
if (TREE_CODE (x) == TARGET_MEM_REF)
x = tree_mem_ref_addr (build_pointer_type (TREE_TYPE (x)), x);
else
x = build_fold_addr_expr (x);
return force_gimple_operand_gsi (gsi, x, true, NULL, true, GSI_SAME_STMT);
}
/* Instrument one address with the logging functions.
ADDR is the address to save.
STMT is the statement before which to place it. */
static void
tm_log_emit_stmt (tree addr, gimple stmt)
{
tree type = TREE_TYPE (addr);
tree size = TYPE_SIZE_UNIT (type);
gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
gimple log;
enum built_in_function code = BUILT_IN_TM_LOG;
if (type == float_type_node)
code = BUILT_IN_TM_LOG_FLOAT;
else if (type == double_type_node)
code = BUILT_IN_TM_LOG_DOUBLE;
else if (type == long_double_type_node)
code = BUILT_IN_TM_LOG_LDOUBLE;
else if (tree_fits_uhwi_p (size))
{
unsigned int n = tree_to_uhwi (size);
switch (n)
{
case 1:
code = BUILT_IN_TM_LOG_1;
break;
case 2:
code = BUILT_IN_TM_LOG_2;
break;
case 4:
code = BUILT_IN_TM_LOG_4;
break;
case 8:
code = BUILT_IN_TM_LOG_8;
break;
default:
code = BUILT_IN_TM_LOG;
if (TREE_CODE (type) == VECTOR_TYPE)
{
if (n == 8 && builtin_decl_explicit (BUILT_IN_TM_LOG_M64))
code = BUILT_IN_TM_LOG_M64;
else if (n == 16 && builtin_decl_explicit (BUILT_IN_TM_LOG_M128))
code = BUILT_IN_TM_LOG_M128;
else if (n == 32 && builtin_decl_explicit (BUILT_IN_TM_LOG_M256))
code = BUILT_IN_TM_LOG_M256;
}
break;
}
}
addr = gimplify_addr (&gsi, addr);
if (code == BUILT_IN_TM_LOG)
log = gimple_build_call (builtin_decl_explicit (code), 2, addr, size);
else
log = gimple_build_call (builtin_decl_explicit (code), 1, addr);
gsi_insert_before (&gsi, log, GSI_SAME_STMT);
}
/* Go through the log and instrument address that must be instrumented
with the logging functions. Leave the save/restore addresses for
later. */
static void
tm_log_emit (void)
{
hash_table<log_entry_hasher>::iterator hi;
struct tm_log_entry *lp;
FOR_EACH_HASH_TABLE_ELEMENT (*tm_log, lp, tm_log_entry_t, hi)
{
size_t i;
gimple stmt;
if (dump_file)
{
fprintf (dump_file, "TM thread private mem logging: ");
print_generic_expr (dump_file, lp->addr, 0);
fprintf (dump_file, "\n");
}
if (lp->save_var)
{
if (dump_file)
fprintf (dump_file, "DUMPING to variable\n");
continue;
}
else
{
if (dump_file)
fprintf (dump_file, "DUMPING with logging functions\n");
for (i = 0; lp->stmts.iterate (i, &stmt); ++i)
tm_log_emit_stmt (lp->addr, stmt);
}
}
}
/* Emit the save sequence for the corresponding addresses in the log.
ENTRY_BLOCK is the entry block for the transaction.
BB is the basic block to insert the code in. */
static void
tm_log_emit_saves (basic_block entry_block, basic_block bb)
{
size_t i;
gimple_stmt_iterator gsi = gsi_last_bb (bb);
gimple stmt;
struct tm_log_entry l, *lp;
for (i = 0; i < tm_log_save_addresses.length (); ++i)
{
l.addr = tm_log_save_addresses[i];
lp = *(tm_log->find_slot (&l, NO_INSERT));
gcc_assert (lp->save_var != NULL);
/* We only care about variables in the current transaction. */
if (lp->entry_block != entry_block)
continue;
stmt = gimple_build_assign (lp->save_var, unshare_expr (lp->addr));
/* Make sure we can create an SSA_NAME for this type. For
instance, aggregates aren't allowed, in which case the system
will create a VOP for us and everything will just work. */
if (is_gimple_reg_type (TREE_TYPE (lp->save_var)))
{
lp->save_var = make_ssa_name (lp->save_var, stmt);
gimple_assign_set_lhs (stmt, lp->save_var);
}
gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
}
}
/* Emit the restore sequence for the corresponding addresses in the log.
ENTRY_BLOCK is the entry block for the transaction.
BB is the basic block to insert the code in. */
static void
tm_log_emit_restores (basic_block entry_block, basic_block bb)
{
int i;
struct tm_log_entry l, *lp;
gimple_stmt_iterator gsi;
gimple stmt;
for (i = tm_log_save_addresses.length () - 1; i >= 0; i--)
{
l.addr = tm_log_save_addresses[i];
lp = *(tm_log->find_slot (&l, NO_INSERT));
gcc_assert (lp->save_var != NULL);
/* We only care about variables in the current transaction. */
if (lp->entry_block != entry_block)
continue;
/* Restores are in LIFO order from the saves in case we have
overlaps. */
gsi = gsi_start_bb (bb);
stmt = gimple_build_assign (unshare_expr (lp->addr), lp->save_var);
gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
}
}
static tree lower_sequence_tm (gimple_stmt_iterator *, bool *,
struct walk_stmt_info *);
static tree lower_sequence_no_tm (gimple_stmt_iterator *, bool *,
struct walk_stmt_info *);
/* Evaluate an address X being dereferenced and determine if it
originally points to a non aliased new chunk of memory (malloc,
alloca, etc).
Return MEM_THREAD_LOCAL if it points to a thread-local address.
Return MEM_TRANSACTION_LOCAL if it points to a transaction-local address.
Return MEM_NON_LOCAL otherwise.
ENTRY_BLOCK is the entry block to the transaction containing the
dereference of X. */
static enum thread_memory_type
thread_private_new_memory (basic_block entry_block, tree x)
{
gimple stmt = NULL;
enum tree_code code;
tm_new_mem_map_t **slot;
tm_new_mem_map_t elt, *elt_p;
tree val = x;
enum thread_memory_type retval = mem_transaction_local;
if (!entry_block
|| TREE_CODE (x) != SSA_NAME
/* Possible uninitialized use, or a function argument. In
either case, we don't care. */
|| SSA_NAME_IS_DEFAULT_DEF (x))
return mem_non_local;
/* Look in cache first. */
elt.val = x;
slot = tm_new_mem_hash->find_slot (&elt, INSERT);
elt_p = *slot;
if (elt_p)
return elt_p->local_new_memory;
/* Optimistically assume the memory is transaction local during
processing. This catches recursion into this variable. */
*slot = elt_p = XNEW (tm_new_mem_map_t);
elt_p->val = val;
elt_p->local_new_memory = mem_transaction_local;
/* Search DEF chain to find the original definition of this address. */
do
{
if (ptr_deref_may_alias_global_p (x))
{
/* Address escapes. This is not thread-private. */
retval = mem_non_local;
goto new_memory_ret;
}
stmt = SSA_NAME_DEF_STMT (x);
/* If the malloc call is outside the transaction, this is
thread-local. */
if (retval != mem_thread_local
&& !dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt), entry_block))
retval = mem_thread_local;
if (is_gimple_assign (stmt))
{
code = gimple_assign_rhs_code (stmt);
/* x = foo ==> foo */
if (code == SSA_NAME)
x = gimple_assign_rhs1 (stmt);
/* x = foo + n ==> foo */
else if (code == POINTER_PLUS_EXPR)
x = gimple_assign_rhs1 (stmt);
/* x = (cast*) foo ==> foo */
else if (code == VIEW_CONVERT_EXPR || code == NOP_EXPR)
x = gimple_assign_rhs1 (stmt);
/* x = c ? op1 : op2 == > op1 or op2 just like a PHI */
else if (code == COND_EXPR)
{
tree op1 = gimple_assign_rhs2 (stmt);
tree op2 = gimple_assign_rhs3 (stmt);
enum thread_memory_type mem;
retval = thread_private_new_memory (entry_block, op1);
if (retval == mem_non_local)
goto new_memory_ret;
mem = thread_private_new_memory (entry_block, op2);
retval = MIN (retval, mem);
goto new_memory_ret;
}
else
{
retval = mem_non_local;
goto new_memory_ret;
}
}
else
{
if (gimple_code (stmt) == GIMPLE_PHI)
{
unsigned int i;
enum thread_memory_type mem;
tree phi_result = gimple_phi_result (stmt);
/* If any of the ancestors are non-local, we are sure to
be non-local. Otherwise we can avoid doing anything
and inherit what has already been generated. */
retval = mem_max;
for (i = 0; i < gimple_phi_num_args (stmt); ++i)
{
tree op = PHI_ARG_DEF (stmt, i);
/* Exclude self-assignment. */
if (phi_result == op)
continue;
mem = thread_private_new_memory (entry_block, op);
if (mem == mem_non_local)
{
retval = mem;
goto new_memory_ret;
}
retval = MIN (retval, mem);
}
goto new_memory_ret;
}
break;
}
}
while (TREE_CODE (x) == SSA_NAME);
if (stmt && is_gimple_call (stmt) && gimple_call_flags (stmt) & ECF_MALLOC)
/* Thread-local or transaction-local. */
;
else
retval = mem_non_local;
new_memory_ret:
elt_p->local_new_memory = retval;
return retval;
}
/* Determine whether X has to be instrumented using a read
or write barrier.
ENTRY_BLOCK is the entry block for the region where stmt resides
in. NULL if unknown.
STMT is the statement in which X occurs in. It is used for thread
private memory instrumentation. If no TPM instrumentation is
desired, STMT should be null. */
static bool
requires_barrier (basic_block entry_block, tree x, gimple stmt)
{
tree orig = x;
while (handled_component_p (x))
x = TREE_OPERAND (x, 0);
switch (TREE_CODE (x))
{
case INDIRECT_REF:
case MEM_REF:
{
enum thread_memory_type ret;
ret = thread_private_new_memory (entry_block, TREE_OPERAND (x, 0));
if (ret == mem_non_local)
return true;
if (stmt && ret == mem_thread_local)
/* ?? Should we pass `orig', or the INDIRECT_REF X. ?? */
tm_log_add (entry_block, orig, stmt);
/* Transaction-locals require nothing at all. For malloc, a
transaction restart frees the memory and we reallocate.
For alloca, the stack pointer gets reset by the retry and
we reallocate. */
return false;
}
case TARGET_MEM_REF:
if (TREE_CODE (TMR_BASE (x)) != ADDR_EXPR)
return true;
x = TREE_OPERAND (TMR_BASE (x), 0);
if (TREE_CODE (x) == PARM_DECL)
return false;
gcc_assert (TREE_CODE (x) == VAR_DECL);
/* FALLTHRU */
case PARM_DECL:
case RESULT_DECL:
case VAR_DECL:
if (DECL_BY_REFERENCE (x))
{
/* ??? This value is a pointer, but aggregate_value_p has been
jigged to return true which confuses needs_to_live_in_memory.
This ought to be cleaned up generically.
FIXME: Verify this still happens after the next mainline
merge. Testcase ie g++.dg/tm/pr47554.C.
*/
return false;
}
if (is_global_var (x))
return !TREE_READONLY (x);
if (/* FIXME: This condition should actually go below in the
tm_log_add() call, however is_call_clobbered() depends on
aliasing info which is not available during
gimplification. Since requires_barrier() gets called
during lower_sequence_tm/gimplification, leave the call
to needs_to_live_in_memory until we eliminate
lower_sequence_tm altogether. */
needs_to_live_in_memory (x))
return true;
else
{
/* For local memory that doesn't escape (aka thread private
memory), we can either save the value at the beginning of
the transaction and restore on restart, or call a tm
function to dynamically save and restore on restart
(ITM_L*). */
if (stmt)
tm_log_add (entry_block, orig, stmt);
return false;
}
default:
return false;
}
}
/* Mark the GIMPLE_ASSIGN statement as appropriate for being inside
a transaction region. */
static void
examine_assign_tm (unsigned *state, gimple_stmt_iterator *gsi)
{
gimple stmt = gsi_stmt (*gsi);
if (requires_barrier (/*entry_block=*/NULL, gimple_assign_rhs1 (stmt), NULL))
*state |= GTMA_HAVE_LOAD;
if (requires_barrier (/*entry_block=*/NULL, gimple_assign_lhs (stmt), NULL))
*state |= GTMA_HAVE_STORE;
}
/* Mark a GIMPLE_CALL as appropriate for being inside a transaction. */
static void
examine_call_tm (unsigned *state, gimple_stmt_iterator *gsi)
{
gimple stmt = gsi_stmt (*gsi);
tree fn;
if (is_tm_pure_call (stmt))
return;
/* Check if this call is a transaction abort. */
fn = gimple_call_fndecl (stmt);
if (is_tm_abort (fn))
*state |= GTMA_HAVE_ABORT;
/* Note that something may happen. */
*state |= GTMA_HAVE_LOAD | GTMA_HAVE_STORE;
}
/* Lower a GIMPLE_TRANSACTION statement. */
static void
lower_transaction (gimple_stmt_iterator *gsi, struct walk_stmt_info *wi)
{
gimple g, stmt = gsi_stmt (*gsi);
unsigned int *outer_state = (unsigned int *) wi->info;
unsigned int this_state = 0;
struct walk_stmt_info this_wi;
/* First, lower the body. The scanning that we do inside gives
us some idea of what we're dealing with. */
memset (&this_wi, 0, sizeof (this_wi));
this_wi.info = (void *) &this_state;
walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
lower_sequence_tm, NULL, &this_wi);
/* If there was absolutely nothing transaction related inside the
transaction, we may elide it. Likewise if this is a nested
transaction and does not contain an abort. */
if (this_state == 0
|| (!(this_state & GTMA_HAVE_ABORT) && outer_state != NULL))
{
if (outer_state)
*outer_state |= this_state;
gsi_insert_seq_before (gsi, gimple_transaction_body (stmt),
GSI_SAME_STMT);
gimple_transaction_set_body (stmt, NULL);
gsi_remove (gsi, true);
wi->removed_stmt = true;
return;
}
/* Wrap the body of the transaction in a try-finally node so that
the commit call is always properly called. */
g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT), 0);
if (flag_exceptions)
{
tree ptr;
gimple_seq n_seq, e_seq;
n_seq = gimple_seq_alloc_with_stmt (g);
e_seq = NULL;
g = gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER),
1, integer_zero_node);
ptr = create_tmp_var (ptr_type_node, NULL);
gimple_call_set_lhs (g, ptr);
gimple_seq_add_stmt (&e_seq, g);
g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH),
1, ptr);
gimple_seq_add_stmt (&e_seq, g);
g = gimple_build_eh_else (n_seq, e_seq);
}
g = gimple_build_try (gimple_transaction_body (stmt),
gimple_seq_alloc_with_stmt (g), GIMPLE_TRY_FINALLY);
gsi_insert_after (gsi, g, GSI_CONTINUE_LINKING);
gimple_transaction_set_body (stmt, NULL);
/* If the transaction calls abort or if this is an outer transaction,
add an "over" label afterwards. */
if ((this_state & (GTMA_HAVE_ABORT))
|| (gimple_transaction_subcode (stmt) & GTMA_IS_OUTER))
{
tree label = create_artificial_label (UNKNOWN_LOCATION);
gimple_transaction_set_label (stmt, label);
gsi_insert_after (gsi, gimple_build_label (label), GSI_CONTINUE_LINKING);
}
/* Record the set of operations found for use later. */
this_state |= gimple_transaction_subcode (stmt) & GTMA_DECLARATION_MASK;
gimple_transaction_set_subcode (stmt, this_state);
}
/* Iterate through the statements in the sequence, lowering them all
as appropriate for being in a transaction. */
static tree
lower_sequence_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p,
struct walk_stmt_info *wi)
{
unsigned int *state = (unsigned int *) wi->info;
gimple stmt = gsi_stmt (*gsi);
*handled_ops_p = true;
switch (gimple_code (stmt))
{
case GIMPLE_ASSIGN:
/* Only memory reads/writes need to be instrumented. */
if (gimple_assign_single_p (stmt))
examine_assign_tm (state, gsi);
break;
case GIMPLE_CALL:
examine_call_tm (state, gsi);
break;
case GIMPLE_ASM:
*state |= GTMA_MAY_ENTER_IRREVOCABLE;
break;
case GIMPLE_TRANSACTION:
lower_transaction (gsi, wi);
break;
default:
*handled_ops_p = !gimple_has_substatements (stmt);
break;
}
return NULL_TREE;
}
/* Iterate through the statements in the sequence, lowering them all
as appropriate for being outside of a transaction. */
static tree
lower_sequence_no_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p,
struct walk_stmt_info * wi)
{
gimple stmt = gsi_stmt (*gsi);
if (gimple_code (stmt) == GIMPLE_TRANSACTION)
{
*handled_ops_p = true;
lower_transaction (gsi, wi);
}
else
*handled_ops_p = !gimple_has_substatements (stmt);
return NULL_TREE;
}
/* Main entry point for flattening GIMPLE_TRANSACTION constructs. After
this, GIMPLE_TRANSACTION nodes still exist, but the nested body has
been moved out, and all the data required for constructing a proper
CFG has been recorded. */
static unsigned int
execute_lower_tm (void)
{
struct walk_stmt_info wi;
gimple_seq body;
/* Transactional clones aren't created until a later pass. */
gcc_assert (!decl_is_tm_clone (current_function_decl));
body = gimple_body (current_function_decl);
memset (&wi, 0, sizeof (wi));
walk_gimple_seq_mod (&body, lower_sequence_no_tm, NULL, &wi);
gimple_set_body (current_function_decl, body);
return 0;
}
namespace {
const pass_data pass_data_lower_tm =
{
GIMPLE_PASS, /* type */
"tmlower", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_TRANS_MEM, /* tv_id */
PROP_gimple_lcf, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_lower_tm : public gimple_opt_pass
{
public:
pass_lower_tm (gcc::context *ctxt)
: gimple_opt_pass (pass_data_lower_tm, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *) { return flag_tm; }
virtual unsigned int execute (function *) { return execute_lower_tm (); }
}; // class pass_lower_tm
} // anon namespace
gimple_opt_pass *
make_pass_lower_tm (gcc::context *ctxt)
{
return new pass_lower_tm (ctxt);
}
/* Collect region information for each transaction. */
struct tm_region
{
/* Link to the next unnested transaction. */
struct tm_region *next;
/* Link to the next inner transaction. */
struct tm_region *inner;
/* Link to the next outer transaction. */
struct tm_region *outer;
/* The GIMPLE_TRANSACTION statement beginning this transaction.
After TM_MARK, this gets replaced by a call to
BUILT_IN_TM_START. */
gimple transaction_stmt;
/* After TM_MARK expands the GIMPLE_TRANSACTION into a call to
BUILT_IN_TM_START, this field is true if the transaction is an
outer transaction. */
bool original_transaction_was_outer;
/* Return value from BUILT_IN_TM_START. */
tree tm_state;
/* The entry block to this region. This will always be the first
block of the body of the transaction. */
basic_block entry_block;
/* The first block after an expanded call to _ITM_beginTransaction. */
basic_block restart_block;
/* The set of all blocks that end the region; NULL if only EXIT_BLOCK.
These blocks are still a part of the region (i.e., the border is
inclusive). Note that this set is only complete for paths in the CFG
starting at ENTRY_BLOCK, and that there is no exit block recorded for
the edge to the "over" label. */
bitmap exit_blocks;
/* The set of all blocks that have an TM_IRREVOCABLE call. */
bitmap irr_blocks;
};
typedef struct tm_region *tm_region_p;
/* True if there are pending edge statements to be committed for the
current function being scanned in the tmmark pass. */
bool pending_edge_inserts_p;
static struct tm_region *all_tm_regions;
static bitmap_obstack tm_obstack;
/* A subroutine of tm_region_init. Record the existence of the
GIMPLE_TRANSACTION statement in a tree of tm_region elements. */
static struct tm_region *
tm_region_init_0 (struct tm_region *outer, basic_block bb, gimple stmt)
{
struct tm_region *region;
region = (struct tm_region *)
obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region));
if (outer)
{
region->next = outer->inner;
outer->inner = region;
}
else
{
region->next = all_tm_regions;
all_tm_regions = region;
}
region->inner = NULL;
region->outer = outer;
region->transaction_stmt = stmt;
region->original_transaction_was_outer = false;
region->tm_state = NULL;
/* There are either one or two edges out of the block containing
the GIMPLE_TRANSACTION, one to the actual region and one to the
"over" label if the region contains an abort. The former will
always be the one marked FALLTHRU. */
region->entry_block = FALLTHRU_EDGE (bb)->dest;
region->exit_blocks = BITMAP_ALLOC (&tm_obstack);
region->irr_blocks = BITMAP_ALLOC (&tm_obstack);
return region;
}
/* A subroutine of tm_region_init. Record all the exit and
irrevocable blocks in BB into the region's exit_blocks and
irr_blocks bitmaps. Returns the new region being scanned. */
static struct tm_region *
tm_region_init_1 (struct tm_region *region, basic_block bb)
{
gimple_stmt_iterator gsi;
gimple g;
if (!region
|| (!region->irr_blocks && !region->exit_blocks))
return region;
/* Check to see if this is the end of a region by seeing if it
contains a call to __builtin_tm_commit{,_eh}. Note that the
outermost region for DECL_IS_TM_CLONE need not collect this. */
for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
{
g = gsi_stmt (gsi);
if (gimple_code (g) == GIMPLE_CALL)
{
tree fn = gimple_call_fndecl (g);
if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
{
if ((DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT
|| DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT_EH)
&& region->exit_blocks)
{
bitmap_set_bit (region->exit_blocks, bb->index);
region = region->outer;
break;
}
if (DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_IRREVOCABLE)
bitmap_set_bit (region->irr_blocks, bb->index);
}
}
}
return region;
}
/* Collect all of the transaction regions within the current function
and record them in ALL_TM_REGIONS. The REGION parameter may specify
an "outermost" region for use by tm clones. */
static void
tm_region_init (struct tm_region *region)
{
gimple g;
edge_iterator ei;
edge e;
basic_block bb;
auto_vec<basic_block> queue;
bitmap visited_blocks = BITMAP_ALLOC (NULL);
struct tm_region *old_region;
auto_vec<tm_region_p> bb_regions;
all_tm_regions = region;
bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
/* We could store this information in bb->aux, but we may get called
through get_all_tm_blocks() from another pass that may be already
using bb->aux. */
bb_regions.safe_grow_cleared (last_basic_block_for_fn (cfun));
queue.safe_push (bb);
bb_regions[bb->index] = region;
do
{
bb = queue.pop ();
region = bb_regions[bb->index];
bb_regions[bb->index] = NULL;
/* Record exit and irrevocable blocks. */
region = tm_region_init_1 (region, bb);
/* Check for the last statement in the block beginning a new region. */
g = last_stmt (bb);
old_region = region;
if (g && gimple_code (g) == GIMPLE_TRANSACTION)
region = tm_region_init_0 (region, bb, g);
/* Process subsequent blocks. */
FOR_EACH_EDGE (e, ei, bb->succs)
if (!bitmap_bit_p (visited_blocks, e->dest->index))
{
bitmap_set_bit (visited_blocks, e->dest->index);
queue.safe_push (e->dest);
/* If the current block started a new region, make sure that only
the entry block of the new region is associated with this region.
Other successors are still part of the old region. */
if (old_region != region && e->dest != region->entry_block)
bb_regions[e->dest->index] = old_region;
else
bb_regions[e->dest->index] = region;
}
}
while (!queue.is_empty ());
BITMAP_FREE (visited_blocks);
}
/* The "gate" function for all transactional memory expansion and optimization
passes. We collect region information for each top-level transaction, and
if we don't find any, we skip all of the TM passes. Each region will have
all of the exit blocks recorded, and the originating statement. */
static bool
gate_tm_init (void)
{
if (!flag_tm)
return false;
calculate_dominance_info (CDI_DOMINATORS);
bitmap_obstack_initialize (&tm_obstack);
/* If the function is a TM_CLONE, then the entire function is the region. */
if (decl_is_tm_clone (current_function_decl))
{
struct tm_region *region = (struct tm_region *)
obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region));
memset (region, 0, sizeof (*region));
region->entry_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
/* For a clone, the entire function is the region. But even if
we don't need to record any exit blocks, we may need to
record irrevocable blocks. */
region->irr_blocks = BITMAP_ALLOC (&tm_obstack);
tm_region_init (region);
}
else
{
tm_region_init (NULL);
/* If we didn't find any regions, cleanup and skip the whole tree
of tm-related optimizations. */
if (all_tm_regions == NULL)
{
bitmap_obstack_release (&tm_obstack);
return false;
}
}
return true;
}
namespace {
const pass_data pass_data_tm_init =
{
GIMPLE_PASS, /* type */
"*tminit", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_TRANS_MEM, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_tm_init : public gimple_opt_pass
{
public:
pass_tm_init (gcc::context *ctxt)
: gimple_opt_pass (pass_data_tm_init, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *) { return gate_tm_init (); }
}; // class pass_tm_init
} // anon namespace
gimple_opt_pass *
make_pass_tm_init (gcc::context *ctxt)
{
return new pass_tm_init (ctxt);
}
/* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region
represented by STATE. */
static inline void
transaction_subcode_ior (struct tm_region *region, unsigned flags)
{
if (region && region->transaction_stmt)
{
flags |= gimple_transaction_subcode (region->transaction_stmt);
gimple_transaction_set_subcode (region->transaction_stmt, flags);
}
}
/* Construct a memory load in a transactional context. Return the
gimple statement performing the load, or NULL if there is no
TM_LOAD builtin of the appropriate size to do the load.
LOC is the location to use for the new statement(s). */
static gimple
build_tm_load (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi)
{
enum built_in_function code = END_BUILTINS;
tree t, type = TREE_TYPE (rhs), decl;
gimple gcall;
if (type == float_type_node)
code = BUILT_IN_TM_LOAD_FLOAT;
else if (type == double_type_node)
code = BUILT_IN_TM_LOAD_DOUBLE;
else if (type == long_double_type_node)
code = BUILT_IN_TM_LOAD_LDOUBLE;
else if (TYPE_SIZE_UNIT (type) != NULL
&& tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
{
switch (tree_to_uhwi (TYPE_SIZE_UNIT (type)))
{
case 1:
code = BUILT_IN_TM_LOAD_1;
break;
case 2:
code = BUILT_IN_TM_LOAD_2;
break;
case 4:
code = BUILT_IN_TM_LOAD_4;
break;
case 8:
code = BUILT_IN_TM_LOAD_8;
break;
}
}
if (code == END_BUILTINS)
{
decl = targetm.vectorize.builtin_tm_load (type);
if (!decl)
return NULL;
}
else
decl = builtin_decl_explicit (code);
t = gimplify_addr (gsi, rhs);
gcall = gimple_build_call (decl, 1, t);
gimple_set_location (gcall, loc);
t = TREE_TYPE (TREE_TYPE (decl));
if (useless_type_conversion_p (type, t))
{
gimple_call_set_lhs (gcall, lhs);
gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
}
else
{
gimple g;
tree temp;
temp = create_tmp_reg (t, NULL);
gimple_call_set_lhs (gcall, temp);
gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
t = fold_build1 (VIEW_CONVERT_EXPR, type, temp);
g = gimple_build_assign (lhs, t);
gsi_insert_before (gsi, g, GSI_SAME_STMT);
}
return gcall;
}
/* Similarly for storing TYPE in a transactional context. */
static gimple
build_tm_store (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi)
{
enum built_in_function code = END_BUILTINS;
tree t, fn, type = TREE_TYPE (rhs), simple_type;
gimple gcall;
if (type == float_type_node)
code = BUILT_IN_TM_STORE_FLOAT;
else if (type == double_type_node)
code = BUILT_IN_TM_STORE_DOUBLE;
else if (type == long_double_type_node)
code = BUILT_IN_TM_STORE_LDOUBLE;
else if (TYPE_SIZE_UNIT (type) != NULL
&& tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
{
switch (tree_to_uhwi (TYPE_SIZE_UNIT (type)))
{
case 1:
code = BUILT_IN_TM_STORE_1;
break;
case 2:
code = BUILT_IN_TM_STORE_2;
break;
case 4:
code = BUILT_IN_TM_STORE_4;
break;
case 8:
code = BUILT_IN_TM_STORE_8;
break;
}
}
if (code == END_BUILTINS)
{
fn = targetm.vectorize.builtin_tm_store (type);
if (!fn)
return NULL;
}
else
fn = builtin_decl_explicit (code);
simple_type = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))));
if (TREE_CODE (rhs) == CONSTRUCTOR)
{
/* Handle the easy initialization to zero. */
if (!CONSTRUCTOR_ELTS (rhs))
rhs = build_int_cst (simple_type, 0);
else
{
/* ...otherwise punt to the caller and probably use
BUILT_IN_TM_MEMMOVE, because we can't wrap a
VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce
valid gimple. */
return NULL;
}
}
else if (!useless_type_conversion_p (simple_type, type))
{
gimple g;
tree temp;
temp = create_tmp_reg (simple_type, NULL);
t = fold_build1 (VIEW_CONVERT_EXPR, simple_type, rhs);
g = gimple_build_assign (temp, t);
gimple_set_location (g, loc);
gsi_insert_before (gsi, g, GSI_SAME_STMT);
rhs = temp;
}
t = gimplify_addr (gsi, lhs);
gcall = gimple_build_call (fn, 2, t, rhs);
gimple_set_location (gcall, loc);
gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
return gcall;
}
/* Expand an assignment statement into transactional builtins. */
static void
expand_assign_tm (struct tm_region *region, gimple_stmt_iterator *gsi)
{
gimple stmt = gsi_stmt (*gsi);
location_t loc = gimple_location (stmt);
tree lhs = gimple_assign_lhs (stmt);
tree rhs = gimple_assign_rhs1 (stmt);
bool store_p = requires_barrier (region->entry_block, lhs, NULL);
bool load_p = requires_barrier (region->entry_block, rhs, NULL);
gimple gcall = NULL;
if (!load_p && !store_p)
{
/* Add thread private addresses to log if applicable. */
requires_barrier (region->entry_block, lhs, stmt);
gsi_next (gsi);
return;
}
// Remove original load/store statement.
gsi_remove (gsi, true);
if (load_p && !store_p)
{
transaction_subcode_ior (region, GTMA_HAVE_LOAD);
gcall = build_tm_load (loc, lhs, rhs, gsi);
}
else if (store_p && !load_p)
{
transaction_subcode_ior (region, GTMA_HAVE_STORE);
gcall = build_tm_store (loc, lhs, rhs, gsi);
}
if (!gcall)
{
tree lhs_addr, rhs_addr, tmp;
if (load_p)
transaction_subcode_ior (region, GTMA_HAVE_LOAD);
if (store_p)
transaction_subcode_ior (region, GTMA_HAVE_STORE);
/* ??? Figure out if there's any possible overlap between the LHS
and the RHS and if not, use MEMCPY. */
if (load_p && is_gimple_reg (lhs))
{
tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
lhs_addr = build_fold_addr_expr (tmp);
}
else
{
tmp = NULL_TREE;
lhs_addr = gimplify_addr (gsi, lhs);
}
rhs_addr = gimplify_addr (gsi, rhs);
gcall = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_MEMMOVE),
3, lhs_addr, rhs_addr,
TYPE_SIZE_UNIT (TREE_TYPE (lhs)));
gimple_set_location (gcall, loc);
gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
if (tmp)
{
gcall = gimple_build_assign (lhs, tmp);
gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
}
}
/* Now that we have the load/store in its instrumented form, add
thread private addresses to the log if applicable. */
if (!store_p)
requires_barrier (region->entry_block, lhs, gcall);
// The calls to build_tm_{store,load} above inserted the instrumented
// call into the stream.
// gsi_insert_before (gsi, gcall, GSI_SAME_STMT);
}
/* Expand a call statement as appropriate for a transaction. That is,
either verify that the call does not affect the transaction, or
redirect the call to a clone that handles transactions, or change
the transaction state to IRREVOCABLE. Return true if the call is
one of the builtins that end a transaction. */
static bool
expand_call_tm (struct tm_region *region,
gimple_stmt_iterator *gsi)
{
gimple stmt = gsi_stmt (*gsi);
tree lhs = gimple_call_lhs (stmt);
tree fn_decl;
struct cgraph_node *node;
bool retval = false;
fn_decl = gimple_call_fndecl (stmt);
if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMCPY)
|| fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMMOVE))
transaction_subcode_ior (region, GTMA_HAVE_STORE | GTMA_HAVE_LOAD);
if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMSET))
transaction_subcode_ior (region, GTMA_HAVE_STORE);
if (is_tm_pure_call (stmt))
return false;
if (fn_decl)
retval = is_tm_ending_fndecl (fn_decl);
if (!retval)
{
/* Assume all non-const/pure calls write to memory, except
transaction ending builtins. */
transaction_subcode_ior (region, GTMA_HAVE_STORE);
}
/* For indirect calls, we already generated a call into the runtime. */
if (!fn_decl)
{
tree fn = gimple_call_fn (stmt);
/* We are guaranteed never to go irrevocable on a safe or pure
call, and the pure call was handled above. */
if (is_tm_safe (fn))
return false;
else
transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
return false;
}
node = cgraph_node::get (fn_decl);
/* All calls should have cgraph here. */
if (!node)
{
/* We can have a nodeless call here if some pass after IPA-tm
added uninstrumented calls. For example, loop distribution
can transform certain loop constructs into __builtin_mem*
calls. In this case, see if we have a suitable TM
replacement and fill in the gaps. */
gcc_assert (DECL_BUILT_IN_CLASS (fn_decl) == BUILT_IN_NORMAL);
enum built_in_function code = DECL_FUNCTION_CODE (fn_decl);
gcc_assert (code == BUILT_IN_MEMCPY
|| code == BUILT_IN_MEMMOVE
|| code == BUILT_IN_MEMSET);
tree repl = find_tm_replacement_function (fn_decl);
if (repl)
{
gimple_call_set_fndecl (stmt, repl);
update_stmt (stmt);
node = cgraph_node::create (repl);
node->local.tm_may_enter_irr = false;
return expand_call_tm (region, gsi);
}
gcc_unreachable ();
}
if (node->local.tm_may_enter_irr)
transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
if (is_tm_abort (fn_decl))
{
transaction_subcode_ior (region, GTMA_HAVE_ABORT);
return true;
}
/* Instrument the store if needed.
If the assignment happens inside the function call (return slot
optimization), there is no instrumentation to be done, since
the callee should have done the right thing. */
if (lhs && requires_barrier (region->entry_block, lhs, stmt)
&& !gimple_call_return_slot_opt_p (stmt))
{
tree tmp = create_tmp_reg (TREE_TYPE (lhs), NULL);
location_t loc = gimple_location (stmt);
edge fallthru_edge = NULL;
/* Remember if the call was going to throw. */
if (stmt_can_throw_internal (stmt))
{
edge_iterator ei;
edge e;
basic_block bb = gimple_bb (stmt);
FOR_EACH_EDGE (e, ei, bb->succs)
if (e->flags & EDGE_FALLTHRU)
{
fallthru_edge = e;
break;
}
}
gimple_call_set_lhs (stmt, tmp);
update_stmt (stmt);
stmt = gimple_build_assign (lhs, tmp);
gimple_set_location (stmt, loc);
/* We cannot throw in the middle of a BB. If the call was going
to throw, place the instrumentation on the fallthru edge, so
the call remains the last statement in the block. */
if (fallthru_edge)
{
gimple_seq fallthru_seq = gimple_seq_alloc_with_stmt (stmt);
gimple_stmt_iterator fallthru_gsi = gsi_start (fallthru_seq);
expand_assign_tm (region, &fallthru_gsi);
gsi_insert_seq_on_edge (fallthru_edge, fallthru_seq);
pending_edge_inserts_p = true;
}
else
{
gsi_insert_after (gsi, stmt, GSI_CONTINUE_LINKING);
expand_assign_tm (region, gsi);
}
transaction_subcode_ior (region, GTMA_HAVE_STORE);
}
return retval;
}
/* Expand all statements in BB as appropriate for being inside
a transaction. */
static void
expand_block_tm (struct tm_region *region, basic_block bb)
{
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
{
gimple stmt = gsi_stmt (gsi);
switch (gimple_code (stmt))
{
case GIMPLE_ASSIGN:
/* Only memory reads/writes need to be instrumented. */
if (gimple_assign_single_p (stmt)
&& !gimple_clobber_p (stmt))
{
expand_assign_tm (region, &gsi);
continue;
}
break;
case GIMPLE_CALL:
if (expand_call_tm (region, &gsi))
return;
break;
case GIMPLE_ASM:
gcc_unreachable ();
default:
break;
}
if (!gsi_end_p (gsi))
gsi_next (&gsi);
}
}
/* Return the list of basic-blocks in REGION.
STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks
following a TM_IRREVOCABLE call.
INCLUDE_UNINSTRUMENTED_P is TRUE if we should include the
uninstrumented code path blocks in the list of basic blocks
returned, false otherwise. */
static vec<basic_block>
get_tm_region_blocks (basic_block entry_block,
bitmap exit_blocks,
bitmap irr_blocks,
bitmap all_region_blocks,
bool stop_at_irrevocable_p,
bool include_uninstrumented_p = true)
{
vec<basic_block> bbs = vNULL;
unsigned i;
edge e;
edge_iterator ei;
bitmap visited_blocks = BITMAP_ALLOC (NULL);
i = 0;
bbs.safe_push (entry_block);
bitmap_set_bit (visited_blocks, entry_block->index);
do
{
basic_block bb = bbs[i++];
if (exit_blocks &&
bitmap_bit_p (exit_blocks, bb->index))
continue;
if (stop_at_irrevocable_p
&& irr_blocks
&& bitmap_bit_p (irr_blocks, bb->index))
continue;
FOR_EACH_EDGE (e, ei, bb->succs)
if ((include_uninstrumented_p
|| !(e->flags & EDGE_TM_UNINSTRUMENTED))
&& !bitmap_bit_p (visited_blocks, e->dest->index))
{
bitmap_set_bit (visited_blocks, e->dest->index);
bbs.safe_push (e->dest);
}
}
while (i < bbs.length ());
if (all_region_blocks)
bitmap_ior_into (all_region_blocks, visited_blocks);
BITMAP_FREE (visited_blocks);
return bbs;
}
// Callback data for collect_bb2reg.
struct bb2reg_stuff
{
vec<tm_region_p> *bb2reg;
bool include_uninstrumented_p;
};
// Callback for expand_regions, collect innermost region data for each bb.
static void *
collect_bb2reg (struct tm_region *region, void *data)
{
struct bb2reg_stuff *stuff = (struct bb2reg_stuff *)data;
vec<tm_region_p> *bb2reg = stuff->bb2reg;
vec<basic_block> queue;
unsigned int i;
basic_block bb;
queue = get_tm_region_blocks (region->entry_block,
region->exit_blocks,
region->irr_blocks,
NULL,
/*stop_at_irr_p=*/true,
stuff->include_uninstrumented_p);
// We expect expand_region to perform a post-order traversal of the region
// tree. Therefore the last region seen for any bb is the innermost.
FOR_EACH_VEC_ELT (queue, i, bb)
(*bb2reg)[bb->index] = region;
queue.release ();
return NULL;
}
// Returns a vector, indexed by BB->INDEX, of the innermost tm_region to
// which a basic block belongs. Note that we only consider the instrumented
// code paths for the region; the uninstrumented code paths are ignored if
// INCLUDE_UNINSTRUMENTED_P is false.
//
// ??? This data is very similar to the bb_regions array that is collected
// during tm_region_init. Or, rather, this data is similar to what could
// be used within tm_region_init. The actual computation in tm_region_init
// begins and ends with bb_regions entirely full of NULL pointers, due to
// the way in which pointers are swapped in and out of the array.
//
// ??? Our callers expect that blocks are not shared between transactions.
// When the optimizers get too smart, and blocks are shared, then during
// the tm_mark phase we'll add log entries to only one of the two transactions,
// and in the tm_edge phase we'll add edges to the CFG that create invalid
// cycles. The symptom being SSA defs that do not dominate their uses.
// Note that the optimizers were locally correct with their transformation,
// as we have no info within the program that suggests that the blocks cannot
// be shared.
//
// ??? There is currently a hack inside tree-ssa-pre.c to work around the
// only known instance of this block sharing.
static vec<tm_region_p>
get_bb_regions_instrumented (bool traverse_clones,
bool include_uninstrumented_p)
{
unsigned n = last_basic_block_for_fn (cfun);
struct bb2reg_stuff stuff;
vec<tm_region_p> ret;
ret.create (n);
ret.safe_grow_cleared (n);
stuff.bb2reg = &ret;
stuff.include_uninstrumented_p = include_uninstrumented_p;
expand_regions (all_tm_regions, collect_bb2reg, &stuff, traverse_clones);
return ret;
}
/* Set the IN_TRANSACTION for all gimple statements that appear in a
transaction. */
void
compute_transaction_bits (void)
{
struct tm_region *region;
vec<basic_block> queue;
unsigned int i;
basic_block bb;
/* ?? Perhaps we need to abstract gate_tm_init further, because we
certainly don't need it to calculate CDI_DOMINATOR info. */
gate_tm_init ();
FOR_EACH_BB_FN (bb, cfun)
bb->flags &= ~BB_IN_TRANSACTION;
for (region = all_tm_regions; region; region = region->next)
{
queue = get_tm_region_blocks (region->entry_block,
region->exit_blocks,
region->irr_blocks,
NULL,
/*stop_at_irr_p=*/true);
for (i = 0; queue.iterate (i, &bb); ++i)
bb->flags |= BB_IN_TRANSACTION;
queue.release ();
}
if (all_tm_regions)
bitmap_obstack_release (&tm_obstack);
}
/* Replace the GIMPLE_TRANSACTION in this region with the corresponding
call to BUILT_IN_TM_START. */
static void *
expand_transaction (struct tm_region *region, void *data ATTRIBUTE_UNUSED)
{
tree tm_start = builtin_decl_explicit (BUILT_IN_TM_START);
basic_block transaction_bb = gimple_bb (region->transaction_stmt);
tree tm_state = region->tm_state;
tree tm_state_type = TREE_TYPE (tm_state);
edge abort_edge = NULL;
edge inst_edge = NULL;
edge uninst_edge = NULL;
edge fallthru_edge = NULL;
// Identify the various successors of the transaction start.
{
edge_iterator i;
edge e;
FOR_EACH_EDGE (e, i, transaction_bb->succs)
{
if (e->flags & EDGE_TM_ABORT)
abort_edge = e;
else if (e->flags & EDGE_TM_UNINSTRUMENTED)
uninst_edge = e;
else
inst_edge = e;
if (e->flags & EDGE_FALLTHRU)
fallthru_edge = e;
}
}
/* ??? There are plenty of bits here we're not computing. */
{
int subcode = gimple_transaction_subcode (region->transaction_stmt);
int flags = 0;
if (subcode & GTMA_DOES_GO_IRREVOCABLE)
flags |= PR_DOESGOIRREVOCABLE;
if ((subcode & GTMA_MAY_ENTER_IRREVOCABLE) == 0)
flags |= PR_HASNOIRREVOCABLE;
/* If the transaction does not have an abort in lexical scope and is not
marked as an outer transaction, then it will never abort. */
if ((subcode & GTMA_HAVE_ABORT) == 0 && (subcode & GTMA_IS_OUTER) == 0)
flags |= PR_HASNOABORT;
if ((subcode & GTMA_HAVE_STORE) == 0)
flags |= PR_READONLY;
if (inst_edge && !(subcode & GTMA_HAS_NO_INSTRUMENTATION))
flags |= PR_INSTRUMENTEDCODE;
if (uninst_edge)
flags |= PR_UNINSTRUMENTEDCODE;
if (subcode & GTMA_IS_OUTER)
region->original_transaction_was_outer = true;
tree t = build_int_cst (tm_state_type, flags);
gimple call = gimple_build_call (tm_start, 1, t);
gimple_call_set_lhs (call, tm_state);
gimple_set_location (call, gimple_location (region->transaction_stmt));
// Replace the GIMPLE_TRANSACTION with the call to BUILT_IN_TM_START.
gimple_stmt_iterator gsi = gsi_last_bb (transaction_bb);
gcc_assert (gsi_stmt (gsi) == region->transaction_stmt);
gsi_insert_before (&gsi, call, GSI_SAME_STMT);
gsi_remove (&gsi, true);
region->transaction_stmt = call;
}
// Generate log saves.
if (!tm_log_save_addresses.is_empty ())
tm_log_emit_saves (region->entry_block, transaction_bb);
// In the beginning, we've no tests to perform on transaction restart.
// Note that after this point, transaction_bb becomes the "most recent
// block containing tests for the transaction".
region->restart_block = region->entry_block;
// Generate log restores.
if (!tm_log_save_addresses.is_empty ())
{
basic_block test_bb = create_empty_bb (transaction_bb);
basic_block code_bb = create_empty_bb (test_bb);
basic_block join_bb = create_empty_bb (code_bb);
add_bb_to_loop (test_bb, transaction_bb->loop_father);
add_bb_to_loop (code_bb, transaction_bb->loop_father);
add_bb_to_loop (join_bb, transaction_bb->loop_father);
if (region->restart_block == region->entry_block)
region->restart_block = test_bb;
tree t1 = create_tmp_reg (tm_state_type, NULL);
tree t2 = build_int_cst (tm_state_type, A_RESTORELIVEVARIABLES);
gimple stmt = gimple_build_assign_with_ops (BIT_AND_EXPR, t1,
tm_state, t2);
gimple_stmt_iterator gsi = gsi_last_bb (test_bb);
gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
t2 = build_int_cst (tm_state_type, 0);
stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
tm_log_emit_restores (region->entry_block, code_bb);
edge ei = make_edge (transaction_bb, test_bb, EDGE_FALLTHRU);
edge et = make_edge (test_bb, code_bb, EDGE_TRUE_VALUE);
edge ef = make_edge (test_bb, join_bb, EDGE_FALSE_VALUE);
redirect_edge_pred (fallthru_edge, join_bb);
join_bb->frequency = test_bb->frequency = transaction_bb->frequency;
join_bb->count = test_bb->count = transaction_bb->count;
ei->probability = PROB_ALWAYS;
et->probability = PROB_LIKELY;
ef->probability = PROB_UNLIKELY;
et->count = apply_probability (test_bb->count, et->probability);
ef->count = apply_probability (test_bb->count, ef->probability);
code_bb->count = et->count;
code_bb->frequency = EDGE_FREQUENCY (et);
transaction_bb = join_bb;
}
// If we have an ABORT edge, create a test to perform the abort.
if (abort_edge)
{
basic_block test_bb = create_empty_bb (transaction_bb);
add_bb_to_loop (test_bb, transaction_bb->loop_father);
if (region->restart_block == region->entry_block)
region->restart_block = test_bb;
tree t1 = create_tmp_reg (tm_state_type, NULL);
tree t2 = build_int_cst (tm_state_type, A_ABORTTRANSACTION);
gimple stmt = gimple_build_assign_with_ops (BIT_AND_EXPR, t1,
tm_state, t2);
gimple_stmt_iterator gsi = gsi_last_bb (test_bb);
gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
t2 = build_int_cst (tm_state_type, 0);
stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
edge ei = make_edge (transaction_bb, test_bb, EDGE_FALLTHRU);
test_bb->frequency = transaction_bb->frequency;
test_bb->count = transaction_bb->count;
ei->probability = PROB_ALWAYS;
// Not abort edge. If both are live, chose one at random as we'll
// we'll be fixing that up below.
redirect_edge_pred (fallthru_edge, test_bb);
fallthru_edge->flags = EDGE_FALSE_VALUE;
fallthru_edge->probability = PROB_VERY_LIKELY;
fallthru_edge->count
= apply_probability (test_bb->count, fallthru_edge->probability);
// Abort/over edge.
redirect_edge_pred (abort_edge, test_bb);
abort_edge->flags = EDGE_TRUE_VALUE;
abort_edge->probability = PROB_VERY_UNLIKELY;
abort_edge->count
= apply_probability (test_bb->count, abort_edge->probability);
transaction_bb = test_bb;
}
// If we have both instrumented and uninstrumented code paths, select one.
if (inst_edge && uninst_edge)
{
basic_block test_bb = create_empty_bb (transaction_bb);
add_bb_to_loop (test_bb, transaction_bb->loop_father);
if (region->restart_block == region->entry_block)
region->restart_block = test_bb;
tree t1 = create_tmp_reg (tm_state_type, NULL);
tree t2 = build_int_cst (tm_state_type, A_RUNUNINSTRUMENTEDCODE);
gimple stmt = gimple_build_assign_with_ops (BIT_AND_EXPR, t1,
tm_state, t2);
gimple_stmt_iterator gsi = gsi_last_bb (test_bb);
gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
t2 = build_int_cst (tm_state_type, 0);
stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL);
gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
// Create the edge into test_bb first, as we want to copy values
// out of the fallthru edge.
edge e = make_edge (transaction_bb, test_bb, fallthru_edge->flags);
e->probability = fallthru_edge->probability;
test_bb->count = e->count = fallthru_edge->count;
test_bb->frequency = EDGE_FREQUENCY (e);
// Now update the edges to the inst/uninist implementations.
// For now assume that the paths are equally likely. When using HTM,
// we'll try the uninst path first and fallback to inst path if htm
// buffers are exceeded. Without HTM we start with the inst path and
// use the uninst path when falling back to serial mode.
redirect_edge_pred (inst_edge, test_bb);
inst_edge->flags = EDGE_FALSE_VALUE;
inst_edge->probability = REG_BR_PROB_BASE / 2;
inst_edge->count
= apply_probability (test_bb->count, inst_edge->probability);
redirect_edge_pred (uninst_edge, test_bb);
uninst_edge->flags = EDGE_TRUE_VALUE;
uninst_edge->probability = REG_BR_PROB_BASE / 2;
uninst_edge->count
= apply_probability (test_bb->count, uninst_edge->probability);
}
// If we have no previous special cases, and we have PHIs at the beginning
// of the atomic region, this means we have a loop at the beginning of the
// atomic region that shares the first block. This can cause problems with
// the transaction restart abnormal edges to be added in the tm_edges pass.
// Solve this by adding a new empty block to receive the abnormal edges.
if (region->restart_block == region->entry_block
&& phi_nodes (region->entry_block))
{
basic_block empty_bb = create_empty_bb (transaction_bb);
region->restart_block = empty_bb;
add_bb_to_loop (empty_bb, transaction_bb->loop_father);
redirect_edge_pred (fallthru_edge, empty_bb);
make_edge (transaction_bb, empty_bb, EDGE_FALLTHRU);
}
return NULL;
}
/* Generate the temporary to be used for the return value of
BUILT_IN_TM_START. */
static void *
generate_tm_state (struct tm_region *region, void *data ATTRIBUTE_UNUSED)
{
tree tm_start = builtin_decl_explicit (BUILT_IN_TM_START);
region->tm_state =
create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start)), "tm_state");
// Reset the subcode, post optimizations. We'll fill this in
// again as we process blocks.
if (region->exit_blocks)
{
unsigned int subcode
= gimple_transaction_subcode (region->transaction_stmt);
if (subcode & GTMA_DOES_GO_IRREVOCABLE)
subcode &= (GTMA_DECLARATION_MASK | GTMA_DOES_GO_IRREVOCABLE
| GTMA_MAY_ENTER_IRREVOCABLE
| GTMA_HAS_NO_INSTRUMENTATION);
else
subcode &= GTMA_DECLARATION_MASK;
gimple_transaction_set_subcode (region->transaction_stmt, subcode);
}
return NULL;
}
// Propagate flags from inner transactions outwards.
static void
propagate_tm_flags_out (struct tm_region *region)
{
if (region == NULL)
return;
propagate_tm_flags_out (region->inner);
if (region->outer && region->outer->transaction_stmt)
{
unsigned s = gimple_transaction_subcode (region->transaction_stmt);
s &= (GTMA_HAVE_ABORT | GTMA_HAVE_LOAD | GTMA_HAVE_STORE
| GTMA_MAY_ENTER_IRREVOCABLE);
s |= gimple_transaction_subcode (region->outer->transaction_stmt);
gimple_transaction_set_subcode (region->outer->transaction_stmt, s);
}
propagate_tm_flags_out (region->next);
}
/* Entry point to the MARK phase of TM expansion. Here we replace
transactional memory statements with calls to builtins, and function
calls with their transactional clones (if available). But we don't
yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */
static unsigned int
execute_tm_mark (void)
{
pending_edge_inserts_p = false;
expand_regions (all_tm_regions, generate_tm_state, NULL,
/*traverse_clones=*/true);
tm_log_init ();
vec<tm_region_p> bb_regions
= get_bb_regions_instrumented (/*traverse_clones=*/true,
/*include_uninstrumented_p=*/false);
struct tm_region *r;
unsigned i;
// Expand memory operations into calls into the runtime.
// This collects log entries as well.
FOR_EACH_VEC_ELT (bb_regions, i, r)
{
if (r != NULL)
{
if (r->transaction_stmt)
{
unsigned sub = gimple_transaction_subcode (r->transaction_stmt);
/* If we're sure to go irrevocable, there won't be
anything to expand, since the run-time will go
irrevocable right away. */
if (sub & GTMA_DOES_GO_IRREVOCABLE
&& sub & GTMA_MAY_ENTER_IRREVOCABLE)
continue;
}
expand_block_tm (r, BASIC_BLOCK_FOR_FN (cfun, i));
}
}
bb_regions.release ();
// Propagate flags from inner transactions outwards.
propagate_tm_flags_out (all_tm_regions);
// Expand GIMPLE_TRANSACTIONs into calls into the runtime.
expand_regions (all_tm_regions, expand_transaction, NULL,
/*traverse_clones=*/false);
tm_log_emit ();
tm_log_delete ();
if (pending_edge_inserts_p)
gsi_commit_edge_inserts ();
free_dominance_info (CDI_DOMINATORS);
return 0;
}
namespace {
const pass_data pass_data_tm_mark =
{
GIMPLE_PASS, /* type */
"tmmark", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_TRANS_MEM, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_update_ssa, /* todo_flags_finish */
};
class pass_tm_mark : public gimple_opt_pass
{
public:
pass_tm_mark (gcc::context *ctxt)
: gimple_opt_pass (pass_data_tm_mark, ctxt)
{}
/* opt_pass methods: */
virtual unsigned int execute (function *) { return execute_tm_mark (); }
}; // class pass_tm_mark
} // anon namespace
gimple_opt_pass *
make_pass_tm_mark (gcc::context *ctxt)
{
return new pass_tm_mark (ctxt);
}
/* Create an abnormal edge from STMT at iter, splitting the block
as necessary. Adjust *PNEXT as needed for the split block. */
static inline void
split_bb_make_tm_edge (gimple stmt, basic_block dest_bb,
gimple_stmt_iterator iter, gimple_stmt_iterator *pnext)
{
basic_block bb = gimple_bb (stmt);
if (!gsi_one_before_end_p (iter))
{
edge e = split_block (bb, stmt);
*pnext = gsi_start_bb (e->dest);
}
make_edge (bb, dest_bb, EDGE_ABNORMAL);
// Record the need for the edge for the benefit of the rtl passes.
if (cfun->gimple_df->tm_restart == NULL)
cfun->gimple_df->tm_restart = htab_create_ggc (31, struct_ptr_hash,
struct_ptr_eq, ggc_free);
struct tm_restart_node dummy;
dummy.stmt = stmt;
dummy.label_or_list = gimple_block_label (dest_bb);
void **slot = htab_find_slot (cfun->gimple_df->tm_restart, &dummy, INSERT);
struct tm_restart_node *n = (struct tm_restart_node *) *slot;
if (n == NULL)
{
n = ggc_alloc<tm_restart_node> ();
*n = dummy;
}
else
{
tree old = n->label_or_list;
if (TREE_CODE (old) == LABEL_DECL)
old = tree_cons (NULL, old, NULL);
n->label_or_list = tree_cons (NULL, dummy.label_or_list, old);
}
}
/* Split block BB as necessary for every builtin function we added, and
wire up the abnormal back edges implied by the transaction restart. */
static void
expand_block_edges (struct tm_region *const region, basic_block bb)
{
gimple_stmt_iterator gsi, next_gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi = next_gsi)
{
gimple stmt = gsi_stmt (gsi);
next_gsi = gsi;
gsi_next (&next_gsi);
// ??? Shouldn't we split for any non-pure, non-irrevocable function?
if (gimple_code (stmt) != GIMPLE_CALL
|| (gimple_call_flags (stmt) & ECF_TM_BUILTIN) == 0)
continue;
if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_TM_ABORT)
{
// If we have a ``_transaction_cancel [[outer]]'', there is only
// one abnormal edge: to the transaction marked OUTER.
// All compiler-generated instances of BUILT_IN_TM_ABORT have a
// constant argument, which we can examine here. Users invoking
// TM_ABORT directly get what they deserve.
tree arg = gimple_call_arg (stmt, 0);
if (TREE_CODE (arg) == INTEGER_CST
&& (TREE_INT_CST_LOW (arg) & AR_OUTERABORT) != 0
&& !decl_is_tm_clone (current_function_decl))
{
// Find the GTMA_IS_OUTER transaction.
for (struct tm_region *o = region; o; o = o->outer)
if (o->original_transaction_was_outer)
{
split_bb_make_tm_edge (stmt, o->restart_block,
gsi, &next_gsi);
break;
}
// Otherwise, the front-end should have semantically checked
// outer aborts, but in either case the target region is not
// within this function.
continue;
}
// Non-outer, TM aborts have an abnormal edge to the inner-most
// transaction, the one being aborted;
split_bb_make_tm_edge (stmt, region->restart_block, gsi, &next_gsi);
}
// All TM builtins have an abnormal edge to the outer-most transaction.
// We never restart inner transactions. For tm clones, we know a-priori
// that the outer-most transaction is outside the function.
if (decl_is_tm_clone (current_function_decl))
continue;
if (cfun->gimple_df->tm_restart == NULL)
cfun->gimple_df->tm_restart
= htab_create_ggc (31, struct_ptr_hash, struct_ptr_eq, ggc_free);
// All TM builtins have an abnormal edge to the outer-most transaction.
// We never restart inner transactions.
for (struct tm_region *o = region; o; o = o->outer)
if (!o->outer)
{
split_bb_make_tm_edge (stmt, o->restart_block, gsi, &next_gsi);
break;
}
// Delete any tail-call annotation that may have been added.
// The tail-call pass may have mis-identified the commit as being
// a candidate because we had not yet added this restart edge.
gimple_call_set_tail (stmt, false);
}
}
/* Entry point to the final expansion of transactional nodes. */
namespace {
const pass_data pass_data_tm_edges =
{
GIMPLE_PASS, /* type */
"tmedge", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_TRANS_MEM, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_update_ssa, /* todo_flags_finish */
};
class pass_tm_edges : public gimple_opt_pass
{
public:
pass_tm_edges (gcc::context *ctxt)
: gimple_opt_pass (pass_data_tm_edges, ctxt)
{}
/* opt_pass methods: */
virtual unsigned int execute (function *);
}; // class pass_tm_edges
unsigned int
pass_tm_edges::execute (function *fun)
{
vec<tm_region_p> bb_regions
= get_bb_regions_instrumented (/*traverse_clones=*/false,
/*include_uninstrumented_p=*/true);
struct tm_region *r;
unsigned i;
FOR_EACH_VEC_ELT (bb_regions, i, r)
if (r != NULL)
expand_block_edges (r, BASIC_BLOCK_FOR_FN (fun, i));
bb_regions.release ();
/* We've got to release the dominance info now, to indicate that it
must be rebuilt completely. Otherwise we'll crash trying to update
the SSA web in the TODO section following this pass. */
free_dominance_info (CDI_DOMINATORS);
bitmap_obstack_release (&tm_obstack);
all_tm_regions = NULL;
return 0;
}
} // anon namespace
gimple_opt_pass *
make_pass_tm_edges (gcc::context *ctxt)
{
return new pass_tm_edges (ctxt);
}
/* Helper function for expand_regions. Expand REGION and recurse to
the inner region. Call CALLBACK on each region. CALLBACK returns
NULL to continue the traversal, otherwise a non-null value which
this function will return as well. TRAVERSE_CLONES is true if we
should traverse transactional clones. */
static void *
expand_regions_1 (struct tm_region *region,
void *(*callback)(struct tm_region *, void *),
void *data,
bool traverse_clones)
{
void *retval = NULL;
if (region->exit_blocks
|| (traverse_clones && decl_is_tm_clone (current_function_decl)))
{
retval = callback (region, data);
if (retval)
return retval;
}
if (region->inner)
{
retval = expand_regions (region->inner, callback, data, traverse_clones);
if (retval)
return retval;
}
return retval;
}
/* Traverse the regions enclosed and including REGION. Execute
CALLBACK for each region, passing DATA. CALLBACK returns NULL to
continue the traversal, otherwise a non-null value which this
function will return as well. TRAVERSE_CLONES is true if we should
traverse transactional clones. */
static void *
expand_regions (struct tm_region *region,
void *(*callback)(struct tm_region *, void *),
void *data,
bool traverse_clones)
{
void *retval = NULL;
while (region)
{
retval = expand_regions_1 (region, callback, data, traverse_clones);
if (retval)
return retval;
region = region->next;
}
return retval;
}
/* A unique TM memory operation. */
typedef struct tm_memop
{
/* Unique ID that all memory operations to the same location have. */
unsigned int value_id;
/* Address of load/store. */
tree addr;
} *tm_memop_t;
/* TM memory operation hashtable helpers. */
struct tm_memop_hasher : typed_free_remove <tm_memop>
{
typedef tm_memop value_type;
typedef tm_memop compare_type;
static inline hashval_t hash (const value_type *);
static inline bool equal (const value_type *, const compare_type *);
};
/* Htab support. Return a hash value for a `tm_memop'. */
inline hashval_t
tm_memop_hasher::hash (const value_type *mem)
{
tree addr = mem->addr;
/* We drill down to the SSA_NAME/DECL for the hash, but equality is
actually done with operand_equal_p (see tm_memop_eq). */
if (TREE_CODE (addr) == ADDR_EXPR)
addr = TREE_OPERAND (addr, 0);
return iterative_hash_expr (addr, 0);
}
/* Htab support. Return true if two tm_memop's are the same. */
inline bool
tm_memop_hasher::equal (const value_type *mem1, const compare_type *mem2)
{
return operand_equal_p (mem1->addr, mem2->addr, 0);
}
/* Sets for solving data flow equations in the memory optimization pass. */
struct tm_memopt_bitmaps
{
/* Stores available to this BB upon entry. Basically, stores that
dominate this BB. */
bitmap store_avail_in;
/* Stores available at the end of this BB. */
bitmap store_avail_out;
bitmap store_antic_in;
bitmap store_antic_out;
/* Reads available to this BB upon entry. Basically, reads that
dominate this BB. */
bitmap read_avail_in;
/* Reads available at the end of this BB. */
bitmap read_avail_out;
/* Reads performed in this BB. */
bitmap read_local;
/* Writes performed in this BB. */
bitmap store_local;
/* Temporary storage for pass. */
/* Is the current BB in the worklist? */
bool avail_in_worklist_p;
/* Have we visited this BB? */
bool visited_p;
};
static bitmap_obstack tm_memopt_obstack;
/* Unique counter for TM loads and stores. Loads and stores of the
same address get the same ID. */
static unsigned int tm_memopt_value_id;
static hash_table<tm_memop_hasher> *tm_memopt_value_numbers;
#define STORE_AVAIL_IN(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in
#define STORE_AVAIL_OUT(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out
#define STORE_ANTIC_IN(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in
#define STORE_ANTIC_OUT(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out
#define READ_AVAIL_IN(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in
#define READ_AVAIL_OUT(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out
#define READ_LOCAL(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local
#define STORE_LOCAL(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local
#define AVAIL_IN_WORKLIST_P(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p
#define BB_VISITED_P(BB) \
((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p
/* Given a TM load/store in STMT, return the value number for the address
it accesses. */
static unsigned int
tm_memopt_value_number (gimple stmt, enum insert_option op)
{
struct tm_memop tmpmem, *mem;
tm_memop **slot;
gcc_assert (is_tm_load (stmt) || is_tm_store (stmt));
tmpmem.addr = gimple_call_arg (stmt, 0);
slot = tm_memopt_value_numbers->find_slot (&tmpmem, op);
if (*slot)
mem = *slot;
else if (op == INSERT)
{
mem = XNEW (struct tm_memop);
*slot = mem;
mem->value_id = tm_memopt_value_id++;
mem->addr = tmpmem.addr;
}
else
gcc_unreachable ();
return mem->value_id;
}
/* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */
static void
tm_memopt_accumulate_memops (basic_block bb)
{
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
bitmap bits;
unsigned int loc;
if (is_tm_store (stmt))
bits = STORE_LOCAL (bb);
else if (is_tm_load (stmt))
bits = READ_LOCAL (bb);
else
continue;
loc = tm_memopt_value_number (stmt, INSERT);
bitmap_set_bit (bits, loc);
if (dump_file)
{
fprintf (dump_file, "TM memopt (%s): value num=%d, BB=%d, addr=",
is_tm_load (stmt) ? "LOAD" : "STORE", loc,
gimple_bb (stmt)->index);
print_generic_expr (dump_file, gimple_call_arg (stmt, 0), 0);
fprintf (dump_file, "\n");
}
}
}
/* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */
static void
dump_tm_memopt_set (const char *set_name, bitmap bits)
{
unsigned i;
bitmap_iterator bi;
const char *comma = "";
fprintf (dump_file, "TM memopt: %s: [", set_name);
EXECUTE_IF_SET_IN_BITMAP (bits, 0, i, bi)
{
hash_table<tm_memop_hasher>::iterator hi;
struct tm_memop *mem = NULL;
/* Yeah, yeah, yeah. Whatever. This is just for debugging. */
FOR_EACH_HASH_TABLE_ELEMENT (*tm_memopt_value_numbers, mem, tm_memop_t, hi)
if (mem->value_id == i)
break;
gcc_assert (mem->value_id == i);
fprintf (dump_file, "%s", comma);
comma = ", ";
print_generic_expr (dump_file, mem->addr, 0);
}
fprintf (dump_file, "]\n");
}
/* Prettily dump all of the memopt sets in BLOCKS. */
static void
dump_tm_memopt_sets (vec<basic_block> blocks)
{
size_t i;
basic_block bb;
for (i = 0; blocks.iterate (i, &bb); ++i)
{
fprintf (dump_file, "------------BB %d---------\n", bb->index);
dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb));
dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb));
dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb));
dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb));
dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb));
dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb));
}
}
/* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */
static void
tm_memopt_compute_avin (basic_block bb)
{
edge e;
unsigned ix;
/* Seed with the AVOUT of any predecessor. */
for (ix = 0; ix < EDGE_COUNT (bb->preds); ix++)
{
e = EDGE_PRED (bb, ix);
/* Make sure we have already visited this BB, and is thus
initialized.
If e->src->aux is NULL, this predecessor is actually on an
enclosing transaction. We only care about the current
transaction, so ignore it. */
if (e->src->aux && BB_VISITED_P (e->src))
{
bitmap_copy (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src));
bitmap_copy (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src));
break;
}
}
for (; ix < EDGE_COUNT (bb->preds); ix++)
{
e = EDGE_PRED (bb, ix);
if (e->src->aux && BB_VISITED_P (e->src))
{
bitmap_and_into (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src));
bitmap_and_into (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src));
}
}
BB_VISITED_P (bb) = true;
}
/* Compute the STORE_ANTIC_IN for the basic block BB. */
static void
tm_memopt_compute_antin (basic_block bb)
{
edge e;
unsigned ix;
/* Seed with the ANTIC_OUT of any successor. */
for (ix = 0; ix < EDGE_COUNT (bb->succs); ix++)
{
e = EDGE_SUCC (bb, ix);
/* Make sure we have already visited this BB, and is thus
initialized. */
if (BB_VISITED_P (e->dest))
{
bitmap_copy (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest));
break;
}
}
for (; ix < EDGE_COUNT (bb->succs); ix++)
{
e = EDGE_SUCC (bb, ix);
if (BB_VISITED_P (e->dest))
bitmap_and_into (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest));
}
BB_VISITED_P (bb) = true;
}
/* Compute the AVAIL sets for every basic block in BLOCKS.
We compute {STORE,READ}_AVAIL_{OUT,IN} as follows:
AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb])
AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors])
This is basically what we do in lcm's compute_available(), but here
we calculate two sets of sets (one for STOREs and one for READs),
and we work on a region instead of the entire CFG.
REGION is the TM region.
BLOCKS are the basic blocks in the region. */
static void
tm_memopt_compute_available (struct tm_region *region,
vec<basic_block> blocks)
{
edge e;
basic_block *worklist, *qin, *qout, *qend, bb;
unsigned int qlen, i;
edge_iterator ei;
bool changed;
/* Allocate a worklist array/queue. Entries are only added to the
list if they were not already on the list. So the size is
bounded by the number of basic blocks in the region. */
qlen = blocks.length () - 1;
qin = qout = worklist =
XNEWVEC (basic_block, qlen);
/* Put every block in the region on the worklist. */
for (i = 0; blocks.iterate (i, &bb); ++i)
{
/* Seed AVAIL_OUT with the LOCAL set. */
bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_LOCAL (bb));
bitmap_ior_into (READ_AVAIL_OUT (bb), READ_LOCAL (bb));
AVAIL_IN_WORKLIST_P (bb) = true;
/* No need to insert the entry block, since it has an AVIN of
null, and an AVOUT that has already been seeded in. */
if (bb != region->entry_block)
*qin++ = bb;
}
/* The entry block has been initialized with the local sets. */
BB_VISITED_P (region->entry_block) = true;
qin = worklist;
qend = &worklist[qlen];
/* Iterate until the worklist is empty. */
while (qlen)
{
/* Take the first entry off the worklist. */
bb = *qout++;
qlen--;
if (qout >= qend)
qout = worklist;
/* This block can be added to the worklist again if necessary. */
AVAIL_IN_WORKLIST_P (bb) = false;
tm_memopt_compute_avin (bb);
/* Note: We do not add the LOCAL sets here because we already
seeded the AVAIL_OUT sets with them. */
changed = bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_AVAIL_IN (bb));
changed |= bitmap_ior_into (READ_AVAIL_OUT (bb), READ_AVAIL_IN (bb));
if (changed
&& (region->exit_blocks == NULL
|| !bitmap_bit_p (region->exit_blocks, bb->index)))
/* If the out state of this block changed, then we need to add
its successors to the worklist if they are not already in. */
FOR_EACH_EDGE (e, ei, bb->succs)
if (!AVAIL_IN_WORKLIST_P (e->dest)
&& e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
{
*qin++ = e->dest;
AVAIL_IN_WORKLIST_P (e->dest) = true;
qlen++;
if (qin >= qend)
qin = worklist;
}
}
free (worklist);
if (dump_file)
dump_tm_memopt_sets (blocks);
}
/* Compute ANTIC sets for every basic block in BLOCKS.
We compute STORE_ANTIC_OUT as follows:
STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb])
STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors])
REGION is the TM region.
BLOCKS are the basic blocks in the region. */
static void
tm_memopt_compute_antic (struct tm_region *region,
vec<basic_block> blocks)
{
edge e;
basic_block *worklist, *qin, *qout, *qend, bb;
unsigned int qlen;
int i;
edge_iterator ei;
/* Allocate a worklist array/queue. Entries are only added to the
list if they were not already on the list. So the size is
bounded by the number of basic blocks in the region. */
qin = qout = worklist = XNEWVEC (basic_block, blocks.length ());
for (qlen = 0, i = blocks.length () - 1; i >= 0; --i)
{
bb = blocks[i];
/* Seed ANTIC_OUT with the LOCAL set. */
bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_LOCAL (bb));
/* Put every block in the region on the worklist. */
AVAIL_IN_WORKLIST_P (bb) = true;
/* No need to insert exit blocks, since their ANTIC_IN is NULL,
and their ANTIC_OUT has already been seeded in. */
if (region->exit_blocks
&& !bitmap_bit_p (region->exit_blocks, bb->index))
{
qlen++;
*qin++ = bb;
}
}
/* The exit blocks have been initialized with the local sets. */
if (region->exit_blocks)
{
unsigned int i;
bitmap_iterator bi;
EXECUTE_IF_SET_IN_BITMAP (region->exit_blocks, 0, i, bi)
BB_VISITED_P (BASIC_BLOCK_FOR_FN (cfun, i)) = true;
}
qin = worklist;
qend = &worklist[qlen];
/* Iterate until the worklist is empty. */
while (qlen)
{
/* Take the first entry off the worklist. */
bb = *qout++;
qlen--;
if (qout >= qend)
qout = worklist;
/* This block can be added to the worklist again if necessary. */
AVAIL_IN_WORKLIST_P (bb) = false;
tm_memopt_compute_antin (bb);
/* Note: We do not add the LOCAL sets here because we already
seeded the ANTIC_OUT sets with them. */
if (bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_ANTIC_IN (bb))
&& bb != region->entry_block)
/* If the out state of this block changed, then we need to add
its predecessors to the worklist if they are not already in. */
FOR_EACH_EDGE (e, ei, bb->preds)
if (!AVAIL_IN_WORKLIST_P (e->src))
{
*qin++ = e->src;
AVAIL_IN_WORKLIST_P (e->src) = true;
qlen++;
if (qin >= qend)
qin = worklist;
}
}
free (worklist);
if (dump_file)
dump_tm_memopt_sets (blocks);
}
/* Offsets of load variants from TM_LOAD. For example,
BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*.
See gtm-builtins.def. */
#define TRANSFORM_RAR 1
#define TRANSFORM_RAW 2
#define TRANSFORM_RFW 3
/* Offsets of store variants from TM_STORE. */
#define TRANSFORM_WAR 1
#define TRANSFORM_WAW 2
/* Inform about a load/store optimization. */
static void
dump_tm_memopt_transform (gimple stmt)
{
if (dump_file)
{
fprintf (dump_file, "TM memopt: transforming: ");
print_gimple_stmt (dump_file, stmt, 0, 0);
fprintf (dump_file, "\n");
}
}
/* Perform a read/write optimization. Replaces the TM builtin in STMT
by a builtin that is OFFSET entries down in the builtins table in
gtm-builtins.def. */
static void
tm_memopt_transform_stmt (unsigned int offset,
gimple stmt,
gimple_stmt_iterator *gsi)
{
tree fn = gimple_call_fn (stmt);
gcc_assert (TREE_CODE (fn) == ADDR_EXPR);
TREE_OPERAND (fn, 0)
= builtin_decl_explicit ((enum built_in_function)
(DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0))
+ offset));
gimple_call_set_fn (stmt, fn);
gsi_replace (gsi, stmt, true);
dump_tm_memopt_transform (stmt);
}
/* Perform the actual TM memory optimization transformations in the
basic blocks in BLOCKS. */
static void
tm_memopt_transform_blocks (vec<basic_block> blocks)
{
size_t i;
basic_block bb;
gimple_stmt_iterator gsi;
for (i = 0; blocks.iterate (i, &bb); ++i)
{
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
bitmap read_avail = READ_AVAIL_IN (bb);
bitmap store_avail = STORE_AVAIL_IN (bb);
bitmap store_antic = STORE_ANTIC_OUT (bb);
unsigned int loc;
if (is_tm_simple_load (stmt))
{
loc = tm_memopt_value_number (stmt, NO_INSERT);
if (store_avail && bitmap_bit_p (store_avail, loc))
tm_memopt_transform_stmt (TRANSFORM_RAW, stmt, &gsi);
else if (store_antic && bitmap_bit_p (store_antic, loc))
{
tm_memopt_transform_stmt (TRANSFORM_RFW, stmt, &gsi);
bitmap_set_bit (store_avail, loc);
}
else if (read_avail && bitmap_bit_p (read_avail, loc))
tm_memopt_transform_stmt (TRANSFORM_RAR, stmt, &gsi);
else
bitmap_set_bit (read_avail, loc);
}
else if (is_tm_simple_store (stmt))
{
loc = tm_memopt_value_number (stmt, NO_INSERT);
if (store_avail && bitmap_bit_p (store_avail, loc))
tm_memopt_transform_stmt (TRANSFORM_WAW, stmt, &gsi);
else
{
if (read_avail && bitmap_bit_p (read_avail, loc))
tm_memopt_transform_stmt (TRANSFORM_WAR, stmt, &gsi);
bitmap_set_bit (store_avail, loc);
}
}
}
}
}
/* Return a new set of bitmaps for a BB. */
static struct tm_memopt_bitmaps *
tm_memopt_init_sets (void)
{
struct tm_memopt_bitmaps *b
= XOBNEW (&tm_memopt_obstack.obstack, struct tm_memopt_bitmaps);
b->store_avail_in = BITMAP_ALLOC (&tm_memopt_obstack);
b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
b->store_antic_in = BITMAP_ALLOC (&tm_memopt_obstack);
b->store_antic_out = BITMAP_ALLOC (&tm_memopt_obstack);
b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
b->read_avail_in = BITMAP_ALLOC (&tm_memopt_obstack);
b->read_avail_out = BITMAP_ALLOC (&tm_memopt_obstack);
b->read_local = BITMAP_ALLOC (&tm_memopt_obstack);
b->store_local = BITMAP_ALLOC (&tm_memopt_obstack);
return b;
}
/* Free sets computed for each BB. */
static void
tm_memopt_free_sets (vec<basic_block> blocks)
{
size_t i;
basic_block bb;
for (i = 0; blocks.iterate (i, &bb); ++i)
bb->aux = NULL;
}
/* Clear the visited bit for every basic block in BLOCKS. */
static void
tm_memopt_clear_visited (vec<basic_block> blocks)
{
size_t i;
basic_block bb;
for (i = 0; blocks.iterate (i, &bb); ++i)
BB_VISITED_P (bb) = false;
}
/* Replace TM load/stores with hints for the runtime. We handle
things like read-after-write, write-after-read, read-after-read,
read-for-write, etc. */
static unsigned int
execute_tm_memopt (void)
{
struct tm_region *region;
vec<basic_block> bbs;
tm_memopt_value_id = 0;
tm_memopt_value_numbers = new hash_table<tm_memop_hasher> (10);
for (region = all_tm_regions; region; region = region->next)
{
/* All the TM stores/loads in the current region. */
size_t i;
basic_block bb;
bitmap_obstack_initialize (&tm_memopt_obstack);
/* Save all BBs for the current region. */
bbs = get_tm_region_blocks (region->entry_block,
region->exit_blocks,
region->irr_blocks,
NULL,
false);
/* Collect all the memory operations. */
for (i = 0; bbs.iterate (i, &bb); ++i)
{
bb->aux = tm_memopt_init_sets ();
tm_memopt_accumulate_memops (bb);
}
/* Solve data flow equations and transform each block accordingly. */
tm_memopt_clear_visited (bbs);
tm_memopt_compute_available (region, bbs);
tm_memopt_clear_visited (bbs);
tm_memopt_compute_antic (region, bbs);
tm_memopt_transform_blocks (bbs);
tm_memopt_free_sets (bbs);
bbs.release ();
bitmap_obstack_release (&tm_memopt_obstack);
tm_memopt_value_numbers->empty ();
}
delete tm_memopt_value_numbers;
tm_memopt_value_numbers = NULL;
return 0;
}
namespace {
const pass_data pass_data_tm_memopt =
{
GIMPLE_PASS, /* type */
"tmmemopt", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_TRANS_MEM, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_tm_memopt : public gimple_opt_pass
{
public:
pass_tm_memopt (gcc::context *ctxt)
: gimple_opt_pass (pass_data_tm_memopt, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *) { return flag_tm && optimize > 0; }
virtual unsigned int execute (function *) { return execute_tm_memopt (); }
}; // class pass_tm_memopt
} // anon namespace
gimple_opt_pass *
make_pass_tm_memopt (gcc::context *ctxt)
{
return new pass_tm_memopt (ctxt);
}
/* Interprocedual analysis for the creation of transactional clones.
The aim of this pass is to find which functions are referenced in
a non-irrevocable transaction context, and for those over which
we have control (or user directive), create a version of the
function which uses only the transactional interface to reference
protected memories. This analysis proceeds in several steps:
(1) Collect the set of all possible transactional clones:
(a) For all local public functions marked tm_callable, push
it onto the tm_callee queue.
(b) For all local functions, scan for calls in transaction blocks.
Push the caller and callee onto the tm_caller and tm_callee
queues. Count the number of callers for each callee.
(c) For each local function on the callee list, assume we will
create a transactional clone. Push *all* calls onto the
callee queues; count the number of clone callers separately
to the number of original callers.
(2) Propagate irrevocable status up the dominator tree:
(a) Any external function on the callee list that is not marked
tm_callable is irrevocable. Push all callers of such onto
a worklist.
(b) For each function on the worklist, mark each block that
contains an irrevocable call. Use the AND operator to
propagate that mark up the dominator tree.
(c) If we reach the entry block for a possible transactional
clone, then the transactional clone is irrevocable, and
we should not create the clone after all. Push all
callers onto the worklist.
(d) Place tm_irrevocable calls at the beginning of the relevant
blocks. Special case here is the entry block for the entire
transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for
the library to begin the region in serial mode. Decrement
the call count for all callees in the irrevocable region.
(3) Create the transactional clones:
Any tm_callee that still has a non-zero call count is cloned.
*/
/* This structure is stored in the AUX field of each cgraph_node. */
struct tm_ipa_cg_data
{
/* The clone of the function that got created. */
struct cgraph_node *clone;
/* The tm regions in the normal function. */
struct tm_region *all_tm_regions;
/* The blocks of the normal/clone functions that contain irrevocable
calls, or blocks that are post-dominated by irrevocable calls. */
bitmap irrevocable_blocks_normal;
bitmap irrevocable_blocks_clone;
/* The blocks of the normal function that are involved in transactions. */
bitmap transaction_blocks_normal;
/* The number of callers to the transactional clone of this function
from normal and transactional clones respectively. */
unsigned tm_callers_normal;
unsigned tm_callers_clone;
/* True if all calls to this function's transactional clone
are irrevocable. Also automatically true if the function
has no transactional clone. */
bool is_irrevocable;
/* Flags indicating the presence of this function in various queues. */
bool in_callee_queue;
bool in_worklist;
/* Flags indicating the kind of scan desired while in the worklist. */
bool want_irr_scan_normal;
};
typedef vec<cgraph_node *> cgraph_node_queue;
/* Return the ipa data associated with NODE, allocating zeroed memory
if necessary. TRAVERSE_ALIASES is true if we must traverse aliases
and set *NODE accordingly. */
static struct tm_ipa_cg_data *
get_cg_data (struct cgraph_node **node, bool traverse_aliases)
{
struct tm_ipa_cg_data *d;
if (traverse_aliases && (*node)->alias)
*node = (*node)->get_alias_target ();
d = (struct tm_ipa_cg_data *) (*node)->aux;
if (d == NULL)
{
d = (struct tm_ipa_cg_data *)
obstack_alloc (&tm_obstack.obstack, sizeof (*d));
(*node)->aux = (void *) d;
memset (d, 0, sizeof (*d));
}
return d;
}
/* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that
it is already present. */
static void
maybe_push_queue (struct cgraph_node *node,
cgraph_node_queue *queue_p, bool *in_queue_p)
{
if (!*in_queue_p)
{
*in_queue_p = true;
queue_p->safe_push (node);
}
}
/* Duplicate the basic blocks in QUEUE for use in the uninstrumented
code path. QUEUE are the basic blocks inside the transaction
represented in REGION.
Later in split_code_paths() we will add the conditional to choose
between the two alternatives. */
static void
ipa_uninstrument_transaction (struct tm_region *region,
vec<basic_block> queue)
{
gimple transaction = region->transaction_stmt;
basic_block transaction_bb = gimple_bb (transaction);
int n = queue.length ();
basic_block *new_bbs = XNEWVEC (basic_block, n);
copy_bbs (queue.address (), n, new_bbs, NULL, 0, NULL, NULL, transaction_bb,
true);
edge e = make_edge (transaction_bb, new_bbs[0], EDGE_TM_UNINSTRUMENTED);
add_phi_args_after_copy (new_bbs, n, e);
// Now we will have a GIMPLE_ATOMIC with 3 possible edges out of it.
// a) EDGE_FALLTHRU into the transaction
// b) EDGE_TM_ABORT out of the transaction
// c) EDGE_TM_UNINSTRUMENTED into the uninstrumented blocks.
free (new_bbs);
}
/* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone.
Queue all callees within block BB. */
static void
ipa_tm_scan_calls_block (cgraph_node_queue *callees_p,
basic_block bb, bool for_clone)
{
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
if (is_gimple_call (stmt) && !is_tm_pure_call (stmt))
{
tree fndecl = gimple_call_fndecl (stmt);
if (fndecl)
{
struct tm_ipa_cg_data *d;
unsigned *pcallers;
struct cgraph_node *node;
if (is_tm_ending_fndecl (fndecl))
continue;
if (find_tm_replacement_function (fndecl))
continue;
node = cgraph_node::get (fndecl);
gcc_assert (node != NULL);
d = get_cg_data (&node, true);
pcallers = (for_clone ? &d->tm_callers_clone
: &d->tm_callers_normal);
*pcallers += 1;
maybe_push_queue (node, callees_p, &d->in_callee_queue);
}
}
}
}
/* Scan all calls in NODE that are within a transaction region,
and push the resulting nodes into the callee queue. */
static void
ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data *d,
cgraph_node_queue *callees_p)
{
struct tm_region *r;
d->transaction_blocks_normal = BITMAP_ALLOC (&tm_obstack);
d->all_tm_regions = all_tm_regions;
for (r = all_tm_regions; r; r = r->next)
{
vec<basic_block> bbs;
basic_block bb;
unsigned i;
bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks, NULL,
d->transaction_blocks_normal, false);
// Generate the uninstrumented code path for this transaction.
ipa_uninstrument_transaction (r, bbs);
FOR_EACH_VEC_ELT (bbs, i, bb)
ipa_tm_scan_calls_block (callees_p, bb, false);
bbs.release ();
}
// ??? copy_bbs should maintain cgraph edges for the blocks as it is
// copying them, rather than forcing us to do this externally.
cgraph_edge::rebuild_edges ();
// ??? In ipa_uninstrument_transaction we don't try to update dominators
// because copy_bbs doesn't return a VEC like iterate_fix_dominators expects.
// Instead, just release dominators here so update_ssa recomputes them.
free_dominance_info (CDI_DOMINATORS);
// When building the uninstrumented code path, copy_bbs will have invoked
// create_new_def_for starting an "ssa update context". There is only one
// instance of this context, so resolve ssa updates before moving on to
// the next function.
update_ssa (TODO_update_ssa);
}
/* Scan all calls in NODE as if this is the transactional clone,
and push the destinations into the callee queue. */
static void
ipa_tm_scan_calls_clone (struct cgraph_node *node,
cgraph_node_queue *callees_p)
{
struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
basic_block bb;
FOR_EACH_BB_FN (bb, fn)
ipa_tm_scan_calls_block (callees_p, bb, true);
}
/* The function NODE has been detected to be irrevocable. Push all
of its callers onto WORKLIST for the purpose of re-scanning them. */
static void
ipa_tm_note_irrevocable (struct cgraph_node *node,
cgraph_node_queue *worklist_p)
{
struct tm_ipa_cg_data *d = get_cg_data (&node, true);
struct cgraph_edge *e;
d->is_irrevocable = true;
for (e = node->callers; e ; e = e->next_caller)
{
basic_block bb;
struct cgraph_node *caller;
/* Don't examine recursive calls. */
if (e->caller == node)
continue;
/* Even if we think we can go irrevocable, believe the user
above all. */
if (is_tm_safe_or_pure (e->caller->decl))
continue;
caller = e->caller;
d = get_cg_data (&caller, true);
/* Check if the callee is in a transactional region. If so,
schedule the function for normal re-scan as well. */
bb = gimple_bb (e->call_stmt);
gcc_assert (bb != NULL);
if (d->transaction_blocks_normal
&& bitmap_bit_p (d->transaction_blocks_normal, bb->index))
d->want_irr_scan_normal = true;
maybe_push_queue (caller, worklist_p, &d->in_worklist);
}
}
/* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement
within the block is irrevocable. */
static bool
ipa_tm_scan_irr_block (basic_block bb)
{
gimple_stmt_iterator gsi;
tree fn;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
switch (gimple_code (stmt))
{
case GIMPLE_ASSIGN:
if (gimple_assign_single_p (stmt))
{
tree lhs = gimple_assign_lhs (stmt);
tree rhs = gimple_assign_rhs1 (stmt);
if (volatile_var_p (lhs) || volatile_var_p (rhs))
return true;
}
break;
case GIMPLE_CALL:
{
tree lhs = gimple_call_lhs (stmt);
if (lhs && volatile_var_p (lhs))
return true;
if (is_tm_pure_call (stmt))
break;
fn = gimple_call_fn (stmt);
/* Functions with the attribute are by definition irrevocable. */
if (is_tm_irrevocable (fn))
return true;
/* For direct function calls, go ahead and check for replacement
functions, or transitive irrevocable functions. For indirect
functions, we'll ask the runtime. */
if (TREE_CODE (fn) == ADDR_EXPR)
{
struct tm_ipa_cg_data *d;
struct cgraph_node *node;
fn = TREE_OPERAND (fn, 0);
if (is_tm_ending_fndecl (fn))
break;
if (find_tm_replacement_function (fn))
break;
node = cgraph_node::get (fn);
d = get_cg_data (&node, true);
/* Return true if irrevocable, but above all, believe
the user. */
if (d->is_irrevocable
&& !is_tm_safe_or_pure (fn))
return true;
}
break;
}
case GIMPLE_ASM:
/* ??? The Approved Method of indicating that an inline
assembly statement is not relevant to the transaction
is to wrap it in a __tm_waiver block. This is not
yet implemented, so we can't check for it. */
if (is_tm_safe (current_function_decl))
{
tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
SET_EXPR_LOCATION (t, gimple_location (stmt));
error ("%Kasm not allowed in %<transaction_safe%> function", t);
}
return true;
default:
break;
}
}
return false;
}
/* For each of the blocks seeded witin PQUEUE, walk the CFG looking
for new irrevocable blocks, marking them in NEW_IRR. Don't bother
scanning past OLD_IRR or EXIT_BLOCKS. */
static bool
ipa_tm_scan_irr_blocks (vec<basic_block> *pqueue, bitmap new_irr,
bitmap old_irr, bitmap exit_blocks)
{
bool any_new_irr = false;
edge e;
edge_iterator ei;
bitmap visited_blocks = BITMAP_ALLOC (NULL);
do
{
basic_block bb = pqueue->pop ();
/* Don't re-scan blocks we know already are irrevocable. */
if (old_irr && bitmap_bit_p (old_irr, bb->index))
continue;
if (ipa_tm_scan_irr_block (bb))
{
bitmap_set_bit (new_irr, bb->index);
any_new_irr = true;
}
else if (exit_blocks == NULL || !bitmap_bit_p (exit_blocks, bb->index))
{
FOR_EACH_EDGE (e, ei, bb->succs)
if (!bitmap_bit_p (visited_blocks, e->dest->index))
{
bitmap_set_bit (visited_blocks, e->dest->index);
pqueue->safe_push (e->dest);
}
}
}
while (!pqueue->is_empty ());
BITMAP_FREE (visited_blocks);
return any_new_irr;
}
/* Propagate the irrevocable property both up and down the dominator tree.
BB is the current block being scanned; EXIT_BLOCKS are the edges of the
TM regions; OLD_IRR are the results of a previous scan of the dominator
tree which has been fully propagated; NEW_IRR is the set of new blocks
which are gaining the irrevocable property during the current scan. */
static void
ipa_tm_propagate_irr (basic_block entry_block, bitmap new_irr,
bitmap old_irr, bitmap exit_blocks)
{
vec<basic_block> bbs;
bitmap all_region_blocks;
/* If this block is in the old set, no need to rescan. */
if (old_irr && bitmap_bit_p (old_irr, entry_block->index))
return;
all_region_blocks = BITMAP_ALLOC (&tm_obstack);
bbs = get_tm_region_blocks (entry_block, exit_blocks, NULL,
all_region_blocks, false);
do
{
basic_block bb = bbs.pop ();
bool this_irr = bitmap_bit_p (new_irr, bb->index);
bool all_son_irr = false;
edge_iterator ei;
edge e;
/* Propagate up. If my children are, I am too, but we must have
at least one child that is. */
if (!this_irr)
{
FOR_EACH_EDGE (e, ei, bb->succs)
{
if (!bitmap_bit_p (new_irr, e->dest->index))
{
all_son_irr = false;
break;
}
else
all_son_irr = true;
}
if (all_son_irr)
{
/* Add block to new_irr if it hasn't already been processed. */
if (!old_irr || !bitmap_bit_p (old_irr, bb->index))
{
bitmap_set_bit (new_irr, bb->index);
this_irr = true;
}
}
}
/* Propagate down to everyone we immediately dominate. */
if (this_irr)
{
basic_block son;
for (son = first_dom_son (CDI_DOMINATORS, bb);
son;
son = next_dom_son (CDI_DOMINATORS, son))
{
/* Make sure block is actually in a TM region, and it
isn't already in old_irr. */
if ((!old_irr || !bitmap_bit_p (old_irr, son->index))
&& bitmap_bit_p (all_region_blocks, son->index))
bitmap_set_bit (new_irr, son->index);
}
}
}
while (!bbs.is_empty ());
BITMAP_FREE (all_region_blocks);
bbs.release ();
}
static void
ipa_tm_decrement_clone_counts (basic_block bb, bool for_clone)
{
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
if (is_gimple_call (stmt) && !is_tm_pure_call (stmt))
{
tree fndecl = gimple_call_fndecl (stmt);
if (fndecl)
{
struct tm_ipa_cg_data *d;
unsigned *pcallers;
struct cgraph_node *tnode;
if (is_tm_ending_fndecl (fndecl))
continue;
if (find_tm_replacement_function (fndecl))
continue;
tnode = cgraph_node::get (fndecl);
d = get_cg_data (&tnode, true);
pcallers = (for_clone ? &d->tm_callers_clone
: &d->tm_callers_normal);
gcc_assert (*pcallers > 0);
*pcallers -= 1;
}
}
}
}
/* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions,
as well as other irrevocable actions such as inline assembly. Mark all
such blocks as irrevocable and decrement the number of calls to
transactional clones. Return true if, for the transactional clone, the
entire function is irrevocable. */
static bool
ipa_tm_scan_irr_function (struct cgraph_node *node, bool for_clone)
{
struct tm_ipa_cg_data *d;
bitmap new_irr, old_irr;
bool ret = false;
/* Builtin operators (operator new, and such). */
if (DECL_STRUCT_FUNCTION (node->decl) == NULL
|| DECL_STRUCT_FUNCTION (node->decl)->cfg == NULL)
return false;
push_cfun (DECL_STRUCT_FUNCTION (node->decl));
calculate_dominance_info (CDI_DOMINATORS);
d = get_cg_data (&node, true);
auto_vec<basic_block, 10> queue;
new_irr = BITMAP_ALLOC (&tm_obstack);
/* Scan each tm region, propagating irrevocable status through the tree. */
if (for_clone)
{
old_irr = d->irrevocable_blocks_clone;
queue.quick_push (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr, NULL))
{
ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
new_irr,
old_irr, NULL);
ret = bitmap_bit_p (new_irr,
single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))->index);
}
}
else
{
struct tm_region *region;
old_irr = d->irrevocable_blocks_normal;
for (region = d->all_tm_regions; region; region = region->next)
{
queue.quick_push (region->entry_block);
if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr,
region->exit_blocks))
ipa_tm_propagate_irr (region->entry_block, new_irr, old_irr,
region->exit_blocks);
}
}
/* If we found any new irrevocable blocks, reduce the call count for
transactional clones within the irrevocable blocks. Save the new
set of irrevocable blocks for next time. */
if (!bitmap_empty_p (new_irr))
{
bitmap_iterator bmi;
unsigned i;
EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi)
ipa_tm_decrement_clone_counts (BASIC_BLOCK_FOR_FN (cfun, i),
for_clone);
if (old_irr)
{
bitmap_ior_into (old_irr, new_irr);
BITMAP_FREE (new_irr);
}
else if (for_clone)
d->irrevocable_blocks_clone = new_irr;
else
d->irrevocable_blocks_normal = new_irr;
if (dump_file && new_irr)
{
const char *dname;
bitmap_iterator bmi;
unsigned i;
dname = lang_hooks.decl_printable_name (current_function_decl, 2);
EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi)
fprintf (dump_file, "%s: bb %d goes irrevocable\n", dname, i);
}
}
else
BITMAP_FREE (new_irr);
pop_cfun ();
return ret;
}
/* Return true if, for the transactional clone of NODE, any call
may enter irrevocable mode. */
static bool
ipa_tm_mayenterirr_function (struct cgraph_node *node)
{
struct tm_ipa_cg_data *d;
tree decl;
unsigned flags;
d = get_cg_data (&node, true);
decl = node->decl;
flags = flags_from_decl_or_type (decl);
/* Handle some TM builtins. Ordinarily these aren't actually generated
at this point, but handling these functions when written in by the
user makes it easier to build unit tests. */
if (flags & ECF_TM_BUILTIN)
return false;
/* Filter out all functions that are marked. */
if (flags & ECF_TM_PURE)
return false;
if (is_tm_safe (decl))
return false;
if (is_tm_irrevocable (decl))
return true;
if (is_tm_callable (decl))
return true;
if (find_tm_replacement_function (decl))
return true;
/* If we aren't seeing the final version of the function we don't
know what it will contain at runtime. */
if (node->get_availability () < AVAIL_AVAILABLE)
return true;
/* If the function must go irrevocable, then of course true. */
if (d->is_irrevocable)
return true;
/* If there are any blocks marked irrevocable, then the function
as a whole may enter irrevocable. */
if (d->irrevocable_blocks_clone)
return true;
/* We may have previously marked this function as tm_may_enter_irr;
see pass_diagnose_tm_blocks. */
if (node->local.tm_may_enter_irr)
return true;
/* Recurse on the main body for aliases. In general, this will
result in one of the bits above being set so that we will not
have to recurse next time. */
if (node->alias)
return ipa_tm_mayenterirr_function (cgraph_node::get (node->thunk.alias));
/* What remains is unmarked local functions without items that force
the function to go irrevocable. */
return false;
}
/* Diagnose calls from transaction_safe functions to unmarked
functions that are determined to not be safe. */
static void
ipa_tm_diagnose_tm_safe (struct cgraph_node *node)
{
struct cgraph_edge *e;
for (e = node->callees; e ; e = e->next_callee)
if (!is_tm_callable (e->callee->decl)
&& e->callee->local.tm_may_enter_irr)
error_at (gimple_location (e->call_stmt),
"unsafe function call %qD within "
"%<transaction_safe%> function", e->callee->decl);
}
/* Diagnose call from atomic transactions to unmarked functions
that are determined to not be safe. */
static void
ipa_tm_diagnose_transaction (struct cgraph_node *node,
struct tm_region *all_tm_regions)
{
struct tm_region *r;
for (r = all_tm_regions; r ; r = r->next)
if (gimple_transaction_subcode (r->transaction_stmt) & GTMA_IS_RELAXED)
{
/* Atomic transactions can be nested inside relaxed. */
if (r->inner)
ipa_tm_diagnose_transaction (node, r->inner);
}
else
{
vec<basic_block> bbs;
gimple_stmt_iterator gsi;
basic_block bb;
size_t i;
bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks,
r->irr_blocks, NULL, false);
for (i = 0; bbs.iterate (i, &bb); ++i)
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
tree fndecl;
if (gimple_code (stmt) == GIMPLE_ASM)
{
error_at (gimple_location (stmt),
"asm not allowed in atomic transaction");
continue;
}
if (!is_gimple_call (stmt))
continue;
fndecl = gimple_call_fndecl (stmt);
/* Indirect function calls have been diagnosed already. */
if (!fndecl)
continue;
/* Stop at the end of the transaction. */
if (is_tm_ending_fndecl (fndecl))
{
if (bitmap_bit_p (r->exit_blocks, bb->index))
break;
continue;
}
/* Marked functions have been diagnosed already. */
if (is_tm_pure_call (stmt))
continue;
if (is_tm_callable (fndecl))
continue;
if (cgraph_node::local_info (fndecl)->tm_may_enter_irr)
error_at (gimple_location (stmt),
"unsafe function call %qD within "
"atomic transaction", fndecl);
}
bbs.release ();
}
}
/* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in
OLD_DECL. The returned value is a freshly malloced pointer that
should be freed by the caller. */
static tree
tm_mangle (tree old_asm_id)
{
const char *old_asm_name;
char *tm_name;
void *alloc = NULL;
struct demangle_component *dc;
tree new_asm_id;
/* Determine if the symbol is already a valid C++ mangled name. Do this
even for C, which might be interfacing with C++ code via appropriately
ugly identifiers. */
/* ??? We could probably do just as well checking for "_Z" and be done. */
old_asm_name = IDENTIFIER_POINTER (old_asm_id);
dc = cplus_demangle_v3_components (old_asm_name, DMGL_NO_OPTS, &alloc);
if (dc == NULL)
{
char length[8];
do_unencoded:
sprintf (length, "%u", IDENTIFIER_LENGTH (old_asm_id));
tm_name = concat ("_ZGTt", length, old_asm_name, NULL);
}
else
{
old_asm_name += 2; /* Skip _Z */
switch (dc->type)
{
case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
/* Don't play silly games, you! */
goto do_unencoded;
case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
/* I'd really like to know if we can ever be passed one of
these from the C++ front end. The Logical Thing would
seem that hidden-alias should be outer-most, so that we
get hidden-alias of a transaction-clone and not vice-versa. */
old_asm_name += 2;
break;
default:
break;
}
tm_name = concat ("_ZGTt", old_asm_name, NULL);
}
free (alloc);
new_asm_id = get_identifier (tm_name);
free (tm_name);
return new_asm_id;
}
static inline void
ipa_tm_mark_force_output_node (struct cgraph_node *node)
{
node->mark_force_output ();
node->analyzed = true;
}
static inline void
ipa_tm_mark_forced_by_abi_node (struct cgraph_node *node)
{
node->forced_by_abi = true;
node->analyzed = true;
}
/* Callback data for ipa_tm_create_version_alias. */
struct create_version_alias_info
{
struct cgraph_node *old_node;
tree new_decl;
};
/* A subroutine of ipa_tm_create_version, called via
cgraph_for_node_and_aliases. Create new tm clones for each of
the existing aliases. */
static bool
ipa_tm_create_version_alias (struct cgraph_node *node, void *data)
{
struct create_version_alias_info *info
= (struct create_version_alias_info *)data;
tree old_decl, new_decl, tm_name;
struct cgraph_node *new_node;
if (!node->cpp_implicit_alias)
return false;
old_decl = node->decl;
tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl));
new_decl = build_decl (DECL_SOURCE_LOCATION (old_decl),
TREE_CODE (old_decl), tm_name,
TREE_TYPE (old_decl));
SET_DECL_ASSEMBLER_NAME (new_decl, tm_name);
SET_DECL_RTL (new_decl, NULL);
/* Based loosely on C++'s make_alias_for(). */
TREE_PUBLIC (new_decl) = TREE_PUBLIC (old_decl);
DECL_CONTEXT (new_decl) = DECL_CONTEXT (old_decl);
DECL_LANG_SPECIFIC (new_decl) = DECL_LANG_SPECIFIC (old_decl);
TREE_READONLY (new_decl) = TREE_READONLY (old_decl);
DECL_EXTERNAL (new_decl) = 0;
DECL_ARTIFICIAL (new_decl) = 1;
TREE_ADDRESSABLE (new_decl) = 1;
TREE_USED (new_decl) = 1;
TREE_SYMBOL_REFERENCED (tm_name) = 1;
/* Perform the same remapping to the comdat group. */
if (DECL_ONE_ONLY (new_decl))
varpool_node::get (new_decl)->set_comdat_group
(tm_mangle (decl_comdat_group_id (old_decl)));
new_node = cgraph_node::create_same_body_alias (new_decl, info->new_decl);
new_node->tm_clone = true;
new_node->externally_visible = info->old_node->externally_visible;
new_node->no_reorder = info->old_node->no_reorder;
/* ?? Do not traverse aliases here. */
get_cg_data (&node, false)->clone = new_node;
record_tm_clone_pair (old_decl, new_decl);
if (info->old_node->force_output
|| info->old_node->ref_list.first_referring ())
ipa_tm_mark_force_output_node (new_node);
if (info->old_node->forced_by_abi)
ipa_tm_mark_forced_by_abi_node (new_node);
return false;
}
/* Create a copy of the function (possibly declaration only) of OLD_NODE,
appropriate for the transactional clone. */
static void
ipa_tm_create_version (struct cgraph_node *old_node)
{
tree new_decl, old_decl, tm_name;
struct cgraph_node *new_node;
old_decl = old_node->decl;
new_decl = copy_node (old_decl);
/* DECL_ASSEMBLER_NAME needs to be set before we call
cgraph_copy_node_for_versioning below, because cgraph_node will
fill the assembler_name_hash. */
tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl));
SET_DECL_ASSEMBLER_NAME (new_decl, tm_name);
SET_DECL_RTL (new_decl, NULL);
TREE_SYMBOL_REFERENCED (tm_name) = 1;
/* Perform the same remapping to the comdat group. */
if (DECL_ONE_ONLY (new_decl))
varpool_node::get (new_decl)->set_comdat_group
(tm_mangle (DECL_COMDAT_GROUP (old_decl)));
gcc_assert (!old_node->ipa_transforms_to_apply.exists ());
new_node = old_node->create_version_clone (new_decl, vNULL, NULL);
new_node->local.local = false;
new_node->externally_visible = old_node->externally_visible;
new_node->lowered = true;
new_node->tm_clone = 1;
get_cg_data (&old_node, true)->clone = new_node;
if (old_node->get_availability () >= AVAIL_INTERPOSABLE)
{
/* Remap extern inline to static inline. */
/* ??? Is it worth trying to use make_decl_one_only? */
if (DECL_DECLARED_INLINE_P (new_decl) && DECL_EXTERNAL (new_decl))
{
DECL_EXTERNAL (new_decl) = 0;
TREE_PUBLIC (new_decl) = 0;
DECL_WEAK (new_decl) = 0;
}
tree_function_versioning (old_decl, new_decl,
NULL, false, NULL,
false, NULL, NULL);
}
record_tm_clone_pair (old_decl, new_decl);
symtab->call_cgraph_insertion_hooks (new_node);
if (old_node->force_output
|| old_node->ref_list.first_referring ())
ipa_tm_mark_force_output_node (new_node);
if (old_node->forced_by_abi)
ipa_tm_mark_forced_by_abi_node (new_node);
/* Do the same thing, but for any aliases of the original node. */
{
struct create_version_alias_info data;
data.old_node = old_node;
data.new_decl = new_decl;
old_node->call_for_symbol_thunks_and_aliases (ipa_tm_create_version_alias,
&data, true);
}
}
/* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */
static void
ipa_tm_insert_irr_call (struct cgraph_node *node, struct tm_region *region,
basic_block bb)
{
gimple_stmt_iterator gsi;
gimple g;
transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE),
1, build_int_cst (NULL_TREE, MODE_SERIALIRREVOCABLE));
split_block_after_labels (bb);
gsi = gsi_after_labels (bb);
gsi_insert_before (&gsi, g, GSI_SAME_STMT);
node->create_edge (cgraph_node::get_create
(builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE)),
g, 0,
compute_call_stmt_bb_frequency (node->decl,
gimple_bb (g)));
}
/* Construct a call to TM_GETTMCLONE and insert it before GSI. */
static bool
ipa_tm_insert_gettmclone_call (struct cgraph_node *node,
struct tm_region *region,
gimple_stmt_iterator *gsi, gimple stmt)
{
tree gettm_fn, ret, old_fn, callfn;
gimple g, g2;
bool safe;
old_fn = gimple_call_fn (stmt);
if (TREE_CODE (old_fn) == ADDR_EXPR)
{
tree fndecl = TREE_OPERAND (old_fn, 0);
tree clone = get_tm_clone_pair (fndecl);
/* By transforming the call into a TM_GETTMCLONE, we are
technically taking the address of the original function and
its clone. Explain this so inlining will know this function
is needed. */
cgraph_node::get (fndecl)->mark_address_taken () ;
if (clone)
cgraph_node::get (clone)->mark_address_taken ();
}
safe = is_tm_safe (TREE_TYPE (old_fn));
gettm_fn = builtin_decl_explicit (safe ? BUILT_IN_TM_GETTMCLONE_SAFE
: BUILT_IN_TM_GETTMCLONE_IRR);
ret = create_tmp_var (ptr_type_node, NULL);
if (!safe)
transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE);
/* Discard OBJ_TYPE_REF, since we weren't able to fold it. */
if (TREE_CODE (old_fn) == OBJ_TYPE_REF)
old_fn = OBJ_TYPE_REF_EXPR (old_fn);
g = gimple_build_call (gettm_fn, 1, old_fn);
ret = make_ssa_name (ret, g);
gimple_call_set_lhs (g, ret);
gsi_insert_before (gsi, g, GSI_SAME_STMT);
node->create_edge (cgraph_node::get_create (gettm_fn), g, 0,
compute_call_stmt_bb_frequency (node->decl,
gimple_bb (g)));
/* Cast return value from tm_gettmclone* into appropriate function
pointer. */
callfn = create_tmp_var (TREE_TYPE (old_fn), NULL);
g2 = gimple_build_assign (callfn,
fold_build1 (NOP_EXPR, TREE_TYPE (callfn), ret));
callfn = make_ssa_name (callfn, g2);
gimple_assign_set_lhs (g2, callfn);
gsi_insert_before (gsi, g2, GSI_SAME_STMT);
/* ??? This is a hack to preserve the NOTHROW bit on the call,
which we would have derived from the decl. Failure to save
this bit means we might have to split the basic block. */
if (gimple_call_nothrow_p (stmt))
gimple_call_set_nothrow (stmt, true);
gimple_call_set_fn (stmt, callfn);
/* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS
for a call statement. Fix it. */
{
tree lhs = gimple_call_lhs (stmt);
tree rettype = TREE_TYPE (gimple_call_fntype (stmt));
if (lhs
&& !useless_type_conversion_p (TREE_TYPE (lhs), rettype))
{
tree temp;
temp = create_tmp_reg (rettype, 0);
gimple_call_set_lhs (stmt, temp);
g2 = gimple_build_assign (lhs,
fold_build1 (VIEW_CONVERT_EXPR,
TREE_TYPE (lhs), temp));
gsi_insert_after (gsi, g2, GSI_SAME_STMT);
}
}
update_stmt (stmt);
cgraph_edge *e = cgraph_node::get (current_function_decl)->get_edge (stmt);
if (e && e->indirect_info)
e->indirect_info->polymorphic = false;
return true;
}
/* Helper function for ipa_tm_transform_calls*. Given a call
statement in GSI which resides inside transaction REGION, redirect
the call to either its wrapper function, or its clone. */
static void
ipa_tm_transform_calls_redirect (struct cgraph_node *node,
struct tm_region *region,
gimple_stmt_iterator *gsi,
bool *need_ssa_rename_p)
{
gimple stmt = gsi_stmt (*gsi);
struct cgraph_node *new_node;
struct cgraph_edge *e = node->get_edge (stmt);
tree fndecl = gimple_call_fndecl (stmt);
/* For indirect calls, pass the address through the runtime. */
if (fndecl == NULL)
{
*need_ssa_rename_p |=
ipa_tm_insert_gettmclone_call (node, region, gsi, stmt);
return;
}
/* Handle some TM builtins. Ordinarily these aren't actually generated
at this point, but handling these functions when written in by the
user makes it easier to build unit tests. */
if (flags_from_decl_or_type (fndecl) & ECF_TM_BUILTIN)
return;
/* Fixup recursive calls inside clones. */
/* ??? Why did cgraph_copy_node_for_versioning update the call edges
for recursion but not update the call statements themselves? */
if (e->caller == e->callee && decl_is_tm_clone (current_function_decl))
{
gimple_call_set_fndecl (stmt, current_function_decl);
return;
}
/* If there is a replacement, use it. */
fndecl = find_tm_replacement_function (fndecl);
if (fndecl)
{
new_node = cgraph_node::get_create (fndecl);
/* ??? Mark all transaction_wrap functions tm_may_enter_irr.
We can't do this earlier in record_tm_replacement because
cgraph_remove_unreachable_nodes is called before we inject
references to the node. Further, we can't do this in some
nice central place in ipa_tm_execute because we don't have
the exact list of wrapper functions that would be used.
Marking more wrappers than necessary results in the creation
of unnecessary cgraph_nodes, which can cause some of the
other IPA passes to crash.
We do need to mark these nodes so that we get the proper
result in expand_call_tm. */
/* ??? This seems broken. How is it that we're marking the
CALLEE as may_enter_irr? Surely we should be marking the
CALLER. Also note that find_tm_replacement_function also
contains mappings into the TM runtime, e.g. memcpy. These
we know won't go irrevocable. */
new_node->local.tm_may_enter_irr = 1;
}
else
{
struct tm_ipa_cg_data *d;
struct cgraph_node *tnode = e->callee;
d = get_cg_data (&tnode, true);
new_node = d->clone;
/* As we've already skipped pure calls and appropriate builtins,
and we've already marked irrevocable blocks, if we can't come
up with a static replacement, then ask the runtime. */
if (new_node == NULL)
{
*need_ssa_rename_p |=
ipa_tm_insert_gettmclone_call (node, region, gsi, stmt);
return;
}
fndecl = new_node->decl;
}
e->redirect_callee (new_node);
gimple_call_set_fndecl (stmt, fndecl);
}
/* Helper function for ipa_tm_transform_calls. For a given BB,
install calls to tm_irrevocable when IRR_BLOCKS are reached,
redirect other calls to the generated transactional clone. */
static bool
ipa_tm_transform_calls_1 (struct cgraph_node *node, struct tm_region *region,
basic_block bb, bitmap irr_blocks)
{
gimple_stmt_iterator gsi;
bool need_ssa_rename = false;
if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index))
{
ipa_tm_insert_irr_call (node, region, bb);
return true;
}
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);
if (!is_gimple_call (stmt))
continue;
if (is_tm_pure_call (stmt))
continue;
/* Redirect edges to the appropriate replacement or clone. */
ipa_tm_transform_calls_redirect (node, region, &gsi, &need_ssa_rename);
}
return need_ssa_rename;
}
/* Walk the CFG for REGION, beginning at BB. Install calls to
tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to
the generated transactional clone. */
static bool
ipa_tm_transform_calls (struct cgraph_node *node, struct tm_region *region,
basic_block bb, bitmap irr_blocks)
{
bool need_ssa_rename = false;
edge e;
edge_iterator ei;
auto_vec<basic_block> queue;
bitmap visited_blocks = BITMAP_ALLOC (NULL);
queue.safe_push (bb);
do
{
bb = queue.pop ();
need_ssa_rename |=
ipa_tm_transform_calls_1 (node, region, bb, irr_blocks);
if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index))
continue;
if (region && bitmap_bit_p (region->exit_blocks, bb->index))
continue;
FOR_EACH_EDGE (e, ei, bb->succs)
if (!bitmap_bit_p (visited_blocks, e->dest->index))
{
bitmap_set_bit (visited_blocks, e->dest->index);
queue.safe_push (e->dest);
}
}
while (!queue.is_empty ());
BITMAP_FREE (visited_blocks);
return need_ssa_rename;
}
/* Transform the calls within the TM regions within NODE. */
static void
ipa_tm_transform_transaction (struct cgraph_node *node)
{
struct tm_ipa_cg_data *d;
struct tm_region *region;
bool need_ssa_rename = false;
d = get_cg_data (&node, true);
push_cfun (DECL_STRUCT_FUNCTION (node->decl));
calculate_dominance_info (CDI_DOMINATORS);
for (region = d->all_tm_regions; region; region = region->next)
{
/* If we're sure to go irrevocable, don't transform anything. */
if (d->irrevocable_blocks_normal
&& bitmap_bit_p (d->irrevocable_blocks_normal,
region->entry_block->index))
{
transaction_subcode_ior (region, GTMA_DOES_GO_IRREVOCABLE
| GTMA_MAY_ENTER_IRREVOCABLE
| GTMA_HAS_NO_INSTRUMENTATION);
continue;
}
need_ssa_rename |=
ipa_tm_transform_calls (node, region, region->entry_block,
d->irrevocable_blocks_normal);
}
if (need_ssa_rename)
update_ssa (TODO_update_ssa_only_virtuals);
pop_cfun ();
}
/* Transform the calls within the transactional clone of NODE. */
static void
ipa_tm_transform_clone (struct cgraph_node *node)
{
struct tm_ipa_cg_data *d;
bool need_ssa_rename;
d = get_cg_data (&node, true);
/* If this function makes no calls and has no irrevocable blocks,
then there's nothing to do. */
/* ??? Remove non-aborting top-level transactions. */
if (!node->callees && !node->indirect_calls && !d->irrevocable_blocks_clone)
return;
push_cfun (DECL_STRUCT_FUNCTION (d->clone->decl));
calculate_dominance_info (CDI_DOMINATORS);
need_ssa_rename =
ipa_tm_transform_calls (d->clone, NULL,
single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
d->irrevocable_blocks_clone);
if (need_ssa_rename)
update_ssa (TODO_update_ssa_only_virtuals);
pop_cfun ();
}
/* Main entry point for the transactional memory IPA pass. */
static unsigned int
ipa_tm_execute (void)
{
cgraph_node_queue tm_callees = cgraph_node_queue ();
/* List of functions that will go irrevocable. */
cgraph_node_queue irr_worklist = cgraph_node_queue ();
struct cgraph_node *node;
struct tm_ipa_cg_data *d;
enum availability a;
unsigned int i;
#ifdef ENABLE_CHECKING
cgraph_node::verify_cgraph_nodes ();
#endif
bitmap_obstack_initialize (&tm_obstack);
initialize_original_copy_tables ();
/* For all local functions marked tm_callable, queue them. */
FOR_EACH_DEFINED_FUNCTION (node)
if (is_tm_callable (node->decl)
&& node->get_availability () >= AVAIL_INTERPOSABLE)
{
d = get_cg_data (&node, true);
maybe_push_queue (node, &tm_callees, &d->in_callee_queue);
}
/* For all local reachable functions... */
FOR_EACH_DEFINED_FUNCTION (node)
if (node->lowered
&& node->get_availability () >= AVAIL_INTERPOSABLE)
{
/* ... marked tm_pure, record that fact for the runtime by
indicating that the pure function is its own tm_callable.
No need to do this if the function's address can't be taken. */
if (is_tm_pure (node->decl))
{
if (!node->local.local)
record_tm_clone_pair (node->decl, node->decl);
continue;
}
push_cfun (DECL_STRUCT_FUNCTION (node->decl));
calculate_dominance_info (CDI_DOMINATORS);
tm_region_init (NULL);
if (all_tm_regions)
{
d = get_cg_data (&node, true);
/* Scan for calls that are in each transaction, and
generate the uninstrumented code path. */
ipa_tm_scan_calls_transaction (d, &tm_callees);
/* Put it in the worklist so we can scan the function
later (ipa_tm_scan_irr_function) and mark the
irrevocable blocks. */
maybe_push_queue (node, &irr_worklist, &d->in_worklist);
d->want_irr_scan_normal = true;
}
pop_cfun ();
}
/* For every local function on the callee list, scan as if we will be
creating a transactional clone, queueing all new functions we find
along the way. */
for (i = 0; i < tm_callees.length (); ++i)
{
node = tm_callees[i];
a = node->get_availability ();
d = get_cg_data (&node, true);
/* Put it in the worklist so we can scan the function later
(ipa_tm_scan_irr_function) and mark the irrevocable
blocks. */
maybe_push_queue (node, &irr_worklist, &d->in_worklist);
/* Some callees cannot be arbitrarily cloned. These will always be
irrevocable. Mark these now, so that we need not scan them. */
if (is_tm_irrevocable (node->decl))
ipa_tm_note_irrevocable (node, &irr_worklist);
else if (a <= AVAIL_NOT_AVAILABLE
&& !is_tm_safe_or_pure (node->decl))
ipa_tm_note_irrevocable (node, &irr_worklist);
else if (a >= AVAIL_INTERPOSABLE)
{
if (!tree_versionable_function_p (node->decl))
ipa_tm_note_irrevocable (node, &irr_worklist);
else if (!d->is_irrevocable)
{
/* If this is an alias, make sure its base is queued as well.
we need not scan the callees now, as the base will do. */
if (node->alias)
{
node = cgraph_node::get (node->thunk.alias);
d = get_cg_data (&node, true);
maybe_push_queue (node, &tm_callees, &d->in_callee_queue);
continue;
}
/* Add all nodes called by this function into
tm_callees as well. */
ipa_tm_scan_calls_clone (node, &tm_callees);
}
}
}
/* Iterate scans until no more work to be done. Prefer not to use
vec::pop because the worklist tends to follow a breadth-first
search of the callgraph, which should allow convergance with a
minimum number of scans. But we also don't want the worklist
array to grow without bound, so we shift the array up periodically. */
for (i = 0; i < irr_worklist.length (); ++i)
{
if (i > 256 && i == irr_worklist.length () / 8)
{
irr_worklist.block_remove (0, i);
i = 0;
}
node = irr_worklist[i];
d = get_cg_data (&node, true);
d->in_worklist = false;
if (d->want_irr_scan_normal)
{
d->want_irr_scan_normal = false;
ipa_tm_scan_irr_function (node, false);
}
if (d->in_callee_queue && ipa_tm_scan_irr_function (node, true))
ipa_tm_note_irrevocable (node, &irr_worklist);
}
/* For every function on the callee list, collect the tm_may_enter_irr
bit on the node. */
irr_worklist.truncate (0);
for (i = 0; i < tm_callees.length (); ++i)
{
node = tm_callees[i];
if (ipa_tm_mayenterirr_function (node))
{
d = get_cg_data (&node, true);
gcc_assert (d->in_worklist == false);
maybe_push_queue (node, &irr_worklist, &d->in_worklist);
}
}
/* Propagate the tm_may_enter_irr bit to callers until stable. */
for (i = 0; i < irr_worklist.length (); ++i)
{
struct cgraph_node *caller;
struct cgraph_edge *e;
struct ipa_ref *ref;
if (i > 256 && i == irr_worklist.length () / 8)
{
irr_worklist.block_remove (0, i);
i = 0;
}
node = irr_worklist[i];
d = get_cg_data (&node, true);
d->in_worklist = false;
node->local.tm_may_enter_irr = true;
/* Propagate back to normal callers. */
for (e = node->callers; e ; e = e->next_caller)
{
caller = e->caller;
if (!is_tm_safe_or_pure (caller->decl)
&& !caller->local.tm_may_enter_irr)
{
d = get_cg_data (&caller, true);
maybe_push_queue (caller, &irr_worklist, &d->in_worklist);
}
}
/* Propagate back to referring aliases as well. */
FOR_EACH_ALIAS (node, ref)
{
caller = dyn_cast<cgraph_node *> (ref->referring);
if (!caller->local.tm_may_enter_irr)
{
/* ?? Do not traverse aliases here. */
d = get_cg_data (&caller, false);
maybe_push_queue (caller, &irr_worklist, &d->in_worklist);
}
}
}
/* Now validate all tm_safe functions, and all atomic regions in
other functions. */
FOR_EACH_DEFINED_FUNCTION (node)
if (node->lowered
&& node->get_availability () >= AVAIL_INTERPOSABLE)
{
d = get_cg_data (&node, true);
if (is_tm_safe (node->decl))
ipa_tm_diagnose_tm_safe (node);
else if (d->all_tm_regions)
ipa_tm_diagnose_transaction (node, d->all_tm_regions);
}
/* Create clones. Do those that are not irrevocable and have a
positive call count. Do those publicly visible functions that
the user directed us to clone. */
for (i = 0; i < tm_callees.length (); ++i)
{
bool doit = false;
node = tm_callees[i];
if (node->cpp_implicit_alias)
continue;
a = node->get_availability ();
d = get_cg_data (&node, true);
if (a <= AVAIL_NOT_AVAILABLE)
doit = is_tm_callable (node->decl);
else if (a <= AVAIL_AVAILABLE && is_tm_callable (node->decl))
doit = true;
else if (!d->is_irrevocable
&& d->tm_callers_normal + d->tm_callers_clone > 0)
doit = true;
if (doit)
ipa_tm_create_version (node);
}
/* Redirect calls to the new clones, and insert irrevocable marks. */
for (i = 0; i < tm_callees.length (); ++i)
{
node = tm_callees[i];
if (node->analyzed)
{
d = get_cg_data (&node, true);
if (d->clone)
ipa_tm_transform_clone (node);
}
}
FOR_EACH_DEFINED_FUNCTION (node)
if (node->lowered
&& node->get_availability () >= AVAIL_INTERPOSABLE)
{
d = get_cg_data (&node, true);
if (d->all_tm_regions)
ipa_tm_transform_transaction (node);
}
/* Free and clear all data structures. */
tm_callees.release ();
irr_worklist.release ();
bitmap_obstack_release (&tm_obstack);
free_original_copy_tables ();
FOR_EACH_FUNCTION (node)
node->aux = NULL;
#ifdef ENABLE_CHECKING
cgraph_node::verify_cgraph_nodes ();
#endif
return 0;
}
namespace {
const pass_data pass_data_ipa_tm =
{
SIMPLE_IPA_PASS, /* type */
"tmipa", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_TRANS_MEM, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_ipa_tm : public simple_ipa_opt_pass
{
public:
pass_ipa_tm (gcc::context *ctxt)
: simple_ipa_opt_pass (pass_data_ipa_tm, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *) { return flag_tm; }
virtual unsigned int execute (function *) { return ipa_tm_execute (); }
}; // class pass_ipa_tm
} // anon namespace
simple_ipa_opt_pass *
make_pass_ipa_tm (gcc::context *ctxt)
{
return new pass_ipa_tm (ctxt);
}
#include "gt-trans-mem.h"
|