File: LocalRA.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 (3067 lines) | stat: -rw-r--r-- 101,949 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
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "LocalRA.h"
#include "Assertions.h"
#include "DebugInfo.h"
#include "common.h"

#include <fstream>
#include <tuple>

using namespace vISA;

// #define _DEBUG
// #define cout cerr

#define NUM_PREGS_FOR_UNIQUE_ASSIGN 50

#define SPLIT_REF_CNT_THRESHOLD 3
#define SPLIT_USE_CNT_THRESHOLD 2
#define SPLIT_USE_DISTANCE_THRESHOLD 100

void getForbiddenGRFs(std::vector<unsigned int> &regNum, G4_Kernel &kernel,
                      unsigned stackCallRegSize, unsigned reserveSpillSize,
                      unsigned reservedRegNum);
void getCallerSaveGRF(std::vector<unsigned int> &regNum, G4_Kernel *kernel);

LocalRA::LocalRA(BankConflictPass &b, GlobalRA &g)
    : kernel(g.kernel), builder(g.builder), bc(b), gra(g) {}

BankAlign LocalRA::getBankAlignForUniqueAssign(G4_Declare *dcl) {
  // FIXME: this code is rather suspicious (we return even alignment
  // for first_half_odd???), should revisit
  switch (gra.getBankConflict(dcl)) {
  case BANK_CONFLICT_FIRST_HALF_EVEN:
  case BANK_CONFLICT_FIRST_HALF_ODD:
    return builder.oneGRFBankDivision() ? BankAlign::Even : BankAlign::Even2GRF;
  case BANK_CONFLICT_SECOND_HALF_EVEN:
  case BANK_CONFLICT_SECOND_HALF_ODD:
    return builder.oneGRFBankDivision() ? BankAlign::Odd : BankAlign::Odd2GRF;
  default:
    return BankAlign::Either;
  }
}

// returns true if kernel contains a back edge
// call/return edge may also be considered as a back edge depending on layout.
bool LocalRA::hasBackEdge() {
  for (auto curBB : kernel.fg) {

    vISA_ASSERT(curBB->size() > 0 || curBB->Succs.size() > 1,
                "Error in detecting back-edge: Basic block found with no inst "
                "list and multiple successors.");

    if (curBB->size() > 0) {
      for (auto succ : curBB->Succs) {
        vISA_ASSERT(succ->size() > 0,
                    "Error in detecting back-edge: Destination basic block of "
                    "a jmp has no instructions.");
        if (curBB->getId() >= succ->getId()) {
          return true;
        }
      }
    }
  }
  return false;
}

void LocalRA::getRowInfo(int size, int &nrows, int &lastRowSize,
                         const IR_Builder &builder) {
  if (size <= (int)builder.numEltPerGRF<Type_UW>()) {
    nrows = 1;
  } else {
    // nrows is total number of rows, including last row even if it is partial
    nrows = size / builder.numEltPerGRF<Type_UW>();
    // lastrowsize is number of words actually used in last row
    lastRowSize = size % builder.numEltPerGRF<Type_UW>();

    if (size % builder.numEltPerGRF<Type_UW>() != 0) {
      nrows++;
    }

    if (lastRowSize == 0) {
      lastRowSize = builder.numEltPerGRF<Type_UW>();
    }
  }

  return;
}

void LocalRA::specialAlign() {
  if (kernel.getInt32KernelAttr(Attributes::ATTR_Target) == VISA_3D &&
      kernel.fg.size() > 2) {
    if (gra.use4GRFAlign) {
      gra.augAlign();
    } else if ((GlobalRA::useGenericAugAlign(kernel.getPlatformGeneration()) &&
         kernel.getSimdSize() >= kernel.numEltPerGRF<Type_UD>()) ||
        (!GlobalRA::useGenericAugAlign(kernel.getPlatformGeneration()) &&
         kernel.getSimdSize() > kernel.numEltPerGRF<Type_UD>())) {
      // Set alignment of all GRF candidates
      // to 2GRF except for NoMask variables
#ifdef DEBUG_VERBOSE_ON
      DEBUG_VERBOSE("Updating alignment to Even for GRF candidates"
                    << "\n");
#endif

      gra.augAlign();
    }
    gra.updateSubRegAlignment(builder.getGRFAlign());
    // Since we are piggy backing on mask field of G4_Declare,
    // we need to make sure we reset it before going further.
    resetMasks();
  }

  return;
}

/*
 *  Pre-localRA analsyis including:
 *  1) Mark reference to create live ranges, 2) alignment handling, 3) Avialable
 * register calcuating and marking
 */
void LocalRA::preLocalRAAnalysis() {
  unsigned int numRowsEOT = 0;
  bool lifetimeOpFound = false;

  int numGRF = kernel.getNumRegTotal();

  // Clear LocalLiveRange* computed preRA
  gra.clearStaleLiveRanges();

  // Mark references made to decls to sieve local from global ranges
  markReferences(numRowsEOT, lifetimeOpFound);

  // Mark those physical registers unavailable that are declared with Output
  // attribute
  blockOutputPhyRegs();

  if (lifetimeOpFound == true) {
    // Check whether pseudo_kill/lifetime.end are only references
    // for their respective variables. Remove them if so. Doing this
    // helps reduce number of variables in symbol table increasing
    // changes of skipping global RA.
    removeUnrequiredLifetimeOps();
  }

  unsigned int numRowsReserved = numRowsEOT;

  // Remove unreferenced dcls
  gra.removeUnreferencedDcls();

  if (gra.useFastRA || gra.useHybridRAwithSpill) {
    unsigned reserveSpillSize = 0;
    unsigned int spillRegSize = 0;
    unsigned int indrSpillRegSize = 0;
    gra.determineSpillRegSize(spillRegSize, indrSpillRegSize);

    reserveSpillSize = spillRegSize + indrSpillRegSize;
    if (reserveSpillSize >= kernel.stackCall.getNumCalleeSaveRegs()) {
      gra.useFastRA = false;
      gra.useHybridRAwithSpill = false;
      numRegLRA = numGRF - numRowsReserved;
    } else {
      numRegLRA = numGRF - numRowsReserved - reserveSpillSize -
                  4; // spill Header, scratch offset, a0.2, WA
    }
  } else {
    numRegLRA = numGRF - numRowsReserved;
  }
  bool isStackCall =
      (kernel.fg.getHasStackCalls() || kernel.fg.getIsStackCallFunc());
  unsigned int stackCallRegSize = isStackCall ? kernel.stackCall.numReservedABIGRF() : 0;
  unsigned int reservedGRFNum =
      builder.getOptions()->getuInt32Option(vISA_ReservedGRFNum);

  if (isStackCall || reservedGRFNum || builder.getOption(vISA_Debug)) {
    std::vector<unsigned int> forbiddenRegs;
    getForbiddenGRFs(forbiddenRegs, kernel, stackCallRegSize, 0,
                     reservedGRFNum);
    for (unsigned int i = 0, size = forbiddenRegs.size(); i < size; i++) {
      unsigned int regNum = forbiddenRegs[i];
      pregs->setGRFUnavailable(regNum);
    }

    // FIXME: this will break if # of GRF is not 128.
    if (isStackCall) {
      // Set r60 to r99 as unavailable for local RA since these registers are
      // callee saved Local RA will use only caller save registers for
      // allocation.
      std::vector<unsigned int> callerSaveRegs;
      getCallerSaveGRF(callerSaveRegs, &kernel);
      for (unsigned int i = 0, size = callerSaveRegs.size(); i < size; i++) {
        unsigned int regNum = callerSaveRegs[i];
        pregs->setGRFUnavailable(regNum);
      }

      numRegLRA = (kernel.getNumRegTotal() - 3) - numRowsReserved;
    }

    if (builder.getOption(vISA_Debug)) {
      // Since LocalRA is not undone when debug info generation is required,
      // for keeping compile time low, we allow fewer physical registers
      // as assignable candidates. Without this, we could run in to a
      // situation where very few physical registers are available for GRA
      // and it is unable to assign registers even with spilling.
#define USABLE_GRFS_WITH_DEBUG_INFO 80
      int maxSendReg = 0;
      for (auto bb : kernel.fg) {
        for (auto inst : *bb) {
          if (inst->isSend() || inst->isSplitSend()) {
            maxSendReg = ((int)inst->getMsgDesc()->getDstLenRegs() > maxSendReg)
                             ? ((int)inst->getMsgDesc()->getDstLenRegs())
                             : maxSendReg;
            maxSendReg =
                ((int)inst->getMsgDesc()->getSrc0LenRegs() > maxSendReg)
                    ? ((int)inst->getMsgDesc()->getSrc0LenRegs())
                    : maxSendReg;
            maxSendReg =
                ((int)inst->getMsgDesc()->getSrc1LenRegs() > maxSendReg)
                    ? ((int)inst->getMsgDesc()->getSrc1LenRegs())
                    : maxSendReg;
          }
        }
      }

      int maxRegsToUse = USABLE_GRFS_WITH_DEBUG_INFO;
      if (maxSendReg > (numGRF - USABLE_GRFS_WITH_DEBUG_INFO)) {
        maxRegsToUse = (numGRF - maxSendReg) - 10;
      }

      // Also check max size of addressed GRF
      unsigned int maxAddressedRows = 0;
      for (auto dcl : kernel.Declares) {
        if (dcl->getAddressed() && maxAddressedRows < dcl->getNumRows()) {
          maxAddressedRows = dcl->getNumRows();
        }
      }

      // Assume indirect operand of maxAddressedRows exists
      // on dst, src0, src1. This is overly conservative but
      // should work for general cases.
      if ((numGRF - maxRegsToUse) / 3 < (int)maxAddressedRows) {
        maxRegsToUse = (numGRF - (maxAddressedRows * 3));

        if (maxRegsToUse < 0)
          maxRegsToUse = 0;
      }

      for (int i = maxRegsToUse; i < numGRF; i++) {
        pregs->setGRFUnavailable(i);
      }
    }
  } else {
    pregs->setSimpleGRFAvailable(true);
    if (kernel.getInt32KernelAttr(Attributes::ATTR_Target) != VISA_3D ||
        !builder.canWriteR0()) {
      pregs->setR0Forbidden();
    }

    if (builder.mustReserveR1()) {
      pregs->setR1Forbidden();
    }
  }

  return;
}

void LocalRA::trivialAssignRA(bool &needGlobalRA, bool threeSourceCandidate) {
  RA_TRACE(std::cout << "\t--trivial RA\n");

  needGlobalRA = assignUniqueRegisters(threeSourceCandidate,
                                       builder.lowHighBundle(), false);

  if (!needGlobalRA) {
    if (threeSourceCandidate) {
      kernel.setRAType(RA_Type::TRIVIAL_BC_RA);
    } else {
      kernel.setRAType(RA_Type::TRIVIAL_RA);
    }
  }

  return;
}

// Entry point to local RA
bool LocalRA::localRAPass(bool doRoundRobin, bool doSplitLLR) {
  bool needGlobalRA = true;
  PhyRegsLocalRA localPregs = *pregs;

  // Iterate over each BB and perform linear scan
  VISA_DEBUG_VERBOSE({
    localPregs.printBusyRegs();
    printAddressTakenDecls();
    printLocalRACandidates();
  });

  if (kernel.fg.funcInfoTable.empty() && !hasBackEdge()) {
    calculateInputIntervals();
  }

  VISA_DEBUG_VERBOSE(printInputLiveIntervals());
  for (auto curBB : kernel.fg) {
    PhyRegsManager pregManager(builder, localPregs, doBCR);
    std::vector<LocalLiveRange *> liveIntervals;

    PhyRegSummary *summary = gra.createPhyRegSummary();

    calculateLiveIntervals(curBB, liveIntervals);

    VISA_DEBUG_VERBOSE(printLocalLiveIntervals(curBB, liveIntervals));

    LinearScan ra(gra, liveIntervals, inputIntervals, pregManager, localPregs,
                  summary, numRegLRA, globalLRSize, doRoundRobin, doBCR,
                  highInternalConflict, doSplitLLR, kernel.getSimdSize());
    ra.run(curBB, builder, LLRUseMap);

    VISA_DEBUG_VERBOSE({
      std::cout << "BB" << curBB->getId() << "\n";
      summary->printBusyRegs();
    });

    // Tag summary of physical registers used by local RA
    gra.addBBLRASummary(curBB, summary);

    if (kernel.getOptions()->getOption(vISA_GenerateDebugInfo)) {
      updateDebugInfo(kernel, liveIntervals);
    }
  }

  if (unassignedRangeFound() == false) {
    needGlobalRA = false;
    VISA_DEBUG_VERBOSE(
        std::cout
        << "Skipping global RA because local RA allocated all live-ranges.\n");
  }

  VISA_DEBUG_VERBOSE(localRAOptReport());

  if (needGlobalRA == true &&
      !(kernel.fg.getHasStackCalls() || kernel.fg.getIsStackCallFunc())) {
    // Check whether unique physical registers can be assigned
    // to global ranges.
    bool twoBanksAssign = false;

    if (builder.oneGRFBankDivision()) {
      twoBanksAssign =
          highInternalConflict || kernel.getSimdSize() == g4::SIMD16;
    } else {
      twoBanksAssign = highInternalConflict;
    }

    needGlobalRA = assignUniqueRegisters(
        doBCR, twoBanksAssign,
        gra.useHybridRAwithSpill && !doRoundRobin);
  }

  if (needGlobalRA && (doRoundRobin || gra.twoSrcBundleBCR)) {
    undoLocalRAAssignments(true);
  }

  if (needGlobalRA == false) {
    VISA_DEBUG(std::cout << "Allocated 100% GRF ranges without graph coloring."
                         << "\n");
  }

  return needGlobalRA;
}

