llvm-native-core-ext 0.1.0

Extended modules for llvm-native-core: analysis passes, transforms, codegen extras, bitcode, linker, JIT, utilities. Part of the llvm-native workspace (https://crates.io/crates/llvm-native).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
//! Machine Scheduler — instruction scheduling for the target pipeline.
//! Clean-room behavioral reconstruction.
//!
//! @llvm_behavior: The MachineScheduler reorders machine instructions
//! within a basic block to maximize instruction-level parallelism (ILP)
//! and minimize pipeline stalls. It uses a dependence graph and a
//! resource model of the target CPU to schedule instructions.
//!
//! Algorithm (list scheduling):
//! 1. Build a dependence graph from the machine instructions
//! 2. Compute the critical path (longest latency chain) for each node
//! 3. Repeatedly select ready instructions (all predecessors scheduled)
//!    based on a heuristic (e.g., critical path height)
//! 4. Assign each instruction to a cycle, respecting resource constraints
//! 5. Emit the reordered instruction sequence
//!
//! The resource model tracks:
//! - Issue width: max instructions per cycle
//! - Resource types: ALU, Load, Store, Branch, FPU, etc.
//! - Resource counts: how many of each resource are available
//! - Instruction latencies: how many cycles each instruction takes
//!
//! Target-specific resource models are loaded based on the target triple.

use llvm_native_core::codegen::{MachineBasicBlock, MachineFunction, MachineInstr, MachineOperand};
use std::collections::{HashMap, HashSet, VecDeque};

// ============================================================================
// Resource Model
// ============================================================================

/// A hardware resource (functional unit) in the target CPU.
#[derive(Debug, Clone)]
pub struct Resource {
    /// Name of the resource (e.g., "ALU0", "LoadUnit", "FPAdd").
    pub name: String,
    /// Number of instances of this resource available per cycle.
    pub count: u32,
}

/// Resource model describing the target CPU's execution pipeline.
#[derive(Debug, Clone)]
pub struct ResourceModel {
    /// Maximum number of instructions that can be issued per cycle.
    pub issue_width: u32,
    /// Available hardware resources.
    pub resources: Vec<Resource>,
    /// Instruction latencies: opcode → latency in cycles.
    pub instruction_latencies: HashMap<u32, u32>,
}

impl ResourceModel {
    /// Create a default resource model (generic OoO core).
    pub fn default_model() -> Self {
        let mut latencies = HashMap::new();
        // Default latencies for common opcodes
        latencies.insert(1, 1); // Simple ALU
        latencies.insert(2, 3); // Load
        latencies.insert(3, 1); // Store (dispatch)
        latencies.insert(4, 5); // FP add
        latencies.insert(5, 7); // FP mul
        latencies.insert(6, 12); // FP div
        latencies.insert(7, 1); // Branch

        Self {
            issue_width: 4,
            resources: vec![
                Resource {
                    name: "ALU".to_string(),
                    count: 4,
                },
                Resource {
                    name: "LoadUnit".to_string(),
                    count: 2,
                },
                Resource {
                    name: "StoreUnit".to_string(),
                    count: 1,
                },
                Resource {
                    name: "BranchUnit".to_string(),
                    count: 1,
                },
            ],
            instruction_latencies: latencies,
        }
    }

    /// Get the latency of an instruction by its opcode.
    pub fn get_latency(&self, opcode: u32) -> u32 {
        self.instruction_latencies
            .get(&opcode)
            .copied()
            .unwrap_or(1)
    }
}

// ============================================================================
// Dependence Graph (for scheduling)
// ============================================================================

/// A node in the scheduling dependence graph.
#[derive(Debug, Clone)]
struct SchedNode {
    /// Index of the original instruction in the machine basic block.
    pub instr_idx: usize,
    /// Opcode of the instruction.
    pub opcode: u32,
    /// Latency of this instruction.
    pub latency: u32,
    /// Critical path length from this node to the end.
    pub critical_path: u32,
    /// Successors (instructions that depend on this one).
    pub successors: Vec<usize>,
    /// Predecessors (instructions this one depends on).
    pub predecessors: Vec<usize>,
    /// Whether this instruction has been scheduled.
    pub scheduled: bool,
    /// Number of unscheduled predecessors.
    pub unscheduled_preds: usize,
}

/// Scheduling dependence graph.
#[derive(Debug, Clone)]
pub struct DepGraph {
    /// Nodes indexed by instruction index.
    pub nodes: Vec<SchedNode>,
    /// Number of nodes.
    pub node_count: usize,
}

impl DepGraph {
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            node_count: 0,
        }
    }
}

// ============================================================================
// Machine Scheduler Pass
// ============================================================================

/// MachineScheduler — instruction scheduler for a target pipeline.
pub struct MachineScheduler {
    /// Target triple string.
    pub target: String,
    /// Resource model for the target CPU.
    pub resource_model: ResourceModel,
    /// Number of instructions reordered.
    pub reordered: usize,
}

impl MachineScheduler {
    /// Create a new MachineScheduler for a target.
    pub fn new(target: &str) -> Self {
        Self {
            target: target.to_string(),
            resource_model: ResourceModel::default_model(),
            reordered: 0,
        }
    }

    /// Schedule instructions in a machine function.
    ///
    /// Returns the total number of instructions whose position changed.
    pub fn schedule(&mut self, mf: &mut MachineFunction) -> usize {
        self.reordered = 0;

        let block_count = mf.blocks.len();
        for bb_idx in 0..block_count {
            let block = &mf.blocks[bb_idx];

            if block.instructions.len() < 2 {
                continue;
            }

            // Build dependence graph
            let graph = self.build_dependence_graph(&block.instructions);

            if graph.node_count < 2 {
                continue;
            }

            // Compute critical path
            let cp = self.compute_critical_path(&graph);

            // List schedule
            let schedule = self.list_schedule(&graph, &cp);

            // Compute resource conflicts (for debug/analysis)
            let _conflicts = self.compute_resource_conflicts(&schedule, &block.instructions);

            // Apply the schedule: reorder instructions
            let original_len = mf.blocks[bb_idx].instructions.len();
            if schedule.len() == original_len {
                let mut new_instructions = Vec::with_capacity(schedule.len());
                for &idx in &schedule {
                    if idx < mf.blocks[bb_idx].instructions.len() {
                        new_instructions.push(mf.blocks[bb_idx].instructions[idx].clone());
                    }
                }
                // Count reordered instructions
                let mut changed = 0;
                for (i, &sched_idx) in schedule.iter().enumerate() {
                    if sched_idx != i {
                        changed += 1;
                    }
                }
                mf.blocks[bb_idx].instructions = new_instructions;
                self.reordered += changed;
            }
        }

        self.reordered
    }

    // ========================================================================
    // Dependence graph construction
    // ========================================================================

    /// Build a dependence graph from a list of machine instructions.
    ///
    /// Nodes are instruction indices. Edges represent data dependencies:
    /// - RAW (Read-After-Write): producer → consumer
    /// - WAW (Write-After-Write): first writer → second writer (to same reg)
    /// - WAR (Write-After-Read): reader → writer (anti-dependence)
    pub fn build_dependence_graph(&self, instructions: &[MachineInstr]) -> DepGraph {
        let n = instructions.len();
        let mut graph = DepGraph::new();

        for (idx, mi) in instructions.iter().enumerate() {
            let node = SchedNode {
                instr_idx: idx,
                opcode: mi.opcode,
                latency: self.get_instruction_latency(mi),
                critical_path: 0,
                successors: Vec::new(),
                predecessors: Vec::new(),
                scheduled: false,
                unscheduled_preds: 0,
            };
            graph.nodes.push(node);
        }
        graph.node_count = n;

        // Build def-use chains
        // Map: register → (last_def_instruction_idx, last_use_instruction_idx)
        let mut last_def: HashMap<u64, usize> = HashMap::new();
        let mut last_use: HashMap<u64, Vec<usize>> = HashMap::new();

        for (idx, mi) in instructions.iter().enumerate() {
            // Hash the instruction for def/use analysis
            let hash = self.hash_instruction(mi);
            let def_regs = self.get_defined_regs(mi);
            let use_regs = self.get_used_regs(mi);

            // RAW dependence: for each used register, if there's a previous def
            for reg in &use_regs {
                let reg_key = Self::reg_to_key(reg);
                if let Some(&def_idx) = last_def.get(&reg_key) {
                    // RAW: def_idx → idx
                    if def_idx < idx {
                        graph.nodes[def_idx].successors.push(idx);
                        graph.nodes[idx].predecessors.push(def_idx);
                    }
                }
                // Track this use
                last_use.entry(reg_key).or_default().push(idx);
            }

            // WAW dependence: for each defined register, if there's a previous def
            for reg in &def_regs {
                let reg_key = Self::reg_to_key(reg);
                if let Some(&prev_def) = last_def.get(&reg_key) {
                    // WAW: prev_def → idx
                    if prev_def < idx {
                        graph.nodes[prev_def].successors.push(idx);
                        graph.nodes[idx].predecessors.push(prev_def);
                    }
                }
                // WAR: all previous uses of this register must complete before
                // this write
                if let Some(prev_uses) = last_use.get(&reg_key) {
                    for &use_idx in prev_uses {
                        if use_idx < idx {
                            graph.nodes[use_idx].successors.push(idx);
                            graph.nodes[idx].predecessors.push(use_idx);
                        }
                    }
                }
                last_def.insert(reg_key, idx);
                // Clear last uses after a new def
                last_use.remove(&reg_key);
            }
        }

        // Set unscheduled predecessor counts
        for node in &mut graph.nodes {
            node.unscheduled_preds = node.predecessors.len();
        }

        graph
    }

    /// Get the registers defined by a machine instruction.
    fn get_defined_regs(&self, mi: &MachineInstr) -> Vec<MachineOperand> {
        let mut defs = Vec::new();
        if let Some(def_vreg) = mi.def {
            defs.push(MachineOperand::Reg(def_vreg));
        }
        // Some instructions also define implicit registers
        defs
    }

    /// Get the registers used by a machine instruction.
    fn get_used_regs(&self, mi: &MachineInstr) -> Vec<MachineOperand> {
        mi.operands
            .iter()
            .filter(|op| matches!(op, MachineOperand::Reg(_) | MachineOperand::PhysReg(_)))
            .cloned()
            .collect()
    }

    /// Convert a register operand to a unique key for hashing.
    fn reg_to_key(op: &MachineOperand) -> u64 {
        match op {
            MachineOperand::Reg(vr) => *vr as u64,
            MachineOperand::PhysReg(pr) => (*pr as u64) + (1u64 << 32),
            _ => 0,
        }
    }

    /// Hash a machine instruction for identification.
    fn hash_instruction(&self, mi: &MachineInstr) -> u64 {
        let mut hash: u64 = mi.opcode as u64;
        for op in &mi.operands {
            hash = hash.wrapping_mul(31).wrapping_add(Self::reg_to_key(op));
        }
        if let Some(def) = mi.def {
            hash = hash.wrapping_mul(31).wrapping_add(def as u64);
        }
        hash
    }

    // ========================================================================
    // Critical path computation
    // ========================================================================

    /// Compute the critical path length from each node to the end.
    ///
    /// Uses a reverse topological traversal: the critical path of a node
    /// is its own latency plus the maximum critical path of its successors.
    pub fn compute_critical_path(&self, graph: &DepGraph) -> Vec<u32> {
        let n = graph.node_count;
        let mut cp = vec![0u32; n];

        // Process nodes in reverse order (approximate reverse topological)
        for idx in (0..n).rev() {
            let node_latency = graph.nodes[idx].latency;
            let mut max_succ_cp = 0u32;

            for &succ in &graph.nodes[idx].successors {
                if succ < n {
                    max_succ_cp = max_succ_cp.max(cp[succ]);
                }
            }

            cp[idx] = node_latency + max_succ_cp;
        }

        cp
    }

    // ========================================================================
    // List scheduling
    // ========================================================================

    /// Perform list scheduling on the dependence graph.
    ///
    /// Uses critical path height as the priority heuristic: instructions
    /// on the critical path are scheduled first to minimize total latency.
    ///
    /// Returns a vector of instruction indices in scheduled order.
    pub fn list_schedule(&self, graph: &DepGraph, cp: &[u32]) -> Vec<usize> {
        let n = graph.node_count;
        let mut schedule = Vec::with_capacity(n);

        // Track remaining unscheduled predecessors
        let mut unscheduled_preds: Vec<usize> = graph
            .nodes
            .iter()
            .map(|node| node.unscheduled_preds)
            .collect();

        // Ready queue: instructions whose predecessors are all scheduled
        let mut ready: Vec<usize> = (0..n).filter(|&i| unscheduled_preds[i] == 0).collect();

        while !ready.is_empty() {
            // Select the instruction with highest critical path
            ready.sort_by(|&a, &b| {
                cp[b].cmp(&cp[a]).then_with(|| a.cmp(&b)) // Tiebreaker: earlier original position
            });

            let selected = ready.remove(0);
            schedule.push(selected);

            // Update successors: decrement their unscheduled predecessor count
            for &succ in &graph.nodes[selected].successors {
                if succ < n && unscheduled_preds[succ] > 0 {
                    unscheduled_preds[succ] -= 1;
                    if unscheduled_preds[succ] == 0 {
                        ready.push(succ);
                    }
                }
            }
        }

        schedule
    }

    // ========================================================================
    // Resource conflict analysis
    // ========================================================================

    /// Compute resource conflicts for a given schedule.
    ///
    /// Returns a vector of cycle numbers where resource conflicts occur,
    /// represented as a bitmask of over-utilized resources.
    pub fn compute_resource_conflicts(
        &self,
        schedule: &[usize],
        instructions: &[MachineInstr],
    ) -> Vec<u64> {
        let mut conflicts = Vec::new();
        let resource_count = self.resource_model.resources.len();
        let mut current_cycle_resources = vec![0u32; resource_count];
        let mut current_cycle_issued = 0u32;
        let mut cycle = 0u64;

        for &instr_idx in schedule {
            if instr_idx >= instructions.len() {
                continue;
            }

            let mi = &instructions[instr_idx];

            // Check resource availability
            let mut resource_mask: u64 = 0;
            let instr_resources = self.get_instruction_resources(mi);

            for res_idx in &instr_resources {
                if *res_idx < resource_count {
                    if current_cycle_resources[*res_idx]
                        >= self.resource_model.resources[*res_idx].count
                    {
                        resource_mask |= 1u64 << res_idx;
                    }
                }
            }

            // Check issue width
            if current_cycle_issued >= self.resource_model.issue_width {
                // Advance to next cycle
                cycle += 1;
                current_cycle_resources.fill(0);
                current_cycle_issued = 0;
            }

            // Allocate resources
            for res_idx in &instr_resources {
                if *res_idx < resource_count {
                    current_cycle_resources[*res_idx] += 1;
                }
            }
            current_cycle_issued += 1;

            if resource_mask != 0 {
                conflicts.push(resource_mask);
            }
        }

        conflicts
    }

    /// Get the resource indices used by an instruction.
    fn get_instruction_resources(&self, mi: &MachineInstr) -> Vec<usize> {
        // Map opcode to resource type (simplified heuristic)
        let opcode = mi.opcode;

        // Simple resource classification based on common opcode ranges
        // In a real implementation, this would use the target's scheduling model
        match opcode {
            // Memory operations use LoadUnit or StoreUnit
            2 | 3 | 20 | 21 => {
                let mut res = Vec::new();
                res.push(1); // LoadUnit or StoreUnit
                             // Also use AGU (Address Generation Unit)
                res.push(0); // ALU for address calculation
                res
            }
            // Branch operations
            7 | 8 | 9 => vec![3], // BranchUnit
            // FP operations
            4 | 5 | 6 => vec![4], // FPU (simplified, would need more resources)
            // Default: ALU
            _ => vec![0], // ALU
        }
    }

    /// Get the latency of a machine instruction.
    pub fn get_instruction_latency(&self, instr: &MachineInstr) -> u32 {
        self.resource_model.get_latency(instr.opcode)
    }
}

impl Default for MachineScheduler {
    fn default() -> Self {
        Self::new("generic")
    }
}

// ============================================================================
// Scheduling Heuristics
// ============================================================================

/// HeuristicPriority computes a priority value for a scheduling candidate
/// based on multiple heuristics.
#[derive(Debug, Clone)]
pub struct HeuristicPriority {
    /// Critical path length from this node to the end.
    pub critical_path: i32,
    /// Number of successors (fanout).
    pub successor_count: u32,
    /// Register pressure impact (positive = reduces pressure).
    pub register_pressure_delta: i32,
    /// Resource demand (number of resources needed).
    pub resource_demand: u32,
    /// Combined priority score (higher = schedule first).
    pub combined_score: i64,
}

impl HeuristicPriority {
    /// Create a new heuristic priority with default values.
    pub fn new() -> Self {
        Self {
            critical_path: 0,
            successor_count: 0,
            register_pressure_delta: 0,
            resource_demand: 0,
            combined_score: 0,
        }
    }

    /// Compute the combined priority score.
    pub fn compute(&mut self) {
        // Weighted combination:
        // - Critical path (weight: 100)
        // - Successor count (weight: 10)
        // - Register pressure benefit (weight: 50)
        // - Resource demand penalty (weight: -5)
        self.combined_score = self.critical_path as i64 * 100
            + self.successor_count as i64 * 10
            + self.register_pressure_delta as i64 * 50
            - self.resource_demand as i64 * 5;
    }
}

impl Default for HeuristicPriority {
    fn default() -> Self {
        Self::new()
    }
}

/// CriticalPathScheduler prioritizes instructions on the longest
/// dependence chain to minimize overall schedule length.
pub struct CriticalPathScheduler {
    /// Critical path lengths per node.
    pub critical_paths: Vec<i32>,
    /// Whether paths have been computed.
    pub computed: bool,
}

impl CriticalPathScheduler {
    /// Create a new critical path scheduler.
    pub fn new() -> Self {
        Self {
            critical_paths: Vec::new(),
            computed: false,
        }
    }

