File: BinaryEncodingCNL.cpp

package info (click to toggle)
intel-graphics-compiler2 2.20.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 107,552 kB
  • sloc: cpp: 807,012; lisp: 287,936; ansic: 16,397; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 37
file content (2569 lines) | stat: -rw-r--r-- 88,196 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "Assertions.h"
#include "BinaryEncodingCNL.h"
#include "BuildIR.h"
using namespace vISA;

////////////////////////////// DST ////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// SRC ///////////////
////////////////////////////// SRC ///////////////

uint32_t BinaryEncodingBase::getEUOpcode(G4_opcode g4opc) {
  G9HDL::EU_OPCODE euopcode = G9HDL::EU_OPCODE_ILLEGAL;
  switch (g4opc) {
    // 3src - complete
  case G4_bfe:
    euopcode = G9HDL::EU_OPCODE_BFE;
    break;
  case G4_bfi2:
    euopcode = G9HDL::EU_OPCODE_BFI2;
    break;
  case G4_csel:
    euopcode = G9HDL::EU_OPCODE_CSEL;
    break;
  case G4_lrp:
    euopcode = G9HDL::EU_OPCODE_LRP;
    break;
  case G4_mad:
    euopcode = G9HDL::EU_OPCODE_MAD;
    break;
  case G4_madm:
    euopcode = G9HDL::EU_OPCODE_MADM;
    break;

    // 1src - complete
  case G4_bfrev:
    euopcode = G9HDL::EU_OPCODE_BFREV;
    break;
  case G4_cbit:
    euopcode = G9HDL::EU_OPCODE_CBIT;
    break;
  case G4_fbh:
    euopcode = G9HDL::EU_OPCODE_FBH;
    break;
  case G4_fbl:
    euopcode = G9HDL::EU_OPCODE_FBL;
    break;
  case G4_lzd:
    euopcode = G9HDL::EU_OPCODE_LZD;
    break;
  case G4_mov:
    euopcode = G9HDL::EU_OPCODE_MOV;
    break;
  case G4_movi:
    euopcode = G9HDL::EU_OPCODE_MOVI;
    break;
  case G4_not:
    euopcode = G9HDL::EU_OPCODE_NOT;
    break;
  case G4_rndd:
    euopcode = G9HDL::EU_OPCODE_RNDD;
    break;
  case G4_rnde:
    euopcode = G9HDL::EU_OPCODE_RNDE;
    break;
  case G4_rndu:
    euopcode = G9HDL::EU_OPCODE_RNDU;
    break;
  case G4_rndz:
    euopcode = G9HDL::EU_OPCODE_RNDZ;
    break;
  case G4_frc:
    euopcode = G9HDL::EU_OPCODE_FRC;
    break;

    // 2src - complete
  case G4_add:
    euopcode = G9HDL::EU_OPCODE_ADD;
    break;
  case G4_addc:
    euopcode = G9HDL::EU_OPCODE_ADDC;
    break;
  case G4_and:
    euopcode = G9HDL::EU_OPCODE_AND;
    break;
  case G4_asr:
    euopcode = G9HDL::EU_OPCODE_ASR;
    break;
  case G4_avg:
    euopcode = G9HDL::EU_OPCODE_AVG;
    break;
  case G4_bfi1:
    euopcode = G9HDL::EU_OPCODE_BFI1;
    break;
  case G4_cmp:
    euopcode = G9HDL::EU_OPCODE_CMP;
    break;
  case G4_cmpn:
    euopcode = G9HDL::EU_OPCODE_CMPN;
    break;
  case G4_dp2:
    euopcode = G9HDL::EU_OPCODE_DP2;
    break;
  case G4_dp3:
    euopcode = G9HDL::EU_OPCODE_DP3;
    break;
  case G4_dp4:
    euopcode = G9HDL::EU_OPCODE_DP4;
    break;
  case G4_dph:
    euopcode = G9HDL::EU_OPCODE_DPH;
    break;
  case G4_line:
    euopcode = G9HDL::EU_OPCODE_LINE;
    break;
  case G4_mac:
    euopcode = G9HDL::EU_OPCODE_MAC;
    break;
  case G4_mach:
    euopcode = G9HDL::EU_OPCODE_MACH;
    break;
  case G4_mul:
    euopcode = G9HDL::EU_OPCODE_MUL;
    break;
  case G4_or:
    euopcode = G9HDL::EU_OPCODE_OR;
    break;
  case G4_pln:
    euopcode = G9HDL::EU_OPCODE_PLN;
    break;
  case G4_sad2:
    euopcode = G9HDL::EU_OPCODE_SAD2;
    break;
  case G4_sada2:
    euopcode = G9HDL::EU_OPCODE_SADA2;
    break;
  case G4_sel:
    euopcode = G9HDL::EU_OPCODE_SEL;
    break;
  case G4_shl:
    euopcode = G9HDL::EU_OPCODE_SHL;
    break;
  case G4_shr:
    euopcode = G9HDL::EU_OPCODE_SHR;
    break;
  case G4_xor:
    euopcode = G9HDL::EU_OPCODE_XOR;
    break;
  case G4_subb:
    euopcode = G9HDL::EU_OPCODE_SUBB;
    break;

    // send type
  case G4_send:
    euopcode = G9HDL::EU_OPCODE_SEND;
    break;
  case G4_sendc:
    euopcode = G9HDL::EU_OPCODE_SENDC;
    break;
  case G4_sends:
    euopcode = G9HDL::EU_OPCODE_SENDS;
    break;
  case G4_sendsc:
    euopcode = G9HDL::EU_OPCODE_SENDSC;
    break;

    // math type
  case G4_math:
    euopcode = G9HDL::EU_OPCODE_MATH;
    break;

    // control flow
  case G4_brc:
    euopcode = G9HDL::EU_OPCODE_BRC;
    break;
  case G4_brd:
    euopcode = G9HDL::EU_OPCODE_BRD;
    break;
  case G4_break:
    euopcode = G9HDL::EU_OPCODE_BREAK;
    break;
  case G4_call:
    euopcode = G9HDL::EU_OPCODE_CALL;
    break;
    // case G4_calla: euopcode = G9HDL::EU_OPCODE_CALLA; break;
  case G4_cont:
    euopcode = G9HDL::EU_OPCODE_CONT;
    break;
  case G4_else:
    euopcode = G9HDL::EU_OPCODE_ELSE;
    break;
  case G4_endif:
    euopcode = G9HDL::EU_OPCODE_ENDIF;
    break;
  case G4_goto:
    euopcode = G9HDL::EU_OPCODE_GOTO;
    break;
  case G4_halt:
    euopcode = G9HDL::EU_OPCODE_HALT;
    break;
  case G4_if:
    euopcode = G9HDL::EU_OPCODE_IF;
    break;
  case G4_jmpi:
    euopcode = G9HDL::EU_OPCODE_JMPI;
    break;
  case G4_join:
    euopcode = G9HDL::EU_OPCODE_JOIN;
    break;
  case G4_return:
    euopcode = G9HDL::EU_OPCODE_RET;
    break;
  case G4_wait:
    euopcode = G9HDL::EU_OPCODE_WAIT;
    break;
  case G4_while:
    euopcode = G9HDL::EU_OPCODE_WHILE;
    break;

    // misc:
  case G4_nop:
    euopcode = G9HDL::EU_OPCODE_NOP;
    break;
  case G4_illegal:
    euopcode = G9HDL::EU_OPCODE_ILLEGAL;
    break;
  case G4_smov:
    euopcode = G9HDL::EU_OPCODE_SMOV;
    break;
  default:
    vISA_ASSERT_UNREACHABLE("Invalid G4 opcode!");
    break;
  }
  return (uint32_t)euopcode;
}

/// \brief Returns the HDL immediate type for a given source operand
///
static inline int GetOperandSrcHDLImmType(G4_Type srcType) {
  int type = G11HDL::SRCIMMTYPE_UD;

  switch (srcType) {
  case Type_UD:
    type = G11HDL::SRCIMMTYPE_UD;
    break;
  case Type_D:
    type = G11HDL::SRCIMMTYPE_D;
    break;
  case Type_UW:
    type = G11HDL::SRCIMMTYPE_UW;
    break;
  case Type_W:
    type = G11HDL::SRCIMMTYPE_W;
    break;
  case Type_UV:
    type = G11HDL::SRCIMMTYPE_UV;
    break;
  case Type_VF:
    type = G11HDL::SRCIMMTYPE_VF;
    break;
  case Type_V:
    type = G11HDL::SRCIMMTYPE_V;
    break;
  case Type_F:
    type = G11HDL::SRCIMMTYPE_F;
    break;
  case Type_UQ:
    type = G11HDL::SRCIMMTYPE_UQ;
    break;
  case Type_Q:
    type = G11HDL::SRCIMMTYPE_Q;
    break;
  case Type_DF:
    type = G11HDL::SRCIMMTYPE_DF;
    break;
  case Type_HF:
    type = G11HDL::SRCIMMTYPE_HF;
    break;
  default:
    vISA_ASSERT_UNREACHABLE("invalid type");
    break;
  }
  return type;
}