bool LocalRA::localRA() {
  RA_TRACE(std::cout << "--local RA--\n");

  bool doRoundRobin = builder.getOption(vISA_LocalRARoundRobin);

  int numGRF = kernel.getNumRegTotal();
  PhyRegsLocalRA phyRegs(builder, numGRF);
  pregs = &phyRegs;

  globalLRSize = 0;
  bool reduceBCInTAandFF = false;
  bool needGlobalRA = true;

  doSplitLLR = (builder.getOption(vISA_SpiltLLR) && kernel.fg.size() == 1 &&
                kernel.getInt32KernelAttr(Attributes::ATTR_Target) == VISA_3D);

  preLocalRAAnalysis();

  bool reduceBCInRR = false;

  if (builder.getOption(vISA_LocalBankConflictReduction) &&
      (builder.hasBankCollision() || gra.twoSrcBundleBCR)) {
    reduceBCInRR = bc.setupBankConflictsForKernel(
        doRoundRobin, reduceBCInTAandFF, numRegLRA, highInternalConflict);
  }

  if (!kernel.fg.getHasStackCalls() && !kernel.fg.getIsStackCallFunc() &&
      !hasSplitInsts) {
    trivialAssignRA(needGlobalRA, reduceBCInTAandFF);
  }

  if (doRoundRobin) {
    doRoundRobin = countLiveIntervals();
  }

  if ((!doRoundRobin && reduceBCInTAandFF) ||
      (doRoundRobin && reduceBCInRR)) // To avoid the scheduling issue for send
  {
    doBCR = true;
  }
  // Local RA passes:
  // RR+BC --> FF+BC -->FF-->Hybrids
  // or
  // RR --> FF --> Hybrid
  if (doRoundRobin) {
    RA_TRACE(std::cout << "\t--round-robin " << (doBCR ? "BCR " : "")
                       << "RA\n");
    needGlobalRA = localRAPass(true, false);
    if (needGlobalRA == true) {
      doRoundRobin = false;
    }
  }

  if (!doRoundRobin) {
    if ((gra.forceBCR || gra.twoSrcBundleBCR) && doBCR) {
      RA_TRACE(std::cout << "\t--first-fit BCR RA\n");
      needGlobalRA = localRAPass(false, doSplitLLR);
    }

    if (needGlobalRA) {
      RA_TRACE(std::cout << "\t--first-fit RA\n");
      globalLRSize = 0;
      if (gra.useHybridRAwithSpill) {
        countLiveIntervals();
      } else {
        globalLRSize = 0;
      }
      specialAlign();
      needGlobalRA = localRAPass(false, doSplitLLR);
    }
  }

  if (needGlobalRA == false) {
    if (doRoundRobin) {
      kernel.setRAType(doBCR ? RA_Type::LOCAL_ROUND_ROBIN_BC_RA
                             : RA_Type::LOCAL_ROUND_ROBIN_RA);
    } else {
      kernel.setRAType(doBCR ? RA_Type::LOCAL_FIRST_FIT_BC_RA
                             : RA_Type::LOCAL_FIRST_FIT_RA);
    }
  }

  setLexicalID(true);

  return !needGlobalRA;
}

void LocalRA::resetMasks() {
  auto &dcls = kernel.Declares;

  for (auto dcl : dcls) {
    gra.setMask(dcl, {});
  }
}

unsigned int LocalRA::getLargestInputGRF() {
  unsigned int largestInput = 0;

  for (auto dcl : kernel.Declares) {
    // Find out the largest GRF regsiter occupied by input variable
    // In case overlap with reserved registers
    if ((dcl->getRegFile() == G4_INPUT || dcl->isLiveIn()) &&
        dcl->isOutput() == false && !(dcl->getRegVar()->isAreg()) &&
        dcl->getRegVar()->isPhyRegAssigned()) {
      G4_RegVar *var = dcl->getRegVar();
      unsigned int regNum = var->getPhyReg()->asGreg()->getRegNum();
      unsigned int regOff = var->getPhyRegOff();
      unsigned int largestGRF =
          (regNum * kernel.numEltPerGRF<Type_UW>() +
           (regOff * dcl->getElemSize()) / G4_WSIZE + dcl->getWordSize() - 1) /
          (kernel.numEltPerGRF<Type_UW>());
      largestInput = largestInput < largestGRF ? largestGRF : largestInput;
    }
  }

  return largestInput;
}

void LocalRA::blockOutputPhyRegs() {
  for (auto dcl : kernel.Declares) {
    if (dcl->isOutput() && dcl->isInput()) {
      // The live-range may never be referenced in the CFG so we need
      // this special pass to block physical registers corresponding
      // to those declares. This may be true for FC.
      pregs->markPhyRegs(dcl);
    }
  }
}

class isLifetimeCandidateOpCandidateForRemoval {
public:
  isLifetimeCandidateOpCandidateForRemoval(GlobalRA &g) : gra(g) {}

  GlobalRA &gra;

  bool operator()(G4_INST *inst) {
    if (inst->isPseudoKill() || inst->isLifeTimeEnd()) {
      G4_Declare *topdcl;

      if (inst->isPseudoKill()) {
        topdcl = GetTopDclFromRegRegion(inst->getDst());
      } else {
        topdcl = GetTopDclFromRegRegion(inst->getSrc(0));
      }

      if (gra.getNumRefs(topdcl) == 0 && (topdcl->getRegFile() == G4_GRF ||
                                          topdcl->getRegFile() == G4_INPUT)) {
        // Remove this lifetime op
        return true;
      }
    }

    return false;
  }
};

void LocalRA::removeUnrequiredLifetimeOps() {
  // Iterate over all instructions and inspect only
  // pseudo_kills/lifetime.end instructions. Remove
  // instructions that have no other useful instruction.

  for (BB_LIST_ITER bb_it = kernel.fg.begin(); bb_it != kernel.fg.end();
       bb_it++) {
    G4_BB *bb = (*bb_it);
    bb->erase(
        std::remove_if(bb->begin(), bb->end(),
                       isLifetimeCandidateOpCandidateForRemoval(this->gra)),
        bb->end());
  }
}

void PhyRegsLocalRA::findRegisterCandiateWithAlignForward(int &i,
                                                          BankAlign align,
                                                          bool evenAlign) {
  if ((align == BankAlign::Even) && (i % 2 != 0)) {
    i++;
  } else if ((align == BankAlign::Odd) && (i % 2 == 0)) {
    i++;
  } else if ((align == BankAlign::QuadGRF) && (i % 4 != 0)) {
    i += (4 - (i % 4));
  } else if (align == BankAlign::Even2GRF) {
    while ((i % 4 >= 2) || (evenAlign && (i % 2 != 0))) {
      i++;
    }
  } else if (align == BankAlign::Odd2GRF) {
    while ((i % 4 < 2) || (evenAlign && (i % 2 != 0))) {
      i++;
    }
  }
}

unsigned int PhyRegsLocalRA::get_bundle(unsigned int baseReg, int offset) {
  if (builder.has64bundleSize2GRFPerBank()) {
    return (((baseReg + offset) % 32) / 4);
  }
  if (builder.hasPartialInt64Support()) {
    return (((baseReg + offset) % 32) / 2);
  }
  if (builder.has64bundleSize()) {
    return (((baseReg + offset) % 16) / 2);
  }
  return (((baseReg + offset) % 64) / 4);
}

int PhyRegsLocalRA::findBundleConflictFreeRegister(
    int curReg, int endReg, unsigned short occupiedBundles, BankAlign align,
    bool evenAlign) {
  int i = curReg;
  while (occupiedBundles & (1 << get_bundle(i, 0)) ||
         occupiedBundles & (1 << get_bundle(i, 1))) {
    i++;
    findRegisterCandiateWithAlignForward(i, align, evenAlign);
    if (i > endReg) // Out of bound, drop the bundle conflict considration
    {
      i = curReg;
      break;
    }
  }

  return i;
}

void PhyRegsLocalRA::findRegisterCandiateWithAlignBackward(int &i,
                                                           BankAlign align,
                                                           bool evenAlign) {
  if ((align == BankAlign::Even) && (i % 2 != 0)) {
    i--;
  } else if ((align == BankAlign::Odd) && (i % 2 == 0)) {
    i--;
  } else if ((align == BankAlign::QuadGRF) && (i % 4 != 0)) {
    if (i > 0)
      i -= (i % 4);
    else
      i -= (4 - (-i % 4));
  } else if (align == BankAlign::Even2GRF) {
    while (i >= 0 && ((i % 4 >= 2) || (evenAlign && (i % 2 != 0)))) {
      i--;
    }
  } else if (align == BankAlign::Odd2GRF) {
    while (i >= 0 && ((i % 4 < 2) || (evenAlign && (i % 2 != 0)))) {
      i--;
    }
  }
}

inline static unsigned short getOccupiedBundle(IR_Builder &builder,
                                               GlobalRA &gra, G4_Declare *dcl,
                                               BankAlign &preferBank) {
  unsigned short occupiedBundles = 0;
  unsigned bundleNum = 0;
  unsigned int evenBankNum = 0;
  unsigned int oddBankNum = 0;

  if (!builder.hasDPAS() ||
      !builder.getOption(vISA_EnableDPASBundleConflictReduction)) {
    return 0;
  }

  for (const BundleConflict &conflict : gra.getBundleConflicts(dcl)) {
    LocalLiveRange *lr = gra.getLocalLR(conflict.dcl);
    if (lr == nullptr)
      continue;
    int subregnum;
    G4_VarBase *preg = lr->getPhyReg(subregnum);
    if (preg == NULL) {
      preg = lr->getTopDcl()->getRegVar()->getPhyReg();
    }

    if (preg != NULL) {
      int offset = conflict.offset;
      unsigned int reg = preg->asGreg()->getRegNum();
      unsigned int bank = gra.get_bank(reg, offset);
      if (bank) {
        oddBankNum++;
      } else {
        evenBankNum++;
      }
      unsigned int bundle = gra.get_bundle(reg, offset);
      unsigned int bundle1 = gra.get_bundle(reg, offset + 1);
      if (!(occupiedBundles & ((unsigned short)1 << bundle))) {
        bundleNum++;
      }
      occupiedBundles |= (unsigned short)1 << bundle;
      occupiedBundles |= (unsigned short)1 << bundle1;
    }
  }
  if (bundleNum > 12) {
    occupiedBundles = 0;
  }

  if (evenBankNum || oddBankNum) {
    preferBank = (evenBankNum < oddBankNum) ? BankAlign::Even : BankAlign::Odd;
  }

  return occupiedBundles;
}