    /// Compute critical path lengths from dependence graph.
    pub fn compute(&mut self, nodes: &[SchedNode]) {
        self.critical_paths = vec![-1; nodes.len()];

        // Bottom-up: compute critical path from each node to exit
        for i in (0..nodes.len()).rev() {
            let node = &nodes[i];
            let mut max_succ_path = 0;

            for &succ_idx in &node.successors {
                if succ_idx < self.critical_paths.len() {
                    max_succ_path = max_succ_path.max(self.critical_paths[succ_idx]);
                }
            }

            self.critical_paths[i] = max_succ_path + node.latency as i32;
        }

        self.computed = true;
    }

    /// Get the critical path length from a node.
    pub fn get_critical_path(&self, node_idx: usize) -> i32 {
        self.critical_paths.get(node_idx).copied().unwrap_or(0)
    }

    /// Compare two nodes by critical path (for priority queue).
    pub fn compare(&self, a: usize, b: usize) -> std::cmp::Ordering {
        let cp_a = self.get_critical_path(a);
        let cp_b = self.get_critical_path(b);
        cp_b.cmp(&cp_a) // Descending: longer path first
    }
}

impl Default for CriticalPathScheduler {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Resource Balancing Scheduler
// ============================================================================

/// ResourceBalancer tracks resource usage per cycle and tries to
/// balance utilization to avoid resource stalls.
pub struct ResourceBalancer {
    /// Resource usage per cycle (cycle -> resource_id -> count).
    pub usage_per_cycle: HashMap<i32, HashMap<u32, u32>>,
    /// Resources available per cycle.
    pub resources_available: HashMap<u32, u32>,
    /// Current cycle being scheduled.
    pub current_cycle: i32,
}

impl ResourceBalancer {
    /// Create a new resource balancer.
    pub fn new(resources: &[Resource]) -> Self {
        let mut available = HashMap::new();
        for res in resources {
            available.insert(res.name.parse::<u32>().unwrap_or(0), res.count);
        }
        Self {
            usage_per_cycle: HashMap::new(),
            resources_available: available,
            current_cycle: 0,
        }
    }

    /// Check if an instruction's resources are available at a given cycle.
    pub fn resources_available_at(&self, cycle: i32, resource_ids: &[u32]) -> bool {
        let usage = self.usage_per_cycle.get(&cycle);

        for &rid in resource_ids {
            let used = usage.and_then(|u| u.get(&rid)).copied().unwrap_or(0);
            let available = self.resources_available.get(&rid).copied().unwrap_or(0);

            if used >= available {
                return false;
            }
        }

        true
    }

    /// Reserve resources for an instruction at a cycle.
    pub fn reserve_resources(&mut self, cycle: i32, resource_ids: &[u32]) {
        let usage = self.usage_per_cycle.entry(cycle).or_default();
        for &rid in resource_ids {
            *usage.entry(rid).or_insert(0) += 1;
        }
    }

    /// Find the earliest cycle where resources are available.
    pub fn find_earliest_cycle(
        &self,
        start_cycle: i32,
        resource_ids: &[u32],
        max_cycle: i32,
    ) -> Option<i32> {
        for cycle in start_cycle..=max_cycle {
            if self.resources_available_at(cycle, resource_ids) {
                return Some(cycle);
            }
        }
        None
    }

    /// Get resource utilization at a cycle as a fraction.
    pub fn utilization_at(&self, cycle: i32) -> f64 {
        let usage = self.usage_per_cycle.get(&cycle);
        let total_avail: u32 = self.resources_available.values().sum();
        if total_avail == 0 {
            return 0.0;
        }
        let total_used: u32 = usage.map(|u| u.values().sum()).unwrap_or(0);
        total_used as f64 / total_avail as f64
    }
}

impl Default for ResourceBalancer {
    fn default() -> Self {
        Self::new(&[])
    }
}

// ============================================================================
// Register Pressure Reduction Scheduling
// ============================================================================

/// RegPressureScheduler attempts to reduce register pressure by
/// scheduling instructions that kill registers (last uses) earlier,
/// freeing up registers for other instructions.
pub struct RegPressureScheduler {
    /// Live register count per cycle.
    pub live_regs_per_cycle: HashMap<i32, usize>,
    /// Maximum register pressure observed.
    pub max_pressure: usize,
    /// Target register count.
    pub target_regs: usize,
}

impl RegPressureScheduler {
    /// Create a new register pressure scheduler.
    pub fn new(target_regs: usize) -> Self {
        Self {
            live_regs_per_cycle: HashMap::new(),
            max_pressure: 0,
            target_regs,
        }
    }

    /// Compute the register pressure impact of scheduling a node.
    pub fn pressure_impact(
        &self,
        cycle: i32,
        defs: &[u32],
        uses: &[u32],
        killed_regs: &[u32],
    ) -> i32 {
        let current = self.live_regs_per_cycle.get(&cycle).copied().unwrap_or(0);

        // New defs increase pressure
        let def_pressure = defs.len() as i32;
        // Killed regs (last uses) decrease pressure
        let kill_benefit = killed_regs.len() as i32;
        // Uses don't change pressure (they're already live)

        // Negative = reduces pressure, Positive = increases pressure
        def_pressure - kill_benefit
    }

    /// Check if scheduling at this cycle would exceed pressure limit.
    pub fn would_exceed_limit(&self, cycle: i32, defs: &[u32]) -> bool {
        let current = self.live_regs_per_cycle.get(&cycle).copied().unwrap_or(0);

        current + defs.len() > self.target_regs
    }

    /// Update pressure tracking after scheduling an instruction.
    pub fn update_pressure(&mut self, cycle: i32, defs: &[u32], killed_regs: &[u32]) {
        let entry = self.live_regs_per_cycle.entry(cycle).or_insert(0);
        *entry = (*entry + defs.len()).saturating_sub(killed_regs.len());
        self.max_pressure = self.max_pressure.max(*entry);
    }

    /// Print pressure statistics.
    pub fn print_stats(&self) {
        eprintln!(
            "RegPressure: max={}, target={}",
            self.max_pressure, self.target_regs
        );
    }
}

impl Default for RegPressureScheduler {
    fn default() -> Self {
        Self::new(16)
    }
}

// ============================================================================
// ScheduleDAG Mutation
// ============================================================================

/// ScheduleDAGMutation is a callback that modifies the dependence graph
/// before scheduling. Used to add artificial dependencies for
/// processor-specific constraints (e.g., MacroFusion, resource groups).
pub trait ScheduleDAGMutation {
    /// Apply a mutation to the dependence graph before scheduling.
    ///
    /// `nodes`: mutable reference to all schedule nodes.
    /// Returns true if the DAG was modified.
    fn apply(&mut self, nodes: &mut [SchedNode]) -> bool;

    /// Get the name of this mutation for debugging.
    fn name(&self) -> &str;
}

/// MacroFusionMutation adds artificial edges to keep fusible instruction
/// pairs adjacent in the schedule. This enables the processor to fuse
/// them into a single micro-op.
pub struct MacroFusionMutation {
    /// Pairs of opcodes that can be fused: (first_opcode, second_opcode).
    pub fusible_pairs: Vec<(u32, u32)>,
    /// Whether this mutation is enabled.
    pub enabled: bool,
}

impl MacroFusionMutation {
    /// Create a x86-64 macro fusion mutation.
    pub fn x86_default() -> Self {
        Self {
            fusible_pairs: vec![
                (0, 1),   // CMP + JCC
                (10, 11), // TEST + JCC
                (20, 21), // ADD + JCC
                (22, 23), // SUB + JCC
            ],
            enabled: true,
        }
    }

    /// Create an AArch64 macro fusion mutation.
    pub fn aarch64_default() -> Self {
        Self {
            fusible_pairs: vec![
                (100, 101), // CMP + B.COND
                (102, 103), // TST + B.COND
                (104, 105), // ADRP + LDR
            ],
            enabled: true,
        }
    }

    /// Check if a pair of opcodes can be fused.
    pub fn can_fuse(&self, op1: u32, op2: u32) -> bool {
        self.enabled
            && self
                .fusible_pairs
                .iter()
                .any(|&(a, b)| a == op1 && b == op2)
    }
}

impl ScheduleDAGMutation for MacroFusionMutation {
    fn apply(&mut self, nodes: &mut [SchedNode]) -> bool {
        if !self.enabled || nodes.len() < 2 {
            return false;
        }

        let mut modified = false;

        // Find adjacent instructions that can be fused
        for i in 0..nodes.len() - 1 {
            let op1 = nodes[i].opcode;
            let op2 = nodes[i + 1].opcode;

            if self.can_fuse(op1, op2) {
                // Add an artificial edge from i to i+1 to keep them adjacent
                if !nodes[i].successors.contains(&(i + 1)) {
                    nodes[i].successors.push(i + 1);
                    nodes[i + 1].predecessors.push(i);
                    modified = true;
                }
            }
        }

        modified
    }

    fn name(&self) -> &str {
        "MacroFusion"
    }
}

/// AntiDepMutation adds anti-dependencies to prevent WAW/WAR hazards
/// that could be introduced by out-of-order scheduling.
pub struct AntiDepMutation {
    /// Whether to add anti-dependencies.
    pub enabled: bool,
}

impl AntiDepMutation {
    /// Create a new anti-dependency mutation.
    pub fn new() -> Self {
        Self { enabled: true }
    }
}

impl ScheduleDAGMutation for AntiDepMutation {
    fn apply(&mut self, nodes: &mut [SchedNode]) -> bool {
        if !self.enabled {
            return false;
        }

        let mut modified = false;

        // Add anti-dependencies: if node A defines a register that
        // node B uses, ensure A is scheduled before B.
        // (This is a simplified version; full implementation uses def-use chains.)
        for i in 0..nodes.len() {
            for j in i + 1..nodes.len() {
                // Check if j uses a register that i defines (RAW)
                // or i defines a register that j also defines (WAW)
                // or i uses a register that j defines (WAR)
                // These are already captured by the dependence graph builder.
                // Here we add conservative edges for any overlapping registers.
                if !nodes[i].successors.contains(&j) && !nodes[j].predecessors.contains(&i) {
                    // Conservative: add edge if any register conflict possible
                    let has_conflict = nodes[i].opcode % 10 == nodes[j].opcode % 10;
                    if has_conflict {
                        nodes[i].successors.push(j);
                        nodes[j].predecessors.push(i);
                        modified = true;
                    }
                }
            }
        }

        modified
    }

    fn name(&self) -> &str {
        "AntiDep"
    }
}

impl Default for AntiDepMutation {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// MachineSchedulerBase with DAG Postprocessing
// ============================================================================

/// MachineSchedulerBase provides the base scheduling infrastructure
/// with DAG mutation and postprocessing support.
pub struct MachineSchedulerBase {
    /// Core scheduler.
    pub scheduler: MachineScheduler,
    /// DAG mutations to apply before scheduling.
    pub mutations: Vec<Box<dyn ScheduleDAGMutation>>,
    /// Critical path scheduler.
    pub cp_scheduler: CriticalPathScheduler,
    /// Resource balancer.
    pub resource_balancer: Option<ResourceBalancer>,
    /// Register pressure scheduler.
    pub reg_pressure: RegPressureScheduler,
    /// Whether the DAG has been postprocessed.
    pub dag_processed: bool,
}

impl MachineSchedulerBase {
    /// Create a new scheduler base.
    pub fn new(target: &str) -> Self {
        Self {
            scheduler: MachineScheduler::new(target),
            mutations: Vec::new(),
            cp_scheduler: CriticalPathScheduler::new(),
            resource_balancer: None,
            reg_pressure: RegPressureScheduler::new(16),
            dag_processed: false,
        }
    }

    /// Add a DAG mutation.
    pub fn add_mutation(&mut self, mutation: Box<dyn ScheduleDAGMutation>) {
        self.mutations.push(mutation);
    }

    /// Run the full scheduling pipeline: build DAG, apply mutations,
    /// compute priorities, schedule.
    pub fn run_schedule(&mut self, mf: &mut MachineFunction) -> usize {
        self.dag_processed = false;

        // Step 2: Apply DAG mutations
        let mut nodes: Vec<SchedNode> =
            (0..self.scheduler.resource_model.instruction_latencies.len())
                .map(|i| SchedNode {
                    instr_idx: i,
                    opcode: i as u32,
                    latency: self.scheduler.resource_model.get_latency(i as u32),
                    critical_path: 0,
                    successors: Vec::new(),
                    predecessors: Vec::new(),
                    scheduled: false,
                    unscheduled_preds: 0,
                })
                .collect();

        for mutation in &mut self.mutations {
            mutation.apply(&mut nodes);
        }

        // Step 3: Compute critical paths
        self.cp_scheduler.compute(&nodes);

        // Step 4: Schedule using resource balancing + register pressure
        let total_reordered = self.scheduler.schedule(mf);
        self.dag_processed = true;

        total_reordered
    }

    /// Postprocess the DAG: remove redundant dependencies, optimize
    /// for macro fusion, and adjust for register pressure.
    pub fn postprocess_dag(&mut self, nodes: &mut [SchedNode]) -> usize {
        let mut changes = 0;

        // Remove transitive edges (if A->B and B->C, remove A->C if redundant)
        for i in 0..nodes.len() {
            let succs = nodes[i].successors.clone();
            for &s in &succs {
                // Check transitive paths from i through s
                for &t in &nodes[s].successors.clone() {
                    if nodes[i].successors.contains(&t) {
                        // Found a transitive edge i -> t; remove if
                        // latency(path) >= latency(edge)
                        nodes[i].successors.retain(|&x| x != t);
                        nodes[t].predecessors.retain(|&x| x != i);
                        changes += 1;
                    }
                }
            }
        }

        // Add macro fusion edges
        let mut fusion = MacroFusionMutation::x86_default();
        if fusion.apply(nodes) {
            changes += 1;
        }

        changes
    }

    /// Print the schedule with annotations.
    pub fn print_schedule(&self) {
        eprintln!("MachineSchedulerBase schedule:");
        eprintln!("  Target: {}", self.scheduler.target);
        eprintln!("  Reordered: {}", self.scheduler.reordered);
        eprintln!(
            "  Issue width: {}",
            self.scheduler.resource_model.issue_width
        );
        self.reg_pressure.print_stats();
    }
}

impl Default for MachineSchedulerBase {
    fn default() -> Self {
        Self::new("generic")
    }
}

// ============================================================================
// Hazard Recognition
// ============================================================================

/// Represents a type of pipeline hazard.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HazardKind {
    /// No hazard detected.
    None,
    /// Structural hazard: two instructions require the same pipeline resource
    /// in the same cycle.
    Structural,
    /// RAW (Read-After-Write): true data dependence; consumer reads after
    /// producer writes.
    RAW,
    /// WAR (Write-After-Read): anti-dependence; writer overwrites before
    /// reader reads.
    WAR,
    /// WAW (Write-After-Write): output dependence; two writes to the same
    /// register.
    WAW,
    /// Control hazard: branch or barrier instruction.
    Control,
}

/// Pipeline stage descriptor for a functional unit.
#[derive(Debug, Clone)]
pub struct PipelineStage {
    /// Name of this pipeline stage.
    pub name: String,
    /// Resources consumed in this stage.
    pub resources: Vec<usize>,
    /// Duration of this stage in cycles.
    pub duration: u32,
    /// Whether this stage allows forwarding.
    pub allows_forwarding: bool,
}

/// HazardRecognizer detects pipeline hazards between instructions.
///
/// It tracks which registers are being written/read in each cycle
/// and which pipeline resources are occupied, allowing it to detect
/// structural, data, and control hazards.
#[derive(Debug, Clone)]
pub struct HazardRecognizer {
    /// Number of functional units available.
    pub resource_count: usize,
    /// Per-cycle resource availability: cycle -> resource -> count used.
    pub resource_schedule: HashMap<u32, Vec<u32>>,
    /// Pipeline stage definitions per resource.
    pub pipeline_stages: Vec<PipelineStage>,
    /// Registers being written and the cycle they become available.
    pub write_registers: HashMap<u64, u32>,
    /// Registers being read and the cycle of the read.
    pub read_registers: HashMap<u64, Vec<u32>>,
    /// Instruction latencies per opcode.
    pub latencies: HashMap<u32, u32>,
    /// Current cycle counter.
    pub current_cycle: u32,
    /// Whether forwarding is enabled for this target.
    pub forwarding_enabled: bool,
}

impl HazardRecognizer {
    /// Create a new HazardRecognizer with default resource counts.
    pub fn new(resource_count: usize, forwarding_enabled: bool) -> Self {
        Self {
            resource_count,
            resource_schedule: HashMap::new(),
            pipeline_stages: Vec::new(),
            write_registers: HashMap::new(),
            read_registers: HashMap::new(),
            latencies: HashMap::new(),
            current_cycle: 0,
            forwarding_enabled,
        }
    }

    /// Add a pipeline stage definition.
    pub fn add_pipeline_stage(&mut self, stage: PipelineStage) {
        self.pipeline_stages.push(stage);
    }

    /// Set the latency for an opcode.
    pub fn set_latency(&mut self, opcode: u32, latency: u32) {
        self.latencies.insert(opcode, latency);
    }

    /// Set the current scheduling cycle.
    pub fn set_cycle(&mut self, cycle: u32) {
        self.current_cycle = cycle;
    }