/// \brief Returns the HDL source type for a given source operand
///
static inline int GetOperandSrcHDLType(G4_Type regType) {
  int type = G11HDL::SRCTYPE_UD;

  switch (regType) {
  case Type_UD:
    type = G11HDL::SRCTYPE_UD;
    break;
  case Type_D:
    type = G11HDL::SRCTYPE_D;
    break;
  case Type_UW:
    type = G11HDL::SRCTYPE_UW;
    break;
  case Type_W:
    type = G11HDL::SRCTYPE_W;
    break;
  case Type_UB:
    type = G11HDL::SRCTYPE_UB;
    break;
  case Type_B:
    type = G11HDL::SRCTYPE_B;
    break;
  case Type_DF:
    type = G11HDL::SRCTYPE_DF;
    break;
  case Type_F:
    type = G11HDL::SRCTYPE_F;
    break;
  case Type_UQ:
    type = G11HDL::SRCTYPE_UQ;
    break;
  case Type_Q:
    type = G11HDL::SRCTYPE_Q;
    break;
  case Type_HF:
    type = G11HDL::SRCTYPE_HF;
    break;
  default:
    vISA_ASSERT_UNREACHABLE("invalid type");
    break;
  }

  return type;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// ENCODERS ///////////////

////////////////////////////// HEADER ///////////////

/// \brief (Header) Field encoder for instruction opcode
///
void BinaryEncodingCNL::EncodeOpCode(G4_INST *inst,
                                     G9HDL::EU_INSTRUCTION_HEADER &header) {
  G4_opcode opcode = inst->opcode();
  G9HDL::EU_OPCODE euopcode = getEUOpcode(opcode);
  header.SetOpcode(euopcode);
}

G9HDL::EU_OPCODE BinaryEncodingCNL::getEUOpcode(G4_opcode g4opc) {

  switch (g4opc) {
    // GEN11 specific
  case G4_ror:
    return G9HDL::EU_OPCODE_ROR;
  case G4_rol:
    return G9HDL::EU_OPCODE_ROL;
  case G4_dp4a:
    return G9HDL::EU_OPCODE_DP4A;
  default:
    break;
  }

  return (G9HDL::EU_OPCODE)BinaryEncodingBase::getEUOpcode(g4opc);
}

////////////////////////////// HEADER.CONTROLS ///////////////

/// \brief (Helper) Converts v-isa execution size to HDL exec size enumeration
///
static inline G9HDL::EXECSIZE
GetHDLExecSizeFromVISAExecSize(uint32_t execSize) {
  switch (execSize) {
  case ES_1_CHANNEL:
    return G9HDL::EXECSIZE_1_CHANNEL_SCALAR_OPERATION;
  case ES_2_CHANNELS:
    return G9HDL::EXECSIZE_2_CHANNELS;
  case ES_4_CHANNELS:
    return G9HDL::EXECSIZE_4_CHANNELS;
  case ES_8_CHANNELS:
    return G9HDL::EXECSIZE_8_CHANNELS;
  case ES_16_CHANNELS:
    return G9HDL::EXECSIZE_16_CHANNELS;
  case ES_32_CHANNELS:
    return G9HDL::EXECSIZE_32_CHANNELS;
  }
  // Default:
  return G9HDL::EXECSIZE_1_CHANNEL_SCALAR_OPERATION;
}

//////////////////////////////////////////////////////////////////////////
/// \brief (Header) Field encoder for execution size
///
void BinaryEncodingCNL::EncodeExecSize(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS_A &controls) {
  G9HDL::EXECSIZE exSz;
  exSz = GetHDLExecSizeFromVISAExecSize(GetEncodeExecSize(inst));
  controls.SetExecsize(exSz);
}

//////////////////////////////////////////////////////////////////////////
/// \brief (Header) Field encoder for access mode bit
///
void BinaryEncodingCNL::EncodeAccessMode(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS_A &controls) {
  if (inst->isAligned1Inst()) {
    controls.SetAccessmode(G9HDL::ACCESSMODE_ALIGN1);
  } else if (inst->isAligned16Inst()) {
    controls.SetAccessmode(G9HDL::ACCESSMODE_ALIGN16);
  }
}

//////////////////////////////////////////////////////////////////////////
/// \brief (Header) Field encoder for QTR control fields
///
void BinaryEncodingCNL::EncodeQtrControl(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS_A &controls) {
  G9HDL::QTRCTRL qtrCtrl = G9HDL::QTRCTRL_1Q;
  G9HDL::NIBCTRL nibCtrl = G9HDL::NIBCTRL::NIBCTRL_EVEN;

  switch (inst->getMaskOffset()) {
  case 0:
    qtrCtrl = G9HDL::QTRCTRL_1Q;
    nibCtrl = G9HDL::NIBCTRL_ODD;
    break;
  case 4:
    qtrCtrl = G9HDL::QTRCTRL_1Q;
    nibCtrl = G9HDL::NIBCTRL_EVEN;
    break;
  case 8:
    qtrCtrl = G9HDL::QTRCTRL_2Q;
    nibCtrl = G9HDL::NIBCTRL_ODD;
    break;
  case 12:
    qtrCtrl = G9HDL::QTRCTRL_2Q;
    nibCtrl = G9HDL::NIBCTRL_EVEN;
    break;
  case 16:
    qtrCtrl = G9HDL::QTRCTRL_3Q;
    nibCtrl = G9HDL::NIBCTRL_ODD;
    break;
  case 20:
    qtrCtrl = G9HDL::QTRCTRL_3Q;
    nibCtrl = G9HDL::NIBCTRL_EVEN;
    break;
  case 24:
    qtrCtrl = G9HDL::QTRCTRL_4Q;
    nibCtrl = G9HDL::NIBCTRL_ODD;
    break;
  case 28:
    qtrCtrl = G9HDL::QTRCTRL_4Q;
    nibCtrl = G9HDL::NIBCTRL_EVEN;
    break;
  }

  controls.SetQtrctrl(qtrCtrl);
  controls.SetNibctrl(nibCtrl);
}

/// \brief Field encoder for dependency control check fields
inline void BinaryEncodingCNL::EncodeDepControl(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS_A &controlsA) {
  if (inst->isNoDDChkInst()) {
    if (inst->isNoDDClrInst()) {
      controlsA.SetDepctrl(G9HDL::DEPCTRL_NODDCLR_NODDCHK);
    } else {
      controlsA.SetDepctrl(G9HDL::DEPCTRL_NODDCHK);
    }
  } else {
    if (inst->isNoDDClrInst()) {
      controlsA.SetDepctrl(G9HDL::DEPCTRL_NODDCLR);
    } else {
      controlsA.SetDepctrl(G9HDL::DEPCTRL_NONE);
    }
  }
}

/// \brief Field encoder for thread control field. Includes some logic that is
//         dependant on the platform.
inline void BinaryEncodingCNL::EncodeThreadControl(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS_A &controlsA) {
  if (inst->opcode() == G4_if || inst->opcode() == G4_else ||
      inst->opcode() == G4_endif) {

  }

  else {
    controlsA.SetThreadControl(
        inst->isAtomicInst() ? G9HDL::THREADCTRL_ATOMIC :
                             // CHAI: Add Switch InstOpt support
            (inst->isYieldInst()
                 ? G9HDL::THREADCTRL_SWITCH
                 : (inst->isNoPreemptInst() ? G9HDL::THREADCTRL_NOPREEMPT
                                            : G9HDL::THREADCTRL_NORMAL)));
  }
}

/// \brief Field encoder for ACC write control field
void BinaryEncodingCNL::EncodeAccWrCtrl(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS &instructionControls) {
  if (inst->isAccWrCtrlInst() ||
      (inst->isFlowControl() && inst->opcode() != G4_jmpi &&
       inst->asCFInst()->isBackward())) {
    instructionControls.SetControlsB_Accwrctrl(G9HDL::ACCWRCTRL_UPDATE_ACC);
  }
}

//////////////////////////////////////////////////////////////////////////
//// Field encoder
void BinaryEncodingCNL::EncodeInstModifier(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS &instructionControls) {
  if (inst->getSaturate()) {
    instructionControls.SetControlsB_Saturate(G9HDL::SATURATE_SAT);
  } else {
    instructionControls.SetControlsB_Saturate(
        G9HDL::SATURATE_NO_DESTINATION_MODIFICATION);
  }
}

//////////////////////////////////////////////////////////////////////////
//// Field encoder
void BinaryEncodingCNL::EncodeFlagRegPredicate(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS_A &instructionControlsA) {
  G4_Predicate *pred = inst->getPredicate();
  if (pred) {
    switch (pred->getState()) {
      // plus and undef are normal
    case PredState_Plus:
    case PredState_undef:
      instructionControlsA.SetPredinv(G9HDL::PREDINV_POSITIVE);
      // instructionHeader.SetControl_Predinv(G9HDL::PREDINV_POSITIVE);
      // flagState = PREDICATE_STATE_NORMAL;
      break;
      // minus is invert
    case PredState_Minus:
      instructionControlsA.SetPredinv(G9HDL::PREDINV_NEGATIVE);
      // instructionHeader.SetControl_Predinv(G9HDL::PREDINV_NEGATIVE);
      // flagState = PREDICATE_STATE_INVERT<<4;
      break;
    }

    G9HDL::PREDCTRL predCtrl = G9HDL::PREDCTRL_SEQUENTIAL_FLAG_CHANNEL_MAPPING;

    if (inst->isAligned16Inst()) {
      switch (getAlign16PredCtrl(pred)) {
      case PRED_ALIGN16_DEFAULT:
        predCtrl = G9HDL::PREDCTRL_SEQUENTIAL_FLAG_CHANNEL_MAPPING;
        break;
      case PRED_ALIGN16_X:
        predCtrl = G9HDL::PREDCTRL_REPLICATION_SWIZZLE_X;
        break;
      case PRED_ALIGN16_Y:
        predCtrl = G9HDL::PREDCTRL_REPLICATION_SWIZZLE_Y;
        break;
      case PRED_ALIGN16_Z:
        predCtrl = G9HDL::PREDCTRL_REPLICATION_SWIZZLE_Z;
        break;
      case PRED_ALIGN16_W:
        predCtrl = G9HDL::PREDCTRL_REPLICATION_SWIZZLE_W;
        break;
      case PRED_ALIGN16_ANY4H:
        predCtrl = G9HDL::PREDCTRL_ANY4H;
        break;
      case PRED_ALIGN16_ALL4H:
        predCtrl = G9HDL::PREDCTRL_ALL4H;
        break;
      default:
        vISA_ASSERT_UNREACHABLE("invalid align16 predicate control");
      }
      instructionControlsA.SetPredctrl(predCtrl);
      // instructionHeader.SetControl_Predctrl(predCtrl);
    } else {
      auto pc = pred->getControl();
      if (pc != PRED_DEFAULT)
        predCtrl = (G9HDL::PREDCTRL)GetAlign1PredCtrl(pc);
      instructionControlsA.SetPredctrl(predCtrl);
    }
  }
}

static const unsigned CONDITION_MODIIFER[11] = {
    (unsigned)G9HDL::CONDMODIFIER_Z,  (unsigned)G9HDL::CONDMODIFIER_E,
    (unsigned)G9HDL::CONDMODIFIER_NZ, (unsigned)G9HDL::CONDMODIFIER_NE,
    (unsigned)G9HDL::CONDMODIFIER_G,  (unsigned)G9HDL::CONDMODIFIER_GE,
    (unsigned)G9HDL::CONDMODIFIER_L,  (unsigned)G9HDL::CONDMODIFIER_LE,
    (unsigned)G9HDL::CONDMODIFIER_O, // Mod_o
    (unsigned)G9HDL::CONDMODIFIER_O, // Mod_r
    (unsigned)G9HDL::CONDMODIFIER_U  // Mod_u
};
/// \brief Field encoder
///
void BinaryEncodingCNL::EncodeCondModifier(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_CONTROLS &instructionControls) {
  G4_CondMod *cModifier = inst->getCondMod();
  if (cModifier) {
    G9HDL::CONDMODIFIER value;

    unsigned mod = (unsigned)cModifier->getMod();
    vISA_ASSERT(mod != (unsigned)Mod_r && mod < (unsigned)Mod_cond_undef,
                 // case Mod_r:
                 //     value = G9HDL::CONDMODIFIER_O; //7
                 //     break;
                 "[Verifying]:[ERR]: Invalid conditional modifier:\t");
    value = (G9HDL::CONDMODIFIER)CONDITION_MODIIFER[mod];
    instructionControls.SetCondmodifier(value);
  }
}

/// \brief Encodes instruction header DWORD, common for all types of
/// instructions
inline void
BinaryEncodingCNL::EncodeInstHeader(G4_INST *inst,
                                    G9HDL::EU_INSTRUCTION_HEADER &header) {
  G9HDL::EU_INSTRUCTION_CONTROLS &controls = header.GetControl();
  G9HDL::EU_INSTRUCTION_CONTROLS_A &controlsA = controls.GetControlsA();

  header.Init();
  EncodeOpCode(inst, header);
  EncodeExecSize(inst, controlsA);
  EncodeAccessMode(inst, controlsA);
  EncodeQtrControl(inst, controlsA);
  EncodeThreadControl(inst, controlsA);
  EncodeDepControl(inst, controlsA);
  EncodeFlagRegPredicate(inst, controlsA);
  EncodeAccWrCtrl(inst, controls);
  EncodeInstModifier(inst, controls);
  EncodeCondModifier(inst, controls);

  controls.SetControlsB_Cmptctrl(inst->isCompactedInst()
                                     ? G9HDL::CMPTCTRL_COMPACTED
                                     : G9HDL::CMPTCTRL_NOCOMPACTION);

  if (inst->isBreakPointInst())
    controls.SetControlsB_Debugctrl(G9HDL::DEBUGCTRL_BREAKPOINT);
}

////////////////////////////// DST.OPERAND_CONTROLS ///////////////
////////////////////////////// DST.OPERAND_CONTROLS ///////////////
////////////////////////////// DST.OPERAND_CONTROLS ///////////////
////////////////////////////// DST.OPERAND_CONTROLS ///////////////
////////////////////////////// DST.OPERAND_CONTROLS ///////////////
////////////////////////////// DST.OPERAND_CONTROLS ///////////////
////////////////////////////// DST.OPERAND_CONTROLS ///////////////
////////////////////////////// DST.OPERAND_CONTROLS ///////////////

//////////////////////////////////////////////////////////////////////////
//// Field encoder
inline void BinaryEncodingCNL::EncodeDstHorzStride(
    G4_INST *inst, G4_DstRegRegion *dst,
    G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS &opnds) {
  switch (dst->getHorzStride()) {
  case 1:
    // NOTE: not sure if this is correct?
    if (inst->isAligned16Inst()) {
      // NOTE: use align1 union, even if setting align16 field. Masks are equal.
      // note: Although Dst.HorzStride is a don't care for Align16, HW needs
      // this to be programmed as '01'.
      opnds.GetDestinationRegisterRegion_Align1()
          .SetDestinationHorizontalStride(G9HDL::HORZSTRIDE_4_ELEMENTS);
    } else {
      opnds.GetDestinationRegisterRegion_Align1()
          .SetDestinationHorizontalStride(G9HDL::HORZSTRIDE_1_ELEMENTS);
    }
    break;
  case 2:
    opnds.GetDestinationRegisterRegion_Align1().SetDestinationHorizontalStride(
        G9HDL::HORZSTRIDE_2_ELEMENTS);
    break;
  case 4:
    opnds.GetDestinationRegisterRegion_Align1().SetDestinationHorizontalStride(
        G9HDL::HORZSTRIDE_4_ELEMENTS);
    break;
  case UNDEFINED_SHORT:
    opnds.GetDestinationRegisterRegion_Align1().SetDestinationHorizontalStride(
        G9HDL::HORZSTRIDE_1_ELEMENTS);
    break;
  default:
    vISA_ASSERT_UNREACHABLE("wrong dst horizontal stride");
    break;
  }
}

//////////////////////////////////////////////////////////////////////////
//// Field encoder
// NOTE: could change interface, so that 'dst' is passed directly
void BinaryEncodingCNL::EncodeDstChanEn(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS &opnds) {
  G4_DstRegRegion *dst = inst->getDst();

  if (dst->isAccRegValid()) {
    // NOTE: this one is special case for instructions that use special
    // accumulators
    opnds.GetDestinationRegisterRegion_Align16().SetDestinationChannelEnable(
        dst->getAccRegSel());
  } else {
    G4_DstRegRegion *dstRegion = static_cast<G4_DstRegRegion *>(dst);

    opnds.GetDestinationRegisterRegion_Align16().SetDestinationChannelEnable(
        getWriteMask(dstRegion));
  }
}

//////////////////////////////////////////////////////////////////////////
//// Field encoder
// NOTE: could change interface, so that 'dst' is passed directly
// NOTE2: Encoding for ARF register type is moved into setting reg destination
// logic.
void BinaryEncodingCNL::EncodeDstRegFile(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS &opnds) {
  G4_DstRegRegion *dst = inst->getDst();
  switch (EncodingHelper::GetDstRegFile(
      dst)) { // Bug Line 846, bitrange: 3-4, should be: 35-36
  case REG_FILE_A:
    opnds.SetDestinationRegisterFile(G9HDL::REGFILE_ARF);
    break;
  case REG_FILE_R:
    opnds.SetDestinationRegisterFile(G9HDL::REGFILE_GRF);
    break;
  case REG_FILE_M:
    vISA_ASSERT_UNREACHABLE(0, " Memory is invalid register file on CNL");
    // opnds.SetDestinationRegisterFile(G9HDL::REGFILE_IMM); break;
  default:
    break;
  }
}

//////////////////////////////////////////////////////////////////////////
//// Field encoder
// NOTE: could change interface, so that 'dst' is passed directly
void BinaryEncodingCNL::EncodeDstRegNum(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS &opnds) {
  G4_DstRegRegion *dst = inst->getDst();

  if (EncodingHelper::GetDstRegFile(dst) != REG_FILE_A &&
      EncodingHelper::GetDstAddrMode(dst) == ADDR_MODE_IMMED) {
    uint32_t byteAddress = dst->getLinearizedStart();

    if (inst->isAligned1Inst()) {
      // register number: 256 bit (32 byte) aligned part of an address
      // sub-register number: 32 byte address (5 bits encoding) within a GRF
      //  98765 43210
      //  regn |subre

      opnds.GetDestinationRegisterRegion_Align1()
          .SetDestinationRegisterNumber_DestinationRegisterNumber(byteAddress >>
                                                                  5);
      if (dst->isAccRegValid() &&
          kernel.fg.builder->encodeAccRegSelAsAlign1()) {
        vISA_ASSERT((byteAddress & 0x1F) == 0,
                     "subreg must be 0 for dst with special accumulator");
        opnds.GetDestinationRegisterRegion_Align1().SetDestinationSpecialAcc(
            dst->getAccRegSel());
      } else {
        opnds.GetDestinationRegisterRegion_Align1()
            .SetDestinationSubregisterNumber_DestinationSubRegisterNumber(
                byteAddress & 0x1F);
      }
    } else { // align 16
      // register number: 256 bit (32 byte) aligned part of an address
      // sub-register number: first/second 16 byte part of 32 byte address.
      // Encoded with 1 bit.
      //  98765 43210
      //  regn |x0000

      // opnds.GetDestinationRegisterRegion_Align1().
      //     SetDestinationRegisterNumber_DestinationRegisterNumber(byteAddress
      //     >> 5);
      opnds.GetDestinationRegisterRegion_Align16()
          .SetDestinationRegisterNumber_DestinationRegisterNumber(byteAddress >>
                                                                  5);

      // opnds.GetDestinationRegisterRegion_Align1().
      //     SetDestinationSubregisterNumber_DestinationSubRegisterNumber((byteAddress
      //     >> 4) & 0x1);
      opnds.GetDestinationRegisterRegion_Align16()
          .SetDestinationSubregisterNumber((WORD)byteAddress);
    }
  }
}

//////////////////////////////////////////////////////////////////////////
//// Field encoder
// NOTE: could change interface, so that 'dst' is passed directly
//
//  SetDstArchRegNum (53:56) + SetDstArchRegFile (57:60)
//  SetDstArchSubRegNumByte (48:52)
//  essentially, this encoding is split in three parts:
//  setting reg num : arch reg file + arch reg number
//  setting sub-reg-num
//  Here, we fuse two original methods into one: EncodeDstArchRegNum and
//  EncodeDstRegFile for ARF
void BinaryEncodingCNL::EncodeDstArchRegNum(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS &opnds) {
  G4_DstRegRegion *dst = inst->getDst();

  if (EncodingHelper::GetDstRegFile(dst) == REG_FILE_A &&
      EncodingHelper::GetDstAddrMode(dst) == ADDR_MODE_IMMED) {
    if (EncodingHelper::GetDstArchRegType(dst) != ARCH_REG_FILE_NULL) {
      bool valid;

      unsigned short RegFile =
          (unsigned short)EncodingHelper::GetDstArchRegType(dst); // 4 bits
      unsigned short RegNumValue = dst->ExRegNum(valid);

      // 7654|3210
      // RegF|RegNumVal
      unsigned short EncodedRegNum = RegFile << 4;
      EncodedRegNum = EncodedRegNum | (RegNumValue & 0xF);

      // the same as for align16
      opnds.GetDestinationRegisterRegion_Align1()
          .SetDestinationRegisterNumber_DestinationRegisterNumber(
              EncodedRegNum);
      {
        bool subValid;
        unsigned short RegSubNumValue = dst->ExSubRegNum(subValid);
        unsigned short ElementSizeValue =
            EncodingHelper::GetElementSizeValue(dst);
        uint32_t regOffset = RegSubNumValue * ElementSizeValue;

        if (inst->isAligned1Inst()) {
          // sub-register number: 32 byte address (5 bits encoding) within a GRF
          opnds.GetDestinationRegisterRegion_Align1()
              .SetDestinationSubregisterNumber_DestinationSubRegisterNumber(
                  (WORD)regOffset);
        } else { // align 16
          // sub-register number: first/second 16 byte part of 32 byte address.
          // Encoded with 1 bit.
          //  9876543210
          //  regn|x0000
          // opnds.GetDestinationRegisterRegion_Align16().SetDestinationSubregisterNumber((regOffset
          // >> 4) & 0x1);
          opnds.GetDestinationRegisterRegion_Align16()
              .SetDestinationSubregisterNumber((WORD)regOffset);
        }
      }
    }
  }
}

//////////////////////////////////////////////////////////////////////////
//// Field encoder
// NOTE: could change interface, so that 'dst' is passed directly
void BinaryEncodingCNL::EncodeDstIndirectRegNum(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS &opnds) {
  G4_DstRegRegion *dst = inst->getDst();

  if (EncodingHelper::GetDstRegFile(dst) == REG_FILE_R ||
      EncodingHelper::GetDstRegFile(dst) == REG_FILE_M) {
    if (EncodingHelper::GetDstAddrMode(dst) == ADDR_MODE_INDIR) { // Indirect
      bool subValid;
      unsigned short IndAddrRegSubNumValue = 0;
      short IndAddrImmedValue = 0;

      IndAddrRegSubNumValue = dst->ExIndSubRegNum(subValid);
      IndAddrImmedValue = dst->ExIndImmVal();

      // the same is for align16
      opnds.GetDestinationRegisterRegion_Align1()
          .SetDestinationAddressSubregisterNumber_AddressSubregisterNumber(
              IndAddrRegSubNumValue);

      /* Set the indirect address immediate value. */
      if (inst->isAligned1Inst()) {
        // bits [0-8]
        opnds.GetDestinationRegisterRegion_Align1()
            .SetDestinationAddressImmediate(IndAddrImmedValue);
        // bit[9:9]
        opnds.SetDestinationAddressImmediate99(IndAddrImmedValue >> 9);
      } else { // here we are setting align16
        // bits [4-8]
        opnds.GetDestinationRegisterRegion_Align16()
            .SetDestinationAddressImmediate84((IndAddrImmedValue >> 4) & 0x1F);
        // bit[9:9], originally: (IndAddrImmedValue / BYTES_PER_OWORD)  >> 5
        opnds.SetDestinationAddressImmediate99(IndAddrImmedValue >> 9);
      }
    }
  }
}

/// \brief Encodes instruction operand (1st DWORD) of instruction
///
/// src0 reg file and type are not encoded here, even though they belong to
/// dword 1
inline void BinaryEncodingCNL::EncodeOperandDst(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS &opnds) {
  G4_DstRegRegion *dst = inst->getDst();

  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeFlagReg(inst,
                                                                    opnds);
  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeMaskCtrl(inst,
                                                                     opnds);

  if (dst == NULL) {
    return;
  }

  EncodeDstRegFile(inst, opnds);
  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeOperandDstType(
      inst, opnds);

  if (inst->isAligned16Inst()) {
    EncodeDstChanEn(inst, opnds);
  }
  // Note: dst doesn't have the vertical stride and width
  EncodeDstRegNum(inst, opnds);
  EncodeDstArchRegNum(inst, opnds);
  EncodeDstIndirectRegNum(inst, opnds);

  EncodeDstHorzStride(inst, dst, opnds);
  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeDstAddrMode(inst,
                                                                        opnds);
}

// EU_INSTRUCTION_BASIC_ONE_SRC
//   (EU_INSTRUCTION_SOURCES_REG
//       EU_INSTRUCTION_OPERAND_SRC_REG_ALIGN1
//           or
//       EU_INSTRUCTION_OPERAND_SRC_REG_ALIGN16)
//     or
//   (EU_INSTRUCTION_SOURCES_IMM32

inline void BinaryEncodingCNL::EncodeOneSrcInst(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_BASIC_ONE_SRC &oneSrc) {
  EncodeInstHeader(inst, oneSrc.Common.Header);
  EncodeOperandDst(inst, oneSrc.Common.OperandControls);
  G4_Operand *src0 = inst->getSrc(0);

  // EncodeSrc0RegFile
  oneSrc.GetOperandControls().SetSrc0Regfile(
      TranslateVisaToHDLRegFile(EncodingHelper::GetSrcRegFile(src0)));

  // EncodeSrc0Type
  if (src0->isImm()) {
    oneSrc.GetOperandControls().SetSrc0Srctype_Imm(
        GetOperandSrcHDLImmType(src0->getType()));
  } else {
    oneSrc.GetOperandControls().SetSrc0Srctype(
        GetOperandSrcHDLType(src0->getType()));
  }

  if (src0->isImm()) {
    // this should be compiled as dead code in non-debug mode
    vISA_ASSERT(inst->opcode() == G4_mov || src0->getTypeSize() != 8,
                 "only Mov is allowed for 64-bit immediate");
    if (src0->getTypeSize() == 8) {
      G9HDL::EU_INSTRUCTION_IMM64_SRC *ptr =
          (G9HDL::EU_INSTRUCTION_IMM64_SRC *)&oneSrc;
      EncodeSrcImm64Data(*ptr, src0);
    } else {
      // EncodeSrcImmData(oneSrc.GetImmsource(), src0);
      SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_IMM32, 0>::EncodeSrcImmData(
          oneSrc.GetImmsource(), src0);
    }
  } else {
    SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG,
               0>::EncodeEuInstructionSourcesReg(inst, src0,
                                                 oneSrc.GetRegsource(),
                                                 *this); // by reference
  }
}

inline void BinaryEncodingCNL::EncodeTwoSrcInst(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC &twoSrc) {

  EncodeInstHeader(inst, twoSrc.Common.Header);
  EncodeOperandDst(inst, twoSrc.Common.OperandControls);
  G4_Operand *src0 = inst->getSrc(0);
  G4_Operand *src1 = inst->getSrc(1);

  // EncodeSrc0RegFile
  twoSrc.GetOperandControls().SetSrc0Regfile(
      TranslateVisaToHDLRegFile(EncodingHelper::GetSrcRegFile(src0)));

  [[maybe_unused]]
  bool src0ImmOk = inst->isMath() && inst->asMathInst()->isOneSrcMath();
  vISA_ASSERT(src0ImmOk || !src0->isImm(),
               "src0 is immediate in two src instruction!");
  // EncodeSrc0Type
  if (src0->isImm()) {
    twoSrc.GetOperandControls().SetSrc0Srctype_Imm(
        GetOperandSrcHDLImmType(src0->getType()));
  } else {
    if (inst->isSend()) {
      twoSrc.GetOperandControls().SetSrc0Srctype(GetOperandSrcHDLType(Type_F));
    } else {
      twoSrc.GetOperandControls().SetSrc0Srctype(
          GetOperandSrcHDLType(src0->getType()));
    }
  }

  if (src0->isImm()) {
    vISA_ASSERT(src0->getTypeSize() < 8,
                 "only Mov is allowed for 64bit immediate");
    // FIXME: feels like this should be 0 here, but it gives a type mismatch as
    // the headers assume src0 must be REG
    SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG_IMM, 1>::EncodeSrcImmData(
        twoSrc.GetImmsource(), src0);
  } else {
    SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG_REG,
               0>::EncodeEuInstructionSourcesReg(inst, src0,
                                                 twoSrc.GetRegsource(),
                                                 *this); // by reference
  }

  // no need to encode one src math instruction
  if (inst->isMath() && src1->isNullReg() && !src0->isImm()) {
    SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG_REG,
               1>::EncodeEuInstructionNullSourcesReg(inst, src1,
                                                     twoSrc.GetRegsource());
    return;
  }

  twoSrc.GetRegsource().SetSrc1Regfile(
      TranslateVisaToHDLRegFile(EncodingHelper::GetSrcRegFile(src1)));
  if (src1->isImm()) {
    twoSrc.GetImmsource().SetSrc1Srctype(
        GetOperandSrcHDLImmType(src1->getType()));
  }
  // adding to fix above no need to encode type if src0 is immediate and src1 is
  // null reg
  else if (!(inst->isMath() && src1->isNullReg() && src0->isImm())) {
    twoSrc.GetRegsource().SetSrc1Srctype(GetOperandSrcHDLType(src1->getType()));
  }

  if (src1->isImm()) {
    vISA_ASSERT(inst->opcode() == G4_mov || src1->getTypeSize() != 8,
                 "only Mov is allowed for 64bit immediate");

    SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG_IMM, 1>::EncodeSrcImmData(
        twoSrc.GetImmsource(), src1);
  } else {
    if (src0->isImm()) {
      // src1 must be null, and don't encode anything so it won't overwrite
      // src0's imm value
      vISA_ASSERT(src1->isNullReg(),
                   "src1 must be null ARF if src0 is immediate");
    } else {
      SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG_REG,
                 1>::EncodeEuInstructionSourcesReg(inst, src1,
                                                   twoSrc.GetRegsource(),
                                                   *this); // by reference
    }
  }
}

/// \brief Given a two-src mask, apply a patch for send instruction
///
void PatchSend(G4_INST *inst, G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *twoSrc) {
  vISA_ASSERT(inst->getMsgDescRaw(), "expected raw descriptor");
  uint32_t msgDesc = inst->getMsgDescRaw()->getExtendedDesc();
  EncExtMsgDescriptor emd;
  emd.ulData = msgDesc;

  G9HDL::EU_INSTRUCTION_SEND *sendInstruction =
      (G9HDL::EU_INSTRUCTION_SEND *)twoSrc;
  G9HDL::SFID sfid = (G9HDL::SFID)emd.ExtMsgDescriptor.TargetUnitId;

  sendInstruction->SetSharedFunctionIdSfid(sfid);
  sendInstruction->SetExdesc1111(msgDesc);

  // this is a hack, but:
  // fixme: this is missing in auto-header, currently need to override the acc
  // write field
  if (inst->isNoSrcDepSet()) {
    // equivalent to: mybin->SetBits(bitsNoSrcDepSet_0, bitsNoSrcDepSet_1, 1);
    sendInstruction->SetControlsB_Accwrctrl(G9HDL::ACCWRCTRL_UPDATE_ACC); // 0x1
  }

  G9HDL::EOT eot = (G9HDL::EOT)emd.ExtMsgDescriptor.EndOfThread;
  sendInstruction->GetMessage().SetEot(eot);
}

/// \brief EncodeMathControl
///
void PatchMath(G4_INST *inst, G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *twoSrc) {
  vISA_ASSERT(inst->isMath(), "PatchMath must be called on math instruction.");

  unsigned int MathControlValue = inst->asMathInst()->getMathCtrl();
  unsigned MathFunction = MathControlValue & 0xf;

  G9HDL::EU_INSTRUCTION_MATH *mathInstruction =
      (G9HDL::EU_INSTRUCTION_MATH *)twoSrc;
  G9HDL::FC mathFunctionControl = (G9HDL::FC)MathFunction;
  mathInstruction->SetFunctionControlFc(mathFunctionControl);

  // fixme: what about partial precision bit?
  // if (!mybin->GetIs3Src())     {
  //     mybin->SetBits(bitsMathFunction_0, bitsMathFunction_1, MathFunction);
  //     mybin->SetBits(bitsMathPartPrec_0, bitsMathPartPrec_1, MathPartPrec);
  // }
}

inline G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::DESTINATION_DATA_TYPE
Get3SrcLimitedType(G4_Type type) {
  switch (type) {
  case Type_F:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::DESTINATION_DATA_TYPE_F;
  case Type_D:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::DESTINATION_DATA_TYPE_D;
  case Type_UD:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::DESTINATION_DATA_TYPE_UD;
  case Type_DF:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::DESTINATION_DATA_TYPE_DF;
  case Type_HF:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::DESTINATION_DATA_TYPE_HF;
  default:
    break;
  }
  // default
  return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::DESTINATION_DATA_TYPE_F;
}

inline static G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::SOURCE_DATA_TYPE
Get3SrcLimitedSrcType(G4_Type type) {
  switch (type) {
  case Type_F:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::SOURCE_DATA_TYPE_F;
  case Type_D:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::SOURCE_DATA_TYPE_D;
  case Type_UD:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::SOURCE_DATA_TYPE_UD;
  case Type_DF:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::SOURCE_DATA_TYPE_DF;
  case Type_HF:
    return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::SOURCE_DATA_TYPE_HF;
  default:
    break;
  }
  // default
  return G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC::SOURCE_DATA_TYPE_F;
}

/// Gets align1 ternary instruction limited type, given execution type
/// (integer/float)
inline static G9HDL::TERNARYALIGN1DATATYPE Get3SrcAlign1LimitedSrcType(
    G4_Type type, G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::EXECUTION_DATATYPE
                      isFloatExecutionType) {
  if (isFloatExecutionType ==
      G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::EXECUTION_DATATYPE_FLOAT) {
    switch (type) {
    case Type_F:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_F;
    case Type_DF:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_DF;
    case Type_HF:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_HF;
    case Type_NF:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_NF;
    default:
      vISA_ASSERT_UNREACHABLE("wrong type for align1 ternary instruction with float "
                      "execution type.");
      break;
    }

    // Some reasonable default:
    return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_F;
  } else {
    switch (type) {
    case Type_UD:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_UD;
    case Type_D:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_D;
    case Type_UW:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_UW;
    case Type_W:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_W;
    case Type_UB:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_UB;
    case Type_B:
      return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_B;
    default:
      vISA_ASSERT_UNREACHABLE("wrong type for align1 ternary instruction with integer "
                      "execution type.");
      break;
    }
    // Some reasonable default:
    return G9HDL::TERNARYALIGN1DATATYPE::TERNARYALIGN1DATATYPE_UD;
  }
}

inline void BinaryEncodingCNL::EncodeThreeSrcInst(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC &threeSrc) {
  G4_Operand *src0 = inst->getSrc(0);
  G4_Operand *src1 = inst->getSrc(1);
  G4_Operand *src2 = inst->getSrc(2);
  G4_Operand *dst = inst->getDst();

  EncodeInstHeader(inst, threeSrc.Common.Header);

  // threeSrc doesn't have OperandControls field, need to implement its dst
  // encoding separately

  DstBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC>::EncodeFlagReg(inst,
                                                                   threeSrc);
  DstBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC>::EncodeMaskCtrl(inst,
                                                                    threeSrc);
  {
    threeSrc.SetDestinationDataType(Get3SrcLimitedType(dst->getType()));
    threeSrc.SetSourceDataType(Get3SrcLimitedSrcType(src0->getType()));
    if (src1->getType() == Type_HF) {
      threeSrc.SetSource1Type(1);
    }
    if (src2->getType() == Type_HF) {
      threeSrc.SetSource2Type(1);
    }

    G4_DstRegRegion *dst = inst->getDst();

    // this is for 'special accumulators', encoded with channel enable..
    if (dst->isAccRegValid()) {
      threeSrc.SetDestinationChannelEnable(dst->getAccRegSel());
      // NOTE: this one is special case for instructions that use special
      // accumulators
    } else {
      G4_DstRegRegion *dstRegion = static_cast<G4_DstRegRegion *>(dst);
      threeSrc.SetDestinationChannelEnable(getWriteMask(dstRegion));
    }

    if (EncodingHelper::GetDstRegFile(dst) != REG_FILE_A &&
        EncodingHelper::GetDstAddrMode(dst) == ADDR_MODE_IMMED) {
      uint32_t byteAddress = dst->getLinearizedStart();

      threeSrc.SetDestinationRegisterNumber_DestinationRegisterNumber(
          byteAddress >> 5);
      // must be DWORD aligned
      // 3 bits for subregnum
      threeSrc.SetDestinationSubregisterNumber((byteAddress >> 2) & 0x7);
      vISA_ASSERT(inst->isAligned16Inst(), "3src only support align16 mode");
    }

    // src0
    {
      // EncodeSrc0RepCtrl
      G4_SrcRegRegion *src0Region = src0->asSrcRegRegion();
      G4_SrcRegRegion *src1Region = src1->asSrcRegRegion();
      G4_SrcRegRegion *src2Region = src2->asSrcRegRegion();

      // source modifiers:
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 0>::EncodeSrcModifier(
          inst, src0, threeSrc);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 1>::EncodeSrcModifier(
          inst, src1, threeSrc);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 2>::EncodeSrcModifier(
          inst, src2, threeSrc);

      // rep control:
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC,
                 0>::Encode3SrcReplicateControl(&threeSrc, src0Region, *this);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC,
                 1>::Encode3SrcReplicateControl(&threeSrc, src1Region, *this);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC,
                 2>::Encode3SrcReplicateControl(&threeSrc, src2Region, *this);

      // chan select:
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 0>::EncodeSrcChanSelect(
          &threeSrc, inst, src0, src0Region, *this);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 1>::EncodeSrcChanSelect(
          &threeSrc, inst, src1, src1Region, *this);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 2>::EncodeSrcChanSelect(
          &threeSrc, inst, src2, src2Region, *this);

      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 0>::EncodeSrcRegNum3Src(
          inst, src0, threeSrc);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 1>::EncodeSrcRegNum3Src(
          inst, src1, threeSrc);
      SrcBuilder<G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC, 2>::EncodeSrcRegNum3Src(
          inst, src2, threeSrc);
      // register
      // sub-register
    }
  }
}

inline void BinaryEncodingCNL::EncodeThreeSrcInstAlign1(
    G4_INST *inst, G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC &threeSrc) {
  G4_Operand *src0 = inst->getSrc(0);
  G4_Operand *src1 = inst->getSrc(1);
  G4_Operand *src2 = inst->getSrc(2);
  G4_DstRegRegion *dst = inst->getDst();

  // Common instruction fields:
  EncodeInstHeader(inst, threeSrc.TheStructure.Common.Header);
  DstBuilder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC>::EncodeFlagReg(inst,
                                                                    threeSrc);
  DstBuilder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC>::EncodeMaskCtrl(inst,
                                                                     threeSrc);

  vISA_ASSERT(
      (IS_TYPE_FLOAT_ALL(src0->getType()) &&
       IS_TYPE_FLOAT_ALL(src1->getType()) &&
       IS_TYPE_FLOAT_ALL(src2->getType()) &&
       IS_TYPE_FLOAT_ALL(dst->getType())) ||
          (IS_TYPE_INT(src0->getType()) && IS_TYPE_INT(src1->getType()) &&
           IS_TYPE_INT(src2->getType()) && IS_TYPE_INT(dst->getType())),
      "No mixed mode in align1 ternary encoding.");

  G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::EXECUTION_DATATYPE execType =
      IS_TYPE_FLOAT_ALL(dst->getType())
          ? G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::EXECUTION_DATATYPE_FLOAT
          : G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::EXECUTION_DATATYPE_INTEGER;

  threeSrc.SetExecutionDatatype(execType);

  // DST REGFILE
  switch (EncodingHelper::GetDstRegFile(dst)) {
  case REG_FILE_R:
    threeSrc.SetDestinationRegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::DESTINATION_REGISTER_FILE_GRF);
    break;
  case REG_FILE_A:
    threeSrc.SetDestinationRegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::DESTINATION_REGISTER_FILE_ARF);
    break;
  default:
    vISA_ASSERT_UNREACHABLE("Invalid dst register file for for align1 ternary "
                    "instruction (expected grf or arf).");
    break;
  }

  // SRC modifiers:
  if (src0->isSrcRegRegion()) {
    threeSrc.SetSource0Modifier(
        BinaryEncodingCNL::GetSrcHLDMod(src0->asSrcRegRegion()));
  } else {
    threeSrc.SetSource0Modifier(G9HDL::SRCMOD_NO_MODIFICATION);
  }

  if (src1->isSrcRegRegion()) {
    threeSrc.SetSource1Modifier(
        BinaryEncodingCNL::GetSrcHLDMod(src1->asSrcRegRegion()));
  } else {
    threeSrc.SetSource1Modifier(G9HDL::SRCMOD_NO_MODIFICATION);
  }
  if (src2->isSrcRegRegion()) {
    threeSrc.SetSource2Modifier(
        BinaryEncodingCNL::GetSrcHLDMod(src2->asSrcRegRegion()));
  } else {
    threeSrc.SetSource2Modifier(G9HDL::SRCMOD_NO_MODIFICATION);
  }

  // SRC0 regfile
  switch (EncodingHelper::GetSrcRegFile(src0)) {
  case REG_FILE_R:
    threeSrc.SetSource0RegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::SOURCE_0_REGISTER_FILE_GRF);
    break;
  case REG_FILE_I:
    threeSrc.SetSource0RegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::SOURCE_0_REGISTER_FILE_IMM);
    break;
  case REG_FILE_A:
    vISA_ASSERT(src0->getType() == Type_NF,
                 "Invalid register file for src0 in align1 ternary instruction "
                 "(expected grf or imm).");
    threeSrc.SetSource0RegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::SOURCE_0_REGISTER_FILE_ARF);
    break;
  default:
    break;
  }

  // SRC1 regfile
  switch (EncodingHelper::GetSrcRegFile(src1)) {
  case REG_FILE_R:
    threeSrc.SetSource1RegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::SOURCE_1_REGISTER_FILE_GRF);
    break;
  case REG_FILE_A:
    threeSrc.SetSource1RegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::SOURCE_1_REGISTER_FILE_ARF);
    break;
  default:
    vISA_ASSERT_UNREACHABLE("Invalid register file for src1 in align1 ternary "
                    "instruction(expected grf or arf).");
    break;
  }

  // SRC2 regfile
  switch (EncodingHelper::GetSrcRegFile(src2)) {
  case REG_FILE_R:
    threeSrc.SetSource2RegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::SOURCE_2_REGISTER_FILE_GRF);
    break;
  case REG_FILE_I:
    threeSrc.SetSource2RegisterFile(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::SOURCE_2_REGISTER_FILE_IMM);
    break;
  default:
    vISA_ASSERT_UNREACHABLE("Invalid register file for src2 in align1 ternary "
                    "instruction(expected grf or imm).");
    break;
  }

  threeSrc.SetDestinationDatatype(
      Get3SrcAlign1LimitedSrcType(dst->getType(), execType));

  // Dst reg/subreg
  switch (EncodingHelper::GetDstRegFile(dst)) {
  case REG_FILE_R: {
    uint32_t byteAddress = dst->getLinearizedStart();
    if (kernel.fg.builder->encodeAccRegSelAsAlign1() && dst->isAccRegValid()) {
      // special accumulator region (acc2-acc9) is encoded as part of subreg
      vISA_ASSERT((byteAddress & 0x1F) == 0,
                   "subreg must be 0 for dst with special accumulator");
      threeSrc.SetDestinationSpecialAcc(dst->getAccRegSel());
    } else {
      threeSrc.SetDestinationSubregisterNumber(byteAddress & 0x1f);
    }
    threeSrc.SetDestinationRegisterNumber(byteAddress >> 5);
    break;
  }
  case REG_FILE_A: {
    bool valid;
    unsigned short RegFile =
        (unsigned short)EncodingHelper::GetDstArchRegType(dst); // 4 bits
    unsigned short RegNumValue = dst->ExRegNum(valid);

    // 7654|3210
    // RegF|RegNumVal
    unsigned short EncodedRegNum = RegFile << 4;
    EncodedRegNum = EncodedRegNum | (RegNumValue & 0xF);
    threeSrc.SetDestinationRegisterNumber(EncodedRegNum);
    break;
  }
  default:
    vISA_ASSERT_UNREACHABLE("Invalid register file for dst in align1 ternary "
                    "instruction(expected grf or acc).");
    break;
  }

  switch (dst->getHorzStride()) {
  case 1:
    threeSrc.SetDestinationHorizontalStride(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::
            DESTINATION_HORIZONTAL_STRIDE_1_ELEMENT);
    break;
  case 2:
    threeSrc.SetDestinationHorizontalStride(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::
            DESTINATION_HORIZONTAL_STRIDE_2_ELEMENT);
    break;
  case UNDEFINED_SHORT:
    vISA_ASSERT_UNREACHABLE(
        "Dst horizontal stride for align1 ternary instruction is undefined.");
    threeSrc.SetDestinationHorizontalStride(
        G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC::
            DESTINATION_HORIZONTAL_STRIDE_1_ELEMENT);
    break;
  default:
    vISA_ASSERT_UNREACHABLE("Wrong dst horizontal stride for align1 ternary "
                        "instruction (is neither 1 nor 2).");
    break;
  }

  // SRC0 fields
  //////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////
  threeSrc.SetSource0Datatype(
      Get3SrcAlign1LimitedSrcType(src0->getType(), execType));

  if (src0->isSrcRegRegion()) {
    const RegionDesc *regdesc0 = src0->asSrcRegRegion()->getRegion();
    vISA_ASSERT(regdesc0, "Align1 ternary encoder: src0 region desc for "
                           "ternary instruction is null!");

    // look for <N;N,1> contiguous region
    if (regdesc0->vertStride > 8) {
      // TODO: should we care about this case?
      vISA_ASSERT(
          0, "Align1 3 src instruction with vertStride>8 not supported yet.");
    } else {
      // src0: vstride
      switch (regdesc0->vertStride) {
      case 0:
        SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 0>::
            SetSourceVerticalStride(&threeSrc,
                                    G9HDL::TERNARYALIGN1VERTSTRIDE_0_ELEMENTS);
        break;
      case 2:
        SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 0>::
            SetSourceVerticalStride(&threeSrc,
                                    G9HDL::TERNARYALIGN1VERTSTRIDE_2_ELEMENTS);
        break;
      case 4:
        SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 0>::
            SetSourceVerticalStride(&threeSrc,
                                    G9HDL::TERNARYALIGN1VERTSTRIDE_4_ELEMENTS);
        break;
      case 8:
        SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 0>::
            SetSourceVerticalStride(&threeSrc,
                                    G9HDL::TERNARYALIGN1VERTSTRIDE_8_ELEMENTS);
        break;
      default:
        vISA_ASSERT_UNREACHABLE(
            "wrong vertical stride for ternary align1 instruction at src0!");
        break;
      }

      // src0: hstride
      SrcBuilder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC,
                 0>::EncodeSrcHorzStride(inst, &threeSrc, regdesc0, src0);
    }

    vISA_ASSERT(EncodingHelper::GetSrcRegFile(src0) != REG_FILE_I,
                 "Align1 ternary: src0 register file must not be immediate if "
                 "src region is present. ");
    if (EncodingHelper::GetSrcRegFile(src0) != REG_FILE_A) {
      uint32_t byteAddress = src0->getLinearizedStart();
      threeSrc.SetSource0RegisterNumber_SourceRegisterNumber(byteAddress >> 5);
      if (kernel.fg.builder->encodeAccRegSelAsAlign1() &&
          src0->isAccRegValid()) {
        vISA_ASSERT((byteAddress & 0x1F) == 0,
                     "subreg must be 0 for source with special accumualators");
        threeSrc.SetSource0SpecialAcc(src0->getAccRegSel());
      } else {
        threeSrc.SetSource0SubregisterNumber_SourceSubRegisterNumber(
            byteAddress & 0x1f);
      }
    } else {
      vISA_ASSERT(src0->getType() == Type_NF,
                   "only NF type src0 can be accumulator");
      // encode acc
      bool valid;
      unsigned short RegFile =
          (unsigned short)EncodingHelper::GetSrcArchRegType(src0); // 4 bits
      unsigned short RegNumValue = src0->asSrcRegRegion()->ExRegNum(valid);

      // 7654|3210
      // RegF|RegNumVal
      unsigned short EncodedRegNum = RegFile << 4;
      EncodedRegNum = EncodedRegNum | (RegNumValue & 0xF);
      threeSrc.SetSource0RegisterNumber_SourceRegisterNumber(EncodedRegNum);
    }
  } else {
    vISA_ASSERT(EncodingHelper::GetSrcRegFile(src0) == REG_FILE_I,
                 "Align1 ternary: src0 reg file must be immediate if operand "
                 "is immediate.");

    G4_Imm *isrc = (G4_Imm *)src0->asImm();
    threeSrc.SetSource0ImmediateValue((uint32_t)isrc->getImm());
  }

  // SRC1 fields
  //////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////
  threeSrc.SetSource1Datatype(
      Get3SrcAlign1LimitedSrcType(src1->getType(), execType));

  vISA_ASSERT(EncodingHelper::GetSrcRegFile(src1) != REG_FILE_I,
               "Align1 ternary:src1 immediate register file not supported by "
               "definition.");
  if (EncodingHelper::GetSrcRegFile(src1) != REG_FILE_A) {
    uint32_t byteAddress = src1->getLinearizedStart();
    threeSrc.SetSource1RegisterNumber_SourceRegisterNumber(byteAddress >> 5);
    if (kernel.fg.builder->encodeAccRegSelAsAlign1() && src1->isAccRegValid()) {
      vISA_ASSERT((byteAddress & 0x1F) == 0,
                   "subreg must be 0 for source with special accumualators");
      threeSrc.SetSource1SpecialAcc(src1->getAccRegSel());
    } else {
      threeSrc.SetSource1SubregisterNumber_SourceSubRegisterNumber(byteAddress &
                                                                   0x1f);
    }
  } else {
    // encode acc
    bool valid;
    unsigned short RegFile =
        (unsigned short)EncodingHelper::GetSrcArchRegType(src1); // 4 bits
    unsigned short RegNumValue = src1->asSrcRegRegion()->ExRegNum(valid);

    // 7654|3210
    // RegF|RegNumVal
    unsigned short EncodedRegNum = RegFile << 4;
    EncodedRegNum = EncodedRegNum | (RegNumValue & 0xF);
    threeSrc.SetSource1RegisterNumber_SourceRegisterNumber(EncodedRegNum);
  }

  const RegionDesc *regdesc1 = src1->asSrcRegRegion()->getRegion();
  vISA_ASSERT(regdesc1, "Align1 ternary encoder: src1 region desc for ternary "
                         "instruction is null!");

  // look for <N;N,1> contiguous region
  if (regdesc1->vertStride > 8) {
    // TODO: should we care about this case?
    vISA_ASSERT(
        0, "align1 3 src instruction with vertStride>8 not supported yet.");
  } else {
    SrcBuilder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 1>::EncodeSrcHorzStride(
        inst, &threeSrc, regdesc1, src1);

    // src1: vstride
    switch (regdesc1->vertStride) {
    case 0:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 1>::
          SetSourceVerticalStride(&threeSrc,
                                  G9HDL::TERNARYALIGN1VERTSTRIDE_0_ELEMENTS);
      break;
    case 2:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 1>::
          SetSourceVerticalStride(&threeSrc,
                                  G9HDL::TERNARYALIGN1VERTSTRIDE_2_ELEMENTS);
      break;
    case 4:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 1>::
          SetSourceVerticalStride(&threeSrc,
                                  G9HDL::TERNARYALIGN1VERTSTRIDE_4_ELEMENTS);
      break;
    case 8:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 1>::
          SetSourceVerticalStride(&threeSrc,
                                  G9HDL::TERNARYALIGN1VERTSTRIDE_8_ELEMENTS);
      break;
    default:
      vISA_ASSERT_UNREACHABLE(
          "wrong vertical stride for ternary align1 instruction at src0!");
      break;
    }
  }

  // SRC2 fields
  //////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////
  threeSrc.SetSource2Datatype(
      Get3SrcAlign1LimitedSrcType(src2->getType(), execType));

  if (src2->isSrcRegRegion()) {
    const RegionDesc *regdesc2 = src2->asSrcRegRegion()->getRegion();
    vISA_ASSERT(regdesc2, "Align1 ternary instruction: src2 region desc for "
                           "instruction is null!");
    vISA_ASSERT(EncodingHelper::GetSrcRegFile(src2) != REG_FILE_I,
                 "Align1 ternary encoder: src2 reg file is immediate even if "
                 "src region present.");

    // src2 reg/subreg

    if (EncodingHelper::GetSrcRegFile(src2) != REG_FILE_A) {
      uint32_t byteAddress = src2->getLinearizedStart();
      threeSrc.SetSource2RegisterNumber_SourceRegisterNumber(byteAddress >> 5);
      if (kernel.fg.builder->encodeAccRegSelAsAlign1() &&
          src2->isAccRegValid()) {
        vISA_ASSERT((byteAddress & 0x1F) == 0,
                     "subreg must be 0 for source with special accumualators");
        threeSrc.SetSource2SpecialAcc(src2->getAccRegSel());
      } else {
        threeSrc.SetSource2SubregisterNumber_SourceSubRegisterNumber(
            byteAddress & 0x1f);
      }
    }

    switch (regdesc2->horzStride) {
    case 0:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 2>::
          SetSourceHorizontalStride(&threeSrc, G9HDL::HORZSTRIDE_0_ELEMENTS);
      break;
    case 1:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 2>::
          SetSourceHorizontalStride(&threeSrc, G9HDL::HORZSTRIDE_1_ELEMENTS);
      break;
    case 2:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 2>::
          SetSourceHorizontalStride(&threeSrc, G9HDL::HORZSTRIDE_2_ELEMENTS);
      break;
    case 4:
      SrcOperandEncoder<G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC, 2>::
          SetSourceHorizontalStride(&threeSrc, G9HDL::HORZSTRIDE_4_ELEMENTS);
      break;
    default:
      vISA_ASSERT_UNREACHABLE("Align1 ternary: src2 has non-encodable horizontal "
                          "stride (accepted: 0,1,2,4).");
      break;
    }
  } else {
    vISA_ASSERT(EncodingHelper::GetSrcRegFile(src2) == REG_FILE_I,
                 "Align1 ternary: src2 reg file must be immediate if operand "
                 "is immediate.");

    G4_Imm *isrc = (G4_Imm *)src2->asImm();
    threeSrc.SetSource2ImmediateValue((uint32_t)isrc->getImm());
  }
}