bool LocalRA::assignUniqueRegisters(bool twoBanksRA, bool twoDirectionsAssign,
                                    bool hybridWithSpill) {
  // Iterate over all dcls and calculate number of rows
  // required if each unallocated dcl had its own physical
  // register. Check number of registers used up by local RA.
  // If unique assignments are possible, then assign and set
  // needGlobalRA to false.
  unsigned int numRows = 0;
  std::vector<G4_Declare *> unallocatedRanges;
  bool needGlobalRA = true;
  uint32_t nextEOTGRF = numRegLRA;
  std::unordered_set<unsigned int> emptyForbidden;

  unsigned int largestInput = getLargestInputGRF();
  if (((nextEOTGRF < (kernel.getNumRegTotal() - 16)) ||
       (nextEOTGRF < largestInput)) &&
      builder.hasEOTGRFBinding()) {
    // we can't guarantee unique assignments for all the EOT sources
    // punt to global RA
    return true;
  }

  // Identify global ranges not having an allocation
  for (auto dcl : kernel.Declares) {
    if (dcl->getAliasDeclare() == NULL && dcl->getRegFile() == G4_GRF &&
        dcl->getRegVar()->isPhyRegAssigned() == false) {
      auto dclLR = gra.getLocalLR(dcl);

      if (dclLR && dclLR->isEOT() && builder.hasEOTGRFBinding()) {
        // For EOT use r112 - r127
        // for simplicity we assign each EOT source unique GRFs
        G4_Greg *phyReg = builder.phyregpool.getGreg(nextEOTGRF);
        dcl->getRegVar()->setPhyReg(phyReg, 0);
        dclLR->setPhyReg(phyReg, 0);
        dclLR->setAssigned(true);
        // we have to make sure src0 and src1 of split send get different GRFs
        nextEOTGRF += dcl->getNumRows();
        if (kernel.getOption(vISA_GenerateDebugInfo)) {
          uint32_t start = 0, end = 0;
          for (auto rit = kernel.fg.rbegin(); rit != kernel.fg.rend(); rit++) {
            G4_BB *bb = (*rit);

            if (bb->size() > 0) {
              end = bb->back()->getVISAId();
              break;
            }
          }
          updateDebugInfo(kernel, dcl, start, end);
        }
      } else {
        numRows += dcl->getNumRows();
        if (hybridWithSpill) {
          if (dcl->isDoNotSpill()) {
            unallocatedRanges.push_back(dcl);
          }
        } else {
          unallocatedRanges.push_back(dcl);
        }
      }
    }
  }

  if (numRows < numRegLRA || hybridWithSpill) {
    // Get superset of registers used by local RA
    // in all basic blocks.
    PhyRegsManager phyRegMgr(builder, *pregs, twoBanksRA);

    if (!hybridWithSpill && kernel.getOption(vISA_Debug)) {
      // when doing trivial RA, if any kernel input registers
      // are unreferenced, they're overwritten by other
      // variables. when running in -O0 mode, which is usually
      // for debug info generation, mark those input
      // registers as busy so they're not overwritten.
      // this is a problem for trivial assignments because
      // live-range computation is not done when trivial RA
      // succeeds. all variables are expected to be available
      // throughout the program since they're trivial assignments.
      for (auto dcl : kernel.Declares) {
        if (!dcl->getRootDeclare())
          continue;
        if (!dcl->isInput())
          continue;

        if (gra.getNumRefs(dcl) == 0) {
          // this is an unreferenced input
          auto phyReg = dcl->getRegVar()->getPhyReg();
          if (!phyReg || !phyReg->isGreg())
            continue;

          phyRegMgr.getAvailableRegs()->markPhyRegs(dcl);
        }
      }
    }

    for (auto bb : kernel.fg) {
      PhyRegSummary *summary = gra.getBBLRASummary(bb);
      if (summary) {
        for (unsigned int i = 0; i < numRegLRA; i++) {
          if (summary->isGRFBusy(i)) {
            phyRegMgr.getAvailableRegs()->setGRFBusy(i, 1);
          }
        }
      }
    }

    needGlobalRA = hybridWithSpill;
    bool assignFromFront = true;
#ifdef DEBUG_VERBOSE_ON
    COUT_ERROR << "Trival RA: "
               << "\n";
#endif
    for (auto dcl : unallocatedRanges) {
      int regNum = 0;
      int subregNum = 0;
      int sizeInWords = dcl->getWordSize();
      int nrows = 0;
      BankAlign bankAlign = BankAlign::Either;
      if (twoBanksRA && gra.getBankConflict(dcl) != BANK_CONFLICT_NONE) {
        bankAlign = getBankAlignForUniqueAssign(dcl);

        if (bankAlign == BankAlign::Even || bankAlign == BankAlign::Even2GRF) {
          assignFromFront = true;
        }
        if (twoDirectionsAssign &&
            (bankAlign == BankAlign::Odd || bankAlign == BankAlign::Odd2GRF)) {
          assignFromFront = false;
        }
      }

      auto assignAlign = gra.isQuadAligned(dcl)   ? BankAlign::QuadGRF
                         : gra.isEvenAligned(dcl) ? BankAlign::Even
                                                  : bankAlign;
      G4_SubReg_Align subAlign =
          builder.GRFAlign() ? builder.getGRFAlign() : gra.getSubRegAlign(dcl);
      auto dclLR = gra.getLocalLR(dcl);

      if (assignFromFront) {
        BankAlign preferBank = BankAlign::Either;
        unsigned short occupiedBundles =
            getOccupiedBundle(builder, gra, dcl, preferBank);
        nrows = phyRegMgr.findFreeRegs(
            sizeInWords, assignAlign, subAlign, regNum, subregNum, 0,
            numRegLRA - 1, occupiedBundles, 0, false,
            dclLR ? dclLR->getForbidden() : emptyForbidden);
      } else {
        nrows = phyRegMgr.findFreeRegs(
            sizeInWords, assignAlign, subAlign, regNum, subregNum,
            numRegLRA - 1, 0, 0, 0, false,
            dclLR ? dclLR->getForbidden() : emptyForbidden);
      }

      if (nrows) {
        G4_Greg *phyReg = builder.phyregpool.getGreg(regNum);
        // adjust subreg num to type size
        if (dcl->getElemSize() == 1) {
          subregNum *= 2;
        } else {
          subregNum /= (dcl->getElemSize() / 2);
        }
        dcl->getRegVar()->setPhyReg(phyReg, subregNum);
      } else {
        needGlobalRA = true;
        break;
      }
      if (twoBanksRA && twoDirectionsAssign) {
        assignFromFront = !assignFromFront;
      }
    }

    if (needGlobalRA) {
      for (auto dcl : unallocatedRanges) {
        if (!hybridWithSpill) {
          dcl->getRegVar()->resetPhyReg();
        } else if (!dcl->isDoNotSpill()) {
          dcl->getRegVar()->resetPhyReg();
        }
      }
    } else {
      if (kernel.getOption(vISA_GenerateDebugInfo)) {
        uint32_t start = 0, end = UNMAPPABLE_VISA_INDEX;
        for (auto rit = kernel.fg.rbegin(); rit != kernel.fg.rend(); rit++) {
          G4_BB *bb = (*rit);

          if (bb->size() > 0) {
            for (auto inst : *bb) {
              if (inst->getVISAId() != UNMAPPABLE_VISA_INDEX) {
                end = bb->back()->getVISAId();
                break;
              }
            }
            if (end != UNMAPPABLE_VISA_INDEX)
              break;
          }
        }

        for (auto dcl : unallocatedRanges) {
          updateDebugInfo(kernel, dcl, start, end);
        }

        // unallocatedRanges doesnt contain input dcls
        for (auto dcl : kernel.Declares) {
          if (dcl->isInput()) {
            // if an input is never referenced in the program, its
            // GRF storage may be overwritten in -O2 mode. only in
            // presence of -debug switch does compiler preserve
            // kernel inputs registers even when unreferenced with
            // trivial RA.
            if (gra.getNumRefs(dcl) > 0 || kernel.getOption(vISA_Debug))
              updateDebugInfo(kernel, dcl, start, end);
          }
        }
      }
    }

  } else {
    needGlobalRA = true;
  }
#ifdef DEBUG_VERBOSE_ON
  COUT_ERROR << "\n";
#endif

  return needGlobalRA;
}

void GlobalRA::removeUnreferencedDcls() {
  // Iterate over all dcls and remove those with 0
  // ref count and not addressed. This is done only for
  // GRF dcls.

  // Propagate top dcl info to aliases
  for (auto dcl : kernel.Declares) {
    if (dcl->getAliasDeclare() != nullptr) {
      setNumRefs(dcl, getNumRefs(dcl->getAliasDeclare()));
    }
  }

  auto isUnrefDcl = [this](G4_Declare *dcl) {
    return (dcl->getRegFile() == G4_GRF || dcl->getRegFile() == G4_INPUT) &&
           getNumRefs(dcl) == 0 &&
           dcl->getRegVar()->isPhyRegAssigned() == false &&
           dcl != kernel.fg.builder->getBuiltinR0() &&
           dcl != kernel.fg.builder->getSpillSurfaceOffset() &&
           (!kernel.fg.builder->getOptions()->getuInt32Option(vISA_CodePatch) ||
            dcl->getAliasDeclare() != kernel.fg.builder->getInputR1());
  };

  kernel.Declares.erase(std::remove_if(kernel.Declares.begin(),
                                       kernel.Declares.end(), isUnrefDcl),
                        kernel.Declares.end());
}

bool LocalRA::unassignedRangeFound() {
  bool unassignedRangeFound = false;
  for (auto dcl : kernel.Declares) {
    if (dcl->getAliasDeclare() == NULL && dcl->getRegFile() == G4_GRF &&
        dcl->getRegVar()->isPhyRegAssigned() == false) {
      unassignedRangeFound = true;
      break;
    }
  }

  return unassignedRangeFound;
}

// Update numRegsUsed to max physical register used based on summary
void LocalRA::updateRegUsage(PhyRegSummary *summary,
                             unsigned int &numRegsUsed) {
  for (uint32_t i = 0, numGRF = summary->getNumGRF(); i < numGRF; i++) {
    if (numRegsUsed < i && summary->isGRFBusy(i) == true) {
      numRegsUsed = i;
    }
  }
}

// Emit out localRA opt report
void LocalRA::localRAOptReport() {
  unsigned int totalRanges = 0, localRanges = 0;

  for (auto dcl : kernel.Declares) {
    auto dcl_it_lr = gra.getLocalLR(dcl);
    if (dcl_it_lr && dcl_it_lr->isLiveRangeLocal() &&
        dcl_it_lr->isGRFRegAssigned()) {
      localRanges++;
    }

    if ((dcl->getRegFile() == G4_GRF || dcl->getRegFile() == G4_INPUT) &&
        dcl->getAliasDeclare() == NULL) {
      totalRanges++;
    }
  }

  VISA_DEBUG({
    std::cout << "\n";
    std::cout << "Total GRF ranges: " << totalRanges << "\n";
    std::cout << "GRF ranges allocated by local RA: " << localRanges << "\n";
    if (totalRanges) {
      std::cout << (int)(localRanges * 100 / totalRanges)
                << "% allocated by local RA\n\n";
    }
  });
}

// Given a src/dst reg region in opnd, traverse to its base and
// return the declaration. Resolve any aliases and return the
// topmost declaration.
// opnd->topdcl should give same result for most cases. But for
// spill code, if an address register spills then opnd->topdcl does
// not return expected dcl for the fill.
G4_Declare *GetTopDclFromRegRegion(G4_Operand *opnd) {
  G4_Declare *dcl = nullptr;

  vISA_ASSERT(opnd->isRegRegion(),
              "Operand is not a register region so cannot have a top dcl");
  G4_VarBase *base = opnd->getBase();

  if (base && base->isRegVar()) {
    dcl = base->asRegVar()->getDeclare()->getRootDeclare();
  }

  return dcl;
}

unsigned int LocalRA::convertSubRegOffFromWords(G4_Declare *dcl,
                                                int subregnuminwords) {
  // Return subreg offset in units of dcl's element size.
  // Input is subregnum in word units.
  unsigned int subregnum;

  subregnum = (subregnuminwords * 2) / dcl->getElemSize();

  return subregnum;
}

unsigned int LocalRA::convertSubRegOffToWords(G4_Declare *dcl, int subregnum) {
  // Return subreg offset in units of words from dcl's element size
  unsigned int subregnuminwords;

  subregnuminwords = subregnum * dcl->getElemSize() / 2;

  return subregnuminwords;
}

void LocalRA::undoLocalRAAssignments(bool clearInterval) {
  VISA_DEBUG(std::cout << "Undoing local RA assignments\n");

  // Undo all assignments made by local RA
  for (auto dcl : kernel.Declares) {
    LocalLiveRange *lr = gra.getLocalLR(dcl);
    if (lr != NULL) {
      if (lr->getAssigned() == true) {
        // Undo the assignment
        lr->setAssigned(false);
        lr->getTopDcl()->getRegVar()->resetPhyReg();

        if (kernel.getOption(vISA_GenerateDebugInfo)) {
          kernel.getKernelDebugInfo()
              ->getLiveIntervalInfo(lr->getTopDcl(), false)
              ->clearLiveIntervals();
        }
      }
      if (clearInterval) {
        if (lr->isLiveRangeLocal()) {
          lr->setFirstRef(NULL, 0);
        }
        if (lr->getTopDcl()->getRegFile() == G4_INPUT) {
          lr->setLastRef(NULL, 0);
        }
      }
    }
  }

  // Delete summary information stored with each basic block
  gra.clearBBLRASummaries();
}

LocalLiveRange *GlobalRA::GetOrCreateLocalLiveRange(G4_Declare *topdcl) {
  LocalLiveRange *lr = getLocalLR(topdcl);

  // Check topdcl of operand and setup a new live range if required
  if (!lr) {
    localLiveRanges.push_back(LocalLiveRange(builder));
    lr = &localLiveRanges.back();
    setLocalLR(topdcl, lr);
  }

  vISA_ASSERT(lr != NULL, "Local LR could not be created");
  return lr;
}

void LocalRA::markReferencesInOpnd(G4_Operand *opnd, bool isEOT,
                                   INST_LIST_ITER inst_it, unsigned int pos) {
  G4_Declare *topdcl = NULL;

  if ((opnd->isSrcRegRegion() || opnd->isDstRegRegion())) {
    topdcl = GetTopDclFromRegRegion(opnd);

    if (topdcl &&
        (topdcl->getRegFile() == G4_GRF || topdcl->getRegFile() == G4_INPUT)) {
      // Handle GRF here
      vISA_ASSERT(topdcl->getAliasDeclare() == NULL, "Not topdcl");
      LocalLiveRange *lr = gra.GetOrCreateLocalLiveRange(topdcl);
      lr->recordRef(curBB);

      if (doSplitLLR && opnd->isSrcRegRegion()) {
        LLR_USE_MAP_ITER useMapIter;
        useMapIter = LLRUseMap.find(lr);
        if (useMapIter == LLRUseMap.end()) {
          std::vector<std::pair<INST_LIST_ITER, unsigned int>> useList;
          useList.push_back(make_pair(inst_it, pos));
          LLRUseMap.insert(make_pair(lr, useList));
        } else {
          (*useMapIter).second.push_back(make_pair(inst_it, pos));
        }
      }

      if (topdcl->getRegVar() && topdcl->getRegVar()->isPhyRegAssigned() &&
          topdcl->getRegVar()->getPhyReg()->isGreg()) {
        pregs->markPhyRegs(topdcl);
      }

      if (isEOT) {
        lr->markEOT();
      }

      gra.recordRef(topdcl);

      if (kernel.getInt32KernelAttr(Attributes::ATTR_Target) == VISA_3D &&
          opnd->isDstRegRegion() && !topdcl->getAddressed()) {
        if (opnd->getInst()->isWriteEnableInst() == false) {
        } else {
          gra.setAugmentationMask(topdcl, AugmentationMasks::NonDefault);
        }
      }
    } else {
      // Handle register other than GRF
    }
  } else if (opnd->isAddrExp()) {
    G4_AddrExp *addrExp = opnd->asAddrExp();

    topdcl = addrExp->getRegVar()->getDeclare();
    while (topdcl->getAliasDeclare() != NULL)
      topdcl = topdcl->getAliasDeclare();

    vISA_ASSERT(topdcl != NULL, "Top dcl was null for addr exp opnd");

    LocalLiveRange *lr = gra.GetOrCreateLocalLiveRange(topdcl);
    lr->recordRef(curBB);
    lr->markIndirectRef();

    if (topdcl->getRegVar() && topdcl->getRegVar()->isPhyRegAssigned() &&
        topdcl->getRegVar()->getPhyReg()->isGreg()) {
      pregs->markPhyRegs(topdcl);
    }

    gra.recordRef(topdcl);
  }
}

void LocalRA::markReferencesInInst(INST_LIST_ITER inst_it) {
  auto inst = (*inst_it);

  if (inst->getNumDst() > 0) {
    // Scan dst
    G4_Operand *dst = inst->getDst();

    if (dst) {
      markReferencesInOpnd(dst, false, inst_it, 0);
    }
  }

  // Scan srcs
  for (int i = 0, nSrcs = inst->getNumSrc(); i < nSrcs; i++) {
    G4_Operand *src = inst->getSrc(i);

    if (src) {
      markReferencesInOpnd(src, inst->isEOT(), inst_it, i);
    }
  }
}

void LocalRA::setLexicalID(bool includePseudo) {
  unsigned int id = 0;
  for (auto bb : kernel.fg) {
    for (auto curInst : *bb) {
      if ((!includePseudo) &&
          (curInst->isPseudoKill() || curInst->isLifeTimeEnd())) {
        curInst->setLexicalId(id);
      } else {
        curInst->setLexicalId(id++);
      }
    }
  }
}

void LocalRA::addOutOfBoundForbidden(G4_Declare *dcl, G4_Operand *opnd) {
  LocalLiveRange *lr = gra.getLocalLR(dcl);
  if (!lr) {
    return;
  }

  int opndEndGRF = opnd->getLinearizedEnd() / builder.numEltPerGRF<Type_UB>();
  int dclEndGRF = (dcl->getByteSize() - 1) / builder.numEltPerGRF<Type_UB>();
  if (opndEndGRF > dclEndGRF) {
    for (int i = 0; i < (opndEndGRF - dclEndGRF); i++) {
      lr->addForbidden(kernel.getNumRegTotal() - i + 1);
    }
  }

  return;
}

