1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
|
//
// 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 LclVarsInfo XX
XX XX
XX The variables to be used by the code generator. XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "emit.h"
#include "register_arg_convention.h"
/*****************************************************************************/
#ifdef DEBUG
#if DOUBLE_ALIGN
/* static */
unsigned Compiler::s_lvaDoubleAlignedProcsCount = 0;
#endif
#endif
/*****************************************************************************/
void Compiler::lvaInit()
{
/* We haven't allocated stack variables yet */
lvaRefCountingStarted = false;
lvaLocalVarRefCounted = false;
lvaSortAgain = false; // false: We don't need to call lvaSortOnly()
lvaTrackedFixed = false; // false: We can still add new tracked variables
lvaDoneFrameLayout = NO_FRAME_LAYOUT;
#if !FEATURE_EH_FUNCLETS
lvaShadowSPslotsVar = BAD_VAR_NUM;
#endif // !FEATURE_EH_FUNCLETS
lvaInlinedPInvokeFrameVar = BAD_VAR_NUM;
#if FEATURE_FIXED_OUT_ARGS
#if INLINE_NDIRECT
lvaPInvokeFrameRegSaveVar = BAD_VAR_NUM;
#endif // !INLINE_NDIRECT
lvaOutgoingArgSpaceVar = BAD_VAR_NUM;
#endif // FEATURE_FIXED_OUT_ARGS
#ifdef _TARGET_ARM_
lvaPromotedStructAssemblyScratchVar = BAD_VAR_NUM;
#endif // _TARGET_ARM_
lvaLocAllocSPvar = BAD_VAR_NUM;
lvaGSSecurityCookie = BAD_VAR_NUM;
#ifdef _TARGET_X86_
lvaVarargsBaseOfStkArgs = BAD_VAR_NUM;
#endif // _TARGET_X86_
lvaVarargsHandleArg = BAD_VAR_NUM;
lvaSecurityObject = BAD_VAR_NUM;
lvaStubArgumentVar = BAD_VAR_NUM;
lvaArg0Var = BAD_VAR_NUM;
lvaMonAcquired = BAD_VAR_NUM;
lvaInlineeReturnSpillTemp = BAD_VAR_NUM;
gsShadowVarInfo = NULL;
#if FEATURE_EH_FUNCLETS
lvaPSPSym = BAD_VAR_NUM;
#endif
#if FEATURE_SIMD
lvaSIMDInitTempVarNum = BAD_VAR_NUM;
#endif // FEATURE_SIMD
lvaCurEpoch = 0;
#ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
lvaFirstStackIncomingArgNum = BAD_VAR_NUM;
#endif // !FEATURE_UNIX_AMD64_STRUCT_PASSING
}
/*****************************************************************************/
void Compiler::lvaInitTypeRef()
{
/* x86 args look something like this:
[this ptr] [hidden return buffer] [declared arguments]* [generic context] [var arg cookie]
x64 is closer to the native ABI:
[this ptr] [hidden return buffer] [generic context] [var arg cookie] [declared arguments]*
(Note: prior to .NET Framework 4.5.1 for Windows 8.1 (but not .NET Framework 4.5.1 "downlevel"),
the "hidden return buffer" came before the "this ptr". Now, the "this ptr" comes first. This
is different from the C++ order, where the "hidden return buffer" always comes first.)
ARM and ARM64 are the same as the current x64 convention:
[this ptr] [hidden return buffer] [generic context] [var arg cookie] [declared arguments]*
Key difference:
The var arg cookie and generic context are swapped with respect to the user arguments
*/
/* Set compArgsCount and compLocalsCount */
info.compArgsCount = info.compMethodInfo->args.numArgs;
// Is there a 'this' pointer
if (!info.compIsStatic)
{
info.compArgsCount++;
}
else
{
info.compThisArg = BAD_VAR_NUM;
}
info.compILargsCount = info.compArgsCount;
#ifdef FEATURE_SIMD
if (featureSIMD && (info.compRetNativeType == TYP_STRUCT))
{
var_types structType = impNormStructType(info.compMethodInfo->args.retTypeClass);
info.compRetType = structType;
}
#endif // FEATURE_SIMD
// Are we returning a struct by value?
const bool hasRetBuffArg = impMethodInfo_hasRetBuffArg(info.compMethodInfo);
// Change the compRetNativeType if we are returning a struct by value in a register
if (!hasRetBuffArg && varTypeIsStruct(info.compRetNativeType))
{
#ifdef _TARGET_ARM_
// TODO-ARM64-NYI: HFA
if (!info.compIsVarArgs && IsHfa(info.compMethodInfo->args.retTypeClass))
{
info.compRetNativeType = TYP_STRUCT;
}
else
#endif
{
#ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
SYSTEMV_AMD64_CORINFO_STRUCT_REG_PASSING_DESCRIPTOR structDesc;
eeGetSystemVAmd64PassStructInRegisterDescriptor(info.compMethodInfo->args.retTypeClass, &structDesc);
if (structDesc.eightByteCount > 1)
{
info.compRetNativeType = TYP_STRUCT;
}
else
{
info.compRetNativeType = getEightByteType(structDesc, 0);
}
#else // !FEATURE_UNIX_AMD64_STRUCT_PASSING
// Check for TYP_STRUCT argument that can fit into a single register
var_types argRetType = argOrReturnTypeForStruct(info.compMethodInfo->args.retTypeClass, true /* forReturn */);
info.compRetNativeType = argRetType;
if (argRetType == TYP_UNKNOWN)
{
assert(!"Unexpected size when returning struct by value");
}
#endif // !FEATURE_UNIX_AMD64_STRUCT_PASSING
}
}
// Do we have a RetBuffArg?
if (hasRetBuffArg)
{
info.compArgsCount++;
}
else
{
info.compRetBuffArg = BAD_VAR_NUM;
}
/* There is a 'hidden' cookie pushed last when the
calling convention is varargs */
if (info.compIsVarArgs)
{
info.compArgsCount++;
}
// Is there an extra parameter used to pass instantiation info to
// shared generic methods and shared generic struct instance methods?
if (info.compMethodInfo->args.callConv & CORINFO_CALLCONV_PARAMTYPE)
{
info.compArgsCount++;
}
else
{
info.compTypeCtxtArg = BAD_VAR_NUM;
}
lvaCount =
info.compLocalsCount = info.compArgsCount +
info.compMethodInfo->locals.numArgs;
info.compILlocalsCount = info.compILargsCount +
info.compMethodInfo->locals.numArgs;
/* Now allocate the variable descriptor table */
if (compIsForInlining())
{
lvaTable = impInlineInfo->InlinerCompiler->lvaTable;
lvaCount = impInlineInfo->InlinerCompiler->lvaCount;
lvaTableCnt = impInlineInfo->InlinerCompiler->lvaTableCnt;
// No more stuff needs to be done.
return;
}
lvaTableCnt = lvaCount * 2;
if (lvaTableCnt < 16)
lvaTableCnt = 16;
lvaTable = (LclVarDsc*)compGetMemArray(lvaTableCnt, sizeof(*lvaTable), CMK_LvaTable);
size_t tableSize = lvaTableCnt * sizeof(*lvaTable);
memset(lvaTable, 0, tableSize);
for (unsigned i = 0; i < lvaTableCnt; i++)
{
new (&lvaTable[i], jitstd::placement_t()) LclVarDsc(this); // call the constructor.
}
//-------------------------------------------------------------------------
// Count the arguments and initialize the respective lvaTable[] entries
//
// First the implicit arguments
//-------------------------------------------------------------------------
InitVarDscInfo varDscInfo;
varDscInfo.Init(lvaTable, hasRetBuffArg);
lvaInitArgs(&varDscInfo);
//-------------------------------------------------------------------------
// Finally the local variables
//-------------------------------------------------------------------------
unsigned varNum = varDscInfo.varNum;
LclVarDsc * varDsc = varDscInfo.varDsc;
CORINFO_ARG_LIST_HANDLE localsSig = info.compMethodInfo->locals.args;
for (unsigned i = 0;
i < info.compMethodInfo->locals.numArgs;
i++, varNum++, varDsc++, localsSig = info.compCompHnd->getArgNext(localsSig))
{
CORINFO_CLASS_HANDLE typeHnd;
CorInfoTypeWithMod corInfoType = info.compCompHnd->getArgType(
&info.compMethodInfo->locals,
localsSig,
&typeHnd);
lvaInitVarDsc(varDsc,
varNum,
strip(corInfoType),
typeHnd,
localsSig,
&info.compMethodInfo->locals);
varDsc->lvPinned = ((corInfoType & CORINFO_TYPE_MOD_PINNED) != 0);
varDsc->lvOnFrame = true; // The final home for this local variable might be our local stack frame
}
if (// If there already exist unsafe buffers, don't mark more structs as unsafe
// as that will cause them to be placed along with the real unsafe buffers,
// unnecessarily exposing them to overruns. This can affect GS tests which
// intentionally do buffer-overruns.
!getNeedsGSSecurityCookie() &&
// GS checks require the stack to be re-ordered, which can't be done with EnC
!opts.compDbgEnC &&
compStressCompile(STRESS_UNSAFE_BUFFER_CHECKS, 25))
{
setNeedsGSSecurityCookie();
compGSReorderStackLayout = true;
for (unsigned i = 0; i < lvaCount; i++)
{
if ((lvaTable[i].lvType == TYP_STRUCT) && compStressCompile(STRESS_GENERIC_VARN, 60))
lvaTable[i].lvIsUnsafeBuffer = true;
}
}
if (getNeedsGSSecurityCookie())
{
// Ensure that there will be at least one stack variable since
// we require that the GSCookie does not have a 0 stack offset.
unsigned dummy = lvaGrabTempWithImplicitUse(false DEBUGARG("GSCookie dummy"));
lvaTable[dummy].lvType = TYP_INT;
}
#ifdef DEBUG
if (verbose)
lvaTableDump(INITIAL_FRAME_LAYOUT);
#endif
}
/*****************************************************************************/
void Compiler::lvaInitArgs(InitVarDscInfo * varDscInfo)
{
compArgSize = 0;
#if defined(_TARGET_ARM_) && defined(PROFILING_SUPPORTED)
// Prespill all argument regs on to stack in case of Arm when under profiler.
if (compIsProfilerHookNeeded())
{
codeGen->regSet.rsMaskPreSpillRegArg |= RBM_ARG_REGS;
}
#endif
//----------------------------------------------------------------------
/* Is there a "this" pointer ? */
lvaInitThisPtr(varDscInfo);
/* If we have a hidden return-buffer parameter, that comes here */
lvaInitRetBuffArg(varDscInfo);
//======================================================================
#if USER_ARGS_COME_LAST
//@GENERICS: final instantiation-info argument for shared generic methods
// and shared generic struct instance methods
lvaInitGenericsCtxt(varDscInfo);
/* If the method is varargs, process the varargs cookie */
lvaInitVarArgsHandle(varDscInfo);
#endif
//-------------------------------------------------------------------------
// Now walk the function signature for the explicit user arguments
//-------------------------------------------------------------------------
lvaInitUserArgs(varDscInfo);
#if !USER_ARGS_COME_LAST
//@GENERICS: final instantiation-info argument for shared generic methods
// and shared generic struct instance methods
lvaInitGenericsCtxt(varDscInfo);
/* If the method is varargs, process the varargs cookie */
lvaInitVarArgsHandle(varDscInfo);
#endif
//----------------------------------------------------------------------
// We have set info.compArgsCount in compCompile()
noway_assert(varDscInfo->varNum == info.compArgsCount);
assert (varDscInfo->intRegArgNum <= MAX_REG_ARG);
codeGen->intRegState.rsCalleeRegArgNum = varDscInfo->intRegArgNum;
#if !FEATURE_STACK_FP_X87
codeGen->floatRegState.rsCalleeRegArgNum = varDscInfo->floatRegArgNum;
#endif // FEATURE_STACK_FP_X87
// The total argument size must be aligned.
noway_assert((compArgSize % sizeof(void*)) == 0);
#ifdef _TARGET_X86_
/* We can not pass more than 2^16 dwords as arguments as the "ret"
instruction can only pop 2^16 arguments. Could be handled correctly
but it will be very difficult for fully interruptible code */
if (compArgSize != (size_t)(unsigned short)compArgSize)
NO_WAY("Too many arguments for the \"ret\" instruction to pop");
#endif
}
/*****************************************************************************/
void Compiler::lvaInitThisPtr(InitVarDscInfo * varDscInfo)
{
LclVarDsc * varDsc = varDscInfo->varDsc;
if (!info.compIsStatic)
{
varDsc->lvIsParam = 1;
#if ASSERTION_PROP
varDsc->lvSingleDef = 1;
#endif
varDsc->lvIsPtr = 1;
lvaArg0Var = info.compThisArg = varDscInfo->varNum;
noway_assert(info.compThisArg == 0);
if (eeIsValueClass(info.compClassHnd))
{
varDsc->lvType = TYP_BYREF;
#ifdef FEATURE_SIMD
if (featureSIMD)
{
var_types simdBaseType = TYP_UNKNOWN;
var_types type = impNormStructType(info.compClassHnd, nullptr, nullptr, &simdBaseType);
if (simdBaseType != TYP_UNKNOWN)
{
assert(varTypeIsSIMD(type));
varDsc->lvSIMDType = true;
varDsc->lvBaseType = simdBaseType;
}
}
#endif // FEATURE_SIMD
}
else
{
varDsc->lvType = TYP_REF;
}
if (tiVerificationNeeded)
{
varDsc->lvVerTypeInfo = verMakeTypeInfo(info.compClassHnd);
if (varDsc->lvVerTypeInfo.IsValueClass())
varDsc->lvVerTypeInfo.MakeByRef();
}
else
{
varDsc->lvVerTypeInfo = typeInfo();
}
// Mark the 'this' pointer for the method
varDsc->lvVerTypeInfo.SetIsThisPtr();
varDsc->lvIsRegArg = 1;
noway_assert(varDscInfo->intRegArgNum == 0);
varDsc->lvArgReg = genMapRegArgNumToRegNum(varDscInfo->allocRegArg(TYP_INT), varDsc->TypeGet());
varDsc->setPrefReg(varDsc->lvArgReg, this);
varDsc->lvOnFrame = true; // The final home for this incoming register might be our local stack frame
#ifdef DEBUG
if (verbose)
{
printf("'this' passed in register %s\n", getRegName(varDsc->lvArgReg));
}
#endif
compArgSize += TARGET_POINTER_SIZE;
varDscInfo->varNum++;
varDscInfo->varDsc++;
}
}
/*****************************************************************************/
void Compiler::lvaInitRetBuffArg(InitVarDscInfo * varDscInfo)
{
LclVarDsc * varDsc = varDscInfo->varDsc;
bool hasRetBuffArg = impMethodInfo_hasRetBuffArg(info.compMethodInfo);
#ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
if (varTypeIsStruct(info.compRetNativeType))
{
if (IsRegisterPassable(info.compMethodInfo->args.retTypeClass))
{
hasRetBuffArg = false;
}
}
#endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
if (hasRetBuffArg)
{
info.compRetBuffArg = varDscInfo->varNum;
varDsc->lvType = TYP_BYREF;
varDsc->lvIsParam = 1;
varDsc->lvIsRegArg = 1;
#if ASSERTION_PROP
varDsc->lvSingleDef = 1;
#endif
varDsc->lvArgReg = genMapRegArgNumToRegNum(varDscInfo->allocRegArg(TYP_INT), varDsc->TypeGet());
varDsc->setPrefReg(varDsc->lvArgReg, this);
varDsc->lvOnFrame = true; // The final home for this incoming register might be our local stack frame
info.compRetBuffDefStack = 0;
if (info.compRetType == TYP_STRUCT)
{
CORINFO_SIG_INFO sigInfo;
info.compCompHnd->getMethodSig(info.compMethodHnd, &sigInfo);
assert(JITtype2varType(sigInfo.retType) == info.compRetType); // Else shouldn't have a ret buff.
info.compRetBuffDefStack = (info.compCompHnd->isStructRequiringStackAllocRetBuf(sigInfo.retTypeClass) == TRUE);
if (info.compRetBuffDefStack)
{
// If we're assured that the ret buff argument points into a callers stack, we will type it as "TYP_I_IMPL"
// (native int/unmanaged pointer) so that it's not tracked as a GC ref.
varDsc->lvType = TYP_I_IMPL;
}
}
assert(genMapIntRegNumToRegArgNum(varDsc->lvArgReg) < MAX_REG_ARG);
#ifdef DEBUG
if (verbose)
{
printf("'__retBuf' passed in register %s\n", getRegName(varDsc->lvArgReg));
}
#endif
/* Update the total argument size, count and varDsc */
compArgSize += TARGET_POINTER_SIZE;
varDscInfo->varNum++;
varDscInfo->varDsc++;
}
}
/*****************************************************************************/
void Compiler::lvaInitUserArgs(InitVarDscInfo * varDscInfo)
{
//-------------------------------------------------------------------------
// Walk the function signature for the explicit arguments
//-------------------------------------------------------------------------
#if defined(_TARGET_X86_)
// Only (some of) the implicit args are enregistered for varargs
varDscInfo->maxIntRegArgNum = info.compIsVarArgs ? varDscInfo->intRegArgNum : MAX_REG_ARG;
#elif defined(_TARGET_AMD64_) && !defined(UNIX_AMD64_ABI)
// On System V type environment the float registers are not indexed together with the int ones.
varDscInfo->floatRegArgNum = varDscInfo->intRegArgNum;
#endif // _TARGET_*
CORINFO_ARG_LIST_HANDLE argLst = info.compMethodInfo->args.args;
const unsigned argSigLen = info.compMethodInfo->args.numArgs;
regMaskTP doubleAlignMask = RBM_NONE;
for (unsigned i = 0;
i < argSigLen;
i++, varDscInfo->varNum++, varDscInfo->varDsc++, argLst = info.compCompHnd->getArgNext(argLst))
{
LclVarDsc * varDsc = varDscInfo->varDsc;
CORINFO_CLASS_HANDLE typeHnd = NULL;
CorInfoTypeWithMod corInfoType = info.compCompHnd->getArgType(&info.compMethodInfo->args,
argLst,
&typeHnd);
varDsc->lvIsParam = 1;
#if ASSERTION_PROP
varDsc->lvSingleDef = 1;
#endif
lvaInitVarDsc( varDsc,
varDscInfo->varNum,
strip(corInfoType),
typeHnd,
argLst,
&info.compMethodInfo->args);
// For ARM, ARM64, and AMD64 varargs, all arguments go in integer registers
var_types argType = mangleVarArgsType(varDsc->TypeGet());
unsigned argSize = eeGetArgSize(argLst, &info.compMethodInfo->args);
unsigned cSlots = argSize / TARGET_POINTER_SIZE; // the total number of slots of this argument
// The number of slots that must be enregistered if we are to consider this argument enregistered.
// This is normally the same as cSlots, since we normally either enregister the entire object,
// or none of it. For structs on ARM, however, we only need to enregister a single slot to consider
// it enregistered, as long as we can split the rest onto the stack.
// TODO-ARM64-NYI: we can enregister a struct <= 16 bytes into two consecutive registers, if there are enough remaining argument registers.
// TODO-ARM64-NYI: HFA
unsigned cSlotsToEnregister = cSlots;
#ifdef _TARGET_ARM_
var_types hfaType = (varTypeIsStruct(argType)) ? GetHfaType(typeHnd) : TYP_UNDEF;
bool isHfaArg = !info.compIsVarArgs && varTypeIsFloating(hfaType);
// On ARM we pass the first 4 words of integer arguments and non-HFA structs in registers.
// But we pre-spill user arguments in varargs methods and structs.
//
unsigned cAlign;
bool preSpill = info.compIsVarArgs;
switch (argType)
{
case TYP_STRUCT:
assert(varDsc->lvSize() == argSize);
cAlign = varDsc->lvStructDoubleAlign ? 2 : 1;
// HFA arguments go on the stack frame. They don't get spilled in the prolog like struct
// arguments passed in the integer registers but get homed immediately after the prolog.
if (!isHfaArg)
{
cSlotsToEnregister = 1; // HFAs must be totally enregistered or not, but other structs can be split.
preSpill = true;
}
break;
case TYP_DOUBLE:
case TYP_LONG:
cAlign = 2;
break;
default:
cAlign = 1;
break;
}
if (isHfaArg)
{
// We've got the HFA size and alignment, so from here on out treat
// the type as a float or double.
argType = hfaType;
}
if (isRegParamType(argType))
{
compArgSize += varDscInfo->alignReg(argType, cAlign) * REGSIZE_BYTES;
}
if (argType == TYP_STRUCT)
{
// Are we going to split the struct between registers and stack? We can do that as long as
// no floating-point arguments have been put on the stack.
//
// From the ARM Procedure Call Standard:
// Rule C.5: "If the NCRN is less than r4 **and** the NSAA is equal to the SP,"
// then split the argument between registers and stack. Implication: if something
// has already been spilled to the stack, then anything that would normally be
// split between the core registers and the stack will be put on the stack.
// Anything that follows will also be on the stack. However, if something from
// floating point regs has been spilled to the stack, we can still use r0-r3 until they are full.
if (varDscInfo->canEnreg(TYP_INT, 1) && // The beginning of the struct can go in a register
!varDscInfo->canEnreg(TYP_INT, cSlots) && // The end of the struct can't fit in a register
varDscInfo->existAnyFloatStackArgs()) // There's at least one stack-based FP arg already
{
varDscInfo->setAllRegArgUsed(TYP_INT); // Prevent all future use of integer registers
preSpill = false; // This struct won't be prespilled, since it will go on the stack
}
}
if (preSpill)
{
for (unsigned ix = 0; ix < cSlots; ix++)
{
if (!varDscInfo->canEnreg(TYP_INT, ix + 1))
{
break;
}
regMaskTP regMask = genMapArgNumToRegMask(varDscInfo->regArgNum(TYP_INT) + ix, TYP_INT);
if (cAlign == 2)
{
doubleAlignMask |= regMask;
}
codeGen->regSet.rsMaskPreSpillRegArg |= regMask;
}
}
else
{
varDsc->lvOnFrame = true; // The final home for this incoming register might be our local stack frame
}
#else // !_TARGET_ARM_
#if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
SYSTEMV_AMD64_CORINFO_STRUCT_REG_PASSING_DESCRIPTOR structDesc;
if (varTypeIsStruct(argType))
{
assert(typeHnd != nullptr);
eeGetSystemVAmd64PassStructInRegisterDescriptor(typeHnd, &structDesc);
if (structDesc.passedInRegisters)
{
unsigned intRegCount = 0;
unsigned floatRegCount = 0;
for (unsigned int i = 0; i < structDesc.eightByteCount; i++)
{
switch (structDesc.eightByteClassifications[i])
{
case SystemVClassificationTypeInteger:
case SystemVClassificationTypeIntegerReference:
intRegCount++;
break;
case SystemVClassificationTypeSSE:
floatRegCount++;
break;
default:
assert(false && "Invalid eightbyte classification type.");
break;
}
}
if (intRegCount != 0 && !varDscInfo->canEnreg(TYP_INT, intRegCount))
{
structDesc.passedInRegisters = false; // No register to enregister the eightbytes.
}
if (floatRegCount != 0 && !varDscInfo->canEnreg(TYP_FLOAT, floatRegCount))
{
structDesc.passedInRegisters = false; // No register to enregister the eightbytes.
}
}
}
#endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
// The final home for this incoming register might be our local stack frame
// For System V platforms the final home will always be on the local stack frame.
varDsc->lvOnFrame = true;
#endif // !_TARGET_ARM_
bool canPassArgInRegisters = false;
#if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
if (varTypeIsStruct(argType))
{
canPassArgInRegisters = structDesc.passedInRegisters;
}
else
#endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
{
canPassArgInRegisters = varDscInfo->canEnreg(argType, cSlotsToEnregister);
}
if (canPassArgInRegisters)
{
/* Another register argument */
// Allocate the registers we need. allocRegArg() returns the first argument register number of the set.
// For non-HFA structs, we still "try" to enregister the whole thing; it will just max out if splitting
// to the stack happens.
unsigned firstAllocatedRegArgNum = 0;
#if FEATURE_MULTIREG_STRUCT_ARGS
varDsc->lvOtherArgReg = REG_NA;
#endif
#if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
unsigned secondAllocatedRegArgNum = 0;
var_types firstEightByteType = TYP_UNDEF;
var_types secondEightByteType = TYP_UNDEF;
if (varTypeIsStruct(argType))
{
if (structDesc.eightByteCount >= 1)
{
firstEightByteType = getEightByteType(structDesc, 0);
firstAllocatedRegArgNum = varDscInfo->allocRegArg(firstEightByteType, 1);
}
}
else
#endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
{
firstAllocatedRegArgNum = varDscInfo->allocRegArg(argType, cSlots);
}
#ifdef _TARGET_ARM_
if (isHfaArg)
{
// We need to save the fact that this HFA is enregistered
varDsc->lvIsHfaRegArg = true;
varDsc->SetHfaType(argType);
}
#endif // _TARGET_ARM_
varDsc->lvIsRegArg = 1;
#if FEATURE_MULTIREG_STRUCT_ARGS
if (varTypeIsStruct(argType))
{
#if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
varDsc->lvArgReg = genMapRegArgNumToRegNum(firstAllocatedRegArgNum, firstEightByteType);
// If there is a second eightbyte, get a register for it too and map the arg to the reg number.
if (structDesc.eightByteCount >= 2)
{
secondEightByteType = getEightByteType(structDesc, 1);
secondAllocatedRegArgNum = varDscInfo->allocRegArg(secondEightByteType, 1);
}
if (secondEightByteType != TYP_UNDEF)
{
varDsc->lvOtherArgReg = genMapRegArgNumToRegNum(secondAllocatedRegArgNum, secondEightByteType);
varDsc->addPrefReg(genRegMask(varDsc->lvOtherArgReg), this);
}
#else // ARM32 or ARM64
varDsc->lvArgReg = genMapRegArgNumToRegNum(firstAllocatedRegArgNum, TYP_I_IMPL);
#ifdef _TARGET_ARM64_
if (cSlots == 2)
{
varDsc->lvOtherArgReg = genMapRegArgNumToRegNum(firstAllocatedRegArgNum+1, TYP_I_IMPL);
varDsc->addPrefReg(genRegMask(varDsc->lvOtherArgReg), this);
}
#endif // _TARGET_ARM64_
#endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
}
else
#endif // FEATURE_MULTIREG_STRUCT_ARGS
{
varDsc->lvArgReg = genMapRegArgNumToRegNum(firstAllocatedRegArgNum, argType);
}
varDsc->setPrefReg(varDsc->lvArgReg, this);
#ifdef _TARGET_ARM_
if (varDsc->TypeGet() == TYP_LONG)
{
varDsc->lvOtherReg = genMapRegArgNumToRegNum(firstAllocatedRegArgNum + 1, TYP_INT);
varDsc->addPrefReg(genRegMask(varDsc->lvOtherReg), this);
}
#endif // _TARGET_ARM_
#ifdef DEBUG
if (verbose)
{
printf("Arg #%u passed in register(s) ", varDscInfo->varNum);
bool isFloat = false;
#if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
// In case of one eightbyte struct the type is already normalized earlier.
// The varTypeIsFloating(argType) is good for this case.
if (varTypeIsStruct(argType) && (structDesc.eightByteCount >= 1))
{
isFloat = varTypeIsFloating(firstEightByteType);
}
else
#else // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
{
isFloat = varTypeIsFloating(argType);
}
#endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
#if defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
if (varTypeIsStruct(argType))
{
// Print both registers, just to be clear
if (firstEightByteType == TYP_UNDEF)
{
printf("firstEightByte: <not used>");
}
else
{
printf("firstEightByte: %s", getRegName(genMapRegArgNumToRegNum(firstAllocatedRegArgNum, firstEightByteType), isFloat));
}
if (secondEightByteType == TYP_UNDEF)
{
printf(", secondEightByte: <not used>");
}
else
{
printf(", secondEightByte: %s", getRegName(genMapRegArgNumToRegNum(secondAllocatedRegArgNum, secondEightByteType), varTypeIsFloating(secondEightByteType)));
}
}
else
#endif // defined(FEATURE_UNIX_AMD64_STRUCT_PASSING)
{
unsigned regArgNum = genMapRegNumToRegArgNum(varDsc->lvArgReg, argType);
for (unsigned ix = 0; ix < cSlots; ix++, regArgNum++)
{
if (ix > 0)
printf(",");
if (!isFloat && (regArgNum >= varDscInfo->maxIntRegArgNum)) // a struct has been split between registers and stack
{
printf(" stack slots:%d", cSlots - ix);
break;
}
#ifdef _TARGET_ARM_
if (isFloat)
{
// Print register size prefix
if (argType == TYP_DOUBLE)
{
// Print both registers, just to be clear
printf("%s/%s", getRegName(genMapRegArgNumToRegNum(regArgNum, argType), isFloat),
getRegName(genMapRegArgNumToRegNum(regArgNum + 1, argType), isFloat));
// doubles take 2 slots
assert(ix + 1 < cSlots);
++ix;
++regArgNum;
}
else
{
printf("%s", getRegName(genMapRegArgNumToRegNum(regArgNum, argType), isFloat));
}
}
else
#endif // _TARGET_ARM_
{
printf("%s", getRegName(genMapRegArgNumToRegNum(regArgNum, argType), isFloat));
}
}
}
printf("\n");
}
#endif // DEBUG
} // end if (canPassArgInRegisters)
else
{
#ifdef _TARGET_ARM_
varDscInfo->setAllRegArgUsed(argType);
if (varTypeIsFloating(argType))
{
varDscInfo->setAnyFloatStackArgs();
}
#endif
}
#ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
// The arg size is returning the number of bytes of the argument. For a struct it could return a size not a multiple of
// TARGET_POINTER_SIZE. The stack allocated space should always be multiple of TARGET_POINTER_SIZE, so round it up.
compArgSize += (unsigned)roundUp(argSize, TARGET_POINTER_SIZE);
#else // !FEATURE_UNIX_AMD64_STRUCT_PASSING
compArgSize += argSize;
#endif // !FEATURE_UNIX_AMD64_STRUCT_PASSING
if (info.compIsVarArgs)
{
#if defined(_TARGET_X86_)
varDsc->lvStkOffs = compArgSize;
#else // !_TARGET_X86_
// TODO-CQ: We shouldn't have to go as far as to declare these
// address-exposed -- DoNotEnregister should suffice.
lvaSetVarAddrExposed(varDscInfo->varNum);
#endif // !_TARGET_X86_
}
} // for each user arg
#ifdef _TARGET_ARM_
if (doubleAlignMask != RBM_NONE)
{
assert(RBM_ARG_REGS == 0xF);
assert((doubleAlignMask & RBM_ARG_REGS) == doubleAlignMask);
if (doubleAlignMask != RBM_NONE && doubleAlignMask != RBM_ARG_REGS)
{
// doubleAlignMask can only be 0011 and/or 1100 as 'double aligned types' can
// begin at r0 or r2.
assert(doubleAlignMask == 0x3 || doubleAlignMask == 0xC /* || 0xF is if'ed out */);
// Now if doubleAlignMask is 0011 i.e., {r0,r1} and we prespill r2 or r3
// but not both, then the stack would be misaligned for r0. So spill both
// r2 and r3.
//
// ; +0 --- caller SP double aligned ----
// ; -4 r2 r3
// ; -8 r1 r1
// ; -c r0 r0 <-- misaligned.
// ; callee saved regs
if (doubleAlignMask == 0x3 && doubleAlignMask != codeGen->regSet.rsMaskPreSpillRegArg)
{
codeGen->regSet.rsMaskPreSpillAlign = (~codeGen->regSet.rsMaskPreSpillRegArg & ~doubleAlignMask) & RBM_ARG_REGS;
}
}
}
#endif // _TARGET_ARM_
}
/*****************************************************************************/
void Compiler::lvaInitGenericsCtxt(InitVarDscInfo * varDscInfo)
{
//@GENERICS: final instantiation-info argument for shared generic methods
// and shared generic struct instance methods
if (info.compMethodInfo->args.callConv & CORINFO_CALLCONV_PARAMTYPE)
{
info.compTypeCtxtArg = varDscInfo->varNum;
LclVarDsc * varDsc = varDscInfo->varDsc;
varDsc->lvIsParam = 1;
#if ASSERTION_PROP
varDsc->lvSingleDef = 1;
#endif
varDsc->lvType = TYP_I_IMPL;
if (varDscInfo->canEnreg(TYP_I_IMPL))
{
/* Another register argument */
varDsc->lvIsRegArg = 1;
varDsc->lvArgReg = genMapRegArgNumToRegNum(varDscInfo->regArgNum(TYP_INT), varDsc->TypeGet());
varDsc->setPrefReg(varDsc->lvArgReg, this);
varDsc->lvOnFrame = true; // The final home for this incoming register might be our local stack frame
varDscInfo->intRegArgNum++;
#ifdef DEBUG
if (verbose)
{
printf("'GenCtxt' passed in register %s\n", getRegName(varDsc->lvArgReg));
}
#endif
}
compArgSize += TARGET_POINTER_SIZE;
#if defined(_TARGET_X86_)
if (info.compIsVarArgs)
varDsc->lvStkOffs = compArgSize;
#endif // _TARGET_X86_
varDscInfo->varNum++;
varDscInfo->varDsc++;
}
}
/*****************************************************************************/
void Compiler::lvaInitVarArgsHandle(InitVarDscInfo * varDscInfo)
{
if (info.compIsVarArgs)
{
lvaVarargsHandleArg = varDscInfo->varNum;
LclVarDsc * varDsc = varDscInfo->varDsc;
varDsc->lvType = TYP_I_IMPL;
varDsc->lvIsParam = 1;
// Make sure this lives in the stack -- address may be reported to the VM.
// TODO-CQ: This should probably be:
// lvaSetVarDoNotEnregister(varDscInfo->varNum DEBUG_ARG(DNER_VMNeedsStackAddr));
// But that causes problems, so, for expedience, I switched back to this heavyweight
// hammer. But I think it should be possible to switch; it may just work now
// that other problems are fixed.
lvaSetVarAddrExposed(varDscInfo->varNum);
#if ASSERTION_PROP
varDsc->lvSingleDef = 1;
#endif
if (varDscInfo->canEnreg(TYP_I_IMPL))
{
/* Another register argument */
unsigned varArgHndArgNum = varDscInfo->allocRegArg(TYP_I_IMPL);
varDsc->lvIsRegArg = 1;
varDsc->lvArgReg = genMapRegArgNumToRegNum(varArgHndArgNum, TYP_I_IMPL);
varDsc->setPrefReg(varDsc->lvArgReg, this);
varDsc->lvOnFrame = true; // The final home for this incoming register might be our local stack frame
#ifdef _TARGET_ARM_
// This has to be spilled right in front of the real arguments and we have
// to pre-spill all the argument registers explicitly because we only have
// have symbols for the declared ones, not any potential variadic ones.
for (unsigned ix = varArgHndArgNum; ix < ArrLen(intArgMasks); ix++)
{
codeGen->regSet.rsMaskPreSpillRegArg |= intArgMasks[ix];
}
#endif // _TARGET_ARM_
#ifdef DEBUG
if (verbose)
{
printf("'VarArgHnd' passed in register %s\n", getRegName(varDsc->lvArgReg));
}
#endif // DEBUG
}
#ifndef LEGACY_BACKEND
else
{
// For the RyuJIT backend, we need to mark these as being on the stack,
// as this is not done elsewhere in the case that canEnreg returns false.
varDsc->lvOnFrame = true;
}
#endif // !LEGACY_BACKEND
/* Update the total argument size, count and varDsc */
compArgSize += TARGET_POINTER_SIZE;
varDscInfo->varNum++;
varDscInfo->varDsc++;
#if defined(_TARGET_X86_)
varDsc->lvStkOffs = compArgSize;
// Allocate a temp to point at the beginning of the args
lvaVarargsBaseOfStkArgs = lvaGrabTemp(false DEBUGARG("Varargs BaseOfStkArgs"));
lvaTable[lvaVarargsBaseOfStkArgs].lvType = TYP_I_IMPL;
#endif // _TARGET_X86_
}
}
/*****************************************************************************/
void Compiler::lvaInitVarDsc(LclVarDsc * varDsc,
unsigned varNum,
CorInfoType corInfoType,
CORINFO_CLASS_HANDLE typeHnd,
CORINFO_ARG_LIST_HANDLE varList,
CORINFO_SIG_INFO * varSig)
{
noway_assert(varDsc == &lvaTable[varNum]);
switch (corInfoType)
{
// Mark types that looks like a pointer for doing shadow-copying of
// parameters if we have an unsafe buffer.
// Note that this does not handle structs with pointer fields. Instead,
// we rely on using the assign-groups/equivalence-groups in
// gsFindVulnerableParams() to determine if a buffer-struct contains a
// pointer. We could do better by having the EE determine this for us.
// Note that we want to keep buffers without pointers at lower memory
// addresses than buffers with pointers.
case CORINFO_TYPE_PTR:
case CORINFO_TYPE_BYREF:
case CORINFO_TYPE_CLASS:
case CORINFO_TYPE_STRING:
case CORINFO_TYPE_VAR:
case CORINFO_TYPE_REFANY:
varDsc->lvIsPtr = 1;
break;
default:
break;
}
var_types type = JITtype2varType(corInfoType);
if (varTypeIsFloating(type))
{
compFloatingPointUsed = true;
}
if (tiVerificationNeeded || compIsMethodForLRSampling)
{
varDsc->lvVerTypeInfo = verParseArgSigToTypeInfo(varSig, varList);
}
if (tiVerificationNeeded)
{
if (varDsc->lvIsParam)
{
// For an incoming ValueType we better be able to have the full type information
// so that we can layout the parameter offsets correctly
if (varTypeIsStruct(type) && varDsc->lvVerTypeInfo.IsDead())
{
BADCODE("invalid ValueType parameter");
}
// For an incoming reference type we need to verify that the actual type is
// a reference type and not a valuetype.
if (type == TYP_REF && !(varDsc->lvVerTypeInfo.IsType(TI_REF) ||
varDsc->lvVerTypeInfo.IsUnboxedGenericTypeVar()))
{
BADCODE("parameter type mismatch");
}
}
// Disallow byrefs to byref like objects (ArgTypeHandle)
// techncally we could get away with just not setting them
if (varDsc->lvVerTypeInfo.IsByRef() && verIsByRefLike(DereferenceByRef(varDsc->lvVerTypeInfo)))
varDsc->lvVerTypeInfo = typeInfo();
// we don't want the EE to assert in lvaSetStruct on bad sigs, so change
// the JIT type to avoid even trying to call back
if (varTypeIsStruct(type) && varDsc->lvVerTypeInfo.IsDead())
type = TYP_VOID;
}
if (typeHnd)
{
unsigned cFlags = info.compCompHnd->getClassAttribs(typeHnd);
// We can get typeHnds for primitive types, these are value types which only contain
// a primitive. We will need the typeHnd to distinguish them, so we store it here.
if ((cFlags & CORINFO_FLG_VALUECLASS) &&
!varTypeIsStruct(type))
{
if (tiVerificationNeeded == false)
{
// printf("This is a struct that the JIT will treat as a primitive\n");
varDsc->lvVerTypeInfo = verMakeTypeInfo(typeHnd);
}
}
varDsc->lvOverlappingFields = StructHasOverlappingFields(cFlags);
}
if (varTypeIsGC(type))
varDsc->lvStructGcCount = 1;
// Set the lvType (before this point it is TYP_UNDEF).
if ((varTypeIsStruct(type)))
{
lvaSetStruct(varNum, typeHnd, typeHnd!=NULL, !tiVerificationNeeded);
}
else
{
varDsc->lvType = type;
}
#if OPT_BOOL_OPS
if (type == TYP_BOOL)
varDsc->lvIsBoolean = true;
#endif
#ifdef DEBUG
varDsc->lvStkOffs = BAD_STK_OFFS;
#endif
}
/*****************************************************************************
* Returns our internal varNum for a given IL variable.
* Asserts assume it is called after lvaTable[] has been set up.
*/
unsigned Compiler::compMapILvarNum(unsigned ILvarNum)
{
noway_assert(ILvarNum < info.compILlocalsCount ||
ILvarNum > unsigned(ICorDebugInfo::UNKNOWN_ILNUM));
unsigned varNum;
if (ILvarNum == (unsigned)ICorDebugInfo::VARARGS_HND_ILNUM)
{
// The varargs cookie is the last argument in lvaTable[]
noway_assert(info.compIsVarArgs);
varNum = lvaVarargsHandleArg;
noway_assert(lvaTable[varNum].lvIsParam);
}
else if (ILvarNum == (unsigned)ICorDebugInfo::RETBUF_ILNUM)
{
noway_assert(info.compRetBuffArg != BAD_VAR_NUM);
varNum = info.compRetBuffArg;
}
else if (ILvarNum == (unsigned)ICorDebugInfo::TYPECTXT_ILNUM)
{
noway_assert(info.compTypeCtxtArg >= 0);
varNum = unsigned(info.compTypeCtxtArg);
}
else if (ILvarNum < info.compILargsCount)
{
// Parameter
varNum = compMapILargNum(ILvarNum);
noway_assert(lvaTable[varNum].lvIsParam);
}
else if (ILvarNum < info.compILlocalsCount)
{
// Local variable
unsigned lclNum = ILvarNum - info.compILargsCount;
varNum = info.compArgsCount + lclNum;
noway_assert(!lvaTable[varNum].lvIsParam);
}
else
{
unreached();
}
noway_assert(varNum < info.compLocalsCount);
return varNum;
}
/*****************************************************************************
* Returns the IL variable number given our internal varNum.
* Special return values are VARG_ILNUM, RETBUF_ILNUM, TYPECTXT_ILNUM.
*
* Returns UNKNOWN_ILNUM if it can't be mapped.
*/
unsigned Compiler::compMap2ILvarNum(unsigned varNum)
{
if (compIsForInlining())
{
return impInlineInfo->InlinerCompiler->compMap2ILvarNum(varNum);
}
noway_assert(varNum < lvaCount);
if (varNum == info.compRetBuffArg)
return (unsigned)ICorDebugInfo::RETBUF_ILNUM;
// Is this a varargs function?
if (info.compIsVarArgs && varNum == lvaVarargsHandleArg)
return (unsigned)ICorDebugInfo::VARARGS_HND_ILNUM;
// We create an extra argument for the type context parameter
// needed for shared generic code.
if ((info.compMethodInfo->args.callConv & CORINFO_CALLCONV_PARAMTYPE) &&
varNum == (unsigned)info.compTypeCtxtArg)
return (unsigned)ICorDebugInfo::TYPECTXT_ILNUM;
// Now mutate varNum to remove extra parameters from the count.
if ((info.compMethodInfo->args.callConv & CORINFO_CALLCONV_PARAMTYPE) &&
varNum > (unsigned)info.compTypeCtxtArg)
varNum--;
if (info.compIsVarArgs && varNum > lvaVarargsHandleArg)
varNum--;
/* Is there a hidden argument for the return buffer.
Note that this code works because if the RetBuffArg is not present,
compRetBuffArg will be BAD_VAR_NUM */
if (info.compRetBuffArg != BAD_VAR_NUM && varNum > info.compRetBuffArg)
varNum--;
if (varNum >= info.compLocalsCount)
return (unsigned)ICorDebugInfo::UNKNOWN_ILNUM; // Cannot be mapped
return varNum;
}
/*****************************************************************************
* Returns true if variable "varNum" may be address-exposed.
*/
bool Compiler::lvaVarAddrExposed(unsigned varNum)
{
noway_assert(varNum < lvaCount);
LclVarDsc * varDsc = &lvaTable[varNum];
return varDsc->lvAddrExposed;
}
/*****************************************************************************
* Returns true iff variable "varNum" should not be enregistered (or one of several reasons).
*/
bool Compiler::lvaVarDoNotEnregister(unsigned varNum)
{
noway_assert(varNum < lvaCount);
LclVarDsc * varDsc = &lvaTable[varNum];
return varDsc->lvDoNotEnregister;
}
/*****************************************************************************
* Returns the handle to the class of the local variable varNum
*/
CORINFO_CLASS_HANDLE Compiler::lvaGetStruct(unsigned varNum)
{
noway_assert(varNum < lvaCount);
LclVarDsc * varDsc = &lvaTable[varNum];
return varDsc->lvVerTypeInfo.GetClassHandleForValueClass();
}
/*****************************************************************************
*
* Compare function passed to qsort() by Compiler::lvaCanPromoteStructVar().
*/
/* static */
int __cdecl Compiler::lvaFieldOffsetCmp(const void * field1, const void * field2)
{
lvaStructFieldInfo * pFieldInfo1 = (lvaStructFieldInfo *)field1;
lvaStructFieldInfo * pFieldInfo2 = (lvaStructFieldInfo *)field2;
if (pFieldInfo1->fldOffset == pFieldInfo2->fldOffset)
{
return 0;
}
else
{
return (pFieldInfo1->fldOffset > pFieldInfo2->fldOffset) ? +1 : -1;
}
}
/*****************************************************************************
* Is this type promotable? */
void Compiler::lvaCanPromoteStructType(CORINFO_CLASS_HANDLE typeHnd,
lvaStructPromotionInfo * StructPromotionInfo,
bool sortFields)
{
assert(eeIsValueClass(typeHnd));
if (typeHnd != StructPromotionInfo->typeHnd)
{
// sizeof(double) represents the size of the largest primitive type that we can struct promote
// In the future this may be changing to XMM_REGSIZE_BYTES
const int MaxOffset = MAX_NumOfFieldsInPromotableStruct * sizeof(double); // must be a compile time constant
assert((BYTE)MaxOffset == MaxOffset); // because lvaStructFieldInfo.fldOffset is byte-sized
assert((BYTE)MAX_NumOfFieldsInPromotableStruct == MAX_NumOfFieldsInPromotableStruct); // because lvaStructFieldInfo.fieldCnt is byte-sized
bool requiresScratchVar = false;
bool containsHoles = false;
bool customLayout = false;
bool containsGCpointers = false;
StructPromotionInfo->typeHnd = typeHnd;
StructPromotionInfo->canPromote = false;
unsigned structSize = info.compCompHnd->getClassSize(typeHnd);
if (structSize >= MaxOffset)
{
return; // struct is too large
}
unsigned fieldCnt = info.compCompHnd->getClassNumInstanceFields(typeHnd);
if (fieldCnt == 0 ||
fieldCnt > MAX_NumOfFieldsInPromotableStruct)
{
return; // struct must have between 1 and MAX_NumOfFieldsInPromotableStruct fields
}
StructPromotionInfo->fieldCnt = (BYTE)fieldCnt;
DWORD typeFlags = info.compCompHnd->getClassAttribs(typeHnd);
bool treatAsOverlapping = StructHasOverlappingFields(typeFlags);
#if 1 // TODO-Cleanup: Consider removing this entire #if block in the future
// This method has two callers. The one in Importer.cpp passes sortFields == false
// and the other passes sortFields == true.
// This is a workaround that leave the inlining behavior the same and before while still
// performing extra struct promotions when compiling the method.
//
if (!sortFields) // the condition "!sortFields" really means "we are inlining"
{
treatAsOverlapping = StructHasCustomLayout(typeFlags);
}
#endif
if (treatAsOverlapping)
{
return;
}
#ifdef _TARGET_ARM_
// For ARM don't struct promote if we have an CUSTOMLAYOUT flag on an HFA type
if (StructHasCustomLayout(typeFlags) && IsHfa(typeHnd))
{
return;
}
// On ARM, we have a requirement on the struct alignment; see below.
unsigned structAlignment = roundUp(info.compCompHnd->getClassAlignmentRequirement(typeHnd), TARGET_POINTER_SIZE);
#endif // _TARGET_ARM
bool isHole[MaxOffset]; // isHole[] is initialized to true for every valid offset in the struct and false for the rest
unsigned i; // then as we process the fields we clear the isHole[] values that the field spans.
for (i=0; i < MaxOffset; i++)
{
isHole[i] = (i < structSize) ? true : false;
}
for (BYTE ordinal=0;
ordinal < fieldCnt;
++ordinal)
{
lvaStructFieldInfo * pFieldInfo = &StructPromotionInfo->fields[ordinal];
pFieldInfo->fldHnd = info.compCompHnd->getFieldInClass(typeHnd, ordinal);
unsigned fldOffset = info.compCompHnd->getFieldOffset(pFieldInfo->fldHnd);
// The fldOffset value should never be larger than our structSize.
if (fldOffset >= structSize)
{
noway_assert(false);
return;
}
pFieldInfo->fldOffset = (BYTE)fldOffset;
pFieldInfo->fldOrdinal = ordinal;
CorInfoType corType = info.compCompHnd->getFieldType(pFieldInfo->fldHnd, &pFieldInfo->fldTypeHnd);
var_types varType = JITtype2varType(corType);
pFieldInfo->fldType = varType;
pFieldInfo->fldSize = genTypeSize(varType);
if (varTypeIsGC(varType))
{
containsGCpointers = true;
}
if (pFieldInfo->fldSize == 0)
{
// Non-primitive struct field. Don't promote.
return;
}
if ((pFieldInfo->fldOffset % pFieldInfo->fldSize) != 0)
{
// The code in Compiler::genPushArgList that reconstitutes
// struct values on the stack from promoted fields expects
// those fields to be at their natural alignment.
return;
}
// The end offset for this field should never be larger than our structSize.
noway_assert(fldOffset + pFieldInfo->fldSize <= structSize);
for (i=0; i < pFieldInfo->fldSize; i++)
{
isHole[fldOffset+i] = false;
}
#ifdef _TARGET_ARM_
// On ARM, for struct types that don't use explicit layout, the alignment of the struct is
// at least the max alignment of its fields. We take advantage of this invariant in struct promotion,
// so verify it here.
if (pFieldInfo->fldSize > structAlignment)
{
// Don't promote vars whose struct types violates the invariant. (Alignment == size for primitives.)
return;
}
// If we have any small fields we will allocate a single PromotedStructScratch local var for the method.
// This is a stack area that we use to assemble the small fields in order to place them in a register argument.
//
if (pFieldInfo->fldSize < TARGET_POINTER_SIZE)
{
requiresScratchVar = true;
}
#endif // _TARGET_ARM_
}
// If we saw any GC pointer fields above then the CORINFO_FLG_CONTAINS_GC_PTR has to be set!
noway_assert((containsGCpointers == false) || ((typeFlags & CORINFO_FLG_CONTAINS_GC_PTR) != 0));
// If we have "Custom Layout" then we might have an explicit Size attribute
// Managed C++ uses this for its structs, such C++ types will not contain GC pointers.
//
// The current VM implementation also incorrectly sets the CORINFO_FLG_CUSTOMLAYOUT
// whenever a managed value class contains any GC pointers.
// (See the comment for VMFLAG_NOT_TIGHTLY_PACKED in class.h)
//
// It is important to struct promote managed value classes that have GC pointers
// So we compute the correct value for "CustomLayout" here
//
if (StructHasCustomLayout(typeFlags) &&
((typeFlags & CORINFO_FLG_CONTAINS_GC_PTR) == 0) )
{
customLayout = true;
}
// Check if this promoted struct contains any holes
//
for (i=0; i < structSize; i++)
{
if (isHole[i])
{
containsHoles = true;
break;
}
}
// Cool, this struct is promotable.
StructPromotionInfo->canPromote = true;
StructPromotionInfo->requiresScratchVar = requiresScratchVar;
StructPromotionInfo->containsHoles = containsHoles;
StructPromotionInfo->customLayout = customLayout;
if (sortFields)
{
// Sort the fields according to the increasing order of the field offset.
// This is needed because the fields need to be pushed on stack (for GT_LDOBJ) in order.
qsort(StructPromotionInfo->fields,
StructPromotionInfo->fieldCnt,
sizeof(*StructPromotionInfo->fields),
lvaFieldOffsetCmp);
}
}
else
{
// Asking for the same type of struct as the last time.
// Nothing need to be done.
// Fall through ...
}
}
/*****************************************************************************
* Is this struct type local variable promotable? */
void Compiler::lvaCanPromoteStructVar(unsigned lclNum, lvaStructPromotionInfo * StructPromotionInfo)
{
noway_assert(lclNum < lvaCount);
LclVarDsc * varDsc = &lvaTable[lclNum];
noway_assert(varTypeIsStruct(varDsc));
noway_assert(!varDsc->lvPromoted); // Don't ask again :)
#ifdef FEATURE_SIMD
// If this lclVar is used in a SIMD intrinsic, then we don't want to struct promote it.
// Note, however, that SIMD lclVars that are NOT used in a SIMD intrinsic may be
// profitably promoted.
if (varDsc->lvIsUsedInSIMDIntrinsic())
{
StructPromotionInfo->canPromote = false;
return;
}
#endif
#ifdef _TARGET_ARM_
// Explicitly check for HFA reg args and reject them for promotion here.
// Promoting HFA args will fire an assert in lvaAssignFrameOffsets
// when the HFA reg arg is struct promoted.
//
if (varDsc->lvIsHfaRegArg)
{
StructPromotionInfo->canPromote = false;
return;
}
#endif
CORINFO_CLASS_HANDLE typeHnd = varDsc->lvVerTypeInfo.GetClassHandle();
lvaCanPromoteStructType(typeHnd, StructPromotionInfo, true);
}
/*****************************************************************************
* Promote a struct type local */
void Compiler::lvaPromoteStructVar(unsigned lclNum, lvaStructPromotionInfo * StructPromotionInfo)
{
LclVarDsc * varDsc = &lvaTable[lclNum];
// We should never see a reg-sized non-field-addressed struct here.
noway_assert(!varDsc->lvRegStruct);
noway_assert(StructPromotionInfo->canPromote);
noway_assert(StructPromotionInfo->typeHnd == varDsc->lvVerTypeInfo.GetClassHandle());
varDsc->lvFieldCnt = StructPromotionInfo->fieldCnt;
varDsc->lvFieldLclStart = lvaCount;
varDsc->lvPromoted = true;
varDsc->lvContainsHoles = StructPromotionInfo->containsHoles;
varDsc->lvCustomLayout = StructPromotionInfo->customLayout;
#ifdef DEBUG
//Don't change the source to a TYP_BLK either.
varDsc->lvKeepType = 1;
#endif
#ifdef DEBUG
if (verbose)
{
printf("\nPromoting struct local V%02u (%s):",
lclNum, eeGetClassName(StructPromotionInfo->typeHnd));
}
#endif
for (unsigned index=0;
index<StructPromotionInfo->fieldCnt;
++index)
{
lvaStructFieldInfo * pFieldInfo = &StructPromotionInfo->fields[index];
if (varTypeIsFloating(pFieldInfo->fldType))
{
lvaTable[lclNum].lvContainsFloatingFields = 1;
// Whenever we promote a struct that contains a floating point field
// it's possible we transition from a method that originally only had integer
// local vars to start having FP. We have to communicate this through this flag
// since LSRA later on will use this flag to determine whether or not to track FP register sets.
compFloatingPointUsed = true;
}
// Now grab the temp for the field local.
#ifdef DEBUG
char buf[200];
char * bufp = &buf[0];
sprintf_s(bufp, sizeof(buf), "%s V%02u.%s (fldOffset=0x%x)",
"field",
lclNum,
eeGetFieldName(pFieldInfo->fldHnd),
pFieldInfo->fldOffset);
if (index>0)
{
noway_assert(pFieldInfo->fldOffset > (pFieldInfo-1)->fldOffset);
}
#endif
unsigned varNum = lvaGrabTemp(false DEBUGARG(bufp)); // Lifetime of field locals might span multiple BBs, so they are long lifetime temps.
LclVarDsc * fieldVarDsc = &lvaTable[varNum];
fieldVarDsc->lvType = pFieldInfo->fldType;
fieldVarDsc->lvExactSize = pFieldInfo->fldSize;
fieldVarDsc->lvIsStructField = true;
fieldVarDsc->lvFldOffset = pFieldInfo->fldOffset;
fieldVarDsc->lvFldOrdinal = pFieldInfo->fldOrdinal;
fieldVarDsc->lvParentLcl = lclNum;
fieldVarDsc->lvIsParam = varDsc->lvIsParam;
#if defined(_TARGET_AMD64_) || defined(_TARGET_ARM64_)
// Do we have a parameter that can be enregistered?
//
if (varDsc->lvIsRegArg)
{
fieldVarDsc->lvIsRegArg = true;
fieldVarDsc->lvArgReg = varDsc->lvArgReg;
fieldVarDsc->setPrefReg(varDsc->lvArgReg, this); // Set the preferred register
lvaMarkRefsWeight = BB_UNITY_WEIGHT; // incRefCnts can use this compiler global variable
fieldVarDsc->incRefCnts(BB_UNITY_WEIGHT, this); // increment the ref count for prolog initialization
}
#endif
#ifdef DEBUG
// This temporary should not be converted to a double in stress mode,
// because we introduce assigns to it after the stress conversion
fieldVarDsc->lvKeepType = 1;
#endif
}
}
#if !defined(_TARGET_64BIT_)
//------------------------------------------------------------------------
// lvaPromoteLongVars: "Struct promote" all register candidate longs as if they are structs of two ints.
//
// Arguments:
// None.
//
// Return Value:
// None.
//
void Compiler::lvaPromoteLongVars()
{
if ((opts.compFlags & CLFLG_REGVAR) == 0)
{
return;
}
// The lvaTable might grow as we grab temps. Make a local copy here.
unsigned startLvaCount = lvaCount;
for (unsigned lclNum = 0;
lclNum < startLvaCount;
lclNum++)
{
LclVarDsc * varDsc = &lvaTable[lclNum];
if(!varTypeIsLong(varDsc) || varDsc->lvDoNotEnregister || (varDsc->lvRefCnt == 0))
{
continue;
}
// Will this work ???
// We can't have nested promoted structs.
if (varDsc->lvIsStructField)
{
if (lvaGetPromotionType(varDsc->lvParentLcl) != PROMOTION_TYPE_INDEPENDENT)
{
continue;
}
varDsc->lvIsStructField = false;
varDsc->lvTracked = false;
}
varDsc->lvFieldCnt = 2;
varDsc->lvFieldLclStart = lvaCount;
varDsc->lvPromoted = true;
varDsc->lvContainsHoles = false;
#ifdef DEBUG
if (verbose)
{
printf("\nPromoting long local V%02u:", lclNum);
}
#endif
bool isParam = varDsc->lvIsParam;
for (unsigned index=0; index < 2; ++index)
{
// Grab the temp for the field local.
#ifdef DEBUG
char buf[200];
char * bufp = &buf[0];
sprintf_s(bufp, sizeof(buf), "%s V%02u.%s (fldOffset=0x%x)",
"field",
lclNum,
index == 0 ? "lo" : "hi",
index * 4);
#endif
unsigned varNum = lvaGrabTemp(false DEBUGARG(bufp)); // Lifetime of field locals might span multiple BBs, so they are long lifetime temps.
LclVarDsc * fieldVarDsc = &lvaTable[varNum];
fieldVarDsc->lvType = TYP_INT;
fieldVarDsc->lvExactSize = genTypeSize(TYP_INT);
fieldVarDsc->lvIsStructField = true;
fieldVarDsc->lvFldOffset = (unsigned char)(index * genTypeSize(TYP_INT));
fieldVarDsc->lvFldOrdinal = (unsigned char)index;
fieldVarDsc->lvParentLcl = lclNum;
fieldVarDsc->lvIsParam = isParam;
}
}
}
#endif // !_TARGET_64BIT_
/*****************************************************************************
* Given a fldOffset in a promoted struct var, return the index of the local
that represents this field.
*/
unsigned Compiler::lvaGetFieldLocal(LclVarDsc * varDsc, unsigned int fldOffset)
{
noway_assert(varTypeIsStruct(varDsc));
noway_assert(varDsc->lvPromoted);
for (unsigned i = varDsc->lvFieldLclStart;
i < varDsc->lvFieldLclStart + varDsc->lvFieldCnt;
++i)
{
noway_assert(lvaTable[i].lvIsStructField);
noway_assert(lvaTable[i].lvParentLcl == (unsigned) (varDsc-lvaTable));
if (lvaTable[i].lvFldOffset == fldOffset)
{
return i;
}
}
noway_assert(!"Cannot find field local.");
return BAD_VAR_NUM;
}
/*****************************************************************************
*
* Set the local var "varNum" as address-exposed.
* If this is a promoted struct, label it's fields the same way.
*/
void Compiler::lvaSetVarAddrExposed(unsigned varNum)
{
noway_assert(varNum < lvaCount);
LclVarDsc * varDsc = &lvaTable[varNum];
varDsc->lvAddrExposed = 1;
if (varDsc->lvPromoted)
{
noway_assert(varTypeIsStruct(varDsc));
for (unsigned i = varDsc->lvFieldLclStart;
i < varDsc->lvFieldLclStart + varDsc->lvFieldCnt;
++i)
{
noway_assert(lvaTable[i].lvIsStructField);
lvaTable[i].lvAddrExposed = 1; // Make field local as address-exposed.
lvaSetVarDoNotEnregister(i DEBUG_ARG(DNER_AddrExposed));
}
}
lvaSetVarDoNotEnregister(varNum DEBUG_ARG(DNER_AddrExposed));
}
/*****************************************************************************
*
* Record that the local var "varNum" should not be enregistered (for one of several reasons.)
*/
void Compiler::lvaSetVarDoNotEnregister(unsigned varNum DEBUG_ARG(DoNotEnregisterReason reason))
{
noway_assert(varNum < lvaCount);
LclVarDsc * varDsc = &lvaTable[varNum];
varDsc->lvDoNotEnregister = 1;
#ifdef DEBUG
if (verbose)
{
printf("\nLocal V%02u should not be enregistered because: ", varNum);
}
switch (reason)
{
case DNER_AddrExposed:
JITDUMP("it is address exposed\n");
assert(varDsc->lvAddrExposed);
break;
case DNER_IsStruct:
JITDUMP("it is a struct\n");
assert(varTypeIsStruct(varDsc));
break;
case DNER_BlockOp:
JITDUMP("written in a block op\n");
varDsc->lvLclBlockOpAddr = 1;
break;
case DNER_LocalField:
JITDUMP("was accessed as a local field\n");
varDsc->lvLclFieldExpr = 1;
break;
case DNER_VMNeedsStackAddr:
JITDUMP("needs stack addr\n");
varDsc->lvVMNeedsStackAddr = 1;
break;
case DNER_LiveInOutOfHandler:
JITDUMP("live in/out of a handler\n");
varDsc->lvLiveInOutOfHndlr = 1;
break;
case DNER_LiveAcrossUnmanagedCall:
JITDUMP("live across unmanaged call\n");
varDsc->lvLiveAcrossUCall = 1;
break;
#ifdef JIT32_GCENCODER
case DNER_PinningRef:
JITDUMP("pinning ref\n");
assert(varDsc->lvPinned);
break;
#endif
default:
unreached();
break;
}
#endif
}
/*****************************************************************************
* Set the lvClass for a local variable of a struct type */
void Compiler::lvaSetStruct(unsigned varNum, CORINFO_CLASS_HANDLE typeHnd, bool unsafeValueClsCheck, bool setTypeInfo)
{
noway_assert(varNum < lvaCount);
LclVarDsc * varDsc = &lvaTable[varNum];
if (setTypeInfo)
varDsc->lvVerTypeInfo = typeInfo(TI_STRUCT, typeHnd);
// Set the type and associated info if we haven't already set it.
var_types structType = varDsc->lvType;
if (varDsc->lvType == TYP_UNDEF)
{
varDsc->lvType = TYP_STRUCT;
}
if (varDsc->lvExactSize == 0)
{
varDsc->lvExactSize = info.compCompHnd->getClassSize(typeHnd);
size_t lvSize = varDsc->lvSize();
assert((lvSize % sizeof(void*)) == 0); // The struct needs to be a multiple of sizeof(void*) bytes for getClassGClayout() to be valid.
varDsc->lvGcLayout = (BYTE*)compGetMemA((lvSize / sizeof(void*)) * sizeof(BYTE), CMK_LvaTable);
unsigned numGCVars;
var_types simdBaseType = TYP_UNKNOWN;
varDsc->lvType = impNormStructType(typeHnd, varDsc->lvGcLayout, &numGCVars, &simdBaseType);
// We only save the count of GC vars in a struct up to 7.
if (numGCVars >= 8)
numGCVars = 7;
varDsc->lvStructGcCount = numGCVars;
#if FEATURE_SIMD
if (simdBaseType != TYP_UNKNOWN)
{
assert(varTypeIsSIMD(varDsc));
varDsc->lvSIMDType = true;
varDsc->lvBaseType = simdBaseType;
}
#endif // FEATURE_SIMD
}
else
{
assert(varDsc->lvExactSize != 0);
#if FEATURE_SIMD
assert(!varTypeIsSIMD(varDsc) || (varDsc->lvBaseType != TYP_UNKNOWN));
#endif // FEATURE_SIMD
}
#ifndef _TARGET_64BIT_
bool fDoubleAlignHint = FALSE;
# ifdef _TARGET_X86_
fDoubleAlignHint = TRUE;
# endif
if (info.compCompHnd->getClassAlignmentRequirement(typeHnd, fDoubleAlignHint) == 8)
{
#ifdef DEBUG
if (verbose)
{
printf("Marking struct in V%02i with double align flag\n", varNum);
}
#endif
varDsc->lvStructDoubleAlign = 1;
}
#endif // not _TARGET_64BIT_
unsigned classAttribs = info.compCompHnd->getClassAttribs(typeHnd);
varDsc->lvOverlappingFields = StructHasOverlappingFields(classAttribs);
// Check whether this local is an unsafe value type and requires GS cookie protection.
// GS checks require the stack to be re-ordered, which can't be done with EnC.
if (unsafeValueClsCheck &&
(classAttribs & CORINFO_FLG_UNSAFE_VALUECLASS) &&
!opts.compDbgEnC)
{
setNeedsGSSecurityCookie();
compGSReorderStackLayout = true;
varDsc->lvIsUnsafeBuffer = true;
}
}
/*****************************************************************************
* Returns the array of BYTEs containing the GC layout information
*/
BYTE * Compiler::lvaGetGcLayout(unsigned varNum)
{
noway_assert(varTypeIsStruct(lvaTable[varNum].lvType) && (lvaTable[varNum].lvExactSize >= TARGET_POINTER_SIZE));
return lvaTable[varNum].lvGcLayout;
}
/*****************************************************************************
* Return the number of bytes needed for a local variable
*/
unsigned Compiler::lvaLclSize(unsigned varNum)
{
noway_assert(varNum < lvaCount);
var_types varType = lvaTable[varNum].TypeGet();
switch (varType)
{
case TYP_STRUCT:
case TYP_BLK:
return lvaTable[varNum].lvSize();
case TYP_LCLBLK:
#if FEATURE_FIXED_OUT_ARGS
noway_assert(lvaOutgoingArgSpaceSize >= 0);
noway_assert(varNum == lvaOutgoingArgSpaceVar);
return lvaOutgoingArgSpaceSize;
#else // FEATURE_FIXED_OUT_ARGS
assert(!"Unknown size");
NO_WAY("Target doesn't support TYP_LCLBLK");
// Keep prefast happy
__fallthrough;
#endif // FEATURE_FIXED_OUT_ARGS
default: // This must be a primitive var. Fall out of switch statement
break;
}
// We only need this Quirk for _TARGET_64BIT_
#ifdef _TARGET_64BIT_
if (lvaTable[varNum].lvQuirkToLong)
{
noway_assert(lvaTable[varNum].lvAddrExposed);
return genTypeStSz(TYP_LONG)*sizeof(int); // return 8 (2 * 4)
}
#endif
return genTypeStSz(varType)*sizeof(int);
}
//
// Return the exact width of local variable "varNum" -- the number of bytes
// you'd need to copy in order to overwrite the value.
//
unsigned Compiler::lvaLclExactSize(unsigned varNum)
{
noway_assert(varNum < lvaCount);
var_types varType = lvaTable[varNum].TypeGet();
switch (varType)
{
case TYP_STRUCT:
case TYP_BLK:
return lvaTable[varNum].lvExactSize;
case TYP_LCLBLK:
#if FEATURE_FIXED_OUT_ARGS
noway_assert(lvaOutgoingArgSpaceSize >= 0);
noway_assert(varNum == lvaOutgoingArgSpaceVar);
return lvaOutgoingArgSpaceSize;
#else // FEATURE_FIXED_OUT_ARGS
assert(!"Unknown size");
NO_WAY("Target doesn't support TYP_LCLBLK");
// Keep prefast happy
__fallthrough;
#endif // FEATURE_FIXED_OUT_ARGS
default: // This must be a primitive var. Fall out of switch statement
break;
}
return genTypeSize(varType);
}
//getBBWeight -- get the normalized weight of this block
unsigned BasicBlock::getBBWeight(Compiler * comp)
{
if (this->bbWeight == 0)
return 0;
else
{
unsigned calledWeight = comp->fgCalledWeight;
if (calledWeight == 0)
{
calledWeight = comp->fgFirstBB->bbWeight;
if (calledWeight == 0)
calledWeight = BB_UNITY_WEIGHT;
}
if (this->bbWeight < (BB_MAX_WEIGHT / BB_UNITY_WEIGHT))
return max(1, (((this->bbWeight * BB_UNITY_WEIGHT) + (calledWeight/2)) / calledWeight));
else
return (unsigned) ((((double)this->bbWeight * (double)BB_UNITY_WEIGHT) / (double)calledWeight) + 0.5);
}
}
/*****************************************************************************
*
* Callback used by the tree walker to call lvaDecRefCnts
*/
Compiler::fgWalkResult Compiler::lvaDecRefCntsCB(GenTreePtr *pTree, fgWalkData *data)
{
data->compiler->lvaDecRefCnts(*pTree);
return WALK_CONTINUE;
}
// Decrement the ref counts for all locals contained in the tree and its children.
void Compiler::lvaRecursiveDecRefCounts(GenTreePtr tree)
{
assert(lvaLocalVarRefCounted);
// We could just use the recursive walker for all cases but that is a
// fairly heavyweight thing to spin up when we're usually just handling a leaf.
if (tree->OperIsLeaf())
{
if (tree->OperIsLocal())
{
lvaDecRefCnts(tree);
}
}
else
{
fgWalkTreePre(&tree, Compiler::lvaDecRefCntsCB, (void *)this, true);
}
}
// Increment the ref counts for all locals contained in the tree and its children.
void Compiler::lvaRecursiveIncRefCounts(GenTreePtr tree)
{
assert(lvaLocalVarRefCounted);
// We could just use the recursive walker for all cases but that is a
// fairly heavyweight thing to spin up when we're usually just handling a leaf.
if (tree->OperIsLeaf())
{
if (tree->OperIsLocal())
{
lvaIncRefCnts(tree);
}
}
else
{
fgWalkTreePre(&tree, Compiler::lvaIncRefCntsCB, (void *)this, true);
}
}
/*****************************************************************************
*
* Helper passed to the tree walker to decrement the refCnts for
* all local variables in an expression
*/
void Compiler::lvaDecRefCnts(GenTreePtr tree)
{
unsigned lclNum;
LclVarDsc * varDsc;
noway_assert(lvaRefCountingStarted || lvaLocalVarRefCounted);
if ((tree->gtOper == GT_CALL) && (tree->gtFlags & GTF_CALL_UNMANAGED))
{
assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
if (!opts.ShouldUsePInvokeHelpers())
{
/* Get the special variable descriptor */
lclNum = info.compLvFrameListRoot;
noway_assert(lclNum <= lvaCount);
varDsc = lvaTable + lclNum;
/* Decrement the reference counts twice */
varDsc->decRefCnts(compCurBB->getBBWeight(this), this);
varDsc->decRefCnts(compCurBB->getBBWeight(this), this);
}
}
else
{
/* This must be a local variable */
noway_assert(tree->OperIsLocal());
/* Get the variable descriptor */
lclNum = tree->gtLclVarCommon.gtLclNum;
noway_assert(lclNum < lvaCount);
varDsc = lvaTable + lclNum;
/* Decrement its lvRefCnt and lvRefCntWtd */
varDsc->decRefCnts(compCurBB->getBBWeight(this), this);
}
}
/*****************************************************************************
*
* Callback used by the tree walker to call lvaIncRefCnts
*/
Compiler::fgWalkResult Compiler::lvaIncRefCntsCB(GenTreePtr *pTree, fgWalkData *data)
{
data->compiler->lvaIncRefCnts(*pTree);
return WALK_CONTINUE;
}
/*****************************************************************************
*
* Helper passed to the tree walker to increment the refCnts for
* all local variables in an expression
*/
void Compiler::lvaIncRefCnts(GenTreePtr tree)
{
unsigned lclNum;
LclVarDsc * varDsc;
noway_assert(lvaRefCountingStarted || lvaLocalVarRefCounted);
if ((tree->gtOper == GT_CALL) && (tree->gtFlags & GTF_CALL_UNMANAGED))
{
assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
if (!opts.ShouldUsePInvokeHelpers())
{
/* Get the special variable descriptor */
lclNum = info.compLvFrameListRoot;
noway_assert(lclNum <= lvaCount);
varDsc = lvaTable + lclNum;
/* Increment the reference counts twice */
varDsc->incRefCnts(compCurBB->getBBWeight(this), this);
varDsc->incRefCnts(compCurBB->getBBWeight(this), this);
}
}
else
{
/* This must be a local variable */
noway_assert(tree->gtOper == GT_LCL_VAR || tree->gtOper == GT_LCL_FLD || tree->gtOper == GT_STORE_LCL_VAR || tree->gtOper == GT_STORE_LCL_FLD);
/* Get the variable descriptor */
lclNum = tree->gtLclVarCommon.gtLclNum;
noway_assert(lclNum < lvaCount);
varDsc = lvaTable + lclNum;
/* Increment its lvRefCnt and lvRefCntWtd */
varDsc->incRefCnts(compCurBB->getBBWeight(this), this);
}
}
/*****************************************************************************
*
* Compare function passed to qsort() by Compiler::lclVars.lvaSortByRefCount().
* when generating SMALL_CODE.
* Return positive if dsc2 has a higher ref count
* Return negative if dsc1 has a higher ref count
* Return zero if the ref counts are the same
* lvPrefReg is only used to break ties
*/
/* static */
int __cdecl Compiler::RefCntCmp(const void *op1, const void *op2)
{
LclVarDsc * dsc1 = *(LclVarDsc * *)op1;
LclVarDsc * dsc2 = *(LclVarDsc * *)op2;
/* Make sure we preference tracked variables over untracked variables */
if (dsc1->lvTracked != dsc2->lvTracked)
{
return (dsc2->lvTracked) ? +1 : -1;
}
unsigned weight1 = dsc1->lvRefCnt;
unsigned weight2 = dsc2->lvRefCnt;
#if !FEATURE_FP_REGALLOC
/* Force integer candidates to sort above float candidates */
bool isFloat1 = isFloatRegType(dsc1->lvType);
bool isFloat2 = isFloatRegType(dsc2->lvType);
if (isFloat1 != isFloat2)
{
if (weight2 && isFloat1)
return +1;
if (weight1 && isFloat2)
return -1;
}
#endif
int diff = weight2 - weight1;
if (diff != 0)
return diff;
/* The unweighted ref counts were the same */
/* If the weighted ref counts are different then use their difference */
diff = dsc2->lvRefCntWtd - dsc1->lvRefCntWtd;
if (diff != 0)
return diff;
/* We have equal ref counts and weighted ref counts */
/* Break the tie by: */
/* Increasing the weight by 2 if we have exactly one bit set in lvPrefReg */
/* Increasing the weight by 1 if we have more than one bit set in lvPrefReg */
/* Increasing the weight by 0.5 if we are a GC type */
/* Increasing the weight by 0.5 if we were enregistered in the previous pass */
if (weight1)
{
if (dsc1->lvPrefReg)
{
if ( (dsc1->lvPrefReg & ~RBM_BYTE_REG_FLAG) && genMaxOneBit((unsigned)dsc1->lvPrefReg))
weight1 += 2 * BB_UNITY_WEIGHT;
else
weight1 += 1 * BB_UNITY_WEIGHT;
}
if (varTypeIsGC(dsc1->TypeGet()))
weight1 += BB_UNITY_WEIGHT / 2;
if (dsc1->lvRegister)
weight1 += BB_UNITY_WEIGHT / 2;
}
if (weight2)
{
if (dsc2->lvPrefReg)
{
if ( (dsc2->lvPrefReg & ~RBM_BYTE_REG_FLAG) && genMaxOneBit((unsigned)dsc2->lvPrefReg))
weight2 += 2 * BB_UNITY_WEIGHT;
else
weight2 += 1 * BB_UNITY_WEIGHT;
}
if (varTypeIsGC(dsc2->TypeGet()))
weight1 += BB_UNITY_WEIGHT / 2;
if (dsc2->lvRegister)
weight2 += BB_UNITY_WEIGHT / 2;
}
diff = weight2 - weight1;
if (diff != 0)
return diff;
/* To achieve a Stable Sort we use the LclNum (by way of the pointer address) */
if (dsc1 < dsc2)
return -1;
if (dsc1 > dsc2)
return +1;
return 0;
}
/*****************************************************************************
*
* Compare function passed to qsort() by Compiler::lclVars.lvaSortByRefCount().
* when not generating SMALL_CODE.
* Return positive if dsc2 has a higher weighted ref count
* Return negative if dsc1 has a higher weighted ref count
* Return zero if the ref counts are the same
*/
/* static */
int __cdecl Compiler::WtdRefCntCmp(const void *op1, const void *op2)
{
LclVarDsc * dsc1 = *(LclVarDsc * *)op1;
LclVarDsc * dsc2 = *(LclVarDsc * *)op2;
/* Make sure we preference tracked variables over untracked variables */
if (dsc1->lvTracked != dsc2->lvTracked)
{
return (dsc2->lvTracked) ? +1 : -1;
}
unsigned weight1 = dsc1->lvRefCntWtd;
unsigned weight2 = dsc2->lvRefCntWtd;
#if !FEATURE_FP_REGALLOC
/* Force integer candidates to sort above float candidates */
bool isFloat1 = isFloatRegType(dsc1->lvType);
bool isFloat2 = isFloatRegType(dsc2->lvType);
if (isFloat1 != isFloat2)
{
if (weight2 && isFloat1)
return +1;
if (weight1 && isFloat2)
return -1;
}
#endif
/* Increase the weight by 2 if we have exactly one bit set in lvPrefReg */
/* Increase the weight by 1 if we have more than one bit set in lvPrefReg */
if (weight1 && dsc1->lvPrefReg)
{
if ( (dsc1->lvPrefReg & ~RBM_BYTE_REG_FLAG) && genMaxOneBit((unsigned)dsc1->lvPrefReg))
weight1 += 2 * BB_UNITY_WEIGHT;
else
weight1 += 1 * BB_UNITY_WEIGHT;
}
if (weight2 && dsc2->lvPrefReg)
{
if ( (dsc2->lvPrefReg & ~RBM_BYTE_REG_FLAG) && genMaxOneBit((unsigned)dsc2->lvPrefReg))
weight2 += 2 * BB_UNITY_WEIGHT;
else
weight2 += 1 * BB_UNITY_WEIGHT;
}
if (weight2 > weight1)
{
return 1;
}
else if (weight2 < weight1)
{
return -1;
}
// Otherwise, we have equal weighted ref counts.
/* If the unweighted ref counts are different then use their difference */
int diff = (int)dsc2->lvRefCnt - (int)dsc1->lvRefCnt;
if (diff != 0)
return diff;
/* If one is a GC type and the other is not the GC type wins */
if (varTypeIsGC(dsc1->TypeGet()) != varTypeIsGC(dsc2->TypeGet()))
{
if (varTypeIsGC(dsc1->TypeGet()))
diff = -1;
else
diff = +1;
return diff;
}
/* If one was enregistered in the previous pass then it wins */
if (dsc1->lvRegister != dsc2->lvRegister)
{
if (dsc1->lvRegister)
diff = -1;
else
diff = +1;
return diff;
}
/* We have a tie! */
/* To achieve a Stable Sort we use the LclNum (by way of the pointer address) */
if (dsc1 < dsc2)
return -1;
if (dsc1 > dsc2)
return +1;
return 0;
}
/*****************************************************************************
*
* Sort the local variable table by refcount and assign tracking indices.
*/
void Compiler::lvaSortOnly()
{
/* Now sort the variable table by ref-count */
qsort(lvaRefSorted, lvaCount, sizeof(*lvaRefSorted),
(compCodeOpt() == SMALL_CODE) ? RefCntCmp
: WtdRefCntCmp);
lvaSortAgain = false;
lvaDumpRefCounts();
}
void
Compiler::lvaDumpRefCounts()
{
#ifdef DEBUG
if (verbose && lvaCount)
{
printf("refCnt table for '%s':\n", info.compMethodName);
for (unsigned lclNum = 0; lclNum < lvaCount; lclNum++)
{
unsigned refCnt = lvaRefSorted[lclNum]->lvRefCnt;
if (refCnt == 0)
break;
unsigned refCntWtd = lvaRefSorted[lclNum]->lvRefCntWtd;
printf(" ");
gtDispLclVar((unsigned)(lvaRefSorted[lclNum] - lvaTable));
printf(" [%6s]: refCnt = %4u, refCntWtd = %6s",
varTypeName(lvaRefSorted[lclNum]->TypeGet()),
refCnt,
refCntWtd2str(refCntWtd));
regMaskSmall pref = lvaRefSorted[lclNum]->lvPrefReg;
if (pref)
{
printf(" pref ");
dspRegMask(pref);
}
printf("\n");
}
printf("\n");
}
#endif
}
/*****************************************************************************
*
* Sort the local variable table by refcount and assign tracking indices.
*/
void Compiler::lvaSortByRefCount()
{
lvaTrackedCount = 0;
lvaTrackedCountInSizeTUnits = 0;
if (lvaCount == 0)
return;
unsigned lclNum;
LclVarDsc * varDsc;
LclVarDsc * * refTab;
/* We'll sort the variables by ref count - allocate the sorted table */
lvaRefSorted = refTab = new (this, CMK_LvaTable) LclVarDsc*[lvaCount];
/* Fill in the table used for sorting */
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
/* Append this variable to the table for sorting */
*refTab++ = varDsc;
/* If we have JMP, all arguments must have a location
* even if we don't use them inside the method */
if (compJmpOpUsed && varDsc->lvIsParam)
{
/* ...except when we have varargs and the argument is
passed on the stack. In that case, it's important
for the ref count to be zero, so that we don't attempt
to track them for GC info (which is not possible since we
don't know their offset in the stack). See the assert at the
end of raMarkStkVars and bug #28949 for more info. */
if (!raIsVarargsStackArg(lclNum))
{
varDsc->incRefCnts(1, this);
}
}
/* For now assume we'll be able to track all locals */
varDsc->lvTracked = 1;
/* If the ref count is zero */
if (varDsc->lvRefCnt == 0)
{
/* Zero ref count, make this untracked */
varDsc->lvTracked = 0;
varDsc->lvRefCntWtd = 0;
}
#if !defined(_TARGET_64BIT_) && !defined(LEGACY_BACKEND)
if (varTypeIsLong(varDsc) && varDsc->lvPromoted)
{
varDsc->lvTracked = 0;
}
#endif // !defined(_TARGET_64BIT_) && !defined(LEGACY_BACKEND)
// Variables that are address-exposed, and all struct locals, are never enregistered, or tracked.
// (The struct may be promoted, and its field variables enregistered/tracked, or the VM may "normalize"
// its type so that its not seen by the JIT as a struct.)
// Pinned variables may not be tracked (a condition of the GCInfo representation)
// or enregistered, on x86 -- it is believed that we can enregister pinned (more properly, "pinning")
// references when using the general GC encoding.
if (varDsc->lvAddrExposed)
{
varDsc->lvTracked = 0;
assert(varDsc->lvType != TYP_STRUCT || varDsc->lvDoNotEnregister); // For structs, should have set this when we set lvAddrExposed.
}
else if (varTypeIsStruct(varDsc))
{
// Promoted structs will never be considered for enregistration anyway,
// and the DoNotEnregister flag was used to indicate whether promotion was
// independent or dependent.
if (varDsc->lvPromoted)
{
varDsc->lvTracked = 0;
}
else if ((varDsc->lvType == TYP_STRUCT) && !varDsc->lvRegStruct)
{
lvaSetVarDoNotEnregister(lclNum DEBUG_ARG(DNER_IsStruct));
}
}
else if (varDsc->lvIsStructField &&
(lvaGetParentPromotionType(lclNum) != PROMOTION_TYPE_INDEPENDENT))
{
// SSA must exclude struct fields that are not independently promoted
// as dependent fields could be assigned using a CopyBlock
// resulting in a single node causing multiple SSA definitions
// which isn't currently supported by SSA
//
// TODO-CQ: Consider using lvLclBlockOpAddr and only marking these LclVars
// untracked when a blockOp is used to assign the struct.
//
varDsc->lvTracked = 0; // so, don't mark as tracked
}
else if (varDsc->lvPinned)
{
varDsc->lvTracked = 0;
#ifdef JIT32_GCENCODER
lvaSetVarDoNotEnregister(lclNum DEBUG_ARG(DNER_PinningRef));
#endif
}
// Are we not optimizing and we have exception handlers?
// if so mark all args and locals "do not enregister".
//
if (opts.MinOpts() && compHndBBtabCount > 0)
{
lvaSetVarDoNotEnregister(lclNum DEBUG_ARG(DNER_LiveInOutOfHandler));
continue;
}
var_types type = genActualType(varDsc->TypeGet());
switch (type)
{
#if CPU_HAS_FP_SUPPORT
case TYP_FLOAT:
case TYP_DOUBLE:
#endif
case TYP_INT:
case TYP_LONG:
case TYP_REF:
case TYP_BYREF:
#ifdef FEATURE_SIMD
case TYP_SIMD8:
case TYP_SIMD12:
case TYP_SIMD16:
case TYP_SIMD32:
#endif // FEATURE_SIMD
case TYP_STRUCT:
break;
case TYP_UNDEF:
case TYP_UNKNOWN:
noway_assert(!"lvType not set correctly");
varDsc->lvType = TYP_INT;
__fallthrough;
default:
varDsc->lvTracked = 0;
}
}
/* Now sort the variable table by ref-count */
lvaSortOnly();
/* Decide which variables will be worth tracking */
if (lvaCount > lclMAX_TRACKED)
{
/* Mark all variables past the first 'lclMAX_TRACKED' as untracked */
for (lclNum = lclMAX_TRACKED; lclNum < lvaCount; lclNum++)
{
lvaRefSorted[lclNum]->lvTracked = 0;
}
}
#ifdef DEBUG
// Re-Initialize to -1 for safety in debug build.
memset(lvaTrackedToVarNum, -1, sizeof(lvaTrackedToVarNum));
#endif
/* Assign indices to all the variables we've decided to track */
for (lclNum = 0; lclNum < min(lvaCount,lclMAX_TRACKED); lclNum++)
{
varDsc = lvaRefSorted[lclNum];
if (varDsc->lvTracked)
{
noway_assert(varDsc->lvRefCnt > 0);
/* This variable will be tracked - assign it an index */
lvaTrackedToVarNum[lvaTrackedCount] = (unsigned)(varDsc - lvaTable); // The type of varDsc and lvaTable
// is LclVarDsc. Subtraction will give us
// the index.
varDsc->lvVarIndex = lvaTrackedCount++;
}
}
// We have a new epoch, and also cache the tracked var count in terms of size_t's sufficient to hold that many bits.
lvaCurEpoch++;
lvaTrackedCountInSizeTUnits = unsigned(roundUp(lvaTrackedCount, sizeof(size_t)*8))/unsigned(sizeof(size_t)*8);
#ifdef DEBUG
VarSetOps::AssignNoCopy(this, lvaTrackedVars, VarSetOps::MakeFull(this));
#endif
}
#if ASSERTION_PROP
/*****************************************************************************
*
* This is called by lvaMarkLclRefs to disqualify a variable from being
* considered by optAddCopies()
*/
void LclVarDsc::lvaDisqualifyVar()
{
this->lvDisqualify = true;
this->lvSingleDef = false;
this->lvDefStmt = NULL;
}
#endif // ASSERTION_PROP
#ifndef LEGACY_BACKEND
/**********************************************************************************
* Get type of a variable when passed as an argument.
*/
var_types LclVarDsc::lvaArgType()
{
var_types type = TypeGet();
#ifdef _TARGET_AMD64_
if (type == TYP_STRUCT)
{
switch (lvExactSize)
{
case 1: type = TYP_BYTE; break;
case 2: type = TYP_SHORT; break;
case 4: type = TYP_INT; break;
case 8:
switch (*lvGcLayout)
{
case TYPE_GC_NONE:
type = TYP_I_IMPL;
break;
case TYPE_GC_REF:
type = TYP_REF;
break;
case TYPE_GC_BYREF:
type = TYP_BYREF;
break;
default:
unreached();
}
break;
default:
type = TYP_BYREF;
break;
}
}
#else
NYI("unknown target");
#endif //_TARGET_AMD64_
return type;
}
#endif // !LEGACY_BACKEND
/*****************************************************************************
*
* This is called by lvaMarkLclRefsCallback() to do variable ref marking
*/
void Compiler::lvaMarkLclRefs(GenTreePtr tree)
{
#if INLINE_NDIRECT
/* Is this a call to unmanaged code ? */
if (tree->gtOper == GT_CALL && tree->gtFlags & GTF_CALL_UNMANAGED)
{
assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
if (!opts.ShouldUsePInvokeHelpers())
{
/* Get the special variable descriptor */
unsigned lclNum = info.compLvFrameListRoot;
noway_assert(lclNum <= lvaCount);
LclVarDsc * varDsc = lvaTable + lclNum;
/* Increment the ref counts twice */
varDsc->incRefCnts(lvaMarkRefsWeight, this);
varDsc->incRefCnts(lvaMarkRefsWeight, this);
}
}
#endif
/* Is this an assigment? */
if (tree->OperKind() & GTK_ASGOP)
{
GenTreePtr op1 = tree->gtOp.gtOp1;
GenTreePtr op2 = tree->gtOp.gtOp2;
/* Set target register for RHS local if assignment is of a "small" type */
if (varTypeIsByte(tree->gtType))
{
unsigned lclNum;
LclVarDsc * varDsc = NULL;
/* GT_CHS is special it doesn't have a valid op2 */
if (tree->gtOper == GT_CHS)
{
if (op1->gtOper == GT_LCL_VAR)
{
lclNum = op1->gtLclVarCommon.gtLclNum;
noway_assert(lclNum < lvaCount);
varDsc = &lvaTable[lclNum];
}
}
else
{
if (op2->gtOper == GT_LCL_VAR)
{
lclNum = op2->gtLclVarCommon.gtLclNum;
noway_assert(lclNum < lvaCount);
varDsc = &lvaTable[lclNum];
}
}
#if CPU_HAS_BYTE_REGS
if (varDsc)
varDsc->addPrefReg(RBM_BYTE_REG_FLAG, this);
#endif
}
#if OPT_BOOL_OPS
/* Is this an assignment to a local variable? */
if (op1->gtOper == GT_LCL_VAR && op2->gtType != TYP_BOOL)
{
/* Only simple assignments allowed for booleans */
if (tree->gtOper != GT_ASG)
goto NOT_BOOL;
/* Is the RHS clearly a boolean value? */
switch (op2->gtOper)
{
unsigned lclNum;
case GT_CNS_INT:
if (op2->gtIntCon.gtIconVal == 0)
break;
if (op2->gtIntCon.gtIconVal == 1)
break;
// Not 0 or 1, fall through ....
__fallthrough;
default:
if (op2->OperIsCompare())
break;
NOT_BOOL:
lclNum = op1->gtLclVarCommon.gtLclNum;
noway_assert(lclNum < lvaCount);
lvaTable[lclNum].lvIsBoolean = false;
break;
}
}
#endif
}
#if FANCY_ARRAY_OPT
/* Special case: assignment node */
if (tree->gtOper == GT_ASG)
{
if (tree->gtType == TYP_INT)
{
unsigned lclNum1;
LclVarDsc * varDsc1;
GenTreePtr op1 = tree->gtOp.gtOp1;
if (op1->gtOper != GT_LCL_VAR)
return;
lclNum1 = op1->gtLclVarCommon.gtLclNum;
noway_assert(lclNum1 < lvaCount);
varDsc1 = lvaTable + lclNum1;
if (varDsc1->lvAssignOne)
varDsc1->lvAssignTwo = true;
else
varDsc1->lvAssignOne = true;
}
return;
}
#endif
#ifdef _TARGET_XARCH_
/* Special case: integer shift node by a variable amount */
if (tree->gtOper == GT_LSH ||
tree->gtOper == GT_RSH ||
tree->gtOper == GT_RSZ ||
tree->gtOper == GT_ROL ||
tree->gtOper == GT_ROR)
{
if (tree->gtType == TYP_INT)
{
GenTreePtr op2 = tree->gtOp.gtOp2;
if (op2->gtOper == GT_LCL_VAR)
{
unsigned lclNum = op2->gtLclVarCommon.gtLclNum;
noway_assert(lclNum < lvaCount);
lvaTable[lclNum].setPrefReg(REG_ECX, this);
}
}
return;
}
#endif
if ((tree->gtOper != GT_LCL_VAR) && (tree->gtOper != GT_LCL_FLD))
return;
/* This must be a local variable reference */
noway_assert((tree->gtOper == GT_LCL_VAR) || (tree->gtOper == GT_LCL_FLD));
unsigned lclNum = tree->gtLclVarCommon.gtLclNum;
noway_assert(lclNum < lvaCount);
LclVarDsc * varDsc = lvaTable + lclNum;
/* Increment the reference counts */
varDsc->incRefCnts(lvaMarkRefsWeight, this);
if (lvaVarAddrExposed(lclNum))
varDsc->lvIsBoolean = false;
if (tree->gtOper == GT_LCL_FLD)
{
#if ASSERTION_PROP
// variables that have uses inside a GT_LCL_FLD
// cause problems, so we will disqualify them here
varDsc->lvaDisqualifyVar();
#endif // ASSERTION_PROP
return;
}
#if ASSERTION_PROP
/* Exclude the normal entry block */
if (fgDomsComputed &&
(lvaMarkRefsCurBlock->bbNum != 1) &&
lvaMarkRefsCurBlock->bbIDom != NULL)
{
// If any entry block except the normal entry block dominates the block, then mark the local with the lvVolatileHint flag.
if (BlockSetOps::MayBeUninit(lvaMarkRefsCurBlock->bbDoms))
{
// Lazy init (If a block is not dominated by any other block, we'll redo this every time, but it'll be fast)
BlockSetOps::AssignNoCopy(this, lvaMarkRefsCurBlock->bbDoms, fgGetDominatorSet(lvaMarkRefsCurBlock));
BlockSetOps::RemoveElemD(this, lvaMarkRefsCurBlock->bbDoms, fgFirstBB->bbNum);
}
assert(fgEnterBlksSetValid);
if (!BlockSetOps::IsEmptyIntersection(this, lvaMarkRefsCurBlock->bbDoms, fgEnterBlks))
{
varDsc->lvVolatileHint = 1;
}
}
/* Record if the variable has a single def or not */
if (!varDsc->lvDisqualify) // If this variable is already disqualified we can skip this
{
if (tree->gtFlags & GTF_VAR_DEF) // Is this is a def of our variable
{
/*
If we have one of these cases:
1. We have already seen a definition (i.e lvSingleDef is true)
2. or info.CompInitMem is true (thus this would be the second definition)
3. or we have an assignment inside QMARK-COLON trees
4. or we have an update form of assignment (i.e. +=, -=, *=)
Then we must disqualify this variable for use in optAddCopies()
Note that all parameters start out with lvSingleDef set to true
*/
if ((varDsc->lvSingleDef == true) ||
(info.compInitMem == true) ||
(tree->gtFlags & GTF_COLON_COND) ||
(tree->gtFlags & GTF_VAR_USEASG) )
{
varDsc->lvaDisqualifyVar();
}
else
{
varDsc->lvSingleDef = true;
varDsc->lvDefStmt = lvaMarkRefsCurStmt;
}
}
else // otherwise this is a ref of our variable
{
if (BlockSetOps::MayBeUninit(varDsc->lvRefBlks))
{
// Lazy initialization
BlockSetOps::AssignNoCopy(this, varDsc->lvRefBlks, BlockSetOps::MakeEmpty(this));
}
BlockSetOps::AddElemD(this, varDsc->lvRefBlks, lvaMarkRefsCurBlock->bbNum);
}
}
#endif // ASSERTION_PROP
bool allowStructs = false;
#ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
// On System V the type of the var could be a struct type.
allowStructs = varTypeIsStruct(varDsc);
#endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
/* Variables must be used as the same type throughout the method */
noway_assert(tiVerificationNeeded ||
varDsc->lvType == TYP_UNDEF || tree->gtType == TYP_UNKNOWN ||
allowStructs ||
genActualType(varDsc->TypeGet()) == genActualType(tree->gtType) ||
(tree->gtType == TYP_BYREF && varDsc->TypeGet() == TYP_I_IMPL) ||
(tree->gtType == TYP_I_IMPL && varDsc->TypeGet() == TYP_BYREF) ||
(tree->gtFlags & GTF_VAR_CAST) ||
varTypeIsFloating(varDsc->TypeGet()) && varTypeIsFloating(tree->gtType));
/* Remember the type of the reference */
if (tree->gtType == TYP_UNKNOWN || varDsc->lvType == TYP_UNDEF)
{
varDsc->lvType = tree->gtType;
noway_assert(genActualType(varDsc->TypeGet()) == tree->gtType); // no truncation
}
#ifdef DEBUG
if (tree->gtFlags & GTF_VAR_CAST)
{
// it should never be bigger than the variable slot
// Trees don't store the full information about structs
// so we can't check them.
if (tree->TypeGet() != TYP_STRUCT)
{
unsigned treeSize = genTypeSize(tree->TypeGet());
unsigned varSize = genTypeSize(varDsc->TypeGet());
if (varDsc->TypeGet() == TYP_STRUCT)
varSize = varDsc->lvSize();
assert(treeSize <= varSize);
}
}
#endif
}
/*****************************************************************************
*
* Helper passed to Compiler::fgWalkTreePre() to do variable ref marking.
*/
/* static */
Compiler::fgWalkResult Compiler::lvaMarkLclRefsCallback(GenTreePtr *pTree, fgWalkData *data)
{
data->compiler->lvaMarkLclRefs(*pTree);
return WALK_CONTINUE;
}
/*****************************************************************************
*
* Update the local variable reference counts for one basic block
*/
void Compiler::lvaMarkLocalVars(BasicBlock * block)
{
#if ASSERTION_PROP
lvaMarkRefsCurBlock = block;
#endif
lvaMarkRefsWeight = block->getBBWeight(this);
#ifdef DEBUG
if (verbose)
printf("\n*** marking local variables in block BB%02u (weight=%s)\n",
block->bbNum, refCntWtd2str(lvaMarkRefsWeight));
#endif
#if JIT_FEATURE_SSA_SKIP_DEFS
for (GenTreePtr tree = block->FirstNonPhiDef(); tree; tree = tree->gtNext)
#else
for (GenTreePtr tree = block->bbTreeList; tree; tree = tree->gtNext)
#endif
{
noway_assert(tree->gtOper == GT_STMT);
#if ASSERTION_PROP
lvaMarkRefsCurStmt = tree;
#endif
#ifdef DEBUG
if (verbose)
gtDispTree(tree);
#endif
fgWalkTreePre(&tree->gtStmt.gtStmtExpr,
Compiler::lvaMarkLclRefsCallback,
(void *) this,
false);
}
}
/*****************************************************************************
*
* Create the local variable table and compute local variable reference
* counts.
*/
void Compiler::lvaMarkLocalVars()
{
#ifdef DEBUG
if (verbose)
printf("\n*************** In lvaMarkLocalVars()");
#endif
#if INLINE_NDIRECT
/* If there is a call to an unmanaged target, we already grabbed a
local slot for the current thread control block.
*/
if (info.compCallUnmanaged != 0)
{
assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
if (!opts.ShouldUsePInvokeHelpers())
{
noway_assert(info.compLvFrameListRoot >= info.compLocalsCount &&
info.compLvFrameListRoot < lvaCount);
lvaTable[info.compLvFrameListRoot].lvType = TYP_I_IMPL;
/* Set the refCnt, it is used in the prolog and return block(s) */
lvaTable[info.compLvFrameListRoot].lvRefCnt = 2;
lvaTable[info.compLvFrameListRoot].lvRefCntWtd = 2 * BB_UNITY_WEIGHT;
}
}
#endif
lvaAllocOutgoingArgSpace();
#if !FEATURE_EH_FUNCLETS
// Grab space for exception handling
if (ehNeedsShadowSPslots())
{
// The first slot is reserved for ICodeManager::FixContext(ppEndRegion)
// ie. the offset of the end-of-last-executed-filter
unsigned slotsNeeded = 1;
unsigned handlerNestingLevel = ehMaxHndNestingCount;
if (opts.compDbgEnC && (handlerNestingLevel < (unsigned)MAX_EnC_HANDLER_NESTING_LEVEL))
handlerNestingLevel = (unsigned)MAX_EnC_HANDLER_NESTING_LEVEL;
slotsNeeded += handlerNestingLevel;
// For a filter (which can be active at the same time as a catch/finally handler)
slotsNeeded++;
// For zero-termination of the shadow-Stack-pointer chain
slotsNeeded++;
lvaShadowSPslotsVar = lvaGrabTempWithImplicitUse(false DEBUGARG("lvaShadowSPslotsVar"));
LclVarDsc * shadowSPslotsVar = &lvaTable[lvaShadowSPslotsVar];
shadowSPslotsVar->lvType = TYP_BLK;
shadowSPslotsVar->lvExactSize = (slotsNeeded * TARGET_POINTER_SIZE);
}
#endif // !FEATURE_EH_FUNCLETS
#if FEATURE_EH_FUNCLETS
if (ehNeedsPSPSym())
{
lvaPSPSym = lvaGrabTempWithImplicitUse(false DEBUGARG("PSPSym"));
LclVarDsc * lclPSPSym = &lvaTable[lvaPSPSym];
lclPSPSym->lvType = TYP_I_IMPL;
}
#endif // FEATURE_EH_FUNCLETS
if (compLocallocUsed)
{
lvaLocAllocSPvar = lvaGrabTempWithImplicitUse(false DEBUGARG("LocAllocSPvar"));
LclVarDsc * locAllocSPvar = &lvaTable[lvaLocAllocSPvar];
locAllocSPvar->lvType = TYP_I_IMPL;
}
BasicBlock * block;
#if defined(DEBUGGING_SUPPORT) || defined(DEBUG)
// Assign slot numbers to all variables.
// If compiler generated local variables, slot numbers will be
// invalid (out of range of info.compVarScopes).
// Also have to check if variable was not reallocated to another
// slot in which case we have to register the original slot #.
// We don't need to do this for IL, but this keeps lvSlotNum consistent.
#ifndef DEBUG
if (opts.compScopeInfo && (info.compVarScopesCount > 0))
#endif
{
unsigned lclNum;
LclVarDsc * varDsc;
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
varDsc->lvSlotNum = lclNum;
}
}
#endif // defined(DEBUGGING_SUPPORT) || defined(DEBUG)
/* Mark all local variable references */
lvaRefCountingStarted = true;
for (block = fgFirstBB;
block;
block = block->bbNext)
{
lvaMarkLocalVars(block);
}
/* For incoming register arguments, if there are references in the body
* then we will have to copy them to the final home in the prolog
* This counts as an extra reference with a weight of 2
*/
unsigned lclNum;
LclVarDsc * varDsc;
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
if (lclNum >= info.compArgsCount)
break; // early exit for loop
if ((varDsc->lvIsRegArg) && (varDsc->lvRefCnt > 0))
{
// Fix 388376 ARM JitStress WP7
varDsc->incRefCnts(BB_UNITY_WEIGHT, this);
varDsc->incRefCnts(BB_UNITY_WEIGHT, this);
}
}
#if ASSERTION_PROP
if (!opts.MinOpts() && !opts.compDbgCode)
{
// Note: optAddCopies() depends on lvaRefBlks, which is set in lvaMarkLocalVars(BasicBlock*), called above.
optAddCopies();
}
#endif
if (lvaKeepAliveAndReportThis() && lvaTable[0].lvRefCnt == 0)
lvaTable[0].lvRefCnt = 1;
// This isn't strictly needed as we will make a copy of the param-type-arg
// in the prolog. However, this ensures that the LclVarDsc corresponding to
// info.compTypeCtxtArg is valid.
else if (lvaReportParamTypeArg() && lvaTable[info.compTypeCtxtArg].lvRefCnt == 0)
lvaTable[info.compTypeCtxtArg].lvRefCnt = 1;
lvaLocalVarRefCounted = true;
lvaRefCountingStarted = false;
lvaSortByRefCount();
}
void Compiler::lvaAllocOutgoingArgSpace()
{
#if FEATURE_FIXED_OUT_ARGS
// Setup the outgoing argument region, in case we end up using it later
if (lvaOutgoingArgSpaceVar == BAD_VAR_NUM)
{
lvaOutgoingArgSpaceVar = lvaGrabTemp(false DEBUGARG("OutgoingArgSpace"));
lvaTable[lvaOutgoingArgSpaceVar].lvType = TYP_LCLBLK;
/* Set the refCnts */
lvaTable[lvaOutgoingArgSpaceVar].lvRefCnt = 1;
lvaTable[lvaOutgoingArgSpaceVar].lvRefCntWtd = BB_UNITY_WEIGHT;
#if defined(PROFILING_SUPPORTED) && defined(_TARGET_AMD64_) && !defined(UNIX_AMD64_ABI) // No 4 slots for outgoing params on System V.
// If we are generating profiling Enter/Leave/TailCall hooks, make sure
// that outgoing arg space size is minimum 4 slots. This will ensure
// that even methods without any calls will have 4-slot outgoing arg area.
if (compIsProfilerHookNeeded() && (lvaOutgoingArgSpaceSize == 0))
{
lvaOutgoingArgSpaceSize = 4 * REGSIZE_BYTES;
}
#endif // PROFILING_SUPPORTED && _TARGET_AMD64_ && !UNIX_AMD64_ABI
}
noway_assert(lvaOutgoingArgSpaceVar >= info.compLocalsCount &&
lvaOutgoingArgSpaceVar < lvaCount);
#endif // FEATURE_FIXED_OUT_ARGS
}
inline void Compiler::lvaIncrementFrameSize(unsigned size)
{
if (size > MAX_FrameSize || compLclFrameSize + size > MAX_FrameSize)
BADCODE("Frame size overflow");
compLclFrameSize += size;
}
/****************************************************************************
*
* Return true if absolute offsets of temps are larger than vars, or in other
* words, did we allocate temps before of after vars. The /GS buffer overrun
* checks want temps to be at low stack addresses than buffers
*/
bool Compiler::lvaTempsHaveLargerOffsetThanVars()
{
#ifdef _TARGET_ARM_
// We never want to place the temps with larger offsets for ARM
return false;
#else
if (compGSReorderStackLayout)
{
return codeGen->isFramePointerUsed();
}
else
{
return true;
}
#endif
}
/****************************************************************************
*
* Return an upper bound estimate for the size of the compiler spill temps
*
*/
unsigned Compiler::lvaGetMaxSpillTempSize()
{
unsigned result = 0;
#ifndef LEGACY_BACKEND
if (lvaDoneFrameLayout >= REGALLOC_FRAME_LAYOUT)
{
result = tmpSize;
}
else
{
result = MAX_SPILL_TEMP_SIZE;
}
#else // LEGACY_BACKEND
if (lvaDoneFrameLayout >= FINAL_FRAME_LAYOUT)
{
result = tmpSize;
}
else
{
if (lvaDoneFrameLayout >= REGALLOC_FRAME_LAYOUT)
{
unsigned maxTmpSize = sizeof(double) + sizeof(int);
maxTmpSize += (tmpDoubleSpillMax * sizeof(double)) +
(tmpIntSpillMax * sizeof(int));
result = maxTmpSize;
}
else
{
result = MAX_SPILL_TEMP_SIZE;
}
#ifdef DEBUG
// When StressRegs is >=1, there can be a bunch of spills that are not
// predicted by the predictor (see logic in rsPickReg). It is very hard
// to teach the predictor about the behavior of rsPickReg for StressRegs >= 1,
// so instead let's make MaxTmpSize large enough so that we won't be wrong.
if (codeGen->regSet.rsStressRegs() >= 1)
{
result += (REG_TMP_ORDER_COUNT * REGSIZE_BYTES);
}
#endif // DEBUG
}
#endif // LEGACY_BACKEND
return result;
}
/*****************************************************************************
*
* Compute stack frame offsets for arguments, locals and optionally temps.
*
* The frame is laid out as follows for x86:
*
* ESP frames
*
* | |
* |-----------------------|
* | incoming |
* | arguments |
* |-----------------------| <---- Virtual '0'
* | return address |
* +=======================+
* |Callee saved registers |
* |-----------------------|
* | Temps |
* |-----------------------|
* | Variables |
* |-----------------------| <---- Ambient ESP
* | Arguments for the |
* ~ next function ~
* | |
* | | |
* | | Stack grows |
* | downward
* V
*
*
* EBP frames
*
* | |
* |-----------------------|
* | incoming |
* | arguments |
* |-----------------------| <---- Virtual '0'
* | return address |
* +=======================+
* | incoming EBP |
* |-----------------------| <---- EBP
* |Callee saved registers |
* |-----------------------|
* | security object |
* |-----------------------|
* | ParamTypeArg |
* |-----------------------|
* | Last-executed-filter |
* |-----------------------|
* | |
* ~ Shadow SPs ~
* | |
* |-----------------------|
* | |
* ~ Variables ~
* | |
* ~-----------------------|
* | Temps |
* |-----------------------|
* | localloc |
* |-----------------------| <---- Ambient ESP
* | Arguments for the |
* | next function ~
* | |
* | | |
* | | Stack grows |
* | downward
* V
*
*
* The frame is laid out as follows for x64:
*
* RSP frames
* | |
* |-----------------------|
* | incoming |
* | arguments |
* |-----------------------|
* | 4 fixed incoming |
* | argument slots |
* |-----------------------| <---- Caller's SP & Virtual '0'
* | return address |
* +=======================+
* | Callee saved Int regs |
* -------------------------
* | Padding | <---- this padding (0 or 8 bytes) is to ensure flt registers are saved at a mem location aligned at 16-bytes
* | | so that we can save 128-bit callee saved xmm regs using performant "movaps" instruction instead of "movups"
* -------------------------
* | Callee saved Flt regs | <----- entire 128-bits of callee saved xmm registers are stored here
* |-----------------------|
* | Temps |
* |-----------------------|
* | Variables |
* |-----------------------|
* | Arguments for the |
* ~ next function ~
* | |
* |-----------------------|
* | 4 fixed outgoing |
* | argument slots |
* |-----------------------| <---- Ambient RSP
* | | |
* ~ | Stack grows ~
* | | downward |
* V
*
*
* RBP frames
* | |
* |-----------------------|
* | incoming |
* | arguments |
* |-----------------------|
* | 4 fixed incoming |
* | argument slots |
* |-----------------------| <---- Caller's SP & Virtual '0'
* | return address |
* +=======================+
* | Callee saved Int regs |
* -------------------------
* | Padding |
* -------------------------
* | Callee saved Flt regs |
* |-----------------------|
* | security object |
* |-----------------------|
* | ParamTypeArg |
* |-----------------------|
* | |
* | |
* ~ Variables ~
* | |
* | |
* |-----------------------|
* | Temps |
* |-----------------------|
* | |
* ~ localloc ~ // not in frames with EH
* | |
* |-----------------------|
* | PSPSym | // only in frames with EH (thus no localloc)
* | |
* |-----------------------| <---- RBP in localloc frames (max 240 bytes from Initial-SP)
* | Arguments for the |
* ~ next function ~
* | |
* |-----------------------|
* | 4 fixed outgoing |
* | argument slots |
* |-----------------------| <---- Ambient RSP (before localloc, this is Initial-SP)
* | | |
* ~ | Stack grows ~
* | | downward |
* V
*
*
* The frame is laid out as follows for ARM (this is a general picture; details may differ for different conditions):
*
* SP frames
* | |
* |-----------------------|
* | incoming |
* | arguments |
* +=======================+ <---- Caller's SP
* | Pre-spill registers |
* |-----------------------| <---- Virtual '0'
* |Callee saved registers |
* |-----------------------|
* ~ possible double align ~
* |-----------------------|
* | security object |
* |-----------------------|
* | ParamTypeArg |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Variables |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Temps |
* |-----------------------|
* | Stub Argument Var |
* |-----------------------|
* |Inlined PInvoke Frame V|
* |-----------------------|
* ~ possible double align ~
* |-----------------------|
* | Arguments for the |
* ~ next function ~
* | |
* |-----------------------| <---- Ambient SP
* | | |
* ~ | Stack grows ~
* | | downward |
* V
*
*
* FP / R11 frames
* | |
* |-----------------------|
* | incoming |
* | arguments |
* +=======================+ <---- Caller's SP
* | Pre-spill registers |
* |-----------------------| <---- Virtual '0'
* |Callee saved registers |
* |-----------------------|
* | PSPSym | // Only for frames with EH, which means FP-based frames
* |-----------------------|
* ~ possible double align ~
* |-----------------------|
* | security object |
* |-----------------------|
* | ParamTypeArg |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Variables |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Temps |
* |-----------------------|
* | Stub Argument Var |
* |-----------------------|
* |Inlined PInvoke Frame V|
* |-----------------------|
* ~ possible double align ~
* |-----------------------|
* | localloc |
* |-----------------------|
* | Arguments for the |
* ~ next function ~
* | |
* |-----------------------| <---- Ambient SP
* | | |
* ~ | Stack grows ~
* | | downward |
* V
*
*
* The frame is laid out as follows for ARM64 (this is a general picture; details may differ for different conditions):
* TODO-ARM64-NYI: this is preliminary (copied from ARM and modified), and needs to be reviewed.
* NOTE: SP must be 16-byte aligned, so there may be alignment slots in the frame.
* We will often save and establish a frame pointer to create better ETW stack walks.
*
* SP frames
* | |
* |-----------------------|
* | incoming |
* | arguments |
* +=======================+ <---- Caller's SP
* | homed | // this is only needed if reg argument need to be homed, e.g., for varargs
* | register arguments |
* |-----------------------| <---- Virtual '0'
* |Callee saved registers |
* | except fp/lr |
* |-----------------------|
* | security object |
* |-----------------------|
* | ParamTypeArg |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Variables |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Temps |
* |-----------------------|
* | Stub Argument Var |
* |-----------------------|
* |Inlined PInvoke Frame V|
* |-----------------------|
* | Saved LR |
* |-----------------------|
* | Saved FP | <---- Frame pointer
* |-----------------------|
* | Stack arguments for |
* | the next function |
* |-----------------------| <---- SP
* | | |
* ~ | Stack grows ~
* | | downward |
* V
*
*
* FP (R29 / x29) frames
* | |
* |-----------------------|
* | incoming |
* | arguments |
* +=======================+ <---- Caller's SP
* | optional homed | // this is only needed if reg argument need to be homed, e.g., for varargs
* | register arguments |
* |-----------------------| <---- Virtual '0'
* |Callee saved registers |
* | except fp/lr |
* |-----------------------|
* | PSPSym | // Only for frames with EH, which requires FP-based frames
* |-----------------------|
* | security object |
* |-----------------------|
* | ParamTypeArg |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Variables |
* |-----------------------|
* | possible GS cookie |
* |-----------------------|
* | Temps |
* |-----------------------|
* | Stub Argument Var |
* |-----------------------|
* |Inlined PInvoke Frame V|
* |-----------------------|
* | Saved LR |
* |-----------------------|
* | Saved FP | <---- Frame pointer
* |-----------------------|
* ~ localloc ~
* |-----------------------|
* | Stack arguments for |
* | the next function |
* |-----------------------| <---- Ambient SP
* | | |
* ~ | Stack grows ~
* | | downward |
* V
*
*
* Doing this all in one pass is 'hard'. So instead we do it in 2 basic passes:
* 1. Assign all the offsets relative to the Virtual '0'. Offsets above (the
* incoming arguments) are positive. Offsets below (everything else) are
* negative. This pass also calcuates the total frame size (between Caller's
* SP/return address and the Ambient SP).
* 2. Figure out where to place the frame pointer, and then adjust the offsets
* as needed for the final stack size and whether the offset is frame pointer
* relative or stack pointer relative.
*
*/
void Compiler::lvaAssignFrameOffsets(FrameLayoutState curState)
{
noway_assert(lvaDoneFrameLayout < curState);
lvaDoneFrameLayout = curState;
#ifdef DEBUG
if (verbose)
{
printf("*************** In lvaAssignFrameOffsets");
if (curState == INITIAL_FRAME_LAYOUT)
printf("(INITIAL_FRAME_LAYOUT)");
else if (curState == PRE_REGALLOC_FRAME_LAYOUT)
printf("(PRE_REGALLOC_FRAME_LAYOUT)");
else if (curState == REGALLOC_FRAME_LAYOUT)
printf("(REGALLOC_FRAME_LAYOUT)");
else if (curState == TENTATIVE_FRAME_LAYOUT)
printf("(TENTATIVE_FRAME_LAYOUT)");
else if (curState == FINAL_FRAME_LAYOUT)
printf("(FINAL_FRAME_LAYOUT)");
else
{
printf("(UNKNOWN)");
unreached();
}
printf("\n");
}
#endif
#if FEATURE_FIXED_OUT_ARGS
assert(lvaOutgoingArgSpaceVar != BAD_VAR_NUM);
#endif // FEATURE_FIXED_OUT_ARGS
/*-------------------------------------------------------------------------
*
* First process the arguments.
*
*-------------------------------------------------------------------------
*/
lvaAssignVirtualFrameOffsetsToArgs();
/*-------------------------------------------------------------------------
*
* Now compute stack offsets for any variables that don't live in registers
*
*-------------------------------------------------------------------------
*/
lvaAssignVirtualFrameOffsetsToLocals();
lvaAlignFrame();
/*-------------------------------------------------------------------------
*
* Now patch the offsets
*
*-------------------------------------------------------------------------
*/
lvaFixVirtualFrameOffsets();
// Modify the stack offset for fields of promoted structs.
lvaAssignFrameOffsetsToPromotedStructs();
/*-------------------------------------------------------------------------
*
* Finalize
*
*-------------------------------------------------------------------------
*/
// If it's not the final frame layout, then it's just an estimate. This means
// we're allowed to once again write to these variables, even if we've read
// from them to make tentative code generation or frame layout decisions.
if (curState < FINAL_FRAME_LAYOUT)
{
codeGen->resetFramePointerUsedWritePhase();
}
}
/*****************************************************************************
* lvaFixVirtualFrameOffsets() : Now that everything has a virtual offset,
* determine the final value for the frame pointer (if needed) and then
* adjust all the offsets appropriately.
*
* This routine fixes virtual offset to be relative to frame pointer or SP
* based on whether varDsc->lvFramePointerBased is true or false respectively.
*/
void Compiler::lvaFixVirtualFrameOffsets()
{
// The delta to be added to virtual offset to adjust it relative to frame pointer or SP
int delta = 0;
#ifdef _TARGET_XARCH_
delta += REGSIZE_BYTES; // pushed PC (return address) for x86/x64
if (codeGen->doubleAlignOrFramePointerUsed())
delta += REGSIZE_BYTES; // pushed EBP (frame pointer)
#endif
if (!codeGen->isFramePointerUsed())
{
// pushed registers, return address, and padding
delta += codeGen->genTotalFrameSize();
}
#if defined(_TARGET_ARM_)
else
{
// We set FP to be after LR, FP
delta += 2 * REGSIZE_BYTES;
}
#elif defined(_TARGET_AMD64_) || defined(_TARGET_ARM64_)
else
{
// FP is used.
delta += codeGen->genTotalFrameSize() - codeGen->genSPtoFPdelta();
}
#endif //_TARGET_AMD64_
unsigned lclNum;
LclVarDsc * varDsc;
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
bool doAssignStkOffs = true;
// Can't be relative to EBP unless we have an EBP
noway_assert(!varDsc->lvFramePointerBased || codeGen->doubleAlignOrFramePointerUsed());
// Is this a non-param promoted struct field?
// if so then set doAssignStkOffs to false.
//
if (varDsc->lvIsStructField && !varDsc->lvIsParam)
{
LclVarDsc * parentvarDsc = &lvaTable[varDsc->lvParentLcl];
lvaPromotionType promotionType = lvaGetPromotionType(parentvarDsc);
if (promotionType == PROMOTION_TYPE_DEPENDENT)
{
doAssignStkOffs = false; // Assigned later in lvaAssignFrameOffsetsToPromotedStructs()
}
}
if (!varDsc->lvOnFrame)
{
if (!varDsc->lvIsParam
#if !defined(_TARGET_AMD64_)
|| (varDsc->lvIsRegArg
#if defined(_TARGET_ARM_) && defined(PROFILING_SUPPORTED)
&& compIsProfilerHookNeeded() && !lvaIsPreSpilled(lclNum, codeGen->regSet.rsMaskPreSpillRegs(false)) // We need assign stack offsets for prespilled arguments
#endif
)
#endif // !defined(_TARGET_AMD64_)
)
{
doAssignStkOffs = false; // Not on frame or an incomming stack arg
}
}
if (doAssignStkOffs)
{
varDsc->lvStkOffs += delta;
#if DOUBLE_ALIGN
if (genDoubleAlign() && !codeGen->isFramePointerUsed())
{
if (varDsc->lvFramePointerBased)
{
varDsc->lvStkOffs -= delta;
// We need to re-adjust the offsets of the parameters so they are EBP
// relative rather than stack/frame pointer relative
varDsc->lvStkOffs += (2 * sizeof(void *)); // return address and pushed EBP
noway_assert(varDsc->lvStkOffs >= FIRST_ARG_STACK_OFFS);
}
}
#endif
// On System V environments the stkOffs could be 0 for params passed in registers.
assert(codeGen->isFramePointerUsed() || varDsc->lvStkOffs >= 0); // Only EBP relative references can have negative offsets
}
}
assert(tmpAllFree());
for (TempDsc* temp = tmpListBeg();
temp != nullptr;
temp = tmpListNxt(temp))
{
temp->tdAdjustTempOffs(delta);
}
lvaCachedGenericContextArgOffs += delta;
#if FEATURE_EH_FUNCLETS && defined(_TARGET_AMD64_)
if (ehNeedsPSPSym())
{
assert(lvaPSPSym != BAD_VAR_NUM);
varDsc = &lvaTable[lvaPSPSym];
varDsc->lvFramePointerBased = false;
varDsc->lvMustInit = false;
varDsc->lvStkOffs = lvaLclSize(lvaOutgoingArgSpaceVar); // put the PSPSym just above the outgoing arg space
}
#endif
#if FEATURE_FIXED_OUT_ARGS
if (lvaOutgoingArgSpaceVar != BAD_VAR_NUM)
{
varDsc = &lvaTable[lvaOutgoingArgSpaceVar];
varDsc->lvStkOffs = 0;
varDsc->lvFramePointerBased = false;
varDsc->lvMustInit = false;
}
#endif // FEATURE_FIXED_OUT_ARGS
}
#ifdef _TARGET_ARM_
bool Compiler::lvaIsPreSpilled(unsigned lclNum, regMaskTP preSpillMask)
{
const LclVarDsc& desc = lvaTable[lclNum];
return desc.lvIsRegArg && (preSpillMask & genRegMask(desc.lvArgReg));
}
#endif // _TARGET_ARM_
#ifndef LEGACY_BACKEND
/*****************************************************************************
* lvaUpdateArgsWithInitialReg() : For each argument variable descriptor, update
* its current register with the initial register as assigned by LSRA.
*/
void Compiler::lvaUpdateArgsWithInitialReg()
{
if (!compLSRADone)
return;
for (unsigned lclNum = 0; lclNum < info.compArgsCount; lclNum++)
{
LclVarDsc* varDsc = lvaTable + lclNum;
if (varDsc->lvPromotedStruct())
{
noway_assert(varDsc->lvFieldCnt == 1); // We only handle one field here
unsigned fieldVarNum = varDsc->lvFieldLclStart;
varDsc = lvaTable + fieldVarNum;
}
noway_assert(varDsc->lvIsParam);
if (varDsc->lvIsRegCandidate())
{
if (varTypeIsMultiReg(varDsc))
{
regPairNo initialRegPair = varDsc->lvArgInitRegPair;
varDsc->lvRegNum = genRegPairLo(initialRegPair);
varDsc->lvOtherReg = genRegPairHi(initialRegPair);
}
else
{
varDsc->lvRegNum = varDsc->lvArgInitReg;
}
}
}
}
#endif // !LEGACY_BACKEND
/*****************************************************************************
* lvaAssignVirtualFrameOffsetsToArgs() : Assign virtual stack offsets to the
* arguments, and implicit arguments (this ptr, return buffer, generics,
* and varargs).
*/
void Compiler::lvaAssignVirtualFrameOffsetsToArgs()
{
unsigned lclNum = 0;
int argOffs = 0;
#ifdef UNIX_AMD64_ABI
int callerArgOffset = 0;
#endif // UNIX_AMD64_ABI
/*
Assign stack offsets to arguments (in reverse order of passing).
This means that if we pass arguments left->right, we start at
the end of the list and work backwards, for right->left we start
with the first argument and move forward.
This is all relative to our Virtual '0'
*/
if (Target::g_tgtArgOrder == Target::ARG_ORDER_L2R)
argOffs = compArgSize;
/* Update the argOffs to reflect arguments that are passed in registers */
noway_assert(codeGen->intRegState.rsCalleeRegArgNum <= MAX_REG_ARG);
noway_assert(compArgSize >= codeGen->intRegState.rsCalleeRegArgNum * sizeof(void *));
#ifdef _TARGET_X86_
argOffs -= codeGen->intRegState.rsCalleeRegArgNum * sizeof(void *);
#endif
#ifndef LEGACY_BACKEND
// Update the arg initial register locations.
lvaUpdateArgsWithInitialReg();
#endif // !LEGACY_BACKEND
/* Is there a "this" argument? */
if (!info.compIsStatic)
{
noway_assert(lclNum == info.compThisArg);
#ifndef _TARGET_X86_
argOffs = lvaAssignVirtualFrameOffsetToArg(lclNum, REGSIZE_BYTES, argOffs UNIX_AMD64_ABI_ONLY_ARG(&callerArgOffset));
#endif // _TARGET_X86_
lclNum++;
}
/* if we have a hidden buffer parameter, that comes here */
if (info.compRetBuffArg != BAD_VAR_NUM)
{
noway_assert(lclNum == info.compRetBuffArg);
noway_assert(lvaTable[lclNum].lvIsRegArg);
#ifndef _TARGET_X86_
argOffs = lvaAssignVirtualFrameOffsetToArg(lclNum, REGSIZE_BYTES, argOffs UNIX_AMD64_ABI_ONLY_ARG(&callerArgOffset));
#endif // _TARGET_X86_
lclNum++;
}
#if USER_ARGS_COME_LAST
//@GENERICS: extra argument for instantiation info
if (info.compMethodInfo->args.callConv & CORINFO_CALLCONV_PARAMTYPE)
{
noway_assert(lclNum == (unsigned)info.compTypeCtxtArg);
argOffs = lvaAssignVirtualFrameOffsetToArg(lclNum++, sizeof(void *), argOffs UNIX_AMD64_ABI_ONLY_ARG(&callerArgOffset));
}
if (info.compIsVarArgs)
{
argOffs = lvaAssignVirtualFrameOffsetToArg(lclNum++, sizeof(void *), argOffs UNIX_AMD64_ABI_ONLY_ARG(&callerArgOffset));
}
#endif // USER_ARGS_COME_LAST
CORINFO_ARG_LIST_HANDLE argLst = info.compMethodInfo->args.args;
unsigned argSigLen = info.compMethodInfo->args.numArgs;
#ifdef _TARGET_ARM_
//
// struct_n { int; int; ... n times };
//
// Consider signature:
//
// Foo (float a,double b,float c,double d,float e,double f,float g,double h,
// float i,double j,float k,double l,struct_3 m) { }
//
// Basically the signature is: (all float regs full, 1 double, struct_3);
//
// The double argument occurs before pre spill in the argument iteration and
// computes an argOffset of 0. struct_3 offset becomes 8. This is wrong.
// Because struct_3 is prespilled and double occurs after prespill.
// The correct offsets are double = 16 (aligned stk), struct_3 = 0..12,
// Offset 12 will be skipped for double alignment of double.
//
// Another example is (struct_2, all float regs full, double, struct_2);
// Here, notice the order is similarly messed up because of 2 pre-spilled
// struct_2.
//
// Succinctly,
// ARG_INDEX(i) > ARG_INDEX(j) DOES NOT IMPLY |ARG_OFFSET(i)| > |ARG_OFFSET(j)|
//
// Therefore, we'll do a two pass offset calculation, one that considers pre-spill
// and the next, stack args.
//
unsigned argLcls = 0;
// Take care of pre spill registers first.
regMaskTP preSpillMask = codeGen->regSet.rsMaskPreSpillRegs(false);
regMaskTP tempMask = RBM_NONE;
for (unsigned i = 0, preSpillLclNum = lclNum; i < argSigLen; ++i, ++preSpillLclNum)
{
if (lvaIsPreSpilled(preSpillLclNum, preSpillMask))
{
unsigned argSize = eeGetArgSize(argLst, &info.compMethodInfo->args);
argOffs = lvaAssignVirtualFrameOffsetToArg(
preSpillLclNum,
argSize,
argOffs);
argLcls++;
// Early out if we can. If size is 8 and base reg is 2, then the mask is 0x1100
tempMask |= ((((1 << (roundUp(argSize) / REGSIZE_BYTES))) - 1) << lvaTable[preSpillLclNum].lvArgReg);
if (tempMask == preSpillMask)
{
// We won't encounter more pre-spilled registers,
// so don't bother iterating further.
break;
}
}
argLst = info.compCompHnd->getArgNext(argLst);
}
// Take care of non pre-spilled stack arguments.
argLst = info.compMethodInfo->args.args;
for (unsigned i = 0, stkLclNum = lclNum; i < argSigLen; ++i, ++stkLclNum)
{
if (!lvaIsPreSpilled(stkLclNum, preSpillMask))
{
argOffs = lvaAssignVirtualFrameOffsetToArg(
stkLclNum,
eeGetArgSize(argLst, &info.compMethodInfo->args),
argOffs);
argLcls++;
}
argLst = info.compCompHnd->getArgNext(argLst);
}
lclNum += argLcls;
#else // !_TARGET_ARM_
for (unsigned i = 0; i < argSigLen; i++)
{
unsigned argumentSize = eeGetArgSize(argLst, &info.compMethodInfo->args);
#ifdef FEATURE_UNIX_AMD64_STRUCT_PASSING
// On the stack frame the homed arg always takes a full number of slots
// for proper stack alignment. Make sure the real struct size is properly rounded up.
argumentSize = (unsigned)roundUp(argumentSize, TARGET_POINTER_SIZE);
#endif // FEATURE_UNIX_AMD64_STRUCT_PASSING
argOffs = lvaAssignVirtualFrameOffsetToArg(lclNum++,
argumentSize,
argOffs
UNIX_AMD64_ABI_ONLY_ARG(&callerArgOffset));
argLst = info.compCompHnd->getArgNext(argLst);
}
#endif // !_TARGET_ARM_
#if !USER_ARGS_COME_LAST
//@GENERICS: extra argument for instantiation info
if (info.compMethodInfo->args.callConv & CORINFO_CALLCONV_PARAMTYPE)
{
noway_assert(lclNum == (unsigned)info.compTypeCtxtArg);
argOffs = lvaAssignVirtualFrameOffsetToArg(lclNum++, sizeof(void *), argOffs UNIX_AMD64_ABI_ONLY_ARG(&callerArgOffset));
}
if (info.compIsVarArgs)
{
argOffs = lvaAssignVirtualFrameOffsetToArg(lclNum++, sizeof(void *), argOffs UNIX_AMD64_ABI_ONLY_ARG(&callerArgOffset));
}
#endif // USER_ARGS_COME_LAST
}
#ifdef UNIX_AMD64_ABI
//
// lvaAssignVirtualFrameOffsetToArg() : Assign virtual stack offsets to an
// individual argument, and return the offset for the next argument.
// Note: This method only calculates the initial offset of the stack passed/spilled arguments
// (if any - the RA might decide to spill(home on the stack) register passed arguments, if rarely used.)
// The final offset is calculated in lvaFixVirtualFrameOffsets method. It accounts for FP existance,
// ret address slot, stack frame padding, alloca instructions, etc.
// Note: This is the implementation for UNIX_AMD64 System V platforms.
//
int Compiler::lvaAssignVirtualFrameOffsetToArg(unsigned lclNum, unsigned argSize, int argOffs UNIX_AMD64_ABI_ONLY_ARG(int * callerArgOffset))
{
noway_assert(lclNum < info.compArgsCount);
noway_assert(argSize);
if (Target::g_tgtArgOrder == Target::ARG_ORDER_L2R)
argOffs -= argSize;
unsigned fieldVarNum = BAD_VAR_NUM;
noway_assert(lclNum < lvaCount);
LclVarDsc * varDsc = lvaTable + lclNum;
if (varDsc->lvPromotedStruct())
{
noway_assert(varDsc->lvFieldCnt == 1); // We only handle one field here
fieldVarNum = varDsc->lvFieldLclStart;
lvaPromotionType promotionType = lvaGetPromotionType(varDsc);
if (promotionType == PROMOTION_TYPE_INDEPENDENT)
{
lclNum = fieldVarNum;
noway_assert(lclNum < lvaCount);
varDsc = lvaTable + lclNum;
assert(varDsc->lvIsStructField);
}
}
noway_assert(varDsc->lvIsParam);
if (varDsc->lvIsRegArg)
{
// Argument is passed in a register, don't count it
// when updating the current offset on the stack.
if (varDsc->lvOnFrame)
{
// The offset for args needs to be set only for the stack homed arguments for System V.
varDsc->lvStkOffs = argOffs;
}
else
{
varDsc->lvStkOffs = 0;
}
}
else
{
// For Windows AMD64 there are 4 slots for the register passed arguments on the top of the caller's stack. This is where they are always homed.
// So, they can be accessed with positive offset.
// On System V platforms, if the RA decides to home a register passed arg on the stack,
// it creates a stack location on the callee stack (like any other local var.) In such a case, the register passed, stack homed arguments
// are accessed using negative offsets and the stack passed arguments are accessed using positive offset (from the caller's stack.)
// For System V platforms if there is no frame pointer the caller stack parameter offset should include the callee allocated space.
// If frame register is used, the callee allocated space should not be included for accessing the caller stack parameters.
// The last two requirements are met in lvaFixVirtualFrameOffsets method, which fixes the offsets, based on frame pointer existence,
// existence of alloca instructions, ret address pushed, ets.
varDsc->lvStkOffs = *callerArgOffset;
// Structs passed on stack could be of size less than TARGET_POINTER_SIZE.
// Make sure they get at least TARGET_POINTER_SIZE on the stack - this is required for alignment.
if (argSize > TARGET_POINTER_SIZE)
{
*callerArgOffset += (int)roundUp(argSize, TARGET_POINTER_SIZE);
}
else
{
*callerArgOffset += TARGET_POINTER_SIZE;
}
}
// For struct promoted parameters we need to set the offsets for both LclVars.
//
// For a dependent promoted struct we also assign the struct fields stack offset
if (varDsc->lvPromotedStruct())
{
lvaPromotionType promotionType = lvaGetPromotionType(varDsc);
if (promotionType == PROMOTION_TYPE_DEPENDENT)
{
noway_assert(varDsc->lvFieldCnt == 1); // We only handle one field here
assert(fieldVarNum == varDsc->lvFieldLclStart);
lvaTable[fieldVarNum].lvStkOffs = varDsc->lvStkOffs;
}
}
// For an independent promoted struct field we also assign the parent struct stack offset
else if (varDsc->lvIsStructField)
{
noway_assert(varDsc->lvParentLcl < lvaCount);
lvaTable[varDsc->lvParentLcl].lvStkOffs = varDsc->lvStkOffs;
}
if (Target::g_tgtArgOrder == Target::ARG_ORDER_R2L && !varDsc->lvIsRegArg)
argOffs += argSize;
return argOffs;
}
#else // !UNIX_AMD64_ABI
//
// lvaAssignVirtualFrameOffsetToArg() : Assign virtual stack offsets to an
// individual argument, and return the offset for the next argument.
// Note: This method only calculates the initial offset of the stack passed/spilled arguments
// (if any - the RA might decide to spill(home on the stack) register passed arguments, if rarely used.)
// The final offset is calculated in lvaFixVirtualFrameOffsets method. It accounts for FP existance,
// ret address slot, stack frame padding, alloca instructions, etc.
// Note: This implementation for all the platforms but UNIX_AMD64 OSs (System V 64 bit.)
int Compiler::lvaAssignVirtualFrameOffsetToArg(unsigned lclNum, unsigned argSize, int argOffs UNIX_AMD64_ABI_ONLY_ARG(int * callerArgOffset))
{
noway_assert(lclNum < info.compArgsCount);
noway_assert(argSize);
if (Target::g_tgtArgOrder == Target::ARG_ORDER_L2R)
argOffs -= argSize;
unsigned fieldVarNum = BAD_VAR_NUM;
noway_assert(lclNum < lvaCount);
LclVarDsc * varDsc = lvaTable + lclNum;
if (varDsc->lvPromotedStruct())
{
noway_assert(varDsc->lvFieldCnt == 1); // We only handle one field here
fieldVarNum = varDsc->lvFieldLclStart;
lvaPromotionType promotionType = lvaGetPromotionType(varDsc);
if (promotionType == PROMOTION_TYPE_INDEPENDENT)
{
lclNum = fieldVarNum;
noway_assert(lclNum < lvaCount);
varDsc = lvaTable + lclNum;
assert(varDsc->lvIsStructField);
}
}
noway_assert(varDsc->lvIsParam);
if (varDsc->lvIsRegArg)
{
/* Argument is passed in a register, don't count it
* when updating the current offset on the stack */
#if !defined(_TARGET_ARMARCH_)
// TODO: Remove this noway_assert and replace occurrences of sizeof(void *) with argSize
// Also investigate why we are incrementing argOffs for X86 as this seems incorrect
//
#if DEBUG
noway_assert(argSize == sizeof(void *));
#endif // DEBUG
#endif
#if defined(_TARGET_X86_)
argOffs += sizeof(void *);
#elif defined(_TARGET_AMD64_)
// Register arguments on AMD64 also takes stack space. (in the backing store)
varDsc->lvStkOffs = argOffs;
argOffs += sizeof(void *);
#elif defined(_TARGET_ARM64_)
// Register arguments on ARM64 only take stack space when they have a frame home.
#elif defined(_TARGET_ARM_)
// On ARM we spill the registers in codeGen->regSet.rsMaskPreSpillRegArg
// in the prolog, so we have to fill in lvStkOffs here
//
regMaskTP regMask = genRegMask(varDsc->lvArgReg);
if (codeGen->regSet.rsMaskPreSpillRegArg & regMask)
{
// Signature: void foo(struct_8, int, struct_4)
// ------- CALLER SP -------
// r3 struct_4
// r2 int - not prespilled, but added for alignment. argOffs should skip this.
// r1 struct_8
// r0 struct_8
// -------------------------
// If we added alignment we need to fix argOffs for all registers above alignment.
if (codeGen->regSet.rsMaskPreSpillAlign != RBM_NONE)
{
assert(genCountBits(codeGen->regSet.rsMaskPreSpillAlign) == 1);
// Is register beyond the alignment pos?
if (regMask > codeGen->regSet.rsMaskPreSpillAlign)
{
// Increment argOffs just once for the _first_ register after alignment pos
// in the prespill mask.
if (!BitsBetween(codeGen->regSet.rsMaskPreSpillRegArg, regMask, codeGen->regSet.rsMaskPreSpillAlign))
{
argOffs += TARGET_POINTER_SIZE;
}
}
}
switch (varDsc->lvType)
{
case TYP_STRUCT:
if (!varDsc->lvStructDoubleAlign)
{
break;
}
__fallthrough;
case TYP_DOUBLE:
case TYP_LONG:
{
//
// Let's assign offsets to arg1, a double in r2. argOffs has to be 4 not 8.
//
// ------- CALLER SP -------
// r3
// r2 double -- argOffs = 4, but it doesn't need to be skipped, because there is no skipping.
// r1 VACookie -- argOffs = 0
// -------------------------
//
// Consider argOffs as if it accounts for number of prespilled registers before the current register.
// In the above example, for r2, it is r1 that is prespilled, but since r1 is accounted for by argOffs
// being 4, there should have been no skipping. Instead, if we didn't assign r1 to any variable, then
// argOffs would still be 0 which implies it is not accounting for r1, equivalently r1 is skipped.
//
// If prevRegsSize is unaccounted for by a corresponding argOffs, we must have skipped a register.
int prevRegsSize = genCountBits(codeGen->regSet.rsMaskPreSpillRegArg & (regMask - 1)) * TARGET_POINTER_SIZE;
if (argOffs < prevRegsSize)
{
// We must align up the argOffset to a multiple of 8 to account for skipped registers.
argOffs = roundUp(argOffs, 2 * TARGET_POINTER_SIZE);
}
// We should've skipped only a single register.
assert(argOffs == prevRegsSize);
}
break;
default:
// No alignment of argOffs required
break;
}
varDsc->lvStkOffs = argOffs;
argOffs += argSize;
}
#else // _TARGET_*
#error Unsupported or unset target architecture
#endif // _TARGET_*
}
else
{
#if defined(_TARGET_ARM_)
// Dev11 Bug 42817: incorrect codegen for DrawFlatCheckBox causes A/V in WinForms
//
// Here we have method with a signature (int a1, struct a2, struct a3, int a4, int a5).
// Struct parameter 'a2' is 16-bytes with no alignment requirements;
// it uses r1,r2,r3 and [OutArg+0] when passed.
// Struct parameter 'a3' is 16-bytes that is required to be double aligned;
// the caller skips [OutArg+4] and starts the argument at [OutArg+8].
// Thus the caller generates the correct code to pass the arguments.
// When generating code to receive the arguments we set codeGen->regSet.rsMaskPreSpillRegArg to [r1,r2,r3]
// and spill these three registers as the first instruction in the prolog.
// Then when we layout the arguments' stack offsets we have an argOffs 0 which
// points at the location that we spilled r1 into the stack. For this first
// struct we take the lvIsRegArg path above with "codeGen->regSet.rsMaskPreSpillRegArg &" matching.
// Next when we calculate the argOffs for the second 16-byte struct we have an argOffs
// of 16, which appears to be aligned properly so we don't skip a stack slot.
//
// To fix this we must recover the actual OutArg offset by subtracting off the
// sizeof of the PreSpill register args.
// Then we align this offset to a multiple of 8 and add back the sizeof
// of the PreSpill register args.
//
// Dev11 Bug 71767: failure of assert(sizeofPreSpillRegArgs <= argOffs)
//
// We have a method with 'this' passed in r0, RetBuf arg in r1, VarArgs cookie
// in r2. The first user arg is a 144 byte struct with double alignment required,
// r3 is skipped, and the struct is passed on the stack. However, 'r3' is added
// to the codeGen->regSet.rsMaskPreSpillRegArg mask by the VarArgs cookie code, since we need to
// home all the potential varargs arguments in registers, even if we don't have
// signature type information for the variadic arguments. However, due to alignment,
// we have skipped a register that doesn't have a corresponding symbol. Make up
// for that by increasing argOffs here.
//
int sizeofPreSpillRegArgs = genCountBits(codeGen->regSet.rsMaskPreSpillRegs(true)) * REGSIZE_BYTES;
if (argOffs < sizeofPreSpillRegArgs)
{
// This can only happen if we skipped the last register spot because current stk arg
// is a struct requiring alignment or a pre-spill alignment was required because the
// first reg arg needed alignment.
//
// Example 1: First Stk Argument requiring alignment in vararg case (same as above comment.)
// Signature (int a0, int a1, int a2, struct {long} a3, ...)
//
// stk arg a3 --> argOffs here will be 12 (r0-r2) but pre-spill will be 16.
// ---- Caller SP ----
// r3 --> Stack slot is skipped in this case.
// r2 int a2
// r1 int a1
// r0 int a0
//
// Example 2: First Reg Argument requiring alignment in no-vararg case.
// Signature (struct {long} a0, struct {int} a1, int a2, int a3)
//
// stk arg --> argOffs here will be 12 {r0-r2} but pre-spill will be 16.
// ---- Caller SP ----
// r3 int a2 --> pushed (not pre-spilled) for alignment of a0 by lvaInitUserArgs.
// r2 struct { int } a1
// r0-r1 struct { long } a0
#ifdef PROFILING_SUPPORTED
// On Arm under profiler, r0-r3 are always prespilled on stack.
// It is possible to have methods that accept only HFAs as parameters e.g. Signature(struct hfa1, struct hfa2)
// In which case hfa1 and hfa2 will be en-registered in co-processor registers and will have an argument offset
// less than size of preSpill.
//
// For this reason the following conditions are asserted when not under profiler.
if (!compIsProfilerHookNeeded())
#endif
{
bool cond = (info.compIsVarArgs &&
// Does cur stk arg require double alignment?
((varDsc->lvType == TYP_STRUCT && varDsc->lvStructDoubleAlign) ||
(varDsc->lvType == TYP_DOUBLE) ||
(varDsc->lvType == TYP_LONG))
) ||
// Did first reg arg require alignment?
(codeGen->regSet.rsMaskPreSpillAlign & genRegMask(REG_ARG_LAST));
noway_assert(cond);
noway_assert(sizeofPreSpillRegArgs <= argOffs + TARGET_POINTER_SIZE); // at most one register of alignment
}
argOffs = sizeofPreSpillRegArgs;
}
noway_assert(argOffs >= sizeofPreSpillRegArgs);
int argOffsWithoutPreSpillRegArgs = argOffs - sizeofPreSpillRegArgs;
switch (varDsc->lvType)
{
case TYP_STRUCT:
if (!varDsc->lvStructDoubleAlign)
break;
__fallthrough;
case TYP_DOUBLE:
case TYP_LONG:
// We must align up the argOffset to a multiple of 8
argOffs = roundUp(argOffsWithoutPreSpillRegArgs, 2 * TARGET_POINTER_SIZE) + sizeofPreSpillRegArgs;
break;
default:
// No alignment of argOffs required
break;
}
#endif // _TARGET_ARM_
varDsc->lvStkOffs = argOffs;
}
// For struct promoted parameters we need to set the offsets for both LclVars.
//
// For a dependent promoted struct we also assign the struct fields stack offset
#if !defined(_TARGET_64BIT_)
if ((varDsc->TypeGet() == TYP_LONG) && varDsc->lvPromoted)
{
noway_assert(varDsc->lvFieldCnt == 2);
fieldVarNum = varDsc->lvFieldLclStart;
lvaTable[fieldVarNum].lvStkOffs = varDsc->lvStkOffs;
lvaTable[fieldVarNum + 1].lvStkOffs = varDsc->lvStkOffs + genTypeSize(TYP_INT);
}
else
#endif // !defined(_TARGET_64BIT_)
if (varDsc->lvPromotedStruct())
{
lvaPromotionType promotionType = lvaGetPromotionType(varDsc);
if (promotionType == PROMOTION_TYPE_DEPENDENT)
{
noway_assert(varDsc->lvFieldCnt == 1); // We only handle one field here
assert(fieldVarNum == varDsc->lvFieldLclStart);
lvaTable[fieldVarNum].lvStkOffs = varDsc->lvStkOffs;
}
}
// For an independent promoted struct field we also assign the parent struct stack offset
else if (varDsc->lvIsStructField)
{
noway_assert(varDsc->lvParentLcl < lvaCount);
lvaTable[varDsc->lvParentLcl].lvStkOffs = varDsc->lvStkOffs;
}
if (Target::g_tgtArgOrder == Target::ARG_ORDER_R2L && !varDsc->lvIsRegArg)
argOffs += argSize;
return argOffs;
}
#endif // !UNIX_AMD64_ABI
/*****************************************************************************
* lvaAssignVirtualFrameOffsetsToLocals() : Assign virtual stack offsets to
* locals, temps, and anything else. These will all be negative offsets
* (stack grows down) relative to the virtual '0'/return address
*/
void Compiler::lvaAssignVirtualFrameOffsetsToLocals()
{
int stkOffs = 0;
// codeGen->isFramePointerUsed is set in regalloc phase. Initialize it to a guess for pre-regalloc layout.
if (lvaDoneFrameLayout <= PRE_REGALLOC_FRAME_LAYOUT)
codeGen->setFramePointerUsed(codeGen->isFramePointerRequired());
#ifdef _TARGET_XARCH_
// On x86/amd64, the return address has already been pushed by the call instruction in the caller.
stkOffs -= sizeof(void *); // return address;
// TODO-AMD64-CQ: for X64 eventually this should be pushed with all the other
// calleeregs. When you fix this, you'll also need to fix
// the assert at the bottom of this method
if (codeGen->doubleAlignOrFramePointerUsed())
{
stkOffs -= REGSIZE_BYTES;
}
#endif //_TARGET_XARCH_
int preSpillSize = 0;
bool mustDoubleAlign = false;
#ifdef _TARGET_ARM_
mustDoubleAlign = true;
preSpillSize = genCountBits(codeGen->regSet.rsMaskPreSpillRegs(true)) * REGSIZE_BYTES;
#else // !_TARGET_ARM_
#if DOUBLE_ALIGN
if (genDoubleAlign())
{
mustDoubleAlign = true; // X86 only
}
#endif
#endif // !_TARGET_ARM_
#ifdef _TARGET_ARM64_
// If the frame pointer is used, then we'll save FP/LR at the bottom of the stack.
// Otherwise, we won't store FP, and we'll store LR at the top, with the other callee-save
// registers (if any).
if (isFramePointerUsed())
{
// Subtract off FP and LR.
assert(compCalleeRegsPushed >= 2);
stkOffs -= (compCalleeRegsPushed - 2) * REGSIZE_BYTES;
}
else
{
stkOffs -= compCalleeRegsPushed * REGSIZE_BYTES;
}
#else // !_TARGET_ARM64_
stkOffs -= compCalleeRegsPushed * REGSIZE_BYTES;
#endif // !_TARGET_ARM64_
compLclFrameSize = 0;
#ifdef _TARGET_AMD64_
// In case of Amd64 compCalleeRegsPushed includes float regs (Xmm6-xmm15) that
// need to be pushed. But Amd64 doesn't support push/pop of xmm registers.
// Instead we need to allocate space for them on the stack and save them in prolog.
// Therefore, we consider xmm registers being saved while computing stack offsets
// but space for xmm registers is considered part of compLclFrameSize.
// Notes
// 1) We need to save the entire 128-bits of xmm register to stack, since amd64
// prolog unwind codes allow encoding of an instruction that stores the entire xmm reg
// at an offset relative to SP
// 2) We adjust frame size so that SP is aligned at 16-bytes after pushing integer registers.
// This means while saving the first xmm register to its allocated stack location we might
// have to skip 8-bytes. The reason for padding is to use efficient "movaps" to save/restore
// xmm registers to/from stack to match Jit64 codegen. Without the aligning on 16-byte
// boundary we would have to use movups when offset turns out unaligned. Movaps is more
// performant than movups.
unsigned calleeFPRegsSavedSize = genCountBits(compCalleeFPRegsSavedMask) * XMM_REGSIZE_BYTES;
if (calleeFPRegsSavedSize > 0 && ((stkOffs % XMM_REGSIZE_BYTES) != 0))
{
// Take care of alignment
int alignPad = (int)AlignmentPad((unsigned)-stkOffs, XMM_REGSIZE_BYTES);
stkOffs -= alignPad;
lvaIncrementFrameSize(alignPad);
}
stkOffs -= calleeFPRegsSavedSize;
lvaIncrementFrameSize(calleeFPRegsSavedSize);
// Quirk for VS debug-launch scenario to work
if (compVSQuirkStackPaddingNeeded > 0)
{
#ifdef DEBUG
if (verbose)
{
printf("\nAdding VS quirk stack padding of %d bytes between save-reg area and locals\n", compVSQuirkStackPaddingNeeded);
}
#endif // DEBUG
stkOffs -= compVSQuirkStackPaddingNeeded;
lvaIncrementFrameSize(compVSQuirkStackPaddingNeeded);
}
#endif //_TARGET_AMD64_
#if FEATURE_EH_FUNCLETS && defined(_TARGET_ARMARCH_)
if (ehNeedsPSPSym())
{
// On ARM/ARM64, if we need a PSPSym, allocate it first, before anything else, including
// padding (so we can avoid computing the same padding in the funclet
// frame). Note that there is no special padding requirement for the PSPSym.
noway_assert(codeGen->isFramePointerUsed()); // We need an explicit frame pointer
assert(lvaPSPSym != BAD_VAR_NUM); // We should have created the PSPSym variable
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaPSPSym, TARGET_POINTER_SIZE, stkOffs);
}
#endif // FEATURE_EH_FUNCLETS && defined(_TARGET_ARMARCH_)
if (mustDoubleAlign)
{
if (lvaDoneFrameLayout != FINAL_FRAME_LAYOUT)
{
// Allocate a pointer sized stack slot, since we may need to double align here
// when lvaDoneFrameLayout == FINAL_FRAME_LAYOUT
//
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
// If we have any TYP_LONG, TYP_DOUBLE or double aligned structs
// then we need to allocate a second pointer sized stack slot,
// since we may need to double align that LclVar when we see it
// in the loop below. We will just always do this so that the
// offsets that we calculate for the stack frame will always
// be greater (or equal) to what they can be in the final layout.
//
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
else // FINAL_FRAME_LAYOUT
{
if (((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) != 0)
{
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
// We should now have a double-aligned (stkOffs+preSpillSize)
noway_assert(((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) == 0);
}
}
if (lvaMonAcquired != BAD_VAR_NUM)
{
// This var must go first, in what is called the 'frame header' for EnC so that it is
// preserved when remapping occurs. See vm\eetwain.cpp for detailed comment specifying frame
// layout requirements for EnC to work.
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaMonAcquired, lvaLclSize(lvaMonAcquired), stkOffs);
}
if (opts.compNeedSecurityCheck)
{
/* This can't work without an explicit frame, so make sure */
#ifdef JIT32_GCENCODER
noway_assert(codeGen->isFramePointerUsed());
#endif
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaSecurityObject, TARGET_POINTER_SIZE, stkOffs);
}
if (compLocallocUsed)
{
#ifdef JIT32_GCENCODER
noway_assert(codeGen->isFramePointerUsed()); // else offsets of locals of frameless methods will be incorrect
#endif
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaLocAllocSPvar, TARGET_POINTER_SIZE, stkOffs);
}
if (lvaReportParamTypeArg())
{
#ifdef JIT32_GCENCODER
noway_assert(codeGen->isFramePointerUsed());
#endif
// For CORINFO_CALLCONV_PARAMTYPE (if needed)
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
lvaCachedGenericContextArgOffs = stkOffs;
}
#ifndef JIT32_GCENCODER
else if (lvaKeepAliveAndReportThis())
{
// When "this" is also used as generic context arg.
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
lvaCachedGenericContextArgOffs = stkOffs;
}
#endif
#if !FEATURE_EH_FUNCLETS
/* If we need space for slots for shadow SP, reserve it now */
if (ehNeedsShadowSPslots())
{
noway_assert(codeGen->isFramePointerUsed()); // else offsets of locals of frameless methods will be incorrect
if (!lvaReportParamTypeArg())
{
#ifndef JIT32_GCENCODER
if (!lvaKeepAliveAndReportThis())
#endif
{
// In order to keep the gc info encoding smaller, the VM assumes that all methods with EH
// have also saved space for a ParamTypeArg, so we need to do that here
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
}
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaShadowSPslotsVar, lvaLclSize(lvaShadowSPslotsVar), stkOffs);
}
#endif // !FEATURE_EH_FUNCLETS
if (compGSReorderStackLayout)
{
assert(getNeedsGSSecurityCookie());
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaGSSecurityCookie, lvaLclSize(lvaGSSecurityCookie), stkOffs);
}
/*
If we're supposed to track lifetimes of pointer temps, we'll
assign frame offsets in the following order:
non-pointer local variables (also untracked pointer variables)
pointer local variables
pointer temps
non-pointer temps
*/
enum Allocation{
ALLOC_NON_PTRS = 0x1, // assign offsets to non-ptr
ALLOC_PTRS = 0x2, // Second pass, assign offsets to tracked ptrs
ALLOC_UNSAFE_BUFFERS = 0x4,
ALLOC_UNSAFE_BUFFERS_WITH_PTRS = 0x8
};
UINT alloc_order[5];
unsigned int cur = 0;
if (compGSReorderStackLayout)
{
noway_assert(getNeedsGSSecurityCookie());
if (codeGen->isFramePointerUsed())
{
alloc_order[cur++] = ALLOC_UNSAFE_BUFFERS;
alloc_order[cur++] = ALLOC_UNSAFE_BUFFERS_WITH_PTRS;
}
}
bool tempsAllocated = false;
#ifdef _TARGET_ARM_
// On ARM, SP based offsets use smaller encoding. Since temps are relatively
// rarer than lcl usage, allocate them farther from SP.
if (!opts.MinOpts() && !compLocallocUsed)
#else
if (lvaTempsHaveLargerOffsetThanVars() && !codeGen->isFramePointerUsed())
#endif
{
// Because we want the temps to have a larger offset than locals
// and we're not using a frame pointer, we have to place the temps
// above the vars. Otherwise we place them after the vars (at the
// bottom of the frame).
noway_assert(!tempsAllocated);
stkOffs = lvaAllocateTemps(stkOffs, mustDoubleAlign);
tempsAllocated = true;
}
alloc_order[cur++] = ALLOC_NON_PTRS;
if (opts.compDbgEnC)
{
/* We will use just one pass, and assign offsets to all variables */
alloc_order[cur-1] |= ALLOC_PTRS;
noway_assert(compGSReorderStackLayout == false);
}
else
{
alloc_order[cur++] = ALLOC_PTRS;
}
if (!codeGen->isFramePointerUsed() && compGSReorderStackLayout)
{
alloc_order[cur++] = ALLOC_UNSAFE_BUFFERS_WITH_PTRS;
alloc_order[cur++] = ALLOC_UNSAFE_BUFFERS;
}
alloc_order[cur] = 0;
noway_assert(cur < sizeof(alloc_order)/sizeof(alloc_order[0]));
// Force first pass to happen
UINT assignMore = 0xFFFFFFFF;
bool have_LclVarDoubleAlign = false;
for (cur = 0; alloc_order[cur]; cur++)
{
if ((assignMore & alloc_order[cur]) == 0)
continue;
assignMore = 0;
unsigned lclNum;
LclVarDsc * varDsc;
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
/* Ignore field locals of the promotion type PROMOTION_TYPE_FIELD_DEPENDENT.
In other words, we will not calculate the "base" address of the struct local if
the promotion type is PROMOTION_TYPE_FIELD_DEPENDENT.
*/
if (lvaIsFieldOfDependentlyPromotedStruct(varDsc))
{
continue;
}
#if FEATURE_FIXED_OUT_ARGS
// The scratch mem is used for the outgoing arguments, and it must be absolutely last
if (lclNum == lvaOutgoingArgSpaceVar)
{
continue;
}
#endif
bool allocateOnFrame = varDsc->lvOnFrame;
if (varDsc->lvRegister &&
(lvaDoneFrameLayout == REGALLOC_FRAME_LAYOUT) &&
((varDsc->TypeGet() != TYP_LONG) || (varDsc->lvOtherReg != REG_STK)))
{
allocateOnFrame = false;
}
/* Ignore variables that are not on the stack frame */
if (!allocateOnFrame)
{
/* For EnC, all variables have to be allocated space on the
stack, even though they may actually be enregistered. This
way, the frame layout can be directly inferred from the
locals-sig.
*/
if (!opts.compDbgEnC)
continue;
else if (lclNum >= info.compLocalsCount) // ignore temps for EnC
continue;
}
else if (lvaGSSecurityCookie == lclNum && getNeedsGSSecurityCookie())
{
continue; // This is allocated outside of this loop.
}
// These need to be located as the very first variables (highest memory address)
// and so they have already been assigned an offset
if (
#if FEATURE_EH_FUNCLETS
lclNum == lvaPSPSym ||
#else
lclNum == lvaShadowSPslotsVar ||
#endif // FEATURE_EH_FUNCLETS
lclNum == lvaLocAllocSPvar ||
lclNum == lvaSecurityObject)
{
assert(varDsc->lvStkOffs != BAD_STK_OFFS);
continue;
}
if (lclNum == lvaMonAcquired)
continue;
// This should be low on the stack. Hence, it will be assigned later.
if (lclNum == lvaStubArgumentVar)
{
#ifdef JIT32_GCENCODER
noway_assert(codeGen->isFramePointerUsed());
#endif
continue;
}
// This should be low on the stack. Hence, it will be assigned later.
if (lclNum == lvaInlinedPInvokeFrameVar)
{
noway_assert(codeGen->isFramePointerUsed());
continue;
}
if (varDsc->lvIsParam)
{
#if defined(_TARGET_AMD64_) && !defined(UNIX_AMD64_ABI)
// On Windows AMD64 we can use the caller-reserved stack area that is already setup
assert(varDsc->lvStkOffs != BAD_STK_OFFS);
continue;
#else // !_TARGET_AMD64_
// A register argument that is not enregistered ends up as
// a local variable which will need stack frame space.
//
if (!varDsc->lvIsRegArg)
continue;
#ifdef _TARGET_ARM_
// On ARM we spill the registers in codeGen->regSet.rsMaskPreSpillRegArg
// in the prolog, thus they don't need stack frame space.
//
if ((codeGen->regSet.rsMaskPreSpillRegs(false) & genRegMask(varDsc->lvArgReg)) != 0)
{
assert(varDsc->lvStkOffs != BAD_STK_OFFS);
continue;
}
#endif
#endif // !_TARGET_AMD64_
}
/* Make sure the type is appropriate */
if (varDsc->lvIsUnsafeBuffer && compGSReorderStackLayout)
{
if (varDsc->lvIsPtr)
{
if ((alloc_order[cur] & ALLOC_UNSAFE_BUFFERS_WITH_PTRS) == 0)
{
assignMore |= ALLOC_UNSAFE_BUFFERS_WITH_PTRS;
continue;
}
}
else
{
if ((alloc_order[cur] & ALLOC_UNSAFE_BUFFERS) == 0)
{
assignMore |= ALLOC_UNSAFE_BUFFERS;
continue;
}
}
}
else if (varTypeIsGC(varDsc->TypeGet()) && varDsc->lvTracked)
{
if ((alloc_order[cur] & ALLOC_PTRS) == 0)
{
assignMore |= ALLOC_PTRS;
continue;
}
}
else
{
if ((alloc_order[cur] & ALLOC_NON_PTRS) == 0)
{
assignMore |= ALLOC_NON_PTRS;
continue;
}
}
/* Need to align the offset? */
if (mustDoubleAlign && (
varDsc->lvType == TYP_DOUBLE // Align doubles for ARM and x86
#ifdef _TARGET_ARM_
|| varDsc->lvType == TYP_LONG // Align longs for ARM
#endif
#ifndef _TARGET_64BIT_
|| varDsc->lvStructDoubleAlign // Align when lvStructDoubleAlign is true
#endif // !_TARGET_64BIT_
))
{
noway_assert((compLclFrameSize % TARGET_POINTER_SIZE) == 0);
if ((lvaDoneFrameLayout != FINAL_FRAME_LAYOUT) && !have_LclVarDoubleAlign)
{
// If this is the first TYP_LONG, TYP_DOUBLE or double aligned struct
// then we have seen in this loop then we allocate a pointer sized
// stack slot since we may need to double align this LclVar
// when lvaDoneFrameLayout == FINAL_FRAME_LAYOUT
//
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
else
{
if (((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) != 0)
{
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
// We should now have a double-aligned (stkOffs+preSpillSize)
noway_assert(((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) == 0);
}
// Remember that we had to double align a LclVar
have_LclVarDoubleAlign = true;
}
// Reserve the stack space for this variable
stkOffs = lvaAllocLocalAndSetVirtualOffset(lclNum, lvaLclSize(lclNum), stkOffs);
#ifdef _TARGET_ARM64_
// If we have an incoming register argument that has a struct promoted field
// then we need to copy the lvStkOff (the stack home) from the reg arg to the field lclvar
//
if (varDsc->lvIsRegArg && varDsc->lvPromotedStruct())
{
noway_assert(varDsc->lvFieldCnt == 1); // We only handle one field here
unsigned fieldVarNum = varDsc->lvFieldLclStart;
lvaTable[fieldVarNum].lvStkOffs = varDsc->lvStkOffs;
}
#endif
}
}
if (getNeedsGSSecurityCookie() && !compGSReorderStackLayout)
{
// LOCALLOC used, but we have no unsafe buffer. Allocated cookie last, close to localloc buffer.
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaGSSecurityCookie, lvaLclSize(lvaGSSecurityCookie), stkOffs);
}
if (tempsAllocated == false)
{
/*-------------------------------------------------------------------------
*
* Now the temps
*
*-------------------------------------------------------------------------
*/
stkOffs = lvaAllocateTemps(stkOffs, mustDoubleAlign);
}
/*-------------------------------------------------------------------------
*
* Now do some final stuff
*
*-------------------------------------------------------------------------
*/
// lvaInlinedPInvokeFrameVar and lvaStubArgumentVar need to be assigned last
// Important: The stack walker depends on lvaStubArgumentVar immediately
// following lvaInlinedPInvokeFrameVar in the frame.
if (lvaStubArgumentVar != BAD_VAR_NUM)
{
#ifdef JIT32_GCENCODER
noway_assert(codeGen->isFramePointerUsed());
#endif
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaStubArgumentVar, lvaLclSize(lvaStubArgumentVar), stkOffs);
}
if (lvaInlinedPInvokeFrameVar != BAD_VAR_NUM)
{
noway_assert(codeGen->isFramePointerUsed());
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaInlinedPInvokeFrameVar, lvaLclSize(lvaInlinedPInvokeFrameVar), stkOffs);
}
if (mustDoubleAlign)
{
if (lvaDoneFrameLayout != FINAL_FRAME_LAYOUT)
{
// Allocate a pointer sized stack slot, since we may need to double align here
// when lvaDoneFrameLayout == FINAL_FRAME_LAYOUT
//
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
if (have_LclVarDoubleAlign)
{
// If we have any TYP_LONG, TYP_DOUBLE or double aligned structs
// the we need to allocate a second pointer sized stack slot,
// since we may need to double align the last LclVar that we saw
// in the loop above. We do this so that the offsets that we
// calculate for the stack frame are always greater than they will
// be in the final layout.
//
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
}
else // FINAL_FRAME_LAYOUT
{
if (((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) != 0)
{
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
// We should now have a double-aligned (stkOffs+preSpillSize)
noway_assert(((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) == 0);
}
}
#if FEATURE_EH_FUNCLETS && defined(_TARGET_AMD64_)
if (ehNeedsPSPSym())
{
// On AMD64, if we need a PSPSym, allocate it last, immediately above the outgoing argument
// space. Any padding will be higher on the stack than this
// (including the padding added by lvaAlignFrame()). Here, we will give it an offset, but really we
// will set its value at the end of lvaFixVirtualFrameOffsets, as for lvaOutgoingArgSpace.
// There is a comment above that the P/Invoke vars "need to be assigned last". We are ignoring
// that here (TODO-AMD64-Bug?: is that ok? JIT64 does things this way).
noway_assert(codeGen->isFramePointerUsed()); // We need an explicit frame pointer
assert(lvaPSPSym != BAD_VAR_NUM); // We should have created the PSPSym variable
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaPSPSym, TARGET_POINTER_SIZE, stkOffs);
}
#endif // FEATURE_EH_FUNCLETS && defined(_TARGET_AMD64_)
#ifdef _TARGET_ARM64_
if (isFramePointerUsed())
{
// Create space for saving FP and LR.
stkOffs -= 2 * REGSIZE_BYTES;
}
#endif // _TARGET_ARM64_
#if FEATURE_FIXED_OUT_ARGS
if (lvaOutgoingArgSpaceSize > 0)
{
#if defined(_TARGET_AMD64_) && !defined(UNIX_AMD64_ABI) // No 4 slots for outgoing params on System V.
noway_assert(lvaOutgoingArgSpaceSize >= (4 * sizeof(void*)));
#endif
noway_assert((lvaOutgoingArgSpaceSize % sizeof(void*)) == 0);
// Give it a value so we can avoid asserts in CHK builds.
// Since this will always use an SP relative offset of zero
// at the end of lvaFixVirtualFrameOffsets, it will be set to absolute '0'
stkOffs = lvaAllocLocalAndSetVirtualOffset(lvaOutgoingArgSpaceVar, lvaLclSize(lvaOutgoingArgSpaceVar), stkOffs);
}
#endif // FEATURE_FIXED_OUT_ARGS
// compLclFrameSize equals our negated virtual stack offset minus the pushed registers and return address
// and the pushed frame pointer register which for some strange reason isn't part of 'compCalleeRegsPushed'.
int pushedCount = compCalleeRegsPushed;
#ifdef _TARGET_XARCH_
if (codeGen->doubleAlignOrFramePointerUsed())
pushedCount += 1; // pushed EBP (frame pointer)
pushedCount += 1; // pushed PC (return address)
#endif
noway_assert(compLclFrameSize == (unsigned)-(stkOffs + (pushedCount * (int) sizeof(void *))));
}
int Compiler::lvaAllocLocalAndSetVirtualOffset(unsigned lclNum, unsigned size, int stkOffs)
{
noway_assert(lclNum != BAD_VAR_NUM);
#ifdef _TARGET_64BIT_
// Before final frame layout, assume the worst case, that every >=8 byte local will need
// maximum padding to be aligned. This is because we generate code based on the stack offset
// computed during tentative frame layout. These offsets cannot get bigger during final
// frame layout, as that would possibly require different code generation (for example,
// using a 4-byte offset instead of a 1-byte offset in an instruction). The offsets can get
// smaller. It is possible there is different alignment at the point locals are allocated
// between tentative and final frame layout which would introduce padding between locals
// and thus increase the offset (from the stack pointer) of one of the locals. Hence the
// need to assume the worst alignment before final frame layout.
// We could probably improve this by sorting all the objects by alignment,
// such that all 8 byte objects are together, 4 byte objects are together, etc., which
// would require at most one alignment padding per group.
//
// TYP_SIMD structs locals have alignment preference given by getSIMDTypeAlignment() for
// better performance.
if ((size >= 8) &&
((lvaDoneFrameLayout != FINAL_FRAME_LAYOUT) ||
((stkOffs % 8) != 0)
#if defined(FEATURE_SIMD) && ALIGN_SIMD_TYPES
|| lclVarIsSIMDType(lclNum)
#endif
))
{
// Note that stack offsets are negative
assert(stkOffs < 0);
// alignment padding
unsigned pad = 0;
#if defined(FEATURE_SIMD) && ALIGN_SIMD_TYPES
if (lclVarIsSIMDType(lclNum) && !lvaIsImplicitByRefLocal(lclNum))
{
int alignment = getSIMDTypeAlignment(lvaTable[lclNum].lvType);
if (stkOffs % alignment != 0)
{
if (lvaDoneFrameLayout != FINAL_FRAME_LAYOUT)
{
pad = alignment-1;
// Note that all the objects will probably be misaligned, but we'll fix that in final layout.
}
else
{
pad = alignment + (stkOffs % alignment); // +1 to +(alignment-1) bytes
}
}
}
else
#endif // FEATURE_SIMD && ALIGN_SIMD_TYPES
{
if (lvaDoneFrameLayout != FINAL_FRAME_LAYOUT)
{
pad = 7;
// Note that all the objects will probably be misaligned, but we'll fix that in final layout.
}
else
{
pad = 8 + (stkOffs % 8); // +1 to +7 bytes
}
}
// Will the pad ever be anything except 4? Do we put smaller-than-4-sized objects on the stack?
lvaIncrementFrameSize(pad);
stkOffs -= pad;
#ifdef DEBUG
if (verbose)
{
printf("Pad ");
gtDispLclVar(lclNum, /*pad*/ false);
printf(", size=%d, stkOffs=%c0x%x, pad=%d\n",
size,
stkOffs < 0 ? '-' : '+',
stkOffs < 0 ? -stkOffs : stkOffs,
pad);
}
#endif
}
#endif // _TARGET_64BIT_
/* Reserve space on the stack by bumping the frame size */
lvaIncrementFrameSize(size);
stkOffs -= size;
lvaTable[lclNum].lvStkOffs = stkOffs;
#ifdef DEBUG
if (verbose)
{
printf("Assign ");
gtDispLclVar(lclNum, /*pad*/ false);
printf(", size=%d, stkOffs=%c0x%x\n",
size,
stkOffs < 0 ? '-' : '+',
stkOffs < 0 ? -stkOffs : stkOffs);
}
#endif
return stkOffs;
}
#ifdef _TARGET_AMD64_
/*****************************************************************************
* lvaIsCalleeSavedIntRegCountEven() : returns true if the number of integer registers
* pushed onto stack is even including RBP if used as frame pointer
*
* Note that this excludes return address (PC) pushed by caller. To know whether
* the SP offset after pushing integer registers is aligned, we need to take
* negation of this routine.
*/
bool Compiler::lvaIsCalleeSavedIntRegCountEven()
{
unsigned regsPushed = compCalleeRegsPushed + (codeGen->isFramePointerUsed() ? 1 : 0);
return (regsPushed % (16/REGSIZE_BYTES)) == 0;
}
#endif //_TARGET_AMD64_
/*****************************************************************************
* lvaAlignFrame() : After allocating everything on the frame, reserve any
* extra space needed to keep the frame aligned
*/
void Compiler::lvaAlignFrame()
{
#if defined(_TARGET_AMD64_)
// Leaf frames do not need full alignment, but the unwind info is smaller if we
// are at least 8 byte aligned (and we assert as much)
if ((compLclFrameSize % 8) != 0)
{
lvaIncrementFrameSize(8 - (compLclFrameSize % 8));
}
else if (lvaDoneFrameLayout != FINAL_FRAME_LAYOUT)
{
// If we are not doing final layout, we don't know the exact value of compLclFrameSize
// and thus do not know how much we will need to add in order to be aligned.
// We add 8 so compLclFrameSize is still a multiple of 8.
lvaIncrementFrameSize(8);
}
assert((compLclFrameSize % 8) == 0);
// Ensure that the stack is always 16-byte aligned by grabbing an unused QWORD
// if needed, but off by 8 because of the return value.
// And don't forget that compCalleeRegsPused does *not* include RBP if we are
// using it as the frame pointer.
//
bool regPushedCountAligned = lvaIsCalleeSavedIntRegCountEven();
bool lclFrameSizeAligned = (compLclFrameSize % 16) == 0;
// If this isn't the final frame layout, assume we have to push an extra QWORD
// Just so the offsets are true upper limits.
#ifdef UNIX_AMD64_ABI
// The compNeedToAlignFrame flag is indicating if there is a need to align the frame.
// On AMD64-Windows, if there are calls, 4 slots for the outgoing ars are allocated, except for
// FastTailCall. This slots makes the frame size non-zero, so alignment logic will be called.
// On AMD64-Unix, there are no such slots. There is a possibility to have calls in the method with frame size of 0.
// The frame alignment logic won't kick in. This flags takes care of the AMD64-Unix case by remembering that there
// are calls and making sure the frame alignment logic is executed.
bool stackNeedsAlignment = (compLclFrameSize != 0 || opts.compNeedToAlignFrame);
#else // !UNIX_AMD64_ABI
bool stackNeedsAlignment = compLclFrameSize != 0;
#endif // !UNIX_AMD64_ABI
if ((!codeGen->isFramePointerUsed() && (lvaDoneFrameLayout != FINAL_FRAME_LAYOUT)) ||
(stackNeedsAlignment && (regPushedCountAligned == lclFrameSizeAligned)))
{
lvaIncrementFrameSize(REGSIZE_BYTES);
}
#elif defined(_TARGET_ARM64_)
// The stack on ARM64 must be 16 byte aligned.
// First, align up to 8.
if ((compLclFrameSize % 8) != 0)
{
lvaIncrementFrameSize(8 - (compLclFrameSize % 8));
}
else if (lvaDoneFrameLayout != FINAL_FRAME_LAYOUT)
{
// If we are not doing final layout, we don't know the exact value of compLclFrameSize
// and thus do not know how much we will need to add in order to be aligned.
// We add 8 so compLclFrameSize is still a multiple of 8.
lvaIncrementFrameSize(8);
}
assert((compLclFrameSize % 8) == 0);
// Ensure that the stack is always 16-byte aligned by grabbing an unused QWORD
// if needed.
bool regPushedCountAligned = (compCalleeRegsPushed % (16/REGSIZE_BYTES)) == 0;
bool lclFrameSizeAligned = (compLclFrameSize % 16) == 0;
// If this isn't the final frame layout, assume we have to push an extra QWORD
// Just so the offsets are true upper limits.
if ((lvaDoneFrameLayout != FINAL_FRAME_LAYOUT) ||
(regPushedCountAligned != lclFrameSizeAligned))
{
lvaIncrementFrameSize(REGSIZE_BYTES);
}
#elif defined(_TARGET_ARM_)
// Ensure that stack offsets will be double-aligned by grabbing an unused DWORD if needed.
//
bool lclFrameSizeAligned = (compLclFrameSize % sizeof(double)) == 0;
bool regPushedCountAligned = ((compCalleeRegsPushed + genCountBits(codeGen->regSet.rsMaskPreSpillRegs(true)))
% (sizeof(double) / sizeof(void *))) == 0;
if (regPushedCountAligned != lclFrameSizeAligned)
{
lvaIncrementFrameSize(sizeof(void *));
}
#elif defined(_TARGET_X86_)
if (genDoubleAlign())
{
// Double Frame Alignement for x86 is handled in Compiler::lvaAssignVirtualFrameOffsetsToLocals()
if (compLclFrameSize == 0)
{
// This can only happen with JitStress=1 or JitDoubleAlign=2
lvaIncrementFrameSize(sizeof(void*));
}
}
#else
NYI("TARGET specific lvaAlignFrame");
#endif // !_TARGET_AMD64_
}
/*****************************************************************************
* lvaAssignFrameOffsetsToPromotedStructs() : Assign offsets to fields
* within a promoted struct (worker for lvaAssignFrameOffsets).
*/
void Compiler::lvaAssignFrameOffsetsToPromotedStructs()
{
LclVarDsc * varDsc = lvaTable;
for (unsigned lclNum = 0; lclNum < lvaCount; lclNum++, varDsc++)
{
// For promoted struct fields that are params, we will
// assign their offsets in lvaAssignVirtualFrameOffsetToArg().
// This is not true for the System V systems since there is no
// outgoing args space. Assign the dependently promoted fields properly.
//
if (varDsc->lvIsStructField
#ifndef UNIX_AMD64_ABI
// For System V platforms there is no outgoing args space.
// A register passed struct arg is homed on the stack in a separate local var.
// The offset of these structs is already calculated in lvaAssignVirtualFrameOffsetToArg methos.
// Make sure the code below is not executed for these structs and the offset is not changed.
&& !varDsc->lvIsParam
#endif // UNIX_AMD64_ABI
)
{
LclVarDsc * parentvarDsc = &lvaTable[varDsc->lvParentLcl];
lvaPromotionType promotionType = lvaGetPromotionType(parentvarDsc);
if (promotionType == PROMOTION_TYPE_INDEPENDENT)
{
// The stack offset for these field locals must have been calculated
// by the normal frame offset assignment.
continue;
}
else
{
noway_assert(promotionType == PROMOTION_TYPE_DEPENDENT);
noway_assert(varDsc->lvOnFrame);
varDsc->lvStkOffs = parentvarDsc->lvStkOffs + varDsc->lvFldOffset;
}
}
}
}
/*****************************************************************************
* lvaAllocateTemps() : Assign virtual offsets to temps (always negative).
*/
int Compiler::lvaAllocateTemps(int stkOffs, bool mustDoubleAlign)
{
unsigned spillTempSize = 0;
if (lvaDoneFrameLayout == FINAL_FRAME_LAYOUT)
{
int preSpillSize = 0;
#ifdef _TARGET_ARM_
preSpillSize = genCountBits(codeGen->regSet.rsMaskPreSpillRegs(true)) * TARGET_POINTER_SIZE;
#endif
bool assignDone;
bool assignNptr;
bool assignPtrs = true;
/* Allocate temps */
if (TRACK_GC_TEMP_LIFETIMES)
{
/* first pointers, then non-pointers in second pass */
assignNptr = false;
assignDone = false;
}
else
{
/* Pointers and non-pointers together in single pass */
assignNptr = true;
assignDone = true;
}
assert(tmpAllFree());
AGAIN2:
for (TempDsc* temp = tmpListBeg();
temp != nullptr;
temp = tmpListNxt(temp))
{
var_types tempType = temp->tdTempType();
unsigned size;
/* Make sure the type is appropriate */
if (!assignPtrs && varTypeIsGC(tempType))
continue;
if (!assignNptr && !varTypeIsGC(tempType))
continue;
size = temp->tdTempSize();
/* Figure out and record the stack offset of the temp */
/* Need to align the offset? */
#ifdef _TARGET_64BIT_
if (varTypeIsGC(tempType) && ((stkOffs % TARGET_POINTER_SIZE) != 0))
{
// Calculate 'pad' as the number of bytes to align up 'stkOffs' to be a multiple of TARGET_POINTER_SIZE
// In practice this is really just a fancy way of writing 4. (as all stack locations are at least 4-byte aligned)
// Note stkOffs is always negative, so (stkOffs % TARGET_POINTER_SIZE) yields a negative value.
//
int alignPad = (int)AlignmentPad((unsigned)-stkOffs, TARGET_POINTER_SIZE);
spillTempSize += alignPad;
lvaIncrementFrameSize(alignPad);
stkOffs -= alignPad;
noway_assert((stkOffs % TARGET_POINTER_SIZE) == 0);
}
#endif
if (mustDoubleAlign && (tempType == TYP_DOUBLE)) // Align doubles for x86 and ARM
{
noway_assert((compLclFrameSize % TARGET_POINTER_SIZE) == 0);
if (((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) != 0)
{
spillTempSize += TARGET_POINTER_SIZE;
lvaIncrementFrameSize(TARGET_POINTER_SIZE);
stkOffs -= TARGET_POINTER_SIZE;
}
// We should now have a double-aligned (stkOffs+preSpillSize)
noway_assert(((stkOffs+preSpillSize) % (2*TARGET_POINTER_SIZE)) == 0);
}
spillTempSize += size;
lvaIncrementFrameSize(size);
stkOffs -= size;
temp->tdSetTempOffs(stkOffs);
}
#ifdef _TARGET_ARM_
// Only required for the ARM platform that we have an accurate estimate for the spillTempSize
noway_assert(spillTempSize <= lvaGetMaxSpillTempSize());
#endif
/* If we've only assigned some temps, go back and do the rest now */
if (!assignDone)
{
assignNptr = !assignNptr;
assignPtrs = !assignPtrs;
assignDone = true;
goto AGAIN2;
}
}
else // We haven't run codegen, so there are no Spill temps yet!
{
unsigned size = lvaGetMaxSpillTempSize();
lvaIncrementFrameSize(size);
stkOffs -= size;
}
return stkOffs;
}
#ifdef DEBUG
/*****************************************************************************
*
* Dump the register a local is in right now.
* For non-LSRA, this will be the register it is always in. For LSRA, it's only the current
* location, since the location changes and it is updated throughout code generation based on
* LSRA register assignments.
*/
void Compiler::lvaDumpRegLocation(unsigned lclNum)
{
LclVarDsc * varDsc = lvaTable + lclNum;
var_types type = varDsc->TypeGet();
#if FEATURE_STACK_FP_X87
if (varTypeIsFloating(type))
{
printf("fpu stack ");
}
else
#endif
if (isRegPairType(type))
{
if (!doLSRA()) noway_assert(varDsc->lvRegNum != REG_STK);
if (doLSRA() && varDsc->lvRegNum == REG_STK)
{
/* Hi-only enregistered long */
int offset = varDsc->lvStkOffs;
printf("%-3s:[%1s0x%02X]",
getRegName(varDsc->lvOtherReg), // hi32
(offset < 0 ? "-" : "+"),
(offset < 0 ? -offset : offset));
}
else if (varDsc->lvOtherReg != REG_STK)
{
/* Fully enregistered long */
printf("%3s:%-3s ",
getRegName(varDsc->lvOtherReg), // hi32
getRegName(varDsc->lvRegNum)); // lo32
}
else
{
/* Partially enregistered long */
int offset = varDsc->lvStkOffs+4;
printf("[%1s0x%02X]:%-3s",
(offset < 0 ? "-" : "+"),
(offset < 0 ? -offset : offset),
getRegName(varDsc->lvRegNum)); // lo32
}
}
#ifdef _TARGET_ARM_
else if (varDsc->TypeGet() == TYP_DOUBLE)
{
printf("%3s:%-3s ", getRegName(varDsc->lvRegNum), getRegName(varDsc->lvOtherReg));
}
#endif
else
{
printf("%3s ", getRegName(varDsc->lvRegNum));
}
}
/*****************************************************************************
*
* Dump the frame location assigned to a local.
* For non-LSRA, this will only be valid if there is no assigned register.
* For LSRA, it's the home location, even though the variable doesn't always live
* in its home location.
*/
void Compiler::lvaDumpFrameLocation(unsigned lclNum)
{
int offset;
regNumber baseReg;
#ifdef _TARGET_ARM_
offset = lvaFrameAddress(lclNum, compLocallocUsed, &baseReg, 0);
#else
bool EBPbased;
offset = lvaFrameAddress(lclNum, &EBPbased);
baseReg = EBPbased ? REG_FPBASE : REG_SPBASE;
#endif
printf("[%2s%1s0x%02X] ",
getRegName(baseReg),
(offset < 0 ? "-" : "+"),
(offset < 0 ? -offset : offset));
}
/*****************************************************************************
*
* dump a single lvaTable entry
*/
void Compiler::lvaDumpEntry(unsigned lclNum, FrameLayoutState curState, size_t refCntWtdWidth)
{
LclVarDsc * varDsc = lvaTable + lclNum;
var_types type = varDsc->TypeGet();
if (curState == INITIAL_FRAME_LAYOUT)
{
printf("; ");
gtDispLclVar(lclNum);
printf(" %7s ", varTypeName(type));
if (genTypeSize(type) == 0)
printf("(%2d) ", lvaLclSize(lclNum));
}
else
{
if (varDsc->lvRefCnt == 0)
{
// Print this with a special indicator that the variable is unused. Even though the
// variable itself is unused, it might be a struct that is promoted, so seeing it
// can be useful when looking at the promoted struct fields. It's also weird to see
// missing var numbers if these aren't printed.
printf(";* ");
}
else
#if FEATURE_FIXED_OUT_ARGS
if ((lclNum == lvaOutgoingArgSpaceVar) && (lvaLclSize(lclNum) == 0))
{
// Similar to above; print this anyway.
printf(";# ");
}
else
#endif
{
printf("; ");
}
gtDispLclVar(lclNum);
printf("[V%02u", lclNum);
if (varDsc->lvTracked) printf(",T%02u]", varDsc->lvVarIndex);
else printf(" ]");
printf(" (%3u,%*s)",
varDsc->lvRefCnt,
(int)refCntWtdWidth,
refCntWtd2str(varDsc->lvRefCntWtd));
printf(" %7s ", varTypeName(type));
if (genTypeSize(type) == 0)
printf("(%2d) ", lvaLclSize(lclNum));
else
printf(" -> ");
// The register or stack location field is 11 characters wide.
if (varDsc->lvRefCnt == 0)
{
printf("zero-ref ");
}
else if (varDsc->lvRegister != 0)
{
// It's always a register, and always in the same register.
lvaDumpRegLocation(lclNum);
}
else if (varDsc->lvOnFrame == 0)
{
printf("multi-reg ");
}
else
{
// For RyuJIT backend, it might be in a register part of the time, but it will definitely have a stack home location.
// Otherwise, it's always on the stack.
if (lvaDoneFrameLayout != NO_FRAME_LAYOUT)
lvaDumpFrameLocation(lclNum);
}
}
#ifdef _TARGET_ARM_
if (varDsc->lvIsHfaRegArg)
{
if (varDsc->lvHfaTypeIsFloat)
{
printf(" (enregistered HFA: float) ");
}
else
{
printf(" (enregistered HFA: double)");
}
}
#endif // _TARGET_ARM_
if (varDsc->lvDoNotEnregister)
{
printf(" do-not-enreg[");
if (varDsc->lvAddrExposed) printf("X");
if (varTypeIsStruct(varDsc)) printf("S");
if (varDsc->lvVMNeedsStackAddr) printf("V");
if (varDsc->lvLiveInOutOfHndlr) printf("H");
if (varDsc->lvLclFieldExpr) printf("F");
if (varDsc->lvLclBlockOpAddr) printf("B");
if (varDsc->lvLiveAcrossUCall) printf("U");
#ifdef JIT32_GCENCODER
if (varDsc->lvPinned) printf("P");
#endif // JIT32_GCENCODER
printf("]");
}
if (varDsc->lvMustInit) printf(" must-init");
if (varDsc->lvAddrExposed) printf(" addr-exposed");
if (varDsc->lvHasLdAddrOp) printf(" ld-addr-op");
if (varDsc->lvVerTypeInfo.IsThisPtr()) printf(" this");
if (varDsc->lvPinned) printf(" pinned");
if (varDsc->lvRefAssign) printf(" ref-asgn");
if (varDsc->lvStackByref) printf(" stack-byref");
#ifndef _TARGET_64BIT_
if (varDsc->lvStructDoubleAlign) printf(" double-align");
#endif // !_TARGET_64BIT_
if (varDsc->lvOverlappingFields) printf(" overlapping-fields");
if (compGSReorderStackLayout && !varDsc->lvRegister)
{
if (varDsc->lvIsPtr) printf(" ptr");
if (varDsc->lvIsUnsafeBuffer) printf(" unsafe-buffer");
}
if (varDsc->lvIsStructField)
{
LclVarDsc * parentvarDsc = &lvaTable[varDsc->lvParentLcl];
#if !defined(_TARGET_64BIT_)
if (varTypeIsLong(parentvarDsc))
{
bool isLo = (lclNum == parentvarDsc->lvFieldLclStart);
printf(" V%02u.%s(offs=0x%02x)",
varDsc->lvParentLcl,
isLo ? "lo" : "hi",
isLo ? 0 : genTypeSize(TYP_INT)
);
}
else
#endif // !defined(_TARGET_64BIT_)
{
CORINFO_CLASS_HANDLE typeHnd = parentvarDsc->lvVerTypeInfo.GetClassHandle();
CORINFO_FIELD_HANDLE fldHnd = info.compCompHnd->getFieldInClass(typeHnd, varDsc->lvFldOrdinal);
printf(" V%02u.%s(offs=0x%02x)",
varDsc->lvParentLcl,
eeGetFieldName(fldHnd),
varDsc->lvFldOffset
);
lvaPromotionType promotionType = lvaGetPromotionType(parentvarDsc);
// We should never have lvIsStructField set if it is a reg-sized non-field-addressed struct.
assert(!varDsc->lvRegStruct);
switch (promotionType)
{
case PROMOTION_TYPE_NONE: printf(" P-NONE"); break;
case PROMOTION_TYPE_DEPENDENT: printf(" P-DEP"); break;
case PROMOTION_TYPE_INDEPENDENT: printf(" P-INDEP"); break;
}
}
}
printf("\n");
}
/*****************************************************************************
*
* dump the lvaTable
*/
void Compiler::lvaTableDump(FrameLayoutState curState)
{
if (curState == NO_FRAME_LAYOUT)
{
curState = lvaDoneFrameLayout;
if (curState == NO_FRAME_LAYOUT)
{
// Still no layout? Could be a bug, but just display the initial layout
curState = INITIAL_FRAME_LAYOUT;
}
}
if (curState == INITIAL_FRAME_LAYOUT)
printf("; Initial");
else if (curState == PRE_REGALLOC_FRAME_LAYOUT)
printf("; Pre-RegAlloc");
else if (curState == REGALLOC_FRAME_LAYOUT)
printf("; RegAlloc");
else if (curState == TENTATIVE_FRAME_LAYOUT)
printf("; Tentative");
else if (curState == FINAL_FRAME_LAYOUT)
printf("; Final");
else
{
printf("UNKNOWN FrameLayoutState!");
unreached();
}
printf(" local variable assignments\n");
printf(";\n");
unsigned lclNum;
LclVarDsc * varDsc;
// Figure out some sizes, to help line things up
size_t refCntWtdWidth = 6; // Use 6 as the minimum width
if (curState != INITIAL_FRAME_LAYOUT) // don't need this info for INITIAL_FRAME_LAYOUT
{
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
size_t width = strlen(refCntWtd2str(varDsc->lvRefCntWtd));
if (width > refCntWtdWidth)
refCntWtdWidth = width;
}
}
// Do the actual output
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
lvaDumpEntry(lclNum, curState, refCntWtdWidth);
}
//-------------------------------------------------------------------------
// Display the code-gen temps
assert(tmpAllFree());
for (TempDsc* temp = tmpListBeg();
temp != nullptr;
temp = tmpListNxt(temp))
{
printf("; TEMP_%02u %26s%*s%7s -> ",
-temp->tdTempNum(),
" ",
refCntWtdWidth,
" ",
varTypeName(temp->tdTempType()));
int offset = temp->tdTempOffs();
printf(" [%2s%1s0x%02X]\n",
isFramePointerUsed() ? STR_FPBASE : STR_SPBASE,
(offset < 0 ? "-" : "+"),
(offset < 0 ? -offset : offset));
}
if (curState >= TENTATIVE_FRAME_LAYOUT)
{
printf(";\n");
printf("; Lcl frame size = %d\n", compLclFrameSize);
}
}
#endif // DEBUG
/*****************************************************************************
*
* Conservatively estimate the layout of the stack frame.
*
* This function is only used before final frame layout. It conservatively estimates the
* number of callee-saved registers that must be saved, then calls lvaAssignFrameOffsets().
* To do final frame layout, the callee-saved registers are known precisely, so
* lvaAssignFrameOffsets() is called directly.
*
* Returns the (conservative, that is, overly large) estimated size of the frame,
* including the callee-saved registers. This is only used by the emitter during code
* generation when estimating the size of the offset of instructions accessing temps,
* and only if temps have a larger offset than variables.
*/
unsigned Compiler::lvaFrameSize(FrameLayoutState curState)
{
assert(curState < FINAL_FRAME_LAYOUT);
unsigned result;
/* Layout the stack frame conservatively.
Assume all callee-saved registers are spilled to stack */
compCalleeRegsPushed = CNT_CALLEE_SAVED;
#if defined(_TARGET_ARMARCH_)
if (compFloatingPointUsed)
compCalleeRegsPushed += CNT_CALLEE_SAVED_FLOAT;
compCalleeRegsPushed++; // we always push LR. See genPushCalleeSavedRegisters
#elif defined(_TARGET_AMD64_)
if (compFloatingPointUsed)
compCalleeFPRegsSavedMask = RBM_FLT_CALLEE_SAVED;
else
compCalleeFPRegsSavedMask = RBM_NONE;
#endif
#if DOUBLE_ALIGN
if (genDoubleAlign())
{
// X86 only - account for extra 4-byte pad that may be created by "and esp, -8" instruction
compCalleeRegsPushed++;
}
#endif
#ifdef _TARGET_XARCH_
// Since FP/EBP is included in the SAVED_REG_MAXSZ we need to
// subtract 1 register if codeGen->isFramePointerUsed() is true.
if (codeGen->isFramePointerUsed())
compCalleeRegsPushed--;
#endif
lvaAssignFrameOffsets(curState);
unsigned calleeSavedRegMaxSz = CALLEE_SAVED_REG_MAXSZ;
#if defined(_TARGET_ARMARCH_)
if (compFloatingPointUsed)
{
calleeSavedRegMaxSz += CALLEE_SAVED_FLOAT_MAXSZ;
}
calleeSavedRegMaxSz += REGSIZE_BYTES; // we always push LR. See genPushCalleeSavedRegisters
#endif
result = compLclFrameSize + calleeSavedRegMaxSz;
return result;
}
//------------------------------------------------------------------------
// lvaGetSPRelativeOffset: Given a variable, return the offset of that
// variable in the frame from the stack pointer. This number will be positive,
// since the stack pointer must be at a lower address than everything on the
// stack.
//
// This can't be called for localloc functions, since the stack pointer
// varies, and thus there is no fixed offset to a variable from the stack pointer.
//
// Arguments:
// varNum - the variable number
//
// Return Value:
// The offset.
int Compiler::lvaGetSPRelativeOffset(unsigned varNum)
{
assert(!compLocallocUsed);
assert(lvaDoneFrameLayout == FINAL_FRAME_LAYOUT);
assert(varNum < lvaCount);
const LclVarDsc* varDsc = lvaTable + varNum;
assert(varDsc->lvOnFrame);
int spRelativeOffset;
if (varDsc->lvFramePointerBased)
{
// The stack offset is relative to the frame pointer, so convert it to be
// relative to the stack pointer (which makes no sense for localloc functions).
spRelativeOffset = varDsc->lvStkOffs + codeGen->genSPtoFPdelta();
}
else
{
spRelativeOffset = varDsc->lvStkOffs;
}
assert(spRelativeOffset >= 0);
return spRelativeOffset;
}
/*****************************************************************************
*
* Return the caller-SP-relative stack offset of a local/parameter.
* Requires the local to be on the stack and frame layout to be complete.
*/
int Compiler::lvaGetCallerSPRelativeOffset(unsigned varNum)
{
assert(lvaDoneFrameLayout == FINAL_FRAME_LAYOUT);
assert(varNum < lvaCount);
LclVarDsc * varDsc = lvaTable + varNum;
assert(varDsc->lvOnFrame);
return lvaToCallerSPRelativeOffset(varDsc->lvStkOffs, varDsc->lvFramePointerBased);
}
int Compiler::lvaToCallerSPRelativeOffset(int offset, bool isFpBased)
{
assert(lvaDoneFrameLayout == FINAL_FRAME_LAYOUT);
if (isFpBased)
{
offset += codeGen->genCallerSPtoFPdelta();
}
else
{
offset += codeGen->genCallerSPtoInitialSPdelta();
}
return offset;
}
/*****************************************************************************
*
* Return the Initial-SP-relative stack offset of a local/parameter.
* Requires the local to be on the stack and frame layout to be complete.
*/
int Compiler::lvaGetInitialSPRelativeOffset(unsigned varNum)
{
assert(lvaDoneFrameLayout == FINAL_FRAME_LAYOUT);
assert(varNum < lvaCount);
LclVarDsc * varDsc = lvaTable + varNum;
assert(varDsc->lvOnFrame);
return lvaToInitialSPRelativeOffset(varDsc->lvStkOffs, varDsc->lvFramePointerBased);
}
// Given a local variable offset, and whether that offset is frame-pointer based, return its offset from Initial-SP.
// This is used, for example, to figure out the offset of the frame pointer from Initial-SP.
int Compiler::lvaToInitialSPRelativeOffset(unsigned offset, bool isFpBased)
{
assert(lvaDoneFrameLayout == FINAL_FRAME_LAYOUT);
#ifdef _TARGET_AMD64_
if (isFpBased)
{
// Currently, the frame starts by pushing ebp, ebp points to the saved ebp
// (so we have ebp pointer chaining). Add the fixed-size frame size plus the
// size of the callee-saved regs (not including ebp itself) to find Initial-SP.
assert(codeGen->isFramePointerUsed());
offset += codeGen->genSPtoFPdelta();
}
else
{
// The offset is correct already!
}
#else // !_TARGET_AMD64_
NYI("lvaToInitialSPRelativeOffset");
#endif // !_TARGET_AMD64_
return offset;
}
/*****************************************************************************/
#ifdef DEBUG
/*****************************************************************************
* Pick a padding size at "random" for the local.
* 0 means that it should not be converted to a GT_LCL_FLD
*/
static
unsigned LCL_FLD_PADDING(unsigned lclNum)
{
// Convert every 2nd variable
if (lclNum % 2)
return 0;
// Pick a padding size at "random"
unsigned size = lclNum % 7;
return size;
}
/*****************************************************************************
*
* Callback for fgWalkAllTreesPre()
* Convert as many GT_LCL_VAR's to GT_LCL_FLD's
*/
/* static */
/*
The stress mode does 2 passes.
In the first pass we will mark the locals where we CAN't apply the stress mode.
In the second pass we will do the appropiate morphing wherever we've not determined we can't do it.
*/
Compiler::fgWalkResult Compiler::lvaStressLclFldCB(GenTreePtr *pTree, fgWalkData *data)
{
GenTreePtr tree = *pTree;
genTreeOps oper = tree->OperGet();
GenTreePtr lcl;
switch (oper)
{
case GT_LCL_VAR:
lcl = tree;
break;
case GT_ADDR:
if (tree->gtOp.gtOp1->gtOper != GT_LCL_VAR)
return WALK_CONTINUE;
lcl = tree->gtOp.gtOp1;
break;
default:
return WALK_CONTINUE;
}
Compiler * pComp = ((lvaStressLclFldArgs*)data->pCallbackData)->m_pCompiler;
bool bFirstPass = ((lvaStressLclFldArgs*)data->pCallbackData)->m_bFirstPass;
noway_assert(lcl->gtOper == GT_LCL_VAR);
unsigned lclNum = lcl->gtLclVarCommon.gtLclNum;
var_types type = lcl->TypeGet();
LclVarDsc * varDsc = &pComp->lvaTable[lclNum];
if (varDsc->lvNoLclFldStress)
{
// Already determined we can't do anything for this var
return WALK_SKIP_SUBTREES;
}
if (bFirstPass)
{
// Ignore arguments and temps
if (varDsc->lvIsParam || lclNum >= pComp->info.compLocalsCount)
{
varDsc->lvNoLclFldStress = true;
return WALK_SKIP_SUBTREES;
}
// Fix for lcl_fld stress mode
if (varDsc->lvKeepType)
{
varDsc->lvNoLclFldStress = true;
return WALK_SKIP_SUBTREES;
}
// Can't have GC ptrs in TYP_BLK.
if (!varTypeIsArithmetic(type))
{
varDsc->lvNoLclFldStress = true;
return WALK_SKIP_SUBTREES;
}
// Weed out "small" types like TYP_BYTE as we don't mark the GT_LCL_VAR
// node with the accurate small type. If we bash lvaTable[].lvType,
// then there will be no indication that it was ever a small type.
var_types varType = varDsc->TypeGet();
if (varType != TYP_BLK &&
genTypeSize(varType) != genTypeSize(genActualType(varType)))
{
varDsc->lvNoLclFldStress = true;
return WALK_SKIP_SUBTREES;
}
// Offset some of the local variable by a "random" non-zero amount
unsigned padding = LCL_FLD_PADDING(lclNum);
if (padding == 0)
{
varDsc->lvNoLclFldStress = true;
return WALK_SKIP_SUBTREES;
}
}
else
{
// Do the morphing
noway_assert(varDsc->lvType == lcl->gtType || varDsc->lvType == TYP_BLK);
var_types varType = varDsc->TypeGet();
// Calculate padding
unsigned padding = LCL_FLD_PADDING(lclNum);
// Change the variable to a TYP_BLK
if (varType != TYP_BLK)
{
varDsc->lvExactSize = (unsigned)(roundUp(padding + pComp->lvaLclSize(lclNum)));
varDsc->lvType = TYP_BLK;
pComp->lvaSetVarAddrExposed(lclNum);
}
tree->gtFlags |= GTF_GLOB_REF;
/* Now morph the tree appropriately */
if (oper == GT_LCL_VAR)
{
/* Change lclVar(lclNum) to lclFld(lclNum,padding) */
tree->ChangeOper(GT_LCL_FLD);
tree->gtLclFld.gtLclOffs = padding;
}
else
{
/* Change addr(lclVar) to addr(lclVar)+padding */
noway_assert(oper == GT_ADDR);
GenTreePtr newAddr = new(pComp, GT_NONE) GenTreeOp(*tree->AsOp());
tree->ChangeOper(GT_ADD);
tree->gtOp.gtOp1 = newAddr;
tree->gtOp.gtOp2 = pComp->gtNewIconNode(padding);
lcl->gtType = TYP_BLK;
}
}
return WALK_SKIP_SUBTREES;
}
/*****************************************************************************/
void Compiler::lvaStressLclFld()
{
if (!compStressCompile(STRESS_LCL_FLDS, 5))
return;
lvaStressLclFldArgs Args;
Args.m_pCompiler = this;
Args.m_bFirstPass = true;
// Do First pass
fgWalkAllTreesPre(lvaStressLclFldCB, &Args);
// Second pass
Args.m_bFirstPass = false;
fgWalkAllTreesPre(lvaStressLclFldCB, &Args);
}
/*****************************************************************************/
#endif // DEBUG
/*****************************************************************************
*
* A little routine that displays a local variable bitset.
* 'set' is mask of variables that have to be displayed
* 'allVars' is the complete set of interesting variables (blank space is
* inserted if its corresponding bit is not in 'set').
*/
#ifdef DEBUG
void Compiler::lvaDispVarSet(VARSET_VALARG_TP set)
{
VARSET_TP VARSET_INIT_NOCOPY(allVars, VarSetOps::MakeEmpty(this));
lvaDispVarSet(set, allVars);
}
void Compiler::lvaDispVarSet(VARSET_VALARG_TP set, VARSET_VALARG_TP allVars)
{
printf("{");
bool needSpace = false;
for (unsigned index = 0; index < lvaTrackedCount; index++)
{
if (VarSetOps::IsMember(this, set, index))
{
unsigned lclNum;
LclVarDsc * varDsc;
/* Look for the matching variable */
for (lclNum = 0, varDsc = lvaTable;
lclNum < lvaCount;
lclNum++ , varDsc++)
{
if ((varDsc->lvVarIndex == index) && varDsc->lvTracked)
break;
}
if (needSpace)
printf(" ");
else
needSpace = true;
printf("V%02u", lclNum);
}
else if (VarSetOps::IsMember(this, allVars, index))
{
if (needSpace)
printf(" ");
else
needSpace = true;
printf(" ");
}
}
printf("}");
}
#endif // DEBUG
|