/// \brief Get compaction control bit from already encoded binary instruction
///
uint32_t BinaryEncodingCNL::GetCompactCtrl(BinInst *mybin) {
  G9HDL::EU_INSTRUCTION_HEADER *ptr =
      (G9HDL::EU_INSTRUCTION_HEADER *)&(mybin->DWords);
  return (uint32_t)ptr->GetControl().GetControlsB_Cmptctrl();
}

/// \brief Set compaction control bit. TODO: how to write to mybin->DWords.
/// below doesn't work
///
void BinaryEncodingCNL::SetCompactCtrl(BinInst *mybin, uint32_t value) {
  G9HDL::EU_INSTRUCTION_HEADER *ptr =
      (G9HDL::EU_INSTRUCTION_HEADER *)&(mybin->DWords);
  ptr->GetControl().SetControlsB_Cmptctrl(G9HDL::CMPTCTRL_COMPACTED);
}

/// \brief Encode JIP and(or) UIP at their respective places
///
/// Comments are left from original binary encoder
/// TODO: move as a class method
void BinaryEncodingCNL::SetBranchOffsets(G4_INST *inst, uint32_t JIP,
                                         uint32_t UIP) {
  BinInst *mybin = getBinInst(inst);
  G4_opcode opc = inst->opcode();

  {
    if (opc == G4_if || opc == G4_break || opc == G4_cont || opc == G4_halt ||
        opc == G4_goto || opc == G4_else) {
      // cast binary as branch two src
      // note: if an instruction has both JIP and UIP, then
      // only src0 type and regfile are being set (higher dword is occupied)

      G9HDL::EU_INSTRUCTION_BRANCH_TWO_SRC *twoSrc =
          (G9HDL::EU_INSTRUCTION_BRANCH_TWO_SRC *)mybin->DWords;

      twoSrc->GetOperandControl().SetSrc0Regfile(G9HDL::REGFILE_IMM);
      twoSrc->GetOperandControl().SetSrc0Srctype_Imm(
          GetOperandSrcHDLImmType(Type_D));
      twoSrc->SetJip(JIP);
      twoSrc->SetUip(UIP);
      // SetBranchJIPUIP(mybin, JIP, UIP);
    } else {
      G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *oneSrc =
          (G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *)mybin->DWords;

      oneSrc->SetSrc1Regfile(G9HDL::REGFILE_IMM);
      oneSrc->SetSrc1Srctype(GetOperandSrcHDLImmType(Type_D));
      oneSrc->SetJip(JIP);
      // SetBranchJIP(mybin, JIP);
    }
  }
}