void LocalRA::markSpecialForbidden(INST_LIST_ITER inst_it) {
  G4_INST *inst = (*inst_it);

  if (!inst->isSend()) {
    return;
  }

  if (!inst->asSendInst()->isSVMScatterRW() ||
      inst->getExecSize() >= g4::SIMD8) {
    return;
  }

  G4_DstRegRegion *dst = inst->getDst();
  if (dst && !dst->isNullReg()) {
    G4_Declare *dcl = inst->getDst()->getTopDcl();
    if (dcl) {
      dcl = dcl->getRootDeclare();
      addOutOfBoundForbidden(dcl, dst);
    }
  }

  for (unsigned i = 0, numSrc = inst->getNumSrc(); i < numSrc; i++) {
    G4_Operand *src = inst->getSrc(i);
    if (src) {
      G4_Declare *dcl = src->getTopDcl();
      if (dcl) {
        dcl = dcl->getRootDeclare();
        addOutOfBoundForbidden(dcl, src);
      }
    }
  }

  return;
}

void LocalRA::markReferences(unsigned int &numRowsEOT, bool &lifetimeOpFound) {
  unsigned int id = 0;
  // Iterate over all BBs
  for (auto _curBB : kernel.fg) {
    curBB = _curBB;
    // Iterate over all insts
    for (INST_LIST_ITER inst_it = curBB->begin(), inst_end = curBB->end();
         inst_it != inst_end; ++inst_it) {
      G4_INST *curInst = (*inst_it);

      if (curInst->isPseudoKill() || curInst->isLifeTimeEnd()) {
        curInst->setLexicalId(id);
        lifetimeOpFound = true;
        if (curInst->isLifeTimeEnd()) {
          markReferencesInInst(inst_it);
        }
        continue;
      }

      if (curInst->isSplitIntrinsic())
        hasSplitInsts = true;

      // set lexical ID
      curInst->setLexicalId(id++);

      if (curInst->isEOT() && kernel.fg.builder->hasEOTGRFBinding()) {
        numRowsEOT += curInst->getSrc(0)->getTopDcl()->getNumRows();

        if (curInst->isSplitSend() && !curInst->getSrc(1)->isNullReg()) {
          // both src0 and src1 have to be >=r112
          numRowsEOT += curInst->getSrc(1)->getTopDcl()->getNumRows();
        }
      }

      markReferencesInInst(inst_it);
      markSpecialForbidden(inst_it);
    }
  }
}