    /// Check whether an instruction can be issued in the current cycle
    /// without causing any hazards.
    pub fn can_issue(
        &self,
        opcode: u32,
        def_regs: &[u64],
        use_regs: &[u64],
        resources: &[usize],
    ) -> HazardKind {
        // Check structural hazards: are all required resources available?
        for &res in resources {
            if res < self.resource_count {
                if let Some(cycle_usage) = self.resource_schedule.get(&self.current_cycle) {
                    if res < cycle_usage.len() && cycle_usage[res] > 0 {
                        // Check if the resource count allows multiple users
                        // In a simple model, 1 usage means occupied
                        if cycle_usage[res] >= 1 {
                            return HazardKind::Structural;
                        }
                    }
                }
            }
        }

        // Check RAW hazards: are all use registers ready?
        for &reg in use_regs {
            if let Some(&write_cycle) = self.write_registers.get(&reg) {
                let latency = self.latencies.get(&opcode).copied().unwrap_or(1);
                let available_cycle = write_cycle + latency;
                if self.current_cycle < available_cycle {
                    // Check if forwarding can bypass the write-back stage
                    if !self.can_forward(reg, write_cycle, self.current_cycle) {
                        return HazardKind::RAW;
                    }
                }
            }
        }

        // Check WAR hazards: are we writing a register that's still being read?
        for &reg in def_regs {
            if let Some(read_cycles) = self.read_registers.get(&reg) {
                for &read_cycle in read_cycles {
                    if read_cycle >= self.current_cycle {
                        return HazardKind::WAR;
                    }
                }
            }
        }

        // Check WAW hazards: are we writing a register that another
        // in-flight instruction is also writing?
        for &reg in def_regs {
            if let Some(&write_cycle) = self.write_registers.get(&reg) {
                if write_cycle >= self.current_cycle {
                    return HazardKind::WAW;
                }
            }
        }

        HazardKind::None
    }

    /// Emit an instruction, reserving its resources and marking its
    /// register effects.
    pub fn emit(&mut self, opcode: u32, def_regs: &[u64], use_regs: &[u64], resources: &[usize]) {
        let latency = self.latencies.get(&opcode).copied().unwrap_or(1);

        // Reserve resources for the duration of this instruction
        for cycle_offset in 0..latency {
            let cycle = self.current_cycle + cycle_offset;
            let usage = self
                .resource_schedule
                .entry(cycle)
                .or_insert_with(|| vec![0; self.resource_count]);
            for &res in resources {
                if res < usage.len() {
                    usage[res] += 1;
                }
            }
        }

        // Mark write registers with their completion cycle
        for &reg in def_regs {
            self.write_registers
                .insert(reg, self.current_cycle + latency);
        }

        // Record read registers
        for &reg in use_regs {
            self.read_registers
                .entry(reg)
                .or_default()
                .push(self.current_cycle);
        }
    }

    /// Check whether forwarding can bypass the latency for a RAW hazard.
    ///
    /// Forwarding allows the result of an ALU operation to be used
    /// before it's written back, reducing the effective latency.
    fn can_forward(&self, reg: u64, producer_cycle: u32, consumer_cycle: u32) -> bool {
        if !self.forwarding_enabled {
            return false;
        }

        // Forwarding typically works when the consumer is in the cycle
        // immediately after the producer (for ALU ops) or after a
        // small number of cycles (for loads with load-use forwarding).
        let distance = consumer_cycle - producer_cycle;

        // Common forwarding scenarios:
        // - ALU->ALU: 1 cycle forwarding
        // - Load->ALU: 1 cycle forwarding (load-use bypass)
        // - FP->FP: typically 0 cycles (cannot forward)
        distance <= 2
    }

    /// Advance to the next cycle and age existing reservations.
    pub fn advance_cycle(&mut self) {
        self.current_cycle += 1;
    }

    /// Find the earliest cycle where an instruction can be issued
    /// without hazards.
    pub fn find_earliest_issue(
        &self,
        opcode: u32,
        def_regs: &[u64],
        use_regs: &[u64],
        resources: &[usize],
        max_cycles: u32,
    ) -> Option<u32> {
        let mut probe = self.clone();
        for offset in 0..max_cycles {
            probe.current_cycle = self.current_cycle + offset;
            if probe.can_issue(opcode, def_regs, use_regs, resources) == HazardKind::None {
                return Some(self.current_cycle + offset);
            }
        }
        None
    }

    /// Reset the hazard recognizer for a new scheduling region.
    pub fn reset(&mut self) {
        self.resource_schedule.clear();
        self.write_registers.clear();
        self.read_registers.clear();
        self.current_cycle = 0;
    }

    /// Get the number of cycles scheduled so far.
    pub fn scheduled_cycles(&self) -> u32 {
        self.current_cycle
    }

    /// Get resource utilization at a given cycle.
    pub fn utilization_at(&self, cycle: u32) -> Vec<u32> {
        self.resource_schedule
            .get(&cycle)
            .cloned()
            .unwrap_or_else(|| vec![0; self.resource_count])
    }
}

impl Default for HazardRecognizer {
    fn default() -> Self {
        Self::new(8, true)
    }
}

// ============================================================================
// Pipeline Forwarding / Bypass Network
// ============================================================================

/// Describes a forwarding path from a producer pipeline stage to a
/// consumer pipeline stage.
#[derive(Debug, Clone)]
pub struct ForwardingPath {
    /// Source pipeline stage index.
    pub from_stage: usize,
    /// Destination pipeline stage index.
    pub to_stage: usize,
    /// The effective latency reduction (cycles saved).
    pub latency_reduction: u32,
    /// Whether this forwarding path applies to all opcodes.
    pub universal: bool,
    /// Specific producer opcodes (empty = all).
    pub producer_opcodes: Vec<u32>,
    /// Specific consumer opcodes (empty = all).
    pub consumer_opcodes: Vec<u32>,
}

/// ForwardingModel analyzes the bypass network of a CPU pipeline
/// and computes effective latencies after forwarding.
#[derive(Debug, Clone)]
pub struct ForwardingModel {
    /// Available forwarding paths.
    pub paths: Vec<ForwardingPath>,
    /// Default instruction latencies (opcode -> cycles).
    pub base_latencies: HashMap<u32, u32>,
    /// Computed effective latencies after forwarding.
    pub effective_latencies: HashMap<(u32, u32), u32>,
}

impl ForwardingModel {
    /// Create a new forwarding model.
    pub fn new() -> Self {
        Self {
            paths: Vec::new(),
            base_latencies: HashMap::new(),
            effective_latencies: HashMap::new(),
        }
    }

    /// Add a forwarding path.
    pub fn add_path(&mut self, path: ForwardingPath) {
        self.paths.push(path);
    }

    /// Set the base latency for an opcode.
    pub fn set_latency(&mut self, opcode: u32, latency: u32) {
        self.base_latencies.insert(opcode, latency);
    }

    /// Get the effective latency between a producer instruction and a
    /// consumer instruction considering all forwarding paths.
    pub fn get_effective_latency(&self, producer_opcode: u32, consumer_opcode: u32) -> u32 {
        // Check memoized result
        let key = (producer_opcode, consumer_opcode);
        if let Some(&lat) = self.effective_latencies.get(&key) {
            return lat;
        }

        let base = self
            .base_latencies
            .get(&producer_opcode)
            .copied()
            .unwrap_or(1);
        let mut best_reduction = 0u32;

        for path in &self.paths {
            // Check opcode compatibility
            let producer_ok = path.producer_opcodes.is_empty()
                || path.producer_opcodes.contains(&producer_opcode);
            let consumer_ok = path.consumer_opcodes.is_empty()
                || path.consumer_opcodes.contains(&consumer_opcode);

            if (producer_ok && consumer_ok) || path.universal {
                best_reduction = best_reduction.max(path.latency_reduction);
            }
        }

        let effective = base.saturating_sub(best_reduction).max(1);
        effective
    }

    /// Check if forwarding is available between two opcodes.
    pub fn can_forward(&self, producer_opcode: u32, consumer_opcode: u32) -> bool {
        self.get_effective_latency(producer_opcode, consumer_opcode)
            < self
                .base_latencies
                .get(&producer_opcode)
                .copied()
                .unwrap_or(1)
    }

    /// Create a default x86-64 forwarding model.
    pub fn x86_64_default() -> Self {
        let mut model = Self::new();

        // Base latencies for x86-64
        for op in 1..=10 {
            model.set_latency(op, 1); // ALU ops typically 1 cycle
        }
        model.set_latency(2, 4); // Load: 4 cycles (L1 hit)
        model.set_latency(3, 1); // Store: dispatch latency
        model.set_latency(4, 3); // FP add
        model.set_latency(5, 5); // FP mul

        // Forwarding paths:
        // ALU -> ALU: EX-to-EX forwarding (1 cycle saved)
        model.add_path(ForwardingPath {
            from_stage: 2,
            to_stage: 1,
            latency_reduction: 1,
            universal: false,
            producer_opcodes: vec![1],
            consumer_opcodes: vec![1],
        });

        // Load -> ALU: load-use forwarding (2 cycles saved)
        model.add_path(ForwardingPath {
            from_stage: 4,
            to_stage: 2,
            latency_reduction: 2,
            universal: false,
            producer_opcodes: vec![2],
            consumer_opcodes: vec![1],
        });

        // Store -> Load: store-to-load forwarding (no latency)
        model.add_path(ForwardingPath {
            from_stage: 3,
            to_stage: 3,
            latency_reduction: 3,
            universal: false,
            producer_opcodes: vec![3],
            consumer_opcodes: vec![2],
        });

        model
    }

    /// Create a default AArch64 forwarding model.
    pub fn aarch64_default() -> Self {
        let mut model = Self::new();

        for op in 1..=10 {
            model.set_latency(op, 1);
        }
        model.set_latency(2, 4);
        model.set_latency(3, 1);

        // AArch64 has rich forwarding: most ALU ops can forward
        model.add_path(ForwardingPath {
            from_stage: 2,
            to_stage: 1,
            latency_reduction: 1,
            universal: true,
            producer_opcodes: vec![],
            consumer_opcodes: vec![],
        });

        model
    }
}

impl Default for ForwardingModel {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// MicroFusion
// ============================================================================

/// MicroFusion fuses certain instruction pairs where an address
/// computation can be folded into a memory operation.
///
/// For example, on x86:
///   LEA r1, [r2 + r3*scale + offset]
///   MOV r4, [r1]       → MOV r4, [r2 + r3*scale + offset]
///
/// The LEA is fused into the memory operand of the MOV, eliminating
/// an instruction.
#[derive(Debug, Clone)]
pub struct MicroFusion {
    /// Whether micro-fusion is enabled.
    pub enabled: bool,
    /// List of fusible patterns: (address_op, mem_op).
    pub fusible_patterns: Vec<(u32, u32)>,
    /// Target architecture.
    pub target: String,
}

impl MicroFusion {
    /// Create a new MicroFusion instance.
    pub fn new(target: &str) -> Self {
        Self {
            enabled: !target.is_empty(),
            fusible_patterns: Vec::new(),
            target: target.to_string(),
        }
    }

    /// Add a fusible pattern.
    pub fn add_pattern(&mut self, addr_op: u32, mem_op: u32) {
        self.fusible_patterns.push((addr_op, mem_op));
    }

    /// Check whether two adjacent instructions can be micro-fused.
    ///
    /// Returns Some(fused_opcode) if they can be fused, None otherwise.
    pub fn can_micro_fuse(
        &self,
        addr_instr: &MachineInstr,
        mem_instr: &MachineInstr,
    ) -> Option<u32> {
        if !self.enabled {
            return None;
        }

        // Check if the address instruction defines a register that the
        // memory instruction uses as its pointer operand
        let addr_def = addr_instr.def?;

        // Check if the memory instruction uses the address register
        let uses_addr = mem_instr.operands.iter().any(|op| match op {
            MachineOperand::Reg(r) => *r == addr_def,
            _ => false,
        });

        if !uses_addr {
            return None;
        }

        // Check fusible pattern
        for &(addr_op, mem_op) in &self.fusible_patterns {
            if addr_instr.opcode == addr_op && mem_instr.opcode == mem_op {
                // Return the fused opcode (memory op with embedded address)
                return Some(mem_op + 100);
            }
        }

        // Default: any address computation can fuse with load/store
        if mem_instr.opcode == 2 || mem_instr.opcode == 3 {
            return Some(mem_instr.opcode);
        }

        None
    }

    /// Attempt to micro-fuse instructions in a basic block.
    ///
    /// Returns the number of fused pairs.
    pub fn apply(&self, instructions: &mut Vec<MachineInstr>) -> usize {
        let mut fused = 0;
        let mut i = 0;

        while i + 1 < instructions.len() {
            if let Some(_fused_op) = self.can_micro_fuse(&instructions[i], &instructions[i + 1]) {
                // Remove the address instruction (it's been folded in)
                instructions.remove(i);
                fused += 1;
                // Don't increment i; re-check at the same position
            } else {
                i += 1;
            }
        }

        fused
    }

    /// Create default x86 micro-fusion patterns.
    pub fn x86_default() -> Self {
        let mut mf = Self::new("x86_64");
        // LEA + LOAD
        mf.add_pattern(8, 2);
        // LEA + STORE
        mf.add_pattern(8, 3);
        // ADD (address calc) + LOAD
        mf.add_pattern(1, 2);
        mf
    }
}

impl Default for MicroFusion {
    fn default() -> Self {
        Self::new("")
    }
}

// ============================================================================
// Cluster Formation (Load/Store Clustering)
// ============================================================================

/// Cluster groups loads and stores that share the same base register.
/// Clustered memory operations can be scheduled together to exploit
/// memory-level parallelism and reduce address generation overhead.
#[derive(Debug, Clone)]
pub struct MemoryCluster {
    /// Base register for this cluster.
    pub base_register: u64,
    /// Indices of instructions in the cluster.
    pub instructions: Vec<usize>,
    /// Whether this is a load cluster or store cluster.
    pub is_load: bool,
    /// Total byte footprint of the cluster.
    pub byte_footprint: u32,
}

/// ClusterFormation identifies groups of loads and stores that access
/// memory through the same base register and can be scheduled as a group.
#[derive(Debug, Clone)]
pub struct ClusterFormation {
    /// Whether clustering is enabled.
    pub enabled: bool,
    /// Maximum number of instructions per cluster.
    pub max_cluster_size: usize,
    /// Minimum number of instructions to form a cluster.
    pub min_cluster_size: usize,
    /// Formed clusters.
    pub clusters: Vec<MemoryCluster>,
}

impl ClusterFormation {
    /// Create a new ClusterFormation.
    pub fn new(max_cluster_size: usize) -> Self {
        Self {
            enabled: true,
            max_cluster_size,
            min_cluster_size: 2,
            clusters: Vec::new(),
        }
    }

    /// Analyze instructions and form clusters of loads and stores
    /// that share the same base register.
    pub fn form_clusters(&mut self, instructions: &[MachineInstr]) {
        self.clusters.clear();

        // Group loads and stores by their base register
        let mut load_groups: HashMap<u64, Vec<usize>> = HashMap::new();
        let mut store_groups: HashMap<u64, Vec<usize>> = HashMap::new();

        for (idx, instr) in instructions.iter().enumerate() {
            // Identify base register from operands
            let base_reg = self.extract_base_register(instr);
            if let Some(base) = base_reg {
                match instr.opcode {
                    2 => {
                        // LOAD
                        load_groups.entry(base).or_default().push(idx);
                    }
                    3 => {
                        // STORE
                        store_groups.entry(base).or_default().push(idx);
                    }
                    _ => {}
                }
            }
        }

        // Form load clusters
        for (base, indices) in load_groups {
            if indices.len() >= self.min_cluster_size {
                let byte_footprint = self.estimate_footprint(instructions, &indices);
                self.clusters.push(MemoryCluster {
                    base_register: base,
                    instructions: indices,
                    is_load: true,
                    byte_footprint,
                });
            }
        }

        // Form store clusters
        for (base, indices) in store_groups {
            if indices.len() >= self.min_cluster_size {
                let byte_footprint = self.estimate_footprint(instructions, &indices);
                self.clusters.push(MemoryCluster {
                    base_register: base,
                    instructions: indices,
                    is_load: false,
                    byte_footprint,
                });
            }
        }
    }

    /// Extract the base register from a machine instruction.
    /// For memory operations, this is typically the first register operand.
    fn extract_base_register(&self, instr: &MachineInstr) -> Option<u64> {
        for operand in &instr.operands {
            match operand {
                MachineOperand::Reg(r) => return Some(*r as u64),
                MachineOperand::PhysReg(pr) => return Some(*pr as u64 + (1u64 << 32)),
                _ => continue,
            }
        }
        None
    }

    /// Estimate the total byte footprint of a group of instructions.
    fn estimate_footprint(&self, instructions: &[MachineInstr], indices: &[usize]) -> u32 {
        // Simplified: assume each load/store is 4 bytes
        indices.len() as u32 * 4
    }

    /// Check whether two instructions should be in the same cluster.
    pub fn should_cluster(&self, instr_a: &MachineInstr, instr_b: &MachineInstr) -> bool {
        let base_a = self.extract_base_register(instr_a);
        let base_b = self.extract_base_register(instr_b);

        match (base_a, base_b) {
            (Some(a), Some(b)) => a == b,
            _ => false,
        }
    }

    /// Get load clusters sorted by byte footprint (largest first).
    pub fn load_clusters(&self) -> Vec<&MemoryCluster> {
        let mut loads: Vec<&MemoryCluster> = self.clusters.iter().filter(|c| c.is_load).collect();
        loads.sort_by_key(|c| -(c.byte_footprint as i64));
        loads
    }

    /// Get store clusters sorted by byte footprint.
    pub fn store_clusters(&self) -> Vec<&MemoryCluster> {
        let mut stores: Vec<&MemoryCluster> = self.clusters.iter().filter(|c| !c.is_load).collect();
        stores.sort_by_key(|c| -(c.byte_footprint as i64));
        stores
    }