/// \brief encodes JIP and(or) UIP offsets
///
/// This is done in this separate method, and not during the main
/// encoding phase, since the real jump offsets are visible only
/// now.
bool BinaryEncodingCNL::EncodeConditionalBranches(G4_INST *inst,
                                                  uint32_t insOffset) {
  std::string jipLabel;
  std::string uipLabel;
  int32_t jipOffset = 0;
  int32_t uipOffset = 0;
  G4_opcode op = inst->opcode();

  // while and case only have JIP for all platforms
  // break, cont and halt have both JIP and UIP for all platforms
  if (op == G4_if || op == G4_else || op == G4_endif || op == G4_while ||
      op == G4_break || op == G4_cont || op == G4_halt || op == G4_goto ||
      op == G4_join) {
    G4_Label *jip = inst->asCFInst()->getJip();
    if (jip) {
      int32_t info = GetLabelInfo(jip);
      if (info == -1) {
        return false;
      }
      jipOffset = info - insOffset;
      jipOffset *= (int32_t)JUMP_INST_COUNT_SIZE;
    } else if (op == G4_while || op == G4_endif || op == G4_join) {
      {
        BinInst *mybin = getBinInst(inst);
        G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *oneSrc =
            (G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *)mybin->DWords;

        oneSrc->SetSrc1Regfile(G9HDL::REGFILE_IMM);
        oneSrc->SetSrc1Srctype(GetOperandSrcHDLImmType(Type_D));
      }
    }
  }

  // halt has both JIP and UIP on all platforms
  // else has JIP for all platforms; else has UIP for BDW only
  if (op == G4_break || op == G4_cont || op == G4_halt || op == G4_if ||
      op == G4_else || op == G4_goto) {
    G4_Label *uip = inst->asCFInst()->getUip();
    if (uip) {
      int32_t info = GetLabelInfo(uip);
      if (info == -1) {
        return false;
      }
      uipOffset = info - insOffset;
      uipOffset *= (uint32_t)JUMP_INST_COUNT_SIZE;
    }
  }

  if (op == G4_endif && jipOffset == 0) {
    jipOffset = INST_SIZE;
  }

  if (jipOffset != 0 || uipOffset != 0) {
    SetBranchOffsets(inst, jipOffset, uipOffset);
  }

  if (op == G4_jmpi && inst->getSrc(0) && inst->getSrc(0)->isLabel()) {
    // find the label's IP count
    G4_Operand *opnd = inst->getSrc(0);
    BinInst *mybin = getBinInst(inst);
    // Calculate the address offset
    // Label has the same IP count as the following instruction,
    // "break 1" is to the fall through instruction
    int32_t info = GetLabelInfo(opnd->asLabel());
    if (info == -1) {
      return false;
    }
    int32_t jmpOffset = info - insOffset;
    if (GetCompactCtrl(mybin))
      jmpOffset -= 1;
    else
      jmpOffset -= 2;

    jmpOffset *= (int32_t)JUMP_INST_COUNT_SIZE;

    // FIXME: add compaction support
    // if (GetCompactCtrl(mybin))
    //{
    //     SetCmpSrc1RegNum(mybin, jmpOffset & 0xff);          // 63:56
    //     SetCmpSrc1Index(mybin, (jmpOffset >> 8)& 0x1f);
    // }
    // else
    {
      BinInst *mybin = getBinInst(inst);
      G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *oneSrc =
          (G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *)mybin->DWords;

      oneSrc->SetSrc1Regfile(G9HDL::REGFILE_IMM);
      oneSrc->SetSrc1Srctype(GetOperandSrcHDLImmType(Type_D));
      oneSrc->SetJip((uint32_t)jmpOffset);
    }
  }

  if (op == G4_call && inst->getSrc(0) && inst->getSrc(0)->isLabel()) {
    G4_Operand *opnd = inst->getSrc(0);
    int32_t info = GetLabelInfo(opnd->asLabel());
    if (info == -1) {
      return false;
    }

    int32_t jmpOffset = info - insOffset;

    { jmpOffset *= (int32_t)JUMP_INST_COUNT_SIZE; }

    BinInst *mybin = getBinInst(inst);
    G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *oneSrc =
        (G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC *)mybin->DWords;

    oneSrc->SetSource0_SourceVerticalStride(G9HDL::VERTSTRIDE_2_ELEMENTS);
    oneSrc->SetSource0_SourceWidth(G9HDL::WIDTH_4_ELEMENTS);
    oneSrc->SetSource0_SourceHorizontalStride(G9HDL::HORZSTRIDE_1_ELEMENTS);

    oneSrc->SetSrc1Regfile(G9HDL::REGFILE_IMM);
    oneSrc->SetSrc1Srctype(GetOperandSrcHDLImmType(Type_D));
    oneSrc->SetJip((uint32_t)jmpOffset);

    // TODO: do not forget about compacted variant
  }
  return true;
}