// Function to calculate input register live intervals,
// which are now tracked at word granularity because
// there may be overlap between two different input variables.
void LocalRA::calculateInputIntervals() {
  setLexicalID(false);

  int numGRF = kernel.getNumRegTotal();
  std::vector<uint32_t> inputRegLastRef(numGRF * kernel.numEltPerGRF<Type_UW>(),
                                        UINT_MAX);

  for (BB_LIST_RITER bb_it = kernel.fg.rbegin(), bb_rend = kernel.fg.rend();
       bb_it != bb_rend; bb_it++) {
    G4_BB *bb = (*bb_it);

    for (INST_LIST_RITER inst_it = bb->rbegin(), inst_rend = bb->rend();
         inst_it != inst_rend; inst_it++) {
      G4_INST *curInst = (*inst_it);

      G4_Declare *topdcl = NULL;
      LocalLiveRange *lr = NULL;

      // scan dst operand (may be unnecessary but added for safety)
      if (curInst->getDst() != NULL) {
        // Scan dst
        G4_DstRegRegion *dst = curInst->getDst();

        topdcl = GetTopDclFromRegRegion(dst);

        if (topdcl && (lr = gra.getLocalLR(topdcl))) {
          if (topdcl->getRegFile() == G4_INPUT && !(dst->isAreg()) &&
              topdcl->isOutput() == false && lr->hasIndirectAccess() == false &&
              !builder.isPreDefArg(topdcl)) {
            unsigned int lastRef = 0;

            vISA_ASSERT(lr->isGRFRegAssigned(),
                        "Input variable has no pre-assigned physical register");

            if (lr->getLastRef(lastRef) == NULL) {
              unsigned int curInstId = curInst->getLexicalId();
              lr->setLastRef(curInst, curInstId);

              G4_RegVar *var = topdcl->getRegVar();
              unsigned int regNum = var->getPhyReg()->asGreg()->getRegNum();
              unsigned int regOff = var->getPhyRegOff();
              unsigned int idx = regNum * kernel.numEltPerGRF<Type_UW>() +
                                 (regOff * topdcl->getElemSize()) / G4_WSIZE +
                                 topdcl->getWordSize() - 1;

              unsigned int numWords = topdcl->getWordSize();
              for (int i = numWords - 1; i >= 0; --i, --idx) {
                if (inputRegLastRef[idx] == UINT_MAX &&
                    // Check for registers that are marked as forbidden,
                    // e.g., BuiltinR0 and those reserved for stack call
                    pregs->isGRFAvailable(idx /
                                          kernel.numEltPerGRF<Type_UW>())) {
                  inputRegLastRef[idx] = curInstId;
                  inputIntervals.push_front(InputLiveRange(idx, curInstId));
                  if (kernel.getOptions()->getOption(vISA_GenerateDebugInfo)) {
                    updateDebugInfo(kernel, topdcl, 0, curInst->getVISAId());
                  }
                }
              }
            }
          }
        }
      }

      // Scan src operands
      for (int i = 0, nSrcs = curInst->getNumSrc(); i < nSrcs; i++) {
        G4_Operand *src = curInst->getSrc(i);

        if (src && src->getTopDcl()) {
          topdcl = GetTopDclFromRegRegion(src);

          if (topdcl && (lr = gra.getLocalLR(topdcl))) {
            // Check whether it is input
            if ((topdcl->getRegFile() == G4_INPUT || topdcl->isLiveIn()) &&
                !(src->isAreg()) && topdcl->isOutput() == false &&
                lr->hasIndirectAccess() == false &&
                !builder.isPreDefArg(topdcl)) {
              unsigned int lastRef = 0;

              vISA_ASSERT(
                  lr->isGRFRegAssigned(),
                  "Input variable has no pre-assigned physical register");

              if (lr->getLastRef(lastRef) == NULL) {
                unsigned int curInstId = curInst->getLexicalId();
                lr->setLastRef(curInst, curInstId);

                G4_RegVar *var = topdcl->getRegVar();
                unsigned int regNum = var->getPhyReg()->asGreg()->getRegNum();
                unsigned int regOff = var->getPhyRegOff();
                unsigned int idx =
                    regNum * kernel.numEltPerGRF<Type_UW>() +
                    (regOff * TypeSize(topdcl->getElemType())) / G4_WSIZE +
                    topdcl->getWordSize() - 1;

                unsigned int numWords = topdcl->getWordSize();
                for (int i = numWords - 1; i >= 0; --i, --idx) {
                  if (inputRegLastRef[idx] == UINT_MAX &&
                      // Check for registers that are marked as forbidden,
                      // e.g., BuiltinR0 and those reserved for stack call
                      pregs->isGRFAvailable(idx /
                                            kernel.numEltPerGRF<Type_UW>())) {
                    inputRegLastRef[idx] = curInstId;
                    if (builder.avoidDstSrcOverlap() &&
                        curInst->getDst() != NULL &&
                        hasDstSrcOverlapPotential(curInst->getDst(),
                                                  src->asSrcRegRegion())) {
                      inputIntervals.push_front(
                          InputLiveRange(idx, curInstId + 1));
                    } else {
                      inputIntervals.push_front(InputLiveRange(idx, curInstId));
                    }
                    if (kernel.getOptions()->getOption(
                            vISA_GenerateDebugInfo)) {
                      updateDebugInfo(kernel, topdcl, 0, curInst->getVISAId());
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

#ifdef _DEBUG
  for (DECLARE_LIST_ITER dcl_it = kernel.Declares.begin();
       dcl_it != kernel.Declares.end(); dcl_it++) {
    G4_Declare *curDcl = (*dcl_it);

    if (curDcl->isOutput() && gra.getLocalLR(curDcl) &&
        gra.getLocalLR(curDcl)->isGRFRegAssigned()) {
      G4_RegVar *var = curDcl->getRegVar();
      unsigned int regNum = var->getPhyReg()->asGreg()->getRegNum();
      unsigned int regOff = var->getPhyRegOff();
      unsigned int idx = regNum * kernel.numEltPerGRF<Type_UW>() +
                         (regOff * TypeSize(curDcl->getElemType())) / G4_WSIZE;

      unsigned int numWords = curDcl->getWordSize();
      for (unsigned int i = 0; i < numWords; ++i, ++idx) {
        if (inputRegLastRef[idx] != UINT_MAX) {
          vISA_ASSERT(
              false,
              "Invalid output variable with overlapping input variable!");
        }
      }
    }
  }
#endif
}

bool LocalRA::hasDstSrcOverlapPotential(G4_DstRegRegion *dst,
                                        G4_SrcRegRegion *src) {
  bool dstOpndNumRows = false;

  if (dst->getBase()->isRegVar()) {
    G4_Declare *dstDcl = dst->getBase()->asRegVar()->getDeclare();
    if (dstDcl != nullptr) {
      int dstOffset = (dstDcl->getOffsetFromBase() + dst->getLeftBound()) /
                      kernel.numEltPerGRF<Type_UB>();
      G4_DstRegRegion *dstRgn = dst;
      dstOpndNumRows = dstRgn->getSubRegOff() * dstRgn->getTypeSize() +
                           dstRgn->getLinearizedEnd() -
                           dstRgn->getLinearizedStart() + 1 >
                       kernel.numEltPerGRF<Type_UB>();

      if (src != NULL && src->isSrcRegRegion() &&
          src->asSrcRegRegion()->getBase()->isRegVar()) {
        G4_SrcRegRegion *srcRgn = src->asSrcRegRegion();
        G4_Declare *srcDcl = src->getBase()->asRegVar()->getDeclare();
        int srcOffset = (srcDcl->getOffsetFromBase() + src->getLeftBound()) /
                        kernel.numEltPerGRF<Type_UB>();
        bool srcOpndNumRows = srcRgn->getSubRegOff() * srcRgn->getTypeSize() +
                                  srcRgn->getLinearizedEnd() -
                                  srcRgn->getLinearizedStart() + 1 >
                              kernel.numEltPerGRF<Type_UB>();

        if (dstOpndNumRows || srcOpndNumRows) {
          if (!(gra.isEvenAligned(dstDcl) && gra.isEvenAligned(srcDcl) &&
                srcOffset % 2 == dstOffset % 2 && dstOpndNumRows &&
                srcOpndNumRows)) {
            return true;
          }
        }
      }
    }
  }

  return false;
}

// Function to calculate local live intervals in ascending order of starting
// point in basic block bb.
//
// FIXME: current implementation cannot handle partial use-before-define case as
// shown below:
//
// loop_start:
// V1 (1) =
// P1 =
// (P1) (16) = V1
// ...
// V1 (16) =
// ...
// (P2) Jmp  loop_start
//
// One way to fix this is to check the emask and simd size of local def/use and
// make sure they satisfy the followign rules: 1) The SIMD size of a use should
// be equal to or smaller than earlier def seen for that variable. 2) The Emask
// of use should be equal of narrower than def seen earlier. 3) Each use should
// be fully defined in current BB (e.g., if a use is predicated,
//    the def should be either non-predicated or have the same predicate).
//
void LocalRA::calculateLiveIntervals(
    G4_BB *bb, std::vector<LocalLiveRange *> &liveIntervals) {
  int idx = 0;
  bool brk = false;

  for (INST_LIST_ITER inst_it = bb->begin(), bbend = bb->end();
       inst_it != bbend && !brk; inst_it++, idx += 2) {
    G4_INST *curInst = (*inst_it);
    G4_Declare *topdcl = NULL;
    LocalLiveRange *lr = NULL;

    if (curInst->isPseudoKill() || curInst->isLifeTimeEnd()) {
      continue;
    }

    // Scan srcs
    for (int i = 0, nSrcs = curInst->getNumSrc(); i < nSrcs; i++) {
      G4_Operand *src = curInst->getSrc(i);

      if (src && src->getTopDcl()) {
        // Scan all srcs
        topdcl = GetTopDclFromRegRegion(src);
        LocalLiveRange *topdclLR = nullptr;

        if (topdcl && (topdclLR = gra.getLocalLR(topdcl))) {
          lr = topdclLR;

          // Check whether local LR is a candidate
          if (lr->isLiveRangeLocal() && lr->isGRFRegAssigned() == false) {
            unsigned int startIdx;

            if (lr->getFirstRef(startIdx) == NULL) {
              // Skip this lr and update its ref count,
              // So it will be treated as a global LR
              lr->recordRef(NULL);
              continue;
            }

            vISA_ASSERT(idx > 0,
                        "Candidate use found in first inst of basic block");

            if ((builder.WaDisableSendSrcDstOverlap() &&
                 ((curInst->isSend() && i == 0) ||
                  (curInst->isSplitSend() && i == 1))) ||
                (curInst->isDpas() &&
                 i == 1) // For DPAS, as part of same instruction, src1 should
                         // not have overlap with dst. Src0 and src2 are okay to
                         // have overlap
                || (builder.avoidDstSrcOverlap() && curInst->getDst() != NULL &&
                    hasDstSrcOverlapPotential(curInst->getDst(),
                                              src->asSrcRegRegion()))) {
#ifdef DEBUG_VERBOSE_ON
              if (curInst->getDst() != NULL &&
                  hasDstSrcOverlapPotential(curInst->getDst(),
                                            src->asSrcRegRegion())) {
                curInst->dump();
              }
#endif
              lr->setLastRef(curInst, idx + 1);
            } else {
              lr->setLastRef(curInst, idx);
            }
          }
        }
      }
    }

    if (G4_Inst_Table[curInst->opcode()].n_dst == 1 ||
        curInst->isSplitIntrinsic()) {
      // Scan dst
      G4_DstRegRegion *dst = curInst->getDst();

      if (dst) {
        topdcl = GetTopDclFromRegRegion(dst);

        if (topdcl && (lr = gra.getLocalLR(topdcl))) {
          // Check whether local LR is a candidate
          if (lr->isLiveRangeLocal() && lr->isGRFRegAssigned() == false) {
            unsigned int startIdx;

            if (lr->getFirstRef(startIdx) == NULL) {
              lr->setFirstRef(curInst, idx);
              liveIntervals.push_back(lr);
            }

            lr->setLastRef(curInst, idx);
          }
        }
      }
    }
  }
}

void LocalRA::printAddressTakenDecls() {
  DEBUG_VERBOSE("Address taken decls:"
                << "\n");

  for (DECLARE_LIST_ITER dcl_it = kernel.Declares.begin();
       dcl_it != kernel.Declares.end(); dcl_it++) {
    G4_Declare *curDcl = (*dcl_it);

    if (curDcl->getAliasDeclare() != NULL) {
      vISA_ASSERT(gra.getLocalLR(curDcl) == NULL,
                  "Local LR found for alias declare");
      continue;
    }

    if (gra.getLocalLR(curDcl) &&
        gra.getLocalLR(curDcl)->hasIndirectAccess() == true) {
      DEBUG_VERBOSE(curDcl->getName() << ", ");
    }
  }

  DEBUG_VERBOSE("\n");
}

void LocalRA::printLocalRACandidates() {
  DEBUG_VERBOSE("Local RA candidates:"
                << "\n");

  for (DECLARE_LIST_ITER dcl_it = kernel.Declares.begin();
       dcl_it != kernel.Declares.end(); dcl_it++) {
    G4_Declare *curDcl = (*dcl_it);

    if (curDcl->getAliasDeclare() != NULL) {
      vISA_ASSERT(gra.getLocalLR(curDcl) == NULL,
                  "Local LR found for alias declare");
      continue;
    }

    if (gra.getLocalLR(curDcl) && gra.getLocalLR(curDcl)->isLiveRangeLocal() &&
        gra.getLocalLR(curDcl)->isGRFRegAssigned() == false) {
      DEBUG_VERBOSE(curDcl->getName() << ", ");
    }
  }

  DEBUG_VERBOSE("\n");
}

void LocalRA::printLocalLiveIntervals(
    G4_BB *bb, std::vector<LocalLiveRange *> &liveIntervals) {
  DEBUG_VERBOSE("Live intervals for bb " << bb->getId() << "\n");

  for (auto lr : liveIntervals) {
    unsigned int start, end;

    lr->getFirstRef(start);
    lr->getLastRef(end);

    DEBUG_VERBOSE(lr->getTopDcl()->getName()
                  << "(" << start << ", " << end << ", "
                  << lr->getTopDcl()->getByteSize() << ")");
    DEBUG_VERBOSE("\n");
  }

  DEBUG_VERBOSE("\n");
}

void LocalRA::printInputLiveIntervals() {
  DEBUG_VERBOSE("\n"
                << "Input Live intervals "
                << "\n");

  for (auto it = inputIntervals.begin(); it != inputIntervals.end(); it++) {
    [[maybe_unused]] unsigned int regWordIdx, lrEndIdx, regNum, subRegInWord;

    InputLiveRange lr = *it;

    regWordIdx = lr.getRegWordIdx();
    regNum = regWordIdx / kernel.numEltPerGRF<Type_UW>();
    subRegInWord = regWordIdx % kernel.numEltPerGRF<Type_UW>();
    lrEndIdx = lr.getLrEndIdx();

    DEBUG_VERBOSE("r" << regNum << "." << subRegInWord << " " << lrEndIdx);
    DEBUG_VERBOSE("\n");
  }

  DEBUG_VERBOSE("\n");
}

void LocalRA::countLocalLiveIntervals(
    std::vector<LocalLiveRange *> &liveIntervals, unsigned grfSize) {
  unsigned int numScalars = 0, numHalfGRF = 0, numOneGRF = 0,
               numTwoOrMoreGRF = 0, numTotal = 0;

  for (auto lr : liveIntervals) {
    unsigned size, numrows;
    G4_Declare *dcl = lr->getTopDcl();

    numrows = dcl->getNumRows();
    size = dcl->getNumElems() * dcl->getElemSize();
    numTotal++;

    if (numrows > 1) {
      numTwoOrMoreGRF++;
    } else {
      if (dcl->getNumElems() > 1 && size > (grfSize / 2u) && size <= grfSize)
        numOneGRF++;
      else if (dcl->getNumElems() > 1 && size <= (grfSize / 2u))
        numHalfGRF++;
      else if (dcl->getNumElems() == 1)
        numScalars++;
      else
        vISA_ASSERT_UNREACHABLE("Unknown size");
    }
  }

  printLocalLiveIntervalDistribution(numScalars, numHalfGRF, numOneGRF,
                                     numTwoOrMoreGRF, numTotal);
}

void LocalRA::printLocalLiveIntervalDistribution(unsigned int numScalars,
                                                 unsigned int numHalfGRF,
                                                 unsigned int numOneGRF,
                                                 unsigned int numTwoOrMoreGRF,
                                                 unsigned int numTotal) {
  DEBUG_VERBOSE("In units of num of live ranges:"
                << "\n");
  DEBUG_VERBOSE("Total local live ranges: " << numTotal << "\n");
  DEBUG_VERBOSE("2 GRFs or more: " << numTwoOrMoreGRF << "\n");
  DEBUG_VERBOSE("1 GRF: " << numOneGRF << "\n");
  DEBUG_VERBOSE("Half GRF: " << numHalfGRF << "\n");
  DEBUG_VERBOSE("Scalar: " << numScalars << "\n");
  DEBUG_VERBOSE("\n");
}

// return false if roundrobin RA should be disabled
bool LocalRA::countLiveIntervals() {
  int globalRows = 0;
  uint32_t localRows = 0;
  for (auto curDcl : kernel.Declares) {
    LocalLiveRange *curDclLR = nullptr;

    if (curDcl->getAliasDeclare() == NULL && curDcl->getRegFile() == G4_GRF &&
        (curDclLR = gra.getLocalLR(curDcl)) &&
        curDclLR->isGRFRegAssigned() == false) {
      if (curDclLR->isLiveRangeGlobal()) {
        globalRows += curDcl->getNumRows();
      } else if (curDclLR->isLiveRangeLocal()) {
        localRows += curDcl->getNumRows();
      }
    }
  }

  int scaledUniqueAssign = kernel.getScaledGRFSize(NUM_PREGS_FOR_UNIQUE_ASSIGN);

  if (globalRows <= scaledUniqueAssign) {
    globalLRSize = globalRows;
  } else {
    if (localRows < (numRegLRA - scaledUniqueAssign)) {
      globalLRSize = scaledUniqueAssign;
    } else {
      return false;
    }
  }

  return true;
}

// ********* LocalLiveRange class implementation *********
void LocalLiveRange::recordRef(G4_BB *bb) {
  if (bb != prevBBRef)
    numRefsInFG++;

  prevBBRef = bb;
}

bool LocalLiveRange::isLiveRangeLocal() const {
  if (isIndirectAccess == false && numRefsInFG == 1 && eot == false &&
      !builder.isPreDefArg(topdcl) && !builder.isPreDefRet(topdcl) &&
      topdcl->isOutput() == false) {
    return true;
  }

  return false;
}

bool LocalLiveRange::isLiveRangeGlobal() {
  if (isIndirectAccess == true || (numRefsInFG > 1 && eot == false) ||
      topdcl->isOutput() == true) {
    return true;
  }

  return false;
}

bool LocalLiveRange::isGRFRegAssigned() {
  vISA_ASSERT(topdcl != NULL, "Top dcl not set");
  G4_RegVar *rvar = topdcl->getRegVar();
  bool isPhyRegAssigned = false;

  if (rvar) {
    if (rvar->isPhyRegAssigned())
      isPhyRegAssigned = true;
  }

  return isPhyRegAssigned;
}

unsigned int LocalLiveRange::getSizeInWords() {
  int nrows = getTopDcl()->getNumRows();
  int elemsize = getTopDcl()->getElemSize();
  int nelems = getTopDcl()->getNumElems();
  int words = 0;

  if (nrows > 1) {
    // If sizeInWords is set, use it otherwise consider entire row reserved
    unsigned int sizeInWords = getTopDcl()->getWordSize();

    if (sizeInWords > 0)
      words = sizeInWords;
    else
      words = nrows * builder.numEltPerGRF<Type_UW>();
  } else if (nrows == 1) {
    int nbytesperword = 2;
    words = (nelems * elemsize + 1) / nbytesperword;
  }

  return words;
}

// ********* PhyRegsLocalRA class implementation *********
void PhyRegsLocalRA::setGRFBusy(int which) {
  vISA_ASSERT(isGRFAvailable(which), "Invalid register");

  // all 1 word mask based on register size
  uint64_t wordMask = (1ULL << (builder.getGRFSize() / 2)) - 1;
  regBusyVector[which] = (uint32_t)wordMask;

  if (twoBanksRA) {
    if (which < SECOND_HALF_BANK_START_GRF) {
      bank1AvailableRegNum--;
    } else {
      bank2AvailableRegNum--;
    }
  }
}

// ********* PhyRegsLocalRA class implementation *********
void PhyRegsLocalRA::setGRFBusy(int which, int howmany) {
  for (int i = 0; i < howmany; i++) {
    setGRFBusy(which + i);
  }
}

void PhyRegsLocalRA::setWordBusy(int whichgrf, int word) {
  vISA_ASSERT(isGRFAvailable(whichgrf), "Invalid register");
  vISA_ASSERT(word < (int)builder.numEltPerGRF<Type_UW>(), "Invalid word");

  if (twoBanksRA) {
    if (regBusyVector[whichgrf] == 0) {
      if (whichgrf < SECOND_HALF_BANK_START_GRF) {
        bank1AvailableRegNum--;
      } else {
        bank2AvailableRegNum--;
      }
    }
  }

  regBusyVector[whichgrf] |= (WORD_BUSY << word);
}

void PhyRegsLocalRA::setWordBusy(int whichgrf, int word, int howmany) {
  for (int i = 0; i < howmany; i++) {
    setWordBusy(whichgrf, word + i);
  }
}

void PhyRegsLocalRA::setGRFNotBusy(int which, int instID) {
  vISA_ASSERT(isGRFAvailable(which), "Invalid register");

  regBusyVector[which] = 0;

  if (twoBanksRA) {
    if (which < SECOND_HALF_BANK_START_GRF) {
      lastUseSum1 -= regLastUse[which];
      lastUseSum1 += instID;
      bank1AvailableRegNum++;
    } else {
      lastUseSum2 -= regLastUse[which];
      lastUseSum2 += instID;
      bank2AvailableRegNum++;
    }
  }
  if (instID) {
    regLastUse[which] = instID;
  }
}

void PhyRegsLocalRA::setWordNotBusy(int whichgrf, int word, int instID) {
  vISA_ASSERT(isGRFAvailable(whichgrf), "Invalid register");
  vISA_ASSERT(word < (int)builder.numEltPerGRF<Type_UW>(), "Invalid word");

  if (twoBanksRA) {
    if (whichgrf < SECOND_HALF_BANK_START_GRF) {
      lastUseSum1 -= regLastUse[whichgrf];
      lastUseSum1 += instID;
      if (regBusyVector[whichgrf] == 0) {
        bank1AvailableRegNum++;
      }
    } else {
      lastUseSum2 -= regLastUse[whichgrf];
      lastUseSum2 += instID;
      if (regBusyVector[whichgrf] == 0) {
        bank2AvailableRegNum++;
      }
    }
  }
  uint32_t mask = ~(1 << word);
  regBusyVector[whichgrf] &= mask;
  if (instID) {
    regLastUse[whichgrf] = instID;
  }
}

inline bool PhyRegsLocalRA::isWordBusy(int whichgrf, int word) {
  vISA_ASSERT(isGRFAvailable(whichgrf), "Invalid register");

  vISA_ASSERT(word < (int)builder.numEltPerGRF<Type_UW>(), "Invalid word");
  bool isBusy = ((regBusyVector[whichgrf] & (WORD_BUSY << word)) != 0);
  return isBusy;
}

bool PhyRegsLocalRA::isWordBusy(int whichgrf, int word, int howmany) {
  bool retval = false;

  for (int i = 0; i < howmany && !retval; i++) {
    retval |= isWordBusy(whichgrf, word + i);
  }

  return retval;
}

bool PhyRegsLocalRA::findFreeMultipleRegsForward(
    int regIdx, BankAlign align, int &regnum, int nrows, int lastRowSize,
    int endReg, unsigned short occupiedBundles, int instID, bool isHybridAlloc,
    std::unordered_set<unsigned int> &forbidden) {
  int foundItem = 0;
  int startReg = 0;
  int i = regIdx;
  int grfRows = 0;
  bool multiSteps = nrows > 1;

  if (lastRowSize % builder.numEltPerGRF<Type_UW>() == 0) {
    grfRows = nrows;
  } else {
    grfRows = nrows - 1;
  }

  findRegisterCandiateWithAlignForward(i, align, multiSteps);
  i = findBundleConflictFreeRegister(i, endReg, occupiedBundles, align,
                                     multiSteps);

  startReg = i;
  while (i <= endReg + nrows - 1) {
    if (isGRFAvailable(i) && forbidden.find(i) == forbidden.end() &&
        regBusyVector[i] == 0 &&
        (!isHybridAlloc ||
         (((instID - regLastUse[i]) / 2 >= LraFFWindowSize) ||
          (regLastUse[i] == 0)))) {
      foundItem++;
    } else if (foundItem < grfRows) {
      foundItem = 0;
      i++;
      findRegisterCandiateWithAlignForward(i, align, multiSteps);
      i = findBundleConflictFreeRegister(i, endReg, occupiedBundles, align,
                                         multiSteps);
      startReg = i;
      continue;
    }

    if (foundItem == grfRows) {
      if (lastRowSize % builder.numEltPerGRF<Type_UW>() == 0) {
        regnum = startReg;
        return true;
      } else {
        if (i + 1 <= endReg + nrows - 1 && isGRFAvailable(i + 1) &&
            forbidden.find(i + 1) == forbidden.end() &&
            (isWordBusy(i + 1, 0, lastRowSize) == false) &&
            (!isHybridAlloc ||
             (((instID - regLastUse[i + 1]) / 2 >= LraFFWindowSize) ||
              (regLastUse[i + 1] == 0)))) {
          regnum = startReg;
          return true;
        } else {
          foundItem = 0;
          i++;
          findRegisterCandiateWithAlignForward(i, align, multiSteps);
          i = findBundleConflictFreeRegister(i, endReg, occupiedBundles, align,
                                             multiSteps);
          startReg = i;
          continue;
        }
      }
    }

    i++;
  }

  return false;
}

bool PhyRegsLocalRA::findFreeMultipleRegsBackward(
    int regIdx, BankAlign align, int &regnum, int nrows, int lastRowSize,
    int endReg, int instID, bool isHybridAlloc,
    std::unordered_set<unsigned int> &forbidden) {
  int foundItem = 0;
  int startReg = 0;
  int grfRows = 0;
  int i = regIdx;
  bool multiSteps = nrows > 1;

  if (lastRowSize % builder.numEltPerGRF<Type_UW>() == 0) {
    grfRows = nrows;
  } else {
    grfRows = nrows - 1;
  }

  findRegisterCandiateWithAlignBackward(i, align, multiSteps);

  startReg = i;

  while (i >= endReg && i >= 0) {
    if (isGRFAvailable(i) && forbidden.find(i) == forbidden.end() &&
        regBusyVector[i] == 0 &&
        (!isHybridAlloc || (((instID - regLastUse[i]) / 2 >= LraFFWindowSize) ||
                            (regLastUse[i] == 0)))) {
      foundItem++;
    } else if (foundItem < grfRows) {
      foundItem = 0;
      i -= nrows;
      findRegisterCandiateWithAlignBackward(i, align, multiSteps);

      startReg = i;
      continue;
    }

    if (foundItem == grfRows) {
      if (lastRowSize % builder.numEltPerGRF<Type_UW>() == 0) {
        regnum = startReg;
        return true;
      } else {
        if (i + 1 <= endReg && isGRFAvailable(i + 1) &&
            forbidden.find(i + 1) == forbidden.end() &&
            (isWordBusy(i + 1, 0, lastRowSize) == false) &&
            (!isHybridAlloc ||
             (((instID - regLastUse[i + 1]) / 2 >= LraFFWindowSize) ||
              (regLastUse[i + 1] == 0)))) {
          regnum = startReg;
          return true;
        } else {
          foundItem = 0;
          i -= nrows;
          findRegisterCandiateWithAlignBackward(i, align, multiSteps);

          startReg = i;
          continue;
        }
      }
    }

    i++;
  }

  return false;
}

bool PhyRegsLocalRA::findFreeSingleReg(
    int regIdx, int size, BankAlign align, G4_SubReg_Align subalign,
    int &regnum, int &subregnum, int endReg, int instID, bool isHybridAlloc,
    bool forward, std::unordered_set<unsigned int> &forbidden) {
  int i = regIdx;
  bool found = false;

  while (!found) {
    if (forward) {
      if (i > endReg) //<= works
        break;
    } else {
      if (i < endReg) //>= works
        break;
    }

    // Align GRF
    if ((align == BankAlign::Even) && (i % 2 != 0)) {
      i += forward ? 1 : -1;
      continue;
    } else if ((align == BankAlign::Odd) && (i % 2 == 0)) {
      i += forward ? 1 : -1;
      continue;
    } else if ((align == BankAlign::QuadGRF) && (i % 4 != 0)) {
      i += forward ? (4 - (i % 4)) : -(i % 4);
      continue;
    } else if ((align == BankAlign::Even2GRF) && ((i % 4 >= 2))) {
      i += forward ? 1 : -1;
      continue;
    } else if ((align == BankAlign::Odd2GRF) && ((i % 4 < 2))) {
      i += forward ? 1 : -1;
      continue;
    }

    if (isGRFAvailable(i, 1) && forbidden.find(i) == forbidden.end() &&
        (!isHybridAlloc || (((instID - regLastUse[i]) / 2 >= LraFFWindowSize) ||
                            (regLastUse[i] == 0)))) {
      found = findFreeSingleReg(i, subalign, regnum, subregnum, size);
      if (found) {
        return true;
      }
    }
    i += forward ? 1 : -1;
  }

  return false;
}

void PhyRegsLocalRA::printBusyRegs() {
  for (int i = 0; i < (int)numRegs; i++) {
    if (isGRFAvailable(i) == false)
      continue;

    for (int j = 0; j < (int)builder.numEltPerGRF<Type_UW>(); j++) {
      if (isWordBusy(i, j) == true) {
        DEBUG_VERBOSE("r" << i << "." << j << ":w, "
                          << "\n");
      }
    }
  }
}

// Based on RegVar, mark available registers as busy
void PhyRegsLocalRA::markPhyRegs(G4_Declare *topdcl) {
  G4_RegVar *rvar = topdcl->getRegVar();
  unsigned numrows = topdcl->getNumRows();
  unsigned numwords = 0;
  unsigned int regnum = 0;

  if (!rvar->getPhyReg() || !rvar->isGreg()) {
    return;
  }
  // Calculate number of physical registers required by this dcl
  if (numrows == 1) {
    unsigned numbytes = topdcl->getElemSize() * topdcl->getNumElems();
    numwords = (numbytes + 1) / 2;
    unsigned int subReg = rvar->getPhyRegOff();
    unsigned int subRegInWord = subReg * rvar->getDeclare()->getElemSize() / 2;

    regnum = rvar->getPhyReg()->asGreg()->getRegNum();
    if (isGRFAvailable(regnum) == true) {
      for (unsigned int i = 0; i < numwords; i++) {
        // In case across one GRF
        if (subRegInWord + i >= builder.numEltPerGRF<Type_UW>()) {
          regnum++;
          if (!isGRFAvailable(regnum)) {
            break;
          }
          numwords -= i;
          i = 0;
          subRegInWord = 0;
        }

        // Starting from first word, mark each consecutive word busy
        setWordBusy(regnum, (subRegInWord + i));
      }
    }
  } else {
    regnum = rvar->getPhyReg()->asGreg()->getRegNum();
    for (unsigned int i = 0; i < topdcl->getNumRows(); i++) {
      if (isGRFAvailable(i + regnum) == true) {
        // Set entire GRF busy
        setGRFBusy(regnum + i);
      }
    }
  }
}

// ********* PhyRegsManager class implementation *********
bool PhyRegsLocalRA::findFreeSingleReg(int regIdx, G4_SubReg_Align subalign,
                                       int &regnum, int &subregnum, int size) {
  bool found = false;
  if (subalign == builder.getGRFAlign()) {
    if (isWordBusy(regIdx, 0, size) == false) {
      subregnum = 0;
      found = true;
    }
  } else if (subalign == builder.getHalfGRFAlign()) {
    if (isWordBusy(regIdx, 0, size) == false) {
      subregnum = 0;
      found = true;
    } else if (size <= (int)builder.numEltPerGRF<Type_UW>() / 2 &&
               isWordBusy(regIdx, builder.numEltPerGRF<Type_UW>() / 2, size) ==
                   false) {
      subregnum = builder.numEltPerGRF<Type_UW>() / 2;
      found = true;
    }
  } else {
    // ToDo: check if dynamic step size has compile time impact
    int step = 1;
    int upBound = builder.numEltPerGRF<Type_UW>() - size + 1;
    switch (subalign) {
    case Eight_Word:
      step = 8;
      break;
    case Four_Word:
      step = 4;
      break;
    case Even_Word:
      step = 2;
      break;
    case Any:
      upBound = builder.numEltPerGRF<Type_UW>() -
                size; // FIXME, why not the last word
      step = 1;
      break;
    default:
      vISA_ASSERT_UNREACHABLE("unexpected alignment");
    }
    for (int j = 0; j < upBound; j += step) {
      if (!isWordBusy(regIdx, j, size)) {
        subregnum = j;
        found = true;
        break;
      }
    }
  }

  if (found) {
    regnum = regIdx;
  }

  return found;
}

int PhyRegsManager::findFreeRegs(int size, BankAlign align,
                                 G4_SubReg_Align subalign, int &regnum,
                                 int &subregnum, int startRegNum, int endRegNum,
                                 unsigned short occupiedBundles,
                                 unsigned int instID, bool isHybridAlloc,
                                 std::unordered_set<unsigned int> &forbidden) {
  int nrows = 0;
  int lastRowSize = 0;
  LocalRA::getRowInfo(size, nrows, lastRowSize, builder);

  bool forward = (startRegNum <= endRegNum ? true : false);
  int startReg = forward ? startRegNum : startRegNum - nrows + 1;
  int endReg = forward ? endRegNum - nrows + 1 : endRegNum;

  bool found = false;

  if (size >= (int)builder.numEltPerGRF<Type_UW>()) {
    if (forward) {
      found = availableRegs.findFreeMultipleRegsForward(
          startReg, align, regnum, nrows, lastRowSize, endReg, occupiedBundles,
          instID, isHybridAlloc, forbidden);
    } else {
      found = availableRegs.findFreeMultipleRegsBackward(
          startReg, align, regnum, nrows, lastRowSize, endReg, instID,
          isHybridAlloc, forbidden);
    }
    if (found) {
      subregnum = 0;
      if (size % builder.numEltPerGRF<Type_UW>() == 0) {
        availableRegs.setGRFBusy(regnum, nrows);
      } else {
        availableRegs.setGRFBusy(regnum, nrows - 1);
        availableRegs.setWordBusy(regnum + nrows - 1, 0, lastRowSize);
      }
    }
  } else {
    found = availableRegs.findFreeSingleReg(startReg, size, align, subalign,
                                            regnum, subregnum, endReg, instID,
                                            isHybridAlloc, forward, forbidden);
    if (found) {
      availableRegs.setWordBusy(regnum, subregnum, size);
    }
  }

  if (found) {
    return nrows;
  }

  return 0;
}

// subregnum parameter is expected to be in units of word
void PhyRegsManager::freeRegs(int regnum, int subregnum, int numwords,
                              int instID) {
  while (numwords >= (int)builder.numEltPerGRF<Type_UW>()) {
    availableRegs.setGRFNotBusy(regnum, instID);
    numwords -= builder.numEltPerGRF<Type_UW>();
    regnum++;
  }

  while (numwords >= 1) {
    availableRegs.setWordNotBusy(regnum, subregnum, instID);
    subregnum++;

    if (subregnum >= (int)builder.numEltPerGRF<Type_UW>()) {
      subregnum = 0;
      regnum++;
    }
    numwords--;
  }
}

// ********* LinearScan class implementation *********

LinearScan::LinearScan(GlobalRA &g,
                       std::vector<LocalLiveRange *> &localLiveIntervals,
                       std::list<InputLiveRange> &inputLivelIntervals,
                       PhyRegsManager &pregMgr, PhyRegsLocalRA &pregs,
                       PhyRegSummary *s, unsigned int numReg, unsigned int glrs,
                       bool roundRobin, bool bankConflict,
                       bool internalConflict, bool splitLLR, unsigned int simdS)
    : gra(g), builder(g.builder), pregManager(pregMgr), initPregs(pregs),
      liveIntervals(localLiveIntervals), inputIntervals(inputLivelIntervals),
      summary(s),
      pregs(g.kernel.getNumRegTotal() * g.kernel.numEltPerGRF<Type_UW>(),
            false),
      simdSize(simdS), globalLRSize(glrs), numRegLRA(numReg),
      useRoundRobin(roundRobin), doBankConflict(bankConflict),
      highInternalConflict(internalConflict), doSplitLLR(splitLLR) {

  // FIXME: this entire class is a mess, whole thing needs a rewrite
  if (g.kernel.getNumRegTotal() < 128) {
    // code below can segfault if user specifies #GRF < 128. We just hack it so
    // bank1 == bank2
    bank1StartGRFReg = bank1_start = 0;
    bank2StartGRFReg = bank2_start = 0;
    bank1_end = bank2_end = numRegLRA - 1;
    startGRFReg = &bank1StartGRFReg;
    return;
  }

  // register number boundaries
  bank1_start = 0;
  bank1_end = SECOND_HALF_BANK_START_GRF - globalLRSize / 2 - 1;
  if (useRoundRobin) { // From middle to back
    bank2_start = SECOND_HALF_BANK_START_GRF + (globalLRSize + 1) / 2;
    bank2_end = numRegLRA - 1;
  } else { // From back to middle
    bank2_start = numRegLRA - 1;
    bank2_end = SECOND_HALF_BANK_START_GRF + (globalLRSize + 1) / 2;
  }

  // register number pointers
  bank1StartGRFReg = bank1_start;
  bank2StartGRFReg = bank2_start;

  // register pointer
  startGRFReg = &bank1StartGRFReg;

  int bank1AvailableRegNum = 0;
  for (int i = 0; i < SECOND_HALF_BANK_START_GRF; i++) {
    if (pregManager.getAvailableRegs()->isGRFAvailable(i) &&
        !pregManager.getAvailableRegs()->isGRFBusy(i)) {
      bank1AvailableRegNum++;
    }
  }
  pregManager.getAvailableRegs()->setBank1AvailableRegNum(bank1AvailableRegNum);

  int bank2AvailableRegNum = 0;
  for (unsigned int i = SECOND_HALF_BANK_START_GRF; i < numRegLRA; i++) {
    if (pregManager.getAvailableRegs()->isGRFAvailable(i) &&
        !pregManager.getAvailableRegs()->isGRFBusy(i)) {
      bank2AvailableRegNum++;
    }
  }
  pregManager.getAvailableRegs()->setBank2AvailableRegNum(bank2AvailableRegNum);
}

// Linear scan implementation
void LinearScan::run(G4_BB *bb, IR_Builder &builder, LLR_USE_MAP &LLRUseMap) {
  unsigned int idx = 0;
  bool allocateRegResult = false;

  unsigned int firstLocalIdx = 0, firstGlobalIdx = 0;
  if (liveIntervals.size() > 0) {
    LocalLiveRange *firstLr = (*liveIntervals.begin());
    G4_INST *firstInst = firstLr->getFirstRef(firstLocalIdx);
    firstGlobalIdx = firstInst->getLexicalId();
  }

  for (auto lr : liveIntervals) {
    G4_INST *currInst = lr->getFirstRef(idx);

    expireRanges(idx);
    expireInputRanges(currInst->getLexicalId(), idx, firstGlobalIdx);

    if (doBankConflict && builder.lowHighBundle() &&
        (highInternalConflict ||
         (simdSize >= 16 && builder.oneGRFBankDivision()))) {
      allocateRegResult = allocateRegsFromBanks(lr);
    } else {
      allocateRegResult = allocateRegs(lr, bb, builder, LLRUseMap);
    }

    if (allocateRegResult) {
      updateActiveList(lr);
#ifdef _DEBUG
      int startregnum, endregnum, startsregnum, endsregnum;
      G4_VarBase *op;
      op = lr->getPhyReg(startsregnum);

      startregnum = endregnum = op->asGreg()->getRegNum();
      endsregnum = startsregnum +
                   (lr->getTopDcl()->getNumElems() *
                    lr->getTopDcl()->getElemSize() / 2) -
                   1;

      vISA_ASSERT(((unsigned int)startregnum + lr->getTopDcl()->getNumRows() -
                   1) < numRegLRA,
                  "Linear scan allocated unavailable physical GRF");

      if (lr->getTopDcl()->getNumRows() > 1) {
        endregnum = startregnum + lr->getTopDcl()->getNumRows() - 1;

        if (lr->getTopDcl()->getWordSize() > 0) {
          endsregnum =
              lr->getTopDcl()->getWordSize() % builder.numEltPerGRF<Type_UW>() -
              1;
          if (endsregnum < 0)
            endsregnum = 15;
        } else
          endsregnum = 15; // last word in GRF
      }
#ifdef DEBUG_VERBOSE_ON
      DEBUG_VERBOSE("Assigned physical register to "
                    << lr->getTopDcl()->getName() << " (r" << startregnum << "."
                    << startsregnum << ":w - "
                    << "r" << endregnum << "." << endsregnum << ":w)"
                    << "\n");
#endif
#endif
    }
  }

  expireAllActive();
}

void LinearScan::updateActiveList(LocalLiveRange *lr) {
  bool done = false;
  unsigned int newlr_end;

  lr->getLastRef(newlr_end);

  for (auto active_it = active.begin(); active_it != active.end();
       active_it++) {
    unsigned int end_idx;
    LocalLiveRange *active_lr = (*active_it);

    active_lr->getLastRef(end_idx);

    if (end_idx > newlr_end) {
      active.insert(active_it, lr);
      done = true;
      break;
    }
  }

  if (done == false)
    active.push_back(lr);
}

void LinearScan::expireAllActive() {
  if (active.size() > 0) {
    // Expire any remaining ranges
    LocalLiveRange *lastActive = active.back();
    unsigned int endIdx;

    lastActive->getLastRef(endIdx);

    expireRanges(endIdx);
  }
}

// idx is the current position being processed
// The function will expire active ranges that end at or before idx
void LinearScan::expireRanges(unsigned int idx) {
  // active list is sorted in ascending order of starting index

  while (active.size() > 0) {
    unsigned int endIdx;
    LocalLiveRange *lr = active.front();

    lr->getLastRef(endIdx);

    if (endIdx <= idx) {
      G4_VarBase *preg;
      int subregnumword, subregnum;

      preg = lr->getPhyReg(subregnumword);

      if (preg) {
        subregnum =
            LocalRA::convertSubRegOffFromWords(lr->getTopDcl(), subregnumword);

        // Mark the RegVar object of dcl as assigned to physical register
        lr->getTopDcl()->getRegVar()->setPhyReg(preg, subregnum);
        lr->setAssigned(true);

        if (summary) {
          // mark physical register as used atleast once
          summary->markPhyRegs(preg, lr->getSizeInWords());
        }
      }

      // Free physical regs marked for this range
      freeAllocedRegs(lr, true);

#ifdef DEBUG_VERBOSE_ON
      DEBUG_VERBOSE("Expiring range " << lr->getTopDcl()->getName() << "\n");
#endif

      // Remove range from active list
      active.pop_front();
    } else {
      // As soon as we find first range that ends after ids break loop
      break;
    }
  }
}

void LinearScan::expireInputRanges(unsigned int global_idx,
                                   unsigned int local_idx,
                                   unsigned int first_idx) {
  while (inputIntervals.size() > 0) {
    InputLiveRange lr = inputIntervals.front();
    unsigned int endIdx = lr.getLrEndIdx();

    if (endIdx <= global_idx) {
      unsigned int regnum =
          lr.getRegWordIdx() / builder.numEltPerGRF<Type_UW>();
      unsigned int subRegInWord =
          lr.getRegWordIdx() % builder.numEltPerGRF<Type_UW>();
      int inputIdx =
          (endIdx < first_idx) ? 0 : (local_idx - (global_idx - endIdx) * 2);

      // Free physical regs marked for this range
      pregManager.freeRegs(regnum, subRegInWord, 1, inputIdx);
      initPregs.setWordNotBusy(regnum, subRegInWord, 0);

#ifdef DEBUG_VERBOSE_ON
      DEBUG_VERBOSE("Expiring input r" << regnum << "." << subRegInWord
                                       << "\n");
#endif

      // Remove range from inputIntervals list
      inputIntervals.pop_front();
    } else {
      // As soon as we find first range that ends after ids break loop
      break;
    }
  }
}

// Allocate registers to live range. It makes a decision whether to spill
// a currently active range or the range passed as parameter. The range
// that has larger size and is longer is the spill candidate.
// Return true if free registers found, false if range is to be spilled
bool LinearScan::allocateRegs(LocalLiveRange *lr, G4_BB *bb,
                              IR_Builder &builder, LLR_USE_MAP &LLRUseMap) {
  int regnum, subregnum;
  unsigned int localRABound = 0;
  unsigned int instID;

  G4_INST *currInst = lr->getFirstRef(instID);

  // Let local RA allocate only those ranges that need < 10 GRFs
  // Larger ranges are not many and are best left to global RA
  // as it can make a better judgment by considering the
  // spill cost.
  int nrows = 0;
  int size = lr->getSizeInWords();
  G4_Declare *dcl = lr->getTopDcl();

  G4_SubReg_Align subalign = gra.getSubRegAlign(dcl);
  BankAlign preBank = BankAlign::Either;
  unsigned short occupiedBundles =
      getOccupiedBundle(builder, gra, dcl, preBank);
  localRABound = numRegLRA - globalLRSize -
                 1; //-1, localRABound will be counted in findFreeRegs()
  BankAlign bankAlign = gra.isQuadAligned(dcl)   ? BankAlign::QuadGRF
                        : gra.isEvenAligned(dcl) ? BankAlign::Even
                                                 : BankAlign::Either;
  if (bankAlign == BankAlign::Either && doBankConflict) {
    bankAlign = gra.getBankAlign(dcl);
  } else {
    if (bankAlign == BankAlign::Either) {
      bankAlign = preBank;
    }
  }

  if (useRoundRobin) {
    nrows = pregManager.findFreeRegs(
        size, bankAlign, subalign, regnum, subregnum, *startGRFReg,
        localRABound, occupiedBundles, instID, false, lr->getForbidden());
  } else {
    nrows = pregManager.findFreeRegs(
        size, bankAlign, subalign, regnum, subregnum, *startGRFReg,
        localRABound, occupiedBundles, instID, true, lr->getForbidden());

    if (!nrows) {
      nrows = pregManager.findFreeRegs(
          size, bankAlign, subalign, regnum, subregnum, *startGRFReg,
          localRABound, occupiedBundles, instID, false, lr->getForbidden());
    }
  }

  if (useRoundRobin) {
    if (nrows) {
      *startGRFReg = (regnum + nrows) % (localRABound);
    } else {
      unsigned int endGRFReg = *startGRFReg + nrows;
      endGRFReg = (endGRFReg > localRABound) ? localRABound : endGRFReg;

      nrows = pregManager.findFreeRegs(size, bankAlign, subalign, regnum,
                                       subregnum, 0, endGRFReg, occupiedBundles,
                                       instID, false, lr->getForbidden());

      if (nrows) {
        *startGRFReg = (regnum + nrows) % (localRABound);
      }
    }
  }

  // If allocations fails, return false
  if (!nrows) {
    // Check if any range in active is longer and wider than lr.
    // Spill it if it is and call self again.
    for (auto active_it = active.rbegin(); active_it != active.rend();
         active_it++) {
      LocalLiveRange *activeLR = (*active_it);

      if (activeLR->getSizeInWords() >= lr->getSizeInWords() &&
          (!doSplitLLR ||
           gra.getNumRefs(activeLR->getTopDcl()) > SPLIT_REF_CNT_THRESHOLD)) {
        // Another active range is larger than lr
        unsigned int active_lr_end, lr_end;

        activeLR->getLastRef(active_lr_end);
        lr->getLastRef(lr_end);

        if (active_lr_end > lr_end) {
          // Range in active is longer, so spill it
#ifdef DEBUG_VERBOSE_ON
          DEBUG_VERBOSE("Spilling range " << activeLR->getTopDcl()->getName()
                                          << "\n");
#endif

          freeAllocedRegs(activeLR, false);
          active.erase(--(active_it.base()));

          if (doSplitLLR) {
            G4_Declare *oldDcl = activeLR->getTopDcl();
            unsigned short totalElems = oldDcl->getTotalElems();

            std::vector<std::pair<INST_LIST_ITER, unsigned int>> *useList =
                nullptr;
            auto Iter = LLRUseMap.find(activeLR);
            if (Iter != LLRUseMap.end())
              useList = &(Iter->second);

            if (useList && useList->size() > 2 &&
                gra.getNumRefs(oldDcl) - useList->size() == 1 &&
                (totalElems == 8 || totalElems == 16 || totalElems == 32)) {
              bool split = false;
              unsigned int useCnt = 0;
              INST_LIST_ITER lastUseIt;
              G4_Declare *newDcl = NULL;
              for (const auto &usePoint : *useList) {
                INST_LIST_ITER useIt = usePoint.first;
                G4_INST *useInst = *useIt;
                useCnt++;

                unsigned int currId = currInst->getLexicalId();
                if (useInst->getLexicalId() < currId ||
                    useCnt < SPLIT_USE_CNT_THRESHOLD) {
                  lastUseIt = useIt;
                } else if (split == false &&
                           (useInst->getLexicalId() -
                            (*lastUseIt)->getLexicalId()) >
                               SPLIT_USE_DISTANCE_THRESHOLD &&
                           !(oldDcl->getElemSize() >= 8 &&
                             oldDcl->getTotalElems() >= 16)) {
                  G4_Declare *splitDcl = NULL;
                  const char *splitDclName =
                      builder.getNameString(16, "split_%s", oldDcl->getName());
                  splitDcl = builder.createDeclare(
                      splitDclName, G4_GRF, oldDcl->getNumElems(),
                      oldDcl->getNumRows(), oldDcl->getElemType());
                  splitDcl->copyAlign(oldDcl);

                  LocalLiveRange *splitLR =
                      gra.GetOrCreateLocalLiveRange(splitDcl);
                  splitLR->markSplit();

                  INST_LIST_ITER iter = lastUseIt;
                  iter++;

                  // FIXME: The following code does not handle QWord variable
                  // spilling completely. See the last condition of this
                  // if-stmt.
                  G4_DstRegRegion *dst =
                      builder.createDstRegRegion(splitDcl, 1);
                  G4_SrcRegRegion *src = builder.createSrcRegRegion(
                      oldDcl, builder.getRegionStride1());
                  G4_ExecSize splitInstExecSize(oldDcl->getTotalElems() > 16
                                                    ? 16
                                                    : oldDcl->getTotalElems());
                  G4_INST *splitInst = builder.createMov(
                      splitInstExecSize, dst, src, InstOpt_WriteEnable, false);
                  bb->insertBefore(iter, splitInst);

                  unsigned int idx = 0;
                  gra.getLocalLR(oldDcl)->setLastRef(splitInst, idx);
                  gra.setNumRefs(oldDcl, useCnt);
                  splitLR->setFirstRef(splitInst, idx);
                  gra.setNumRefs(splitDcl, 2);

                  if (totalElems == 32) {
                    G4_DstRegRegion *dst = builder.createDst(
                        splitDcl->getRegVar(), 2, 0, 1, oldDcl->getElemType());
                    auto src = builder.createSrc(oldDcl->getRegVar(), 2, 0,
                                                 builder.getRegionStride1(),
                                                 oldDcl->getElemType());
                    G4_INST *splitInst2 = builder.createMov(
                        g4::SIMD16, dst, src, InstOpt_WriteEnable, false);
                    bb->insertBefore(iter, splitInst2);
                  }

                  const char *newDclName =
                      builder.getNameString(16, "copy_%s", oldDcl->getName());
                  newDcl = builder.createDeclare(
                      newDclName, G4_GRF, oldDcl->getNumElems(),
                      oldDcl->getNumRows(), oldDcl->getElemType());
                  newDcl->copyAlign(oldDcl);

                  unsigned int oldRefs = gra.getNumRefs(oldDcl);
                  LocalLiveRange *newLR = gra.GetOrCreateLocalLiveRange(newDcl);

                  iter = useIt;

                  dst = builder.createDstRegRegion(newDcl, 1);
                  src = builder.createSrcRegRegion(splitDcl,
                                                   builder.getRegionStride1());
                  G4_INST *movInst = builder.createMov(
                      G4_ExecSize(splitDcl->getTotalElems() > 16
                                      ? 16
                                      : splitDcl->getTotalElems()),
                      dst, src, InstOpt_WriteEnable, false);
                  bb->insertBefore(iter, movInst);

                  splitLR->setLastRef(movInst, idx);
                  G4_INST *old_last_use = activeLR->getLastRef(idx);
                  newLR->setFirstRef(splitInst, idx);
                  newLR->setLastRef(old_last_use, idx);
                  gra.setNumRefs(newDcl, oldRefs - useCnt + 1);

                  if (totalElems == 32) {
                    G4_DstRegRegion *dst = builder.createDst(
                        newDcl->getRegVar(), 2, 0, 1, splitDcl->getElemType());
                    auto src = builder.createSrc(splitDcl->getRegVar(), 2, 0,
                                                 builder.getRegionStride1(),
                                                 splitDcl->getElemType());
                    G4_INST *movInst2 = builder.createMov(
                        g4::SIMD16, dst, src, InstOpt_WriteEnable, false);
                    bb->insertBefore(iter, movInst2);
                  }

                  unsigned int pos = usePoint.second;
                  G4_SrcRegRegion *oldSrc =
                      useInst->getSrc(pos)->asSrcRegRegion();
                  G4_Declare *oldSrcDcl =
                      oldSrc->getBase()->asRegVar()->getDeclare();
                  G4_Declare *newSrcDcl = newDcl;
                  G4_Declare *aliasOldSrcDcl = oldSrcDcl->getAliasDeclare();
                  if (aliasOldSrcDcl != NULL) {
                    const char *newSrcDclName = builder.getNameString(
                        16, "copy_%s", oldSrcDcl->getName());
                    newSrcDcl = builder.createDeclare(
                        newSrcDclName, G4_GRF, oldSrcDcl->getNumElems(),
                        oldSrcDcl->getNumRows(), oldSrcDcl->getElemType());
                    newSrcDcl->copyAlign(oldSrcDcl);
                    newSrcDcl->setAliasDeclare(aliasOldSrcDcl,
                                               oldSrcDcl->getAliasOffset());
                  }
                  auto newSrc = builder.createSrcRegRegion(
                      oldSrc->getModifier(), Direct, newSrcDcl->getRegVar(),
                      oldSrc->getRegOff(), oldSrc->getSubRegOff(),
                      oldSrc->getRegion(), oldSrc->getType());
                  useInst->setSrc(newSrc, pos);
                  while (aliasOldSrcDcl && aliasOldSrcDcl != oldDcl) {
                    oldSrcDcl = aliasOldSrcDcl;
                    const char *newSrcDclName = builder.getNameString(
                        16, "copy_%s", oldSrcDcl->getName());
                    newSrcDcl = builder.createDeclare(
                        newSrcDclName, G4_GRF, oldSrcDcl->getNumElems(),
                        oldSrcDcl->getNumRows(), oldSrcDcl->getElemType());
                    newSrcDcl->copyAlign(oldSrcDcl);
                    aliasOldSrcDcl = oldSrcDcl->getAliasDeclare();
                    vISA_ASSERT(aliasOldSrcDcl != NULL, "Invalid alias decl");
                    newSrcDcl->setAliasDeclare(aliasOldSrcDcl,
                                               oldSrcDcl->getAliasOffset());
                  }

                  split = true;
                } else if (split) {
                  unsigned int pos = usePoint.second;
                  G4_SrcRegRegion *oldSrc =
                      useInst->getSrc(pos)->asSrcRegRegion();
                  G4_Declare *oldSrcDcl =
                      oldSrc->getBase()->asRegVar()->getDeclare();
                  G4_Declare *newSrcDcl = newDcl;
                  G4_Declare *aliasOldSrcDcl = oldSrcDcl->getAliasDeclare();
                  if (aliasOldSrcDcl != NULL) {
                    const char *newSrcDclName = builder.getNameString(
                        16, "copy_%s", oldSrcDcl->getName());
                    newSrcDcl = builder.createDeclare(
                        newSrcDclName, G4_GRF, oldSrcDcl->getNumElems(),
                        oldSrcDcl->getNumRows(), oldSrcDcl->getElemType());
                    newSrcDcl->copyAlign(oldSrcDcl);
                    newSrcDcl->setAliasDeclare(aliasOldSrcDcl,
                                               oldSrcDcl->getAliasOffset());
                  }
                  auto newSrc = builder.createSrcRegRegion(
                      oldSrc->getModifier(), Direct, newSrcDcl->getRegVar(),
                      oldSrc->getRegOff(), oldSrc->getSubRegOff(),
                      oldSrc->getRegion(), oldSrc->getType());
                  ;
                  useInst->setSrc(newSrc, pos);
                  while (aliasOldSrcDcl && aliasOldSrcDcl != oldDcl) {
                    oldSrcDcl = aliasOldSrcDcl;
                    const char *newSrcDclName = builder.getNameString(
                        16, "copy_%s", oldSrcDcl->getName());
                    newSrcDcl = builder.createDeclare(
                        newSrcDclName, G4_GRF, oldSrcDcl->getNumElems(),
                        oldSrcDcl->getNumRows(), oldSrcDcl->getElemType());
                    newSrcDcl->copyAlign(oldSrcDcl);
                    aliasOldSrcDcl = oldSrcDcl->getAliasDeclare();
                    vISA_ASSERT(aliasOldSrcDcl != NULL, "Invalid alias decl");
                    newSrcDcl->setAliasDeclare(aliasOldSrcDcl,
                                               oldSrcDcl->getAliasOffset());
                  }
                } else {
                  lastUseIt = useIt;
                }
              }
            }
          }

          // Try again
          return allocateRegs(lr, bb, builder, LLRUseMap);
        }
      }
    }

#ifdef DEBUG_VERBOSE_ON
    DEBUG_VERBOSE("Spilling range " << lr->getTopDcl()->getName() << "\n");
#endif

    // Even after spilling everything could not find an allocation
    return false;
  }
#ifdef DEBUG_VERBOSE_ON
  COUT_ERROR << lr->getTopDcl()->getName() << ":r" << regnum
             << "  BANK: " << gra.getBankConflict(lr->getTopDcl()) << "\n";
#endif

  lr->setPhyReg(builder.phyregpool.getGreg(regnum), subregnum);

  return true;
}

bool LinearScan::allocateRegsFromBanks(LocalLiveRange *lr) {
  vISA_ASSERT(!gra.use4GRFAlign, "not expecting 4GRF align");

  int regnum, subregnum;
  unsigned int tmpLocalRABound = 0;
  int nrows = 0;
  int lastUseSum1 = pregManager.getAvailableRegs()->getLastUseSum1();
  int lastUseSum2 = pregManager.getAvailableRegs()->getLastUseSum2();
  int bank1AvailableRegNum =
      pregManager.getAvailableRegs()->getBank1AvailableRegNum();
  int bank2AvailableRegNum =
      pregManager.getAvailableRegs()->getBank2AvailableRegNum();
  int size = lr->getSizeInWords();
  BankAlign align =
      gra.isEvenAligned(lr->getTopDcl()) ? BankAlign::Even : BankAlign::Either;
  G4_SubReg_Align subalign = gra.getSubRegAlign(lr->getTopDcl());
  unsigned int instID;
  lr->getFirstRef(instID);
  auto lrBC = gra.getBankConflict(lr->getTopDcl());

  // Reverse the order for FF two banks RA.
  // For FF, second bank register allocated from back to front
  if (align == BankAlign::Either && lrBC != BANK_CONFLICT_NONE) {
    switch (lrBC) {
    case BANK_CONFLICT_FIRST_HALF_EVEN:
    case BANK_CONFLICT_FIRST_HALF_ODD:
      align = BankAlign::Even;
      startGRFReg = &bank1StartGRFReg;
      tmpLocalRABound = bank1_end;
      break;
    case BANK_CONFLICT_SECOND_HALF_EVEN:
    case BANK_CONFLICT_SECOND_HALF_ODD:
      if (useRoundRobin) {
        align = BankAlign::Odd;
      }
      startGRFReg = &bank2StartGRFReg;
      tmpLocalRABound = bank2_end;
      break;
    default:
      break;
    }
  } else {
    if (useRoundRobin) {
      if (bank1AvailableRegNum == 0 && bank2AvailableRegNum == 0) {
        return false;
      }

      float bank1Average = bank1AvailableRegNum == 0
                               ? (float)0x7fffffff
                               : (float)lastUseSum1 / bank1AvailableRegNum;
      float bank2Average = bank2AvailableRegNum == 0
                               ? (float)0x7fffffff
                               : (float)lastUseSum2 / bank2AvailableRegNum;
      if (bank1Average > bank2Average) {
        startGRFReg = &bank2StartGRFReg;
        tmpLocalRABound = bank2_end;
      } else {
        startGRFReg = &bank1StartGRFReg;
        tmpLocalRABound = bank1_end;
      }
    } else {
      if (bank1AvailableRegNum < bank2AvailableRegNum) {
        startGRFReg = &bank2StartGRFReg;
        tmpLocalRABound = bank2_end;
      } else {
        startGRFReg = &bank1StartGRFReg;
        tmpLocalRABound = bank1_end;
      }
    }
  }

  if (useRoundRobin) {
    nrows = pregManager.findFreeRegs(size, align, subalign, regnum, subregnum,
                                     *startGRFReg, tmpLocalRABound, 0, instID,
                                     false, lr->getForbidden());

    if (nrows) {
      // Wrapper handling
      if (tmpLocalRABound) {
        *startGRFReg = (regnum + nrows) % (tmpLocalRABound);
      }

      if (startGRFReg == &bank2StartGRFReg) {
        if (*startGRFReg < SECOND_HALF_BANK_START_GRF) {
          *startGRFReg += bank2_start;
          if (*startGRFReg >= numRegLRA) {
            *startGRFReg = bank2_start;
            return false;
          }
        }
      }
    } else {
      // record the original endReg in case failed to allocate in bank switch
      unsigned int endReg = *startGRFReg + nrows;

      // If failed, try another bank first.
      if (startGRFReg == &bank2StartGRFReg) {
        startGRFReg = &bank1StartGRFReg;
        tmpLocalRABound = bank1_end;
      } else {
        startGRFReg = &bank2StartGRFReg;
        tmpLocalRABound = bank2_end;
      }

      nrows = pregManager.findFreeRegs(size, align, subalign, regnum, subregnum,
                                       *startGRFReg, tmpLocalRABound, 0, instID,
                                       false, lr->getForbidden());

      if (!nrows) {
        // Still fail, wrapper round in original bank
        if (startGRFReg == &bank1StartGRFReg) // switch back to before
        {
          startGRFReg = &bank2StartGRFReg;
          *startGRFReg = bank2_start;
          tmpLocalRABound = ((endReg) > (bank2_end)) ? bank2_end : endReg;
        } else {
          startGRFReg = &bank1StartGRFReg;
          *startGRFReg = bank1_start;
          tmpLocalRABound = (endReg > bank1_end) ? bank1_end : endReg;
        }
        nrows = pregManager.findFreeRegs(
            size, align, subalign, regnum, subregnum, *startGRFReg,
            tmpLocalRABound, 0, instID, false, lr->getForbidden());
      }
    }
  } else {
    nrows = pregManager.findFreeRegs(size, align, subalign, regnum, subregnum,
                                     *startGRFReg, tmpLocalRABound, 0, instID,
                                     true, lr->getForbidden());

    if (!nrows) { // Try without window, no even/odd alignment for bank, but
                  // still low and high(keep in same bank)
      nrows = pregManager.findFreeRegs(
          size,
          gra.isEvenAligned(lr->getTopDcl()) ? BankAlign::Even
                                             : BankAlign::Either,
          subalign, regnum, subregnum, *startGRFReg, tmpLocalRABound, 0, instID,
          false, lr->getForbidden());
    }

    if (!nrows) {
      // Try another bank, no even/odd alignment for bank, also no low and high
      if (startGRFReg == &bank2StartGRFReg) {
        startGRFReg = &bank1StartGRFReg;
        tmpLocalRABound = bank1_end;
      } else {
        startGRFReg = &bank2StartGRFReg;
        tmpLocalRABound = bank2_end;
      }

      nrows = pregManager.findFreeRegs(
          size,
          gra.isEvenAligned(lr->getTopDcl()) ? BankAlign::Even
                                             : BankAlign::Either,
          subalign, regnum, subregnum, *startGRFReg, tmpLocalRABound, 0, instID,
          false, lr->getForbidden());
    }
  }

  // If allocations fails, return false
  if (!nrows) {
    return false;
  }

  lr->setPhyReg(builder.phyregpool.getGreg(regnum), subregnum);

#ifdef DEBUG_VERBOSE_ON
  COUT_ERROR << lr->getTopDcl()->getName() << ":r" << regnum
             << "  BANK: " << gra.getBankConflict(lr->getTopDcl()) << "\n";
#endif
  return true;
}

// Mark physical register allocated to range lr as available
void LinearScan::freeAllocedRegs(LocalLiveRange *lr, bool setInstID) {
  int sregnum;

  G4_VarBase *preg = lr->getPhyReg(sregnum);

  vISA_ASSERT(
      preg != NULL,
      "Physical register not assigned to live range. Cannot free regs.");

  unsigned int idx = 0;
  if (setInstID) {
    lr->getLastRef(idx);
  }

  pregManager.freeRegs(preg->asGreg()->getRegNum(), sregnum,
                       lr->getSizeInWords(), idx);
}

// ********* PhyRegSummary class implementation *********

// Mark GRFs as used
void PhyRegSummary::markPhyRegs(G4_VarBase *pr, unsigned int size) {
  // Assume that pr is aligned to GRF start if it cannot fit in a single GRF
  vISA_ASSERT(pr->isGreg(), "Expecting GRF as operand");
  vISA_ASSERT(builder != nullptr, "ir builder should be set");

  int numGRFs = size / builder->numEltPerGRF<Type_UW>();
  unsigned int regnum = pr->asGreg()->getRegNum();

  if (size % builder->numEltPerGRF<Type_UW>() != 0)
    numGRFs++;

  for (int i = 0; i < numGRFs; i++) {
    GRFUsage[regnum + i] = true;
  }
}

void PhyRegSummary::printBusyRegs() {
  VISA_DEBUG_VERBOSE({
    std::cout << "GRFs used: ";
    for (int i = 0, size = static_cast<int>(GRFUsage.size()); i < size; i++) {
      if (isGRFBusy(i))
        std::cout << "r" << i << ", ";
    }
    std::cout << "\n\n";
  });
}