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
|
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Lower XX
XX XX
XX Preconditions: XX
XX XX
XX Postconditions (for the nodes currently handled): XX
XX - All operands requiring a register are explicit in the graph XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#ifndef LEGACY_BACKEND // This file is ONLY used for the RyuJIT backend that uses the linear scan register allocator
#include "lower.h"
//------------------------------------------------------------------------
// MakeSrcContained: Make "tree" a contained node
//
// Arguments:
// 'parentNode' is a non-leaf node that can contain its 'childNode'
// 'childNode' is an op that will now be contained by its parent.
//
// Notes:
// If 'childNode' it has any existing sources, they will now be sources for the parent.
//
void Lowering::MakeSrcContained(GenTreePtr parentNode, GenTreePtr childNode)
{
assert(!parentNode->OperIsLeaf());
int srcCount = childNode->gtLsraInfo.srcCount;
assert(srcCount >= 0);
m_lsra->clearOperandCounts(childNode);
assert(parentNode->gtLsraInfo.srcCount > 0);
parentNode->gtLsraInfo.srcCount += srcCount - 1;
}
//------------------------------------------------------------------------
// CheckImmedAndMakeContained: Check and make 'childNode' contained
// Arguments:
// 'parentNode' is any non-leaf node
// 'childNode' is an child op of 'parentNode'
// Return value:
// returns true if we are able to make childNode contained immediate
//
// Notes:
// Checks if the 'childNode' is a containable immediate
// and then makes it contained
//
bool Lowering::CheckImmedAndMakeContained(GenTree* parentNode, GenTree* childNode)
{
assert(!parentNode->OperIsLeaf());
// If childNode is a containable immediate
if (IsContainableImmed(parentNode, childNode))
{
// then make it contained within the parentNode
MakeSrcContained(parentNode, childNode);
return true;
}
return false;
}
//------------------------------------------------------------------------
// IsSafeToContainMem: Checks for conflicts between childNode and parentNode.
//
// Arguments:
// parentNode - a non-leaf binary node
// childNode - a memory op that is a child op of 'parentNode'
//
// Return value:
// returns true if it is safe to make childNode a contained memory op
//
// Notes:
// Checks for memory conflicts in the instructions between childNode and parentNode,
// and returns true iff childNode can be contained.
bool Lowering::IsSafeToContainMem(GenTree* parentNode, GenTree* childNode)
{
assert(parentNode->OperIsBinary());
assert(childNode->isMemoryOp());
// Check conflicts against nodes between 'childNode' and 'parentNode'
GenTree* node;
unsigned int childFlags = (childNode->gtFlags & GTF_ALL_EFFECT);
for (node = childNode->gtNext;
(node != parentNode) && (node != nullptr);
node = node->gtNext)
{
if ((childFlags != 0) && node->IsCall())
{
bool isPureHelper = (node->gtCall.gtCallType == CT_HELPER) && comp->s_helperCallProperties.IsPure(comp->eeGetHelperNum(node->gtCall.gtCallMethHnd));
if (!isPureHelper && ((node->gtFlags & childFlags & GTF_ALL_EFFECT) != 0))
{
return false;
}
}
else if (node->OperIsStore() && comp->fgNodesMayInterfere(node, childNode))
{
return false;
}
}
if (node != parentNode)
{
assert(!"Ran off end of stmt\n");
return false;
}
return true;
}
//------------------------------------------------------------------------
Compiler::fgWalkResult Lowering::DecompNodeHelper(GenTreePtr* pTree, Compiler::fgWalkData* data)
{
Lowering* lower = (Lowering*)data->pCallbackData;
lower->DecomposeNode(pTree, data);
return Compiler::WALK_CONTINUE;
}
Compiler::fgWalkResult Lowering::LowerNodeHelper(GenTreePtr* pTree, Compiler::fgWalkData* data)
{
Lowering* lower = (Lowering*)data->pCallbackData;
lower->LowerNode(pTree, data);
return Compiler::WALK_CONTINUE;
}
//------------------------------------------------------------------------
// DecomposeNode: Decompose long-type trees into lower & upper halves.
//
// Arguments:
// *pTree - A node that may or may not require decomposition.
// data - The tree-walk data that provides the context.
//
// Return Value:
// None.
//
// Notes: The rationale behind this is to avoid adding code complexity
// downstream caused by the introduction of handling longs as special cases, especially in
// LSRA.
// This function is called just prior to the more general purpose lowering.
//
void Lowering::DecomposeNode(GenTreePtr* pTree, Compiler::fgWalkData* data)
{
#if !defined(_TARGET_64BIT_)
GenTree* tree = *(pTree);
// Handle the case where we are implicitly using the lower half of a long lclVar.
if ((tree->TypeGet() == TYP_INT) && tree->OperIsLocal())
{
LclVarDsc * varDsc = comp->lvaTable + tree->AsLclVarCommon()->gtLclNum;
if (varTypeIsLong(varDsc) && varDsc->lvPromoted)
{
#ifdef DEBUG
if (comp->verbose)
{
printf("Changing implicit reference to lo half of long lclVar to an explicit reference of its promoted half:\n");
comp->gtDispTree(tree);
}
#endif
comp->lvaDecRefCnts(tree);
unsigned loVarNum = varDsc->lvFieldLclStart;
tree->AsLclVarCommon()->SetLclNum(loVarNum);
comp->lvaIncRefCnts(tree);
return;
}
}
if (tree->TypeGet() != TYP_LONG)
{
return;
}
#ifdef DEBUG
if (comp->verbose)
{
printf("Decomposing TYP_LONG tree. BEFORE:\n");
comp->gtDispTree(tree);
}
#endif
// The most common pattern is that we will create a loResult and a hiResult, where
// the loResult reuses some or all of the existing tree, and the hiResult is new.
// For these, we will set loResult and hiResult in the switch case below, ensuring
// that loResult is correctly linked into the statement, and hiResult is not, but
// is internally sequenced appropriately (if it is not a single node).
// Common code after the switch will link in hiResult and create a GT_LONG node.
GenTree* newTree = nullptr;
GenTree* loResult = nullptr;
GenTree* hiResult = nullptr;
GenTreeStmt* curStmt = comp->compCurStmt->AsStmt();
genTreeOps oper = tree->OperGet();
switch(oper)
{
case GT_PHI:
case GT_PHI_ARG:
break;
case GT_LCL_VAR:
{
loResult = tree;
unsigned varNum = tree->AsLclVarCommon()->gtLclNum;
LclVarDsc * varDsc = comp->lvaTable + varNum;
comp->lvaDecRefCnts(tree);
hiResult = comp->gtNewLclLNode(varNum, TYP_INT);
if (varDsc->lvPromoted)
{
assert(varDsc->lvFieldCnt == 2);
unsigned loVarNum = varDsc->lvFieldLclStart;
unsigned hiVarNum = loVarNum + 1;
tree->AsLclVarCommon()->SetLclNum(loVarNum);
hiResult->AsLclVarCommon()->SetLclNum(hiVarNum);
}
else
{
noway_assert(varDsc->lvLRACandidate == false);
tree->SetOper(GT_LCL_FLD);
tree->AsLclFld()->gtLclOffs = 0;
tree->AsLclFld()->gtFieldSeq = FieldSeqStore::NotAField();
hiResult->SetOper(GT_LCL_FLD);
hiResult->AsLclFld()->gtLclOffs = 4;
hiResult->AsLclFld()->gtFieldSeq = FieldSeqStore::NotAField();
}
tree->gtType = TYP_INT;
comp->lvaIncRefCnts(loResult);
comp->lvaIncRefCnts(hiResult);
break;
}
case GT_LCL_FLD:
loResult = tree;
loResult->gtType = TYP_INT;
hiResult = comp->gtNewLclFldNode(loResult->AsLclFld()->gtLclNum,
TYP_INT,
loResult->AsLclFld()->gtLclOffs + 4);
break;
case GT_STORE_LCL_VAR:
{
GenTree* nextTree = tree->gtNext;
GenTree* rhs = tree->gtGetOp1();
if (rhs->OperGet() == GT_PHI)
{
break;
}
noway_assert(rhs->OperGet() == GT_LONG);
unsigned varNum = tree->AsLclVarCommon()->gtLclNum;
LclVarDsc * varDsc = comp->lvaTable + varNum;
comp->lvaDecRefCnts(tree);
GenTree* loRhs = rhs->gtGetOp1();
GenTree* hiRhs = rhs->gtGetOp2();
GenTree* hiStore = comp->gtNewLclLNode(varNum, TYP_INT);
if (varDsc->lvPromoted)
{
assert(varDsc->lvFieldCnt == 2);
unsigned loVarNum = varDsc->lvFieldLclStart;
unsigned hiVarNum = loVarNum + 1;
tree->AsLclVarCommon()->SetLclNum(loVarNum);
hiStore->SetOper(GT_STORE_LCL_VAR);
}
else
{
noway_assert(varDsc->lvLRACandidate == false);
tree->SetOper(GT_STORE_LCL_FLD);
tree->AsLclFld()->gtLclOffs = 0;
tree->AsLclFld()->gtFieldSeq = FieldSeqStore::NotAField();
hiStore->SetOper(GT_STORE_LCL_FLD);
hiStore->AsLclFld()->gtLclOffs = 4;
hiStore->AsLclFld()->gtFieldSeq = FieldSeqStore::NotAField();
}
tree->gtOp.gtOp1 = loRhs;
tree->gtType = TYP_INT;
loRhs->gtNext = tree;
tree->gtPrev = loRhs;
hiStore->gtOp.gtOp1 = hiRhs;
hiStore->CopyCosts(tree);
hiStore->gtFlags |= GTF_VAR_DEF;
comp->lvaIncRefCnts(tree);
comp->lvaIncRefCnts(hiStore);
hiRhs->gtPrev = tree;
hiRhs->gtNext = hiStore;
hiStore->gtPrev = hiRhs;
hiStore->gtNext = nextTree;
if (nextTree != nullptr)
{
nextTree->gtPrev = hiStore;
}
nextTree = hiRhs;
GenTreeStmt* stmt;
if (comp->compCurStmt->gtStmt.gtStmtExpr == tree)
{
tree->gtNext = nullptr;
hiRhs->gtPrev = nullptr;
stmt = comp->fgNewStmtFromTree(hiStore);
comp->fgInsertStmtAfter(comp->compCurBB, comp->compCurStmt, stmt);
}
else
{
stmt = comp->fgMakeEmbeddedStmt(comp->compCurBB, hiStore, comp->compCurStmt);
}
stmt->gtStmtILoffsx = comp->compCurStmt->gtStmt.gtStmtILoffsx;
#ifdef DEBUG
stmt->gtStmtLastILoffs = comp->compCurStmt->gtStmt.gtStmtLastILoffs;
#endif // DEBUG
}
break;
case GT_CAST:
{
assert(tree->gtPrev == tree->gtGetOp1());
NYI_IF(tree->gtOverflow(), "TYP_LONG cast with overflow");
switch (tree->AsCast()->CastFromType())
{
case TYP_INT:
loResult = tree->gtGetOp1();
hiResult = new (comp, GT_CNS_INT) GenTreeIntCon(TYP_INT, 0);
comp->fgSnipNode(curStmt, tree);
break;
default:
NYI("Unimplemented type for Lowering of cast to TYP_LONG");
break;
}
break;
}
case GT_CNS_LNG:
{
INT32 hiVal = tree->AsLngCon()->HiVal();
loResult = tree;
loResult->ChangeOperConst(GT_CNS_INT);
loResult->gtType = TYP_INT;
hiResult = new (comp, GT_CNS_INT) GenTreeIntCon(TYP_INT, hiVal);
hiResult->CopyCosts(tree);
}
break;
case GT_CALL:
NYI("Call with TYP_LONG return value");
break;
case GT_RETURN:
assert(tree->gtOp.gtOp1->OperGet() == GT_LONG);
break;
case GT_STOREIND:
assert(tree->gtOp.gtOp2->OperGet() == GT_LONG);
NYI("StoreInd of of TYP_LONG");
break;
case GT_STORE_LCL_FLD:
assert(tree->gtOp.gtOp1->OperGet() == GT_LONG);
NYI("st.lclFld of of TYP_LONG");
break;
case GT_IND:
NYI("GT_IND of TYP_LONG");
break;
case GT_NOT:
{
GenTree* op1 = tree->gtGetOp1();
noway_assert(op1->OperGet() == GT_LONG);
GenTree* loOp1 = op1->gtGetOp1();
GenTree* hiOp1 = op1->gtGetOp2();
comp->fgSnipNode(curStmt, op1);
loResult = tree;
loResult->gtOp.gtOp1 = loOp1;
loOp1->gtNext = loResult;
loResult->gtPrev = loOp1;
hiResult = new (comp, oper) GenTreeOp(oper, TYP_INT, hiOp1, nullptr);
hiOp1->gtNext = hiResult;
hiResult->gtPrev = hiOp1;
}
break;
case GT_NEG:
NYI("GT_NEG of TYP_LONG");
break;
// Binary operators whose long result is simply the concatenation of the int result
// on its constituent halves:
case GT_OR:
case GT_XOR:
case GT_AND:
NYI("Logical binary operators on TYP_LONG");
break;
// Binary operators whose upper and lower halves require different computation.
case GT_ADD:
case GT_SUB:
case GT_MUL:
case GT_DIV:
case GT_MOD:
case GT_UDIV:
case GT_UMOD:
case GT_LSH:
case GT_RSH:
case GT_RSZ:
case GT_MULHI:
NYI("Arithmetic binary operators on TYP_LONG");
break;
case GT_LOCKADD:
case GT_XADD:
case GT_XCHG:
case GT_CMPXCHG:
NYI("Interlocked operations on TYP_LONG");
break;
default:
{
JITDUMP("Illegal TYP_LONG node %s in Lowering.", GenTree::NodeName(tree->OperGet()));
noway_assert(!"Illegal TYP_LONG node in Lowering.");
break;
}
}
if (loResult != nullptr)
{
noway_assert(hiResult != nullptr);
comp->fgInsertTreeInListAfter(hiResult, loResult, curStmt);
hiResult->CopyCosts(tree);
newTree = new (comp, GT_LONG) GenTreeOp(GT_LONG, TYP_LONG, loResult, hiResult);
SimpleLinkNodeAfter(hiResult, newTree);
}
if (newTree != nullptr)
{
comp->fgFixupIfCallArg(data->parentStack, tree, newTree);
newTree->CopyCosts(tree);
*pTree = newTree;
}
#ifdef DEBUG
if (comp->verbose)
{
printf(" AFTER:\n");
comp->gtDispTree(*pTree);
}
#endif
#endif // //_TARGET_64BIT_
}
/** Creates an assignment of an existing tree to a new temporary local variable
* and the specified reference count for the new variable.
*/
GenTreePtr Lowering::CreateLocalTempAsg(GenTreePtr rhs,
unsigned refCount,
GenTreePtr* ppLclVar) //out legacy arg
{
unsigned lclNum = comp->lvaGrabTemp(true DEBUGARG("Lowering is creating a new local variable"));
comp->lvaSortAgain = true;
comp->lvaTable[lclNum].lvType = rhs->TypeGet();
// Make sure we don't lose precision when downgrading to short
noway_assert(FitsIn<short>(refCount));
comp->lvaTable[lclNum].lvRefCnt = (short)(refCount);
JITDUMP("Lowering has requested a new temporary local variable: V%02u with refCount %u \n", lclNum, refCount);
GenTreeLclVar* store = new(comp, GT_STORE_LCL_VAR) GenTreeLclVar(GT_STORE_LCL_VAR, rhs->TypeGet(), lclNum, BAD_IL_OFFSET);
store->gtOp1 = rhs;
store->gtFlags = (rhs->gtFlags & GTF_COMMON_MASK);
store->gtFlags |= GTF_VAR_DEF;
return store;
}
// This is the main entry point for Lowering.
// In addition to that, LowerNode is also responsible for initializing the
// treeNodeMap data structure consumed by LSRA. This map is a 1:1 mapping between
// expression trees and TreeNodeInfo structs. Currently, Lowering initializes
// treeNodeMap with new instances of TreeNodeInfo for each tree and also annotates them
// with the register requirements needed for each tree.
// We receive a double pointer to a tree in order to be able, if needed, to entirely
// replace the tree by creating a new one and updating the underying pointer so this
// enables in-place tree manipulation.
// The current design is made in such a way we perform a helper call for each different
// type of tree. Currently, the only supported node is GT_IND and for that we call the
// LowerInd private method. The build system picks up the appropiate Lower.cpp (either
// LowerArm/LowerX86/LowerAMD64) that has the machine dependent logic to lower each node.
// TODO-Throughput: Modify post-order traversal to propagate parent info OR
// implement child iterator directly on GenTree, so that we can
// lower in-place.
void Lowering::LowerNode(GenTreePtr* ppTree, Compiler::fgWalkData* data)
{
// First, lower any child nodes (done via post-order walk)
assert(ppTree);
assert(*ppTree);
switch ((*ppTree)->gtOper)
{
case GT_IND:
case GT_STOREIND:
LowerInd(ppTree);
break;
case GT_ADD:
LowerAdd(ppTree, data);
break;
case GT_SWITCH:
LowerSwitch(ppTree);
break;
case GT_CALL:
LowerCall(*ppTree);
break;
case GT_JMP:
LowerJmpMethod(*ppTree);
break;
case GT_RETURN:
LowerRet(*ppTree);
break;
case GT_CAST:
LowerCast(ppTree);
break;
case GT_ARR_ELEM:
{
GenTree* oldTree = *ppTree;
LowerArrElem(ppTree, data);
comp->fgFixupIfCallArg(data->parentStack, oldTree, *ppTree);
}
break;
#ifdef FEATURE_SIMD
case GT_SIMD:
if ((*ppTree)->TypeGet() == TYP_SIMD12)
{
// GT_SIMD node requiring to produce TYP_SIMD12 in fact
// produces a TYP_SIMD16 result
(*ppTree)->gtType = TYP_SIMD16;
}
break;
case GT_LCL_VAR:
case GT_STORE_LCL_VAR:
if ((*ppTree)->TypeGet() == TYP_SIMD12)
{
#ifdef _TARGET_64BIT_
// On 64-bit architectures, size of Vector3 local on stack will be
// rounded to TARGET_POINTER_SIZE and hence will be 16 bytes. Therefore,
// Vector3 locals on stack could be read/written as if they were TYP_SIMD16
(*ppTree)->gtType = TYP_SIMD16;
#else
NYI("Lowering of TYP_SIMD12 locals");
#endif // _TARGET_64BIT_
}
#endif //FEATURE_SIMD
default:
return;
}
}
/** -- Switch Lowering --
* The main idea of switch lowering is to keep transparency of the register requirements of this node
* downstream in LSRA. Given that the switch instruction is inherently a control statement which in the JIT
* is represented as a simple tree node, at the time we actually generate code for it we end up
* generating instructions that actually modify the flow of execution that imposes complicated
* register requirement and lifetimes.
*
* So, for the purpose of LSRA, we want to have a more detailed specification of what a switch node actually
* means and more importantly, which and when do we need a register for each instruction we want to issue
* to correctly allocate them downstream.
*
* For this purpose, this procedure performs switch lowering in two different ways:
*
* a) Represent the switch statement as a zero-index jump table construct. This means that for every destination
* of the switch, we will store this destination in an array of addresses and the code generator will issue
* a data section where this array will live and will emit code that based on the switch index, will indirect and
* jump to the destination specified in the jump table.
*
* For this transformation we introduce a new GT node called GT_SWITCH_TABLE that is a specialization of the switch node
* for jump table based switches.
* The overall structure of a GT_SWITCH_TABLE is:
*
* GT_SWITCH_TABLE
* |_________ localVar (a temporary local that holds the switch index)
* |_________ jumpTable (this is a special node that holds the address of the jump table array)
*
* Now, the way we morph a GT_SWITCH node into this lowered switch table node form is the following:
*
* Input: GT_SWITCH (inside a basic block whose Branch Type is BBJ_SWITCH)
* |_____ expr (an arbitrary complex GT_NODE that represents the switch index)
*
* This gets transformed into the following statements inside a BBJ_COND basic block (the target would be
* the default case of the switch in case the conditional is evaluated to true).
*
* GT_ASG
* |_____ tempLocal (a new temporary local variable used to store the switch index)
* |_____ expr (the index expression)
*
* GT_JTRUE
* |_____ GT_COND
* |_____ GT_GE
* |___ Int_Constant (This constant is the index of the default case
* that happens to be the highest index in the jump table).
* |___ tempLocal (The local variable were we stored the index expression).
*
* GT_SWITCH_TABLE
* |_____ tempLocal
* |_____ jumpTable (a new jump table node that now LSRA can allocate registers for explicitly
* and LinearCodeGen will be responsible to generate downstream).
*
* This way there are no implicit temporaries.
*
* b) For small-sized switches, we will actually morph them into a series of conditionals of the form
* if (case falls into the default){ goto jumpTable[size]; // last entry in the jump table is the default case }
* (For the default case conditional, we'll be constructing the exact same code as the jump table case one).
* else if (case == firstCase){ goto jumpTable[1]; }
* else if (case == secondCase) { goto jumptable[2]; } and so on.
*
* This transformation is of course made in JIT-IR, not downstream to CodeGen level, so this way we no longer require
* internal temporaries to maintain the index we're evaluating plus we're using existing code from LinearCodeGen
* to implement this instead of implement all the control flow constructs using InstrDscs and InstrGroups downstream.
*/
void Lowering::LowerSwitch(GenTreePtr* pTree)
{
unsigned jumpCnt;
unsigned targetCnt;
BasicBlock** jumpTab;
GenTreePtr tree = *pTree;
assert(tree->gtOper == GT_SWITCH);
// The first step is to build the default case conditional construct that is
// shared between both kinds of expansion of the switch node.
// To avoid confusion, we'll alias compCurBB to originalSwitchBB
// that represents the node we're morphing.
BasicBlock* originalSwitchBB = comp->compCurBB;
// jumpCnt is the number of elements in the jump table array.
// jumpTab is the actual pointer to the jump table array.
// targetCnt is the number of unique targets in the jump table array.
jumpCnt = originalSwitchBB->bbJumpSwt->bbsCount;
jumpTab = originalSwitchBB->bbJumpSwt->bbsDstTab;
targetCnt = originalSwitchBB->NumSucc(comp);
JITDUMP("Lowering switch BB%02u, %d cases\n", originalSwitchBB->bbNum, jumpCnt);
// Handle a degenerate case: if the switch has only a default case, just convert it
// to an unconditional branch. This should only happen in minopts or with debuggable
// code.
if (targetCnt == 1)
{
JITDUMP("Lowering switch BB%02u: single target; converting to BBJ_ALWAYS\n", originalSwitchBB->bbNum);
noway_assert(comp->opts.MinOpts() || comp->opts.compDbgCode);
if (originalSwitchBB->bbNext == jumpTab[0])
{
originalSwitchBB->bbJumpKind = BBJ_NONE;
originalSwitchBB->bbJumpDest = nullptr;
}
else
{
originalSwitchBB->bbJumpKind = BBJ_ALWAYS;
originalSwitchBB->bbJumpDest = jumpTab[0];
}
// Remove extra predecessor links if there was more than one case.
for (unsigned i = 1; i < jumpCnt; ++i)
{
(void) comp->fgRemoveRefPred(jumpTab[i], originalSwitchBB);
}
// We have to get rid of the GT_SWITCH node but a child might have side effects so just assign
// the result of the child subtree to a temp.
GenTree* store = CreateLocalTempAsg(tree->gtOp.gtOp1, 1);
tree->InsertAfterSelf(store, comp->compCurStmt->AsStmt());
Compiler::fgSnipNode(comp->compCurStmt->AsStmt(), tree);
*pTree = store;
return;
}
noway_assert(jumpCnt >= 2);
// Split the switch node to insert an assignment to a temporary variable.
// Note that 'tree' is the GT_SWITCH, and its op1 may be overwritten by SplitTree
//
GenTreeStmt* asgStmt = comp->fgInsertEmbeddedFormTemp(&(tree->gtOp.gtOp1));
// GT_SWITCH(indexExpression) is now two statements:
// 1. a statement containing 'asg' (for temp = indexExpression)
// 2. and a statement with GT_SWITCH(temp)
// The return value of fgInsertEmbeddedFormTemp is stmt 1
// The 'asg' can either be a GT_ASG or a GT_STORE_LCL_VAR
// 'tree' is still a GT_SWITCH but tree->gtOp.gtOp1 is modified to be 'temp'
// The asgStmt needs to pickup the IL offsets from the current statement
//
asgStmt->gtStmtILoffsx = comp->compCurStmt->gtStmt.gtStmtILoffsx;
#ifdef DEBUG
asgStmt->gtStmtLastILoffs = comp->compCurStmt->gtStmt.gtStmtLastILoffs;
#endif // DEBUG
assert(tree->gtOper == GT_SWITCH);
GenTreePtr temp = tree->gtOp.gtOp1;
assert(temp->gtOper == GT_LCL_VAR);
unsigned tempLclNum = temp->gtLclVarCommon.gtLclNum;
LclVarDsc * tempVarDsc = comp->lvaTable + tempLclNum;
var_types tempLclType = tempVarDsc->TypeGet();
BasicBlock* defaultBB = jumpTab[jumpCnt - 1];
BasicBlock* followingBB = originalSwitchBB->bbNext;
/* Is the number of cases right for a test and jump switch? */
const bool fFirstCaseFollows = (followingBB == jumpTab[0]);
const bool fDefaultFollows = (followingBB == defaultBB);
unsigned minSwitchTabJumpCnt = 2; // table is better than just 2 cmp/jcc
// This means really just a single cmp/jcc (aka a simple if/else)
if (fFirstCaseFollows || fDefaultFollows)
minSwitchTabJumpCnt++;
#if defined(_TARGET_ARM_)
// On ARM for small switch tables we will
// generate a sequence of compare and branch instructions
// because the code to load the base of the switch
// table is huge and hideous due to the relocation... :(
minSwitchTabJumpCnt += 2;
#elif defined(_TARGET_ARM64_) // _TARGET_ARM_
// In the case of ARM64 we'll stick to generate a sequence of
// compare and branch for now to get switch working and revisit
// to implement jump tables in the future.
//
// TODO-AMD64-NYI: Implement Jump Tables.
minSwitchTabJumpCnt = -1;
#endif // _TARGET_ARM64_
// Once we have the temporary variable, we construct the conditional branch for
// the default case. As stated above, this conditional is being shared between
// both GT_SWITCH lowering code paths.
// This condition is of the form: if (temp > jumpTableLength - 2){ goto jumpTable[jumpTableLength - 1]; }
GenTreePtr gtDefaultCaseCond = comp->gtNewOperNode(GT_GT, TYP_INT,
comp->gtNewLclvNode(tempLclNum, tempLclType),
comp->gtNewIconNode(jumpCnt - 2, TYP_INT));
//
// Make sure we perform an unsigned comparison, just in case the switch index in 'temp'
// is now less than zero 0 (that would also hit the default case).
gtDefaultCaseCond->gtFlags |= GTF_UNSIGNED;
/* Increment the lvRefCnt and lvRefCntWtd for temp */
tempVarDsc->incRefCnts(originalSwitchBB->getBBWeight(comp), comp);
GenTreePtr gtDefaultCaseJump = comp->gtNewOperNode(GT_JTRUE,
TYP_VOID,
gtDefaultCaseCond);
gtDefaultCaseJump->gtFlags = tree->gtFlags;
GenTreePtr condStmt = comp->fgNewStmtFromTree(gtDefaultCaseJump, originalSwitchBB, comp->compCurStmt->gtStmt.gtStmtILoffsx);
#ifdef DEBUG
condStmt->gtStmt.gtStmtLastILoffs = comp->compCurStmt->gtStmt.gtStmtLastILoffs;
#endif // DEBUG
comp->fgInsertStmtAfter(originalSwitchBB, comp->compCurStmt, condStmt);
BasicBlock* afterDefCondBlock = comp->fgSplitBlockAfterStatement(originalSwitchBB, condStmt);
// afterDefCondBlock is now the switch, and all the switch targets have it as a predecessor.
// originalSwitchBB is now a BBJ_NONE, and there is a predecessor edge in afterDefCondBlock
// representing the fall-through flow from originalSwitchBB.
assert(originalSwitchBB->bbJumpKind == BBJ_NONE);
assert(afterDefCondBlock->bbJumpKind == BBJ_SWITCH);
assert(afterDefCondBlock->bbJumpSwt->bbsHasDefault);
// Turn originalSwitchBB into a BBJ_COND.
originalSwitchBB->bbJumpKind = BBJ_COND;
originalSwitchBB->bbJumpDest = jumpTab[jumpCnt - 1];
// Fix the pred for the default case: the default block target still has originalSwitchBB
// as a predecessor, but the fgSplitBlockAfterStatement() moved all predecessors to point
// to afterDefCondBlock.
flowList* oldEdge = comp->fgRemoveRefPred(jumpTab[jumpCnt - 1], afterDefCondBlock);
comp->fgAddRefPred(jumpTab[jumpCnt - 1], originalSwitchBB, oldEdge);
// If we originally had 2 unique successors, check to see whether there is a unique
// non-default case, in which case we can eliminate the switch altogether.
// Note that the single unique successor case is handled above.
BasicBlock* uniqueSucc = nullptr;
if (targetCnt == 2)
{
uniqueSucc = jumpTab[0];
noway_assert(jumpCnt >= 2);
for (unsigned i = 1; i < jumpCnt - 1; i++)
{
if (jumpTab[i] != uniqueSucc)
{
uniqueSucc = nullptr;
break;
}
}
}
if (uniqueSucc != nullptr)
{
// If the unique successor immediately follows this block, we have nothing to do -
// it will simply fall-through after we remove the switch, below.
// Otherwise, make this a BBJ_ALWAYS.
// Now, fixup the predecessor links to uniqueSucc. In the original jumpTab:
// jumpTab[i-1] was the default target, which we handled above,
// jumpTab[0] is the first target, and we'll leave that predecessor link.
// Remove any additional predecessor links to uniqueSucc.
for (unsigned i = 1; i < jumpCnt - 1; ++i)
{
assert(jumpTab[i] == uniqueSucc);
(void) comp->fgRemoveRefPred(uniqueSucc, afterDefCondBlock);
}
if (afterDefCondBlock->bbNext == uniqueSucc)
{
afterDefCondBlock->bbJumpKind = BBJ_NONE;
afterDefCondBlock->bbJumpDest = nullptr;
}
else
{
afterDefCondBlock->bbJumpKind = BBJ_ALWAYS;
afterDefCondBlock->bbJumpDest = uniqueSucc;
}
}
// If the number of possible destinations is small enough, we proceed to expand the switch
// into a series of conditional branches, otherwise we follow the jump table based switch
// transformation.
else if (jumpCnt < minSwitchTabJumpCnt)
{
// Lower the switch into a series of compare and branch IR trees.
//
// In this case we will morph the tree in the following way:
// 1. Generate a JTRUE statement to evaluate the default case. (This happens above.)
// 2. Start splitting the switch basic block into subsequent basic blocks, each of which will contain
// a statement that is responsible for performing a comparison of the table index and conditional
// branch if equal.
JITDUMP("Lowering switch BB%02u: using compare/branch expansion\n", originalSwitchBB->bbNum);
// We'll use 'afterDefCondBlock' for the first conditional. After that, we'll add new
// blocks. If we end up not needing it at all (say, if all the non-default cases just fall through),
// we'll delete it.
bool fUsedAfterDefCondBlock = false;
BasicBlock* currentBlock = afterDefCondBlock;
// Walk to entries 0 to jumpCnt - 1. If a case target follows, ignore it and let it fall through.
// If no case target follows, the last one doesn't need to be a compare/branch: it can be an
// unconditional branch.
bool fAnyTargetFollows = false;
for (unsigned i = 0; i < jumpCnt - 1; ++i)
{
assert(currentBlock != nullptr);
// Remove the switch from the predecessor list of this case target's block.
// We'll add the proper new predecessor edge later.
flowList* oldEdge = comp->fgRemoveRefPred(jumpTab[i], afterDefCondBlock);
if (jumpTab[i] == followingBB)
{
// This case label follows the switch; let it fall through.
fAnyTargetFollows = true;
continue;
}
// We need a block to put in the new compare and/or branch.
// If we haven't used the afterDefCondBlock yet, then use that.
if (fUsedAfterDefCondBlock)
{
BasicBlock* newBlock = comp->fgNewBBafter(BBJ_NONE, currentBlock, true);
comp->fgAddRefPred(newBlock, currentBlock); // The fall-through predecessor.
currentBlock = newBlock;
}
else
{
assert(currentBlock == afterDefCondBlock);
fUsedAfterDefCondBlock = true;
}
// We're going to have a branch, either a conditional or unconditional,
// to the target. Set the target.
currentBlock->bbJumpDest = jumpTab[i];
// Wire up the predecessor list for the "branch" case.
comp->fgAddRefPred(jumpTab[i], currentBlock, oldEdge);
if (!fAnyTargetFollows && (i == jumpCnt - 2))
{
// We're processing the last one, and there is no fall through from any case
// to the following block, so we can use an unconditional branch to the final
// case: there is no need to compare against the case index, since it's
// guaranteed to be taken (since the default case was handled first, above).
currentBlock->bbJumpKind = BBJ_ALWAYS;
}
else
{
// Otherwise, it's a conditional branch. Set the branch kind, then add the
// condition statement.
currentBlock->bbJumpKind = BBJ_COND;
// Now, build the conditional statement for the current case that is
// being evaluated:
// GT_JTRUE
// |__ GT_COND
// |____GT_EQ
// |____ (switchIndex) (The temp variable)
// |____ (ICon) (The actual case constant)
GenTreePtr gtCaseCond = comp->gtNewOperNode(GT_EQ, TYP_INT,
comp->gtNewLclvNode(tempLclNum, tempLclType),
comp->gtNewIconNode(i, TYP_INT));
/* Increment the lvRefCnt and lvRefCntWtd for temp */
tempVarDsc->incRefCnts(originalSwitchBB->getBBWeight(comp), comp);
GenTreePtr gtCaseBranch = comp->gtNewOperNode(GT_JTRUE, TYP_VOID, gtCaseCond);
GenTreePtr gtCaseStmt = comp->fgNewStmtFromTree(gtCaseBranch, currentBlock);
comp->fgInsertStmtAtEnd(currentBlock, gtCaseStmt);
}
}
if (fAnyTargetFollows)
{
// There is a fall-through to the following block. In the loop
// above, we deleted all the predecessor edges from the switch.
// In this case, we need to add one back.
comp->fgAddRefPred(currentBlock->bbNext, currentBlock);
}
if (!fUsedAfterDefCondBlock)
{
// All the cases were fall-through! We don't need this block.
// Convert it from BBJ_SWITCH to BBJ_NONE and unset the BBF_DONT_REMOVE flag
// so fgRemoveBlock() doesn't complain.
JITDUMP("Lowering switch BB%02u: all switch cases were fall-through\n", originalSwitchBB->bbNum);
assert(currentBlock == afterDefCondBlock);
assert(currentBlock->bbJumpKind == BBJ_SWITCH);
currentBlock->bbJumpKind = BBJ_NONE;
currentBlock->bbFlags &= ~BBF_DONT_REMOVE;
comp->fgRemoveBlock(currentBlock, /* unreachable */ false); // It's an empty block.
}
}
else
{
// Lower the switch into an indirect branch using a jump table:
//
// 1. Create the constant for the default case
// 2. Generate a GT_GE condition to compare to the default case
// 3. Generate a GT_JTRUE to jump.
// 4. Load the jump table address into a local (presumably the just
// created constant for GT_SWITCH).
// 5. Create a new node for the lowered switch, this will both generate
// the branch table and also will be responsible for the indirect
// branch.
JITDUMP("Lowering switch BB%02u: using jump table expansion\n", originalSwitchBB->bbNum);
GenTreePtr gtTableSwitch = comp->gtNewOperNode(GT_SWITCH_TABLE,
TYP_VOID,
comp->gtNewLclvNode(tempLclNum, tempLclType),
comp->gtNewJmpTableNode());
/* Increment the lvRefCnt and lvRefCntWtd for temp */
tempVarDsc->incRefCnts(originalSwitchBB->getBBWeight(comp), comp);
// this block no longer branches to the default block
afterDefCondBlock->bbJumpSwt->removeDefault();
comp->fgInvalidateSwitchDescMapEntry(afterDefCondBlock);
GenTreeStmt* stmt = comp->fgNewStmtFromTree(gtTableSwitch);
comp->fgInsertStmtAtEnd(afterDefCondBlock, stmt);
}
// Get rid of the original GT_SWITCH.
comp->fgRemoveStmt(originalSwitchBB, comp->compCurStmt, false);
// Set compCurStmt. If asgStmt is top-level, we need to set it to that, so that any of
// its embedded statements are traversed. Otherwise, set it to condStmt, which will
// contain the embedded asgStmt.
if (asgStmt->gtStmtIsTopLevel())
{
comp->compCurStmt = asgStmt;
}
else
{
#ifdef DEBUG
GenTree* nextStmt = condStmt->gtNext;
while (nextStmt != nullptr && nextStmt != asgStmt)
{
nextStmt = nextStmt->gtNext;
}
assert(nextStmt == asgStmt);
#endif // DEBUG
comp->compCurStmt = condStmt;
}
}
// splice in a unary op, between the child and parent
// resulting in parent->newNode->child
void Lowering::SpliceInUnary(GenTreePtr parent, GenTreePtr* ppChild, GenTreePtr newNode)
{
GenTreePtr oldChild = *ppChild;
// Replace tree in the parent node
*ppChild = newNode;
newNode->gtOp.gtOp1 = oldChild;
oldChild->InsertAfterSelf(newNode);
}
GenTreePtr Lowering::NewPutArg(GenTreeCall* call, GenTreePtr arg, fgArgTabEntryPtr fp, var_types type)
{
GenTreePtr putArg;
bool updateArgTable = true;
#if !defined(_TARGET_64BIT_)
if (varTypeIsLong(type))
{
// For TYP_LONG, we leave the GT_LONG as the arg, and put the putArg below it.
// Therefore, we don't update the arg table entry.
updateArgTable = false;
type = TYP_INT;
}
#endif // !defined(_TARGET_64BIT_)
if (fp->regNum != REG_STK)
{
#ifdef FEATURE_SIMD
// We can have SIMD types that are handled as TYP_DOUBLE, but which need to be
// passed in integer registers. We need the putArg node to be of the int type.
if (type == TYP_DOUBLE && genIsValidIntReg(fp->regNum))
{
type = TYP_LONG;
}
#endif //FEATURE_SIMD
putArg = comp->gtNewOperNode(GT_PUTARG_REG, type, arg);
}
else
{
// Mark this one as tail call arg if it is a fast tail call.
// This provides the info to put this argument in in-coming arg area slot
// instead of in out-going arg area slot.
#if FEATURE_FASTTAILCALL
putArg = new (comp, GT_PUTARG_STK) GenTreePutArgStk(GT_PUTARG_STK, type, arg, fp->slotNum, call->IsFastTailCall() DEBUG_ARG(call));
#else
putArg = new (comp, GT_PUTARG_STK) GenTreePutArgStk(GT_PUTARG_STK, type, arg, fp->slotNum DEBUG_ARG(call));
#endif
}
putArg->CopyCosts(arg);
if (arg->InReg())
putArg->SetInReg();
JITDUMP("new node is : ");
DISPNODE(putArg);
JITDUMP("\n");
if (arg->gtFlags & GTF_LATE_ARG)
{
putArg->gtFlags |= GTF_LATE_ARG;
}
else if (updateArgTable)
{
fp->node = putArg;
}
return putArg;
}
// lower one arg of a call
// this currently entails splicing in a "putarg" node in between the arg evaluation and call
// These are the point at which the source is consumed and the values transition from control
// of the register allocator to the calling convention.
void Lowering::LowerArg(GenTreeCall* call, GenTreePtr* ppArg)
{
GenTreePtr arg = *ppArg;
JITDUMP("lowering arg : ");
DISPNODE(arg);
// No assignments should remain by Lowering.
assert(!arg->OperIsAssignment());
// assignments/stores at this level are not really placing an arg
// they are setting up temporary locals that will later be placed into
// outgoing regs or stack
if (!arg->OperIsAssignment() &&
!arg->OperIsStore() &&
!arg->IsArgPlaceHolderNode() &&
!arg->IsNothingNode() &&
!arg->OperIsCopyBlkOp()) // these are de facto placeholders (apparently)
{
fgArgTabEntryPtr fp = comp->gtArgEntryByNode(call, arg);
assert(fp->node == arg);
bool isReg = (fp->regNum != REG_STK);
var_types type = arg->TypeGet();
if (varTypeIsSmall(type))
{
// Normalize 'type', it represents the item that we will be storing in the Outgoing Args
type = TYP_INT;
}
GenTreePtr putArg;
// if we hit this we are probably double-lowering
assert(arg->gtOper != GT_PUTARG_REG && arg->gtOper != GT_PUTARG_STK);
#if !defined(_TARGET_64BIT_)
if (varTypeIsLong(type))
{
if (isReg)
{
NYI("Lowering of long register argument");
}
// For longs, we will create two PUTARG_STKs below the GT_LONG.
// This is because the lo/hi values will be marked localDefUse, and we need to ensure that
// they are pushed onto the stack as soon as they are created.
// We also need to reverse the order, since the hi argument needs to be pushed first.
noway_assert(arg->OperGet() == GT_LONG);
GenTreePtr argLo = arg->gtGetOp1();
GenTreePtr argHi = arg->gtGetOp2();
GenTreePtr putArgLo = NewPutArg(call, argLo, fp, type);
GenTreePtr putArgHi = NewPutArg(call, argHi, fp, type);
arg->gtOp.gtOp1 = putArgLo;
arg->gtOp.gtOp2 = putArgHi;
// Now, reorder the arguments and insert the putArg in the right place.
GenTreePtr argLoFirst = comp->fgGetFirstNode(argLo);
GenTreePtr argHiFirst = comp->fgGetFirstNode(argHi);
GenTreePtr argLoPrev = argLoFirst->gtPrev;
noway_assert(argHiFirst->gtPrev == argLo);
noway_assert(arg->gtPrev == argHi);
argHiFirst->gtPrev = argLoPrev;
if (argLoPrev != nullptr)
{
argLoPrev->gtNext = argHiFirst;
}
else
{
assert(comp->compCurStmt->gtStmt.gtStmtList == argLoFirst);
comp->compCurStmt->gtStmt.gtStmtList = argHiFirst;
}
argHi->gtNext = putArgHi;
putArgHi->gtPrev = argHi;
putArgHi->gtNext = argLoFirst;
argLoFirst->gtPrev = putArgHi;
argLo->gtNext = putArgLo;
putArgLo->gtPrev = argLo;
putArgLo->gtNext = arg;
arg->gtPrev = putArgLo;
assert((arg->gtFlags & GTF_REVERSE_OPS) == 0);
arg->gtFlags |= GTF_REVERSE_OPS;
}
else
#endif // !defined(_TARGET_64BIT_)
{
putArg = NewPutArg(call, arg, fp, type);
SpliceInUnary(call, ppArg, putArg);
}
}
}
// do lowering steps for each arg of a call
void Lowering::LowerArgsForCall(GenTreeCall* call)
{
JITDUMP("\n");
JITDUMP("objp:\n======\n");
if (call->gtCallObjp)
{
LowerArg(call, &call->gtCallObjp);
}
GenTreeArgList* args = call->gtCallArgs;
JITDUMP("\nargs:\n======\n");
for (; args; args = args->Rest())
{
LowerArg(call, &args->Current());
}
JITDUMP("\nlate:\n======\n");
for (args = call->gtCallLateArgs; args; args = args->Rest())
{
LowerArg(call, &args->Current());
}
JITDUMP("\nafter:\n=====\n");
DISPTREE(call);
}
// helper that create a node representing a relocatable physical address computation
// (optionally specifying the register to place it in)
GenTree* Lowering::AddrGen(ssize_t addr, regNumber reg)
{
//this should end up in codegen as : instGen_Set_Reg_To_Imm(EA_HANDLE_CNS_RELOC, reg, addr)
GenTree* result = comp->gtNewIconHandleNode(addr, GTF_ICON_FTN_ADDR);
result->gtRegNum = reg;
return result;
}
// variant that takes a void*
GenTree* Lowering::AddrGen(void* addr, regNumber reg)
{
return AddrGen((ssize_t)addr, reg);
}
// do some common operations on trees before they are inserted as top level statements
GenTreeStmt* Lowering::LowerMorphAndSeqTree(GenTree* tree)
{
tree = comp->fgMorphTree(tree);
GenTreeStmt* stmt = comp->fgNewStmtFromTree(tree);
return stmt;
}
// do lowering steps for a call
// this includes:
// - adding the placement nodes (either stack or register variety) for arguments
// - lowering the expression that calculates the target address
// - adding nodes for other operations that occur after the call sequence starts and before
// control transfer occurs (profiling and tail call helpers, pinvoke incantations)
//
void Lowering::LowerCall(GenTree* node)
{
GenTreeCall* call = node->AsCall();
GenTreeStmt* callStmt = comp->compCurStmt->AsStmt();
assert(comp->fgTreeIsInStmt(call, callStmt));
JITDUMP("lowering call:\n");
DISPTREE(call);
JITDUMP("\n");
LowerArgsForCall(call);
// RyuJIT arm is not set up for lowered call control
#ifndef _TARGET_ARM_
// note that everything generated from this point on runs AFTER the outgoing args are placed
GenTree* result = nullptr;
// for x86, this is where we record ESP for checking later to make sure stack is balanced
// Check for Delegate.Invoke(). If so, we inline it. We get the
// target-object and target-function from the delegate-object, and do
// an indirect call.
if (call->IsDelegateInvoke())
{
result = LowerDelegateInvoke(call);
}
else
{
// Virtual and interface calls
switch (call->gtFlags & GTF_CALL_VIRT_KIND_MASK)
{
case GTF_CALL_VIRT_STUB:
result = LowerVirtualStubCall(call);
break;
case GTF_CALL_VIRT_VTABLE:
// stub dispatching is off or this is not a virtual call (could be a tailcall)
result = LowerVirtualVtableCall(call);
break;
case GTF_CALL_NONVIRT:
{
#if INLINE_PINVOKE
if (call->IsUnmanaged())
{
result = LowerNonvirtPinvokeCall(call);
break;
}
#endif
#if defined(_TARGET_AMD64_) && defined(FEATURE_READYTORUN_COMPILER)
if (call->gtEntryPoint.addr != nullptr)
{
break;
}
#endif
if (call->gtCallType == CT_INDIRECT)
{
result = LowerIndirectNonvirtCall(call);
break;
}
else
{
result = LowerDirectCall(call);
break;
}
}
break;
default:
noway_assert(!"strange call type");
break;
}
}
#ifdef DEBUG
comp->fgDebugCheckNodeLinks(comp->compCurBB, comp->compCurStmt);
#endif
if (result)
{
// The controlExpr is newly constructed, so we can use tree sequencing
comp->gtSetEvalOrder(result);
comp->fgSetTreeSeq(result, nullptr);
JITDUMP("results of lowering call:\n");
DISPTREE(result);
}
if (call->IsTailCallViaHelper())
{
// Either controlExpr or gtCallAddr must contain real call target.
if (result == nullptr)
{
assert(call->gtCallAddr != nullptr);
result = call->gtCallAddr;
}
result = LowerTailCallViaHelper(call, result);
// We got a new call target constructed, so resequence it.
comp->gtSetEvalOrder(result);
comp->fgSetTreeSeq(result, nullptr);
JITDUMP("results of lowering tail call via helper:\n");
DISPTREE(result);
}
else if (call->IsFastTailCall())
{
LowerFastTailCall(call);
}
if (result)
{
GenTree* insertionPoint = call;
if (!call->IsTailCallViaHelper())
{
// The controlExpr should go before the gtCallCookie and the gtCallAddr, if they exist
if (call->gtCallType == CT_INDIRECT)
{
if (call->gtCallCookie != nullptr)
{
insertionPoint = comp->fgGetFirstNode(call->gtCallCookie);
}
else if (call->gtCallAddr != nullptr)
{
insertionPoint = comp->fgGetFirstNode(call->gtCallAddr);
}
}
}
comp->fgInsertTreeInListBefore(result, insertionPoint, callStmt);
call->gtControlExpr = result;
}
#endif //!_TARGET_ARM_
#ifdef DEBUG
comp->fgDebugCheckNodeLinks(comp->compCurBB, callStmt);
#endif
if (comp->opts.IsJit64Compat())
{
CheckVSQuirkStackPaddingNeeded(call);
}
}
// Though the below described issue gets fixed in intellitrace dll of VS2015 (a.k.a Dev14),
// we still need this quirk for desktop so that older version of VS (e.g. VS2010/2012)
// continues to work.
// This quirk is excluded from other targets that have no back compat burden.
//
// Quirk for VS debug-launch scenario to work:
// See if this is a PInvoke call with exactly one param that is the address of a struct local.
// In such a case indicate to frame-layout logic to add 16-bytes of padding
// between save-reg area and locals. This is to protect against the buffer
// overrun bug in microsoft.intellitrace.11.0.0.dll!ProfilerInterop.InitInterop().
//
// A work-around to this bug is to disable IntelliTrace debugging
// (VS->Tools->Options->IntelliTrace->Enable IntelliTrace - uncheck this option).
// The reason why this works on Jit64 is that at the point of AV the call stack is
//
// GetSystemInfo() Native call
// IL_Stub generated for PInvoke declaration.
// ProfilerInterface::InitInterop()
// ProfilerInterface.Cctor()
// VM asm worker
//
// The cctor body has just the call to InitInterop(). VM asm worker is holding
// something in rbx that is used immediately after the Cctor call. Jit64 generated
// InitInterop() method is pushing the registers in the following order
//
// rbx
// rbp
// rsi
// rdi
// r12
// r13
// Struct local
//
// Due to buffer overrun, rbx doesn't get impacted. Whereas RyuJIT jitted code of
// the same method is pushing regs in the following order
//
// rbp
// rdi
// rsi
// rbx
// struct local
//
// Therefore as a fix, we add padding between save-reg area and locals to
// make this scenario work against JB.
//
// Note: If this quirk gets broken due to other JIT optimizations, we should consider
// more tolerant fix. One such fix is to padd the struct.
void Lowering::CheckVSQuirkStackPaddingNeeded(GenTreeCall* call)
{
assert(comp->opts.IsJit64Compat());
#ifdef _TARGET_AMD64_
// Confine this to IL stub calls which aren't marked as unmanaged.
if (call->IsPInvoke() && !call->IsUnmanaged())
{
bool paddingNeeded = false;
GenTreePtr firstPutArgReg = nullptr;
for (GenTreeArgList* args = call->gtCallLateArgs; args; args = args->Rest())
{
GenTreePtr tmp = args->Current();
if (tmp->OperGet() == GT_PUTARG_REG)
{
if (firstPutArgReg == nullptr)
{
firstPutArgReg = tmp;
GenTreePtr op1 = firstPutArgReg->gtOp.gtOp1;
if (op1->OperGet() == GT_LCL_VAR_ADDR)
{
unsigned lclNum = op1->AsLclVarCommon()->GetLclNum();
if(comp->lvaTable[lclNum].TypeGet() == TYP_STRUCT)
{
// First arg is addr of a struct local.
paddingNeeded = true;
}
else
{
// Not a struct local.
assert(paddingNeeded == false);
break;
}
}
else
{
// First arg is not a local var addr.
assert(paddingNeeded == false);
break;
}
}
else
{
// Has more than one arg.
paddingNeeded = false;
break;
}
}
}
if (paddingNeeded)
{
comp->compVSQuirkStackPaddingNeeded = VSQUIRK_STACK_PAD;
}
}
#endif // _TARGET_AMD64_
}
// Inserts profiler hook, GT_PROF_HOOK for a tail call node.
//
// We need to insert this after all nested calls, but before all the arguments to this call have been set up.
// To do this, we look for the first GT_PUTARG_STK or GT_PUTARG_REG, and insert the hook immediately before
// that. If there are no args, then it should be inserted before the call node.
//
// For example:
// * stmtExpr void (top level) (IL 0x000...0x010)
// arg0 SETUP | /--* argPlace ref REG NA $c5
// this in rcx | | /--* argPlace ref REG NA $c1
// | | | /--* call ref System.Globalization.CultureInfo.get_InvariantCulture $c2
// arg1 SETUP | | +--* st.lclVar ref V02 tmp1 REG NA $c2
// | | | /--* lclVar ref V02 tmp1 u : 2 (last use) REG NA $c2
// arg1 in rdx | | +--* putarg_reg ref REG NA
// | | | /--* lclVar ref V00 arg0 u : 2 (last use) REG NA $80
// this in rcx | | +--* putarg_reg ref REG NA
// | | /--* call nullcheck ref System.String.ToLower $c5
// | | { * stmtExpr void (embedded)(IL 0x000... ? ? ? )
// | | { \--* prof_hook void REG NA
// arg0 in rcx | +--* putarg_reg ref REG NA
// control expr | +--* const(h) long 0x7ffe8e910e98 ftn REG NA
// \--* call void System.Runtime.Remoting.Identity.RemoveAppNameOrAppGuidIfNecessary $VN.Void
//
// In this case, the GT_PUTARG_REG src is a nested call. We need to put the embedded statement after that call (as shown).
// We assume that of all the GT_PUTARG_*, only the first one can have a nested call.
//
// Params:
// callNode - tail call node
// insertionPoint - if caller has an insertion point; If null
// profiler hook is inserted before args are setup
// but after all arg side effects are computed.
void Lowering::InsertProfTailCallHook(GenTreeCall* call, GenTree *insertionPoint)
{
assert(call->IsTailCall());
assert(comp->compIsProfilerHookNeeded());
if (insertionPoint == nullptr)
{
GenTreePtr tmp = nullptr;
for (GenTreeArgList* args = call->gtCallArgs; args; args = args->Rest())
{
tmp = args->Current();
assert(tmp->OperGet() != GT_PUTARG_REG); // We don't expect to see these in gtCallArgs
if (tmp->OperGet() == GT_PUTARG_STK)
{
// found it
insertionPoint = tmp;
break;
}
}
if (insertionPoint == nullptr)
{
for (GenTreeArgList* args = call->gtCallLateArgs; args; args = args->Rest())
{
tmp = args->Current();
if ((tmp->OperGet() == GT_PUTARG_REG) || (tmp->OperGet() == GT_PUTARG_STK))
{
// found it
insertionPoint = tmp;
break;
}
}
// If there are no args, insert before the call node
if (insertionPoint == nullptr)
{
insertionPoint = call;
}
}
}
assert(insertionPoint != nullptr);
GenTreeStmt* callStmt = comp->compCurStmt->AsStmt();
GenTreePtr profHookNode = new (comp, GT_PROF_HOOK) GenTree(GT_PROF_HOOK, TYP_VOID);
comp->fgInsertTreeBeforeAsEmbedded(profHookNode, insertionPoint, callStmt, comp->compCurBB);
}
// Lower fast tail call implemented as epilog+jmp.
// Also inserts PInvoke method epilog if required.
void Lowering::LowerFastTailCall(GenTreeCall *call)
{
#if FEATURE_FASTTAILCALL
// Tail call restrictions i.e. conditions under which tail prefix is ignored.
// Most of these checks are already done by importer or fgMorphTailCall().
// This serves as a double sanity check.
assert((comp->info.compFlags & CORINFO_FLG_SYNCH) == 0); // tail calls from synchronized methods
assert(!comp->opts.compNeedSecurityCheck); // tail call from methods that need security check
assert(!call->IsUnmanaged()); // tail calls to unamanaged methods
assert(!comp->compLocallocUsed); // tail call from methods that also do localloc
assert(!comp->getNeedsGSSecurityCookie()); // jit64 compat: tail calls from methods that need GS check
// We expect to see a call that meets the following conditions
assert(call->IsFastTailCall());
// VM cannot use return address hijacking when A() and B() tail call each
// other in mutual recursion. Therefore, this block is reachable through
// a GC-safe point or the whole method is marked as fully interruptible.
//
// TODO-Cleanup:
// optReachWithoutCall() depends on the fact that loop headers blocks
// will have a block number > fgLastBB. These loop headers gets added
// after dominator computation and get skipped by OptReachWithoutCall().
// The below condition cannot be asserted in lower because fgSimpleLowering()
// can add a new basic block for range check failure which becomes
// fgLastBB with block number > loop header block number.
//assert((comp->compCurBB->bbFlags & BBF_GC_SAFE_POINT) || !comp->optReachWithoutCall(comp->fgFirstBB, comp->compCurBB) || comp->genInterruptible);
#if INLINE_PINVOKE
// If PInvokes are in-lined, we have to remember to execute PInvoke method epilog anywhere that
// a method returns. This is a case of caller method has both PInvokes and tail calls.
if (comp->info.compCallUnmanaged)
{
InsertPInvokeMethodEpilog(comp->compCurBB);
}
#endif
// Args for tail call are setup in incoming arg area. The gc-ness of args of
// caller and callee (which being tail called) may not match. Therefore, everything
// from arg setup until the epilog need to be non-interuptible by GC. This is
// achieved by inserting GT_START_NONGC before the very first GT_PUTARG_STK node
// of call is setup. Note that once a stack arg is setup, it cannot have nested
// calls subsequently in execution order to setup other args, because the nested
// call could over-write the stack arg that is setup earlier.
GenTreePtr firstPutArgStk = nullptr;
GenTreeArgList* args;
ArrayStack<GenTree*> putargs(comp);
for (args = call->gtCallArgs; args; args = args->Rest())
{
GenTreePtr tmp = args->Current();
if (tmp->OperGet() == GT_PUTARG_STK)
putargs.Push(tmp);
}
for (args = call->gtCallLateArgs; args; args = args->Rest())
{
GenTreePtr tmp = args->Current();
if (tmp->OperGet() == GT_PUTARG_STK)
putargs.Push(tmp);
}
if (putargs.Height() > 0)
firstPutArgStk = putargs.Bottom();
// If we have a putarg_stk node, also count the number of non-standard args the
// call node has. Note that while determining whether a tail call can be fast
// tail called, we don't count non-standard args (passed in R10 or R11) since they
// don't contribute to outgoing arg space. These non-standard args are not
// accounted in caller's arg count but accounted in callee's arg count after
// fgMorphArgs(). Therefore, exclude callee's non-standard args while mapping
// callee's stack arg num to corresponding caller's stack arg num.
unsigned calleeNonStandardArgCount = call->GetNonStandardArgCount();
#ifdef DEBUG
// cross check non-standard arg count.
if (firstPutArgStk && call->HasNonStandardArgs())
{
fgArgInfoPtr argInfo = call->gtCall.fgArgInfo;
unsigned argCount = argInfo->ArgCount();
fgArgTabEntryPtr * argTable = argInfo->ArgTable();
unsigned cnt = 0;
for (unsigned i=0; i < argCount; i++)
{
if (argTable[i]->isNonStandard)
++cnt;
}
assert(cnt == calleeNonStandardArgCount);
}
#endif
// Say Caller(a, b, c, d, e) fast tail calls Callee(e, d, c, b, a)
// i.e. passes its arguments in reverse to Callee. During call site
// setup, after computing argument side effects, stack args are setup
// first and reg args next. In the above example, both Callers and
// Callee stack args (e and a respectively) share the same stack slot
// and are alive at the same time. The act of setting up Callee's
// stack arg will over-write the stack arg of Caller and if there are
// further uses of Caller stack arg we have to make sure that we move
// it to a temp before over-writing its slot and use temp in place of
// the corresponding Caller stack arg.
//
// For the above example, conceptually this is what is done
// tmp = e;
// Stack slot of e = a
// R9 = b, R8 = c, RDx = d
// RCX = tmp
//
// The below logic is meant to detect cases like this and introduce
// temps to set up args correctly for Callee.
GenTreeStmt* callStmt = comp->compCurStmt->AsStmt();
for (int i = 0; i < putargs.Height(); i++)
{
GenTreePtr putArgStkNode = putargs.Bottom(i);
assert(putArgStkNode->OperGet() == GT_PUTARG_STK);
// Get the caller arg num corresponding to this callee arg.
// Note that these two args share the same stack slot. Therefore,
// if there are further uses of corresponding caller arg, we need
// to move it to a temp and use the temp in this call tree.
//
// Note that Caller is guaranteed to have a param corresponding to
// this Callee's arg since fast tail call mechanism counts the
// stack slots required for both Caller and Callee for passing params
// and allow fast tail call only if stack slots required by Caller >=
// Callee.
fgArgTabEntryPtr argTabEntry = comp->gtArgEntryByNode(call, putArgStkNode);
assert(argTabEntry);
unsigned callerArgNum = argTabEntry->argNum - calleeNonStandardArgCount;
noway_assert(callerArgNum < comp->info.compArgsCount);
unsigned callerArgLclNum = callerArgNum;
LclVarDsc* callerArgDsc = comp->lvaTable + callerArgLclNum;
if (callerArgDsc->lvPromoted)
{
callerArgLclNum = callerArgDsc->lvFieldLclStart; // update the callerArgNum to the promoted struct field's lclNum
callerArgDsc = comp->lvaTable + callerArgLclNum;
}
noway_assert(callerArgDsc->lvIsParam);
// Start searching in execution order list till we encounter call node
unsigned tmpLclNum = BAD_VAR_NUM;
var_types tmpType = TYP_UNDEF;
for (GenTreePtr treeNode = putArgStkNode->gtNext; treeNode != call; treeNode = treeNode->gtNext)
{
if (treeNode->OperIsLocal() || treeNode->OperIsLocalAddr())
{
// This should neither be a GT_REG_VAR nor GT_PHI_ARG.
assert((treeNode->OperGet() != GT_REG_VAR) && (treeNode->OperGet() != GT_PHI_ARG));
GenTreeLclVarCommon *lcl = treeNode->AsLclVarCommon();
LclVarDsc* lclVar = &comp->lvaTable[lcl->gtLclNum];
// Fast tail calling criteria permits passing of structs of size 1, 2, 4 and 8 as args.
// It is possible that the callerArgLclNum corresponds to such a struct whose stack slot
// is getting over-written by setting up of a stack arg and there are further uses of
// any of its fields if such a struct is type-dependently promoted. In this case too
// we need to introduce a temp.
if ((lcl->gtLclNum == callerArgNum) || (lcl->gtLclNum == callerArgLclNum))
{
// Create tmp and use it in place of callerArgDsc
if (tmpLclNum == BAD_VAR_NUM)
{
tmpLclNum = comp->lvaGrabTemp(true DEBUGARG("Fast tail call lowering is creating a new local variable"));
comp->lvaSortAgain = true;
tmpType = genActualType(callerArgDsc->lvaArgType());
comp->lvaTable[tmpLclNum].lvType = tmpType;
comp->lvaTable[tmpLclNum].lvRefCnt = 1;
}
lcl->SetLclNum(tmpLclNum);
lcl->SetOper(GT_LCL_VAR);
}
}
}
// If we have created a temp, insert an embedded assignment stmnt before
// the first putargStkNode i.e.
// tmpLcl = CallerArg
if (tmpLclNum != BAD_VAR_NUM)
{
assert(tmpType != TYP_UNDEF);
GenTreeLclVar* local = new(comp, GT_LCL_VAR) GenTreeLclVar(GT_LCL_VAR, tmpType, callerArgLclNum, BAD_IL_OFFSET);
GenTree* assignExpr = comp->gtNewTempAssign(tmpLclNum, local);
comp->fgInsertTreeBeforeAsEmbedded(assignExpr, firstPutArgStk, callStmt, comp->compCurBB);
}
}
// Insert GT_START_NONGC node before the first GT_PUTARG_STK node.
// Note that if there are no args to be setup on stack, no need to
// insert GT_START_NONGC node.
GenTreePtr startNonGCNode = nullptr;
if (firstPutArgStk != nullptr)
{
startNonGCNode = new (comp, GT_START_NONGC) GenTree(GT_START_NONGC, TYP_VOID);
comp->fgInsertTreeBeforeAsEmbedded(startNonGCNode, firstPutArgStk, callStmt, comp->compCurBB);
// Gc-interruptability in the following case:
// foo(a, b, c, d, e) { bar(a, b, c, d, e); }
// bar(a, b, c, d, e) { foo(a, b, d, d, e); }
//
// Since the instruction group starting from the instruction that sets up first
// stack arg to the end of the tail call is marked as non-gc interruptible,
// this will form a non-interruptible tight loop causing gc-starvation. To fix
// this we insert GT_NO_OP as embedded stmt before GT_START_NONGC, if the method
// has a single basic block and is not a GC-safe point. The presence of a single
// nop outside non-gc interruptible region will prevent gc starvation.
if ((comp->fgBBcount == 1) && !(comp->compCurBB->bbFlags & BBF_GC_SAFE_POINT))
{
assert(comp->fgFirstBB == comp->compCurBB);
GenTreePtr noOp = new (comp, GT_NO_OP) GenTree(GT_NO_OP, TYP_VOID);
comp->fgInsertTreeBeforeAsEmbedded(noOp, startNonGCNode, callStmt, comp->compCurBB);
}
}
// Insert GT_PROF_HOOK node to emit profiler tail call hook. This should be
// inserted before the args are setup but after the side effects of args are
// computed. That is, GT_PROF_HOOK node needs to be inserted before GT_START_NONGC
// node if one exists.
if (comp->compIsProfilerHookNeeded())
{
InsertProfTailCallHook(call, startNonGCNode);
}
#else // !FEATURE_FASTTAILCALL
// Platform choose not to implement fast tail call mechanism.
// In such a case we should never be reaching this method as
// the expectation is that IsTailCallViaHelper() will always
// be true on such a platform.
unreached();
#endif
}
// Lower tail.call(void *copyRoutine, void *dummyArg, ...) as Jit_TailCall(void *copyRoutine, void *callTarget, ...).
// Also inserts PInvoke method epilog if required.
//
// Params
// call - The call node
// callTarget - The real call target. This is used to replace the dummyArg during lowering.
//
// Returns control expr for making a call to helper Jit_TailCall.
GenTree* Lowering::LowerTailCallViaHelper(GenTreeCall* call, GenTree *callTarget)
{
NYI_X86("Lower tail call dispatched via helper");
// Tail call restrictions i.e. conditions under which tail prefix is ignored.
// Most of these checks are already done by importer or fgMorphTailCall().
// This serves as a double sanity check.
assert((comp->info.compFlags & CORINFO_FLG_SYNCH) == 0); // tail calls from synchronized methods
assert(!comp->opts.compNeedSecurityCheck); // tail call from methods that need security check
assert(!call->IsUnmanaged()); // tail calls to unamanaged methods
assert(!comp->compLocallocUsed); // tail call from methods that also do localloc
assert(!comp->getNeedsGSSecurityCookie()); // jit64 compat: tail calls from methods that need GS check
// We expect to see a call that meets the following conditions
assert(call->IsTailCallViaHelper());
assert(callTarget != nullptr);
// TailCall helper though is a call never returns to caller nor GC interruptible.
// Therefore the block containg the tail call should be a GC-SafePoint to avoid
// GC starvation.
assert(comp->compCurBB->bbFlags & BBF_GC_SAFE_POINT);
#if INLINE_PINVOKE
// If PInvokes are in-lined, we have to remember to execute PInvoke method epilog anywhere that
// a method returns. This is a case of caller method has both PInvokes and tail calls.
if (comp->info.compCallUnmanaged)
{
InsertPInvokeMethodEpilog(comp->compCurBB);
}
#endif
// Remove gtCallAddr from execution order if one present.
GenTreeStmt* callStmt = comp->compCurStmt->AsStmt();
if (call->gtCallType == CT_INDIRECT)
{
assert(call->gtCallAddr != nullptr);
comp->fgDeleteTreeFromList(callStmt, call->gtCallAddr);
}
// In case of helper based tail calls, first argument is CopyRoutine and second argument
// is a place holder node.
fgArgTabEntry* argEntry;
#ifdef DEBUG
argEntry = comp->gtArgEntryByArgNum(call, 0);
assert(argEntry != nullptr);
assert(argEntry->node->gtOper == GT_PUTARG_REG);
GenTree *firstArg = argEntry->node->gtOp.gtOp1;
assert(firstArg->gtOper == GT_CNS_INT);
#endif
// Replace second arg by callTarget.
argEntry = comp->gtArgEntryByArgNum(call, 1);
assert(argEntry != nullptr);
assert(argEntry->node->gtOper == GT_PUTARG_REG);
GenTree *secondArg = argEntry->node->gtOp.gtOp1;
comp->fgInsertTreeInListAfter(callTarget, secondArg, callStmt);
comp->fgDeleteTreeFromList(callStmt, secondArg);
argEntry->node->gtOp.gtOp1 = callTarget;
// Transform this call node into a call to Jit tail call helper.
call->gtCallType = CT_HELPER;
call->gtCallMethHnd = comp->eeFindHelper(CORINFO_HELP_TAILCALL);
call->gtFlags &= ~GTF_CALL_VIRT_KIND_MASK;
// Lower this as if it were a pure helper call.
call->gtFlags &= ~(GTF_CALL_M_TAILCALL | GTF_CALL_M_TAILCALL_VIA_HELPER);
GenTree *result = LowerDirectCall(call);
// Now add back tail call flags for identifying this node as tail call dispatched via helper.
call->gtFlags |= GTF_CALL_M_TAILCALL | GTF_CALL_M_TAILCALL_VIA_HELPER;
// Insert profiler tail call hook if needed.
// Since we don't know the insertion point, pass null for second param.
if(comp->compIsProfilerHookNeeded())
{
InsertProfTailCallHook(call, nullptr);
}
return result;
}
// Lower "jmp <method>" tail call to insert PInvoke method epilog if required.
void Lowering::LowerJmpMethod(GenTree* jmp)
{
assert(jmp->OperGet() == GT_JMP);
JITDUMP("lowering GT_JMP\n");
DISPTREE(jmp);
JITDUMP("============");
#if INLINE_PINVOKE
// If PInvokes are in-lined, we have to remember to execute PInvoke method epilog anywhere that
// a method returns.
if (comp->info.compCallUnmanaged)
{
InsertPInvokeMethodEpilog(comp->compCurBB);
}
#endif
}
// Lower GT_RETURN node to insert PInvoke method epilog if required.
void Lowering::LowerRet(GenTree* ret)
{
assert(ret->OperGet() == GT_RETURN);
JITDUMP("lowering GT_RETURN\n");
DISPTREE(ret);
JITDUMP("============");
#if INLINE_PINVOKE
// Method doing PInvokes has exactly one return block unless it has tail calls.
if (comp->info.compCallUnmanaged && (comp->compCurBB == comp->genReturnBB))
{
InsertPInvokeMethodEpilog(comp->compCurBB);
}
#endif
}
GenTree* Lowering::LowerDirectCall(GenTreeCall* call)
{
noway_assert(call->gtCallType == CT_USER_FUNC || call->gtCallType == CT_HELPER);
// Don't support tail calling helper methods.
// But we might encounter tail calls dispatched via JIT helper appear as a tail call to helper.
noway_assert(!call->IsTailCall() || call->IsTailCallViaHelper() || call->gtCallType == CT_USER_FUNC);
// Non-virtual direct/indirect calls: Work out if the address of the
// call is known at JIT time. If not it is either an indirect call
// or the address must be accessed via an single/double indirection.
void* addr;
InfoAccessType accessType;
CorInfoHelpFunc helperNum = comp->eeGetHelperNum(call->gtCallMethHnd);
#ifdef FEATURE_READYTORUN_COMPILER
if (call->gtEntryPoint.addr != nullptr)
{
accessType = call->gtEntryPoint.accessType;
addr = call->gtEntryPoint.addr;
}
else
#endif
if (call->gtCallType == CT_HELPER)
{
noway_assert(helperNum != CORINFO_HELP_UNDEF);
// the convention on getHelperFtn seems to be (it's not documented)
// that it returns an address or if it returns null, pAddr is set to
// another address, which requires an indirection
void* pAddr;
addr = comp->info.compCompHnd->getHelperFtn(helperNum, (void**)&pAddr);
if (addr != nullptr)
{
accessType = IAT_VALUE;
}
else
{
accessType = IAT_PVALUE;
addr = pAddr;
}
}
else
{
noway_assert(helperNum == CORINFO_HELP_UNDEF);
CORINFO_ACCESS_FLAGS aflags = CORINFO_ACCESS_ANY;
if (call->IsSameThis())
aflags = (CORINFO_ACCESS_FLAGS)(aflags | CORINFO_ACCESS_THIS);
if ((call->NeedsNullCheck()) == 0)
aflags = (CORINFO_ACCESS_FLAGS)(aflags | CORINFO_ACCESS_NONNULL);
CORINFO_CONST_LOOKUP addrInfo;
comp->info.compCompHnd->getFunctionEntryPoint(call->gtCallMethHnd, &addrInfo, aflags);
accessType = addrInfo.accessType;
addr = addrInfo.addr;
}
GenTree* result = nullptr;
switch (accessType)
{
case IAT_VALUE:
// Non-virtual direct call to known address
if (!IsCallTargetInRange(addr) || call->IsTailCall())
{
result = AddrGen(addr);
}
else
{
// a direct call within range of hardware relative call instruction
// there is no extra code to generate
}
break;
case IAT_PVALUE:
// Non-virtual direct calls to addresses accessed by
// a single indirection.
result = Ind(AddrGen(addr));
break;
case IAT_PPVALUE:
// Non-virtual direct calls to addresses accessed by
// a double indirection.
//
// Double-indirection. Load the address into a register
// and call indirectly through the register
noway_assert(helperNum == CORINFO_HELP_UNDEF);
result = AddrGen(addr);
result = Ind(Ind(result));
break;
default:
noway_assert(!"Bad accessType");
break;
}
return result;
}
GenTree* Lowering::LowerDelegateInvoke(GenTreeCall* call)
{
noway_assert(call->gtCallType == CT_USER_FUNC);
assert((comp->info.compCompHnd->getMethodAttribs(call->gtCallMethHnd) & (CORINFO_FLG_DELEGATE_INVOKE|CORINFO_FLG_FINAL)) == (CORINFO_FLG_DELEGATE_INVOKE|CORINFO_FLG_FINAL));
GenTree* thisNode;
if (call->IsTailCallViaHelper())
{
// In case of helper dispatched tail calls, "thisptr" will be the third arg.
// The first two args are: real call target and addr of args copy routine.
const unsigned argNum = 2;
fgArgTabEntryPtr thisArgTabEntry = comp->gtArgEntryByArgNum(call, argNum);
thisNode = thisArgTabEntry->node;
}
else
{
thisNode = comp->gtGetThisArg(call);
}
assert(thisNode->gtOper == GT_PUTARG_REG);
GenTree** pThisExpr = &(thisNode->gtOp.gtOp1);
// replace original expression feeding into thisPtr with
// [originalThis + offsetOfDelegateInstance]
GenTreeStmt* newStmt = comp->fgInsertEmbeddedFormTemp(pThisExpr);
GenTree* stloc = newStmt->gtStmtExpr;
newStmt->gtFlags |= GTF_STMT_SKIP_LOWER;
unsigned originalThisLclNum = stloc->AsLclVarCommon()->GetLclNum();
GenTree* originalThisValue = *pThisExpr;
GenTree* newThisAddr = new(comp, GT_LEA) GenTreeAddrMode(TYP_REF,
originalThisValue,
nullptr,
0,
comp->eeGetEEInfo()->offsetOfDelegateInstance);
originalThisValue->InsertAfterSelf(newThisAddr);
GenTree* newThis = comp->gtNewOperNode(GT_IND, TYP_REF, newThisAddr);
newThis->SetCosts(IND_COST_EX, 2);
newThisAddr->InsertAfterSelf(newThis);
*pThisExpr = newThis;
// the control target is
// [originalThis + firstTgtOffs]
GenTree* base = new (comp, GT_LCL_VAR) GenTreeLclVar(originalThisValue->TypeGet(), originalThisLclNum, BAD_IL_OFFSET);
unsigned targetOffs = comp->eeGetEEInfo()->offsetOfDelegateFirstTarget;
GenTree* result = new(comp, GT_LEA) GenTreeAddrMode(TYP_REF, base, nullptr, 0, targetOffs);
GenTree* callTarget = Ind(result);
// don't need to sequence and insert this tree, caller will do it
return callTarget;
}
GenTree* Lowering::LowerIndirectNonvirtCall(GenTreeCall* call)
{
// Indirect cookie calls gets transformed by fgMorphArgs as indirect call with non-standard args.
// Hence we should never see this type of call in lower.
#ifdef _TARGET_X86_
if (call->gtCallCookie != nullptr)
{
NYI_X86("Morphing indirect non-virtual call with non-standard args");
}
#endif
noway_assert(call->gtCallCookie == nullptr);
return nullptr;
}
// The return trap checks some global location
// (the runtime tells us where that is and how many indirections to make)
// then based on the result conditionally calls a GC helper.
// we use a special node for this because at this time, introducing flow is tedious/difficult
GenTree* Lowering::CreateReturnTrapSeq()
{
// the GT_RETURNTRAP expands to this:
// if (g_TrapReturningThreads)
// {
// RareDisablePreemptiveGC();
// }
// the only thing to do here is build up the expression that evaluates 'g_TrapReturningThreads'
LONG* addrOfCaptureThreadGlobal;
LONG** pAddrOfCaptureThreadGlobal;
addrOfCaptureThreadGlobal = comp->info.compCompHnd->getAddrOfCaptureThreadGlobal((void**) &pAddrOfCaptureThreadGlobal);
GenTree* testTree;
if (addrOfCaptureThreadGlobal)
{
testTree = Ind(AddrGen(addrOfCaptureThreadGlobal));
}
else
{
testTree = Ind(Ind(AddrGen(pAddrOfCaptureThreadGlobal)));
}
return comp->gtNewOperNode(GT_RETURNTRAP, TYP_INT, testTree);
}
// returns a tree that stores the given constant (1 or 0) into the thread's GC state field
GenTree* Lowering::SetGCState(int state)
{
// Thread.offsetOfGcState = 0/1
assert(state == 0 || state == 1);
CORINFO_EE_INFO* pInfo = comp->eeGetEEInfo();
GenTree* base = new(comp, GT_LCL_VAR) GenTreeLclVar(TYP_I_IMPL, comp->info.compLvFrameListRoot, -1);
GenTree* storeGcState = new(comp, GT_STOREIND)
GenTreeIndir(GT_STOREIND, TYP_BYTE,
new(comp, GT_LEA) GenTreeAddrMode(TYP_I_IMPL,
base,
nullptr, 1, pInfo->offsetOfGCState),
new (comp, GT_CNS_INT) GenTreeIntCon(TYP_BYTE, state));
return storeGcState;
}
// Returns a tree that either links the locally-allocated Frame or unlinks it out of the frame list.
// This is used for pinvoke inlining
GenTree* Lowering::CreateFrameLinkUpdate(FrameLinkAction action)
{
CORINFO_EE_INFO* pInfo = comp->eeGetEEInfo();
CORINFO_EE_INFO::InlinedCallFrameInfo& callFrameInfo = pInfo->inlinedCallFrameInfo;
GenTree* TCB = new(comp, GT_LCL_VAR)
GenTreeLclVar(GT_LCL_VAR, TYP_I_IMPL, comp->info.compLvFrameListRoot, (IL_OFFSET)-1); // cast to resolve ambiguity.
// Thread->m_pFrame
GenTree* addr = new(comp, GT_LEA)
GenTreeAddrMode(TYP_I_IMPL, TCB, nullptr, 1, comp->eeGetEEInfo()->offsetOfThreadFrame);
GenTree*data = nullptr;
if (action == PushFrame)
{
// Thread->m_pFrame = &InlinedCallFrame;
data = new(comp, GT_LCL_FLD_ADDR)
GenTreeLclFld(GT_LCL_FLD_ADDR, TYP_BYREF, comp->lvaInlinedPInvokeFrameVar, callFrameInfo.offsetOfFrameVptr);
}
else
{
assert(action == PopFrame);
// Thread->m_pFrame = InlinedCallFrame.m_pNext;
data = new(comp, GT_LCL_FLD)
GenTreeLclFld(GT_LCL_FLD, TYP_BYREF, comp->lvaInlinedPInvokeFrameVar, pInfo->inlinedCallFrameInfo.offsetOfFrameLink);
}
GenTree* storeInd = new(comp, GT_STOREIND)
GenTreeIndir(GT_STOREIND, TYP_I_IMPL, addr, data);
return storeInd;
}
// this is the lowering equivalent of genPInvokeMethodProlog
// This creates the code that runs at the start of every method that has pinvoke calls
void Lowering::InsertPInvokeMethodProlog()
{
NYI_X86("Implement PInvoke frame init inlining for x86");
noway_assert(comp->info.compCallUnmanaged);
noway_assert(comp->lvaInlinedPInvokeFrameVar != BAD_VAR_NUM);
CORINFO_EE_INFO* pInfo = comp->eeGetEEInfo();
const CORINFO_EE_INFO::InlinedCallFrameInfo& callFrameInfo = pInfo->inlinedCallFrameInfo;
//first arg: &compiler->lvaInlinedPInvokeFrameVar + callFrameInfo.offsetOfFrameVptr
GenTree* frameAddr = new(comp, GT_LCL_FLD_ADDR)
GenTreeLclFld(GT_LCL_FLD_ADDR, TYP_BYREF, comp->lvaInlinedPInvokeFrameVar, callFrameInfo.offsetOfFrameVptr);
// obtain Thread object
// call runtime helper to fill in our frame and push it:
// TCB = CORINFO_HELP_INIT_PINVOKE_FRAME(&symFrameStart, secretArg);
GenTree* call = comp->gtNewHelperCallNode(CORINFO_HELP_INIT_PINVOKE_FRAME, TYP_I_IMPL, 0, comp->gtNewArgList(frameAddr, PhysReg(REG_SECRET_STUB_PARAM)));
// some sanity checks on the frame list root vardsc
LclVarDsc* varDsc = &comp->lvaTable[comp->info.compLvFrameListRoot];
noway_assert(!varDsc->lvIsParam);
noway_assert(varDsc->lvType == TYP_I_IMPL);
GenTree* store = new(comp, GT_STORE_LCL_VAR)
GenTreeLclVar(GT_STORE_LCL_VAR, TYP_I_IMPL, comp->info.compLvFrameListRoot, (IL_OFFSET)-1); // cast to resolve ambiguity.
store->gtOp.gtOp1 = call;
store->gtFlags |= GTF_VAR_DEF;
GenTreeStmt* stmt = LowerMorphAndSeqTree(store);
comp->fgInsertStmtAtBeg(comp->fgFirstBB, stmt);
JITDUMP("inserting pinvoke method prolog\n");
DISPTREE(stmt);
GenTree* lastStmt = stmt;
// --------------------------------------------------------
// InlinedCallFrame.m_pCallSiteSP = @RSP;
GenTreeLclFld* storeSP = new(comp, GT_STORE_LCL_FLD)
GenTreeLclFld(GT_STORE_LCL_FLD, TYP_I_IMPL, comp->lvaInlinedPInvokeFrameVar,
callFrameInfo.offsetOfCallSiteSP);
storeSP->gtOp1 = PhysReg(REG_SPBASE);
GenTreeStmt* storeSPStmt = LowerMorphAndSeqTree(storeSP);
comp->fgInsertStmtAfter(comp->fgFirstBB, lastStmt, storeSPStmt);
lastStmt = storeSPStmt;
// --------------------------------------------------------
// InlinedCallFrame.m_pCalleeSavedEBP = @RBP;
GenTreeLclFld* storeFP = new(comp, GT_STORE_LCL_FLD)
GenTreeLclFld(GT_STORE_LCL_FLD, TYP_I_IMPL, comp->lvaInlinedPInvokeFrameVar,
callFrameInfo.offsetOfCalleeSavedFP);
storeFP->gtOp1 = PhysReg(REG_FPBASE);
GenTreeStmt* storeFPStmt = LowerMorphAndSeqTree(storeFP);
comp->fgInsertStmtAfter(comp->fgFirstBB, lastStmt, storeFPStmt);
lastStmt = storeFPStmt;
// --------------------------------------------------------
if (comp->opts.eeFlags & CORJIT_FLG_IL_STUB)
{
// Push a frame - if we are NOT in an IL stub, this is done right before the call
// The init routine sets InlinedCallFrame's m_pNext, so we just set the thead's top-of-stack
GenTree* frameUpd = CreateFrameLinkUpdate(PushFrame);
comp->fgInsertStmtAfter(comp->fgFirstBB, lastStmt, LowerMorphAndSeqTree(frameUpd));
}
}
// code that needs to be run when exiting any method that has pinvoke inlines
// this needs to be inserted any place you can exit the function: returns, tailcalls and jmps
void Lowering::InsertPInvokeMethodEpilog(BasicBlock *returnBB)
{
assert(returnBB != nullptr);
assert(comp->info.compCallUnmanaged);
// Method doing Pinvoke calls has exactly one return block unless it has "jmp" or tail calls.
assert(((returnBB == comp->genReturnBB) && (returnBB->bbJumpKind == BBJ_RETURN)) || returnBB->endsWithTailCallOrJmp(comp));
// Thread.offsetOfGcState = 0/1
// That is [tcb + offsetOfGcState] = 1
GenTree* storeGCState = SetGCState(1);
comp->fgInsertStmtNearEnd(returnBB, LowerMorphAndSeqTree(storeGCState));
if (comp->opts.eeFlags & CORJIT_FLG_IL_STUB)
{
// Pop the frame, in non-stubs we do this around each pinvoke call
GenTree* frameUpd = CreateFrameLinkUpdate(PopFrame);
comp->fgInsertStmtNearEnd(returnBB, LowerMorphAndSeqTree(frameUpd));
}
}
// This function emits the call-site prolog for direct calls to unmanaged code.
// It does all the necessary setup of the InlinedCallFrame.
// frameListRoot specifies the local containing the thread control block.
// argSize or methodHandle is the value to be copied into the m_datum
// field of the frame (methodHandle may be indirected & have a reloc)
void Lowering::InsertPInvokeCallProlog(GenTreeCall* call)
{
// emit the following sequence
//
// frame.callTarget := methodHandle
// (x86) frame.callSiteTracker = SP
// frame.callSiteReturnAddress = return address
// thread->gcState = 0
// (non-stub) - update top Frame on TCB
//
GenTree* insertBefore = call;
if (call->gtCallType == CT_INDIRECT)
{
insertBefore = comp->fgGetFirstNode(call->gtCallAddr);
}
CORINFO_EE_INFO* pInfo = comp->eeGetEEInfo();
CORINFO_EE_INFO::InlinedCallFrameInfo& callFrameInfo = pInfo->inlinedCallFrameInfo;
gtCallTypes callType = (gtCallTypes)call->gtCallType;
noway_assert(comp->lvaInlinedPInvokeFrameVar != BAD_VAR_NUM);
// ----------------------------------------------------------------------------------
// Setup frame.callSiteTarget (what it referred to in the JIT)
// The actual field is Frame.m_Datum which has many different uses and meanings.
//
CORINFO_METHOD_HANDLE methodHandle;
if (callType == CT_INDIRECT)
methodHandle = nullptr;
else
methodHandle = call->gtCallMethHnd;
GenTree* src = nullptr;
if (callType == CT_INDIRECT)
{
assert(methodHandle == nullptr);
if (comp->info.compPublishStubParam)
{
src = new (comp, GT_LCL_VAR) GenTreeLclVar(TYP_I_IMPL, comp->lvaStubArgumentVar, BAD_IL_OFFSET);
}
// else { If we don't have secret parameter, m_Datum will be initialized by VM code }
}
else
{
assert(callType == CT_USER_FUNC);
void* embedMethodHandle;
void* pEmbedMethodHandle;
embedMethodHandle = (void*)comp->info.compCompHnd->embedMethodHandle(
methodHandle,
&pEmbedMethodHandle);
noway_assert((!embedMethodHandle) != (!pEmbedMethodHandle));
if (embedMethodHandle != NULL)
{
// frame.callSiteTarget = methodDesc
src = AddrGen(embedMethodHandle);
}
else
{
// frame.callSiteTarget = *pEmbedMethodHandle
src = Ind(AddrGen(pEmbedMethodHandle));
}
}
if (src)
{
// store into frame.m_Datum which offset is given by offsetOfCallTarget
GenTreeLclVarCommon* store = new(comp, GT_STORE_LCL_FLD)
GenTreeLclFld(GT_STORE_LCL_FLD,
TYP_I_IMPL,
comp->lvaInlinedPInvokeFrameVar,
callFrameInfo.offsetOfCallTarget);
store->gtOp1 = src;
comp->fgInsertTreeBeforeAsEmbedded(store, insertBefore, comp->compCurStmt->AsStmt(), currBlock);
}
#ifdef _TARGET_X86_
// ----------------------------------------------------------------------------------
// frame.callSiteTracker = SP
GenTree* storeTracker = new(comp, GT_STORE_LCL_FLD)
GenTreeLclFld(GT_STORE_LCL_FLD, TYP_I_IMPL,
comp->lvaInlinedPInvokeFrameVar,
callFrameInfo.offsetOfCallSiteSP);
storeTracker->AsLclVarCommon()->gtOp1 = PhysReg(REG_SPBASE);
comp->fgInsertTreeBeforeAsEmbedded(storeTracker, insertBefore, comp->compCurStmt->AsStmt(), currBlock);
DISPTREE(comp->compCurStmt);
#endif
// ----------------------------------------------------------------------------------
// frame.callSiteReturnAddress = &label (the point immediately after call)
GenTree* storeLab = new(comp, GT_STORE_LCL_FLD)
GenTreeLclFld(GT_STORE_LCL_FLD, TYP_I_IMPL,
comp->lvaInlinedPInvokeFrameVar,
callFrameInfo.offsetOfReturnAddress);
// we don't have a real label, and inserting one is hard (even if we made a special node)
// so for now we will just 'know' what this means in codegen
GenTreeLabel* labelRef = new(comp, GT_LABEL) GenTreeLabel(nullptr);
labelRef->gtType = TYP_I_IMPL;
storeLab->AsLclVarCommon()->gtOp1 = labelRef;
comp->fgInsertTreeBeforeAsEmbedded(storeLab, insertBefore, comp->compCurStmt->AsStmt(), currBlock);
if (!(comp->opts.eeFlags & CORJIT_FLG_IL_STUB))
{
// Set the TCB's frame to be the one we just created.
// Note the init routine for the InlinedCallFrame (CORINFO_HELP_INIT_PINVOKE_FRAME)
// has prepended it to the linked list to maintain the stack of Frames.
//
// Stubs do this once per stub, not once per call
GenTree* frameUpd = CreateFrameLinkUpdate(PushFrame);
comp->fgInsertTreeBeforeAsEmbedded(frameUpd, insertBefore, comp->compCurStmt->AsStmt(), currBlock);
}
// IMPORTANT **** This instruction must come last!!! ****
// It changes the thread's state to Preemptive mode
// ----------------------------------------------------------------------------------
// [tcb + offsetOfGcState] = 0
GenTree* storeGCState = SetGCState(0);
comp->fgInsertTreeBeforeAsEmbedded(storeGCState, insertBefore, comp->compCurStmt->AsStmt(), currBlock);
}
// insert the code that goes after every inlined pinvoke call
void Lowering::InsertPInvokeCallEpilog(GenTreeCall* call)
{
CORINFO_EE_INFO* pInfo = comp->eeGetEEInfo();
GenTreeStmt* newStmt;
GenTreeStmt* topStmt = comp->compCurStmt->AsStmt();
// gcstate = 1
GenTree* latest = call;
GenTree* tree = SetGCState(1);
newStmt = comp->fgInsertTreeAfterAsEmbedded(tree, latest, topStmt, currBlock);
latest = tree;
if (newStmt->gtStmtIsTopLevel())
{
topStmt = newStmt;
}
tree = CreateReturnTrapSeq();
newStmt = comp->fgInsertTreeAfterAsEmbedded(tree, latest, topStmt, currBlock);
latest = tree;
if (newStmt->gtStmtIsTopLevel())
{
topStmt = newStmt;
}
// Pop the frame
if (!(comp->opts.eeFlags & CORJIT_FLG_IL_STUB))
{
GenTree* frameUpd = CreateFrameLinkUpdate(PopFrame);
newStmt = comp->fgInsertTreeAfterAsEmbedded(frameUpd, latest, topStmt, currBlock);
latest = frameUpd;
if (newStmt->gtStmtIsTopLevel())
topStmt = newStmt;
}
}
GenTree* Lowering::LowerNonvirtPinvokeCall(GenTreeCall* call)
{
//------------------------------------------------------
// Non-virtual/Indirect calls: PInvoke calls.
GenTree* result = nullptr;
void* addr = nullptr;
// assert we have seen one of these
noway_assert(comp->info.compCallUnmanaged != 0);
// All code generated by this function must not contain the randomly-inserted NOPs
// that we insert to inhibit JIT spraying in partial trust scenarios.
// The PINVOKE_PROLOG op signals this to the code generator/emitter.
GenTree* prolog = new (comp, GT_NOP) GenTree(GT_PINVOKE_PROLOG, TYP_VOID);
comp->fgInsertTreeBeforeAsEmbedded(prolog, call, comp->compCurStmt->AsStmt(), currBlock);
InsertPInvokeCallProlog(call);
if (call->gtCallType != CT_INDIRECT)
{
GenTree* indir = nullptr;
noway_assert(call->gtCallType == CT_USER_FUNC);
CORINFO_METHOD_HANDLE methHnd = call->gtCallMethHnd;
void* pAddr;
addr = comp->info.compCompHnd->getAddressOfPInvokeFixup(methHnd, (void**)&pAddr);
if (addr != nullptr)
{
indir = Ind(AddrGen(addr));
}
else
{
// double indirection
indir = Ind(Ind(AddrGen(pAddr)));
}
result = indir;
}
InsertPInvokeCallEpilog(call);
return result;
}
// Expand the code necessary to calculate the control target.
// Returns: the expression needed to calculate the control target
// May insert embedded statements
GenTree* Lowering::LowerVirtualVtableCall(GenTreeCall* call)
{
noway_assert(call->gtCallType == CT_USER_FUNC);
// If this is a tail call via helper, thisPtr will be the third argument.
int thisPtrArgNum;
regNumber thisPtrArgReg;
if (call->IsTailCallViaHelper())
{
thisPtrArgNum = 2;
#ifdef _TARGET_X86_
NYI("Tail call via helper for x86");
thisPtrArgReg = REG_NA;
#else // !_TARGET_X86_
thisPtrArgReg = REG_ARG_2;
#endif // !_TARGET_X86_
}
else
{
thisPtrArgNum = 0;
thisPtrArgReg = comp->codeGen->genGetThisArgReg(call);
}
// get a reference to the thisPtr being passed
fgArgTabEntry* argEntry = comp->gtArgEntryByArgNum(call, thisPtrArgNum);
assert(argEntry->regNum == thisPtrArgReg);
assert(argEntry->node->gtOper == GT_PUTARG_REG);
GenTree *thisPtr = argEntry->node->gtOp.gtOp1;
// If what we are passing as the thisptr is not already a local, make a new local to place it in
// because we will be creating expressions based on it.
unsigned lclNum;
if (thisPtr->IsLocal())
{
lclNum = thisPtr->gtLclVarCommon.gtLclNum;
}
else
{
// Split off the thisPtr and store to a temporary variable.
if (vtableCallTemp == BAD_VAR_NUM)
{
vtableCallTemp = comp->lvaGrabTemp(true DEBUGARG("temp for virtual vtable call"));
}
GenTreeStmt* newStmt = comp->fgInsertEmbeddedFormTemp(&(argEntry->node->gtOp.gtOp1), vtableCallTemp);
newStmt->gtFlags |= GTF_STMT_SKIP_LOWER; // we're in postorder so we have already processed this subtree
GenTree* stLclVar = newStmt->gtStmtExpr;
assert(stLclVar->OperIsLocalStore());
lclNum = stLclVar->gtLclVar.gtLclNum;
}
// We'll introduce another use of this local so increase its ref count.
comp->lvaTable[lclNum].incRefCnts(comp->compCurBB->getBBWeight(comp), comp);
// If the thisPtr is a local field, then construct a local field type node
GenTree* local;
if (thisPtr->isLclField())
{
local = new(comp, GT_LCL_FLD) GenTreeLclFld(GT_LCL_FLD, thisPtr->TypeGet(), lclNum, thisPtr->AsLclFld()->gtLclOffs);
}
else
{
local = new(comp, GT_LCL_VAR) GenTreeLclVar(GT_LCL_VAR, thisPtr->TypeGet(), lclNum, BAD_IL_OFFSET);
}
// pointer to virtual table = [REG_CALL_THIS + offs]
GenTree* result = Ind(Offset(local, VPTR_OFFS));
// Get hold of the vtable offset (note: this might be expensive)
unsigned vtabOffsOfIndirection;
unsigned vtabOffsAfterIndirection;
comp->info.compCompHnd->getMethodVTableOffset(call->gtCallMethHnd, &vtabOffsOfIndirection, &vtabOffsAfterIndirection);
// Get the appropriate vtable chunk
// result = [REG_CALL_IND_SCRATCH + vtabOffsOfIndirection]
result = Ind(Offset(result, vtabOffsOfIndirection));
// Load the function address
// result = [reg+vtabOffs]
result = Ind(Offset(result, vtabOffsAfterIndirection));
return result;
}
// Lower stub dispatched virtual calls.
GenTree* Lowering::LowerVirtualStubCall(GenTreeCall* call)
{
assert((call->gtFlags & GTF_CALL_VIRT_KIND_MASK) == GTF_CALL_VIRT_STUB);
GenTree* result = nullptr;
// An x86 JIT which uses full stub dispatch must generate only
// the following stub dispatch calls:
//
// (1) isCallRelativeIndirect:
// call dword ptr [rel32] ; FF 15 ---rel32----
// (2) isCallRelative:
// call abc ; E8 ---rel32----
// (3) isCallRegisterIndirect:
// 3-byte nop ;
// call dword ptr [eax] ; FF 10
//
// THIS IS VERY TIGHTLY TIED TO THE PREDICATES IN
// vm\i386\cGenCpu.h, esp. isCallRegisterIndirect.
NYI_X86("Virtual Stub dispatched call lowering");
#ifdef _TARGET_64BIT_
// Non-tail calls: Jump Stubs are not taken into account by VM for mapping an AV into a NullRef
// exception. Therefore, JIT needs to emit an explicit null check. Note that Jit64 too generates
// an explicit null check.
//
// Tail calls: fgMorphTailCall() materializes null check explicitly and hence no need to emit
// null check.
// Non-64-bit: No need to null check the this pointer - the dispatch code will deal with this.
// The VM considers exceptions that occur in stubs on 64-bit to be not managed exceptions and
// it would be difficult to change this in a way so that it affects only the right stubs.
if (!call->IsTailCallViaHelper())
{
call->gtFlags |= GTF_CALL_NULLCHECK;
}
#endif
// TODO-Cleanup: Disable emitting random NOPs
// This is code to set up an indirect call to a stub address computed
// via dictionary lookup.
if (call->gtCallType == CT_INDIRECT)
{
// The importer decided we needed a stub call via a computed
// stub dispatch address, i.e. an address which came from a dictionary lookup.
// - The dictionary lookup produces an indirected address, suitable for call
// via "call [REG_VIRTUAL_STUB_PARAM]"
//
// This combination will only be generated for shared generic code and when
// stub dispatch is active.
// fgMorphArgs will have created trees to pass the address in REG_VIRTUAL_STUB_PARAM.
// All we have to do here is add an indirection to generate the actual call target.
GenTree* ind = Ind(call->gtCallAddr);
call->gtCallAddr->InsertAfterSelf(ind);
call->gtCallAddr = ind;
}
else
{
// Direct stub call.
// Get stub addr. This will return NULL if virtual call stubs are not active
void* stubAddr = call->gtStubCallStubAddr;
noway_assert(stubAddr != nullptr);
// If not CT_INDIRECT, then it should always be relative indir call.
// This is ensured by VM.
noway_assert(call->IsVirtualStubRelativeIndir());
// Direct stub calls, though the stubAddr itself may still need to be
// accesed via an indirection.
GenTree* tmp = AddrGen(stubAddr);
tmp->gtRegNum = REG_VIRTUAL_STUB_PARAM;
tmp = Ind(tmp);
tmp->gtRegNum = REG_JUMP_THUNK_PARAM;
result = tmp;
}
// TODO-Cleanup: start emitting random NOPS
return result;
}
//------------------------------------------------------------------------
// LowerIndCleanupHelper: Remove the nodes that are no longer used after an
// addressing mode is constructed
//
// Arguments:
// addrMode - A pointer to a new GenTreeAddrMode
// tree - The tree currently being considered to removal
//
// Return Value:
// None.
//
// Assumptions:
// 'addrMode' and 'tree' must be contained in comp->compCurStmt
void Lowering::LowerIndCleanupHelper(GenTreeAddrMode* addrMode, GenTreePtr tree)
{
if (tree == addrMode->Base() || tree == addrMode->Index()) return;
unsigned childCount = tree->NumChildren();
for (unsigned i = 0; i < childCount; i++)
{
LowerIndCleanupHelper(addrMode, tree->GetChild(i));
}
Compiler::fgSnipNode(comp->compCurStmt->AsStmt(), tree);
}
// given two nodes which will be used in an addressing mode (src1, src2)
// walk backwards from the use to those nodes to determine if they are
// potentially modified in that range
//
// returns: true if the sources given may be modified before they are used
bool Lowering::AreSourcesPossiblyModified(GenTree* use, GenTree* src1, GenTree* src2)
{
GenTree* cursor = use;
GenTree* firstTree = comp->compCurStmt->AsStmt()->gtStmtList;
while (cursor && cursor != firstTree)
{
cursor = cursor->gtPrev;
if (cursor == src1)
src1 = nullptr;
if (cursor == src2)
src2 = nullptr;
if (src2 == nullptr && src1 == nullptr)
return false;
if (src1 && comp->fgNodesMayInterfere(src1, cursor))
{
return true;
}
if (src2 && comp->fgNodesMayInterfere(src2, cursor))
{
return true;
}
}
assert(!"ran off beginning of stmt\n");
return true;
}
//------------------------------------------------------------------------
// LowerAddrMode: recognize trees which can be implemented using an addressing
// mode and transform them to a GT_LEA
//
// Arguments:
// pTree: pointer to the parent node's link to the node we care about
// before: node to insert the new GT_LEA before
// data: fgWalkData which is used to get info about parents and fixup call args
// isIndir: true if this addressing mode is the child of an indir
//
void Lowering::LowerAddrMode(GenTreePtr* pTree, GenTree* before, Compiler::fgWalkData* data, bool isIndir)
{
GenTree* addr = *pTree;
GenTreePtr base, index;
unsigned scale, offset;
bool rev;
// If it's not an indir, we need the fgWalkData to get info about the parent.
assert(isIndir || data);
// Find out if an addressing mode can be constructed
bool doAddrMode =
comp->codeGen->genCreateAddrMode(addr, -1, true, 0, &rev, &base, &index, &scale, &offset, true /*nogen*/);
if (scale == 0)
scale = 1;
if (!isIndir)
{
// this is just a reg-const add
if (!index)
return;
// this is just a reg-reg add
if (index && scale == 1 && offset == 0)
return;
}
// make sure there are not any side effects between def of leaves and use
if (doAddrMode && !AreSourcesPossiblyModified(addr, base, index))
{
GenTreePtr arrLength = NULL;
JITDUMP("Addressing mode:\n");
JITDUMP(" Base\n");
DISPNODE(base);
if (index != NULL)
{
JITDUMP(" + Index * %u + %u\n", scale, offset);
DISPNODE(index);
}
else
{
JITDUMP(" + %u\n", offset);
}
var_types addrModeType = addr->TypeGet();
if (addrModeType == TYP_REF)
addrModeType = TYP_BYREF;
GenTreeAddrMode* addrMode =
new(comp, GT_LEA) GenTreeAddrMode(addrModeType, base, index, scale, offset);
addrMode->CopyCosts(addr);
addrMode->gtRsvdRegs = addr->gtRsvdRegs;
addrMode->gtFlags |= (addr->gtFlags & (GTF_ALL_EFFECT | GTF_IND_FLAGS));
JITDUMP("New addressing mode node:\n");
DISPNODE(addrMode);
JITDUMP("\n");
// Required to prevent assert failure:
// Assertion failed 'op1 && op2' in flowgraph.cpp, Line: 34431
// when iterating the operands of a GT_LEA
// Test Case: self_host_tests_amd64\jit\jit64\opt\cse\VolatileTest_op_mul.exe
// Method: TestCSE:.cctor
// The method genCreateAddrMode() above probably should be fixed
// to not return rev=true, when index is returned as NULL
//
if (rev && !index)
rev = false;
if (rev)
{
addrMode->gtFlags |= GTF_REVERSE_OPS;
}
else
{
addrMode->gtFlags &= ~(GTF_REVERSE_OPS);
}
comp->fgInsertLinearNodeBefore(addrMode, before);
// Now we need to snip from the linear order all the nodes subsumed by the addrMode
LowerIndCleanupHelper(addrMode, addr);
GenTree* old = *pTree;
*pTree = addrMode;
if (!isIndir)
{
// this could be an arg to a call
comp->fgFixupIfCallArg(data->parentStack, old, addrMode);
}
}
else
{
JITDUMP(" No addressing mode\n");
}
}
//------------------------------------------------------------------------
// LowerAdd: turn this add into a GT_LEA if that would be profitable
//
// Arguments:
// pTree: pointer to the parent node's link to the node we care about
// data: fgWalkData which is used to get info about parents and fixup call args
void Lowering::LowerAdd(GenTreePtr* pTree, Compiler::fgWalkData* data)
{
GenTreePtr newNode = NULL;
GenTreePtr addr = *pTree;
#ifdef _TARGET_ARMARCH_
// For ARM architectures we don't have the LEA instruction
// therefore we won't get much benefit from doing this.
return;
#else // _TARGET_ARMARCH_
if (data->parentStack->Height() < 2)
return;
// if this is a child of an indir, let the parent handle it
if (data->parentStack->Index(1)->OperIsIndir())
return;
// if there is a chain of adds, only look at the topmost one
if (data->parentStack->Index(1)->gtOper == GT_ADD)
return;
if (!varTypeIsIntegralOrI(addr))
return;
LowerAddrMode(pTree, addr, data, false);
#endif // !_TARGET_ARMARCH_
}
//------------------------------------------------------------------------
// LowerInd: attempt to transform indirected expression into an addressing mode
//
// Arguments:
// pTree: pointer to the parent node's link to the node we care about
void Lowering::LowerInd(GenTreePtr* pTree)
{
GenTreePtr newNode = NULL;
GenTreePtr cTree = *pTree;
GenTreePtr base, index;
unsigned scale, offset;
bool rev;
JITDUMP("\n");
DISPNODE(cTree);
GenTreePtr addr = cTree->gtOp.gtOp1;
GenTreePtr before = cTree;
if (cTree->OperGet() == GT_STOREIND && !cTree->IsReverseOp())
before = comp->fgGetFirstNode(cTree->gtGetOp2());
LowerAddrMode(&cTree->gtOp.gtOp1, before, nullptr, true);
}
//------------------------------------------------------------------------
// LowerArrElem: Lower a GT_ARR_ELEM node
//
// Arguments:
// pTree - pointer to the field in the parent node that holds the pointer to the GT_ARR_ELEM node.
//
// Return Value:
// None.
//
// Assumptions:
// pTree points to a pointer to a GT_ARR_ELEM node.
//
// Notes:
// This performs the following lowering. We start with a node of the form:
// /--* <arrObj>
// +--* <index0>
// +--* <index1>
// /--* arrMD&[,]
//
// First, we create temps for arrObj if it is not already a lclVar, and for any of the index
// expressions that have side-effects.
// We then transform the tree into:
// <offset is null - no accumulated offset for the first index>
// /--* <arrObj>
// +--* <index0>
// /--* ArrIndex[i, ]
// +--* <arrObj>
// /--| arrOffs[i, ]
// | +--* <arrObj>
// | +--* <index1>
// +--* ArrIndex[*,j]
// +--* <arrObj>
// /--| arrOffs[*,j]
// +--* lclVar NewTemp
// /--* lea (scale = element size, offset = offset of first element)
//
// The new stmtExpr may be omitted if the <arrObj> is a lclVar.
// The new stmtExpr may be embedded if the <arrObj> is not the first tree in linear order for
// the statement containing the original arrMD.
// Note that the arrMDOffs is the INDEX of the lea, but is evaluated before the BASE (which is the second
// reference to NewTemp), because that provides more accurate lifetimes.
// There may be 1, 2 or 3 dimensions, with 1, 2 or 3 arrMDIdx nodes, respectively.
void
Lowering::LowerArrElem(GenTree **ppTree, Compiler::fgWalkData* data)
{
GenTreePtr tree = *ppTree;
// This will assert if we don't have an ArrElem node
GenTreeArrElem* arrElem = tree->AsArrElem();
Compiler* comp = data->compiler;
GenTreePtr curStmt = comp->compCurStmt;
unsigned char rank = arrElem->gtArrElem.gtArrRank;
JITDUMP("Lowering ArrElem\n");
JITDUMP("============\n");
DISPTREE(arrElem);
JITDUMP("\n");
assert(arrElem->gtArrObj->TypeGet() == TYP_REF);
// We need to have the array object in a lclVar.
if (!arrElem->gtArrObj->IsLocal())
{
// Split off the array object and store to a temporary variable.
GenTreeStmt* newStmt = comp->fgInsertEmbeddedFormTemp(&(arrElem->gtArrObj));
newStmt->gtFlags |= GTF_STMT_SKIP_LOWER;
GenTreePtr stLclVar = newStmt->gtStmtExpr;
assert(stLclVar->OperIsLocalStore());
// If we have made a new top-level statement, and it has inherited any
// embedded statements from curStmt, they have not yet been lowered.
if (newStmt->gtStmtIsTopLevel())
{
for (GenTreePtr nextEmbeddedStmt = newStmt->gtStmtNextIfEmbedded();
nextEmbeddedStmt != nullptr;
nextEmbeddedStmt = nextEmbeddedStmt->gtStmt.gtStmtNextIfEmbedded())
{
comp->compCurStmt = nextEmbeddedStmt;
comp->fgWalkTreePost(&nextEmbeddedStmt->gtStmt.gtStmtExpr, &Lowering::LowerNodeHelper, this, true);
nextEmbeddedStmt->gtFlags |= GTF_STMT_SKIP_LOWER;
}
}
// Restore curStmt.
comp->compCurStmt = curStmt;
}
GenTreePtr arrObjNode = arrElem->gtArrObj;
assert(arrObjNode->IsLocal());
GenTreePtr nextNode = arrElem->gtNext;
// ??? Can we have a top-level GT_ARR_ELEM ???
noway_assert(nextNode != nullptr);
// We need to evaluate the index expressions up-front if they have side effects.
for (unsigned char dim = 0; dim < rank; dim++)
{
GenTree* currIndexNode = arrElem->gtArrElem.gtArrInds[dim];
assert(varTypeIsIntegral(currIndexNode->TypeGet()));
if ((currIndexNode->gtFlags & GTF_SIDE_EFFECT) != 0)
{
// Split off this index computation and store to a temporary variable.
GenTreeStmt* newStmt = comp->fgInsertEmbeddedFormTemp(&(arrElem->gtArrElem.gtArrInds[dim]));
GenTreePtr stLclVar = newStmt->gtStmtExpr;
assert(stLclVar->OperIsLocalStore());
// We can't have made a new top-level statement, because we know we've got an ArrObj
// prior to the index nodes.
assert(newStmt->gtStmtIsEmbedded());
newStmt->gtFlags |= GTF_STMT_SKIP_LOWER;
// Restore curStmt (we've already lowered the tree we just split off).
comp->compCurStmt = curStmt;
}
}
// The first ArrOffs node will have 0 for the offset of the previous dimension.
GenTree* prevArrOffs = new(comp, GT_CNS_INT) GenTreeIntCon(TYP_I_IMPL, 0);
comp->fgInsertLinearNodeBefore(prevArrOffs, arrObjNode);
comp->fgSnipInnerNode(arrElem);
for (unsigned char dim = 0; dim < rank; dim++)
{
GenTree* currIndexTree = arrElem->gtArrElem.gtArrInds[dim];
GenTree* insertBeforeNode = nextNode;
// Use the original arrObjNode on the 0th ArrIndex node, and clone it for subsequent ones.
GenTreePtr idxArrObjNode;
if (dim == 0)
{
idxArrObjNode = arrObjNode;
}
else
{
idxArrObjNode = comp->gtClone(arrObjNode);
comp->fgInsertLinearNodeBefore(idxArrObjNode, nextNode);
}
// Move the index (temp created above, or non-side-effect computation) if needed.
// (All side-effecting computations we've split above need to come before the GT_ARR_INDEX nodes.)
if (currIndexTree->gtNext != insertBeforeNode)
{
GenTree* firstIndexNode = comp->fgGetFirstNode(currIndexTree);
GenTree* oldPrevNode = firstIndexNode->gtPrev;
GenTree* oldNextNode = currIndexTree->gtNext;
GenTree* newPrevNode = insertBeforeNode->gtPrev;
// All these are inner nodes, so they cannot be null.
assert(oldPrevNode != nullptr && oldNextNode != nullptr && newPrevNode != nullptr);
oldPrevNode->gtNext = oldNextNode;
oldNextNode->gtPrev = oldPrevNode;
firstIndexNode->gtPrev = newPrevNode;
newPrevNode->gtNext = firstIndexNode;
currIndexTree->gtNext = insertBeforeNode;
insertBeforeNode->gtPrev = currIndexTree;
}
// Next comes the GT_ARR_INDEX node.
GenTreeArrIndex* arrMDIdx = new(comp, GT_ARR_INDEX)
GenTreeArrIndex(TYP_INT, idxArrObjNode, currIndexTree, dim, rank, arrElem->gtArrElem.gtArrElemType);
arrMDIdx->gtFlags |= ((idxArrObjNode->gtFlags|currIndexTree->gtFlags) & GTF_ALL_EFFECT);
comp->fgInsertLinearNodeBefore(arrMDIdx, insertBeforeNode);
GenTree* offsArrObjNode = comp->gtClone(arrObjNode);
comp->fgInsertLinearNodeBefore(offsArrObjNode, insertBeforeNode);
GenTreeArrOffs* arrOffs = new(comp, GT_ARR_OFFSET)
GenTreeArrOffs(TYP_I_IMPL, prevArrOffs, arrMDIdx, offsArrObjNode, dim, rank, arrElem->gtArrElem.gtArrElemType);
comp->fgInsertLinearNodeBefore(arrOffs, insertBeforeNode);
arrOffs->gtFlags |= ((prevArrOffs->gtFlags|arrMDIdx->gtFlags|offsArrObjNode->gtFlags) & GTF_ALL_EFFECT);
prevArrOffs = arrOffs;
}
// Generate the LEA and make it reverse evaluation, because we want to evaluate the index expression before the base.
GenTreePtr leaBase = comp->gtClone(arrObjNode);
unsigned scale = arrElem->gtArrElem.gtArrElemSize;
// Note that below we're specifically using genTypeSize(TYP_INT) because array indices are not native int.
unsigned offset = comp->eeGetArrayDataOffset(arrElem->gtArrElem.gtArrElemType) + 2 * genTypeSize(TYP_INT) * arrElem->gtArrElem.gtArrRank;
GenTreePtr leaIndexNode = prevArrOffs;
if (!jitIsScaleIndexMul(scale))
{
// We do the address arithmetic in TYP_I_IMPL, though note that the lower bounds and lengths in memory are TYP_INT
GenTreePtr scaleNode = new(comp, GT_CNS_INT) GenTreeIntCon(TYP_I_IMPL, scale);
GenTreePtr mulNode = new(comp, GT_MUL) GenTreeOp(GT_MUL, TYP_I_IMPL, leaIndexNode, scaleNode);
comp->fgInsertLinearNodeBefore(scaleNode, nextNode);
comp->fgInsertLinearNodeBefore(mulNode, nextNode);
leaIndexNode = mulNode;
scale = 1;
}
comp->fgInsertLinearNodeBefore(leaBase, nextNode);
GenTreePtr leaNode = new(comp, GT_LEA) GenTreeAddrMode(arrElem->TypeGet(), leaBase, leaIndexNode, scale, offset);
leaNode->gtFlags |= GTF_REVERSE_OPS;
comp->fgInsertLinearNodeBefore(leaNode, nextNode);
*ppTree = leaNode;
// Update the costs.
comp->gtSetStmtInfo(curStmt);
JITDUMP("Results of lowering ArrElem:\n");
DISPTREE(leaNode);
JITDUMP("\nResulting statement:\n");
DISPTREE(curStmt);
JITDUMP("\n\n");
}
void Lowering::DoPhase()
{
#if 0
// The code in this #if can be used to debug lowering issues according to
// method hash. To use, simply set environment variables lowerhashlo and lowerhashhi
#ifdef DEBUG
unsigned methHash = info.compMethodHash();
char* lostr = getenv("lowerhashlo");
unsigned methHashLo = 0;
if (lostr != NULL)
{
sscanf_s(lostr, "%x", &methHashLo);
}
char* histr = getenv("lowerhashhi");
unsigned methHashHi = UINT32_MAX;
if (histr != NULL)
{
sscanf_s(histr, "%x", &methHashHi);
}
if (methHash < methHashLo || methHash > methHashHi)
return;
else
{
printf("Lowering for method %s, hash = 0x%x.\n",
info.compFullName, info.compMethodHash());
printf(""); // in our logic this causes a flush
}
#endif
#endif
#if !defined(_TARGET_64BIT_)
comp->lvaPromoteLongVars();
#endif // !defined(_TARGET_64BIT_)
#ifdef DEBUG
unsigned stmtNum = 0;
#endif
for (BasicBlock* block = comp->fgFirstBB; block; block = block->bbNext)
{
GenTreePtr stmt;
/* Make the block publicly available */
currBlock = block;
comp->compCurBB = block;
/* Walk the statement trees in this basic block */
for (stmt = block->bbTreeList; stmt; stmt = stmt->gtNext)
{
if (stmt->gtFlags & GTF_STMT_SKIP_LOWER)
{
continue;
}
#ifdef DEBUG
++stmtNum;
if (comp->verbose)
{
// This is a useful location for a conditional breakpoint in Visual Studio (i.e. when stmtNum == 15)
printf("Lowering BB%02u, stmt %u\n", block->bbNum, stmtNum);
}
#endif
comp->compCurStmt = stmt;
comp->fgWalkTreePost(&stmt->gtStmt.gtStmtExpr, &Lowering::DecompNodeHelper, this, true);
comp->fgWalkTreePost(&stmt->gtStmt.gtStmtExpr, &Lowering::LowerNodeHelper, this, true);
// We may have removed "stmt" in LowerNode().
stmt = comp->compCurStmt;
}
}
#if INLINE_PINVOKE
// If we have any PInvoke calls, insert the one-time prolog code. We've already inserted the epilog code in the appropriate spots.
// NOTE: there is a minor optimization opportunity here, as we still create p/invoke data structures and setup/teardown
// even if we've eliminated all p/invoke calls due to dead code elimination.
if (comp->info.compCallUnmanaged)
{
InsertPInvokeMethodProlog();
}
#endif
#ifdef DEBUG
JITDUMP("Lower has completed modifying nodes, proceeding to initialize LSRA TreeNodeInfo structs...\n");
if (VERBOSE)
{
comp->fgDispBasicBlocks(true);
}
#endif
// TODO-Throughput: We re-sort local variables to get the goodness of enregistering recently
// introduced local variables both by Rationalize and Lower; downside is we need to
// recompute standard local variable liveness in order to get Linear CodeGen working.
// For now we'll take the throughput hit of recomputing local liveness but in the long term
// we're striving to use the unified liveness computation (fgLocalVarLiveness) and stop
// computing it separately in LSRA.
if (comp->lvaCount != 0)
{
comp->lvaSortAgain = true;
}
comp->EndPhase(PHASE_LOWERING_DECOMP);
comp->fgLocalVarLiveness();
// local var liveness can delete code, which may create empty blocks
if (!comp->opts.MinOpts() && !comp->opts.compDbgCode)
{
comp->optLoopsMarked = false;
bool modified = comp->fgUpdateFlowGraph();
if (modified || comp->lvaSortAgain)
{
JITDUMP("had to run another liveness pass:\n");
comp->fgLocalVarLiveness();
}
}
#ifdef DEBUG
JITDUMP("Liveness pass finished after lowering, IR:\n");
JITDUMP("lvasortagain = %d\n", comp->lvaSortAgain);
if (VERBOSE)
{
comp->fgDispBasicBlocks(true);
}
#endif
// The initialization code for the TreeNodeInfo map was initially part of a single full IR
// traversal and it has been split because the order of traversal performed by fgWalkTreePost
// does not necessarily lower nodes in execution order and also, it could potentially
// add new BasicBlocks on the fly as part of the Lowering pass so the traversal won't be complete.
//
// Doing a new traversal guarantees we 'see' all new introduced trees and basic blocks allowing us
// to correctly initialize all the data structures LSRA requires later on.
// This code still has issues when it has to do with initialization of recently introduced locals by
// lowering. The effect of this is that any temporary local variable introduced by lowering won't be
// enregistered yielding suboptimal CQ.
// The reason for this is because we cannot re-sort the local variables per ref-count and bump of the number of
// tracked variables just here because then LSRA will work with mismatching BitSets (i.e. BitSets with different
// 'epochs' that were created before and after variable resorting, that will result in different number of tracked
// local variables).
//
// The fix for this is to refactor this code to be run JUST BEFORE LSRA and not as part of lowering.
// It's also desirable to avoid initializing this code using a non-execution order traversal.
//
LsraLocation currentLoc = 1;
for( BasicBlock* block = m_lsra->startBlockSequence();
block != nullptr;
block = m_lsra->moveToNextBlock())
{
GenTreePtr stmt;
// Increment the LsraLocation (currentLoc) at each BasicBlock.
// This ensures that the block boundary (RefTypeBB, RefTypeExpUse and RefTypeDummyDef) RefPositions
// are in increasing location order.
currentLoc += 2;
for (stmt = block->FirstNonPhiDef(); stmt; stmt = stmt->gtNext)
{
if (stmt->gtStmt.gtStmtIsEmbedded())
continue;
/* We increment the number position of each tree node by 2 to
* simplify the logic when there's the case of a tree that implicitly
* does a dual-definition of temps (the long case). In this case
* is easier to already have an idle spot to handle a dual-def instead
* of making some messy adjustments if we only increment the
* number position by one.
*/
GenTreePtr node;
foreach_treenode_execution_order(node, stmt)
{
#ifdef DEBUG
node->gtSeqNum = currentLoc;
#endif
TreeNodeInfo* info = &node->gtLsraInfo;
info->internalIntCount = 0;
info->internalFloatCount = 0;
info->isLocalDefUse = false;
info->isHelperCallWithKills = false;
info->isLsraAdded = false;
// if there is a reg indicated on the tree node, use that for dstCandidates
// the exception is the NOP, which sometimes show up around late args.
// TODO-Cleanup: get rid of those NOPs.
if (node->gtRegNum == REG_NA
|| node->gtOper == GT_NOP)
{
info->setDstCandidates(m_lsra, m_lsra->allRegs(node->TypeGet()));
}
else
{
info->setDstCandidates(m_lsra, genRegMask(node->gtRegNum));
}
info->setSrcCandidates(m_lsra, info->getDstCandidates(m_lsra));
info->setInternalCandidates(m_lsra, m_lsra->allRegs(TYP_INT));
info->isInitialized = true;
info->loc = currentLoc;
node->gtClearReg(comp);
currentLoc += 2;
}
}
for (stmt = block->FirstNonPhiDef(); stmt; stmt = stmt->gtNext)
{
if (stmt->gtStmt.gtStmtIsEmbedded())
continue;
comp->compCurStmt = stmt;
TreeNodeInfoInit(stmt);
// In the special case where a comma node is at the top level, make it consume
// its (op2) source
GenTreePtr tree = stmt->gtStmt.gtStmtExpr;
if (tree->gtOper == GT_COMMA && tree->TypeGet() != TYP_VOID)
{
tree->gtLsraInfo.srcCount = 1;
}
// In the special case where a lclVar node is at the top level, set it as
// localDefUse
// TODO-Cleanup: This used to be isCandidateLocalRef, but we haven't initialized the
// lvLRACandidate field yet. Fix this.
else if (comp->optIsTrackedLocal(tree))
{
tree->gtLsraInfo.isLocalDefUse = true;
tree->gtLsraInfo.dstCount = 0;
}
#if 0
// TODO-CQ: Enable this code after fixing the isContained() logic to not abort for these
// top-level nodes that throw away their result.
// If this is an interlocked operation that has a non-last-use lclVar as its op2,
// make sure we allocate a target register for the interlocked operation.; otherwise we need
// not allocate a register
else if ((tree->OperGet() == GT_LOCKADD || tree->OperGet() == GT_XCHG || tree->OperGet() == GT_XADD))
{
tree->gtLsraInfo.dstCount = 0;
if (tree->gtGetOp2()->IsLocal() && (tree->gtFlags & GTF_VAR_DEATH) == 0)
tree->gtLsraInfo.isLocalDefUse = true;
}
#endif
}
}
DBEXEC(VERBOSE, DumpNodeInfoMap());
}
/** Verifies if both of these trees represent the same indirection.
* Used by Lower to annotate if CodeGen generate an instruction of the
* form *addrMode BinOp= expr
*
* Preconditions: both trees are children of GT_INDs and their underlying children
* have the same gtOper.
*
* This is a first iteration to actually recognize trees that can be code-generated
* as a single read-modify-write instruction on AMD64/x86. For now
* this method only supports the recognition of simple addresing modes (through GT_LEA)
* or local var indirections. Local fields, array access and other more complex nodes are
* not yet supported.
*
* TODO-CQ: Perform tree recognition by using the Value Numbering Package, that way we can recognize
* arbitrary complex trees and support much more addressing patterns.
*/
bool Lowering::IndirsAreEquivalent(GenTreePtr candidate, GenTreePtr storeInd)
{
assert(candidate->OperGet() == GT_IND);
assert(storeInd->OperGet() == GT_STOREIND);
GenTreePtr pTreeA = candidate->gtGetOp1();
GenTreePtr pTreeB = storeInd->gtGetOp1();
// This method will be called by codegen (as well as during lowering).
// After register allocation, the sources may have been spilled and reloaded
// to a different register, indicated by an inserted GT_RELOAD node.
pTreeA = pTreeA->gtSkipReloadOrCopy();
pTreeB = pTreeB->gtSkipReloadOrCopy();
genTreeOps oper;
unsigned kind;
if (pTreeA->OperGet() != pTreeB->OperGet())
return false;
if (genTypeSize(candidate->gtType) != genTypeSize(storeInd->gtType))
return false;
oper = pTreeA->OperGet();
switch (oper)
{
case GT_LCL_VAR:
case GT_CLS_VAR_ADDR:
case GT_CNS_INT:
return NodesAreEquivalentLeaves(pTreeA, pTreeB);
case GT_LEA:
{
GenTreeAddrMode* gtAddr1 = pTreeA->AsAddrMode();
GenTreeAddrMode* gtAddr2 = pTreeB->AsAddrMode();
return NodesAreEquivalentLeaves(gtAddr1->Base(), gtAddr2->Base()) &&
NodesAreEquivalentLeaves(gtAddr1->Index(), gtAddr2->Index()) &&
gtAddr1->gtScale == gtAddr2->gtScale &&
gtAddr1->gtOffset == gtAddr2->gtOffset;
}
default:
// We don't handle anything that is not either a constant,
// a local var or LEA.
return false;
}
}
/** Test whether the two given nodes are the same leaves.
* Right now, only constant integers and local variables are supported
*/
bool Lowering::NodesAreEquivalentLeaves(GenTreePtr tree1, GenTreePtr tree2)
{
if (tree1 == nullptr && tree2 == nullptr)
return true;
// both null, they are equivalent, otherwise if either is null not equivalent
if (tree1 == nullptr || tree2 == nullptr)
return false;
tree1 = tree1->gtSkipReloadOrCopy();
tree2 = tree2->gtSkipReloadOrCopy();
if (tree1->TypeGet() != tree2->TypeGet())
return false;
if (tree1->OperGet() != tree2->OperGet())
return false;
if (!tree1->OperIsLeaf() || !tree2->OperIsLeaf())
return false;
switch (tree1->OperGet())
{
case GT_CNS_INT:
return tree1->gtIntCon.gtIconVal == tree2->gtIntCon.gtIconVal;
case GT_LCL_VAR:
return tree1->gtLclVarCommon.gtLclNum == tree2->gtLclVarCommon.gtLclNum;
case GT_CLS_VAR_ADDR:
return tree1->gtClsVar.gtClsVarHnd == tree2->gtClsVar.gtClsVarHnd;
default:
return false;
}
}
/**
* Takes care of replaing a GenTree node's child with a new tree.
*
* Assumptions:
* a) replacementNode has been unlinked (orphaned) and the expression it represents
* is a valid tree, and correctly sequenced internally in case it's not a leaf node.
* b) The location specified in ppTreeLocation must be a descendant of 'stmt'.
*
*/
void Lowering::ReplaceNode(GenTree** ppTreeLocation, GenTree* replacementNode, GenTree* stmt, BasicBlock* block)
{
assert(ppTreeLocation != nullptr);
GenTreePtr& treeLocation = *ppTreeLocation;
assert(treeLocation != nullptr);
assert(replacementNode != nullptr);
JITDUMP("The node to replace is:\n");
DISPNODE(treeLocation);
JITDUMP("The node that replaces it is:\n");
DISPTREE(replacementNode);
assert(comp->fgStmtContainsNode((GenTreeStmt*) stmt, treeLocation));
GenTreePtr first = comp->fgGetFirstNode(treeLocation);
comp->fgRemoveContainedEmbeddedStatements(treeLocation, stmt->AsStmt(), block);
assert(first != nullptr);
GenTreePtr gtPrev = first->gtPrev;
GenTreePtr gtNext = treeLocation->gtNext;
assert(!treeLocation->OperIsLeaf() || gtPrev == treeLocation->gtPrev);
if (gtPrev == nullptr)
{
stmt->gtStmt.gtStmtList = replacementNode;
}
else
{
gtPrev->gtNext = replacementNode;
}
// If we have an embedded statement, and the node we want to
// replace it's the first one in execution order, it won't fit
// the special case of having gtPrev == nullptr, so we have to
// ask directly whether is the first or not.
if (stmt->gtStmt.gtStmtIsEmbedded() && stmt->gtStmt.gtStmtList == first)
{
stmt->gtStmt.gtStmtList = replacementNode;
}
replacementNode->gtPrev = gtPrev;
if (gtNext != nullptr)
{
gtNext->gtPrev = replacementNode;
}
replacementNode->gtNext = gtNext;
treeLocation = replacementNode;
#ifdef DEBUG
comp->fgDebugCheckLinks();
#endif
}
/**
* Unlinks a node hanging from the specified location and replaces it with a GT_NOP
*
* Assumptions:
* The location specified in ppParentLink must be a descendant of stmt.
*
*/
void Lowering::UnlinkNode(GenTree** ppParentLink, GenTree* stmt, BasicBlock* block)
{
ReplaceNode(ppParentLink, comp->gtNewNothingNode(), stmt, block);
}
void Lowering::SimpleLinkNodeAfter(GenTree* prevTree, GenTree* newTree)
{
GenTree* nextTree = prevTree->gtNext;
newTree->gtPrev = prevTree;
prevTree->gtNext = newTree;
if (nextTree != nullptr)
{
newTree->gtNext = nextTree;
nextTree->gtPrev = newTree;
}
}
#ifdef DEBUG
void Lowering::DumpNodeInfoMap()
{
// dump tree node info
printf("-----------------------------\n");
printf("TREE NODE INFO DUMP\n");
printf("-----------------------------\n");
for (BasicBlock* block = comp->fgFirstBB; block; block = block->bbNext)
{
GenTreePtr stmt;
GenTreePtr tree;
for (stmt = block->FirstNonPhiDef(); stmt; stmt = stmt->gtNext)
{
GenTreePtr node;
foreach_treenode_execution_order(node, stmt)
{
if (stmt->gtStmt.gtStmtIsEmbedded()) continue;
comp->gtDispTree(node, nullptr, nullptr, true);
printf(" +");
node->gtLsraInfo.dump(m_lsra);
}
}
}
}
#endif // DEBUG
#endif // !LEGACY_BACKEND
|