/// \brief initializes auto-header generated structure for split send
/// instruction
///
BinaryEncodingCNL::Status
BinaryEncodingCNL::EncodeSplitSend(G4_INST *inst,
                                   G9HDL::EU_INSTRUCTION_SENDS &sends) {
  Status myStatus = SUCCESS;

  G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *twoSrcMirror =
      (G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *)&sends;

  EncodeInstHeader(inst, twoSrcMirror->Common.Header);

  // encode dst part
  // trimmed down EncodeOperandDst
  {
    DstBuilder<G9HDL::EU_INSTRUCTION_SENDS>::EncodeFlagReg(inst, sends);
    DstBuilder<G9HDL::EU_INSTRUCTION_SENDS>::EncodeMaskCtrl(inst, sends);
    DstBuilder<G9HDL::EU_INSTRUCTION_SENDS>::EncodeOperandDstType(inst, sends);
    DstBuilder<G9HDL::EU_INSTRUCTION_SENDS>::EncodeDstAddrMode(inst, sends);

    G4_DstRegRegion *dst = inst->getDst();

    // encode dst reg file
    // note: for sends, we have only one bit available for dst reg file
    {
      switch (EncodingHelper::GetDstRegFile(dst)) {
      case REG_FILE_R:
        sends.SetDestinationRegisterFile(G9HDL::REGFILE_GRF);
        break;
      case REG_FILE_A:
        sends.SetDestinationRegisterFile(G9HDL::REGFILE_ARF);
        break;
      default:
        vISA_ASSERT_UNREACHABLE(" Invalid register file for split-send.");
        break;
      }
    }

    if (EncodingHelper::GetDstAddrMode(dst) == ADDR_MODE_INDIR) {
      // addr subregister
      // addr immediate
      bool subValid;
      uint16_t IndAddrRegSubNumValue = dst->ExIndSubRegNum(subValid);
      int16_t IndAddrImmedValue = dst->ExIndImmVal();

      sends.SetDestinationAddressSubregisterNumber(IndAddrRegSubNumValue);
      sends.SetDestinationAddressImmediate84((IndAddrImmedValue >> 4) & 0x1F);
      sends.SetDestinationAddressImmediateSign9((IndAddrImmedValue >> 9) & 0x1);
    } else {
      if (EncodingHelper::GetDstRegFile(dst) != REG_FILE_A) {
        uint32_t byteAddress = dst->getLinearizedStart();
        vISA_ASSERT(byteAddress % 16 == 0,
                     "dst for sends/sendsc must be oword-aligned");

        sends.SetDestinationRegisterNumber(byteAddress >> 5);
        sends.SetDestinationSubregisterNumber4((byteAddress >> 4) & 0x1);
      }
    }
  }

  // encode src1
  {
    G4_Operand *src1 = inst->getSrc(1);

    // src1 reg file - 1 bit
    switch (EncodingHelper::GetSrcRegFile(src1)) {
    case REG_FILE_R:
      sends.SetSrc1Regfile(G9HDL::REGFILE_GRF);
      break;
    case REG_FILE_A:
      sends.SetSrc1Regfile(G9HDL::REGFILE_ARF);
      break;
    default:
      vISA_ASSERT_UNREACHABLE(" Invalid register file for split-send.");
      break;
    }

    G4_SrcRegRegion *src1Region = src1->asSrcRegRegion();

    SrcBuilder<G9HDL::EU_INSTRUCTION_SENDS, 1>::EncodeSrcAddrMode(&sends, inst,
                                                                  src1);
    if (EncodingHelper::GetSrcAddrMode(src1) == ADDR_MODE_INDIR) {
      bool subValid;
      uint16_t IndAddrRegSubNumValue = src1Region->ExIndSubRegNum(subValid);
      int16_t IndAddrImmedValue = src1Region->ExIndImmVal();

      sends.SetSource1_SourceAddressImmediate84((IndAddrImmedValue >> 4) &
                                                0x1F);
      sends.SetSource1_SourceAddressImmediateSign9((IndAddrImmedValue >> 9) &
                                                   0x1);
      sends.SetSource1_SourceAddressSubregisterNumber_0(IndAddrRegSubNumValue);
    } else {
      uint32_t byteAddress = src1->getLinearizedStart();
      vISA_ASSERT(byteAddress % 32 == 0,
                   "src1 for sends/sendsc must be GRF-aligned");
      sends.SetSource1_SourceRegisterNumber(byteAddress >> 5);
      // mybin->SetBits(bitsSendsSrc1RegNum_0, bitsSendsSrc1RegNum_1,
      // byteAddress >> 5);
    }
  }

  // encode src0
  {
    G4_SrcRegRegion *src0 = inst->getSrc(0)->asSrcRegRegion();
    // note: no regfile for src0

    SrcBuilder<G9HDL::EU_INSTRUCTION_SENDS, 0>::EncodeSrcAddrMode(&sends, inst,
                                                                  src0);

    if (EncodingHelper::GetSrcAddrMode(src0) == ADDR_MODE_INDIR) {
      bool subValid;
      uint16_t IndAddrRegSubNumValue = src0->ExIndSubRegNum(subValid);
      int16_t IndAddrImmedValue = src0->ExIndImmVal();

      sends.SetSource0_SourceAddressImmediate84((IndAddrImmedValue >> 4) &
                                                0x1F);
      sends.SetSource0_SourceAddressImmediateSign9((IndAddrImmedValue >> 9) &
                                                   0x1);
      sends.SetSource0_SourceAddressSubregisterNumber(IndAddrRegSubNumValue);
    } else {
      uint32_t byteAddress = src0->getLinearizedStart();
      vISA_ASSERT(byteAddress % 32 == 0,
                   "src1 for sends/sendsc must be GRF-aligned");
      sends.SetSource0_SourceRegisterNumber(byteAddress >> 5);
      // mybin->SetBits(bitsSendsSrc1RegNum_0, bitsSendsSrc1RegNum_1,
      // byteAddress >> 5);
    }
  }

  // encode src2
  {
    G4_Operand *src2 = inst->getSrc(2);
    if (src2 == NULL) {
      return FAILURE;
    }

    if (src2->isImm()) {
      sends.SetSelreg32desc(0);
      sends.GetMessage().GetDWORD(0) = (uint32_t)src2->asImm()->getInt();
    } else if (src2->isSrcRegRegion() && src2->asSrcRegRegion()->isDirectA0()) {
      sends.SetSelreg32desc(1);
    }
  }

  // Patch SFID and EOT
  {
    vISA_ASSERT(inst->getMsgDescRaw(), "expected raw descriptor");
    uint32_t msgDesc = inst->getMsgDescRaw()->getExtendedDesc();
    EncExtMsgDescriptor emd;
    emd.ulData = msgDesc;

    G9HDL::SFID sfid = (G9HDL::SFID)emd.ExtMsgDescriptor.TargetUnitId;
    sends.SetSharedFunctionIdSfid(sfid);

    G9HDL::EOT eot = (G9HDL::EOT)emd.ExtMsgDescriptor.EndOfThread;
    sends.GetMessage().SetEot(eot);

    // additional extended msg desc to be encoded
    sends.SetExdesc96(msgDesc);
    sends.SetExdesc3116(msgDesc);
    sends.SetExdesc1111(msgDesc);

    // encoding for src3 A0 is done in DoAllEncodingSplitSEND later on
  }

  // this is a hack, but:
  // fixme: this is missing in auto-header, currently need to override the acc
  // write field
  if (inst->isNoSrcDepSet()) {
    // equivalent to: mybin->SetBits(bitsNoSrcDepSet_0, bitsNoSrcDepSet_1, 1);
    sends.SetControlsB_Accwrctrl(G9HDL::ACCWRCTRL_UPDATE_ACC); // 0x1
  }

  return myStatus;
}