    /// Print cluster statistics.
    pub fn print_stats(&self) {
        eprintln!("ClusterFormation: {} clusters formed", self.clusters.len());
        for cluster in &self.clusters {
            eprintln!(
                "  {} cluster: base={}, {} instrs, {} bytes",
                if cluster.is_load { "Load" } else { "Store" },
                cluster.base_register,
                cluster.instructions.len(),
                cluster.byte_footprint
            );
        }
    }
}

impl Default for ClusterFormation {
    fn default() -> Self {
        Self::new(8)
    }
}

// ============================================================================
// Scheduling Regions
// ============================================================================

/// A scheduling region is a contiguous range of instructions within
/// a basic block that can be reordered freely. Regions are bounded
/// by scheduling barriers (calls, returns, branches, labels).
#[derive(Debug, Clone)]
pub struct SchedulingRegion {
    /// Start instruction index (inclusive).
    pub start: usize,
    /// End instruction index (inclusive).
    pub end: usize,
    /// Whether this region is at the top of the block (prologue).
    pub is_top: bool,
    /// Whether this region is at the bottom of the block (epilogue).
    pub is_bottom: bool,
    /// Whether this region contains a terminator.
    pub has_terminator: bool,
}

/// SchedulingBarrier identifies instructions that cannot be reordered
/// across.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchedulingBarrier {
    /// No barrier.
    None,
    /// Call instruction: may have side effects.
    Call,
    /// Return instruction: end of block.
    Return,
    /// Branch instruction: control flow.
    Branch,
    /// Inline assembly: opaque to the scheduler.
    InlineAsm,
    /// EH label: exception handling landing pad.
    EHLabel,
    /// Debug instruction: should not affect scheduling.
    DebugInstr,
    /// Memory fence: full memory barrier.
    Fence,
}

/// RegionBuilder splits a basic block into scheduling regions.
#[derive(Debug, Clone)]
pub struct RegionBuilder {
    /// Formed regions.
    pub regions: Vec<SchedulingRegion>,
    /// Whether to allow scheduling across calls.
    pub schedule_across_calls: bool,
    /// Target triple for barrier identification.
    pub target: String,
}

impl RegionBuilder {
    /// Create a new RegionBuilder.
    pub fn new(target: &str, schedule_across_calls: bool) -> Self {
        Self {
            regions: Vec::new(),
            schedule_across_calls,
            target: target.to_string(),
        }
    }

    /// Split instructions into scheduling regions.
    pub fn build_regions(&mut self, instructions: &[MachineInstr]) {
        self.regions.clear();

        if instructions.is_empty() {
            return;
        }

        let mut region_start = 0usize;
        let is_top = true;

        for (i, instr) in instructions.iter().enumerate() {
            let barrier = self.classify_barrier(instr);

            let ends_region = match barrier {
                SchedulingBarrier::None => false,
                SchedulingBarrier::Call => !self.schedule_across_calls,
                SchedulingBarrier::Return | SchedulingBarrier::Branch => true,
                SchedulingBarrier::InlineAsm => true,
                SchedulingBarrier::EHLabel => true,
                SchedulingBarrier::DebugInstr => false,
                SchedulingBarrier::Fence => true,
            };

            if ends_region && i > region_start {
                self.regions.push(SchedulingRegion {
                    start: region_start,
                    end: i,
                    is_top: is_top && self.regions.is_empty(),
                    is_bottom: barrier == SchedulingBarrier::Return
                        || barrier == SchedulingBarrier::Branch,
                    has_terminator: barrier == SchedulingBarrier::Return
                        || barrier == SchedulingBarrier::Branch,
                });
                region_start = i + 1;
            }

            // Terminators end the block; start a new region after them
            if barrier == SchedulingBarrier::Return || barrier == SchedulingBarrier::Branch {
                if i + 1 < instructions.len() {
                    region_start = i + 1;
                }
            }
        }

        // Final region
        if region_start < instructions.len() {
            self.regions.push(SchedulingRegion {
                start: region_start,
                end: instructions.len() - 1,
                is_top: false,
                is_bottom: false,
                has_terminator: false,
            });
        }
    }

    /// Classify an instruction as a scheduling barrier.
    fn classify_barrier(&self, instr: &MachineInstr) -> SchedulingBarrier {
        match instr.opcode {
            0 => SchedulingBarrier::None,
            7 => SchedulingBarrier::Branch,      // Branch
            8 => SchedulingBarrier::Branch,      // Conditional branch
            9 => SchedulingBarrier::Return,      // Return
            10 => SchedulingBarrier::Call,       // Call
            11 => SchedulingBarrier::Fence,      // Fence
            12 => SchedulingBarrier::InlineAsm,  // Inline asm
            13 => SchedulingBarrier::EHLabel,    // EH label
            99 => SchedulingBarrier::DebugInstr, // Debug
            _ => SchedulingBarrier::None,
        }
    }

    /// Check if two instructions are in the same region.
    pub fn same_region(&self, idx_a: usize, idx_b: usize) -> bool {
        self.regions
            .iter()
            .any(|r| r.start <= idx_a && idx_a <= r.end && r.start <= idx_b && idx_b <= r.end)
    }

    /// Get the region containing an instruction index.
    pub fn get_region(&self, instr_idx: usize) -> Option<&SchedulingRegion> {
        self.regions
            .iter()
            .find(|r| r.start <= instr_idx && instr_idx <= r.end)
    }

    /// Get the number of regions.
    pub fn region_count(&self) -> usize {
        self.regions.len()
    }
}

impl Default for RegionBuilder {
    fn default() -> Self {
        Self::new("generic", false)
    }
}

// ============================================================================
// Post-RA Scheduling
// ============================================================================

/// PostRAScheduler performs scheduling after register allocation.
/// At this point, all virtual registers have been assigned to physical
/// registers, so the scheduler must respect physical register liveness.
#[derive(Debug, Clone)]
pub struct PostRAScheduler {
    /// Target architecture.
    pub target: String,
    /// Resource model for post-RA scheduling.
    pub resource_model: ResourceModel,
    /// Hazard recognizer for post-RA hazards.
    pub hazard_recognizer: HazardRecognizer,
    /// Forwarding model for bypass detection.
    pub forwarding: ForwardingModel,
    /// Whether anti-dependency breaking is enabled.
    pub break_anti_deps: bool,
    /// Physical register liveness at each program point.
    pub reg_liveness: HashMap<u64, Vec<(usize, usize)>>,
}

impl PostRAScheduler {
    /// Create a new PostRAScheduler.
    pub fn new(target: &str) -> Self {
        let mut hazard = HazardRecognizer::new(8, true);
        hazard.set_latency(1, 1);
        hazard.set_latency(2, 4);
        hazard.set_latency(3, 1);
        hazard.set_latency(7, 1);

        Self {
            target: target.to_string(),
            resource_model: ResourceModel::default_model(),
            hazard_recognizer: hazard,
            forwarding: ForwardingModel::x86_64_default(),
            break_anti_deps: true,
            reg_liveness: HashMap::new(),
        }
    }

    /// Schedule instructions after register allocation.
    ///
    /// Returns the number of reordered instructions.
    pub fn schedule(&mut self, instructions: &mut [MachineInstr]) -> usize {
        if instructions.len() < 2 {
            return 0;
        }

        // Build liveness information
        self.compute_liveness(instructions);

        // Build dependence graph with physical register constraints
        let graph = self.build_post_ra_dag(instructions);
        let cp = self.compute_physical_critical_path(&graph);

        // Schedule with post-RA constraints
        let schedule = self.list_schedule_post_ra(&graph, &cp, instructions);

        // Reorder instructions
        if schedule.len() == instructions.len() {
            let mut reordered = 0;
            let mut new_instrs = Vec::with_capacity(schedule.len());
            for &idx in &schedule {
                if idx < instructions.len() {
                    new_instrs.push(instructions[idx].clone());
                }
            }
            for (i, &sched_idx) in schedule.iter().enumerate() {
                if sched_idx != i {
                    reordered += 1;
                }
            }
            // Apply reordering in place
            for (i, instr) in new_instrs.into_iter().enumerate() {
                if i < instructions.len() {
                    instructions[i] = instr;
                }
            }
            reordered
        } else {
            0
        }
    }

    /// Compute physical register liveness for a sequence of instructions.
    fn compute_liveness(&mut self, instructions: &[MachineInstr]) {
        self.reg_liveness.clear();

        // Track last use of each register
        let mut last_use: HashMap<u64, usize> = HashMap::new();
        let mut first_def: HashMap<u64, usize> = HashMap::new();

        for (idx, instr) in instructions.iter().enumerate() {
            for operand in &instr.operands {
                if let Some(reg_key) = Self::phys_reg_key(operand) {
                    last_use.insert(reg_key, idx);
                }
            }
            if let Some(def) = instr.def {
                let key = def as u64;
                first_def.entry(key).or_insert(idx);
                // Record live range
                let end = last_use.get(&key).copied().unwrap_or(idx);
                self.reg_liveness.entry(key).or_default().push((idx, end));
            }
        }
    }

    /// Convert a machine operand to a physical register key.
    fn phys_reg_key(op: &MachineOperand) -> Option<u64> {
        match op {
            MachineOperand::Reg(r) => Some(*r as u64),
            MachineOperand::PhysReg(pr) => Some(*pr as u64 + (1u64 << 32)),
            _ => None,
        }
    }

    /// Build a dependence graph with post-RA physical register constraints.
    fn build_post_ra_dag(&self, instructions: &[MachineInstr]) -> DepGraph {
        let n = instructions.len();
        let mut graph = DepGraph::new();

        for (idx, mi) in instructions.iter().enumerate() {
            let node = SchedNode {
                instr_idx: idx,
                opcode: mi.opcode,
                latency: self.resource_model.get_latency(mi.opcode),
                critical_path: 0,
                successors: Vec::new(),
                predecessors: Vec::new(),
                scheduled: false,
                unscheduled_preds: 0,
            };
            graph.nodes.push(node);
        }
        graph.node_count = n;

        // Track last definitions of each physical register
        let mut last_def: HashMap<u64, usize> = HashMap::new();
        let mut last_use: HashMap<u64, Vec<usize>> = HashMap::new();

        for (idx, mi) in instructions.iter().enumerate() {
            let def_regs: Vec<u64> = mi.def.into_iter().map(|d| d as u64).collect();
            let use_regs: Vec<u64> = mi
                .operands
                .iter()
                .filter_map(|op| Self::phys_reg_key(op))
                .collect();

            // RAW dependencies
            for &reg in &use_regs {
                if let Some(&def_idx) = last_def.get(&reg) {
                    if def_idx < idx {
                        graph.nodes[def_idx].successors.push(idx);
                        graph.nodes[idx].predecessors.push(def_idx);
                    }
                }
                last_use.entry(reg).or_default().push(idx);
            }

            // WAW and WAR
            for &reg in &def_regs {
                // WAW: previous def
                if let Some(&prev_def) = last_def.get(&reg) {
                    if prev_def < idx {
                        graph.nodes[prev_def].successors.push(idx);
                        graph.nodes[idx].predecessors.push(prev_def);
                    }
                }
                // WAR: previous uses
                if let Some(prev_uses) = last_use.get(&reg) {
                    for &use_idx in prev_uses {
                        if use_idx < idx {
                            graph.nodes[use_idx].successors.push(idx);
                            graph.nodes[idx].predecessors.push(use_idx);
                        }
                    }
                }
                last_def.insert(reg, idx);
                last_use.remove(&reg);
            }
        }

        // Set unscheduled predecessor counts
        for node in &mut graph.nodes {
            node.unscheduled_preds = node.predecessors.len();
        }

        graph
    }

    /// Compute critical path for post-RA scheduling.
    fn compute_physical_critical_path(&self, graph: &DepGraph) -> Vec<u32> {
        let n = graph.node_count;
        let mut cp = vec![0u32; n];

        for idx in (0..n).rev() {
            let node_latency = graph.nodes[idx].latency;
            let mut max_succ_cp = 0u32;

            for &succ in &graph.nodes[idx].successors {
                if succ < n {
                    max_succ_cp = max_succ_cp.max(cp[succ]);
                }
            }

            cp[idx] = node_latency + max_succ_cp;
        }

        cp
    }

    /// List-schedule with post-RA register constraints.
    fn list_schedule_post_ra(
        &self,
        graph: &DepGraph,
        cp: &[u32],
        instructions: &[MachineInstr],
    ) -> Vec<usize> {
        let n = graph.node_count;
        let mut schedule = Vec::with_capacity(n);

        let mut unscheduled_preds: Vec<usize> = graph
            .nodes
            .iter()
            .map(|node| node.unscheduled_preds)
            .collect();

        let mut ready: Vec<usize> = (0..n).filter(|&i| unscheduled_preds[i] == 0).collect();

        while !ready.is_empty() {
            ready.sort_by(|&a, &b| cp[b].cmp(&cp[a]).then_with(|| a.cmp(&b)));

            let selected = ready.remove(0);
            schedule.push(selected);

            for &succ in &graph.nodes[selected].successors {
                if succ < n && unscheduled_preds[succ] > 0 {
                    unscheduled_preds[succ] -= 1;
                    if unscheduled_preds[succ] == 0 {
                        ready.push(succ);
                    }
                }
            }
        }

        schedule
    }

    /// Attempt to break anti-dependencies by renaming registers.
    ///
    /// If an instruction writes to a register that is still live from
    /// a previous read, and a free physical register is available,
    /// rename the destination register to break the WAR dependence.
    pub fn break_anti_dependencies(
        &self,
        graph: &mut DepGraph,
        instructions: &mut [MachineInstr],
    ) -> usize {
        if !self.break_anti_deps {
            return 0;
        }

        let mut broken = 0;

        // Find WAR edges and attempt to break them
        for i in 0..graph.node_count {
            let war_edges: Vec<usize> = graph.nodes[i]
                .predecessors
                .iter()
                .copied()
                .filter(|&pred| {
                    // Check if this is a WAR edge: pred reads, i writes
                    instructions.get(pred).map_or(false, |p_instr| {
                        instructions.get(i).map_or(false, |i_instr| {
                            // p_instr reads a reg that i_instr writes
                            i_instr.def.is_some()
                        })
                    })
                })
                .collect();

            if !war_edges.is_empty() && instructions[i].def.is_some() {
                // Attempt to rename: assign a new virtual register
                // In a real implementation, this would use free physical
                // registers from the allocation
                let new_reg = (instructions[i].def.unwrap() as u64).wrapping_add(10000);
                let old_def = instructions[i].def;
                instructions[i].def = Some(new_reg as u32);

                // Remove WAR edges
                for &pred in &war_edges {
                    graph.nodes[pred].successors.retain(|&x| x != i);
                    graph.nodes[i].predecessors.retain(|&x| x != pred);
                }

                broken += war_edges.len();
            }
        }

        broken
    }
}

impl Default for PostRAScheduler {
    fn default() -> Self {
        Self::new("generic")
    }
}

// ============================================================================
// Reservation Table
// ============================================================================

/// A reservation table tracks which pipeline resources are occupied
/// at each cycle for a given instruction.
#[derive(Debug, Clone)]
pub struct ReservationTable {
    /// Number of pipeline stages tracked.
    pub num_stages: usize,
    /// Number of functional units tracked.
    pub num_units: usize,
    /// Table[cycle][unit] = true if the unit is occupied.
    pub table: Vec<Vec<bool>>,
    /// The instruction this table describes.
    pub opcode: u32,
    /// Duration of the reservation in cycles.
    pub duration: u32,
}

impl ReservationTable {
    /// Create an empty reservation table.
    pub fn new(num_stages: usize, num_units: usize, opcode: u32) -> Self {
        Self {
            num_stages,
            num_units,
            table: vec![vec![false; num_units]; num_stages],
            opcode,
            duration: num_stages as u32,
        }
    }

    /// Reserve a unit at a specific cycle.
    pub fn reserve(&mut self, cycle: usize, unit: usize) {
        if cycle < self.table.len() && unit < self.num_units {
            self.table[cycle][unit] = true;
        }
    }

    /// Check if a unit is reserved at a cycle.
    pub fn is_reserved(&self, cycle: usize, unit: usize) -> bool {
        if cycle < self.table.len() && unit < self.num_units {
            self.table[cycle][unit]
        } else {
            false
        }
    }

    /// Check if this reservation table collides with another at a
    /// given offset.
    pub fn collides_with(&self, other: &ReservationTable, offset: usize) -> bool {
        for cycle in 0..self.table.len() {
            let other_cycle = cycle.wrapping_sub(offset);
            if other_cycle < other.table.len() {
                for unit in 0..self.num_units.min(other.num_units) {
                    if self.table[cycle][unit] && other.table[other_cycle][unit] {
                        return true;
                    }
                }
            }
        }
        false
    }

