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
|
/*
* Copyright 2014 Red Hat Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors: Ben Skeggs <bskeggs@redhat.com>
*/
#include "codegen/nv50_ir_target_gm107.h"
//#define GM107_DEBUG_SCHED_DATA
namespace nv50_ir {
class CodeEmitterGM107 : public CodeEmitter
{
public:
CodeEmitterGM107(const TargetGM107 *);
virtual bool emitInstruction(Instruction *);
virtual uint32_t getMinEncodingSize(const Instruction *) const;
virtual void prepareEmission(Program *);
virtual void prepareEmission(Function *);
inline void setProgramType(Program::Type pType) { progType = pType; }
private:
const TargetGM107 *targGM107;
Program::Type progType;
const Instruction *insn;
const bool writeIssueDelays;
uint32_t *data;
private:
inline void emitField(uint32_t *, int, int, uint32_t);
inline void emitField(int b, int s, uint32_t v) { emitField(code, b, s, v); }
inline void emitInsn(uint32_t, bool);
inline void emitInsn(uint32_t o) { emitInsn(o, true); }
inline void emitPred();
inline void emitGPR(int, const Value *);
inline void emitGPR(int pos) {
emitGPR(pos, (const Value *)NULL);
}
inline void emitGPR(int pos, const ValueRef &ref) {
emitGPR(pos, ref.get() ? ref.rep() : (const Value *)NULL);
}
inline void emitGPR(int pos, const ValueRef *ref) {
emitGPR(pos, ref ? ref->rep() : (const Value *)NULL);
}
inline void emitGPR(int pos, const ValueDef &def) {
emitGPR(pos, def.get() ? def.rep() : (const Value *)NULL);
}
inline void emitSYS(int, const Value *);
inline void emitSYS(int pos, const ValueRef &ref) {
emitSYS(pos, ref.get() ? ref.rep() : (const Value *)NULL);
}
inline void emitPRED(int, const Value *);
inline void emitPRED(int pos) {
emitPRED(pos, (const Value *)NULL);
}
inline void emitPRED(int pos, const ValueRef &ref) {
emitPRED(pos, ref.get() ? ref.rep() : (const Value *)NULL);
}
inline void emitPRED(int pos, const ValueDef &def) {
emitPRED(pos, def.get() ? def.rep() : (const Value *)NULL);
}
inline void emitADDR(int, int, int, int, const ValueRef &);
inline void emitCBUF(int, int, int, int, int, const ValueRef &);
inline bool longIMMD(const ValueRef &);
inline void emitIMMD(int, int, const ValueRef &);
void emitCond3(int, CondCode);
void emitCond4(int, CondCode);
void emitCond5(int pos, CondCode cc) { emitCond4(pos, cc); }
inline void emitO(int);
inline void emitP(int);
inline void emitSAT(int);
inline void emitCC(int);
inline void emitX(int);
inline void emitABS(int, const ValueRef &);
inline void emitNEG(int, const ValueRef &);
inline void emitNEG2(int, const ValueRef &, const ValueRef &);
inline void emitFMZ(int, int);
inline void emitRND(int, RoundMode, int);
inline void emitRND(int pos) {
emitRND(pos, insn->rnd, -1);
}
inline void emitPDIV(int);
inline void emitINV(int, const ValueRef &);
void emitEXIT();
void emitBRA();
void emitCAL();
void emitPCNT();
void emitCONT();
void emitPBK();
void emitBRK();
void emitPRET();
void emitRET();
void emitSSY();
void emitSYNC();
void emitSAM();
void emitRAM();
void emitMOV();
void emitS2R();
void emitCS2R();
void emitF2F();
void emitF2I();
void emitI2F();
void emitI2I();
void emitSEL();
void emitSHFL();
void emitDADD();
void emitDMUL();
void emitDFMA();
void emitDMNMX();
void emitDSET();
void emitDSETP();
void emitFADD();
void emitFMUL();
void emitFFMA();
void emitMUFU();
void emitFMNMX();
void emitRRO();
void emitFCMP();
void emitFSET();
void emitFSETP();
void emitFSWZADD();
void emitLOP();
void emitNOT();
void emitIADD();
void emitIMUL();
void emitIMAD();
void emitISCADD();
void emitXMAD();
void emitIMNMX();
void emitICMP();
void emitISET();
void emitISETP();
void emitSHL();
void emitSHR();
void emitSHF();
void emitPOPC();
void emitBFI();
void emitBFE();
void emitFLO();
void emitLDSTs(int, DataType);
void emitLDSTc(int);
void emitLDC();
void emitLDL();
void emitLDS();
void emitLD();
void emitSTL();
void emitSTS();
void emitST();
void emitALD();
void emitAST();
void emitISBERD();
void emitAL2P();
void emitIPA();
void emitATOM();
void emitATOMS();
void emitRED();
void emitCCTL();
void emitPIXLD();
void emitTEXs(int);
void emitTEX();
void emitTEXS();
void emitTLD();
void emitTLD4();
void emitTXD();
void emitTXQ();
void emitTMML();
void emitDEPBAR();
void emitNOP();
void emitKIL();
void emitOUT();
void emitBAR();
void emitMEMBAR();
void emitVOTE();
void emitSUTarget();
void emitSUHandle(const int s);
void emitSUSTx();
void emitSULDx();
void emitSUREDx();
};
/*******************************************************************************
* general instruction layout/fields
******************************************************************************/
void
CodeEmitterGM107::emitField(uint32_t *data, int b, int s, uint32_t v)
{
if (b >= 0) {
uint32_t m = ((1ULL << s) - 1);
uint64_t d = (uint64_t)(v & m) << b;
assert(!(v & ~m) || (v & ~m) == ~m);
data[1] |= d >> 32;
data[0] |= d;
}
}
void
CodeEmitterGM107::emitPred()
{
if (insn->predSrc >= 0) {
emitField(16, 3, insn->getSrc(insn->predSrc)->rep()->reg.data.id);
emitField(19, 1, insn->cc == CC_NOT_P);
} else {
emitField(16, 3, 7);
}
}
void
CodeEmitterGM107::emitInsn(uint32_t hi, bool pred)
{
code[0] = 0x00000000;
code[1] = hi;
if (pred)
emitPred();
}
void
CodeEmitterGM107::emitGPR(int pos, const Value *val)
{
emitField(pos, 8, val && !val->inFile(FILE_FLAGS) ?
val->reg.data.id : 255);
}
void
CodeEmitterGM107::emitSYS(int pos, const Value *val)
{
int id = val ? val->reg.data.id : -1;
switch (id) {
case SV_LANEID : id = 0x00; break;
case SV_VERTEX_COUNT : id = 0x10; break;
case SV_INVOCATION_ID : id = 0x11; break;
case SV_THREAD_KILL : id = 0x13; break;
case SV_INVOCATION_INFO: id = 0x1d; break;
case SV_COMBINED_TID : id = 0x20; break;
case SV_TID : id = 0x21 + val->reg.data.sv.index; break;
case SV_CTAID : id = 0x25 + val->reg.data.sv.index; break;
case SV_LANEMASK_EQ : id = 0x38; break;
case SV_LANEMASK_LT : id = 0x39; break;
case SV_LANEMASK_LE : id = 0x3a; break;
case SV_LANEMASK_GT : id = 0x3b; break;
case SV_LANEMASK_GE : id = 0x3c; break;
case SV_CLOCK : id = 0x50 + val->reg.data.sv.index; break;
default:
assert(!"invalid system value");
id = 0;
break;
}
emitField(pos, 8, id);
}
void
CodeEmitterGM107::emitPRED(int pos, const Value *val)
{
emitField(pos, 3, val ? val->reg.data.id : 7);
}
void
CodeEmitterGM107::emitADDR(int gpr, int off, int len, int shr,
const ValueRef &ref)
{
const Value *v = ref.get();
assert(!(v->reg.data.offset & ((1 << shr) - 1)));
if (gpr >= 0)
emitGPR(gpr, ref.getIndirect(0));
emitField(off, len, v->reg.data.offset >> shr);
}
void
CodeEmitterGM107::emitCBUF(int buf, int gpr, int off, int len, int shr,
const ValueRef &ref)
{
const Value *v = ref.get();
const Symbol *s = v->asSym();
assert(!(s->reg.data.offset & ((1 << shr) - 1)));
emitField(buf, 5, v->reg.fileIndex);
if (gpr >= 0)
emitGPR(gpr, ref.getIndirect(0));
emitField(off, 16, s->reg.data.offset >> shr);
}
bool
CodeEmitterGM107::longIMMD(const ValueRef &ref)
{
if (ref.getFile() == FILE_IMMEDIATE) {
const ImmediateValue *imm = ref.get()->asImm();
if (isFloatType(insn->sType))
return imm->reg.data.u32 & 0xfff;
else
return imm->reg.data.s32 > 0x7ffff || imm->reg.data.s32 < -0x80000;
}
return false;
}
void
CodeEmitterGM107::emitIMMD(int pos, int len, const ValueRef &ref)
{
const ImmediateValue *imm = ref.get()->asImm();
uint32_t val = imm->reg.data.u32;
if (len == 19) {
if (insn->sType == TYPE_F32 || insn->sType == TYPE_F16) {
assert(!(val & 0x00000fff));
val >>= 12;
} else if (insn->sType == TYPE_F64) {
assert(!(imm->reg.data.u64 & 0x00000fffffffffffULL));
val = imm->reg.data.u64 >> 44;
} else {
assert(!(val & 0xfff80000) || (val & 0xfff80000) == 0xfff80000);
}
emitField( 56, 1, (val & 0x80000) >> 19);
emitField(pos, len, (val & 0x7ffff));
} else {
emitField(pos, len, val);
}
}
/*******************************************************************************
* modifiers
******************************************************************************/
void
CodeEmitterGM107::emitCond3(int pos, CondCode code)
{
int data = 0;
switch (code) {
case CC_FL : data = 0x00; break;
case CC_LTU:
case CC_LT : data = 0x01; break;
case CC_EQU:
case CC_EQ : data = 0x02; break;
case CC_LEU:
case CC_LE : data = 0x03; break;
case CC_GTU:
case CC_GT : data = 0x04; break;
case CC_NEU:
case CC_NE : data = 0x05; break;
case CC_GEU:
case CC_GE : data = 0x06; break;
case CC_TR : data = 0x07; break;
default:
assert(!"invalid cond3");
break;
}
emitField(pos, 3, data);
}
void
CodeEmitterGM107::emitCond4(int pos, CondCode code)
{
int data = 0;
switch (code) {
case CC_FL: data = 0x00; break;
case CC_LT: data = 0x01; break;
case CC_EQ: data = 0x02; break;
case CC_LE: data = 0x03; break;
case CC_GT: data = 0x04; break;
case CC_NE: data = 0x05; break;
case CC_GE: data = 0x06; break;
// case CC_NUM: data = 0x07; break;
// case CC_NAN: data = 0x08; break;
case CC_LTU: data = 0x09; break;
case CC_EQU: data = 0x0a; break;
case CC_LEU: data = 0x0b; break;
case CC_GTU: data = 0x0c; break;
case CC_NEU: data = 0x0d; break;
case CC_GEU: data = 0x0e; break;
case CC_TR: data = 0x0f; break;
default:
assert(!"invalid cond4");
break;
}
emitField(pos, 4, data);
}
void
CodeEmitterGM107::emitO(int pos)
{
emitField(pos, 1, insn->getSrc(0)->reg.file == FILE_SHADER_OUTPUT);
}
void
CodeEmitterGM107::emitP(int pos)
{
emitField(pos, 1, insn->perPatch);
}
void
CodeEmitterGM107::emitSAT(int pos)
{
emitField(pos, 1, insn->saturate);
}
void
CodeEmitterGM107::emitCC(int pos)
{
emitField(pos, 1, insn->flagsDef >= 0);
}
void
CodeEmitterGM107::emitX(int pos)
{
emitField(pos, 1, insn->flagsSrc >= 0);
}
void
CodeEmitterGM107::emitABS(int pos, const ValueRef &ref)
{
emitField(pos, 1, ref.mod.abs());
}
void
CodeEmitterGM107::emitNEG(int pos, const ValueRef &ref)
{
emitField(pos, 1, ref.mod.neg());
}
void
CodeEmitterGM107::emitNEG2(int pos, const ValueRef &a, const ValueRef &b)
{
emitField(pos, 1, a.mod.neg() ^ b.mod.neg());
}
void
CodeEmitterGM107::emitFMZ(int pos, int len)
{
emitField(pos, len, insn->dnz << 1 | insn->ftz);
}
void
CodeEmitterGM107::emitRND(int rmp, RoundMode rnd, int rip)
{
int rm = 0, ri = 0;
switch (rnd) {
case ROUND_NI: ri = 1;
case ROUND_N : rm = 0; break;
case ROUND_MI: ri = 1;
case ROUND_M : rm = 1; break;
case ROUND_PI: ri = 1;
case ROUND_P : rm = 2; break;
case ROUND_ZI: ri = 1;
case ROUND_Z : rm = 3; break;
default:
assert(!"invalid round mode");
break;
}
emitField(rip, 1, ri);
emitField(rmp, 2, rm);
}
void
CodeEmitterGM107::emitPDIV(int pos)
{
assert(insn->postFactor >= -3 && insn->postFactor <= 3);
if (insn->postFactor > 0)
emitField(pos, 3, 7 - insn->postFactor);
else
emitField(pos, 3, 0 - insn->postFactor);
}
void
CodeEmitterGM107::emitINV(int pos, const ValueRef &ref)
{
emitField(pos, 1, !!(ref.mod & Modifier(NV50_IR_MOD_NOT)));
}
/*******************************************************************************
* control flow
******************************************************************************/
void
CodeEmitterGM107::emitEXIT()
{
emitInsn (0xe3000000);
emitCond5(0x00, CC_TR);
}
void
CodeEmitterGM107::emitBRA()
{
const FlowInstruction *insn = this->insn->asFlow();
int gpr = -1;
if (insn->indirect) {
if (insn->absolute)
emitInsn(0xe2000000); // JMX
else
emitInsn(0xe2500000); // BRX
gpr = 0x08;
} else {
if (insn->absolute)
emitInsn(0xe2100000); // JMP
else
emitInsn(0xe2400000); // BRA
emitField(0x07, 1, insn->allWarp);
}
emitField(0x06, 1, insn->limit);
emitCond5(0x00, CC_TR);
if (!insn->srcExists(0) || insn->src(0).getFile() != FILE_MEMORY_CONST) {
int32_t pos = insn->target.bb->binPos;
if (writeIssueDelays && !(pos & 0x1f))
pos += 8;
if (!insn->absolute)
emitField(0x14, 24, pos - (codeSize + 8));
else
emitField(0x14, 32, pos);
} else {
emitCBUF (0x24, gpr, 20, 16, 0, insn->src(0));
emitField(0x05, 1, 1);
}
}
void
CodeEmitterGM107::emitCAL()
{
const FlowInstruction *insn = this->insn->asFlow();
if (insn->absolute) {
emitInsn(0xe2200000, 0); // JCAL
} else {
emitInsn(0xe2600000, 0); // CAL
}
if (!insn->srcExists(0) || insn->src(0).getFile() != FILE_MEMORY_CONST) {
if (!insn->absolute)
emitField(0x14, 24, insn->target.bb->binPos - (codeSize + 8));
else {
if (insn->builtin) {
int pcAbs = targGM107->getBuiltinOffset(insn->target.builtin);
addReloc(RelocEntry::TYPE_BUILTIN, 0, pcAbs, 0xfff00000, 20);
addReloc(RelocEntry::TYPE_BUILTIN, 1, pcAbs, 0x000fffff, -12);
} else {
emitField(0x14, 32, insn->target.bb->binPos);
}
}
} else {
emitCBUF (0x24, -1, 20, 16, 0, insn->src(0));
emitField(0x05, 1, 1);
}
}
void
CodeEmitterGM107::emitPCNT()
{
const FlowInstruction *insn = this->insn->asFlow();
emitInsn(0xe2b00000, 0);
if (!insn->srcExists(0) || insn->src(0).getFile() != FILE_MEMORY_CONST) {
emitField(0x14, 24, insn->target.bb->binPos - (codeSize + 8));
} else {
emitCBUF (0x24, -1, 20, 16, 0, insn->src(0));
emitField(0x05, 1, 1);
}
}
void
CodeEmitterGM107::emitCONT()
{
emitInsn (0xe3500000);
emitCond5(0x00, CC_TR);
}
void
CodeEmitterGM107::emitPBK()
{
const FlowInstruction *insn = this->insn->asFlow();
emitInsn(0xe2a00000, 0);
if (!insn->srcExists(0) || insn->src(0).getFile() != FILE_MEMORY_CONST) {
emitField(0x14, 24, insn->target.bb->binPos - (codeSize + 8));
} else {
emitCBUF (0x24, -1, 20, 16, 0, insn->src(0));
emitField(0x05, 1, 1);
}
}
void
CodeEmitterGM107::emitBRK()
{
emitInsn (0xe3400000);
emitCond5(0x00, CC_TR);
}
void
CodeEmitterGM107::emitPRET()
{
const FlowInstruction *insn = this->insn->asFlow();
emitInsn(0xe2700000, 0);
if (!insn->srcExists(0) || insn->src(0).getFile() != FILE_MEMORY_CONST) {
emitField(0x14, 24, insn->target.bb->binPos - (codeSize + 8));
} else {
emitCBUF (0x24, -1, 20, 16, 0, insn->src(0));
emitField(0x05, 1, 1);
}
}
void
CodeEmitterGM107::emitRET()
{
emitInsn (0xe3200000);
emitCond5(0x00, CC_TR);
}
void
CodeEmitterGM107::emitSSY()
{
const FlowInstruction *insn = this->insn->asFlow();
emitInsn(0xe2900000, 0);
if (!insn->srcExists(0) || insn->src(0).getFile() != FILE_MEMORY_CONST) {
emitField(0x14, 24, insn->target.bb->binPos - (codeSize + 8));
} else {
emitCBUF (0x24, -1, 20, 16, 0, insn->src(0));
emitField(0x05, 1, 1);
}
}
void
CodeEmitterGM107::emitSYNC()
{
emitInsn (0xf0f80000);
emitCond5(0x00, CC_TR);
}
void
CodeEmitterGM107::emitSAM()
{
emitInsn(0xe3700000, 0);
}
void
CodeEmitterGM107::emitRAM()
{
emitInsn(0xe3800000, 0);
}
/*******************************************************************************
* predicate/cc
******************************************************************************/
/*******************************************************************************
* movement / conversion
******************************************************************************/
void
CodeEmitterGM107::emitMOV()
{
if (insn->src(0).getFile() != FILE_IMMEDIATE) {
switch (insn->src(0).getFile()) {
case FILE_GPR:
if (insn->def(0).getFile() == FILE_PREDICATE) {
emitInsn(0x5b6a0000);
emitGPR (0x08);
} else {
emitInsn(0x5c980000);
}
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c980000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38980000);
emitIMMD(0x14, 19, insn->src(0));
break;
case FILE_PREDICATE:
emitInsn(0x50880000);
emitPRED(0x0c, insn->src(0));
emitPRED(0x1d);
emitPRED(0x27);
break;
default:
assert(!"bad src file");
break;
}
if (insn->def(0).getFile() != FILE_PREDICATE &&
insn->src(0).getFile() != FILE_PREDICATE)
emitField(0x27, 4, insn->lanes);
} else {
emitInsn (0x01000000);
emitIMMD (0x14, 32, insn->src(0));
emitField(0x0c, 4, insn->lanes);
}
if (insn->def(0).getFile() == FILE_PREDICATE) {
emitPRED(0x27);
emitPRED(0x03, insn->def(0));
emitPRED(0x00);
} else {
emitGPR(0x00, insn->def(0));
}
}
void
CodeEmitterGM107::emitS2R()
{
emitInsn(0xf0c80000);
emitSYS (0x14, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitCS2R()
{
emitInsn(0x50c80000);
emitSYS (0x14, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitF2F()
{
RoundMode rnd = insn->rnd;
switch (insn->op) {
case OP_FLOOR: rnd = ROUND_MI; break;
case OP_CEIL : rnd = ROUND_PI; break;
case OP_TRUNC: rnd = ROUND_ZI; break;
default:
break;
}
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5ca80000);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4ca80000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38a80000);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src0 file");
break;
}
emitField(0x32, 1, (insn->op == OP_SAT) || insn->saturate);
emitField(0x31, 1, (insn->op == OP_ABS) || insn->src(0).mod.abs());
emitCC (0x2f);
emitField(0x2d, 1, (insn->op == OP_NEG) || insn->src(0).mod.neg());
emitFMZ (0x2c, 1);
emitField(0x29, 1, insn->subOp);
emitRND (0x27, rnd, 0x2a);
emitField(0x0a, 2, util_logbase2(typeSizeof(insn->sType)));
emitField(0x08, 2, util_logbase2(typeSizeof(insn->dType)));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitF2I()
{
RoundMode rnd = insn->rnd;
switch (insn->op) {
case OP_FLOOR: rnd = ROUND_M; break;
case OP_CEIL : rnd = ROUND_P; break;
case OP_TRUNC: rnd = ROUND_Z; break;
default:
break;
}
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5cb00000);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4cb00000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38b00000);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src0 file");
break;
}
emitField(0x31, 1, (insn->op == OP_ABS) || insn->src(0).mod.abs());
emitCC (0x2f);
emitField(0x2d, 1, (insn->op == OP_NEG) || insn->src(0).mod.neg());
emitFMZ (0x2c, 1);
emitRND (0x27, rnd, 0x2a);
emitField(0x0c, 1, isSignedType(insn->dType));
emitField(0x0a, 2, util_logbase2(typeSizeof(insn->sType)));
emitField(0x08, 2, util_logbase2(typeSizeof(insn->dType)));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitI2F()
{
RoundMode rnd = insn->rnd;
switch (insn->op) {
case OP_FLOOR: rnd = ROUND_M; break;
case OP_CEIL : rnd = ROUND_P; break;
case OP_TRUNC: rnd = ROUND_Z; break;
default:
break;
}
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5cb80000);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4cb80000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38b80000);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src0 file");
break;
}
emitField(0x31, 1, (insn->op == OP_ABS) || insn->src(0).mod.abs());
emitCC (0x2f);
emitField(0x2d, 1, (insn->op == OP_NEG) || insn->src(0).mod.neg());
emitField(0x29, 2, insn->subOp);
emitRND (0x27, rnd, -1);
emitField(0x0d, 1, isSignedType(insn->sType));
emitField(0x0a, 2, util_logbase2(typeSizeof(insn->sType)));
emitField(0x08, 2, util_logbase2(typeSizeof(insn->dType)));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitI2I()
{
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5ce00000);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4ce00000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38e00000);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src0 file");
break;
}
emitSAT (0x32);
emitField(0x31, 1, (insn->op == OP_ABS) || insn->src(0).mod.abs());
emitCC (0x2f);
emitField(0x2d, 1, (insn->op == OP_NEG) || insn->src(0).mod.neg());
emitField(0x29, 2, insn->subOp);
emitField(0x0d, 1, isSignedType(insn->sType));
emitField(0x0c, 1, isSignedType(insn->dType));
emitField(0x0a, 2, util_logbase2(typeSizeof(insn->sType)));
emitField(0x08, 2, util_logbase2(typeSizeof(insn->dType)));
emitGPR (0x00, insn->def(0));
}
static void
selpFlip(const FixupEntry *entry, uint32_t *code, const FixupData& data)
{
int loc = entry->loc;
if (data.force_persample_interp)
code[loc + 1] |= 1 << 10;
else
code[loc + 1] &= ~(1 << 10);
}
void
CodeEmitterGM107::emitSEL()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5ca00000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4ca00000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38a00000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitINV (0x2a, insn->src(2));
emitPRED(0x27, insn->src(2));
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
if (insn->subOp == 1) {
addInterp(0, 0, selpFlip);
}
}
void
CodeEmitterGM107::emitSHFL()
{
int type = 0;
emitInsn (0xef100000);
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitGPR(0x14, insn->src(1));
break;
case FILE_IMMEDIATE:
emitIMMD(0x14, 5, insn->src(1));
type |= 1;
break;
default:
assert(!"invalid src1 file");
break;
}
switch (insn->src(2).getFile()) {
case FILE_GPR:
emitGPR(0x27, insn->src(2));
break;
case FILE_IMMEDIATE:
emitIMMD(0x22, 13, insn->src(2));
type |= 2;
break;
default:
assert(!"invalid src2 file");
break;
}
if (!insn->defExists(1))
emitPRED(0x30);
else {
assert(insn->def(1).getFile() == FILE_PREDICATE);
emitPRED(0x30, insn->def(1));
}
emitField(0x1e, 2, insn->subOp);
emitField(0x1c, 2, type);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
/*******************************************************************************
* double
******************************************************************************/
void
CodeEmitterGM107::emitDADD()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c700000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c700000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38700000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitABS(0x31, insn->src(1));
emitNEG(0x30, insn->src(0));
emitCC (0x2f);
emitABS(0x2e, insn->src(0));
emitNEG(0x2d, insn->src(1));
if (insn->op == OP_SUB)
code[1] ^= 0x00002000;
emitGPR(0x08, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitDMUL()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c800000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c800000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38800000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitNEG2(0x30, insn->src(0), insn->src(1));
emitCC (0x2f);
emitRND (0x27);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitDFMA()
{
switch(insn->src(2).getFile()) {
case FILE_GPR:
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5b700000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4b700000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36700000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitGPR (0x27, insn->src(2));
break;
case FILE_MEMORY_CONST:
emitInsn(0x53700000);
emitGPR (0x27, insn->src(1));
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
break;
default:
assert(!"bad src2 file");
break;
}
emitRND (0x32);
emitNEG (0x31, insn->src(2));
emitNEG2(0x30, insn->src(0), insn->src(1));
emitCC (0x2f);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitDMNMX()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c500000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c500000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38500000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitABS (0x31, insn->src(1));
emitNEG (0x30, insn->src(0));
emitCC (0x2f);
emitABS (0x2e, insn->src(0));
emitNEG (0x2d, insn->src(1));
emitField(0x2a, 1, insn->op == OP_MAX);
emitPRED (0x27);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitDSET()
{
const CmpInstruction *insn = this->insn->asCmp();
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x59000000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x49000000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x32000000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
if (insn->op != OP_SET) {
switch (insn->op) {
case OP_SET_AND: emitField(0x2d, 2, 0); break;
case OP_SET_OR : emitField(0x2d, 2, 1); break;
case OP_SET_XOR: emitField(0x2d, 2, 2); break;
default:
assert(!"invalid set op");
break;
}
emitPRED(0x27, insn->src(2));
} else {
emitPRED(0x27);
}
emitABS (0x36, insn->src(0));
emitNEG (0x35, insn->src(1));
emitField(0x34, 1, insn->dType == TYPE_F32);
emitCond4(0x30, insn->setCond);
emitCC (0x2f);
emitABS (0x2c, insn->src(1));
emitNEG (0x2b, insn->src(0));
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitDSETP()
{
const CmpInstruction *insn = this->insn->asCmp();
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5b800000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4b800000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36800000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
if (insn->op != OP_SET) {
switch (insn->op) {
case OP_SET_AND: emitField(0x2d, 2, 0); break;
case OP_SET_OR : emitField(0x2d, 2, 1); break;
case OP_SET_XOR: emitField(0x2d, 2, 2); break;
default:
assert(!"invalid set op");
break;
}
emitPRED(0x27, insn->src(2));
} else {
emitPRED(0x27);
}
emitCond4(0x30, insn->setCond);
emitABS (0x2c, insn->src(1));
emitNEG (0x2b, insn->src(0));
emitGPR (0x08, insn->src(0));
emitABS (0x07, insn->src(0));
emitNEG (0x06, insn->src(1));
emitPRED (0x03, insn->def(0));
if (insn->defExists(1))
emitPRED(0x00, insn->def(1));
else
emitPRED(0x00);
}
/*******************************************************************************
* float
******************************************************************************/
void
CodeEmitterGM107::emitFADD()
{
if (!longIMMD(insn->src(1))) {
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c580000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c580000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38580000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitSAT(0x32);
emitABS(0x31, insn->src(1));
emitNEG(0x30, insn->src(0));
emitCC (0x2f);
emitABS(0x2e, insn->src(0));
emitNEG(0x2d, insn->src(1));
emitFMZ(0x2c, 1);
if (insn->op == OP_SUB)
code[1] ^= 0x00002000;
} else {
emitInsn(0x08000000);
emitABS(0x39, insn->src(1));
emitNEG(0x38, insn->src(0));
emitFMZ(0x37, 1);
emitABS(0x36, insn->src(0));
emitNEG(0x35, insn->src(1));
emitCC (0x34);
emitIMMD(0x14, 32, insn->src(1));
if (insn->op == OP_SUB)
code[1] ^= 0x00080000;
}
emitGPR(0x08, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitFMUL()
{
if (!longIMMD(insn->src(1))) {
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c680000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c680000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38680000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitSAT (0x32);
emitNEG2(0x30, insn->src(0), insn->src(1));
emitCC (0x2f);
emitFMZ (0x2c, 2);
emitPDIV(0x29);
emitRND (0x27);
} else {
emitInsn(0x1e000000);
emitSAT (0x37);
emitFMZ (0x35, 2);
emitCC (0x34);
emitIMMD(0x14, 32, insn->src(1));
if (insn->src(0).mod.neg() ^ insn->src(1).mod.neg())
code[1] ^= 0x00080000; /* flip immd sign bit */
}
emitGPR(0x08, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitFFMA()
{
bool isLongIMMD = false;
switch(insn->src(2).getFile()) {
case FILE_GPR:
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x59800000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x49800000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
if (longIMMD(insn->getSrc(1))) {
assert(insn->getDef(0)->reg.data.id == insn->getSrc(2)->reg.data.id);
isLongIMMD = true;
emitInsn(0x0c000000);
emitIMMD(0x14, 32, insn->src(1));
} else {
emitInsn(0x32800000);
emitIMMD(0x14, 19, insn->src(1));
}
break;
default:
assert(!"bad src1 file");
break;
}
if (!isLongIMMD)
emitGPR (0x27, insn->src(2));
break;
case FILE_MEMORY_CONST:
emitInsn(0x51800000);
emitGPR (0x27, insn->src(1));
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
break;
default:
assert(!"bad src2 file");
break;
}
if (isLongIMMD) {
emitNEG (0x39, insn->src(2));
emitNEG2(0x38, insn->src(0), insn->src(1));
emitSAT (0x37);
emitCC (0x34);
} else {
emitRND (0x33);
emitSAT (0x32);
emitNEG (0x31, insn->src(2));
emitNEG2(0x30, insn->src(0), insn->src(1));
emitCC (0x2f);
}
emitFMZ(0x35, 2);
emitGPR(0x08, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitMUFU()
{
int mufu = 0;
switch (insn->op) {
case OP_COS: mufu = 0; break;
case OP_SIN: mufu = 1; break;
case OP_EX2: mufu = 2; break;
case OP_LG2: mufu = 3; break;
case OP_RCP: mufu = 4 + 2 * insn->subOp; break;
case OP_RSQ: mufu = 5 + 2 * insn->subOp; break;
case OP_SQRT: mufu = 8; break;
default:
assert(!"invalid mufu");
break;
}
emitInsn (0x50800000);
emitSAT (0x32);
emitNEG (0x30, insn->src(0));
emitABS (0x2e, insn->src(0));
emitField(0x14, 4, mufu);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitFMNMX()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c600000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c600000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38600000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitField(0x2a, 1, insn->op == OP_MAX);
emitPRED (0x27);
emitABS(0x31, insn->src(1));
emitNEG(0x30, insn->src(0));
emitCC (0x2f);
emitABS(0x2e, insn->src(0));
emitNEG(0x2d, insn->src(1));
emitFMZ(0x2c, 1);
emitGPR(0x08, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitRRO()
{
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5c900000);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c900000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38900000);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src file");
break;
}
emitABS (0x31, insn->src(0));
emitNEG (0x2d, insn->src(0));
emitField(0x27, 1, insn->op == OP_PREEX2);
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitFCMP()
{
const CmpInstruction *insn = this->insn->asCmp();
CondCode cc = insn->setCond;
if (insn->src(2).mod.neg())
cc = reverseCondCode(cc);
switch(insn->src(2).getFile()) {
case FILE_GPR:
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5ba00000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4ba00000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36a00000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitGPR (0x27, insn->src(2));
break;
case FILE_MEMORY_CONST:
emitInsn(0x53a00000);
emitGPR (0x27, insn->src(1));
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
break;
default:
assert(!"bad src2 file");
break;
}
emitCond4(0x30, cc);
emitFMZ (0x2f, 1);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitFSET()
{
const CmpInstruction *insn = this->insn->asCmp();
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x58000000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x48000000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x30000000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
if (insn->op != OP_SET) {
switch (insn->op) {
case OP_SET_AND: emitField(0x2d, 2, 0); break;
case OP_SET_OR : emitField(0x2d, 2, 1); break;
case OP_SET_XOR: emitField(0x2d, 2, 2); break;
default:
assert(!"invalid set op");
break;
}
emitPRED(0x27, insn->src(2));
} else {
emitPRED(0x27);
}
emitFMZ (0x37, 1);
emitABS (0x36, insn->src(0));
emitNEG (0x35, insn->src(1));
emitField(0x34, 1, insn->dType == TYPE_F32);
emitCond4(0x30, insn->setCond);
emitCC (0x2f);
emitABS (0x2c, insn->src(1));
emitNEG (0x2b, insn->src(0));
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitFSETP()
{
const CmpInstruction *insn = this->insn->asCmp();
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5bb00000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4bb00000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36b00000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
if (insn->op != OP_SET) {
switch (insn->op) {
case OP_SET_AND: emitField(0x2d, 2, 0); break;
case OP_SET_OR : emitField(0x2d, 2, 1); break;
case OP_SET_XOR: emitField(0x2d, 2, 2); break;
default:
assert(!"invalid set op");
break;
}
emitPRED(0x27, insn->src(2));
} else {
emitPRED(0x27);
}
emitCond4(0x30, insn->setCond);
emitFMZ (0x2f, 1);
emitABS (0x2c, insn->src(1));
emitNEG (0x2b, insn->src(0));
emitGPR (0x08, insn->src(0));
emitABS (0x07, insn->src(0));
emitNEG (0x06, insn->src(1));
emitPRED (0x03, insn->def(0));
if (insn->defExists(1))
emitPRED(0x00, insn->def(1));
else
emitPRED(0x00);
}
void
CodeEmitterGM107::emitFSWZADD()
{
emitInsn (0x50f80000);
emitCC (0x2f);
emitFMZ (0x2c, 1);
emitRND (0x27);
emitField(0x26, 1, insn->lanes); /* abused for .ndv */
emitField(0x1c, 8, insn->subOp);
if (insn->predSrc != 1)
emitGPR (0x14, insn->src(1));
else
emitGPR (0x14);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
/*******************************************************************************
* integer
******************************************************************************/
void
CodeEmitterGM107::emitLOP()
{
int lop = 0;
switch (insn->op) {
case OP_AND: lop = 0; break;
case OP_OR : lop = 1; break;
case OP_XOR: lop = 2; break;
default:
assert(!"invalid lop");
break;
}
if (!longIMMD(insn->src(1))) {
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c400000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c400000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38400000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitPRED (0x30);
emitCC (0x2f);
emitX (0x2b);
emitField(0x29, 2, lop);
emitINV (0x28, insn->src(1));
emitINV (0x27, insn->src(0));
} else {
emitInsn (0x04000000);
emitX (0x39);
emitINV (0x38, insn->src(1));
emitINV (0x37, insn->src(0));
emitField(0x35, 2, lop);
emitCC (0x34);
emitIMMD (0x14, 32, insn->src(1));
}
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
/* special-case of emitLOP(): lop pass_b dst 0 ~src */
void
CodeEmitterGM107::emitNOT()
{
if (!longIMMD(insn->src(0))) {
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5c400700);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c400700);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38400700);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src1 file");
break;
}
emitPRED (0x30);
} else {
emitInsn (0x05600000);
emitIMMD (0x14, 32, insn->src(1));
}
emitGPR(0x08);
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitIADD()
{
if (!longIMMD(insn->src(1))) {
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c100000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c100000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38100000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitSAT(0x32);
emitNEG(0x31, insn->src(0));
emitNEG(0x30, insn->src(1));
emitCC (0x2f);
emitX (0x2b);
} else {
emitInsn(0x1c000000);
emitNEG (0x38, insn->src(0));
emitSAT (0x36);
emitX (0x35);
emitCC (0x34);
emitIMMD(0x14, 32, insn->src(1));
}
if (insn->op == OP_SUB)
code[1] ^= 0x00010000;
emitGPR(0x08, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitIMUL()
{
if (!longIMMD(insn->src(1))) {
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c380000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c380000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38380000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitCC (0x2f);
emitField(0x29, 1, isSignedType(insn->sType));
emitField(0x28, 1, isSignedType(insn->dType));
emitField(0x27, 1, insn->subOp == NV50_IR_SUBOP_MUL_HIGH);
} else {
emitInsn (0x1f000000);
emitField(0x37, 1, isSignedType(insn->sType));
emitField(0x36, 1, isSignedType(insn->dType));
emitField(0x35, 1, insn->subOp == NV50_IR_SUBOP_MUL_HIGH);
emitCC (0x34);
emitIMMD (0x14, 32, insn->src(1));
}
emitGPR(0x08, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitIMAD()
{
/*XXX: imad32i exists, but not using it as third src overlaps dst */
switch(insn->src(2).getFile()) {
case FILE_GPR:
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5a000000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4a000000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x34000000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitGPR (0x27, insn->src(2));
break;
case FILE_MEMORY_CONST:
emitInsn(0x52000000);
emitGPR (0x27, insn->src(1));
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
break;
default:
assert(!"bad src2 file");
break;
}
emitField(0x36, 1, insn->subOp == NV50_IR_SUBOP_MUL_HIGH);
emitField(0x35, 1, isSignedType(insn->sType));
emitNEG (0x34, insn->src(2));
emitNEG2 (0x33, insn->src(0), insn->src(1));
emitSAT (0x32);
emitX (0x31);
emitField(0x30, 1, isSignedType(insn->dType));
emitCC (0x2f);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitISCADD()
{
assert(insn->src(1).get()->asImm());
switch (insn->src(2).getFile()) {
case FILE_GPR:
emitInsn(0x5c180000);
emitGPR (0x14, insn->src(2));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c180000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
break;
case FILE_IMMEDIATE:
emitInsn(0x38180000);
emitIMMD(0x14, 19, insn->src(2));
break;
default:
assert(!"bad src1 file");
break;
}
emitNEG (0x31, insn->src(0));
emitNEG (0x30, insn->src(2));
emitCC (0x2f);
emitIMMD(0x27, 5, insn->src(1));
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitXMAD()
{
assert(insn->src(0).getFile() == FILE_GPR);
bool constbuf = false;
bool psl_mrg = true;
bool immediate = false;
if (insn->src(2).getFile() == FILE_MEMORY_CONST) {
assert(insn->src(1).getFile() == FILE_GPR);
constbuf = true;
psl_mrg = false;
emitInsn(0x51000000);
emitGPR(0x27, insn->src(1));
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
} else if (insn->src(1).getFile() == FILE_MEMORY_CONST) {
assert(insn->src(2).getFile() == FILE_GPR);
constbuf = true;
emitInsn(0x4e000000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
emitGPR(0x27, insn->src(2));
} else if (insn->src(1).getFile() == FILE_IMMEDIATE) {
assert(insn->src(2).getFile() == FILE_GPR);
assert(!(insn->subOp & NV50_IR_SUBOP_XMAD_H1(1)));
immediate = true;
emitInsn(0x36000000);
emitIMMD(0x14, 16, insn->src(1));
emitGPR(0x27, insn->src(2));
} else {
assert(insn->src(1).getFile() == FILE_GPR);
assert(insn->src(2).getFile() == FILE_GPR);
emitInsn(0x5b000000);
emitGPR(0x14, insn->src(1));
emitGPR(0x27, insn->src(2));
}
if (psl_mrg)
emitField(constbuf ? 0x37 : 0x24, 2, insn->subOp & 0x3);
unsigned cmode = (insn->subOp & NV50_IR_SUBOP_XMAD_CMODE_MASK);
cmode >>= NV50_IR_SUBOP_XMAD_CMODE_SHIFT;
emitField(0x32, constbuf ? 2 : 3, cmode);
emitX(constbuf ? 0x36 : 0x26);
emitCC(0x2f);
emitGPR(0x0, insn->def(0));
emitGPR(0x8, insn->src(0));
// source flags
if (isSignedType(insn->sType)) {
uint16_t h1s = insn->subOp & NV50_IR_SUBOP_XMAD_H1_MASK;
emitField(0x30, 2, h1s >> NV50_IR_SUBOP_XMAD_H1_SHIFT);
}
emitField(0x35, 1, insn->subOp & NV50_IR_SUBOP_XMAD_H1(0) ? 1 : 0);
if (!immediate) {
bool h1 = insn->subOp & NV50_IR_SUBOP_XMAD_H1(1);
emitField(constbuf ? 0x34 : 0x23, 1, h1);
}
}
void
CodeEmitterGM107::emitIMNMX()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c200000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c200000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38200000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitField(0x30, 1, isSignedType(insn->dType));
emitCC (0x2f);
emitField(0x2b, 2, insn->subOp);
emitField(0x2a, 1, insn->op == OP_MAX);
emitPRED (0x27);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitICMP()
{
const CmpInstruction *insn = this->insn->asCmp();
CondCode cc = insn->setCond;
if (insn->src(2).mod.neg())
cc = reverseCondCode(cc);
switch(insn->src(2).getFile()) {
case FILE_GPR:
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5b400000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4b400000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36400000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitGPR (0x27, insn->src(2));
break;
case FILE_MEMORY_CONST:
emitInsn(0x53400000);
emitGPR (0x27, insn->src(1));
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
break;
default:
assert(!"bad src2 file");
break;
}
emitCond3(0x31, cc);
emitField(0x30, 1, isSignedType(insn->sType));
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitISET()
{
const CmpInstruction *insn = this->insn->asCmp();
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5b500000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4b500000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36500000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
if (insn->op != OP_SET) {
switch (insn->op) {
case OP_SET_AND: emitField(0x2d, 2, 0); break;
case OP_SET_OR : emitField(0x2d, 2, 1); break;
case OP_SET_XOR: emitField(0x2d, 2, 2); break;
default:
assert(!"invalid set op");
break;
}
emitPRED(0x27, insn->src(2));
} else {
emitPRED(0x27);
}
emitCond3(0x31, insn->setCond);
emitField(0x30, 1, isSignedType(insn->sType));
emitCC (0x2f);
emitField(0x2c, 1, insn->dType == TYPE_F32);
emitX (0x2b);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitISETP()
{
const CmpInstruction *insn = this->insn->asCmp();
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5b600000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4b600000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36600000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
if (insn->op != OP_SET) {
switch (insn->op) {
case OP_SET_AND: emitField(0x2d, 2, 0); break;
case OP_SET_OR : emitField(0x2d, 2, 1); break;
case OP_SET_XOR: emitField(0x2d, 2, 2); break;
default:
assert(!"invalid set op");
break;
}
emitPRED(0x27, insn->src(2));
} else {
emitPRED(0x27);
}
emitCond3(0x31, insn->setCond);
emitField(0x30, 1, isSignedType(insn->sType));
emitX (0x2b);
emitGPR (0x08, insn->src(0));
emitPRED (0x03, insn->def(0));
if (insn->defExists(1))
emitPRED(0x00, insn->def(1));
else
emitPRED(0x00);
}
void
CodeEmitterGM107::emitSHL()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c480000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c480000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38480000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitCC (0x2f);
emitX (0x2b);
emitField(0x27, 1, insn->subOp == NV50_IR_SUBOP_SHIFT_WRAP);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitSHR()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c280000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c280000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38280000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitField(0x30, 1, isSignedType(insn->dType));
emitCC (0x2f);
emitX (0x2c);
emitField(0x27, 1, insn->subOp == NV50_IR_SUBOP_SHIFT_WRAP);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitSHF()
{
unsigned type;
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(insn->op == OP_SHL ? 0x5bf80000 : 0x5cf80000);
emitGPR(0x14, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(insn->op == OP_SHL ? 0x36f80000 : 0x38f80000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
switch (insn->sType) {
case TYPE_U64:
type = 2;
break;
case TYPE_S64:
type = 3;
break;
default:
type = 0;
break;
}
emitField(0x32, 1, !!(insn->subOp & NV50_IR_SUBOP_SHIFT_WRAP));
emitX (0x31);
emitField(0x30, 1, !!(insn->subOp & NV50_IR_SUBOP_SHIFT_HIGH));
emitCC (0x2f);
emitGPR (0x27, insn->src(2));
emitField(0x25, 2, type);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitPOPC()
{
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5c080000);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c080000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38080000);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src1 file");
break;
}
emitINV(0x28, insn->src(0));
emitGPR(0x00, insn->def(0));
}
void
CodeEmitterGM107::emitBFI()
{
switch(insn->src(2).getFile()) {
case FILE_GPR:
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5bf00000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4bf00000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x36f00000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitGPR (0x27, insn->src(2));
break;
case FILE_MEMORY_CONST:
emitInsn(0x53f00000);
emitGPR (0x27, insn->src(1));
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(2));
break;
default:
assert(!"bad src2 file");
break;
}
emitCC (0x2f);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitBFE()
{
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0x5c000000);
emitGPR (0x14, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c000000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0x38000000);
emitIMMD(0x14, 19, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitField(0x30, 1, isSignedType(insn->dType));
emitCC (0x2f);
emitField(0x28, 1, insn->subOp == NV50_IR_SUBOP_EXTBF_REV);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitFLO()
{
switch (insn->src(0).getFile()) {
case FILE_GPR:
emitInsn(0x5c300000);
emitGPR (0x14, insn->src(0));
break;
case FILE_MEMORY_CONST:
emitInsn(0x4c300000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(0));
break;
case FILE_IMMEDIATE:
emitInsn(0x38300000);
emitIMMD(0x14, 19, insn->src(0));
break;
default:
assert(!"bad src1 file");
break;
}
emitField(0x30, 1, isSignedType(insn->dType));
emitCC (0x2f);
emitField(0x29, 1, insn->subOp == NV50_IR_SUBOP_BFIND_SAMT);
emitINV (0x28, insn->src(0));
emitGPR (0x00, insn->def(0));
}
/*******************************************************************************
* memory
******************************************************************************/
void
CodeEmitterGM107::emitLDSTs(int pos, DataType type)
{
int data = 0;
switch (typeSizeof(type)) {
case 1: data = isSignedType(type) ? 1 : 0; break;
case 2: data = isSignedType(type) ? 3 : 2; break;
case 4: data = 4; break;
case 8: data = 5; break;
case 16: data = 6; break;
default:
assert(!"bad type");
break;
}
emitField(pos, 3, data);
}
void
CodeEmitterGM107::emitLDSTc(int pos)
{
int mode = 0;
switch (insn->cache) {
case CACHE_CA: mode = 0; break;
case CACHE_CG: mode = 1; break;
case CACHE_CS: mode = 2; break;
case CACHE_CV: mode = 3; break;
default:
assert(!"invalid caching mode");
break;
}
emitField(pos, 2, mode);
}
void
CodeEmitterGM107::emitLDC()
{
emitInsn (0xef900000);
emitLDSTs(0x30, insn->dType);
emitField(0x2c, 2, insn->subOp);
emitCBUF (0x24, 0x08, 0x14, 16, 0, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitLDL()
{
emitInsn (0xef400000);
emitLDSTs(0x30, insn->dType);
emitLDSTc(0x2c);
emitADDR (0x08, 0x14, 24, 0, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitLDS()
{
emitInsn (0xef480000);
emitLDSTs(0x30, insn->dType);
emitADDR (0x08, 0x14, 24, 0, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitLD()
{
emitInsn (0x80000000);
emitPRED (0x3a);
emitLDSTc(0x38);
emitLDSTs(0x35, insn->dType);
emitField(0x34, 1, insn->src(0).getIndirect(0)->getSize() == 8);
emitADDR (0x08, 0x14, 32, 0, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitSTL()
{
emitInsn (0xef500000);
emitLDSTs(0x30, insn->dType);
emitLDSTc(0x2c);
emitADDR (0x08, 0x14, 24, 0, insn->src(0));
emitGPR (0x00, insn->src(1));
}
void
CodeEmitterGM107::emitSTS()
{
emitInsn (0xef580000);
emitLDSTs(0x30, insn->dType);
emitADDR (0x08, 0x14, 24, 0, insn->src(0));
emitGPR (0x00, insn->src(1));
}
void
CodeEmitterGM107::emitST()
{
emitInsn (0xa0000000);
emitPRED (0x3a);
emitLDSTc(0x38);
emitLDSTs(0x35, insn->dType);
emitField(0x34, 1, insn->src(0).getIndirect(0)->getSize() == 8);
emitADDR (0x08, 0x14, 32, 0, insn->src(0));
emitGPR (0x00, insn->src(1));
}
void
CodeEmitterGM107::emitALD()
{
emitInsn (0xefd80000);
emitField(0x2f, 2, (insn->getDef(0)->reg.size / 4) - 1);
emitGPR (0x27, insn->src(0).getIndirect(1));
emitO (0x20);
emitP (0x1f);
emitADDR (0x08, 20, 10, 0, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitAST()
{
emitInsn (0xeff00000);
emitField(0x2f, 2, (typeSizeof(insn->dType) / 4) - 1);
emitGPR (0x27, insn->src(0).getIndirect(1));
emitP (0x1f);
emitADDR (0x08, 20, 10, 0, insn->src(0));
emitGPR (0x00, insn->src(1));
}
void
CodeEmitterGM107::emitISBERD()
{
emitInsn(0xefd00000);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitAL2P()
{
emitInsn (0xefa00000);
emitField(0x2f, 2, (insn->getDef(0)->reg.size / 4) - 1);
emitPRED (0x2c);
emitO (0x20);
emitField(0x14, 11, insn->src(0).get()->reg.data.offset);
emitGPR (0x08, insn->src(0).getIndirect(0));
emitGPR (0x00, insn->def(0));
}
static void
interpApply(const FixupEntry *entry, uint32_t *code, const FixupData& data)
{
int ipa = entry->ipa;
int reg = entry->reg;
int loc = entry->loc;
if (data.flatshade &&
(ipa & NV50_IR_INTERP_MODE_MASK) == NV50_IR_INTERP_SC) {
ipa = NV50_IR_INTERP_FLAT;
reg = 0xff;
} else if (data.force_persample_interp &&
(ipa & NV50_IR_INTERP_SAMPLE_MASK) == NV50_IR_INTERP_DEFAULT &&
(ipa & NV50_IR_INTERP_MODE_MASK) != NV50_IR_INTERP_FLAT) {
ipa |= NV50_IR_INTERP_CENTROID;
}
code[loc + 1] &= ~(0xf << 0x14);
code[loc + 1] |= (ipa & 0x3) << 0x16;
code[loc + 1] |= (ipa & 0xc) << (0x14 - 2);
code[loc + 0] &= ~(0xff << 0x14);
code[loc + 0] |= reg << 0x14;
}
void
CodeEmitterGM107::emitIPA()
{
int ipam = 0, ipas = 0;
switch (insn->getInterpMode()) {
case NV50_IR_INTERP_LINEAR : ipam = 0; break;
case NV50_IR_INTERP_PERSPECTIVE: ipam = 1; break;
case NV50_IR_INTERP_FLAT : ipam = 2; break;
case NV50_IR_INTERP_SC : ipam = 3; break;
default:
assert(!"invalid ipa mode");
break;
}
switch (insn->getSampleMode()) {
case NV50_IR_INTERP_DEFAULT : ipas = 0; break;
case NV50_IR_INTERP_CENTROID: ipas = 1; break;
case NV50_IR_INTERP_OFFSET : ipas = 2; break;
default:
assert(!"invalid ipa sample mode");
break;
}
emitInsn (0xe0000000);
emitField(0x36, 2, ipam);
emitField(0x34, 2, ipas);
emitSAT (0x33);
emitField(0x2f, 3, 7);
emitADDR (0x08, 0x1c, 10, 0, insn->src(0));
if ((code[0] & 0x0000ff00) != 0x0000ff00)
code[1] |= 0x00000040; /* .idx */
emitGPR(0x00, insn->def(0));
if (insn->op == OP_PINTERP) {
emitGPR(0x14, insn->src(1));
if (insn->getSampleMode() == NV50_IR_INTERP_OFFSET)
emitGPR(0x27, insn->src(2));
addInterp(insn->ipa, insn->getSrc(1)->reg.data.id, interpApply);
} else {
if (insn->getSampleMode() == NV50_IR_INTERP_OFFSET)
emitGPR(0x27, insn->src(1));
emitGPR(0x14);
addInterp(insn->ipa, 0xff, interpApply);
}
if (insn->getSampleMode() != NV50_IR_INTERP_OFFSET)
emitGPR(0x27);
}
void
CodeEmitterGM107::emitATOM()
{
unsigned dType, subOp;
if (insn->subOp == NV50_IR_SUBOP_ATOM_CAS) {
switch (insn->dType) {
case TYPE_U32: dType = 0; break;
case TYPE_U64: dType = 1; break;
default: assert(!"unexpected dType"); dType = 0; break;
}
subOp = 15;
emitInsn (0xee000000);
} else {
switch (insn->dType) {
case TYPE_U32: dType = 0; break;
case TYPE_S32: dType = 1; break;
case TYPE_U64: dType = 2; break;
case TYPE_F32: dType = 3; break;
case TYPE_B128: dType = 4; break;
case TYPE_S64: dType = 5; break;
default: assert(!"unexpected dType"); dType = 0; break;
}
if (insn->subOp == NV50_IR_SUBOP_ATOM_EXCH)
subOp = 8;
else
subOp = insn->subOp;
emitInsn (0xed000000);
}
emitField(0x34, 4, subOp);
emitField(0x31, 3, dType);
emitField(0x30, 1, insn->src(0).getIndirect(0)->getSize() == 8);
emitGPR (0x14, insn->src(1));
emitADDR (0x08, 0x1c, 20, 0, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitATOMS()
{
unsigned dType, subOp;
if (insn->subOp == NV50_IR_SUBOP_ATOM_CAS) {
switch (insn->dType) {
case TYPE_U32: dType = 0; break;
case TYPE_U64: dType = 1; break;
default: assert(!"unexpected dType"); dType = 0; break;
}
subOp = 4;
emitInsn (0xee000000);
emitField(0x34, 1, dType);
} else {
switch (insn->dType) {
case TYPE_U32: dType = 0; break;
case TYPE_S32: dType = 1; break;
case TYPE_U64: dType = 2; break;
case TYPE_S64: dType = 3; break;
default: assert(!"unexpected dType"); dType = 0; break;
}
if (insn->subOp == NV50_IR_SUBOP_ATOM_EXCH)
subOp = 8;
else
subOp = insn->subOp;
emitInsn (0xec000000);
emitField(0x1c, 3, dType);
}
emitField(0x34, 4, subOp);
emitGPR (0x14, insn->src(1));
emitADDR (0x08, 0x1e, 22, 2, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitRED()
{
unsigned dType;
switch (insn->dType) {
case TYPE_U32: dType = 0; break;
case TYPE_S32: dType = 1; break;
case TYPE_U64: dType = 2; break;
case TYPE_F32: dType = 3; break;
case TYPE_B128: dType = 4; break;
case TYPE_S64: dType = 5; break;
default: assert(!"unexpected dType"); dType = 0; break;
}
emitInsn (0xebf80000);
emitField(0x30, 1, insn->src(0).getIndirect(0)->getSize() == 8);
emitField(0x17, 3, insn->subOp);
emitField(0x14, 3, dType);
emitADDR (0x08, 0x1c, 20, 0, insn->src(0));
emitGPR (0x00, insn->src(1));
}
void
CodeEmitterGM107::emitCCTL()
{
unsigned width;
if (insn->src(0).getFile() == FILE_MEMORY_GLOBAL) {
emitInsn(0xef600000);
width = 30;
} else {
emitInsn(0xef800000);
width = 22;
}
emitField(0x34, 1, insn->src(0).getIndirect(0)->getSize() == 8);
emitADDR (0x08, 0x16, width, 2, insn->src(0));
emitField(0x00, 4, insn->subOp);
}
/*******************************************************************************
* surface
******************************************************************************/
void
CodeEmitterGM107::emitPIXLD()
{
emitInsn (0xefe80000);
emitPRED (0x2d);
emitField(0x1f, 3, insn->subOp);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
/*******************************************************************************
* texture
******************************************************************************/
void
CodeEmitterGM107::emitTEXs(int pos)
{
int src1 = insn->predSrc == 1 ? 2 : 1;
if (insn->srcExists(src1))
emitGPR(pos, insn->src(src1));
else
emitGPR(pos);
}
static uint8_t
getTEXSMask(uint8_t mask)
{
switch (mask) {
case 0x1: return 0x0;
case 0x2: return 0x1;
case 0x3: return 0x4;
case 0x4: return 0x2;
case 0x7: return 0x0;
case 0x8: return 0x3;
case 0x9: return 0x5;
case 0xa: return 0x6;
case 0xb: return 0x1;
case 0xc: return 0x7;
case 0xd: return 0x2;
case 0xe: return 0x3;
case 0xf: return 0x4;
default:
assert(!"invalid mask");
}
}
static uint8_t
getTEXSTarget(const TexInstruction *tex)
{
assert(tex->op == OP_TEX || tex->op == OP_TXL);
switch (tex->tex.target.getEnum()) {
case TEX_TARGET_1D:
assert(tex->tex.levelZero);
return 0x0;
case TEX_TARGET_2D:
case TEX_TARGET_RECT:
if (tex->tex.levelZero)
return 0x2;
if (tex->op == OP_TXL)
return 0x3;
return 0x1;
case TEX_TARGET_2D_SHADOW:
case TEX_TARGET_RECT_SHADOW:
if (tex->tex.levelZero)
return 0x6;
if (tex->op == OP_TXL)
return 0x5;
return 0x4;
case TEX_TARGET_2D_ARRAY:
if (tex->tex.levelZero)
return 0x8;
return 0x7;
case TEX_TARGET_2D_ARRAY_SHADOW:
assert(tex->tex.levelZero);
return 0x9;
case TEX_TARGET_3D:
if (tex->tex.levelZero)
return 0xb;
assert(tex->op != OP_TXL);
return 0xa;
case TEX_TARGET_CUBE:
assert(!tex->tex.levelZero);
if (tex->op == OP_TXL)
return 0xd;
return 0xc;
default:
assert(false);
return 0x0;
}
}
static uint8_t
getTLDSTarget(const TexInstruction *tex)
{
switch (tex->tex.target.getEnum()) {
case TEX_TARGET_1D:
if (tex->tex.levelZero)
return 0x0;
return 0x1;
case TEX_TARGET_2D:
case TEX_TARGET_RECT:
if (tex->tex.levelZero)
return tex->tex.useOffsets ? 0x4 : 0x2;
return tex->tex.useOffsets ? 0xc : 0x5;
case TEX_TARGET_2D_MS:
assert(tex->tex.levelZero);
return 0x6;
case TEX_TARGET_3D:
assert(tex->tex.levelZero);
return 0x7;
case TEX_TARGET_2D_ARRAY:
assert(tex->tex.levelZero);
return 0x8;
default:
assert(false);
return 0x0;
}
}
void
CodeEmitterGM107::emitTEX()
{
const TexInstruction *insn = this->insn->asTex();
int lodm = 0;
if (!insn->tex.levelZero) {
switch (insn->op) {
case OP_TEX: lodm = 0; break;
case OP_TXB: lodm = 2; break;
case OP_TXL: lodm = 3; break;
default:
assert(!"invalid tex op");
break;
}
} else {
lodm = 1;
}
if (insn->tex.rIndirectSrc >= 0) {
emitInsn (0xdeb80000);
emitField(0x25, 2, lodm);
emitField(0x24, 1, insn->tex.useOffsets == 1);
} else {
emitInsn (0xc0380000);
emitField(0x37, 2, lodm);
emitField(0x36, 1, insn->tex.useOffsets == 1);
emitField(0x24, 13, insn->tex.r);
}
emitField(0x32, 1, insn->tex.target.isShadow());
emitField(0x31, 1, insn->tex.liveOnly);
emitField(0x23, 1, insn->tex.derivAll);
emitField(0x1f, 4, insn->tex.mask);
emitField(0x1d, 2, insn->tex.target.isCube() ? 3 :
insn->tex.target.getDim() - 1);
emitField(0x1c, 1, insn->tex.target.isArray());
emitTEXs (0x14);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitTEXS()
{
const TexInstruction *insn = this->insn->asTex();
switch (insn->op) {
case OP_TEX:
case OP_TXL:
emitInsn (0xd8000000);
emitField(0x35, 4, getTEXSTarget(insn));
emitField(0x32, 3, getTEXSMask(insn->tex.mask));
break;
case OP_TXF:
emitInsn (0xda000000);
emitField(0x35, 4, getTLDSTarget(insn));
emitField(0x32, 3, getTEXSMask(insn->tex.mask));
break;
case OP_TXG:
assert(insn->tex.useOffsets != 4);
emitInsn (0xdf000000);
emitField(0x34, 2, insn->tex.gatherComp);
emitField(0x33, 1, insn->tex.useOffsets == 1);
emitField(0x32, 1, insn->tex.target.isShadow());
break;
default:
unreachable("unknown op in emitTEXS()");
break;
}
emitField(0x31, 1, insn->tex.liveOnly);
emitField(0x24, 13, insn->tex.r);
if (insn->defExists(1))
emitGPR(0x1c, insn->def(1));
else
emitGPR(0x1c);
if (insn->srcExists(1))
emitGPR(0x14, insn->getSrc(1));
else
emitGPR(0x14);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitTLD()
{
const TexInstruction *insn = this->insn->asTex();
if (insn->tex.rIndirectSrc >= 0) {
emitInsn (0xdd380000);
} else {
emitInsn (0xdc380000);
emitField(0x24, 13, insn->tex.r);
}
emitField(0x37, 1, insn->tex.levelZero == 0);
emitField(0x32, 1, insn->tex.target.isMS());
emitField(0x31, 1, insn->tex.liveOnly);
emitField(0x23, 1, insn->tex.useOffsets == 1);
emitField(0x1f, 4, insn->tex.mask);
emitField(0x1d, 2, insn->tex.target.isCube() ? 3 :
insn->tex.target.getDim() - 1);
emitField(0x1c, 1, insn->tex.target.isArray());
emitTEXs (0x14);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitTLD4()
{
const TexInstruction *insn = this->insn->asTex();
if (insn->tex.rIndirectSrc >= 0) {
emitInsn (0xdef80000);
emitField(0x26, 2, insn->tex.gatherComp);
emitField(0x25, 2, insn->tex.useOffsets == 4);
emitField(0x24, 2, insn->tex.useOffsets == 1);
} else {
emitInsn (0xc8380000);
emitField(0x38, 2, insn->tex.gatherComp);
emitField(0x37, 2, insn->tex.useOffsets == 4);
emitField(0x36, 2, insn->tex.useOffsets == 1);
emitField(0x24, 13, insn->tex.r);
}
emitField(0x32, 1, insn->tex.target.isShadow());
emitField(0x31, 1, insn->tex.liveOnly);
emitField(0x23, 1, insn->tex.derivAll);
emitField(0x1f, 4, insn->tex.mask);
emitField(0x1d, 2, insn->tex.target.isCube() ? 3 :
insn->tex.target.getDim() - 1);
emitField(0x1c, 1, insn->tex.target.isArray());
emitTEXs (0x14);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitTXD()
{
const TexInstruction *insn = this->insn->asTex();
if (insn->tex.rIndirectSrc >= 0) {
emitInsn (0xde780000);
} else {
emitInsn (0xde380000);
emitField(0x24, 13, insn->tex.r);
}
emitField(0x31, 1, insn->tex.liveOnly);
emitField(0x23, 1, insn->tex.useOffsets == 1);
emitField(0x1f, 4, insn->tex.mask);
emitField(0x1d, 2, insn->tex.target.isCube() ? 3 :
insn->tex.target.getDim() - 1);
emitField(0x1c, 1, insn->tex.target.isArray());
emitTEXs (0x14);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitTMML()
{
const TexInstruction *insn = this->insn->asTex();
if (insn->tex.rIndirectSrc >= 0) {
emitInsn (0xdf600000);
} else {
emitInsn (0xdf580000);
emitField(0x24, 13, insn->tex.r);
}
emitField(0x31, 1, insn->tex.liveOnly);
emitField(0x23, 1, insn->tex.derivAll);
emitField(0x1f, 4, insn->tex.mask);
emitField(0x1d, 2, insn->tex.target.isCube() ? 3 :
insn->tex.target.getDim() - 1);
emitField(0x1c, 1, insn->tex.target.isArray());
emitTEXs (0x14);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitTXQ()
{
const TexInstruction *insn = this->insn->asTex();
int type = 0;
switch (insn->tex.query) {
case TXQ_DIMS : type = 0x01; break;
case TXQ_TYPE : type = 0x02; break;
case TXQ_SAMPLE_POSITION: type = 0x05; break;
case TXQ_FILTER : type = 0x10; break;
case TXQ_LOD : type = 0x12; break;
case TXQ_WRAP : type = 0x14; break;
case TXQ_BORDER_COLOUR : type = 0x16; break;
default:
assert(!"invalid txq query");
break;
}
if (insn->tex.rIndirectSrc >= 0) {
emitInsn (0xdf500000);
} else {
emitInsn (0xdf480000);
emitField(0x24, 13, insn->tex.r);
}
emitField(0x31, 1, insn->tex.liveOnly);
emitField(0x1f, 4, insn->tex.mask);
emitField(0x16, 6, type);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitDEPBAR()
{
emitInsn (0xf0f00000);
emitField(0x1d, 1, 1); /* le */
emitField(0x1a, 3, 5);
emitField(0x14, 6, insn->subOp);
emitField(0x00, 6, insn->subOp);
}
/*******************************************************************************
* misc
******************************************************************************/
void
CodeEmitterGM107::emitNOP()
{
emitInsn(0x50b00000);
}
void
CodeEmitterGM107::emitKIL()
{
emitInsn (0xe3300000);
emitCond5(0x00, CC_TR);
}
void
CodeEmitterGM107::emitOUT()
{
const int cut = insn->op == OP_RESTART || insn->subOp;
const int emit = insn->op == OP_EMIT;
switch (insn->src(1).getFile()) {
case FILE_GPR:
emitInsn(0xfbe00000);
emitGPR (0x14, insn->src(1));
break;
case FILE_IMMEDIATE:
emitInsn(0xf6e00000);
emitIMMD(0x14, 19, insn->src(1));
break;
case FILE_MEMORY_CONST:
emitInsn(0xebe00000);
emitCBUF(0x22, -1, 0x14, 16, 2, insn->src(1));
break;
default:
assert(!"bad src1 file");
break;
}
emitField(0x27, 2, (cut << 1) | emit);
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
}
void
CodeEmitterGM107::emitBAR()
{
uint8_t subop;
emitInsn (0xf0a80000);
switch (insn->subOp) {
case NV50_IR_SUBOP_BAR_RED_POPC: subop = 0x02; break;
case NV50_IR_SUBOP_BAR_RED_AND: subop = 0x0a; break;
case NV50_IR_SUBOP_BAR_RED_OR: subop = 0x12; break;
case NV50_IR_SUBOP_BAR_ARRIVE: subop = 0x81; break;
default:
subop = 0x80;
assert(insn->subOp == NV50_IR_SUBOP_BAR_SYNC);
break;
}
emitField(0x20, 8, subop);
// barrier id
if (insn->src(0).getFile() == FILE_GPR) {
emitGPR(0x08, insn->src(0));
} else {
ImmediateValue *imm = insn->getSrc(0)->asImm();
assert(imm);
emitField(0x08, 8, imm->reg.data.u32);
emitField(0x2b, 1, 1);
}
// thread count
if (insn->src(1).getFile() == FILE_GPR) {
emitGPR(0x14, insn->src(1));
} else {
ImmediateValue *imm = insn->getSrc(0)->asImm();
assert(imm);
emitField(0x14, 12, imm->reg.data.u32);
emitField(0x2c, 1, 1);
}
if (insn->srcExists(2) && (insn->predSrc != 2)) {
emitPRED (0x27, insn->src(2));
emitField(0x2a, 1, insn->src(2).mod == Modifier(NV50_IR_MOD_NOT));
} else {
emitField(0x27, 3, 7);
}
}
void
CodeEmitterGM107::emitMEMBAR()
{
emitInsn (0xef980000);
emitField(0x08, 2, insn->subOp >> 2);
}
void
CodeEmitterGM107::emitVOTE()
{
const ImmediateValue *imm;
uint32_t u32;
int r = -1, p = -1;
for (int i = 0; insn->defExists(i); i++) {
if (insn->def(i).getFile() == FILE_GPR)
r = i;
else if (insn->def(i).getFile() == FILE_PREDICATE)
p = i;
}
emitInsn (0x50d80000);
emitField(0x30, 2, insn->subOp);
if (r >= 0)
emitGPR (0x00, insn->def(r));
else
emitGPR (0x00);
if (p >= 0)
emitPRED (0x2d, insn->def(p));
else
emitPRED (0x2d);
switch (insn->src(0).getFile()) {
case FILE_PREDICATE:
emitField(0x2a, 1, insn->src(0).mod == Modifier(NV50_IR_MOD_NOT));
emitPRED (0x27, insn->src(0));
break;
case FILE_IMMEDIATE:
imm = insn->getSrc(0)->asImm();
assert(imm);
u32 = imm->reg.data.u32;
assert(u32 == 0 || u32 == 1);
emitPRED(0x27);
emitField(0x2a, 1, u32 == 0);
break;
default:
assert(!"Unhandled src");
break;
}
}
void
CodeEmitterGM107::emitSUTarget()
{
const TexInstruction *insn = this->insn->asTex();
int target = 0;
assert(insn->op >= OP_SULDB && insn->op <= OP_SUREDP);
if (insn->tex.target == TEX_TARGET_BUFFER) {
target = 2;
} else if (insn->tex.target == TEX_TARGET_1D_ARRAY) {
target = 4;
} else if (insn->tex.target == TEX_TARGET_2D ||
insn->tex.target == TEX_TARGET_RECT) {
target = 6;
} else if (insn->tex.target == TEX_TARGET_2D_ARRAY ||
insn->tex.target == TEX_TARGET_CUBE ||
insn->tex.target == TEX_TARGET_CUBE_ARRAY) {
target = 8;
} else if (insn->tex.target == TEX_TARGET_3D) {
target = 10;
} else {
assert(insn->tex.target == TEX_TARGET_1D);
}
emitField(0x20, 4, target);
}
void
CodeEmitterGM107::emitSUHandle(const int s)
{
const TexInstruction *insn = this->insn->asTex();
assert(insn->op >= OP_SULDB && insn->op <= OP_SUREDP);
if (insn->src(s).getFile() == FILE_GPR) {
emitGPR(0x27, insn->src(s));
} else {
ImmediateValue *imm = insn->getSrc(s)->asImm();
assert(imm);
emitField(0x33, 1, 1);
emitField(0x24, 13, imm->reg.data.u32);
}
}
void
CodeEmitterGM107::emitSUSTx()
{
const TexInstruction *insn = this->insn->asTex();
emitInsn(0xeb200000);
if (insn->op == OP_SUSTB)
emitField(0x34, 1, 1);
emitSUTarget();
emitLDSTc(0x18);
emitField(0x14, 4, 0xf); // rgba
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->src(1));
emitSUHandle(2);
}
void
CodeEmitterGM107::emitSULDx()
{
const TexInstruction *insn = this->insn->asTex();
int type = 0;
emitInsn(0xeb000000);
if (insn->op == OP_SULDB)
emitField(0x34, 1, 1);
emitSUTarget();
switch (insn->dType) {
case TYPE_S8: type = 1; break;
case TYPE_U16: type = 2; break;
case TYPE_S16: type = 3; break;
case TYPE_U32: type = 4; break;
case TYPE_U64: type = 5; break;
case TYPE_B128: type = 6; break;
default:
assert(insn->dType == TYPE_U8);
break;
}
emitLDSTc(0x18);
emitField(0x14, 3, type);
emitGPR (0x00, insn->def(0));
emitGPR (0x08, insn->src(0));
emitSUHandle(1);
}
void
CodeEmitterGM107::emitSUREDx()
{
const TexInstruction *insn = this->insn->asTex();
uint8_t type = 0, subOp;
if (insn->subOp == NV50_IR_SUBOP_ATOM_CAS)
emitInsn(0xeac00000);
else
emitInsn(0xea600000);
if (insn->op == OP_SUREDB)
emitField(0x34, 1, 1);
emitSUTarget();
// destination type
switch (insn->dType) {
case TYPE_S32: type = 1; break;
case TYPE_U64: type = 2; break;
case TYPE_F32: type = 3; break;
case TYPE_S64: type = 5; break;
default:
assert(insn->dType == TYPE_U32);
break;
}
// atomic operation
if (insn->subOp == NV50_IR_SUBOP_ATOM_CAS) {
subOp = 0;
} else if (insn->subOp == NV50_IR_SUBOP_ATOM_EXCH) {
subOp = 8;
} else {
subOp = insn->subOp;
}
emitField(0x24, 3, type);
emitField(0x1d, 4, subOp);
emitGPR (0x14, insn->src(1));
emitGPR (0x08, insn->src(0));
emitGPR (0x00, insn->def(0));
emitSUHandle(2);
}
/*******************************************************************************
* assembler front-end
******************************************************************************/
bool
CodeEmitterGM107::emitInstruction(Instruction *i)
{
const unsigned int size = (writeIssueDelays && !(codeSize & 0x1f)) ? 16 : 8;
bool ret = true;
insn = i;
if (insn->encSize != 8) {
ERROR("skipping undecodable instruction: "); insn->print();
return false;
} else
if (codeSize + size > codeSizeLimit) {
ERROR("code emitter output buffer too small\n");
return false;
}
if (writeIssueDelays) {
int n = ((codeSize & 0x1f) / 8) - 1;
if (n < 0) {
data = code;
data[0] = 0x00000000;
data[1] = 0x00000000;
code += 2;
codeSize += 8;
n++;
}
emitField(data, n * 21, 21, insn->sched);
}
switch (insn->op) {
case OP_EXIT:
emitEXIT();
break;
case OP_BRA:
emitBRA();
break;
case OP_CALL:
emitCAL();
break;
case OP_PRECONT:
emitPCNT();
break;
case OP_CONT:
emitCONT();
break;
case OP_PREBREAK:
emitPBK();
break;
case OP_BREAK:
emitBRK();
break;
case OP_PRERET:
emitPRET();
break;
case OP_RET:
emitRET();
break;
case OP_JOINAT:
emitSSY();
break;
case OP_JOIN:
emitSYNC();
break;
case OP_QUADON:
emitSAM();
break;
case OP_QUADPOP:
emitRAM();
break;
case OP_MOV:
emitMOV();
break;
case OP_RDSV:
if (targGM107->isCS2RSV(insn->getSrc(0)->reg.data.sv.sv))
emitCS2R();
else
emitS2R();
break;
case OP_ABS:
case OP_NEG:
case OP_SAT:
case OP_FLOOR:
case OP_CEIL:
case OP_TRUNC:
case OP_CVT:
if (insn->op == OP_CVT && (insn->def(0).getFile() == FILE_PREDICATE ||
insn->src(0).getFile() == FILE_PREDICATE)) {
emitMOV();
} else if (isFloatType(insn->dType)) {
if (isFloatType(insn->sType))
emitF2F();
else
emitI2F();
} else {
if (isFloatType(insn->sType))
emitF2I();
else
emitI2I();
}
break;
case OP_SHFL:
emitSHFL();
break;
case OP_ADD:
case OP_SUB:
if (isFloatType(insn->dType)) {
if (insn->dType == TYPE_F64)
emitDADD();
else
emitFADD();
} else {
emitIADD();
}
break;
case OP_MUL:
if (isFloatType(insn->dType)) {
if (insn->dType == TYPE_F64)
emitDMUL();
else
emitFMUL();
} else {
emitIMUL();
}
break;
case OP_MAD:
case OP_FMA:
if (isFloatType(insn->dType)) {
if (insn->dType == TYPE_F64)
emitDFMA();
else
emitFFMA();
} else {
emitIMAD();
}
break;
case OP_SHLADD:
emitISCADD();
break;
case OP_XMAD:
emitXMAD();
break;
case OP_MIN:
case OP_MAX:
if (isFloatType(insn->dType)) {
if (insn->dType == TYPE_F64)
emitDMNMX();
else
emitFMNMX();
} else {
emitIMNMX();
}
break;
case OP_SHL:
if (typeSizeof(insn->sType) == 8)
emitSHF();
else
emitSHL();
break;
case OP_SHR:
if (typeSizeof(insn->sType) == 8)
emitSHF();
else
emitSHR();
break;
case OP_POPCNT:
emitPOPC();
break;
case OP_INSBF:
emitBFI();
break;
case OP_EXTBF:
emitBFE();
break;
case OP_BFIND:
emitFLO();
break;
case OP_SLCT:
if (isFloatType(insn->dType))
emitFCMP();
else
emitICMP();
break;
case OP_SET:
case OP_SET_AND:
case OP_SET_OR:
case OP_SET_XOR:
if (insn->def(0).getFile() != FILE_PREDICATE) {
if (isFloatType(insn->sType))
if (insn->sType == TYPE_F64)
emitDSET();
else
emitFSET();
else
emitISET();
} else {
if (isFloatType(insn->sType))
if (insn->sType == TYPE_F64)
emitDSETP();
else
emitFSETP();
else
emitISETP();
}
break;
case OP_SELP:
emitSEL();
break;
case OP_PRESIN:
case OP_PREEX2:
emitRRO();
break;
case OP_COS:
case OP_SIN:
case OP_EX2:
case OP_LG2:
case OP_RCP:
case OP_RSQ:
case OP_SQRT:
emitMUFU();
break;
case OP_AND:
case OP_OR:
case OP_XOR:
emitLOP();
break;
case OP_NOT:
emitNOT();
break;
case OP_LOAD:
switch (insn->src(0).getFile()) {
case FILE_MEMORY_CONST : emitLDC(); break;
case FILE_MEMORY_LOCAL : emitLDL(); break;
case FILE_MEMORY_SHARED: emitLDS(); break;
case FILE_MEMORY_GLOBAL: emitLD(); break;
default:
assert(!"invalid load");
emitNOP();
break;
}
break;
case OP_STORE:
switch (insn->src(0).getFile()) {
case FILE_MEMORY_LOCAL : emitSTL(); break;
case FILE_MEMORY_SHARED: emitSTS(); break;
case FILE_MEMORY_GLOBAL: emitST(); break;
default:
assert(!"invalid store");
emitNOP();
break;
}
break;
case OP_ATOM:
if (insn->src(0).getFile() == FILE_MEMORY_SHARED)
emitATOMS();
else
if (!insn->defExists(0) && insn->subOp < NV50_IR_SUBOP_ATOM_CAS)
emitRED();
else
emitATOM();
break;
case OP_CCTL:
emitCCTL();
break;
case OP_VFETCH:
emitALD();
break;
case OP_EXPORT:
emitAST();
break;
case OP_PFETCH:
emitISBERD();
break;
case OP_AFETCH:
emitAL2P();
break;
case OP_LINTERP:
case OP_PINTERP:
emitIPA();
break;
case OP_PIXLD:
emitPIXLD();
break;
case OP_TEX:
case OP_TXL:
if (insn->asTex()->tex.scalar)
emitTEXS();
else
emitTEX();
break;
case OP_TXB:
emitTEX();
break;
case OP_TXF:
if (insn->asTex()->tex.scalar)
emitTEXS();
else
emitTLD();
break;
case OP_TXG:
if (insn->asTex()->tex.scalar)
emitTEXS();
else
emitTLD4();
break;
case OP_TXD:
emitTXD();
break;
case OP_TXQ:
emitTXQ();
break;
case OP_TXLQ:
emitTMML();
break;
case OP_TEXBAR:
emitDEPBAR();
break;
case OP_QUADOP:
emitFSWZADD();
break;
case OP_NOP:
emitNOP();
break;
case OP_DISCARD:
emitKIL();
break;
case OP_EMIT:
case OP_RESTART:
emitOUT();
break;
case OP_BAR:
emitBAR();
break;
case OP_MEMBAR:
emitMEMBAR();
break;
case OP_VOTE:
emitVOTE();
break;
case OP_SUSTB:
case OP_SUSTP:
emitSUSTx();
break;
case OP_SULDB:
case OP_SULDP:
emitSULDx();
break;
case OP_SUREDB:
case OP_SUREDP:
emitSUREDx();
break;
default:
assert(!"invalid opcode");
emitNOP();
ret = false;
break;
}
if (insn->join) {
/*XXX*/
}
code += 2;
codeSize += 8;
return ret;
}
uint32_t
CodeEmitterGM107::getMinEncodingSize(const Instruction *i) const
{
return 8;
}
/*******************************************************************************
* sched data calculator
******************************************************************************/
class SchedDataCalculatorGM107 : public Pass
{
public:
SchedDataCalculatorGM107(const TargetGM107 *targ) : targ(targ) {}
private:
struct RegScores
{
struct ScoreData {
int r[256];
int p[8];
int c;
} rd, wr;
int base;
void rebase(const int base)
{
const int delta = this->base - base;
if (!delta)
return;
this->base = 0;
for (int i = 0; i < 256; ++i) {
rd.r[i] += delta;
wr.r[i] += delta;
}
for (int i = 0; i < 8; ++i) {
rd.p[i] += delta;
wr.p[i] += delta;
}
rd.c += delta;
wr.c += delta;
}
void wipe()
{
memset(&rd, 0, sizeof(rd));
memset(&wr, 0, sizeof(wr));
}
int getLatest(const ScoreData& d) const
{
int max = 0;
for (int i = 0; i < 256; ++i)
if (d.r[i] > max)
max = d.r[i];
for (int i = 0; i < 8; ++i)
if (d.p[i] > max)
max = d.p[i];
if (d.c > max)
max = d.c;
return max;
}
inline int getLatestRd() const
{
return getLatest(rd);
}
inline int getLatestWr() const
{
return getLatest(wr);
}
inline int getLatest() const
{
return MAX2(getLatestRd(), getLatestWr());
}
void setMax(const RegScores *that)
{
for (int i = 0; i < 256; ++i) {
rd.r[i] = MAX2(rd.r[i], that->rd.r[i]);
wr.r[i] = MAX2(wr.r[i], that->wr.r[i]);
}
for (int i = 0; i < 8; ++i) {
rd.p[i] = MAX2(rd.p[i], that->rd.p[i]);
wr.p[i] = MAX2(wr.p[i], that->wr.p[i]);
}
rd.c = MAX2(rd.c, that->rd.c);
wr.c = MAX2(wr.c, that->wr.c);
}
void print(int cycle)
{
for (int i = 0; i < 256; ++i) {
if (rd.r[i] > cycle)
INFO("rd $r%i @ %i\n", i, rd.r[i]);
if (wr.r[i] > cycle)
INFO("wr $r%i @ %i\n", i, wr.r[i]);
}
for (int i = 0; i < 8; ++i) {
if (rd.p[i] > cycle)
INFO("rd $p%i @ %i\n", i, rd.p[i]);
if (wr.p[i] > cycle)
INFO("wr $p%i @ %i\n", i, wr.p[i]);
}
if (rd.c > cycle)
INFO("rd $c @ %i\n", rd.c);
if (wr.c > cycle)
INFO("wr $c @ %i\n", wr.c);
}
};
RegScores *score; // for current BB
std::vector<RegScores> scoreBoards;
const TargetGM107 *targ;
bool visit(Function *);
bool visit(BasicBlock *);
void commitInsn(const Instruction *, int);
int calcDelay(const Instruction *, int) const;
void setDelay(Instruction *, int, const Instruction *);
void recordWr(const Value *, int, int);
void checkRd(const Value *, int, int&) const;
inline void emitYield(Instruction *);
inline void emitStall(Instruction *, uint8_t);
inline void emitReuse(Instruction *, uint8_t);
inline void emitWrDepBar(Instruction *, uint8_t);
inline void emitRdDepBar(Instruction *, uint8_t);
inline void emitWtDepBar(Instruction *, uint8_t);
inline int getStall(const Instruction *) const;
inline int getWrDepBar(const Instruction *) const;
inline int getRdDepBar(const Instruction *) const;
inline int getWtDepBar(const Instruction *) const;
void setReuseFlag(Instruction *);
inline void printSchedInfo(int, const Instruction *) const;
struct LiveBarUse {
LiveBarUse(Instruction *insn, Instruction *usei)
: insn(insn), usei(usei) { }
Instruction *insn;
Instruction *usei;
};
struct LiveBarDef {
LiveBarDef(Instruction *insn, Instruction *defi)
: insn(insn), defi(defi) { }
Instruction *insn;
Instruction *defi;
};
bool insertBarriers(BasicBlock *);
bool doesInsnWriteTo(const Instruction *insn, const Value *val) const;
Instruction *findFirstUse(const Instruction *) const;
Instruction *findFirstDef(const Instruction *) const;
bool needRdDepBar(const Instruction *) const;
bool needWrDepBar(const Instruction *) const;
};
inline void
SchedDataCalculatorGM107::emitStall(Instruction *insn, uint8_t cnt)
{
assert(cnt < 16);
insn->sched |= cnt;
}
inline void
SchedDataCalculatorGM107::emitYield(Instruction *insn)
{
insn->sched |= 1 << 4;
}
inline void
SchedDataCalculatorGM107::emitWrDepBar(Instruction *insn, uint8_t id)
{
assert(id < 6);
if ((insn->sched & 0xe0) == 0xe0)
insn->sched ^= 0xe0;
insn->sched |= id << 5;
}
inline void
SchedDataCalculatorGM107::emitRdDepBar(Instruction *insn, uint8_t id)
{
assert(id < 6);
if ((insn->sched & 0x700) == 0x700)
insn->sched ^= 0x700;
insn->sched |= id << 8;
}
inline void
SchedDataCalculatorGM107::emitWtDepBar(Instruction *insn, uint8_t id)
{
assert(id < 6);
insn->sched |= 1 << (11 + id);
}
inline void
SchedDataCalculatorGM107::emitReuse(Instruction *insn, uint8_t id)
{
assert(id < 4);
insn->sched |= 1 << (17 + id);
}
inline void
SchedDataCalculatorGM107::printSchedInfo(int cycle,
const Instruction *insn) const
{
uint8_t st, yl, wr, rd, wt, ru;
st = (insn->sched & 0x00000f) >> 0;
yl = (insn->sched & 0x000010) >> 4;
wr = (insn->sched & 0x0000e0) >> 5;
rd = (insn->sched & 0x000700) >> 8;
wt = (insn->sched & 0x01f800) >> 11;
ru = (insn->sched & 0x1e0000) >> 17;
INFO("cycle %i, (st 0x%x, yl 0x%x, wr 0x%x, rd 0x%x, wt 0x%x, ru 0x%x)\n",
cycle, st, yl, wr, rd, wt, ru);
}
inline int
SchedDataCalculatorGM107::getStall(const Instruction *insn) const
{
return insn->sched & 0xf;
}
inline int
SchedDataCalculatorGM107::getWrDepBar(const Instruction *insn) const
{
return (insn->sched & 0x0000e0) >> 5;
}
inline int
SchedDataCalculatorGM107::getRdDepBar(const Instruction *insn) const
{
return (insn->sched & 0x000700) >> 8;
}
inline int
SchedDataCalculatorGM107::getWtDepBar(const Instruction *insn) const
{
return (insn->sched & 0x01f800) >> 11;
}
// Emit the reuse flag which allows to make use of the new memory hierarchy
// introduced since Maxwell, the operand reuse cache.
//
// It allows to reduce bank conflicts by caching operands. Each time you issue
// an instruction, that flag can tell the hw which operands are going to be
// re-used by the next instruction. Note that the next instruction has to use
// the same GPR id in the same operand slot.
void
SchedDataCalculatorGM107::setReuseFlag(Instruction *insn)
{
Instruction *next = insn->next;
BitSet defs(255, 1);
if (!targ->isReuseSupported(insn))
return;
for (int d = 0; insn->defExists(d); ++d) {
const Value *def = insn->def(d).rep();
if (insn->def(d).getFile() != FILE_GPR)
continue;
if (typeSizeof(insn->dType) != 4 || def->reg.data.id == 255)
continue;
defs.set(def->reg.data.id);
}
for (int s = 0; insn->srcExists(s); s++) {
const Value *src = insn->src(s).rep();
if (insn->src(s).getFile() != FILE_GPR)
continue;
if (typeSizeof(insn->sType) != 4 || src->reg.data.id == 255)
continue;
if (defs.test(src->reg.data.id))
continue;
if (!next->srcExists(s) || next->src(s).getFile() != FILE_GPR)
continue;
if (src->reg.data.id != next->getSrc(s)->reg.data.id)
continue;
assert(s < 4);
emitReuse(insn, s);
}
}
void
SchedDataCalculatorGM107::recordWr(const Value *v, int cycle, int ready)
{
int a = v->reg.data.id, b;
switch (v->reg.file) {
case FILE_GPR:
b = a + v->reg.size / 4;
for (int r = a; r < b; ++r)
score->rd.r[r] = ready;
break;
case FILE_PREDICATE:
// To immediately use a predicate set by any instructions, the minimum
// number of stall counts is 13.
score->rd.p[a] = cycle + 13;
break;
case FILE_FLAGS:
score->rd.c = ready;
break;
default:
break;
}
}
void
SchedDataCalculatorGM107::checkRd(const Value *v, int cycle, int &delay) const
{
int a = v->reg.data.id, b;
int ready = cycle;
switch (v->reg.file) {
case FILE_GPR:
b = a + v->reg.size / 4;
for (int r = a; r < b; ++r)
ready = MAX2(ready, score->rd.r[r]);
break;
case FILE_PREDICATE:
ready = MAX2(ready, score->rd.p[a]);
break;
case FILE_FLAGS:
ready = MAX2(ready, score->rd.c);
break;
default:
break;
}
if (cycle < ready)
delay = MAX2(delay, ready - cycle);
}
void
SchedDataCalculatorGM107::commitInsn(const Instruction *insn, int cycle)
{
const int ready = cycle + targ->getLatency(insn);
for (int d = 0; insn->defExists(d); ++d)
recordWr(insn->getDef(d), cycle, ready);
#ifdef GM107_DEBUG_SCHED_DATA
score->print(cycle);
#endif
}
#define GM107_MIN_ISSUE_DELAY 0x1
#define GM107_MAX_ISSUE_DELAY 0xf
int
SchedDataCalculatorGM107::calcDelay(const Instruction *insn, int cycle) const
{
int delay = 0, ready = cycle;
for (int s = 0; insn->srcExists(s); ++s)
checkRd(insn->getSrc(s), cycle, delay);
// TODO: make use of getReadLatency()!
return MAX2(delay, ready - cycle);
}
void
SchedDataCalculatorGM107::setDelay(Instruction *insn, int delay,
const Instruction *next)
{
const OpClass cl = targ->getOpClass(insn->op);
int wr, rd;
if (insn->op == OP_EXIT ||
insn->op == OP_BAR ||
insn->op == OP_MEMBAR) {
delay = GM107_MAX_ISSUE_DELAY;
} else
if (insn->op == OP_QUADON ||
insn->op == OP_QUADPOP) {
delay = 0xd;
} else
if (cl == OPCLASS_FLOW || insn->join) {
delay = 0xd;
}
if (!next || !targ->canDualIssue(insn, next)) {
delay = CLAMP(delay, GM107_MIN_ISSUE_DELAY, GM107_MAX_ISSUE_DELAY);
} else {
delay = 0x0; // dual-issue
}
wr = getWrDepBar(insn);
rd = getRdDepBar(insn);
if (delay == GM107_MIN_ISSUE_DELAY && (wr & rd) != 7) {
// Barriers take one additional clock cycle to become active on top of
// the clock consumed by the instruction producing it.
if (!next || insn->bb != next->bb) {
delay = 0x2;
} else {
int wt = getWtDepBar(next);
if ((wt & (1 << wr)) | (wt & (1 << rd)))
delay = 0x2;
}
}
emitStall(insn, delay);
}
// Return true when the given instruction needs to emit a read dependency
// barrier (for WaR hazards) because it doesn't operate at a fixed latency, and
// setting the maximum number of stall counts is not enough.
bool
SchedDataCalculatorGM107::needRdDepBar(const Instruction *insn) const
{
BitSet srcs(255, 1), defs(255, 1);
int a, b;
if (!targ->isBarrierRequired(insn))
return false;
// Do not emit a read dependency barrier when the instruction doesn't use
// any GPR (like st s[0x4] 0x0) as input because it's unnecessary.
for (int s = 0; insn->srcExists(s); ++s) {
const Value *src = insn->src(s).rep();
if (insn->src(s).getFile() != FILE_GPR)
continue;
if (src->reg.data.id == 255)
continue;
a = src->reg.data.id;
b = a + src->reg.size / 4;
for (int r = a; r < b; ++r)
srcs.set(r);
}
if (!srcs.popCount())
return false;
// Do not emit a read dependency barrier when the output GPRs are equal to
// the input GPRs (like rcp $r0 $r0) because a write dependency barrier will
// be produced and WaR hazards are prevented.
for (int d = 0; insn->defExists(d); ++d) {
const Value *def = insn->def(d).rep();
if (insn->def(d).getFile() != FILE_GPR)
continue;
if (def->reg.data.id == 255)
continue;
a = def->reg.data.id;
b = a + def->reg.size / 4;
for (int r = a; r < b; ++r)
defs.set(r);
}
srcs.andNot(defs);
if (!srcs.popCount())
return false;
return true;
}
// Return true when the given instruction needs to emit a write dependency
// barrier (for RaW hazards) because it doesn't operate at a fixed latency, and
// setting the maximum number of stall counts is not enough. This is only legal
// if the instruction output something.
bool
SchedDataCalculatorGM107::needWrDepBar(const Instruction *insn) const
{
if (!targ->isBarrierRequired(insn))
return false;
for (int d = 0; insn->defExists(d); ++d) {
if (insn->def(d).getFile() == FILE_GPR ||
insn->def(d).getFile() == FILE_FLAGS ||
insn->def(d).getFile() == FILE_PREDICATE)
return true;
}
return false;
}
// Helper function for findFirstUse() and findFirstDef()
bool
SchedDataCalculatorGM107::doesInsnWriteTo(const Instruction *insn,
const Value *val) const
{
if (val->reg.file != FILE_GPR &&
val->reg.file != FILE_PREDICATE &&
val->reg.file != FILE_FLAGS)
return false;
for (int d = 0; insn->defExists(d); ++d) {
const Value* def = insn->getDef(d);
int minGPR = def->reg.data.id;
int maxGPR = minGPR + def->reg.size / 4 - 1;
if (def->reg.file != val->reg.file)
continue;
if (def->reg.file == FILE_GPR) {
if (val->reg.data.id + val->reg.size / 4 - 1 < minGPR ||
val->reg.data.id > maxGPR)
continue;
return true;
} else
if (def->reg.file == FILE_PREDICATE) {
if (val->reg.data.id != minGPR)
continue;
return true;
} else
if (def->reg.file == FILE_FLAGS) {
if (val->reg.data.id != minGPR)
continue;
return true;
}
}
return false;
}
// Find the next instruction inside the same basic block which uses (reads or
// writes from) the output of the given instruction in order to avoid RaW and
// WaW hazards.
Instruction *
SchedDataCalculatorGM107::findFirstUse(const Instruction *bari) const
{
Instruction *insn, *next;
if (!bari->defExists(0))
return NULL;
for (insn = bari->next; insn != NULL; insn = next) {
next = insn->next;
for (int s = 0; insn->srcExists(s); ++s)
if (doesInsnWriteTo(bari, insn->getSrc(s)))
return insn;
for (int d = 0; insn->defExists(d); ++d)
if (doesInsnWriteTo(bari, insn->getDef(d)))
return insn;
}
return NULL;
}
// Find the next instruction inside the same basic block which overwrites, at
// least, one source of the given instruction in order to avoid WaR hazards.
Instruction *
SchedDataCalculatorGM107::findFirstDef(const Instruction *bari) const
{
Instruction *insn, *next;
if (!bari->srcExists(0))
return NULL;
for (insn = bari->next; insn != NULL; insn = next) {
next = insn->next;
for (int s = 0; bari->srcExists(s); ++s)
if (doesInsnWriteTo(insn, bari->getSrc(s)))
return insn;
}
return NULL;
}
// Dependency barriers:
// This pass is a bit ugly and could probably be improved by performing a
// better allocation.
//
// The main idea is to avoid WaR and RaW hazards by emitting read/write
// dependency barriers using the control codes.
bool
SchedDataCalculatorGM107::insertBarriers(BasicBlock *bb)
{
std::list<LiveBarUse> live_uses;
std::list<LiveBarDef> live_defs;
Instruction *insn, *next;
BitSet bars(6, 1);
int bar_id;
for (insn = bb->getEntry(); insn != NULL; insn = next) {
Instruction *usei = NULL, *defi = NULL;
bool need_wr_bar, need_rd_bar;
next = insn->next;
// Expire old barrier uses.
for (std::list<LiveBarUse>::iterator it = live_uses.begin();
it != live_uses.end();) {
if (insn->serial >= it->usei->serial) {
int wr = getWrDepBar(it->insn);
emitWtDepBar(insn, wr);
bars.clr(wr); // free barrier
it = live_uses.erase(it);
continue;
}
++it;
}
// Expire old barrier defs.
for (std::list<LiveBarDef>::iterator it = live_defs.begin();
it != live_defs.end();) {
if (insn->serial >= it->defi->serial) {
int rd = getRdDepBar(it->insn);
emitWtDepBar(insn, rd);
bars.clr(rd); // free barrier
it = live_defs.erase(it);
continue;
}
++it;
}
need_wr_bar = needWrDepBar(insn);
need_rd_bar = needRdDepBar(insn);
if (need_wr_bar) {
// When the instruction requires to emit a write dependency barrier
// (all which write something at a variable latency), find the next
// instruction which reads the outputs (or writes to them, potentially
// completing before this insn.
usei = findFirstUse(insn);
// Allocate and emit a new barrier.
bar_id = bars.findFreeRange(1);
if (bar_id == -1)
bar_id = 5;
bars.set(bar_id);
emitWrDepBar(insn, bar_id);
if (usei)
live_uses.push_back(LiveBarUse(insn, usei));
}
if (need_rd_bar) {
// When the instruction requires to emit a read dependency barrier
// (all which read something at a variable latency), find the next
// instruction which will write the inputs.
defi = findFirstDef(insn);
if (usei && defi && usei->serial <= defi->serial)
continue;
// Allocate and emit a new barrier.
bar_id = bars.findFreeRange(1);
if (bar_id == -1)
bar_id = 5;
bars.set(bar_id);
emitRdDepBar(insn, bar_id);
if (defi)
live_defs.push_back(LiveBarDef(insn, defi));
}
}
// Remove unnecessary barrier waits.
BitSet alive_bars(6, 1);
for (insn = bb->getEntry(); insn != NULL; insn = next) {
int wr, rd, wt;
next = insn->next;
wr = getWrDepBar(insn);
rd = getRdDepBar(insn);
wt = getWtDepBar(insn);
for (int idx = 0; idx < 6; ++idx) {
if (!(wt & (1 << idx)))
continue;
if (!alive_bars.test(idx)) {
insn->sched &= ~(1 << (11 + idx));
} else {
alive_bars.clr(idx);
}
}
if (wr < 6)
alive_bars.set(wr);
if (rd < 6)
alive_bars.set(rd);
}
return true;
}
bool
SchedDataCalculatorGM107::visit(Function *func)
{
ArrayList insns;
func->orderInstructions(insns);
scoreBoards.resize(func->cfg.getSize());
for (size_t i = 0; i < scoreBoards.size(); ++i)
scoreBoards[i].wipe();
return true;
}
bool
SchedDataCalculatorGM107::visit(BasicBlock *bb)
{
Instruction *insn, *next = NULL;
int cycle = 0;
for (Instruction *insn = bb->getEntry(); insn; insn = insn->next) {
/*XXX*/
insn->sched = 0x7e0;
}
if (!debug_get_bool_option("NV50_PROG_SCHED", true))
return true;
// Insert read/write dependency barriers for instructions which don't
// operate at a fixed latency.
insertBarriers(bb);
score = &scoreBoards.at(bb->getId());
for (Graph::EdgeIterator ei = bb->cfg.incident(); !ei.end(); ei.next()) {
// back branches will wait until all target dependencies are satisfied
if (ei.getType() == Graph::Edge::BACK) // sched would be uninitialized
continue;
BasicBlock *in = BasicBlock::get(ei.getNode());
score->setMax(&scoreBoards.at(in->getId()));
}
#ifdef GM107_DEBUG_SCHED_DATA
INFO("=== BB:%i initial scores\n", bb->getId());
score->print(cycle);
#endif
// Because barriers are allocated locally (intra-BB), we have to make sure
// that all produced barriers have been consumed before entering inside a
// new basic block. The best way is to do a global allocation pre RA but
// it's really more difficult, especially because of the phi nodes. Anyways,
// it seems like that waiting on a barrier which has already been consumed
// doesn't add any additional cost, it's just not elegant!
Instruction *start = bb->getEntry();
if (start && bb->cfg.incidentCount() > 0) {
for (int b = 0; b < 6; b++)
emitWtDepBar(start, b);
}
for (insn = bb->getEntry(); insn && insn->next; insn = insn->next) {
next = insn->next;
commitInsn(insn, cycle);
int delay = calcDelay(next, cycle);
setDelay(insn, delay, next);
cycle += getStall(insn);
setReuseFlag(insn);
// XXX: The yield flag seems to destroy a bunch of things when it is
// set on every instruction, need investigation.
//emitYield(insn);
#ifdef GM107_DEBUG_SCHED_DATA
printSchedInfo(cycle, insn);
insn->print();
next->print();
#endif
}
if (!insn)
return true;
commitInsn(insn, cycle);
int bbDelay = -1;
#ifdef GM107_DEBUG_SCHED_DATA
fprintf(stderr, "last instruction is : ");
insn->print();
fprintf(stderr, "cycle=%d\n", cycle);
#endif
for (Graph::EdgeIterator ei = bb->cfg.outgoing(); !ei.end(); ei.next()) {
BasicBlock *out = BasicBlock::get(ei.getNode());
if (ei.getType() != Graph::Edge::BACK) {
// Only test the first instruction of the outgoing block.
next = out->getEntry();
if (next) {
bbDelay = MAX2(bbDelay, calcDelay(next, cycle));
} else {
// When the outgoing BB is empty, make sure to set the number of
// stall counts needed by the instruction because we don't know the
// next instruction.
bbDelay = MAX2(bbDelay, targ->getLatency(insn));
}
} else {
// Wait until all dependencies are satisfied.
const int regsFree = score->getLatest();
next = out->getFirst();
for (int c = cycle; next && c < regsFree; next = next->next) {
bbDelay = MAX2(bbDelay, calcDelay(next, c));
c += getStall(next);
}
next = NULL;
}
}
if (bb->cfg.outgoingCount() != 1)
next = NULL;
setDelay(insn, bbDelay, next);
cycle += getStall(insn);
score->rebase(cycle); // common base for initializing out blocks' scores
return true;
}
/*******************************************************************************
* main
******************************************************************************/
void
CodeEmitterGM107::prepareEmission(Function *func)
{
SchedDataCalculatorGM107 sched(targGM107);
CodeEmitter::prepareEmission(func);
sched.run(func, true, true);
}
static inline uint32_t sizeToBundlesGM107(uint32_t size)
{
return (size + 23) / 24;
}
void
CodeEmitterGM107::prepareEmission(Program *prog)
{
for (ArrayList::Iterator fi = prog->allFuncs.iterator();
!fi.end(); fi.next()) {
Function *func = reinterpret_cast<Function *>(fi.get());
func->binPos = prog->binSize;
prepareEmission(func);
// adjust sizes & positions for schedulding info:
if (prog->getTarget()->hasSWSched) {
uint32_t adjPos = func->binPos;
BasicBlock *bb = NULL;
for (int i = 0; i < func->bbCount; ++i) {
bb = func->bbArray[i];
int32_t adjSize = bb->binSize;
if (adjPos % 32) {
adjSize -= 32 - adjPos % 32;
if (adjSize < 0)
adjSize = 0;
}
adjSize = bb->binSize + sizeToBundlesGM107(adjSize) * 8;
bb->binPos = adjPos;
bb->binSize = adjSize;
adjPos += adjSize;
}
if (bb)
func->binSize = adjPos - func->binPos;
}
prog->binSize += func->binSize;
}
}
CodeEmitterGM107::CodeEmitterGM107(const TargetGM107 *target)
: CodeEmitter(target),
targGM107(target),
writeIssueDelays(target->hasSWSched)
{
code = NULL;
codeSize = codeSizeLimit = 0;
relocInfo = NULL;
}
CodeEmitter *
TargetGM107::createCodeEmitterGM107(Program::Type type)
{
CodeEmitterGM107 *emit = new CodeEmitterGM107(this);
emit->setProgramType(type);
return emit;
}
} // namespace nv50_ir
|