/// \brief Do customized encoding of WAIT instruction
///
BinaryEncodingCNL::Status BinaryEncodingCNL::DoAllEncodingWAIT(G4_INST *inst) {
  Status myStatus = SUCCESS;

  G9HDL::EU_INSTRUCTION_BASIC_ONE_SRC oneSrc;

  oneSrc.Init();

  EncodeInstHeader(inst, oneSrc.Common.Header);
  EncodeOperandDst(inst, oneSrc.Common.OperandControls);
  G4_Operand *src0 = inst->getSrc(0);

  // EncodeSrc0RegFile
  oneSrc.GetOperandControls().SetSrc0Regfile(
      TranslateVisaToHDLRegFile(EncodingHelper::GetSrcRegFile(src0)));

  // EncodeSrc0Type
  vISA_ASSERT(!src0->isImm(),
               "src0 must not be immediate in WAIT instruction!");
  {
    oneSrc.GetOperandControls().SetSrc0Srctype(
        GetOperandSrcHDLType(src0->getType()));
  }

  SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG,
             0>::EncodeEuInstructionSourcesReg(inst, src0,
                                               oneSrc.GetRegsource(),
                                               *this); // by reference

  // Dst patching:

  RegFile regFile = EncodingHelper::GetSrcRegFile(src0);
  vISA_ASSERT(regFile == REG_FILE_A,
               "WAIT instruction source has reg file different than ARF!");
  oneSrc.Common.OperandControls.SetDestinationRegisterFile(
      TranslateVisaToHDLRegFile(regFile));

  if (regFile == REG_FILE_A) {
    bool valid;
    G4_SrcRegRegion *src0Region = src0->asSrcRegRegion();
    unsigned short RegFile =
        (unsigned short)EncodingHelper::GetSrcArchRegType(src0); // 4 bits
    unsigned short RegNumValue = src0Region->ExRegNum(valid);
    unsigned short EncodedRegNum =
        PackArchRegTypeAndArchRegFile(RegFile, RegNumValue);

    oneSrc.Common.OperandControls.GetDestinationRegisterRegion_Align1()
        .SetDestinationRegisterNumber_DestinationRegisterNumber(EncodedRegNum);

    {
      bool subValid;
      unsigned short RegSubNumValue = src0Region->ExSubRegNum(subValid);
      unsigned short ElementSizeValue =
          EncodingHelper::GetElementSizeValue(src0);
      uint32_t regOffset = RegSubNumValue * ElementSizeValue;

      if (inst->isAligned1Inst()) {
        // sub-register number: 32 byte address (5 bits encoding) within a GRF
        oneSrc.Common.OperandControls.GetDestinationRegisterRegion_Align1()
            .SetDestinationSubregisterNumber_DestinationSubRegisterNumber(
                (WORD)regOffset);
      } else { // align 16
        // sub-register number: first/second 16 byte part of 32 byte address.
        // Encoded with 1 bit.
        //  9876543210
        //  regn|x0000
        // opnds.GetDestinationRegisterRegion_Align16().SetDestinationSubregisterNumber((regOffset
        // >> 4) & 0x1);
        oneSrc.Common.OperandControls.GetDestinationRegisterRegion_Align16()
            .SetDestinationSubregisterNumber((WORD)regOffset);
      }
    }
  }

  oneSrc.Common.OperandControls.GetDestinationRegisterRegion_Align1()
      .SetDestinationHorizontalStride(G9HDL::HORZSTRIDE_1_ELEMENTS);

  oneSrc.Common.OperandControls.SetDestinationAddressingMode(
      TranslateVisaToHDLAddrMode(EncodingHelper::GetSrcAddrMode(src0)));

  BinInst *bin = getBinInst(inst);
  bin->DWords[0] = oneSrc.GetDWord(0);
  bin->DWords[1] = oneSrc.GetDWord(1);
  bin->DWords[2] = oneSrc.GetDWord(2);
  bin->DWords[3] = oneSrc.GetDWord(3);

  return myStatus;
}