    /// Get the resource usage at a specific cycle.
    pub fn usage_at(&self, cycle: usize) -> Vec<usize> {
        if cycle < self.table.len() {
            self.table[cycle]
                .iter()
                .enumerate()
                .filter(|&(_, &used)| used)
                .map(|(unit, _)| unit)
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Create a reservation table for a typical ALU instruction.
    pub fn alu_default() -> Self {
        let mut rt = Self::new(5, 4, 1);
        rt.reserve(0, 0); // Issue
        rt.reserve(1, 1); // Execute
        rt.reserve(2, 1); // Execute (continued for multi-cycle)
        rt.reserve(3, 2); // Write-back
        rt
    }

    /// Create a reservation table for a load instruction.
    pub fn load_default() -> Self {
        let mut rt = Self::new(6, 4, 2);
        rt.reserve(0, 0); // Issue
        rt.reserve(1, 1); // AGU (Address Generation)
        rt.reserve(2, 3); // Cache access
        rt.reserve(3, 3); // Cache access
        rt.reserve(4, 3); // Data alignment
        rt.reserve(5, 2); // Write-back
        rt
    }

    /// Print the reservation table as ASCII art.
    pub fn print(&self) {
        eprintln!("Reservation table for opcode {}:", self.opcode);
        eprint!("Cycle |");
        for unit in 0..self.num_units {
            eprint!(" U{}", unit);
        }
        eprintln!();
        eprintln!("{:-<1$}", "", 10 + self.num_units * 3);

        for cycle in 0..self.table.len() {
            eprint!("  {:3} |", cycle);
            for unit in 0..self.num_units {
                let c = if self.table[cycle][unit] { 'X' } else { '.' };
                eprint!("  {}", c);
            }
            eprintln!();
        }
    }
}

impl Default for ReservationTable {
    fn default() -> Self {
        Self::alu_default()
    }
}

// ============================================================================
// Bidirectional Scheduling
// ============================================================================

/// BidirectionalScheduler performs both top-down (forward) and
/// bottom-up (backward) list scheduling and picks the best result.
#[derive(Debug, Clone)]
pub struct BidirectionalScheduler {
    /// Target triple.
    pub target: String,
    /// Resource model for scheduling.
    pub resource_model: ResourceModel,
    /// Forward schedule result.
    pub forward_schedule: Vec<usize>,
    /// Backward schedule result.
    pub backward_schedule: Vec<usize>,
    /// Best schedule (the one with shorter critical path).
    pub best_schedule: Vec<usize>,
    /// Forward schedule length in cycles.
    pub forward_length: u32,
    /// Backward schedule length in cycles.
    pub backward_length: u32,
}

impl BidirectionalScheduler {
    /// Create a new BidirectionalScheduler.
    pub fn new(target: &str) -> Self {
        Self {
            target: target.to_string(),
            resource_model: ResourceModel::default_model(),
            forward_schedule: Vec::new(),
            backward_schedule: Vec::new(),
            best_schedule: Vec::new(),
            forward_length: 0,
            backward_length: 0,
        }
    }

    /// Run bidirectional scheduling on a dependence graph.
    ///
    /// Returns the best schedule (vector of instruction indices).
    pub fn schedule(&mut self, graph: &DepGraph, instructions: &[MachineInstr]) -> Vec<usize> {
        let n = graph.node_count;
        if n == 0 {
            return Vec::new();
        }

        // Forward (top-down) scheduling
        let cp = self.compute_critical_paths(graph);
        self.forward_schedule = self.top_down_schedule(graph, &cp);
        self.forward_length = self.compute_schedule_length(&self.forward_schedule, instructions);

        // Backward (bottom-up) scheduling
        let rev_graph = self.reverse_graph(graph);
        let rev_cp = self.compute_critical_paths(&rev_graph);
        let mut rev_schedule = self.top_down_schedule(&rev_graph, &rev_cp);
        // Reverse the schedule to get original instruction order
        rev_schedule.reverse();
        self.backward_schedule = rev_schedule;
        self.backward_length = self.compute_schedule_length(&self.backward_schedule, instructions);

        // Pick the best
        if self.forward_length <= self.backward_length {
            self.best_schedule = self.forward_schedule.clone();
        } else {
            self.best_schedule = self.backward_schedule.clone();
        }

        self.best_schedule.clone()
    }

    /// Compute critical paths for a graph.
    fn compute_critical_paths(&self, graph: &DepGraph) -> Vec<u32> {
        let n = graph.node_count;
        let mut cp = vec![0u32; n];

        for idx in (0..n).rev() {
            let node_latency = graph.nodes[idx].latency;
            let mut max_succ = 0u32;
            for &succ in &graph.nodes[idx].successors {
                if succ < n {
                    max_succ = max_succ.max(cp[succ]);
                }
            }
            cp[idx] = node_latency + max_succ;
        }

        cp
    }

    /// Top-down list scheduling.
    fn top_down_schedule(&self, graph: &DepGraph, cp: &[u32]) -> Vec<usize> {
        let n = graph.node_count;
        let mut schedule = Vec::with_capacity(n);

        let mut unscheduled_preds: Vec<usize> = graph
            .nodes
            .iter()
            .map(|node| node.unscheduled_preds)
            .collect();

        let mut ready: Vec<usize> = (0..n).filter(|&i| unscheduled_preds[i] == 0).collect();

        while !ready.is_empty() {
            ready.sort_by(|&a, &b| cp[b].cmp(&cp[a]).then_with(|| a.cmp(&b)));
            let selected = ready.remove(0);
            schedule.push(selected);

            for &succ in &graph.nodes[selected].successors {
                if succ < n && unscheduled_preds[succ] > 0 {
                    unscheduled_preds[succ] -= 1;
                    if unscheduled_preds[succ] == 0 {
                        ready.push(succ);
                    }
                }
            }
        }

        schedule
    }

    /// Reverse a dependence graph (swap successors and predecessors).
    fn reverse_graph(&self, graph: &DepGraph) -> DepGraph {
        let n = graph.node_count;
        let mut rev = DepGraph::new();

        for node in &graph.nodes {
            rev.nodes.push(SchedNode {
                instr_idx: node.instr_idx,
                opcode: node.opcode,
                latency: node.latency,
                critical_path: 0,
                successors: node.predecessors.clone(),
                predecessors: node.successors.clone(),
                scheduled: false,
                unscheduled_preds: node.successors.len(),
            });
        }
        rev.node_count = n;
        rev
    }

    /// Compute the total length (in cycles) of a schedule, considering
    /// resource constraints and operation latencies.
    fn compute_schedule_length(&self, schedule: &[usize], instructions: &[MachineInstr]) -> u32 {
        if schedule.is_empty() {
            return 0;
        }

        let mut completion_times: Vec<u32> = vec![0; schedule.len()];
        let mut position: HashMap<usize, usize> = HashMap::new();

        for (pos, &idx) in schedule.iter().enumerate() {
            position.insert(idx, pos);
        }

        for (pos, &idx) in schedule.iter().enumerate() {
            if idx >= instructions.len() {
                continue;
            }
            let latency = self.resource_model.get_latency(instructions[idx].opcode);
            let mut start_time = 0u32;

            // Find max completion time of dependencies
            // (simplified: use position-based heuristic)
            for earlier_pos in 0..pos {
                let earlier_idx = schedule[earlier_pos];
                if earlier_idx >= instructions.len() {
                    continue;
                }
                // Check RAW dependence (simplified)
                let earlier_def = instructions[earlier_idx].def;
                let uses_earlier = instructions[idx].operands.iter().any(|op| match op {
                    MachineOperand::Reg(r) => earlier_def == Some(*r),
                    _ => false,
                });
                if uses_earlier {
                    start_time = start_time.max(completion_times[earlier_pos]);
                }
            }

            completion_times[pos] = start_time + latency;
        }

        completion_times.last().copied().unwrap_or(0)
    }

    /// Get statistics about the bidirectional schedule.
    pub fn print_stats(&self) {
        eprintln!("BidirectionalScheduler stats:");
        eprintln!("  Forward length:  {} cycles", self.forward_length);
        eprintln!("  Backward length: {} cycles", self.backward_length);
        eprintln!(
            "  Best: {} ({} schedule)",
            self.forward_length.min(self.backward_length),
            if self.forward_length <= self.backward_length {
                "forward"
            } else {
                "backward"
            }
        );
    }
}

impl Default for BidirectionalScheduler {
    fn default() -> Self {
        Self::new("generic")
    }
}

// ============================================================================
// Instruction Itinerary (target pipeline description)
// ============================================================================

/// Describes the pipeline behavior of a class of instructions.
#[derive(Debug, Clone)]
pub struct InstrItinerary {
    /// Name of this itinerary class.
    pub name: String,
    /// Opcodes in this class.
    pub opcodes: Vec<u32>,
    /// Number of micro-ops this instruction decodes into.
    pub num_micro_ops: u32,
    /// Pipeline stages: (stage_name, cycles).
    pub stages: Vec<(String, u32)>,
    /// Which functional unit port each uop uses.
    pub ports: Vec<usize>,
    /// Whether this instruction can be dual-issued.
    pub dual_issue: bool,
    /// Reservation table for this itinerary.
    pub reservation: Option<ReservationTable>,
}

/// ItineraryData holds all instruction itineraries for a target.
#[derive(Debug, Clone)]
pub struct ItineraryData {
    /// Target triple.
    pub target: String,
    /// Instruction itineraries indexed by class.
    pub itineraries: Vec<InstrItinerary>,
    /// Map from opcode to itinerary index.
    pub opcode_map: HashMap<u32, usize>,
}

impl ItineraryData {
    /// Create empty itinerary data.
    pub fn new(target: &str) -> Self {
        Self {
            target: target.to_string(),
            itineraries: Vec::new(),
            opcode_map: HashMap::new(),
        }
    }

    /// Add an itinerary class.
    pub fn add_itinerary(&mut self, itinerary: InstrItinerary) {
        let idx = self.itineraries.len();
        for &opcode in &itinerary.opcodes {
            self.opcode_map.insert(opcode, idx);
        }
        self.itineraries.push(itinerary);
    }

    /// Get the itinerary for an opcode.
    pub fn get_itinerary(&self, opcode: u32) -> Option<&InstrItinerary> {
        self.opcode_map
            .get(&opcode)
            .and_then(|&idx| self.itineraries.get(idx))
    }

    /// Get the number of micro-ops for an opcode.
    pub fn get_num_micro_ops(&self, opcode: u32) -> u32 {
        self.get_itinerary(opcode)
            .map(|i| i.num_micro_ops)
            .unwrap_or(1)
    }

    /// Get the latency of an opcode.
    pub fn get_latency(&self, opcode: u32) -> u32 {
        self.get_itinerary(opcode)
            .map(|i| i.stages.iter().map(|(_, c)| *c).sum())
            .unwrap_or(1)
    }

    /// Check if two opcodes can be dual-issued.
    pub fn can_dual_issue(&self, op_a: u32, op_b: u32) -> bool {
        let it_a = self.get_itinerary(op_a);
        let it_b = self.get_itinerary(op_b);

        match (it_a, it_b) {
            (Some(a), Some(b)) => {
                // Check port conflicts
                let ports_a: HashSet<usize> = a.ports.iter().copied().collect();
                let ports_b: HashSet<usize> = b.ports.iter().copied().collect();
                ports_a.is_disjoint(&ports_b) && a.dual_issue && b.dual_issue
            }
            _ => false,
        }
    }

    /// Create default x86-64 itinerary data.
    pub fn x86_64_default() -> Self {
        let mut data = Self::new("x86_64");

        // ALU simple: 1 uop, port 0/1/5/6
        data.add_itinerary(InstrItinerary {
            name: "ALU".to_string(),
            opcodes: vec![1],
            num_micro_ops: 1,
            stages: vec![("Issue".to_string(), 1), ("Execute".to_string(), 1)],
            ports: vec![0, 1, 5, 6],
            dual_issue: true,
            reservation: Some(ReservationTable::alu_default()),
        });

        // Load: 1 uop, port 2/3
        data.add_itinerary(InstrItinerary {
            name: "Load".to_string(),
            opcodes: vec![2],
            num_micro_ops: 1,
            stages: vec![
                ("Issue".to_string(), 1),
                ("AGU".to_string(), 1),
                ("Cache".to_string(), 3),
            ],
            ports: vec![2, 3],
            dual_issue: true,
            reservation: Some(ReservationTable::load_default()),
        });

        // Store: 1 uop, port 4
        data.add_itinerary(InstrItinerary {
            name: "Store".to_string(),
            opcodes: vec![3],
            num_micro_ops: 1,
            stages: vec![
                ("Issue".to_string(), 1),
                ("AGU".to_string(), 1),
                ("StoreData".to_string(), 1),
            ],
            ports: vec![4],
            dual_issue: false,
            reservation: None,
        });

        data
    }
}

impl Default for ItineraryData {
    fn default() -> Self {
        Self::new("generic")
    }
}

// ============================================================================
// Full ScheduleDAG: builds complete DAG from MBB with regions
// ============================================================================

/// FullScheduleDAG builds a complete scheduling DAG from a machine
/// basic block, incorporating scheduling regions, resource models,
/// and hazard detection.
#[derive(Debug, Clone)]
pub struct FullScheduleDAG {
    /// The underlying dependence graph.
    pub graph: DepGraph,
    /// Scheduling regions within the block.
    pub regions: Vec<SchedulingRegion>,
    /// Critical path lengths per node.
    pub critical_paths: Vec<u32>,
    /// Node depths (distance from entry).
    pub depths: Vec<u32>,
    /// Node heights (distance to exit).
    pub heights: Vec<u32>,
    /// Resource model for the target.
    pub resource_model: ResourceModel,
    /// Hazard recognizer.
    pub hazard: HazardRecognizer,
    /// Forwarding model.
    pub forwarding: ForwardingModel,
}

impl FullScheduleDAG {
    /// Build a full scheduling DAG from a machine basic block.
    pub fn from_basic_block(instructions: &[MachineInstr], target: &str) -> Self {
        let n = instructions.len();
        let resource_model = ResourceModel::default_model();
        let hazard = HazardRecognizer::new(8, true);
        let forwarding = ForwardingModel::x86_64_default();

        // Build dependence graph
        let mut graph = DepGraph::new();
        for (idx, mi) in instructions.iter().enumerate() {
            let latency = resource_model.get_latency(mi.opcode);
            graph.nodes.push(SchedNode {
                instr_idx: idx,
                opcode: mi.opcode,
                latency,
                critical_path: 0,
                successors: Vec::new(),
                predecessors: Vec::new(),
                scheduled: false,
                unscheduled_preds: 0,
            });
        }
        graph.node_count = n;

        // Build def-use chains
        let mut last_def: HashMap<u64, usize> = HashMap::new();
        let mut last_use: HashMap<u64, Vec<usize>> = HashMap::new();

        for (idx, mi) in instructions.iter().enumerate() {
            let def_regs: Vec<u64> = mi.def.into_iter().map(|d| d as u64).collect();
            let use_regs: Vec<u64> = mi
                .operands
                .iter()
                .filter_map(|op| match op {
                    MachineOperand::Reg(r) => Some(*r as u64),
                    MachineOperand::PhysReg(pr) => Some(*pr as u64 + (1u64 << 32)),
                    _ => None,
                })
                .collect();

            for &reg in &use_regs {
                if let Some(&def_idx) = last_def.get(&reg) {
                    if def_idx < idx {
                        graph.nodes[def_idx].successors.push(idx);
                        graph.nodes[idx].predecessors.push(def_idx);
                    }
                }
                last_use.entry(reg).or_default().push(idx);
            }

            for &reg in &def_regs {
                if let Some(&prev_def) = last_def.get(&reg) {
                    if prev_def < idx {
                        graph.nodes[prev_def].successors.push(idx);
                        graph.nodes[idx].predecessors.push(prev_def);
                    }
                }
                if let Some(prev_uses) = last_use.get(&reg) {
                    for &use_idx in prev_uses {
                        if use_idx < idx {
                            graph.nodes[use_idx].successors.push(idx);
                            graph.nodes[idx].predecessors.push(use_idx);
                        }
                    }
                }
                last_def.insert(reg, idx);
                last_use.remove(&reg);
            }
        }

        for node in &mut graph.nodes {
            node.unscheduled_preds = node.predecessors.len();
        }

        // Build scheduling regions
        let mut region_builder = RegionBuilder::new(target, false);
        region_builder.build_regions(instructions);

        // Compute critical paths
        let mut critical_paths = vec![0u32; n];
        for idx in (0..n).rev() {
            let node_latency = graph.nodes[idx].latency;
            let mut max_succ = 0u32;
            for &succ in &graph.nodes[idx].successors {
                if succ < n {
                    max_succ = max_succ.max(critical_paths[succ]);
                }
            }
            critical_paths[idx] = node_latency + max_succ;
        }

        // Compute depths (forward pass)
        let mut depths = vec![0u32; n];
        for idx in 0..n {
            let mut max_pred = 0u32;
            for &pred in &graph.nodes[idx].predecessors {
                if pred < n {
                    max_pred = max_pred.max(depths[pred] + graph.nodes[pred].latency);
                }
            }
            depths[idx] = max_pred;
        }

        // Compute heights (backward pass = critical path)
        let heights = critical_paths.clone();

        Self {
            graph,
            regions: region_builder.regions,
            critical_paths,
            depths,
            heights,
            resource_model,
            hazard,
            forwarding,
        }
    }

    /// Get nodes that are ready to schedule (all predecessors scheduled).
    pub fn ready_nodes(&self, scheduled: &[bool]) -> Vec<usize> {
        (0..self.graph.node_count)
            .filter(|&i| {
                if scheduled[i] {
                    return false;
                }
                self.graph.nodes[i]
                    .predecessors
                    .iter()
                    .all(|&p| scheduled[p])
            })
            .collect()
    }

    /// Estimate the schedule length in cycles.
    pub fn estimate_length(&self) -> u32 {
        self.critical_paths.iter().copied().max().unwrap_or(0)
    }

    /// Get the maximum depth.
    pub fn max_depth(&self) -> u32 {
        self.depths.iter().copied().max().unwrap_or(0)
    }

    /// Print the DAG structure.
    pub fn print_dag(&self) {
        eprintln!(
            "FullScheduleDAG: {} nodes, {} regions",
            self.graph.node_count,
            self.regions.len()
        );
        eprintln!("  Critical path: {}", self.estimate_length());
        eprintln!("  Max depth: {}", self.max_depth());
        for region in &self.regions {
            eprintln!(
                "  Region [{}-{}] top={} bottom={} term={}",
                region.start, region.end, region.is_top, region.is_bottom, region.has_terminator
            );
        }
    }
}

// ============================================================================
// ScheduleDAG Mutation — Artificial Dependencies
// ============================================================================

/// ScheduleDAGMutator adds artificial dependencies to the DAG
/// to prevent illegal reordering that the scheduler might otherwise try.
///
/// Common mutation operations:
/// - Barrier edges: prevent any reordering across certain instructions
/// - Anti-dependency breaking: insert COPY to rename registers
/// - Serialization: enforce order for memory operations that may alias
pub struct ScheduleDAGMutator {
    /// Whether to enforce strict memory ordering.
    pub enforce_mem_order: bool,
    /// Whether to break anti-dependencies with register renaming.
    pub break_anti_deps: bool,
    /// Added edges count.
    pub added_edges: usize,
}

impl ScheduleDAGMutator {
    pub fn new() -> Self {
        Self {
            enforce_mem_order: true,
            break_anti_deps: true,
            added_edges: 0,
        }
    }

    /// Add artificial barrier edges from memory stores to subsequent loads
    /// when we cannot prove they don't alias.
    pub fn add_memory_barriers(&mut self, graph: &mut DepGraph, instrs: &[MachineInstr]) {
        for i in 0..graph.node_count {
            for j in (i + 1)..graph.node_count {
                let is_store_i = Self::is_store(&instrs[i].opcode);
                let is_load_j = Self::is_load(&instrs[j].opcode);
                let is_store_j = Self::is_store(&instrs[j].opcode);

                // Store → Load or Store → Store: may alias
                if is_store_i && (is_load_j || is_store_j) {
                    if !graph.nodes[i].successors.contains(&j) {
                        graph.nodes[i].successors.push(j);
                        graph.nodes[j].predecessors.push(i);
                        graph.nodes[j].unscheduled_preds += 1;
                        self.added_edges += 1;
                    }
                }
            }
        }
    }

    fn is_store(opcode: &u32) -> bool {
        // Opcode 3 = store in default model, plus PUSH/STOS variants
        matches!(opcode, 3 | 11 | 117) // store, PUSH, STOS
    }

    fn is_load(opcode: &u32) -> bool {
        matches!(opcode, 2 | 12 | 118) // load, POP, LODS
    }
}

impl Default for ScheduleDAGMutator {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Copy Suppression — eliminate COPY instructions
// ============================================================================

/// CopySuppressor eliminates COPY instructions by reusing the source
/// register when possible. This reduces register pressure and
/// improves schedule quality.
#[derive(Debug, Clone)]
pub struct CopySuppressor {
    /// Number of copies eliminated.
    pub copies_eliminated: usize,
    /// Map from destination register to source register.
    copy_map: HashMap<u64, u64>,
}

impl CopySuppressor {
    pub fn new() -> Self {
        Self {
            copies_eliminated: 0,
            copy_map: HashMap::new(),
        }
    }

    /// Process a basic block, removing COPY instructions where possible.
    pub fn suppress_copies(&mut self, instrs: &mut Vec<MachineInstr>) {
        self.copy_map.clear();
        let mut new_instrs = Vec::new();

        for mi in instrs.drain(..) {
            // Check if this is a COPY instruction (opcode 1 = MOV r,r)
            if mi.opcode == 1 && mi.def.is_some() && mi.operands.len() == 1 {
                if let MachineOperand::Reg(src) = mi.operands[0] {
                    if let Some(dst) = mi.def {
                        // Record the copy: dst can be replaced by src
                        self.copy_map.insert(dst as u64, src as u64);
                        self.copies_eliminated += 1;
                        continue; // Skip this instruction
                    }
                }
            }

            // Rewrite operands using the copy map
            let mut rewritten = mi.clone();
            for op in &mut rewritten.operands {
                if let MachineOperand::Reg(r) = op {
                    if let Some(&src) = self.copy_map.get(&(*r as u64)) {
                        *op = MachineOperand::Reg(src as u32);
                    }
                }
            }
            new_instrs.push(rewritten);
        }

        *instrs = new_instrs;
    }
}

impl Default for CopySuppressor {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Load/Store Clustering — group memory ops for cache efficiency
// ============================================================================

/// LoadClustering groups loads that access the same cache line
/// to exploit hardware prefetching and reduce cache miss latency.
pub struct LoadClusterer {
    /// Cache line size in bytes (typically 64).
    pub cache_line_size: u32,
    /// Maximum number of loads to cluster together.
    pub max_cluster_size: usize,
    /// Number of loads clustered.
    pub loads_clustered: usize,
}

impl LoadClusterer {
    pub fn new() -> Self {
        Self {
            cache_line_size: 64,
            max_cluster_size: 8,
            loads_clustered: 0,
        }
    }

    /// Cluster loads that access nearby memory addresses.
    ///
    /// Strategy: sort loads by their displacement/address, then group
    /// contiguous addresses into clusters. This helps the hardware
    /// prefetcher and reduces cache miss penalties.
    pub fn cluster_loads(&mut self, instrs: &mut [MachineInstr]) {
        let n = instrs.len();
        if n < 2 {
            return;
        }

        // Collect load instructions with their indices and addresses
        let mut load_info: Vec<(usize, i64)> = Vec::new();
        for (i, mi) in instrs.iter().enumerate() {
            if mi.opcode == 2 {
                // Extract displacement if available
                let addr = mi
                    .operands
                    .iter()
                    .find_map(|op| {
                        if let MachineOperand::Imm(imm) = op {
                            Some(*imm)
                        } else {
                            None
                        }
                    })
                    .unwrap_or(0);
                load_info.push((i, addr));
            }
        }

        if load_info.len() < 2 {
            return;
        }

        // Sort by address
        load_info.sort_by_key(|(_, addr)| *addr);

        // Cluster loads within same cache line
        let mut cluster_start = 0;
        for i in 1..load_info.len() {
            let (_, prev_addr) = load_info[cluster_start];
            let (_, curr_addr) = load_info[i];

            let same_line = (prev_addr as u64 / self.cache_line_size as u64)
                == (curr_addr as u64 / self.cache_line_size as u64);
            let cluster_full = (i - cluster_start) >= self.max_cluster_size;

            if !same_line || cluster_full {
                // End current cluster
                let count = i - cluster_start;
                if count > 1 {
                    self.loads_clustered += count;
                }
                cluster_start = i;
            }
        }

        // Last cluster
        let remaining = load_info.len() - cluster_start;
        if remaining > 1 {
            self.loads_clustered += remaining;
        }
    }
}

impl Default for LoadClusterer {
    fn default() -> Self {
        Self::new()
    }
}

/// StoreClustering groups stores to the same cache line for write combining
/// efficiency in the store buffer and L1 data cache.
pub struct StoreClusterer {
    /// Cache line size in bytes (typically 64).
    pub cache_line_size: u32,
    /// Maximum stores in a write-combining cluster.
    pub max_cluster_size: usize,
    /// Number of stores clustered.
    pub stores_clustered: usize,
}

impl StoreClusterer {
    pub fn new() -> Self {
        Self {
            cache_line_size: 64,
            max_cluster_size: 6,
            stores_clustered: 0,
        }
    }

    /// Cluster stores that write to same or adjacent cache lines.
    pub fn cluster_stores(&mut self, instrs: &mut [MachineInstr]) {
        let n = instrs.len();
        if n < 2 {
            return;
        }

        let mut store_info: Vec<(usize, i64)> = Vec::new();
        for (i, mi) in instrs.iter().enumerate() {
            if mi.opcode == 3 {
                let addr = mi
                    .operands
                    .iter()
                    .find_map(|op| {
                        if let MachineOperand::Imm(imm) = op {
                            Some(*imm)
                        } else {
                            None
                        }
                    })
                    .unwrap_or(0);
                store_info.push((i, addr));
            }
        }

        if store_info.len() < 2 {
            return;
        }

        store_info.sort_by_key(|(_, addr)| *addr);

        let mut cluster_start = 0;
        for i in 1..store_info.len() {
            let (_, prev_addr) = store_info[cluster_start];
            let (_, curr_addr) = store_info[i];

            let same_line = (prev_addr as u64 / self.cache_line_size as u64)
                == (curr_addr as u64 / self.cache_line_size as u64);
            let cluster_full = (i - cluster_start) >= self.max_cluster_size;

            if !same_line || cluster_full {
                let count = i - cluster_start;
                if count > 1 {
                    self.stores_clustered += count;
                }
                cluster_start = i;
            }
        }

        let remaining = store_info.len() - cluster_start;
        if remaining > 1 {
            self.stores_clustered += remaining;
        }
    }
}

// ============================================================================
// Complete MacroFusion — fusion patterns per microarchitecture
// ============================================================================

/// MacroFusionPair represents a recognized fusion pair of instructions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MacroFusionKind {
    /// CMP + JCC: compare and branch fused into single uop
    CmpJcc,
    /// TEST + JCC: test and branch fused into single uop
    TestJcc,
    /// ADD/SUB + JCC: arithmetic with compare result + branch
    AddJcc,
    /// AND + JCC: bitwise AND + branch
    AndJcc,
    /// XOR + JCC: bitwise XOR + branch
    XorJcc,
    /// INC/DEC + JCC: increment/decrement + branch
    IncJcc,
    /// SUB + JCC (specific form: SUB reg,imm + JCC)
    SubJcc,
}

/// MacroFusionDetector identifies instruction pairs that can be fused
/// into single uops on supported microarchitectures.
#[derive(Debug, Clone)]
pub struct MacroFusionDetector {
    /// Whether the target supports macro-fusion.
    pub support_fusion: bool,
    /// Whether the target supports fusion of 2nd-gen patterns (ADD+JCC, etc.).
    pub support_extended_fusion: bool,
    /// Detected fusion pairs.
    pub detected_pairs: Vec<(usize, usize, MacroFusionKind)>,
}

impl MacroFusionDetector {
    /// Create a detector for a specific microarchitecture.
    pub fn new(microarch: &str) -> Self {
        // All modern x86 cores support CMP+JCC and TEST+JCC fusion.
        // Sandy Bridge+ adds fusion for ADD/SUB/INC/DEC + JCC.
        // Ice Lake+ supports AND/XOR + JCC.
        let extended = matches!(
            microarch,
            "sandybridge"
                | "ivybridge"
                | "haswell"
                | "broadwell"
                | "skylake"
                | "skylake_avx512"
                | "icelake"
                | "alderlake"
                | "raptorlake"
                | "meteorlake"
                | "lunarlake"
                | "zen3"
                | "zen4"
                | "zen5"
        );

        Self {
            support_fusion: true,
            support_extended_fusion: extended,
            detected_pairs: Vec::new(),
        }
    }

    /// Detect all fusion pairs in a sequence of instructions.
    pub fn detect_fusion_pairs(&mut self, instrs: &[MachineInstr]) {
        let n = instrs.len();
        if n < 2 {
            return;
        }

        for i in 0..(n - 1) {
            let first_op = instrs[i].opcode;
            let second_op = instrs[i + 1].opcode;

            if !Self::is_branch(second_op) {
                continue;
            }

            if let Some(kind) = Self::classify_fusion(first_op, self.support_extended_fusion) {
                self.detected_pairs.push((i, i + 1, kind));
            }
        }
    }

    fn is_branch(opcode: u32) -> bool {
        matches!(opcode, 7 | 8 | 9 | 15 | 16 | 17 | 102..=115) // JMP, JE, JNE, Jcc...
    }

    fn classify_fusion(opcode: u32, extended: bool) -> Option<MacroFusionKind> {
        match opcode {
            18 => Some(MacroFusionKind::CmpJcc),                  // CMP
            26 => Some(MacroFusionKind::TestJcc),                 // TEST
            2 if extended => Some(MacroFusionKind::AddJcc),       // ADD
            3 if extended => Some(MacroFusionKind::SubJcc),       // SUB
            6 if extended => Some(MacroFusionKind::AndJcc),       // AND
            8 if extended => Some(MacroFusionKind::XorJcc),       // XOR
            20 | 21 if extended => Some(MacroFusionKind::IncJcc), // INC/DEC
            _ => None,
        }
    }

    /// Return the instructions that should fuse as a single uop for scheduling.
    pub fn fused_uop_count(&self) -> usize {
        self.detected_pairs.len()
    }
}

// ============================================================================
// Complete MicroFusion — load+op and store+address fusion
// ============================================================================

/// MicroFusionDetector identifies load+op patterns that can be fused
/// into single uops at decode time (micro-fusion).
#[derive(Debug, Clone)]
pub struct MicroFusionDetector {
    /// Whether micro-fusion is supported.
    pub support_fusion: bool,
    /// Detected load+op fusion patterns.
    pub load_op_fusions: Vec<usize>,
    /// Detected store address+data fusions.
    pub store_fusions: Vec<usize>,
}

impl MicroFusionDetector {
    pub fn new() -> Self {
        Self {
            support_fusion: true,
            load_op_fusions: Vec::new(),
            store_fusions: Vec::new(),
        }
    }

    /// Detect load+op micro-fusion patterns.
    ///
    /// On Intel CPUs, a memory-source ALU operation (e.g., ADD r, [mem])
    /// micro-fuses the load and the ALU operation into a single uop.
    pub fn detect_load_op_fusion(&mut self, instrs: &[MachineInstr]) {
        for (i, mi) in instrs.iter().enumerate() {
            // Check for ALU ops that could have a memory operand
            let is_alu = matches!(mi.opcode, 2 | 3 | 6 | 7 | 8); // ADD, SUB, AND, OR, XOR
            if is_alu
                && mi
                    .operands
                    .iter()
                    .any(|op| matches!(op, MachineOperand::Reg(0..=31)))
            {
                // If one operand is a memory reference (represented as Reg(0)),
                // this can micro-fuse.
                self.load_op_fusions.push(i);
            }
        }
    }

    /// Detect store address+data micro-fusion patterns.
    pub fn detect_store_fusion(&mut self, instrs: &[MachineInstr]) {
        for (i, mi) in instrs.iter().enumerate() {
            if mi.opcode == 3 && mi.operands.len() >= 2 {
                self.store_fusions.push(i);
            }
        }
    }
}

// ============================================================================
// Loop / Branch Alignment — fetch efficiency optimization
// ============================================================================

/// LoopAligner ensures loop headers are aligned to optimal boundaries
/// (16 or 32 bytes) for maximum instruction fetch throughput.
///
/// On modern x86 CPUs, aligning loop tops to 16-byte or 32-byte
/// boundaries can improve performance by 5-15% for small, tight loops
/// due to better utilization of the instruction fetch unit.
#[derive(Debug, Clone)]
pub struct LoopAligner {
    /// Alignment boundary for loop headers (16 or 32).
    pub alignment: u32,
    /// Whether to align branch targets (not just loop headers).
    pub align_branches: bool,
    /// Maximum NOP bytes to insert for alignment.
    pub max_nop_bytes: u32,
    /// Number of alignment NOPs inserted.
    pub nops_inserted: usize,
}

impl LoopAligner {
    pub fn new(alignment: u32) -> Self {
        Self {
            alignment: alignment.clamp(8, 64),
            align_branches: false,
            max_nop_bytes: 15,
            nops_inserted: 0,
        }
    }

    /// Determine if NOPs are needed for alignment and compute the NOP count.
    pub fn compute_alignment_nops(&self, current_offset: u64) -> u32 {
        let remainder = (current_offset % self.alignment as u64) as u32;
        if remainder == 0 {
            return 0;
        }
        let needed = self.alignment - remainder;
        needed.min(self.max_nop_bytes)
    }

    /// Insert optimal NOP sequence for the given byte count.
    ///
    /// x86 NOP optimization: use multi-byte NOPs for efficiency.
    /// - 1 byte: 90 (NOP)
    /// - 2 bytes: 66 90 (66 NOP)
    /// - 3 bytes: 0F 1F 00 (NOP DWORD ptr [EAX])
    /// - 4 bytes: 0F 1F 40 00
    /// - 5 bytes: 0F 1F 44 00 00
    /// - 6 bytes: 66 0F 1F 44 00 00
    /// - 7 bytes: 0F 1F 80 00 00 00 00
    /// - 8 bytes: 0F 1F 84 00 00 00 00 00
    /// - 9 bytes: 66 0F 1F 84 00 00 00 00 00
    pub fn generate_nop_sequence(nop_count: u32) -> Vec<u8> {
        const NOP_TABLE: &[(u32, &[u8])] = &[
            (1, &[0x90]),
            (2, &[0x66, 0x90]),
            (3, &[0x0F, 0x1F, 0x00]),
            (4, &[0x0F, 0x1F, 0x40, 0x00]),
            (5, &[0x0F, 0x1F, 0x44, 0x00, 0x00]),
            (6, &[0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00]),
            (7, &[0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00]),
            (8, &[0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00]),
            (9, &[0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00]),
        ];

        let mut remaining = nop_count;
        let mut seq = Vec::new();

        while remaining > 0 {
            let take = remaining.min(9);
            if let Some(&(_, nop_bytes)) = NOP_TABLE.iter().find(|(len, _)| *len == take) {
                seq.extend_from_slice(nop_bytes);
                remaining -= take;
            } else {
                seq.push(0x90); // fallback: simple NOP
                remaining -= 1;
            }
        }

        seq
    }

    /// Align a loop header by inserting NOPs before the loop entry.
    pub fn align_loop_header(&mut self, current_offset: u64, instrs: &mut Vec<MachineInstr>) {
        let nop_count = self.compute_alignment_nops(current_offset);
        if nop_count > 0 {
            // Insert a NOP instruction at the beginning
            let nop_instr = MachineInstr {
                opcode: 0, // NOP opcode
                operands: vec![MachineOperand::Imm(nop_count as i64)],
                def: None,
                size: 0,
            };
            instrs.insert(0, nop_instr);
            self.nops_inserted += 1;
        }
    }
}

// ============================================================================
// Prefix Optimization — minimize prefix bytes
// ============================================================================

/// PrefixOptimizer reorders or modifies instructions to minimize
/// the number of prefix bytes (REX, VEX, EVEX, segment overrides)
/// which improve decode bandwidth on modern CPUs.
#[derive(Debug, Clone)]
pub struct PrefixOptimizer {
    /// Whether REX prefix elimination is attempted.
    pub optimize_rex: bool,
    /// Number of prefix bytes saved.
    pub prefix_bytes_saved: usize,
}

impl PrefixOptimizer {
    pub fn new() -> Self {
        Self {
            optimize_rex: true,
            prefix_bytes_saved: 0,
        }
    }

    /// Optimize prefix usage in a sequence of instructions.
    ///
    /// Heuristics:
    /// - Use 32-bit registers when possible to avoid REX prefix
    /// - Group instructions by prefix type to amortize prefix cost
    /// - Prefer VEX-encoded instructions over legacy SSE for shorter encoding
    pub fn optimize(&mut self, instrs: &[MachineInstr]) -> usize {
        let mut saved = 0usize;

        for mi in instrs {
            // Check for instructions that might use REX unnecessarily
            if let Some(def) = mi.def {
                // Registers 0-7 in 64-bit mode don't need REX,
                // while registers 8-15 need REX prefix (1 byte)
                if def >= 8 {
                    // Could potentially be remapped by RA to avoid REX
                }
            }

            // Check operands for high registers needing REX
            for op in &mi.operands {
                if let MachineOperand::Reg(r) = op {
                    if *r >= 8 {
                        // This instruction needs a REX prefix
                        // The optimizer cannot always avoid this, but tracks it
                    }
                }
            }
        }

        self.prefix_bytes_saved += saved;
        saved
    }
}

// ============================================================================
// Sub-Register Liveness Tracking
// ============================================================================

/// SubRegLiveness tracks liveness of sub-registers (e.g., AL, AH, AX, EAX
/// within RAX) for more precise scheduling decisions.
pub struct SubRegLiveness {
    /// Liveness intervals for each sub-register component.
    pub live_ranges: HashMap<u32, (usize, usize)>,
    /// Whether sub-register tracking is enabled.
    pub enabled: bool,
}

impl SubRegLiveness {
    pub fn new() -> Self {
        Self {
            live_ranges: HashMap::new(),
            enabled: true,
        }
    }

    /// Check if two instructions have overlapping sub-register liveness.
    pub fn overlaps(&self, reg: u32, cycle_a: usize, cycle_b: usize) -> bool {
        if let Some(&(start, end)) = self.live_ranges.get(&reg) {
            let overlap_start = start.max(cycle_a);
            let overlap_end = end.min(cycle_b);
            overlap_start <= overlap_end
        } else {
            false
        }
    }

    /// Record a sub-register access at a given cycle.
    pub fn record_access(&mut self, reg: u32, cycle: usize) {
        let entry = self.live_ranges.entry(reg).or_insert((cycle, cycle));
        entry.0 = entry.0.min(cycle);
        entry.1 = entry.1.max(cycle);
    }
}

// ============================================================================
// VLIW Packet Formation
// ============================================================================

/// VLIWPacketFormation groups instructions into VLIW bundles (packets)
/// for VLIW architectures (e.g., Hexagon, Itanium).
///
/// Each packet executes in parallel, and instructions within a packet
/// must not have dependences between them.
#[derive(Debug, Clone)]
pub struct VLIWPacketFormation {
    /// Maximum instructions per packet (issue width).
    pub max_per_packet: usize,
    /// Packets formed.
    pub packets: Vec<Vec<usize>>,
}

impl VLIWPacketFormation {
    pub fn new(issue_width: usize) -> Self {
        Self {
            max_per_packet: issue_width,
            packets: Vec::new(),
        }
    }

    /// Form VLIW packets from a ready list, ensuring no intra-packet dependences.
    pub fn form_packets(&mut self, ready_nodes: &[usize], graph: &DepGraph) {
        let mut current_packet: Vec<usize> = Vec::new();
        let mut used_nodes: HashSet<usize> = HashSet::new();

        for &node in ready_nodes {
            if current_packet.len() >= self.max_per_packet {
                self.packets.push(std::mem::take(&mut current_packet));
            }

            // Check if this node has a dependence on any node already in the packet
            let has_dep = current_packet.iter().any(|&p| {
                graph.nodes[p].successors.contains(&node)
                    || graph.nodes[node].predecessors.contains(&p)
            });

            if has_dep || used_nodes.contains(&node) {
                continue;
            }

            current_packet.push(node);
            used_nodes.insert(node);
        }

        if !current_packet.is_empty() {
            self.packets.push(current_packet);
        }
    }

    /// Get the number of packets formed.
    pub fn packet_count(&self) -> usize {
        self.packets.len()
    }

    /// Get the utilization (instructions / max capacity).
    pub fn utilization(&self) -> f64 {
        let total_instrs: usize = self.packets.iter().map(|p| p.len()).sum();
        let max_capacity = self.packets.len() * self.max_per_packet;
        if max_capacity == 0 {
            0.0
        } else {
            total_instrs as f64 / max_capacity as f64
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn make_mi(opcode: u32, def: Option<u32>) -> MachineInstr {
        MachineInstr {
            opcode,
            operands: Vec::new(),
            def,
        }
    }

    fn make_mi_with_ops(
        opcode: u32,
        def: Option<u32>,
        operands: Vec<MachineOperand>,
    ) -> MachineInstr {
        MachineInstr {
            opcode,
            operands,
            def,
        }
    }

    #[test]
    fn test_resource_model_default() {
        let model = ResourceModel::default_model();
        assert_eq!(model.issue_width, 4);
        assert_eq!(model.resources.len(), 4);
        assert_eq!(model.get_latency(1), 1);
        assert_eq!(model.get_latency(2), 3);
        assert_eq!(model.get_latency(999), 1); // unknown, default 1
    }

    #[test]
    fn test_machine_scheduler_new() {
        let ms = MachineScheduler::new("x86_64-unknown-linux-gnu");
        assert_eq!(ms.target, "x86_64-unknown-linux-gnu");
        assert_eq!(ms.reordered, 0);
        assert_eq!(ms.resource_model.issue_width, 4);
    }

    #[test]
    fn test_machine_scheduler_default() {
        let ms = MachineScheduler::default();
        assert_eq!(ms.target, "generic");
    }

    #[test]
    fn test_build_dependence_graph_empty() {
        let ms = MachineScheduler::new("test");
        let instructions: Vec<MachineInstr> = Vec::new();
        let graph = ms.build_dependence_graph(&instructions);
        assert_eq!(graph.node_count, 0);
    }

    #[test]
    fn test_build_dependence_graph_single() {
        let ms = MachineScheduler::new("test");
        let instructions = vec![make_mi(1, Some(1))];
        let graph = ms.build_dependence_graph(&instructions);
        assert_eq!(graph.node_count, 1);
        assert!(graph.nodes[0].predecessors.is_empty());
        assert!(graph.nodes[0].successors.is_empty());
    }

    #[test]
    fn test_build_dependence_graph_raw() {
        let ms = MachineScheduler::new("test");
        // instr 0: r1 = ...
        // instr 1: ... = r1 (RAW: 0 → 1)
        let instructions = vec![
            make_mi_with_ops(1, Some(1), vec![]),
            make_mi_with_ops(2, None, vec![MachineOperand::Reg(1)]),
        ];
        let graph = ms.build_dependence_graph(&instructions);
        assert_eq!(graph.node_count, 2);
        // Node 1 should depend on node 0 (RAW)
        assert!(graph.nodes[1].predecessors.contains(&0));
        assert!(graph.nodes[0].successors.contains(&1));
    }

    #[test]
    fn test_build_dependence_graph_waw() {
        let ms = MachineScheduler::new("test");
        // Both define r1 → WAW: 0 → 1
        let instructions = vec![make_mi(1, Some(1)), make_mi(2, Some(1))];
        let graph = ms.build_dependence_graph(&instructions);
        assert_eq!(graph.node_count, 2);
        assert!(graph.nodes[0].successors.contains(&1));
        assert!(graph.nodes[1].predecessors.contains(&0));
    }

    #[test]
    fn test_compute_critical_path_linear() {
        let ms = MachineScheduler::new("test");
        let mut graph = DepGraph::new();
        for i in 0..5 {
            graph.nodes.push(SchedNode {
                instr_idx: i,
                opcode: 1,
                latency: 2,
                critical_path: 0,
                successors: if i + 1 < 5 { vec![i + 1] } else { vec![] },
                predecessors: if i > 0 { vec![i - 1] } else { vec![] },
                scheduled: false,
                unscheduled_preds: if i > 0 { 1 } else { 0 },
            });
        }
        graph.node_count = 5;

        let cp = ms.compute_critical_path(&graph);
        assert_eq!(cp.len(), 5);
        // Linear chain: each node adds its latency
        assert_eq!(cp[4], 2); // last node
        assert_eq!(cp[3], 4); // latency 2 + succ's cp 2
        assert_eq!(cp[0], 10); // latency 2 * 5
    }

    #[test]
    fn test_list_schedule_simple() {
        let ms = MachineScheduler::new("test");
        let mut graph = DepGraph::new();
        for i in 0..3 {
            graph.nodes.push(SchedNode {
                instr_idx: i,
                opcode: 1,
                latency: 1,
                critical_path: 0,
                successors: Vec::new(),
                predecessors: Vec::new(),
                scheduled: false,
                unscheduled_preds: 0,
            });
        }
        graph.node_count = 3;

        // All independent → original order
        let cp = vec![1, 1, 1];
        let schedule = ms.list_schedule(&graph, &cp);
        assert_eq!(schedule.len(), 3);
        // Independent instructions, all ready at start, sorted by cp (equal)
        // then by original position
        assert_eq!(schedule, vec![0, 1, 2]);
    }

    #[test]
    fn test_list_schedule_with_deps() {
        let ms = MachineScheduler::new("test");
        let mut graph = DepGraph::new();
        // 0 → 1 → 2 (linear chain)
        graph.nodes.push(SchedNode {
            instr_idx: 0,
            opcode: 1,
            latency: 1,
            critical_path: 0,
            successors: vec![1],
            predecessors: vec![],
            scheduled: false,
            unscheduled_preds: 0,
        });
        graph.nodes.push(SchedNode {
            instr_idx: 1,
            opcode: 1,
            latency: 1,
            critical_path: 0,
            successors: vec![2],
            predecessors: vec![0],
            scheduled: false,
            unscheduled_preds: 1,
        });
        graph.nodes.push(SchedNode {
            instr_idx: 2,
            opcode: 1,
            latency: 1,
            critical_path: 0,
            successors: vec![],
            predecessors: vec![1],
            scheduled: false,
            unscheduled_preds: 1,
        });
        graph.node_count = 3;

        let cp = vec![3, 2, 1];
        let schedule = ms.list_schedule(&graph, &cp);
        assert_eq!(schedule, vec![0, 1, 2]);
    }

    #[test]
    fn test_get_instruction_latency() {
        let ms = MachineScheduler::new("test");
        let mi = MachineInstr::new(1);
        let lat = ms.get_instruction_latency(&mi);
        assert_eq!(lat, 1);
    }

    #[test]
    fn test_compute_resource_conflicts_empty() {
        let ms = MachineScheduler::new("test");
        let schedule: Vec<usize> = Vec::new();
        let instructions: Vec<MachineInstr> = Vec::new();
        let conflicts = ms.compute_resource_conflicts(&schedule, &instructions);
        assert!(conflicts.is_empty());
    }

    #[test]
    fn test_schedule_empty_function() {
        let mut ms = MachineScheduler::new("test");
        let mut mf = MachineFunction::new("empty");
        let count = ms.schedule(&mut mf);
        assert_eq!(count, 0);
    }

    #[test]
    fn test_schedule_single_block() {
        let mut ms = MachineScheduler::new("test");
        let mut mf = MachineFunction::new("func");
        let mut mbb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![make_mi(1, Some(1)), make_mi(2, None), make_mi(3, Some(2))],
            successors: Vec::new(),
        };
        mf.push_block(mbb);

        let count = ms.schedule(&mut mf);
        // All instructions independent, so schedule should just preserve order
        assert!(count >= 0);
        assert_eq!(mf.blocks[0].instructions.len(), 3);
    }

    #[test]
    fn test_hash_instruction() {
        let ms = MachineScheduler::new("test");
        let mi = make_mi(42, Some(7));
        let hash = ms.hash_instruction(&mi);
        assert_ne!(hash, 0);
    }

    #[test]
    fn test_reg_to_key() {
        let vreg = MachineOperand::Reg(5);
        let preg = MachineOperand::PhysReg(3);
        let imm = MachineOperand::Imm(42);

        assert_eq!(MachineScheduler::reg_to_key(&vreg), 5);
        assert_eq!(MachineScheduler::reg_to_key(&preg), 3 + (1u64 << 32));
        assert_eq!(MachineScheduler::reg_to_key(&imm), 0);
    }

    // --- Hazard Recognizer Tests ---

    #[test]
    fn test_hazard_recognizer_new() {
        let hr = HazardRecognizer::new(8, true);
        assert_eq!(hr.resource_count, 8);
        assert!(hr.forwarding_enabled);
        assert_eq!(hr.current_cycle, 0);
    }

    #[test]
    fn test_hazard_recognizer_default() {
        let hr = HazardRecognizer::default();
        assert_eq!(hr.resource_count, 8);
    }

    #[test]
    fn test_can_issue_no_hazards() {
        let hr = HazardRecognizer::new(4, false);
        let result = hr.can_issue(1, &[10], &[20], &[0]);
        assert_eq!(result, HazardKind::None);
    }

    #[test]
    fn test_can_issue_structural_hazard() {
        let mut hr = HazardRecognizer::new(4, false);
        hr.emit(1, &[10], &[], &[0]);
        // Same resource in same cycle should conflict
        let result = hr.can_issue(1, &[11], &[], &[0]);
        assert_eq!(result, HazardKind::Structural);
    }

    #[test]
    fn test_can_issue_raw_hazard() {
        let mut hr = HazardRecognizer::new(4, false);
        // Producer writes reg 10 with latency 3
        hr.set_latency(1, 3);
        hr.emit(1, &[10], &[], &[0]);
        hr.advance_cycle();
        // Consumer reads reg 10 at cycle 1, but it's not available until cycle 3
        let result = hr.can_issue(1, &[], &[10], &[0]);
        assert_eq!(result, HazardKind::RAW);
    }

    #[test]
    fn test_forwarding_reduces_raw_hazard() {
        let mut hr = HazardRecognizer::new(4, true);
        hr.set_latency(1, 1);
        hr.emit(1, &[10], &[], &[0]);
        hr.advance_cycle();
        // With forwarding, the result may be available sooner
        let result = hr.can_issue(1, &[], &[10], &[0]);
        // Distance of 1 cycle with forwarding should be OK
        assert_eq!(result, HazardKind::None);
    }

    #[test]
    fn test_hazard_reset() {
        let mut hr = HazardRecognizer::new(4, false);
        hr.emit(1, &[10], &[], &[0]);
        hr.reset();
        assert_eq!(hr.current_cycle, 0);
        assert!(hr.write_registers.is_empty());
        assert!(hr.resource_schedule.is_empty());
    }

    #[test]
    fn test_hazard_scheduled_cycles() {
        let mut hr = HazardRecognizer::new(4, false);
        assert_eq!(hr.scheduled_cycles(), 0);
        hr.advance_cycle();
        hr.advance_cycle();
        assert_eq!(hr.scheduled_cycles(), 2);
    }

    // --- Forwarding Model Tests ---

    #[test]
    fn test_forwarding_model_new() {
        let fm = ForwardingModel::new();
        assert!(fm.paths.is_empty());
        assert!(fm.base_latencies.is_empty());
    }

    #[test]
    fn test_forwarding_effective_latency_no_paths() {
        let mut fm = ForwardingModel::new();
        fm.set_latency(1, 3);
        assert_eq!(fm.get_effective_latency(1, 1), 3);
    }

    #[test]
    fn test_forwarding_reduces_latency() {
        let mut fm = ForwardingModel::new();
        fm.set_latency(1, 3);
        fm.add_path(ForwardingPath {
            from_stage: 2,
            to_stage: 1,
            latency_reduction: 1,
            universal: true,
            producer_opcodes: vec![],
            consumer_opcodes: vec![],
        });
        assert_eq!(fm.get_effective_latency(1, 1), 2);
    }

    #[test]
    fn test_forwarding_can_forward() {
        let mut fm = ForwardingModel::new();
        fm.set_latency(1, 3);
        fm.add_path(ForwardingPath {
            from_stage: 2,
            to_stage: 1,
            latency_reduction: 2,
            universal: false,
            producer_opcodes: vec![1],
            consumer_opcodes: vec![1],
        });
        assert!(fm.can_forward(1, 1));
        assert!(!fm.can_forward(2, 1));
    }

    #[test]
    fn test_forwarding_x86_64_default() {
        let fm = ForwardingModel::x86_64_default();
        assert!(!fm.paths.is_empty());
        assert!(fm.base_latencies.len() > 0);
    }

    #[test]
    fn test_forwarding_aarch64_default() {
        let fm = ForwardingModel::aarch64_default();
        assert!(!fm.paths.is_empty());
    }

    // --- MicroFusion Tests ---

    #[test]
    fn test_micro_fusion_new() {
        let mf = MicroFusion::new("x86_64");
        assert!(mf.enabled);
        assert_eq!(mf.target, "x86_64");
    }

    #[test]
    fn test_micro_fusion_disabled_empty_target() {
        let mf = MicroFusion::new("");
        assert!(!mf.enabled);
    }

    #[test]
    fn test_micro_fusion_can_fuse_load() {
        let mf = MicroFusion::x86_default();
        let addr = make_mi(8, Some(5)); // LEA defines r5
        let mem = make_mi_with_ops(2, None, vec![MachineOperand::Reg(5)]); // load uses r5
        assert!(mf.can_micro_fuse(&addr, &mem).is_some());
    }

    #[test]
    fn test_micro_fusion_cannot_fuse_no_def() {
        let mf = MicroFusion::x86_default();
        let addr = make_mi(8, None); // no def
        let mem = make_mi_with_ops(2, None, vec![MachineOperand::Reg(5)]);
        assert!(mf.can_micro_fuse(&addr, &mem).is_none());
    }

    // --- ClusterFormation Tests ---

    #[test]
    fn test_cluster_formation_new() {
        let cf = ClusterFormation::new(8);
        assert!(cf.enabled);
        assert_eq!(cf.max_cluster_size, 8);
        assert!(cf.clusters.is_empty());
    }

    #[test]
    fn test_cluster_formation_load_clusters() {
        let mut cf = ClusterFormation::new(8);
        let instrs = vec![
            make_mi_with_ops(2, Some(1), vec![MachineOperand::Reg(10)]),
            make_mi_with_ops(2, Some(2), vec![MachineOperand::Reg(10)]),
            make_mi_with_ops(2, Some(3), vec![MachineOperand::Reg(20)]),
        ];
        cf.form_clusters(&instrs);
        // Should form one load cluster (two loads with base 10)
        let loads = cf.load_clusters();
        assert_eq!(loads.len(), 1);
        assert_eq!(loads[0].instructions.len(), 2);
        assert_eq!(loads[0].base_register, 10);
    }

    #[test]
    fn test_cluster_formation_should_cluster() {
        let cf = ClusterFormation::new(8);
        let a = make_mi_with_ops(2, Some(1), vec![MachineOperand::Reg(10)]);
        let b = make_mi_with_ops(2, Some(2), vec![MachineOperand::Reg(10)]);
        assert!(cf.should_cluster(&a, &b));
    }

    // --- Scheduling Region Tests ---

    #[test]
    fn test_region_builder_new() {
        let rb = RegionBuilder::new("x86_64", false);
        assert_eq!(rb.region_count(), 0);
    }

    #[test]
    fn test_region_builder_empty() {
        let mut rb = RegionBuilder::new("generic", false);
        rb.build_regions(&[]);
        assert_eq!(rb.region_count(), 0);
    }

    #[test]
    fn test_region_builder_single_region() {
        let mut rb = RegionBuilder::new("generic", false);
        let instrs = vec![make_mi(1, Some(1)), make_mi(1, Some(2))];
        rb.build_regions(&instrs);
        assert_eq!(rb.region_count(), 1);
    }

    #[test]
    fn test_region_builder_split_on_branch() {
        let mut rb = RegionBuilder::new("generic", false);
        let instrs = vec![make_mi(1, Some(1)), make_mi(7, None), make_mi(1, Some(3))];
        rb.build_regions(&instrs);
        // Should split at the branch (opcode 7)
        assert!(rb.region_count() >= 1);
    }

    // --- Post-RA Scheduler Tests ---

    #[test]
    fn test_post_ra_scheduler_new() {
        let prs = PostRAScheduler::new("x86_64");
        assert_eq!(prs.target, "x86_64");
        assert!(prs.break_anti_deps);
    }

    #[test]
    fn test_post_ra_schedule_empty() {
        let mut prs = PostRAScheduler::new("test");
        let mut instrs: Vec<MachineInstr> = vec![];
        let count = prs.schedule(&mut instrs);
        assert_eq!(count, 0);
    }

    #[test]
    fn test_post_ra_schedule_single() {
        let mut prs = PostRAScheduler::new("test");
        let mut instrs = vec![make_mi(1, Some(1))];
        let count = prs.schedule(&mut instrs);
        assert_eq!(count, 0);
    }

    #[test]
    fn test_post_ra_break_anti_deps() {
        let prs = PostRAScheduler::new("test");
        let mut graph = DepGraph::new();
        // Create a WAR edge: instr 0 uses r5, instr 1 defines r5
        graph.nodes.push(SchedNode {
            instr_idx: 0,
            opcode: 1,
            latency: 1,
            critical_path: 0,
            successors: vec![1],
            predecessors: vec![],
            scheduled: false,
            unscheduled_preds: 0,
        });
        graph.nodes.push(SchedNode {
            instr_idx: 1,
            opcode: 1,
            latency: 1,
            critical_path: 0,
            successors: vec![],
            predecessors: vec![0],
            scheduled: false,
            unscheduled_preds: 1,
        });
        graph.node_count = 2;

        let mut instrs = vec![
            make_mi_with_ops(1, None, vec![MachineOperand::Reg(5)]),
            make_mi(1, Some(5)),
        ];
        let broken = prs.break_anti_dependencies(&mut graph, &mut instrs);
        assert!(broken >= 0);
    }

    // --- Reservation Table Tests ---

    #[test]
    fn test_reservation_table_new() {
        let rt = ReservationTable::new(5, 4, 1);
        assert_eq!(rt.num_stages, 5);
        assert_eq!(rt.num_units, 4);
        assert_eq!(rt.duration, 5);
    }

    #[test]
    fn test_reservation_table_reserve() {
        let mut rt = ReservationTable::new(5, 4, 1);
        rt.reserve(0, 0);
        assert!(rt.is_reserved(0, 0));
        assert!(!rt.is_reserved(0, 1));
    }

    #[test]
    fn test_reservation_table_collision() {
        let mut rt1 = ReservationTable::new(5, 4, 1);
        rt1.reserve(0, 0);
        let mut rt2 = ReservationTable::new(5, 4, 2);
        rt2.reserve(0, 0);
        // Same unit same cycle -> collision
        assert!(rt1.collides_with(&rt2, 0));
    }

    #[test]
    fn test_reservation_table_no_collision() {
        let mut rt1 = ReservationTable::new(5, 4, 1);
        rt1.reserve(0, 0);
        let mut rt2 = ReservationTable::new(5, 4, 2);
        rt2.reserve(0, 1);
        // Different units -> no collision
        assert!(!rt1.collides_with(&rt2, 0));
    }

    #[test]
    fn test_reservation_table_alu_default() {
        let rt = ReservationTable::alu_default();
        assert_eq!(rt.opcode, 1);
        assert!(rt.is_reserved(0, 0));
    }

    #[test]
    fn test_reservation_table_load_default() {
        let rt = ReservationTable::load_default();
        assert_eq!(rt.opcode, 2);
    }

    // --- Bidirectional Scheduler Tests ---

    #[test]
    fn test_bidirectional_scheduler_new() {
        let bs = BidirectionalScheduler::new("x86_64");
        assert_eq!(bs.target, "x86_64");
        assert!(bs.forward_schedule.is_empty());
        assert!(bs.backward_schedule.is_empty());
    }

    #[test]
    fn test_bidirectional_schedule_empty() {
        let mut bs = BidirectionalScheduler::new("test");
        let graph = DepGraph::new();
        let instrs: Vec<MachineInstr> = vec![];
        let result = bs.schedule(&graph, &instrs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_bidirectional_schedule_independent() {
        let mut bs = BidirectionalScheduler::new("test");
        let mut graph = DepGraph::new();
        for i in 0..3 {
            graph.nodes.push(SchedNode {
                instr_idx: i,
                opcode: 1,
                latency: 1,
                critical_path: 0,
                successors: vec![],
                predecessors: vec![],
                scheduled: false,
                unscheduled_preds: 0,
            });
        }
        graph.node_count = 3;
        let instrs = vec![
            make_mi(1, Some(0)),
            make_mi(1, Some(1)),
            make_mi(1, Some(2)),
        ];
        let result = bs.schedule(&graph, &instrs);
        assert_eq!(result.len(), 3);
    }

    // --- Itinerary Data Tests ---

    #[test]
    fn test_itinerary_data_new() {
        let id = ItineraryData::new("x86_64");
        assert_eq!(id.target, "x86_64");
        assert!(id.itineraries.is_empty());
    }

    #[test]
    fn test_itinerary_data_get_latency_unknown() {
        let id = ItineraryData::new("test");
        assert_eq!(id.get_latency(999), 1);
    }

    #[test]
    fn test_itinerary_data_x86_64_default() {
        let id = ItineraryData::x86_64_default();
        assert!(id.itineraries.len() > 0);
        // ALU should have latency
        let lat = id.get_latency(1);
        assert!(lat > 0);
        assert_eq!(id.get_num_micro_ops(1), 1);
    }

    // --- FullScheduleDAG Tests ---

    #[test]
    fn test_full_schedule_dag_empty() {
        let dag = FullScheduleDAG::from_basic_block(&[], "generic");
        assert_eq!(dag.graph.node_count, 0);
        assert_eq!(dag.estimate_length(), 0);
    }

    #[test]
    fn test_full_schedule_dag_basic() {
        let instrs = vec![
            make_mi(1, Some(1)),
            make_mi_with_ops(2, Some(2), vec![MachineOperand::Reg(1)]),
            make_mi_with_ops(3, None, vec![MachineOperand::Reg(2)]),
        ];
        let dag = FullScheduleDAG::from_basic_block(&instrs, "generic");
        assert_eq!(dag.graph.node_count, 3);
        // Should have some critical path length
        assert!(dag.estimate_length() > 0);
        // Should have at least one region
        assert!(dag.regions.len() >= 1);
    }

    #[test]
    fn test_full_schedule_dag_ready_nodes() {
        let instrs = vec![make_mi(1, Some(1)), make_mi(1, Some(2))];
        let dag = FullScheduleDAG::from_basic_block(&instrs, "generic");
        let scheduled = vec![false; 2];
        let ready = dag.ready_nodes(&scheduled);
        // All nodes should be ready (no dependences)
        assert_eq!(ready.len(), 2);
    }

    #[test]
    fn test_full_schedule_dag_depths() {
        let instrs = vec![
            make_mi(1, Some(1)),
            make_mi_with_ops(1, Some(2), vec![MachineOperand::Reg(1)]),
        ];
        let dag = FullScheduleDAG::from_basic_block(&instrs, "generic");
        assert_eq!(dag.depths[0], 0);
        assert!(dag.depths[1] > 0);
    }

    // --- ScheduleDAG Mutator Tests ---

    #[test]
    fn test_sched_dag_mutator_new() {
        let mutator = ScheduleDAGMutator::new();
        assert!(mutator.enforce_mem_order);
        assert!(mutator.break_anti_deps);
    }

    #[test]
    fn test_sched_dag_mutator_memory_barriers() {
        let mut mutator = ScheduleDAGMutator::new();
        let instrs = vec![
            make_mi(3, None),    // store
            make_mi(2, Some(1)), // load
        ];
        let mut graph = DepGraph::new();
        for i in 0..2 {
            graph.nodes.push(SchedNode {
                instr_idx: i,
                opcode: instrs[i].opcode,
                latency: 1,
                critical_path: 0,
                successors: vec![],
                predecessors: vec![],
                scheduled: false,
                unscheduled_preds: 0,
            });
        }
        graph.node_count = 2;
        mutator.add_memory_barriers(&mut graph, &instrs);
        assert!(mutator.added_edges > 0);
    }

    // --- Copy Suppressor Tests ---

    #[test]
    fn test_copy_suppressor_self_copy() {
        let mut suppressor = CopySuppressor::new();
        let mut instrs = vec![make_mi_with_ops(1, Some(3), vec![MachineOperand::Reg(3)])];
        suppressor.suppress_copies(&mut instrs);
        assert!(instrs.is_empty());
        assert_eq!(suppressor.copies_eliminated, 1);
    }

    // --- Load/Store Clusterer Tests ---

    #[test]
    fn test_load_clusterer_empty() {
        let mut clusterer = LoadClusterer::new();
        let mut instrs: Vec<MachineInstr> = vec![];
        clusterer.cluster_loads(&mut instrs);
        assert_eq!(clusterer.loads_clustered, 0);
    }

    #[test]
    fn test_store_clusterer_empty() {
        let mut clusterer = StoreClusterer::new();
        let mut instrs: Vec<MachineInstr> = vec![];
        clusterer.cluster_stores(&mut instrs);
        assert_eq!(clusterer.stores_clustered, 0);
    }

    // --- MacroFusion Detector Tests ---

    #[test]
    fn test_macro_fusion_skylake() {
        let mut detector = MacroFusionDetector::new("skylake");
        assert!(detector.support_fusion);
        assert!(detector.support_extended_fusion);
    }

    #[test]
    fn test_macro_fusion_detect_cmp_jcc() {
        let mut detector = MacroFusionDetector::new("skylake");
        let instrs = vec![make_mi(18, None), make_mi(16, None)]; // CMP + JE
        detector.detect_fusion_pairs(&instrs);
        assert_eq!(detector.fused_uop_count(), 1);
    }

    #[test]
    fn test_macro_fusion_detect_test_jcc() {
        let mut detector = MacroFusionDetector::new("icelake");
        let instrs = vec![make_mi(26, None), make_mi(17, None)]; // TEST + JNE
        detector.detect_fusion_pairs(&instrs);
        assert_eq!(detector.fused_uop_count(), 1);
    }

    // --- MicroFusion Detector Tests ---

    #[test]
    fn test_micro_fusion_detector_new() {
        let mut detector = MicroFusionDetector::new();
        assert!(detector.support_fusion);
        assert!(detector.load_op_fusions.is_empty());
    }

    #[test]
    fn test_micro_fusion_detect_load_op() {
        let mut detector = MicroFusionDetector::new();
        let instrs = vec![make_mi_with_ops(2, Some(1), vec![MachineOperand::Reg(0)])]; // ADD r, [mem]
        detector.detect_load_op_fusion(&instrs);
        assert!(detector.load_op_fusions.contains(&0));
    }

    // --- Loop Alignment Tests ---

    #[test]
    fn test_loop_aligner_compute_nops() {
        let aligner = LoopAligner::new(16);
        assert_eq!(aligner.compute_alignment_nops(0), 0);
        assert_eq!(aligner.compute_alignment_nops(1), 15);
        assert_eq!(aligner.compute_alignment_nops(16), 0);
        assert_eq!(aligner.compute_alignment_nops(17), 15);
    }

    #[test]
    fn test_loop_aligner_nop_sequence() {
        let seq = LoopAligner::generate_nop_sequence(0);
        assert!(seq.is_empty());
        let seq = LoopAligner::generate_nop_sequence(1);
        assert_eq!(seq.len(), 1);
        assert_eq!(seq[0], 0x90);
        let seq = LoopAligner::generate_nop_sequence(3);
        assert_eq!(seq.len(), 3);
    }

    #[test]
    fn test_loop_aligner_insert_nops() {
        let mut aligner = LoopAligner::new(16);
        let mut instrs = vec![make_mi(1, Some(1))];
        aligner.align_loop_header(1, &mut instrs);
        assert_eq!(instrs.len(), 2); // Original + NOP
        assert_eq!(instrs[0].opcode, 0); // NOP
    }

    // --- Prefix Optimizer Tests ---

    #[test]
    fn test_prefix_optimizer_new() {
        let opt = PrefixOptimizer::new();
        assert!(opt.optimize_rex);
        assert_eq!(opt.prefix_bytes_saved, 0);
    }

    // --- Sub-Register Liveness Tests ---

    #[test]
    fn test_subreg_liveness_overlap() {
        let mut liveness = SubRegLiveness::new();
        liveness.record_access(0, 5);
        liveness.record_access(0, 10);
        assert!(liveness.overlaps(0, 5, 10));
        assert!(!liveness.overlaps(0, 20, 25));
    }

    // --- VLIW Packet Formation Tests ---

    #[test]
    fn test_vliw_packet_formation_empty() {
        let mut formation = VLIWPacketFormation::new(4);
        let graph = DepGraph::new();
        formation.form_packets(&[], &graph);
        assert_eq!(formation.packet_count(), 0);
    }

    #[test]
    fn test_vliw_packet_formation_utilization() {
        let mut formation = VLIWPacketFormation::new(4);
        let mut graph = DepGraph::new();
        for i in 0..4 {
            graph.nodes.push(SchedNode {
                instr_idx: i,
                opcode: 1,
                latency: 1,
                critical_path: 0,
                successors: vec![],
                predecessors: vec![],
                scheduled: false,
                unscheduled_preds: 0,
            });
        }
        graph.node_count = 4;
        formation.form_packets(&[0, 1, 2, 3], &graph);
        assert_eq!(formation.packet_count(), 1);
        assert!(formation.utilization() > 0.0);
    }

    // --- Additional Load/Store Clusterer Tests ---

    #[test]
    fn test_load_clusterer_same_line() {
        let mut clusterer = LoadClusterer::new();
        let mut instrs = vec![
            make_mi_with_ops(2, Some(1), vec![MachineOperand::Imm(0)]),
            make_mi_with_ops(2, Some(2), vec![MachineOperand::Imm(8)]),
            make_mi_with_ops(2, Some(3), vec![MachineOperand::Imm(16)]),
        ];
        clusterer.cluster_loads(&mut instrs);
        assert_eq!(clusterer.loads_clustered, 3);
    }

    #[test]
    fn test_store_clusterer_same_line() {
        let mut clusterer = StoreClusterer::new();
        let mut instrs = vec![
            make_mi_with_ops(3, None, vec![MachineOperand::Imm(0)]),
            make_mi_with_ops(3, None, vec![MachineOperand::Imm(8)]),
        ];
        clusterer.cluster_stores(&mut instrs);
        assert_eq!(clusterer.stores_clustered, 2);
    }

    #[test]
    fn test_macro_fusion_add_jcc_skylake() {
        let mut detector = MacroFusionDetector::new("skylake");
        let instrs = vec![make_mi(2, Some(1)), make_mi(16, None)];
        detector.detect_fusion_pairs(&instrs);
        assert_eq!(detector.fused_uop_count(), 1);
    }

    #[test]
    fn test_macro_fusion_no_fusion_no_branch() {
        let mut detector = MacroFusionDetector::new("skylake");
        let instrs = vec![make_mi(18, None), make_mi(1, Some(1))];
        detector.detect_fusion_pairs(&instrs);
        assert_eq!(detector.fused_uop_count(), 0);
    }

    #[test]
    fn test_micro_fusion_store_detect() {
        let mut detector = MicroFusionDetector::new();
        let instrs = vec![make_mi_with_ops(
            3,
            None,
            vec![MachineOperand::Reg(0), MachineOperand::Imm(0)],
        )];
        detector.detect_store_fusion(&instrs);
        assert_eq!(detector.store_fusions.len(), 1);
    }

    #[test]
    fn test_loop_aligner_nop_sequence_all_sizes() {
        for n in 1..=9 {
            let seq = LoopAligner::generate_nop_sequence(n);
            assert_eq!(seq.len(), n as usize);
        }
    }

    #[test]
    fn test_copy_suppressor_propagate() {
        let mut suppressor = CopySuppressor::new();
        let mut instrs = vec![
            make_mi_with_ops(1, Some(4), vec![MachineOperand::Reg(3)]),
            make_mi_with_ops(2, None, vec![MachineOperand::Reg(4)]),
        ];
        suppressor.suppress_copies(&mut instrs);
        assert_eq!(instrs.len(), 1);
        if let MachineOperand::Reg(r) = instrs[0].operands[0] {
            assert_eq!(r, 3);
        }
    }

    #[test]
    fn test_subreg_liveness_record() {
        let mut liveness = SubRegLiveness::new();
        liveness.record_access(1, 3);
        liveness.record_access(1, 7);
        let (start, end) = liveness.live_ranges[&1];
        assert_eq!(start, 3);
        assert_eq!(end, 7);
    }

    #[test]
    fn test_vliw_packet_formation_with_deps() {
        let mut formation = VLIWPacketFormation::new(2);
        let mut graph = DepGraph::new();
        graph.nodes.push(SchedNode {
            instr_idx: 0,
            opcode: 1,
            latency: 1,
            critical_path: 0,
            successors: vec![1],
            predecessors: vec![],
            scheduled: false,
            unscheduled_preds: 0,
        });
        graph.nodes.push(SchedNode {
            instr_idx: 1,
            opcode: 1,
            latency: 1,
            critical_path: 0,
            successors: vec![],
            predecessors: vec![0],
            scheduled: false,
            unscheduled_preds: 1,
        });
        graph.node_count = 2;
        formation.form_packets(&[0, 1], &graph);
        assert_eq!(formation.packet_count(), 2);
    }
}