/// \brief Do encoding of JMPI instruction
///
BinaryEncodingCNL::Status BinaryEncodingCNL::DoAllEncodingJMPI(G4_INST *inst) {
  Status myStatus = SUCCESS;

  G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC brOneSrc;

  brOneSrc.Init();
  EncodeInstHeader(inst, brOneSrc.GetHeader());

  // BEGIN: OPND CONTROL WORD:
  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeFlagReg(
      inst, brOneSrc.GetOperandControl());
  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeMaskCtrl(
      inst, brOneSrc.GetOperandControl());

  // hardcode:
  brOneSrc.GetOperandControl().SetDestinationRegisterFile(G9HDL::REGFILE_ARF);
  brOneSrc.GetOperandControl().SetDestinationDataType(G11HDL::DSTTYPE_UD);
  brOneSrc.GetOperandControl().SetDestinationAddressingMode(
      G9HDL::ADDRMODE_DIRECT);

  // FIXME: bxml does not have arch reg file enumerations
  unsigned short RegFile = ARCH_REG_FILE_IP; // 4 bits
  unsigned short RegNumValue = 0;
  // 7654|3210
  // RegF|RegNumVal
  unsigned short EncodedRegNum = RegFile << 4;
  EncodedRegNum = EncodedRegNum | (RegNumValue & 0xF);

  // the same as for align16
  brOneSrc.GetOperandControl()
      .GetDestinationRegisterRegion_Align1()
      .SetDestinationRegisterNumber_DestinationRegisterNumber(EncodedRegNum);
  brOneSrc.GetOperandControl()
      .GetDestinationRegisterRegion_Align1()
      .SetDestinationSubregisterNumber_DestinationSubRegisterNumber(0);

  brOneSrc.GetOperandControl()
      .GetDestinationRegisterRegion_Align1()
      .SetDestinationHorizontalStride(G9HDL::HORZSTRIDE_1_ELEMENTS);

  // src0, but belongs to opndCtl dword
  brOneSrc.GetOperandControl().SetSrc0Regfile(G9HDL::REGFILE_ARF);
  brOneSrc.GetOperandControl().SetSrc0Srctype(GetOperandSrcHDLType(Type_UD));

  // END: OPND CONTROL WORD

  // BEGIN: src0
  if (inst->getSrc(0)) {
    SrcOperandEncoder<G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC,
                      0>::SetSourceAddressingMode(&brOneSrc,
                                                  G9HDL::ADDRMODE_DIRECT);
    // FIXME: bxml does not have arch reg file enumerations
    unsigned short RegFile = ARCH_REG_FILE_IP; // 4 bits
    unsigned short RegNumValue = 0;
    // 7654|3210
    // RegF|RegNumVal
    unsigned short EncodedRegNum = RegFile << 4;
    EncodedRegNum = EncodedRegNum | (RegNumValue & 0xF);
    SrcOperandEncoder<G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC,
                      0>::SetSourceRegisterNumber(&brOneSrc, EncodedRegNum);
    SrcOperandEncoder<G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC,
                      0>::SetSourceSubRegisterNumber(&brOneSrc, 0);

    brOneSrc.SetSource0_SourceWidth(G9HDL::WIDTH_1_ELEMENTS);
    if (inst->getSrc(0)->isLabel()) {
      brOneSrc.SetSource0_SourceVerticalStride(G9HDL::VERTSTRIDE_0_ELEMENTS);
      brOneSrc.SetSource0_SourceHorizontalStride(G9HDL::HORZSTRIDE_0_ELEMENTS);
      brOneSrc.SetSource0_SourceModifier(G9HDL::SRCMOD_NO_MODIFICATION);
    }
  }

  if (inst->getSrc(0) && inst->getSrc(0)->isSrcRegRegion()) {

    G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *ptr =
        (G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *)&brOneSrc;

    SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG_REG,
               1>::EncodeEuInstructionSourcesReg(inst, inst->getSrc(0),
                                                 ptr->GetRegsource(),
                                                 *this); // by reference

    ptr->GetRegsource().SetSrc1Regfile(TranslateVisaToHDLRegFile(
        EncodingHelper::GetSrcRegFile(inst->getSrc(0))));

    if (!inst->getSrc(0)->isImm()) {
      ptr->GetRegsource().SetSrc1Srctype(
          GetOperandSrcHDLType(inst->getSrc(0)->getType()));
    }
  }

  // END: src0

  // BEGIN: src1
  //  The rest is encoded in EncodeConditionalBranches
  // END: src1
  BinInst *bin = getBinInst(inst);
  bin->DWords[0] = brOneSrc.GetDWORD(0);
  bin->DWords[1] = brOneSrc.GetDWORD(1);
  bin->DWords[2] = brOneSrc.GetDWORD(2);
  bin->DWords[3] = brOneSrc.GetDWORD(3);

  return myStatus;
}

BinaryEncodingCNL::Status BinaryEncodingCNL::DoAllEncodingCALL(G4_INST *inst) {
  Status myStatus = SUCCESS;

  BinInst *bin = getBinInst(inst);
  G9HDL::EU_INSTRUCTION_BRANCH_ONE_SRC oneSrc;
  oneSrc.Init();

  EncodeInstHeader(inst, oneSrc.GetHeader());
  EncodeOperandDst(inst, oneSrc.GetOperandControl());

  if (inst->getSrc(0) && !inst->getSrc(0)->isLabel()) {
    // indirect call
    G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *ptr =
        (G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC *)&oneSrc;

    SrcBuilder<G9HDL::EU_INSTRUCTION_SOURCES_REG_REG,
               1>::EncodeEuInstructionSourcesReg(inst, inst->getSrc(0),
                                                 ptr->GetRegsource(),
                                                 *this); // by reference

    ptr->GetRegsource().SetSrc1Regfile(TranslateVisaToHDLRegFile(
        EncodingHelper::GetSrcRegFile(inst->getSrc(0))));

    if (!inst->getSrc(0)->isImm()) {
      ptr->GetRegsource().SetSrc1Srctype(
          GetOperandSrcHDLType(inst->getSrc(0)->getType()));
    }
  } else {
    // Needed for correctness
    oneSrc.SetSrc1Regfile(G9HDL::REGFILE_IMM);
    oneSrc.SetSrc1Srctype(GetOperandSrcHDLImmType(Type_D));
  }

  bin->DWords[0] = oneSrc.GetDWORD(0);
  bin->DWords[1] = oneSrc.GetDWORD(1);
  bin->DWords[2] = oneSrc.GetDWORD(2);
  bin->DWords[3] = oneSrc.GetDWORD(3);

  return myStatus;
}

/// \brief Do encoding of control flow type instructions
///
BinaryEncodingCNL::Status BinaryEncodingCNL::DoAllEncodingCF(G4_INST *inst) {
  Status myStatus = SUCCESS;
  BinInst *bin = getBinInst(inst);

  G9HDL::EU_INSTRUCTION_BRANCH_TWO_SRC brTwoSrc;
  brTwoSrc.Init();
  // brTwoSrc.Common.OperandControls
  EncodeInstHeader(inst, brTwoSrc.GetHeader());
  // fixme: not sure why we are setting only this field for dst, but keep the
  // binary encoding the same as original one...
  brTwoSrc.GetOperandControl()
      .GetDestinationRegisterRegion_Align1()
      .SetDestinationHorizontalStride(G9HDL::HORZSTRIDE_1_ELEMENTS);
  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeFlagReg(
      inst, brTwoSrc.GetOperandControl());
  DstBuilder<G9HDL::EU_INSTRUCTION_OPERAND_CONTROLS>::EncodeMaskCtrl(
      inst, brTwoSrc.GetOperandControl());
  bin->DWords[0] = brTwoSrc.GetDWord(0);
  bin->DWords[1] = brTwoSrc.GetDWord(1);
  bin->DWords[2] = brTwoSrc.GetDWord(2);
  bin->DWords[3] = brTwoSrc.GetDWord(3);
  return myStatus;
}

/// \brief Do encoding of split send instruction
///
BinaryEncodingCNL::Status
BinaryEncodingCNL::DoAllEncodingSplitSEND(G4_INST *inst) {
  Status myStatus = SUCCESS;
  BinInst *bin = getBinInst(inst);

  G9HDL::EU_INSTRUCTION_SENDS sends;
  sends.Init();

  EncodeSplitSend(inst, sends);

  bin->DWords[0] = sends.GetDWORD(0);
  bin->DWords[1] = sends.GetDWORD(1);
  bin->DWords[2] = sends.GetDWORD(2);
  bin->DWords[3] = sends.GetDWORD(3);

  // This is a workaround for BXML defect
  G4_Operand *src3 = inst->getSrc(3);
  // additional extended msg desc to be encoded
  // FIXME: does it apply to regular SKL+ sends too?
  if (src3 && src3->isSrcRegRegion() && src3->asSrcRegRegion()->isDirectA0()) {
    bin->SetBits(bitsSendsSelReg32ExDesc_0, bitsSendsSelReg32ExDesc_1, 1);
    bin->SetBits(bitsSendsExDescRegNum_0, bitsSendsExDescRegNum_1,
                 src3->asSrcRegRegion()->getBase()->asRegVar()->getPhyRegOff());
  }

  return myStatus;
}

/// \brief Do encoding of all 'regular' one, two or three-src instructions
///        Treats send and math instructions as two-src and patches them
///        accordingly.
BinaryEncodingCNL::Status
BinaryEncodingCNL::DoAllEncodingRegular(G4_INST *inst) {
  Status myStatus = SUCCESS;

  vISA_ASSERT(!inst->isSplitSend(), "Improper instruction type called with "
                                     "DoAllEncodingRegular: sends or sendsc");

  BinInst *bin = getBinInst(inst);
  int i = inst->getNumSrc();
  switch (i) {
  case 0: {
    // for nop, we have to encode the opcode, borrow the oneSrc format
    G9HDL::EU_INSTRUCTION_BASIC_ONE_SRC oneSrc;
    oneSrc.Init();
    EncodeOpCode(inst, oneSrc.Common.Header);
    bin->DWords[0] = oneSrc.GetDWord(0);
    bin->DWords[1] = oneSrc.GetDWord(1);
    bin->DWords[2] = oneSrc.GetDWord(2);
    bin->DWords[3] = oneSrc.GetDWord(3);
    break;
  }
  case 1:
    G9HDL::EU_INSTRUCTION_BASIC_ONE_SRC oneSrc;
    oneSrc.Init();
    EncodeOneSrcInst(inst, oneSrc);
    bin->DWords[0] = oneSrc.GetDWord(0);
    bin->DWords[1] = oneSrc.GetDWord(1);
    bin->DWords[2] = oneSrc.GetDWord(2);
    bin->DWords[3] = oneSrc.GetDWord(3);
    break;
  case 2:
    G9HDL::EU_INSTRUCTION_BASIC_TWO_SRC twoSrc;
    twoSrc.Init();
    EncodeTwoSrcInst(inst, twoSrc);
    if (inst->isSend()) {
      PatchSend(inst, &twoSrc);
    } else if (inst->isMath()) {
      // fixme: math is only for two-src encoding?
      PatchMath(inst, &twoSrc);
    }

    bin->DWords[0] = twoSrc.GetDWord(0);
    bin->DWords[1] = twoSrc.GetDWord(1);
    bin->DWords[2] = twoSrc.GetDWord(2);
    bin->DWords[3] = twoSrc.GetDWord(3);
    break;
  case 3: {
    if (inst->isAligned1Inst()) {
      G9HDL::EU_INSTRUCTION_ALIGN1_THREE_SRC threeSrcAlign1;
      threeSrcAlign1.Init();
      EncodeThreeSrcInstAlign1(inst, threeSrcAlign1);
      bin->DWords[0] = threeSrcAlign1.GetRawData(0);
      bin->DWords[1] = threeSrcAlign1.GetRawData(1);
      bin->DWords[2] = threeSrcAlign1.GetRawData(2);
      bin->DWords[3] = threeSrcAlign1.GetRawData(3);

    } else {
      G9HDL::EU_INSTRUCTION_BASIC_THREE_SRC threeSrc;
      threeSrc.Init();
      EncodeThreeSrcInst(inst, threeSrc);
      bin->DWords[0] = threeSrc.GetDWORD(0);
      bin->DWords[1] = threeSrc.GetDWORD(1);
      bin->DWords[2] = threeSrc.GetDWORD(2);
      bin->DWords[3] = threeSrc.GetDWORD(3);
    }

  } break;
  default:
    break;
  }

  return myStatus;
}

/// \brief Do all encoding for an instruction.
BinaryEncodingCNL::Status BinaryEncodingCNL::DoAllEncoding(G4_INST *inst) {
  Status myStatus = SUCCESS;
  bool isFCCall = false, isFCRet = false;

  if (inst->opcode() == G4_label)
    return myStatus;

  if (inst->opcode() == G4_illegal)
    return FAILURE;

  EncodingHelper::mark3Src(inst, *this);

  {

    // Prolog:
    if (inst->opcode() == G4_pseudo_fc_call) {
      inst->asCFInst()->pseudoCallToCall();
      isFCCall = true;
    }

    if (inst->opcode() == G4_pseudo_fc_ret) {
      inst->asCFInst()->pseudoRetToRet();
      isFCRet = true;
    }

    if (inst->opcode() == G4_jmpi) {
      DoAllEncodingJMPI(inst);
    } else if (inst->opcode() == G4_wait) {
      DoAllEncodingWAIT(inst);
    } else if (inst->opcode() == G4_if || inst->opcode() == G4_endif ||
               inst->opcode() == G4_else || inst->opcode() == G4_while ||
               inst->opcode() == G4_break || inst->opcode() == G4_cont ||
               inst->opcode() == G4_halt || inst->opcode() == G4_goto ||
               inst->opcode() == G4_join) {
      DoAllEncodingCF(inst);
    } else if (inst->opcode() == G4_call) {
      DoAllEncodingCALL(inst);
    } else if (inst->isSplitSend()) {
      DoAllEncodingSplitSEND(inst);
    } else if (!EncodingHelper::hasLabelString(inst)) {
      DoAllEncodingRegular(inst);
    }

    // Epilog:
    if (isFCCall == true) {
      inst->setOpcode(G4_pseudo_fc_call);
    }

    if (isFCRet == true) {
      inst->setOpcode(G4_pseudo_fc_ret);
    }
  }

  return myStatus;
}

/// \brief Entry point.
///
/// This is counterpart of ProduceBinaryInstructions from 'old' encoder
void BinaryEncodingCNL::DoAll() {
  std::vector<ForwardJmpOffset> offsetVector;
  FixInst();
  BinaryEncodingBase::InitPlatform();
  // BDW/CHV/SKL/BXT/CNL use the same compaction tables except from 3src.
  for (uint8_t i = 0; i < (int)COMPACT_TABLE_SIZE; i++) {
    BDWCompactControlTable.AddIndex(IVBCompactControlTable[i], i);
    BDWCompactSourceTable.AddIndex(IVBCompactSourceTable[i], i);
    BDWCompactSubRegTable.AddIndex(IVBCompactSubRegTable[i], i);
    BDWCompactSubRegTable.AddIndex1(IVBCompactSubRegTable[i] & 0x1F, i);
    BDWCompactSubRegTable.AddIndex2(IVBCompactSubRegTable[i] & 0x3FF, i);
    if (kernel.getPlatform() >= GENX_ICLLP) {
      BDWCompactDataTypeTableStr.AddIndex(ICLCompactDataTypeTable[i], i);
    } else {
      BDWCompactDataTypeTableStr.AddIndex(BDWCompactDataTypeTable[i], i);
    }
  }

  int globalInstNum = 0;
  int globalHalfInstNum = 0;

  BB_LIST_ITER ib, bend(kernel.fg.end());
  for (ib = kernel.fg.begin(); ib != bend; ++ib) {
    G4_BB *bb = *ib;
    int localInstNum = 0;
    int localHalfInstNum = 0;

    /**
     * Traverse the instruction lists
     */
    INST_LIST_ITER ii, iend(bb->end());
    for (ii = bb->begin(); ii != iend; ++ii) {
      /* do detailed encoding here */
      G4_INST *inst = *ii;
      G4_opcode opcode = inst->opcode();

      if (opcode != G4_label) {
        // reuse "BinInst" from BinaryEncoding.h which can be simplified
        BinInst *bin = new (mem) BinInst();
        setBinInst(inst, bin);

        bin->DWords[0] = 0;
        bin->DWords[1] = 0;
        bin->DWords[2] = 0;
        bin->DWords[3] = 0;

        DoAllEncoding(inst);

        if (inst->opcode() == G4_pseudo_fc_call ||
            inst->opcode() == G4_pseudo_fc_ret) {
          getBinInst(inst)->SetDontCompactFlag(true);
        }

        if (doCompaction()) {
          getBinInst(inst)->SetMustCompactFlag(false);
          getBinInst(inst)->SetDontCompactFlag(inst->isNoCompactedInst());

          /**
           * handling switch/case for gen6: jump table should not be compacted
           */
          bool compacted;
          {
            TIME_SCOPE(ENCODE_COMPACTION);
            compacted = BinaryEncodingBase::compactOneInstruction(inst);
          }

          if (compacted) {
            inst->setCompacted();
          }
        }
        binInstList.push_back(getBinInst(inst));

        if (inst->opcode() >= G4_jmpi && inst->opcode() <= G4_join) {
          if (!EncodeConditionalBranches(inst, globalHalfInstNum)) {
            offsetVector.push_back(ForwardJmpOffset(inst, globalHalfInstNum));
          }
        }
      }

      BuildLabelMap(inst, localHalfInstNum, localInstNum, globalHalfInstNum,
                    globalInstNum);
    } // for inst
  }   // for bb

  kernel.setAsmCount(globalInstNum);
  SetInstCounts((uint32_t)globalHalfInstNum);

  for (auto x = offsetVector.begin(), vEnd = offsetVector.end(); x != vEnd;
       x++) {
    if (!EncodeConditionalBranches(x->inst, x->offset)) {
      vISA_ASSERT(false, "invalid label!");
    }
  }
}