llvm-native-core 0.1.14

LLVM-native core semantic engine — IR, CodeGen, X86 MC, Clang frontend pipeline
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
//! X86 Subtarget — CPU feature detection, feature flags, scheduling models,
//! and feature-string parsing for the x86 architecture family.
//! Phase 10 — LLVM.TARGET.X86.1 Court.
//!
//! This module provides the `X86Subtarget` struct, which encapsulates all
//! target-specific configuration for code generation on x86/x86-64
//! processors. It determines which instruction set extensions are available
//! based on the CPU name and an optional +/-feature string, and exposes
//! helper methods that the instruction selector, frame lowering, and
//! other backend components query to make correct lowering decisions.
//!
//! ## CPU → Feature Mapping
//!
//! Recognised CPU names are matched against a built-in lookup table that
//! encodes the feature set for common Intel/AMD microarchitectures as well
//! as the x86-64 psABI microarchitecture levels (v1–v4).
//!
//! ## Feature String Parsing
//!
//! A feature string is a comma-separated list of feature names, each
//! optionally prefixed with `+` (enable) or `-` (disable). Features not
//! mentioned retain their default value for the selected CPU.
//!
//! Example: `"+sse4.2,-avx,+fma"` enables SSE4.2 and FMA, disables AVX.
//!
//! ## Clean-room reconstruction from:
//! - Intel® 64 and IA-32 Architectures Software Developer's Manual
//! - AMD64 Architecture Programmer's Manual
//! - x86-64 psABI (System V ABI AMD64 supplement)
//! - GCC / Clang `-march=` / `-mtune=` documentation
//! - Black-box oracle interrogation

use std::collections::HashSet;

// ============================================================================
// Scheduling Model & Tuning Enums
// ============================================================================

/// X86 CPU scheduling models for instruction itinerary and resource modelling.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum X86SchedModel {
    /// No specific scheduling model; use generic heuristics.
    Generic,
    /// Intel Pentium / P6 era microarchitecture.
    P6,
    /// Intel Pentium 4 (NetBurst).
    NetBurst,
    /// Intel Core / Penryn microarchitecture.
    Core2,
    /// Intel Nehalem.
    Nehalem,
    /// Intel Sandy Bridge.
    SandyBridge,
    /// Intel Ivy Bridge.
    IvyBridge,
    /// Intel Haswell.
    Haswell,
    /// Intel Broadwell.
    Broadwell,
    /// Intel Skylake client.
    SkylakeClient,
    /// Intel Skylake server (Skylake-SP / Skylake-X).
    SkylakeServer,
    /// Intel Cascade Lake.
    CascadeLake,
    /// Intel Cooper Lake.
    CooperLake,
    /// Intel Cannon Lake.
    CannonLake,
    /// Intel Ice Lake client.
    IceLakeClient,
    /// Intel Ice Lake server.
    IceLakeServer,
    /// Intel Tiger Lake.
    TigerLake,
    /// Intel Sapphire Rapids.
    SapphireRapids,
    /// Intel Emerald Rapids.
    EmeraldRapids,
    /// Intel Granite Rapids.
    GraniteRapids,
    /// Intel Alder Lake (Golden Cove + Gracemont).
    AlderLake,
    /// Intel Rocket Lake.
    RocketLake,
    /// Intel Meteor Lake.
    MeteorLake,
    /// Intel Arrow Lake.
    ArrowLake,
    /// Intel Lunar Lake.
    LunarLake,
    /// Intel Bonnell (Atom).
    Bonnell,
    /// Intel Silvermont.
    Silvermont,
    /// Intel Goldmont.
    Goldmont,
    /// Intel Goldmont Plus.
    GoldmontPlus,
    /// Intel Tremont.
    Tremont,
    /// Intel Gracemont.
    Gracemont,
    /// Intel Knights Landing.
    KnightsLanding,
    /// AMD K8 (Hammer).
    K8,
    /// AMD K10 (Barcelona).
    K10,
    /// AMD Bobcat.
    Bobcat,
    /// AMD Jaguar.
    Jaguar,
    /// AMD Bulldozer.
    Bulldozer,
    /// AMD Piledriver.
    Piledriver,
    /// AMD Steamroller.
    Steamroller,
    /// AMD Excavator.
    Excavator,
    /// AMD Zen 1.
    Zen1,
    /// AMD Zen 2.
    Zen2,
    /// AMD Zen 3.
    Zen3,
    /// AMD Zen 4.
    Zen4,
    /// AMD Zen 5.
    Zen5,
}

impl X86SchedModel {
    /// Human-readable name for the scheduling model.
    pub fn name(&self) -> &'static str {
        match self {
            X86SchedModel::Generic => "Generic",
            X86SchedModel::P6 => "P6",
            X86SchedModel::NetBurst => "NetBurst",
            X86SchedModel::Core2 => "Core2",
            X86SchedModel::Nehalem => "Nehalem",
            X86SchedModel::SandyBridge => "SandyBridge",
            X86SchedModel::IvyBridge => "IvyBridge",
            X86SchedModel::Haswell => "Haswell",
            X86SchedModel::Broadwell => "Broadwell",
            X86SchedModel::SkylakeClient => "SkylakeClient",
            X86SchedModel::SkylakeServer => "SkylakeServer",
            X86SchedModel::CascadeLake => "CascadeLake",
            X86SchedModel::CooperLake => "CooperLake",
            X86SchedModel::CannonLake => "CannonLake",
            X86SchedModel::IceLakeClient => "IceLakeClient",
            X86SchedModel::IceLakeServer => "IceLakeServer",
            X86SchedModel::TigerLake => "TigerLake",
            X86SchedModel::SapphireRapids => "SapphireRapids",
            X86SchedModel::EmeraldRapids => "EmeraldRapids",
            X86SchedModel::GraniteRapids => "GraniteRapids",
            X86SchedModel::AlderLake => "AlderLake",
            X86SchedModel::RocketLake => "RocketLake",
            X86SchedModel::MeteorLake => "MeteorLake",
            X86SchedModel::ArrowLake => "ArrowLake",
            X86SchedModel::LunarLake => "LunarLake",
            X86SchedModel::Bonnell => "Bonnell",
            X86SchedModel::Silvermont => "Silvermont",
            X86SchedModel::Goldmont => "Goldmont",
            X86SchedModel::GoldmontPlus => "GoldmontPlus",
            X86SchedModel::Tremont => "Tremont",
            X86SchedModel::Gracemont => "Gracemont",
            X86SchedModel::KnightsLanding => "KnightsLanding",
            X86SchedModel::K8 => "K8",
            X86SchedModel::K10 => "K10",
            X86SchedModel::Bobcat => "Bobcat",
            X86SchedModel::Jaguar => "Jaguar",
            X86SchedModel::Bulldozer => "Bulldozer",
            X86SchedModel::Piledriver => "Piledriver",
            X86SchedModel::Steamroller => "Steamroller",
            X86SchedModel::Excavator => "Excavator",
            X86SchedModel::Zen1 => "Zen1",
            X86SchedModel::Zen2 => "Zen2",
            X86SchedModel::Zen3 => "Zen3",
            X86SchedModel::Zen4 => "Zen4",
            X86SchedModel::Zen5 => "Zen5",
        }
    }
}

/// Processor scheduling resource widths for a given microarchitecture.
#[derive(Debug, Clone, Copy)]
pub struct X86SchedInfo {
    /// Instructions dispatched per cycle.
    pub dispatch: u32,
    /// Micro-ops issued per cycle.
    pub issue: u32,
    /// Micro-ops retired per cycle.
    pub retire: u32,
    /// Reorder buffer entries.
    pub rob_size: u32,
    /// Load buffer entries.
    pub ld_buf: u32,
    /// Store buffer entries.
    pub st_buf: u32,
}

// ============================================================================
// X86Subtarget
// ============================================================================

/// Encapsulates all target-specific configuration for x86/x86-64 code
/// generation.  This is the single source of truth that every other
/// backend component queries when it needs to know which instructions,
/// registers, or ABIs are legal for the selected subtarget.
#[derive(Debug, Clone, PartialEq)]
pub struct X86Subtarget {
    /// CPU name (e.g. "haswell", "znver3", "generic").
    pub cpu: String,

    /// Set of enabled feature flags (e.g. "sse2", "avx", "fma").
    pub features: HashSet<String>,

    /// Whether we are targeting 64-bit mode (x86-64 / AMD64).
    pub is_64_bit: bool,

    // ---- ISA feature booleans (SSE family) ----
    pub has_sse: bool,
    pub has_sse2: bool,
    pub has_sse3: bool,
    pub has_ssse3: bool,
    pub has_sse41: bool,
    pub has_sse42: bool,

    // ---- ISA feature booleans (AVX family) ----
    pub has_avx: bool,
    pub has_avx2: bool,
    pub has_avx512f: bool,
    pub has_avx512bw: bool,
    pub has_avx512dq: bool,
    pub has_avx512vl: bool,

    // ---- ISA feature booleans (other SIMD / scalar) ----
    pub has_fma: bool,
    pub has_f16c: bool,
    pub has_bmi: bool,
    pub has_bmi2: bool,
    pub has_lzcnt: bool,
    pub has_popcnt: bool,

    // ---- Legacy SIMD / x87 ----
    pub has_mmx: bool,
    pub has_3dnow: bool,
    pub has_x87: bool,

    // ---- Misc scalar features ----
    pub has_cmov: bool,
    pub has_fxr: bool,
    pub has_nopl: bool,
    pub has_sahf: bool,
    pub has_lahf_sahf: bool,

    // ---- Transactional memory / crypto / security ----
    pub has_rtm: bool,
    pub has_adx: bool,
    pub has_sha: bool,
    pub has_sgx: bool,
    pub has_cet: bool,

    // ---- XSAVE family ----
    pub has_xsave: bool,
    pub has_xsaveopt: bool,
    pub has_xsavec: bool,
    pub has_xsaves: bool,

    // ---- Other ISA extensions ----
    pub has_movbe: bool,
    pub has_rdrnd: bool,
    pub has_rdseed: bool,
    pub has_prfchw: bool,

    // ---- AMD-specific features ----
    pub has_mwaitx: bool,
    pub has_clzero: bool,
    pub has_clwb: bool,
    pub has_pku: bool,

    // ---- Crypto / GFNI ----
    pub has_vaes: bool,
    pub has_vpclmulqdq: bool,
    pub has_gfni: bool,

    // ---- Tuning parameters ----
    /// Required stack alignment in bytes (16 for SysV, 4 for some legacy).
    pub stack_alignment: u32,

    /// Maximum size threshold for inlining (heuristic; larger values
    /// make the inliner more aggressive).
    pub max_inline_size_threshold: u32,

    /// Preferred vector width in bits for auto-vectorisation.
    pub pref_vector_width: u32,

    /// When true, use software floating-point routines instead of
    /// hardware FP instructions.
    pub use_soft_float: bool,

    // ---- Extended feature flags (Phase 10 expansion) ----
    pub has_aes: bool,
    pub has_pclmul: bool,
    pub has_fsgsbase: bool,
    pub has_tbm: bool,
    pub has_hle: bool,
    pub has_clflushopt: bool,
    pub has_prefetchwt1: bool,
    pub has_invpcid: bool,
    pub has_smap: bool,
    pub has_smep: bool,
    pub has_mpx: bool,
    pub has_pt: bool,
    pub has_waitpkg: bool,
    pub has_cldemote: bool,
    pub has_movdiri: bool,
    pub has_movdir64b: bool,
    pub has_enqcmd: bool,
    pub has_serialize: bool,
    pub has_tsxldtrk: bool,
    pub has_uintr: bool,
    pub has_amx_bf16: bool,
    pub has_amx_int8: bool,
    pub has_amx_tile: bool,
    pub has_amx_fp16: bool,
    pub has_avx_vnni: bool,
    pub has_avx_ifma: bool,
    pub has_avx_ne_convert: bool,
    pub has_avx_vnni_int8: bool,
    pub has_cmpccxadd: bool,
    pub has_prefetchiti: bool,
    pub has_raoint: bool,
    pub has_sha512: bool,
    pub has_sm3: bool,
    pub has_sm4: bool,
    pub has_hreset: bool,
    pub has_kl: bool,
    pub has_widekl: bool,
    pub has_xop: bool,
    pub has_fma4: bool,

    // ---- Tuning flags ----
    pub pad_short_functions: bool,
    pub opt_for_size: bool,
    pub opt_for_speed: bool,
    pub slow_unaligned_mem_16: bool,
    pub slow_unaligned_mem_32: bool,
    pub slow_divide_32: bool,
    pub slow_divide_64: bool,
    pub lea_uses_ag: bool,
    pub use_inc_dec: bool,
    pub use_sqrt_estimate: bool,
    pub use_reciprocal_estimate: bool,
    pub use_div_eq_delay: bool,
    pub use_cmov_branch: bool,
    pub use_bsr: bool,

    // ---- Processor-specific scheduling ----
    pub dispatch_width: u32,
    pub issue_width: u32,
    pub retire_width: u32,
    pub reorder_buffer_size: u32,
    pub load_buffer_size: u32,
    pub store_buffer_size: u32,

    // ---- Scheduling model ----
    pub schedule_model: X86SchedModel,
}

impl Default for X86Subtarget {
    fn default() -> Self {
        Self::new("x86_64-unknown-linux-gnu", "generic", "")
    }
}

// ============================================================================
// Constructor & helpers
// ============================================================================

impl X86Subtarget {
    /// Construct an `X86Subtarget` from a target triple, CPU name, and
    /// feature string.
    ///
    /// - `triple`: e.g. `"x86_64-unknown-linux-gnu"` or `"i686-pc-windows-msvc"`.
    /// - `cpu`: e.g. `"haswell"`, `"generic"`, `"znver3"`.  Falls back to
    ///   `"generic"` when unrecognised.
    /// - `features`: optional +/-feature string (e.g. `"+sse4.2,-avx"`).
    pub fn new(triple: &str, cpu: &str, features: &str) -> Self {
        // Detect 64-bit mode from the triple.
        let is_64_bit = triple_is_64_bit(triple);

        // Resolve the feature set for the named CPU.
        let mut feature_set = features_for_cpu(cpu);

        // Ensure baseline features for the selected mode (applied before
        // feature-string parsing so user +/- overrides can disable them).
        if is_64_bit {
            // x86-64 mandates SSE2; also enable CMOV (implied by i686+).
            feature_set.insert("sse2".to_string());
            feature_set.insert("cmov".to_string());
        }

        // Apply +/- overrides from the feature string (takes priority over
        // both the CPU defaults and the mode baseline above).
        parse_feature_string(&mut feature_set, features);

        // Derive all boolean fields from the feature set.
        let has_sse = feature_set.contains("sse");
        let has_sse2 = feature_set.contains("sse2");
        let has_sse3 = feature_set.contains("sse3");
        let has_ssse3 = feature_set.contains("ssse3");
        let has_sse41 = feature_set.contains("sse4.1");
        let has_sse42 = feature_set.contains("sse4.2");
        let has_avx = feature_set.contains("avx");
        let has_avx2 = feature_set.contains("avx2");
        let has_avx512f = feature_set.contains("avx512f");
        let has_avx512bw = feature_set.contains("avx512bw");
        let has_avx512dq = feature_set.contains("avx512dq");
        let has_avx512vl = feature_set.contains("avx512vl");
        let has_fma = feature_set.contains("fma");
        let has_f16c = feature_set.contains("f16c");
        let has_bmi = feature_set.contains("bmi");
        let has_bmi2 = feature_set.contains("bmi2");
        let has_lzcnt = feature_set.contains("lzcnt");
        let has_popcnt = feature_set.contains("popcnt");
        let has_mmx = feature_set.contains("mmx");
        let has_3dnow = feature_set.contains("3dnow");
        let has_x87 = feature_set.contains("x87");
        let has_cmov = feature_set.contains("cmov");
        let has_fxr = feature_set.contains("fxr");
        let has_nopl = feature_set.contains("nopl");
        let has_sahf = feature_set.contains("sahf");
        let has_lahf_sahf = feature_set.contains("lahf_sahf");
        let has_rtm = feature_set.contains("rtm");
        let has_adx = feature_set.contains("adx");
        let has_sha = feature_set.contains("sha");
        let has_sgx = feature_set.contains("sgx");
        let has_cet = feature_set.contains("cet");
        let has_xsave = feature_set.contains("xsave");
        let has_xsaveopt = feature_set.contains("xsaveopt");
        let has_xsavec = feature_set.contains("xsavec");
        let has_xsaves = feature_set.contains("xsaves");
        let has_movbe = feature_set.contains("movbe");
        let has_rdrnd = feature_set.contains("rdrnd");
        let has_rdseed = feature_set.contains("rdseed");
        let has_prfchw = feature_set.contains("prfchw");
        let has_mwaitx = feature_set.contains("mwaitx");
        let has_clzero = feature_set.contains("clzero");
        let has_clwb = feature_set.contains("clwb");
        let has_pku = feature_set.contains("pku");
        let has_vaes = feature_set.contains("vaes");
        let has_vpclmulqdq = feature_set.contains("vpclmulqdq");
        let has_gfni = feature_set.contains("gfni");

        // ---- Extended feature flags ----
        let has_aes = feature_set.contains("aes");
        let has_pclmul = feature_set.contains("pclmul");
        let has_fsgsbase = feature_set.contains("fsgsbase");
        let has_tbm = feature_set.contains("tbm");
        let has_hle = feature_set.contains("hle");
        let has_clflushopt = feature_set.contains("clflushopt");
        let has_prefetchwt1 = feature_set.contains("prefetchwt1");
        let has_invpcid = feature_set.contains("invpcid");
        let has_smap = feature_set.contains("smap");
        let has_smep = feature_set.contains("smep");
        let has_mpx = feature_set.contains("mpx");
        let has_pt = feature_set.contains("pt");
        let has_waitpkg = feature_set.contains("waitpkg");
        let has_cldemote = feature_set.contains("cldemote");
        let has_movdiri = feature_set.contains("movdiri");
        let has_movdir64b = feature_set.contains("movdir64b");
        let has_enqcmd = feature_set.contains("enqcmd");
        let has_serialize = feature_set.contains("serialize");
        let has_tsxldtrk = feature_set.contains("tsxldtrk");
        let has_uintr = feature_set.contains("uintr");
        let has_amx_bf16 = feature_set.contains("amx-bf16");
        let has_amx_int8 = feature_set.contains("amx-int8");
        let has_amx_tile = feature_set.contains("amx-tile");
        let has_amx_fp16 = feature_set.contains("amx-fp16");
        let has_avx_vnni = feature_set.contains("avx-vnni");
        let has_avx_ifma = feature_set.contains("avx-ifma");
        let has_avx_ne_convert = feature_set.contains("avx-ne-convert");
        let has_avx_vnni_int8 = feature_set.contains("avx-vnni-int8");
        let has_cmpccxadd = feature_set.contains("cmpccxadd");
        let has_prefetchiti = feature_set.contains("prefetchiti");
        let has_raoint = feature_set.contains("rao-int");
        let has_sha512 = feature_set.contains("sha512");
        let has_sm3 = feature_set.contains("sm3");
        let has_sm4 = feature_set.contains("sm4");
        let has_hreset = feature_set.contains("hreset");
        let has_kl = feature_set.contains("kl");
        let has_widekl = feature_set.contains("widekl");
        let has_xop = feature_set.contains("xop");
        let has_fma4 = feature_set.contains("fma4");

        // ---- Tuning flags ----
        let (
            pad_short_functions,
            opt_for_size,
            opt_for_speed,
            slow_unaligned_mem_16,
            slow_unaligned_mem_32,
            slow_divide_32,
            slow_divide_64,
            lea_uses_ag,
            use_inc_dec,
            use_sqrt_estimate,
            use_reciprocal_estimate,
            use_div_eq_delay,
            use_cmov_branch,
            use_bsr,
        ) = tuning_flags_for_cpu(cpu, is_64_bit);
        let schedule_model = sched_model_for_cpu(cpu);
        let sched = sched_info_for_model(schedule_model);

        // ---- Tuning defaults ----
        let stack_alignment = if is_64_bit || has_avx { 16 } else { 4 };
        let pref_vector_width = if has_avx512f {
            512
        } else if has_avx {
            256
        } else if has_sse2 {
            128
        } else {
            0
        };
        let max_inline_size_threshold = default_inline_threshold(cpu);

        X86Subtarget {
            cpu: cpu.to_string(),
            features: feature_set,
            is_64_bit,
            has_sse,
            has_sse2,
            has_sse3,
            has_ssse3,
            has_sse41,
            has_sse42,
            has_avx,
            has_avx2,
            has_avx512f,
            has_avx512bw,
            has_avx512dq,
            has_avx512vl,
            has_fma,
            has_f16c,
            has_bmi,
            has_bmi2,
            has_lzcnt,
            has_popcnt,
            has_mmx,
            has_3dnow,
            has_x87,
            has_cmov,
            has_fxr,
            has_nopl,
            has_sahf,
            has_lahf_sahf,
            has_rtm,
            has_adx,
            has_sha,
            has_sgx,
            has_cet,
            has_xsave,
            has_xsaveopt,
            has_xsavec,
            has_xsaves,
            has_movbe,
            has_rdrnd,
            has_rdseed,
            has_prfchw,
            has_mwaitx,
            has_clzero,
            has_clwb,
            has_pku,
            has_vaes,
            has_vpclmulqdq,
            has_gfni,
            // ---- Extended ----
            has_aes,
            has_pclmul,
            has_fsgsbase,
            has_tbm,
            has_hle,
            has_clflushopt,
            has_prefetchwt1,
            has_invpcid,
            has_smap,
            has_smep,
            has_mpx,
            has_pt,
            has_waitpkg,
            has_cldemote,
            has_movdiri,
            has_movdir64b,
            has_enqcmd,
            has_serialize,
            has_tsxldtrk,
            has_uintr,
            has_amx_bf16,
            has_amx_int8,
            has_amx_tile,
            has_amx_fp16,
            has_avx_vnni,
            has_avx_ifma,
            has_avx_ne_convert,
            has_avx_vnni_int8,
            has_cmpccxadd,
            has_prefetchiti,
            has_raoint,
            has_sha512,
            has_sm3,
            has_sm4,
            has_hreset,
            has_kl,
            has_widekl,
            has_xop,
            has_fma4,
            // ---- Tuning ----
            pad_short_functions,
            opt_for_size,
            opt_for_speed,
            slow_unaligned_mem_16,
            slow_unaligned_mem_32,
            slow_divide_32,
            slow_divide_64,
            lea_uses_ag,
            use_inc_dec,
            use_sqrt_estimate,
            use_reciprocal_estimate,
            use_div_eq_delay,
            use_cmov_branch,
            use_bsr,
            // ---- Scheduling ----
            dispatch_width: sched.dispatch,
            issue_width: sched.issue,
            retire_width: sched.retire,
            reorder_buffer_size: sched.rob_size,
            load_buffer_size: sched.ld_buf,
            store_buffer_size: sched.st_buf,
            schedule_model,
            stack_alignment,
            max_inline_size_threshold,
            pref_vector_width,
            use_soft_float: false,
        }
    }

    // ========================================================================
    // Convenience query methods
    // ========================================================================

    /// Returns `true` if the subtarget is in 64-bit (long) mode.
    #[inline]
    pub fn in_64bit_mode(&self) -> bool {
        self.is_64_bit
    }

    /// Returns `true` if the subtarget is in 32-bit (protected) mode.
    #[inline]
    pub fn in_32bit_mode(&self) -> bool {
        !self.is_64_bit
    }

    /// Returns `true` if any level of SSE is available.
    #[inline]
    pub fn has_sse(&self) -> bool {
        self.has_sse
    }

    /// Returns `true` if SSE2 is available.
    #[inline]
    pub fn has_sse2(&self) -> bool {
        self.has_sse2
    }

    /// Returns `true` if SSE3 is available.
    #[inline]
    pub fn has_sse3(&self) -> bool {
        self.has_sse3
    }

    /// Returns `true` if SSSE3 is available.
    #[inline]
    pub fn has_ssse3(&self) -> bool {
        self.has_ssse3
    }

    /// Returns `true` if SSE4.1 is available.
    #[inline]
    pub fn has_sse41(&self) -> bool {
        self.has_sse41
    }

    /// Returns `true` if SSE4.2 is available.
    #[inline]
    pub fn has_sse42(&self) -> bool {
        self.has_sse42
    }

    /// Returns `true` if any level of AVX is available.
    #[inline]
    pub fn has_avx(&self) -> bool {
        self.has_avx
    }

    /// Returns `true` if AVX2 is available.
    #[inline]
    pub fn has_avx2(&self) -> bool {
        self.has_avx2
    }

    /// Returns `true` if AVX-512 Foundation is available.
    #[inline]
    pub fn has_avx512f(&self) -> bool {
        self.has_avx512f
    }

    /// Returns `true` if AVX-512BW is available.
    #[inline]
    pub fn has_avx512bw(&self) -> bool {
        self.has_avx512bw
    }

    /// Returns `true` if AVX-512DQ is available.
    #[inline]
    pub fn has_avx512dq(&self) -> bool {
        self.has_avx512dq
    }

    /// Returns `true` if AVX-512VL is available.
    #[inline]
    pub fn has_avx512vl(&self) -> bool {
        self.has_avx512vl
    }

    /// Returns `true` if any AVX-512 level is available.
    #[inline]
    pub fn has_avx512(&self) -> bool {
        self.has_avx512f
    }

    /// Returns `true` if FMA (Fused Multiply-Add) is available.
    #[inline]
    pub fn has_fma(&self) -> bool {
        self.has_fma
    }

    /// Returns `true` if F16C (half-precision conversion) is available.
    #[inline]
    pub fn has_f16c(&self) -> bool {
        self.has_f16c
    }

    /// Returns `true` if BMI is available.
    #[inline]
    pub fn has_bmi(&self) -> bool {
        self.has_bmi
    }

    /// Returns `true` if BMI2 is available.
    #[inline]
    pub fn has_bmi2(&self) -> bool {
        self.has_bmi2
    }

    /// Returns `true` if LZCNT is available.
    #[inline]
    pub fn has_lzcnt(&self) -> bool {
        self.has_lzcnt
    }

    /// Returns `true` if POPCNT is available.
    #[inline]
    pub fn has_popcnt(&self) -> bool {
        self.has_popcnt
    }

    /// Returns `true` if MMX is available.
    #[inline]
    pub fn has_mmx(&self) -> bool {
        self.has_mmx
    }

    /// Returns `true` if 3DNow! is available.
    #[inline]
    pub fn has_3dnow(&self) -> bool {
        self.has_3dnow
    }

    /// Returns `true` if x87 FPU is available.
    #[inline]
    pub fn has_x87(&self) -> bool {
        self.has_x87
    }

    /// Returns `true` if CMOV is available.
    #[inline]
    pub fn has_cmov(&self) -> bool {
        self.has_cmov
    }

    /// Returns `true` if RTM (Restricted Transactional Memory) is available.
    #[inline]
    pub fn has_rtm(&self) -> bool {
        self.has_rtm
    }

    /// Returns `true` if ADX (Multi-Precision Add-Carry) is available.
    #[inline]
    pub fn has_adx(&self) -> bool {
        self.has_adx
    }

    /// Returns `true` if SHA extensions are available.
    #[inline]
    pub fn has_sha(&self) -> bool {
        self.has_sha
    }

    /// Returns `true` if SGX is available.
    #[inline]
    pub fn has_sgx(&self) -> bool {
        self.has_sgx
    }

    /// Returns `true` if CET is available.
    #[inline]
    pub fn has_cet(&self) -> bool {
        self.has_cet
    }

    /// Returns `true` if XSAVE is available.
    #[inline]
    pub fn has_xsave(&self) -> bool {
        self.has_xsave
    }

    /// Returns `true` if MOVBE is available.
    #[inline]
    pub fn has_movbe(&self) -> bool {
        self.has_movbe
    }

    /// Returns `true` if RDRAND is available.
    #[inline]
    pub fn has_rdrnd(&self) -> bool {
        self.has_rdrnd
    }

    /// Returns `true` if RDSEED is available.
    #[inline]
    pub fn has_rdseed(&self) -> bool {
        self.has_rdseed
    }

    /// Returns `true` if VAES is available.
    #[inline]
    pub fn has_vaes(&self) -> bool {
        self.has_vaes
    }

    /// Returns `true` if VPCLMULQDQ is available.
    #[inline]
    pub fn has_vpclmulqdq(&self) -> bool {
        self.has_vpclmulqdq
    }

    /// Returns `true` if GFNI is available.
    #[inline]
    pub fn has_gfni(&self) -> bool {
        self.has_gfni
    }

    /// Returns `true` if CLZERO is available.
    #[inline]
    pub fn has_clzero(&self) -> bool {
        self.has_clzero
    }

    /// Returns `true` if MWAITX is available.
    #[inline]
    pub fn has_mwaitx(&self) -> bool {
        self.has_mwaitx
    }

    /// Returns `true` if CLWB is available.
    #[inline]
    pub fn has_clwb(&self) -> bool {
        self.has_clwb
    }

    /// Returns `true` if PKU is available.
    #[inline]
    pub fn has_pku(&self) -> bool {
        self.has_pku
    }

    /// Returns the required stack alignment in bytes.
    #[inline]
    pub fn get_stack_alignment(&self) -> u32 {
        self.stack_alignment
    }

    /// Returns the preferred vector width in bits.
    #[inline]
    pub fn get_pref_vector_width(&self) -> u32 {
        self.pref_vector_width
    }

    /// Returns the maximum inline size threshold.
    #[inline]
    pub fn get_max_inline_size_threshold(&self) -> u32 {
        self.max_inline_size_threshold
    }

    /// Returns `true` if soft float is used.
    #[inline]
    pub fn use_soft_float(&self) -> bool {
        self.use_soft_float
    }

    /// Returns `true` if the feature named `flag` is enabled.
    #[inline]
    pub fn has_feature(&self, flag: &str) -> bool {
        self.features.contains(flag)
    }

    /// Returns the set of all enabled features.
    pub fn get_feature_set(&self) -> &HashSet<String> {
        &self.features
    }

    /// Returns a string summarising the feature flags for display.
    pub fn feature_string(&self) -> String {
        let mut flags: Vec<&str> = Vec::new();

        if self.has_sse {
            flags.push("sse");
        }
        if self.has_sse2 {
            flags.push("sse2");
        }
        if self.has_sse3 {
            flags.push("sse3");
        }
        if self.has_ssse3 {
            flags.push("ssse3");
        }
        if self.has_sse41 {
            flags.push("sse4.1");
        }
        if self.has_sse42 {
            flags.push("sse4.2");
        }
        if self.has_avx {
            flags.push("avx");
        }
        if self.has_avx2 {
            flags.push("avx2");
        }
        if self.has_avx512f {
            flags.push("avx512f");
        }
        if self.has_avx512bw {
            flags.push("avx512bw");
        }
        if self.has_avx512dq {
            flags.push("avx512dq");
        }
        if self.has_avx512vl {
            flags.push("avx512vl");
        }
        if self.has_fma {
            flags.push("fma");
        }
        if self.has_f16c {
            flags.push("f16c");
        }
        if self.has_bmi {
            flags.push("bmi");
        }
        if self.has_bmi2 {
            flags.push("bmi2");
        }
        if self.has_lzcnt {
            flags.push("lzcnt");
        }
        if self.has_popcnt {
            flags.push("popcnt");
        }
        if self.has_mmx {
            flags.push("mmx");
        }
        if self.has_3dnow {
            flags.push("3dnow");
        }
        if self.has_x87 {
            flags.push("x87");
        }
        if self.has_cmov {
            flags.push("cmov");
        }
        if self.has_rtm {
            flags.push("rtm");
        }
        if self.has_adx {
            flags.push("adx");
        }
        if self.has_sha {
            flags.push("sha");
        }
        if self.has_sgx {
            flags.push("sgx");
        }
        if self.has_cet {
            flags.push("cet");
        }

        if flags.is_empty() {
            "none".to_string()
        } else {
            flags.join(",")
        }
    }
}

// ============================================================================
// Tuning flag tables
// ============================================================================

/// Returns tuning flag booleans for a given CPU and mode.
/// Returns a 14-tuple of (pad_short, opt_for_size, slow_unalign_16, slow_unalign_32,
/// slow_div32, slow_div64, lea_ag, use_incdec, sqrt_est, recip_est, div_eq_delay,
/// cmov_branch, use_bsr_explicit, opt_for_speed)
#[allow(clippy::too_many_arguments)]
fn tuning_flags_for_cpu(
    cpu: &str,
    _is_64_bit: bool,
) -> (
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
    bool,
) {
    let cpu_lower = cpu.to_lowercase();
    match cpu_lower.as_str() {
        // Intel big cores: Skylake/IceLake/AlderLake → modern tuning
        "skylake" | "skylake-avx512" | "cascadelake" | "cooperlake" | "icelake-client"
        | "icelake-server" | "cannonlake" | "rocketlake" | "tigerlake" | "alderlake"
        | "sapphirerapids" | "emeraldrapids" | "graniterapids" | "graniterapids-d"
        | "meteorlake" | "arrowlake" | "lunarlake" | "pantherlake" => {
            // Modern: no SLOW flags, use IncDec, use CMOV, no BSRev
            (
                false, false, false, false, false, false, false, true, false, false, false, true,
                false, true,
            )
        }
        // Intel pre-Skylake big cores
        "haswell" | "broadwell" => (
            false, false, false, false, false, false, false, true, false, false, false, true,
            false, true,
        ),
        "sandybridge" | "ivybridge" => (
            false, false, false, false, false, false, true, true, false, false, false, true, false,
            true,
        ),
        "nehalem" | "westmere" => (
            false, false, false, false, true, true, true, false, false, false, false, true, false,
            true,
        ),
        "core2" | "penryn" => (
            false, false, true, true, true, true, true, false, false, false, false, false, false,
            true,
        ),
        "pentium4" | "pentium4m" => (
            false, false, true, true, true, true, false, false, false, true, false, false, false,
            true,
        ),
        // Atom family
        "atom" | "bonnell" => (
            false, true, true, true, true, true, true, false, false, false, false, false, false,
            false,
        ),
        "silvermont" | "slm" | "goldmont" | "glm" | "goldmont-plus" | "glp" => (
            false, false, false, false, false, false, false, false, false, false, false, true,
            false, false,
        ),
        "tremont" | "tnt" | "gracemont" => (
            false, false, false, false, false, false, false, true, false, false, false, true,
            false, true,
        ),
        // AMD Zen family
        "znver1" | "znver2" | "znver3" | "znver4" | "znver5" => (
            true, false, false, false, false, false, true, false, true, true, false, true, false,
            true,
        ),
        // AMD pre-Zen
        "bdver1" | "bdver2" | "bdver3" | "bdver4" | "excavator" => (
            true, false, false, false, false, false, true, true, true, true, false, true, false,
            true,
        ),
        "btver1" | "btver2" => (
            true, true, false, false, true, true, true, true, false, false, false, false, false,
            false,
        ),
        // Legacy AMD
        "k8" | "opteron" | "athlon64" | "amdfam10" | "barcelona" => (
            false, false, true, true, true, false, true, false, false, false, false, false, false,
            true,
        ),
        // Via / Zhaoxin
        "c3" | "c7" | "nano" | "zxc" | "zxd" => (
            false, true, true, true, true, true, true, false, false, false, false, false, false,
            false,
        ),
        // Fallback (generic)
        _ => (
            false, false, false, false, false, false, false, true, false, false, false, true,
            false, true,
        ),
    }
}

/// Map a CPU name to a scheduling model.
fn sched_model_for_cpu(cpu: &str) -> X86SchedModel {
    let cpu_lower = cpu.to_lowercase();
    match cpu_lower.as_str() {
        // Intel big cores
        "pentium4" | "pentium4m" => X86SchedModel::NetBurst,
        "core2" | "penryn" => X86SchedModel::Core2,
        "nehalem" | "westmere" | "corei7" => X86SchedModel::Nehalem,
        "sandybridge" | "corei7-2" | "snb" => X86SchedModel::SandyBridge,
        "ivybridge" | "corei7-3" | "ivb" => X86SchedModel::IvyBridge,
        "haswell" | "corei7-4" | "hsw" => X86SchedModel::Haswell,
        "broadwell" | "corei7-5" | "bdw" => X86SchedModel::Broadwell,
        "skylake" | "corei7-6" | "skl" => X86SchedModel::SkylakeClient,
        "skylake-avx512" | "skx" => X86SchedModel::SkylakeServer,
        "cascadelake" | "cfl" => X86SchedModel::CascadeLake,
        "cooperlake" | "cpl" => X86SchedModel::CooperLake,
        "cannonlake" | "cnl" => X86SchedModel::CannonLake,
        "icelake-client" | "icl" => X86SchedModel::IceLakeClient,
        "icelake-server" | "icx" => X86SchedModel::IceLakeServer,
        "tigerlake" | "tgl" => X86SchedModel::TigerLake,
        "sapphirerapids" | "spr" => X86SchedModel::SapphireRapids,
        "emeraldrapids" | "emr" => X86SchedModel::EmeraldRapids,
        "graniterapids" | "gnr" => X86SchedModel::GraniteRapids,
        "graniterapids-d" | "gnr-d" => X86SchedModel::GraniteRapids,
        "alderlake" | "adl" => X86SchedModel::AlderLake,
        "rocketlake" | "rkl" => X86SchedModel::RocketLake,
        "meteorlake" | "mtl" => X86SchedModel::MeteorLake,
        "arrowlake" | "arl" => X86SchedModel::ArrowLake,
        "lunarlake" | "lnl" => X86SchedModel::LunarLake,
        // Intel Atom
        "atom" | "bonnell" => X86SchedModel::Bonnell,
        "silvermont" | "slm" => X86SchedModel::Silvermont,
        "goldmont" | "glm" => X86SchedModel::Goldmont,
        "goldmont-plus" | "glp" => X86SchedModel::GoldmontPlus,
        "tremont" | "tnt" => X86SchedModel::Tremont,
        "gracemont" => X86SchedModel::Gracemont,
        // Intel Xeon Phi
        "knl" | "knightslanding" => X86SchedModel::KnightsLanding,
        "knm" | "knightsmill" => X86SchedModel::KnightsLanding,
        // AMD
        "k8" | "opteron" | "athlon64" | "athlon-fx" => X86SchedModel::K8,
        "amdfam10" | "barcelona" => X86SchedModel::K10,
        "btver1" => X86SchedModel::Bobcat,
        "btver2" => X86SchedModel::Jaguar,
        "bdver1" => X86SchedModel::Bulldozer,
        "bdver2" => X86SchedModel::Piledriver,
        "bdver3" => X86SchedModel::Steamroller,
        "bdver4" | "excavator" | "bdver5" => X86SchedModel::Excavator,
        "znver1" => X86SchedModel::Zen1,
        "znver2" => X86SchedModel::Zen2,
        "znver3" => X86SchedModel::Zen3,
        "znver4" => X86SchedModel::Zen4,
        "znver5" => X86SchedModel::Zen5,
        // Legacy
        "pentium" | "pentiumpro" | "pentium2" | "pentium3" | "pentiumiii" => X86SchedModel::P6,
        "pentium-m" => X86SchedModel::P6,
        "i686" | "i586" | "i486" | "i386" => X86SchedModel::Generic,
        // VIA / Zhaoxin
        "c3" | "c7" | "nano" | "zxc" | "zxd" => X86SchedModel::Generic,
        // Fallback
        _ => X86SchedModel::Generic,
    }
}

/// Returns scheduling resource information for a given model.
fn sched_info_for_model(model: X86SchedModel) -> X86SchedInfo {
    match model {
        // Intel modern: Skylake through Granite Rapids derivatives
        X86SchedModel::SkylakeClient
        | X86SchedModel::SkylakeServer
        | X86SchedModel::CascadeLake
        | X86SchedModel::CooperLake
        | X86SchedModel::CannonLake
        | X86SchedModel::IceLakeClient
        | X86SchedModel::IceLakeServer
        | X86SchedModel::TigerLake
        | X86SchedModel::RocketLake
        | X86SchedModel::AlderLake
        | X86SchedModel::MeteorLake => X86SchedInfo {
            dispatch: 6,
            issue: 8,
            retire: 8,
            rob_size: 224,
            ld_buf: 72,
            st_buf: 56,
        },
        // Intel Haswell/Broadwell era
        X86SchedModel::Haswell | X86SchedModel::Broadwell => X86SchedInfo {
            dispatch: 4,
            issue: 8,
            retire: 4,
            rob_size: 192,
            ld_buf: 72,
            st_buf: 42,
        },
        // Intel Sandy/Ivy Bridge
        X86SchedModel::SandyBridge | X86SchedModel::IvyBridge => X86SchedInfo {
            dispatch: 4,
            issue: 6,
            retire: 4,
            rob_size: 168,
            ld_buf: 64,
            st_buf: 36,
        },
        // Intel Nehalem/Westmere
        X86SchedModel::Nehalem => X86SchedInfo {
            dispatch: 4,
            issue: 6,
            retire: 4,
            rob_size: 128,
            ld_buf: 48,
            st_buf: 32,
        },
        // Intel Sapphire/Emerald Rapids
        X86SchedModel::SapphireRapids | X86SchedModel::EmeraldRapids => X86SchedInfo {
            dispatch: 6,
            issue: 12,
            retire: 8,
            rob_size: 512,
            ld_buf: 192,
            st_buf: 114,
        },
        // Intel Granite Rapids
        X86SchedModel::GraniteRapids => X86SchedInfo {
            dispatch: 8,
            issue: 12,
            retire: 8,
            rob_size: 576,
            ld_buf: 240,
            st_buf: 128,
        },
        // Intel Arrow Lake / Lunar Lake
        X86SchedModel::ArrowLake | X86SchedModel::LunarLake => X86SchedInfo {
            dispatch: 8,
            issue: 10,
            retire: 8,
            rob_size: 448,
            ld_buf: 128,
            st_buf: 72,
        },
        // Intel Atom - Bonnell
        X86SchedModel::Bonnell => X86SchedInfo {
            dispatch: 2,
            issue: 2,
            retire: 2,
            rob_size: 0,
            ld_buf: 8,
            st_buf: 4,
        },
        // Intel Silvermont / Goldmont
        X86SchedModel::Silvermont | X86SchedModel::Goldmont | X86SchedModel::GoldmontPlus => {
            X86SchedInfo {
                dispatch: 2,
                issue: 3,
                retire: 2,
                rob_size: 32,
                ld_buf: 10,
                st_buf: 8,
            }
        }
        // Intel Tremont / Gracemont
        X86SchedModel::Tremont | X86SchedModel::Gracemont => X86SchedInfo {
            dispatch: 2,
            issue: 4,
            retire: 4,
            rob_size: 208,
            ld_buf: 32,
            st_buf: 22,
        },
        // Knights Landing
        X86SchedModel::KnightsLanding => X86SchedInfo {
            dispatch: 2,
            issue: 2,
            retire: 2,
            rob_size: 72,
            ld_buf: 32,
            st_buf: 20,
        },
        // AMD Zen 1
        X86SchedModel::Zen1 => X86SchedInfo {
            dispatch: 6,
            issue: 6,
            retire: 8,
            rob_size: 192,
            ld_buf: 72,
            st_buf: 44,
        },
        // AMD Zen 2
        X86SchedModel::Zen2 => X86SchedInfo {
            dispatch: 6,
            issue: 7,
            retire: 8,
            rob_size: 224,
            ld_buf: 84,
            st_buf: 48,
        },
        // AMD Zen 3
        X86SchedModel::Zen3 => X86SchedInfo {
            dispatch: 6,
            issue: 8,
            retire: 8,
            rob_size: 256,
            ld_buf: 116,
            st_buf: 64,
        },
        // AMD Zen 4
        X86SchedModel::Zen4 => X86SchedInfo {
            dispatch: 9,
            issue: 9,
            retire: 9,
            rob_size: 320,
            ld_buf: 118,
            st_buf: 64,
        },
        // AMD Zen 5
        X86SchedModel::Zen5 => X86SchedInfo {
            dispatch: 10,
            issue: 10,
            retire: 10,
            rob_size: 448,
            ld_buf: 144,
            st_buf: 88,
        },
        // AMD Bulldozer / Piledriver / Steamroller / Excavator
        X86SchedModel::Bulldozer | X86SchedModel::Piledriver => X86SchedInfo {
            dispatch: 4,
            issue: 4,
            retire: 4,
            rob_size: 0,
            ld_buf: 40,
            st_buf: 24,
        },
        X86SchedModel::Steamroller | X86SchedModel::Excavator => X86SchedInfo {
            dispatch: 4,
            issue: 4,
            retire: 4,
            rob_size: 0,
            ld_buf: 48,
            st_buf: 32,
        },
        // AMD Bobcat / Jaguar
        X86SchedModel::Bobcat | X86SchedModel::Jaguar => X86SchedInfo {
            dispatch: 2,
            issue: 2,
            retire: 2,
            rob_size: 0,
            ld_buf: 8,
            st_buf: 6,
        },
        // AMD K8 / K10
        X86SchedModel::K8 | X86SchedModel::K10 => X86SchedInfo {
            dispatch: 3,
            issue: 3,
            retire: 3,
            rob_size: 72,
            ld_buf: 32,
            st_buf: 20,
        },
        // Generic fallback: conservative single-issue
        _ => X86SchedInfo {
            dispatch: 1,
            issue: 1,
            retire: 1,
            rob_size: 0,
            ld_buf: 4,
            st_buf: 4,
        },
    }
}

// ============================================================================
// Feature dependency chains
// ============================================================================

/// Feature dependency chains define which features are implied by others.
/// E.g., AVX2 implies AVX, which implies SSE, and so on.
pub fn apply_feature_dependencies(features: &mut HashSet<String>) {
    // --- SSE → SSE2 → SSE3 → SSSE3 → SSE4.1 → SSE4.2 chain ---
    if features.contains("sse4.2") {
        features.insert("sse4.1".to_string());
    }
    if features.contains("sse4.1") {
        features.insert("ssse3".to_string());
    }
    if features.contains("ssse3") {
        features.insert("sse3".to_string());
    }
    if features.contains("sse3") {
        features.insert("sse2".to_string());
    }
    if features.contains("sse2") {
        features.insert("sse".to_string());
    }

    // --- AVX chain ---
    if features.contains("avx2") {
        features.insert("avx".to_string());
    }
    if features.contains("avx") {
        features.insert("sse4.2".to_string());
    }

    // --- AVX-512 chain: AVX512F → AVX512DQ → AVX512BW → AVX512VL ---
    // (Note: VL and DQ are independent of BW, but all depend on F)
    if features.contains("avx512vl") {
        features.insert("avx512f".to_string());
    }
    if features.contains("avx512bw") {
        features.insert("avx512f".to_string());
    }
    if features.contains("avx512dq") {
        features.insert("avx512f".to_string());
    }

    // --- FMA chain (requires AVX) ---
    if features.contains("fma") {
        features.insert("avx".to_string());
    }

    // --- F16C chain (requires AVX) ---
    if features.contains("f16c") {
        features.insert("avx".to_string());
    }

    // --- XSAVE chain ---
    if features.contains("xsavec") || features.contains("xsaves") || features.contains("xsaveopt") {
        features.insert("xsave".to_string());
    }

    // --- AMX depends on AVX-512 ---
    if features.contains("amx-tile")
        || features.contains("amx-bf16")
        || features.contains("amx-int8")
        || features.contains("amx-fp16")
    {
        features.insert("avx512f".to_string());
    }

    // --- SHA implies SSE2 ---
    if features.contains("sha") {
        features.insert("sse2".to_string());
    }

    // --- XOP / FMA4 imply AVX ---
    if features.contains("xop") || features.contains("fma4") {
        features.insert("avx".to_string());
    }

    // --- BMI2 implies BMI ---
    if features.contains("bmi2") {
        features.insert("bmi".to_string());
    }

    // --- CET implies SSE2 (shadow stack needs 64-bit mode) ---
    if features.contains("cet") {
        features.insert("sse2".to_string());
    }
}

/// Returns a human-readable description of all feature dependency chains.
pub fn feature_dependency_chains() -> Vec<(&'static str, Vec<&'static str>)> {
    vec![
        ("sse4.2", vec!["sse4.1"]),
        ("sse4.1", vec!["ssse3"]),
        ("ssse3", vec!["sse3"]),
        ("sse3", vec!["sse2"]),
        ("sse2", vec!["sse"]),
        ("avx2", vec!["avx"]),
        ("avx", vec!["sse4.2"]),
        ("avx512vl", vec!["avx512f"]),
        ("avx512bw", vec!["avx512f"]),
        ("avx512dq", vec!["avx512f"]),
        ("fma", vec!["avx"]),
        ("f16c", vec!["avx"]),
        ("xsavec", vec!["xsave"]),
        ("xsaves", vec!["xsave"]),
        ("xsaveopt", vec!["xsave"]),
        ("amx-tile", vec!["avx512f"]),
        ("amx-bf16", vec!["avx512f"]),
        ("amx-int8", vec!["avx512f"]),
        ("amx-fp16", vec!["avx512f"]),
        ("sha", vec!["sse2"]),
        ("xop", vec!["avx"]),
        ("fma4", vec!["avx"]),
        ("bmi2", vec!["bmi"]),
        ("cet", vec!["sse2"]),
    ]
}

// ============================================================================
// Feature set definitions for common CPU families
// ============================================================================

/// Returns the baseline feature set for a named CPU.
///
/// This lookup table encodes the instruction-set extensions available on
/// every recognised microarchitecture.  CPUs not listed fall back to the
/// `"generic"` baseline (x86-64-v1 level for 64-bit targets, bare i386 for
/// 32-bit targets).
pub fn features_for_cpu(cpu: &str) -> HashSet<String> {
    let cpu_lower = cpu.to_lowercase();
    let features: &[&str] = match cpu_lower.as_str() {
        // ====================================================================
        // Generic / baseline levels
        // ====================================================================
        "generic" => &[
            "cmov", "mmx", "sse", "sse2", "fxr", "nopl", "xsave", "xsaveopt",
        ],

        // ====================================================================
        // x86-64 psABI microarchitecture levels
        // ====================================================================
        // x86-64-v1: baseline (same as generic)
        "x86-64-v1" => &[
            "cmov", "mmx", "sse", "sse2", "fxr", "nopl", "xsave", "xsaveopt",
        ],

        // x86-64-v2: circa 2008 (Nehalem / Barcelona)
        // Adds: SSE3, SSSE3, SSE4.1, SSE4.2, POPCNT, LAHF-SAHF (64-bit)
        "x86-64-v2" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "lahf_sahf",
            "fxr",
            "nopl",
            "xsave",
            "xsaveopt",
        ],

        // x86-64-v3: circa 2013 (Haswell / Excavator)
        // Adds: AVX, AVX2, BMI, BMI2, FMA, F16C, LZCNT, MOVBE, XSAVEC
        "x86-64-v3" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "lahf_sahf",
            "avx",
            "avx2",
            "bmi",
            "bmi2",
            "fma",
            "f16c",
            "lzcnt",
            "movbe",
            "xsavec",
            "fxr",
            "nopl",
            "xsave",
            "xsaveopt",
        ],

        // x86-64-v4: circa 2017 (Skylake-X / Zen 4)
        // Adds: AVX-512F, AVX-512BW, AVX-512DQ, AVX-512VL
        "x86-64-v4" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "lahf_sahf",
            "avx",
            "avx2",
            "bmi",
            "bmi2",
            "fma",
            "f16c",
            "lzcnt",
            "movbe",
            "xsavec",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "fxr",
            "nopl",
            "xsave",
            "xsaveopt",
        ],

        // ====================================================================
        // Intel desktop / server
        // ====================================================================

        // Pentium 4 (NetBurst): SSE2, but no SSE3.
        "pentium4" | "pentium4m" => &["cmov", "mmx", "sse", "sse2", "fxr", "nopl"],

        // Core 2 (Merom / Conroe / Penryn): SSSE3, optionally SSE4.1.
        "core2" | "penryn" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "fxr", "nopl", "xsave", "xsaveopt",
        ],

        // Nehalem (1st gen Core i): SSE4.2, POPCNT.
        "nehalem" | "corei7" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "fxr",
            "nopl", "xsave", "xsaveopt",
        ],

        // Westmere: Nehalem + AES/PCLMUL.
        "westmere" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "fxr",
            "nopl", "xsave", "xsaveopt",
        ],

        // Sandy Bridge: AVX.
        "sandybridge" | "corei7-2" | "snb" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "xsave", "xsaveopt", "fxr", "nopl",
        ],

        // Ivy Bridge: Sandy Bridge + F16C, RDRAND.
        "ivybridge" | "corei7-3" | "ivb" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "f16c", "rdrnd", "xsave", "xsaveopt", "fxr", "nopl",
        ],

        // Haswell: AVX2, FMA, BMI, BMI2, LZCNT, MOVBE.
        "haswell" | "corei7-4" | "hsw" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "xsave", "xsaveopt",
            "xsavec", "fxr", "nopl",
        ],

        // Broadwell: Haswell + ADX, RDSEED, RTM.
        "broadwell" | "corei7-5" | "bdw" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "rdseed", "adx",
            "rtm", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // Skylake (client): Broadwell + MPX, SGX.
        "skylake" | "corei7-6" | "skl" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "rdseed", "adx",
            "rtm", "sgx", "xsave", "xsaveopt", "xsavec", "xsaves", "fxr", "nopl",
        ],

        // Skylake-AVX512 (Skylake-SP / Skylake-X): full AVX-512.
        "skylake-avx512" | "skx" | "corei7-6-avx512" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "rdseed", "adx",
            "rtm", "avx512f", "avx512bw", "avx512dq", "avx512vl", "xsave", "xsaveopt", "xsavec",
            "xsaves", "fxr", "nopl",
        ],

        // Cannon Lake: Skylake + AVX-512 (client) + SHA, GFNI, VAES.
        "cannonlake" | "cnl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Cascade Lake: Skylake-AVX512 + VNNI.
        "cascadelake" | "cfl" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "rdseed", "adx",
            "rtm", "avx512f", "avx512bw", "avx512dq", "avx512vl", "xsave", "xsaveopt", "xsavec",
            "xsaves", "fxr", "nopl",
        ],

        // Cooper Lake: Cascade Lake + BF16.
        "cooperlake" | "cpl" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "rdseed", "adx",
            "rtm", "avx512f", "avx512bw", "avx512dq", "avx512vl", "xsave", "xsaveopt", "xsavec",
            "xsaves", "fxr", "nopl",
        ],

        // Ice Lake (client): Sunny Cove, AVX-512, SHA, GFNI, VAES, VPCLMULQDQ.
        "icelake-client" | "icl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Ice Lake (server): adds more AVX-512 capabilities.
        "icelake-server" | "icx" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Alder Lake (Golden Cove + Gracemont hybrid).
        "alderlake" | "adl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "cet",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Rocket Lake: backported Cypress Cove (no AVX-512).
        "rocketlake" | "rkl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // ====================================================================
        // Intel Atom family
        // ====================================================================

        // Bonnell: SSSE3, no SSE4.
        "atom" | "bonnell" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "movbe", "fxr", "nopl", "xsave",
            "xsaveopt",
        ],

        // Silvermont: SSE4.2, POPCNT, MOVBE, RDRAND.
        "silvermont" | "slm" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "movbe",
            "rdrnd", "fxr", "nopl", "xsave", "xsaveopt",
        ],

        // Goldmont: Silvermont + SHA, RDSEED, XSAVEC.
        "goldmont" | "glm" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "movbe",
            "rdrnd", "rdseed", "sha", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // Goldmont Plus: Goldmont + PTWRITE, RDPID, SGX.
        "goldmont-plus" | "glp" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "movbe",
            "rdrnd", "rdseed", "sha", "sgx", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // Tremont: Goldmont Plus + GFNI, CLWB, WAITPKG.
        "tremont" | "tnt" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "movbe",
            "rdrnd", "rdseed", "sha", "gfni", "clwb", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // ====================================================================
        // Intel Xeon Phi (Knights Landing / Knights Mill)
        // ====================================================================

        // Knights Landing: AVX-512F/CD/ER/PF, no AVX-512BW/DQ/VL.
        "knl" | "knightslanding" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "avx512f", "rdrnd", "rdseed",
            "adx", "rtm", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // Knights Mill: KNL + VPOPCNTDQ.
        "knm" | "knightsmill" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "avx512f", "rdrnd", "rdseed",
            "adx", "rtm", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // ====================================================================
        // AMD Zen family
        // ====================================================================

        // Zen 1 (Naples / Summit Ridge / Raven Ridge): AVX2, no AVX-512.
        "znver1" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "rdseed", "adx",
            "clzero", "mwaitx", "sha", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // Zen 2 (Rome / Matisse): Zen 1 + CLWB, RDPID, WBNOINVD.
        "znver2" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "avx2", "fma", "f16c", "bmi", "bmi2", "lzcnt", "movbe", "rdrnd", "rdseed", "adx",
            "clzero", "mwaitx", "clwb", "sha", "xsave", "xsaveopt", "xsavec", "fxr", "nopl",
        ],

        // Zen 3 (Milan / Vermeer): Zen 2 + VAES, VPCLMULQDQ, PKU, CET-SS.
        "znver3" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "clzero",
            "mwaitx",
            "clwb",
            "pku",
            "vaes",
            "vpclmulqdq",
            "sha",
            "xsave",
            "xsaveopt",
            "xsavec",
            "fxr",
            "nopl",
        ],

        // Zen 4 (Genoa / Raphael): Zen 3 + AVX-512F/BW/DQ/VL, GFNI.
        "znver4" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "clzero",
            "mwaitx",
            "clwb",
            "pku",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "vaes",
            "vpclmulqdq",
            "gfni",
            "sha",
            "xsave",
            "xsaveopt",
            "xsavec",
            "fxr",
            "nopl",
        ],

        // ====================================================================
        // Legacy 32-bit CPUs
        // ====================================================================

        // i386: no CMOV, no MMX.
        "i386" => &["x87", "nopl"],

        // i486: adds some instructions but still no CMOV/MMX.
        "i486" => &["x87", "nopl"],

        // i586 / Pentium: adds MMX sometimes, but not baseline for all.
        "i586" | "pentium" => &["x87", "nopl"],

        // i686 / Pentium Pro / Pentium II: CMOV, MMX.
        "i686" | "pentiumpro" | "pentium2" => &["x87", "cmov", "mmx", "nopl"],

        // Pentium III: SSE.
        "pentium3" | "pentiumiii" => &["x87", "cmov", "mmx", "sse", "nopl"],

        // Pentium M: SSE2.
        "pentium-m" => &["x87", "cmov", "mmx", "sse", "sse2", "nopl"],

        // ====================================================================
        // AMD-specific legacy
        // ====================================================================
        "k6" => &["x87", "mmx", "nopl"],
        "k6-2" => &["x87", "mmx", "3dnow", "nopl"],
        "k6-3" => &["x87", "mmx", "3dnow", "nopl"],
        "athlon" | "athlon-xp" | "athlon-mp" => &["x87", "mmx", "3dnow", "sse", "nopl"],
        "athlon-4" => &["x87", "mmx", "3dnow", "sse", "sse2", "nopl"],
        "k8" | "opteron" | "athlon64" | "athlon-fx" => {
            &["x87", "cmov", "mmx", "sse", "sse2", "3dnow", "fxr", "nopl"]
        }
        "k8-sse3" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "3dnow", "fxr", "nopl",
        ],
        "amdfam10" | "barcelona" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "popcnt", "fxr",
            "nopl", "xsave", "xsaveopt",
        ],
        "btver1" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "f16c", "movbe", "lzcnt", "prfchw", "xsave", "xsaveopt", "fxr", "nopl",
        ],
        "btver2" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "f16c", "bmi", "bmi2", "fma", "movbe", "lzcnt", "prfchw", "rdrnd", "xsave",
            "xsaveopt", "fxr", "nopl",
        ],
        "bdver1" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "f16c", "bmi", "lzcnt", "prfchw", "xsave", "xsaveopt", "fxr", "nopl",
        ],
        "bdver2" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "f16c", "bmi", "bmi2", "fma", "lzcnt", "prfchw", "xsave", "xsaveopt", "fxr",
            "nopl",
        ],
        "bdver3" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "avx2", "f16c", "bmi", "bmi2", "fma", "lzcnt", "movbe", "prfchw", "xsave",
            "xsaveopt", "fxr", "nopl",
        ],
        "bdver4" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "avx2", "f16c", "bmi", "bmi2", "fma", "lzcnt", "movbe", "mwaitx", "rdrnd",
            "xsave", "xsaveopt", "fxr", "nopl",
        ],
        "excavator" | "bdver5" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "avx2", "f16c", "bmi", "bmi2", "fma", "lzcnt", "movbe", "mwaitx", "rdrnd",
            "xsave", "xsaveopt", "fxr", "nopl",
        ],

        // Zen 5 (Turin / Granite Ridge): Zen 4 + AVX-512 VP2INTERSECT, etc.
        "znver5" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "clzero",
            "mwaitx",
            "clwb",
            "pku",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "vaes",
            "vpclmulqdq",
            "gfni",
            "sha",
            "avx-vnni",
            "avx-ifma",
            "avx-ne-convert",
            "avx-vnni-int8",
            "prefetchiti",
            "xsave",
            "xsaveopt",
            "xsavec",
            "fxr",
            "nopl",
        ],

        // ====================================================================
        // Intel modern client/server (Tiger Lake onward)
        // ====================================================================

        // Tiger Lake: Ice Lake client + AVX-VNNI, VP2INTERSECT.
        "tigerlake" | "tgl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "avx-vnni",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Sapphire Rapids: Ice Lake server + AMX-BF16, AMX-INT8, AMX-TILE, AVX512-FP16, TSXLDTRK, SERIALIZE, CLDEMOTE, WAITPKG, ENQCMD.
        "sapphirerapids" | "spr" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "avx-vnni",
            "avx-ifma",
            "avx-vnni-int8",
            "amx-tile",
            "amx-bf16",
            "amx-int8",
            "amx-fp16",
            "serialize",
            "tsxldtrk",
            "cldemote",
            "waitpkg",
            "enqcmd",
            "movdiri",
            "movdir64b",
            "pku",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Emerald Rapids: Sapphire Rapids + CMPCCXADD, AVX-NE-CONVERT, PREFETCHITI, RAO-INT.
        "emeraldrapids" | "emr" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "avx-vnni",
            "avx-ifma",
            "avx-vnni-int8",
            "amx-tile",
            "amx-bf16",
            "amx-int8",
            "amx-fp16",
            "cmpccxadd",
            "avx-ne-convert",
            "prefetchiti",
            "rao-int",
            "serialize",
            "tsxldtrk",
            "cldemote",
            "waitpkg",
            "enqcmd",
            "movdiri",
            "movdir64b",
            "pku",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Granite Rapids: Emerald Rapids + AMX-FP16 (full), SHA512, SM3, SM4.
        "graniterapids" | "gnr" | "graniterapids-d" | "gnr-d" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "avx512f",
            "avx512bw",
            "avx512dq",
            "avx512vl",
            "avx-vnni",
            "avx-ifma",
            "avx-vnni-int8",
            "amx-tile",
            "amx-bf16",
            "amx-int8",
            "amx-fp16",
            "cmpccxadd",
            "avx-ne-convert",
            "prefetchiti",
            "rao-int",
            "sha512",
            "sm3",
            "sm4",
            "serialize",
            "tsxldtrk",
            "cldemote",
            "waitpkg",
            "enqcmd",
            "movdiri",
            "movdir64b",
            "pku",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Meteor Lake: Redwood Cove + Crestmont.
        "meteorlake" | "mtl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "rtm",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "cet",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Arrow Lake: Lion Cove + Skymont.
        "arrowlake" | "arl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "avx-vnni",
            "avx-ifma",
            "avx-ne-convert",
            "avx-vnni-int8",
            "cet",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Lunar Lake: Lion Cove + Skymont (mobile).
        "lunarlake" | "lnl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "avx-vnni",
            "avx-ifma",
            "avx-ne-convert",
            "avx-vnni-int8",
            "cet",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Panther Lake: cougar cove + darkmont.
        "pantherlake" | "ptl" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "avx-vnni",
            "avx-ifma",
            "avx-ne-convert",
            "avx-vnni-int8",
            "cet",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // Alder Lake - N (Gracemont only, no AVX-512, no RTM).
        "alderlake-n" | "adl-n" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "cet",
            "xsave",
            "xsaveopt",
            "xsavec",
            "xsaves",
            "fxr",
            "nopl",
        ],

        // ====================================================================
        // Intel Gracemont (Alder Lake E-core)
        // ====================================================================
        "gracemont" => &[
            "cmov",
            "mmx",
            "sse",
            "sse2",
            "sse3",
            "ssse3",
            "sse4.1",
            "sse4.2",
            "popcnt",
            "avx",
            "avx2",
            "fma",
            "f16c",
            "bmi",
            "bmi2",
            "lzcnt",
            "movbe",
            "rdrnd",
            "rdseed",
            "adx",
            "sha",
            "gfni",
            "vaes",
            "vpclmulqdq",
            "cet",
            "waitpkg",
            "cldemote",
            "serialize",
            "xsave",
            "xsaveopt",
            "xsavec",
            "fxr",
            "nopl",
        ],

        // ====================================================================
        // VIA Technologies
        // ====================================================================

        // VIA C3 (Nehemiah): CMOV, MMX, SSE.
        "c3" | "c3-2" => &["x87", "cmov", "mmx", "sse", "nopl"],

        // VIA C7 (Esther): SSE2, SSE3.
        "c7" => &["x87", "cmov", "mmx", "sse", "sse2", "sse3", "nopl"],

        // VIA Nano: SSSE3, SSE4.1.
        "nano" | "nano-3000" | "nano-x2" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "nopl",
        ],

        // ====================================================================
        // Zhaoxin (Shanghai Zhaoxin Semiconductor)
        // ====================================================================

        // Zhaoxin ZX-C (ZhangJiang): SSE4.2.
        "zxc" | "zhangjiang" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "nopl",
        ],

        // Zhaoxin ZX-D / KX-5000 (WuDaoKou): AVX, F16C.
        "zxd" | "wudaokou" => &[
            "x87", "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt",
            "avx", "f16c", "nopl",
        ],

        // ====================================================================
        // Intel Larrabee (cancelled, but present in GCC/LLVM)
        // ====================================================================
        "larrabee" => &[
            "cmov", "mmx", "sse", "sse2", "sse3", "ssse3", "sse4.1", "sse4.2", "popcnt", "avx",
            "xsave", "xsaveopt", "fxr", "nopl",
        ],

        // ====================================================================
        // Fallback: unknown CPU → generic baseline
        // ====================================================================
        _ => &[
            "cmov", "mmx", "sse", "sse2", "fxr", "nopl", "xsave", "xsaveopt",
        ],
    };

    features.iter().map(|s| s.to_string()).collect()
}

// ============================================================================
// Feature string parsing (+feature,-feature)
// ============================================================================

/// Parse a comma-separated +/-feature string and apply the overrides to
/// the given feature set.
///
/// - `+foo`   → enable feature `foo`
/// - `-foo`   → disable feature `foo`
/// - `foo`    → enable feature `foo` (bare name, same as `+foo`)
///
/// Unknown feature names are silently ignored (they don't cause errors).
pub fn parse_feature_string(features: &mut HashSet<String>, feature_str: &str) {
    if feature_str.is_empty() {
        return;
    }

    for token in feature_str.split(',') {
        let token = token.trim();
        if token.is_empty() {
            continue;
        }

        let (action, name) = if let Some(rest) = token.strip_prefix('+') {
            (true, rest.trim().to_lowercase())
        } else if let Some(rest) = token.strip_prefix('-') {
            (false, rest.trim().to_lowercase())
        } else {
            // Bare name → enable.
            (true, token.to_lowercase())
        };

        // Normalise alternate spellings.
        let name = normalise_feature_name(&name);

        if action {
            features.insert(name.to_string());
        } else {
            features.remove(name);
        }
    }
}

/// Normalise common alternate spellings / aliases for feature names.
fn normalise_feature_name(name: &str) -> &str {
    match name {
        "sse4.1" | "sse4_1" | "sse41" => "sse4.1",
        "sse4.2" | "sse4_2" | "sse42" => "sse4.2",
        "avx512f" | "avx-512f" => "avx512f",
        "avx512bw" | "avx-512bw" => "avx512bw",
        "avx512dq" | "avx-512dq" => "avx512dq",
        "avx512vl" | "avx-512vl" => "avx512vl",
        "vpclmulqdq" | "vpclmul" => "vpclmulqdq",
        "lahf_sahf" | "sahf_lahf" => "lahf_sahf",
        other => other,
    }
}

// ============================================================================
// Utility: detect 64-bit from the triple string
// ============================================================================

/// Heuristically determine whether a target triple string represents a
/// 64-bit x86 target.
///
/// Recognised 64-bit arch names: `x86_64`, `amd64`, `x86-64`.
/// Everything else (including `i386`, `i686`, `x86`) is treated as 32-bit.
fn triple_is_64_bit(triple: &str) -> bool {
    let triple_lower = triple.to_lowercase();

    // Handle "x86-64" specially since it contains a hyphen.
    if triple_lower.starts_with("x86-64") {
        return true;
    }

    let arch = triple_lower.split('-').next().unwrap_or("");

    matches!(arch, "x86_64" | "amd64")
}

// ============================================================================
// Utility: default inline threshold per CPU
// ============================================================================

/// Returns a sensible default inline-size threshold for a given CPU.
///
/// Larger cores (Haswell-class and above) benefit from a slightly higher
/// threshold because they have more execution resources and better branch
/// prediction.  Smaller / low-power cores (Atom family, pre-Haswell) get a
/// lower threshold to avoid code-bloat.
fn default_inline_threshold(cpu: &str) -> u32 {
    let cpu_lower = cpu.to_lowercase();
    match cpu_lower.as_str() {
        // Aggressive inlining on big cores.
        "haswell" | "broadwell" | "skylake" | "skylake-avx512" | "cannonlake" | "cascadelake"
        | "icelake-client" | "icelake-server" | "rocketlake" | "znver1" | "znver2" | "znver3"
        | "znver4" => 250,

        // Moderate on mid-range / older.
        "sandybridge" | "ivybridge" | "nehalem" | "westmere" | "core2" | "penryn" | "bdver1"
        | "bdver2" | "bdver3" | "bdver4" | "excavator" => 200,

        // Conservative on small cores.
        "atom" | "silvermont" | "goldmont" | "goldmont-plus" | "tremont" | "btver1" | "btver2" => {
            150
        }

        // Bare minimum for legacy.
        _ => 180,
    }
}

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

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

    // ------------------------------------------------------------------
    // Constructor / triple detection
    // ------------------------------------------------------------------

    #[test]
    fn test_detect_64bit_triple() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert!(st.is_64_bit);
        assert!(st.in_64bit_mode());
        assert!(!st.in_32bit_mode());
    }

    #[test]
    fn test_detect_32bit_triple() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "generic", "");
        assert!(!st.is_64_bit);
        assert!(!st.in_64bit_mode());
        assert!(st.in_32bit_mode());
    }

    #[test]
    fn test_detect_amd64_triple() {
        let st = X86Subtarget::new("amd64-unknown-freebsd", "generic", "");
        assert!(st.is_64_bit);
    }

    // ------------------------------------------------------------------
    // CPU → feature mapping
    // ------------------------------------------------------------------

    #[test]
    fn test_generic_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert!(st.has_sse2());
        assert!(st.has_cmov());
        assert!(st.has_mmx());
        assert!(!st.has_avx());
        assert!(!st.has_avx2());
    }

    #[test]
    fn test_nehalem_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "nehalem", "");
        assert!(st.has_sse2());
        assert!(st.has_sse3());
        assert!(st.has_ssse3());
        assert!(st.has_sse41());
        assert!(st.has_sse42());
        assert!(st.has_popcnt());
        assert!(!st.has_avx());
    }

    #[test]
    fn test_haswell_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "haswell", "");
        assert!(st.has_sse2());
        assert!(st.has_avx());
        assert!(st.has_avx2());
        assert!(st.has_fma());
        assert!(st.has_f16c());
        assert!(st.has_bmi());
        assert!(st.has_bmi2());
        assert!(st.has_lzcnt());
        assert!(st.has_movbe());
        assert!(st.has_rdrnd());
        assert!(!st.has_avx512f());
    }

    #[test]
    fn test_skylake_avx512_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "skylake-avx512", "");
        assert!(st.has_avx2());
        assert!(st.has_avx512f());
        assert!(st.has_avx512bw());
        assert!(st.has_avx512dq());
        assert!(st.has_avx512vl());
        assert!(st.has_avx512());
    }

    #[test]
    fn test_znver3_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver3", "");
        assert!(st.has_avx2());
        assert!(st.has_vaes());
        assert!(st.has_vpclmulqdq());
        assert!(st.has_clzero());
        assert!(st.has_mwaitx());
        assert!(st.has_pku());
        assert!(st.has_sha());
        assert!(!st.has_avx512f());
    }

    #[test]
    fn test_znver4_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver4", "");
        assert!(st.has_avx2());
        assert!(st.has_avx512f());
        assert!(st.has_avx512bw());
        assert!(st.has_avx512dq());
        assert!(st.has_avx512vl());
        assert!(st.has_gfni());
        assert!(st.has_vaes());
    }

    #[test]
    fn test_knl_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "knl", "");
        assert!(st.has_avx512f());
        assert!(!st.has_avx512bw());
        assert!(!st.has_avx512dq());
        assert!(!st.has_avx512vl());
    }

    #[test]
    fn test_atom_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "atom", "");
        assert!(st.has_sse3());
        assert!(st.has_ssse3());
        assert!(!st.has_sse41());
        assert!(!st.has_avx());
        assert!(st.has_movbe());
    }

    #[test]
    fn test_silvermont_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "silvermont", "");
        assert!(st.has_sse41());
        assert!(st.has_sse42());
        assert!(st.has_popcnt());
        assert!(!st.has_avx());
    }

    #[test]
    fn test_tremont_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "tremont", "");
        assert!(st.has_sse42());
        assert!(st.has_sha());
        assert!(st.has_gfni());
        assert!(st.has_clwb());
        assert!(!st.has_avx());
    }

    #[test]
    fn test_i386_baseline() {
        let st = X86Subtarget::new("i386-unknown-linux-gnu", "i386", "");
        assert!(st.has_x87());
        assert!(!st.has_cmov());
        assert!(!st.has_mmx());
        assert!(!st.has_sse());
        assert!(!st.has_sse2());
    }

    #[test]
    fn test_i686_features() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "i686", "");
        assert!(st.has_cmov());
        assert!(st.has_mmx());
        assert!(!st.has_sse());
    }

    #[test]
    fn test_pentium4_features() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "pentium4", "");
        assert!(st.has_sse2());
        assert!(!st.has_sse3());
    }

    #[test]
    fn test_core2_features() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "core2", "");
        assert!(st.has_sse3());
        assert!(st.has_ssse3());
        assert!(!st.has_sse41());
    }

    // ------------------------------------------------------------------
    // x86-64 psABI levels
    // ------------------------------------------------------------------

    #[test]
    fn test_x86_64_v1() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "x86-64-v1", "");
        assert!(st.has_sse2());
        assert!(!st.has_sse3());
        assert!(!st.has_avx());
    }

    #[test]
    fn test_x86_64_v2() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "x86-64-v2", "");
        assert!(st.has_sse42());
        assert!(st.has_popcnt());
        assert!(!st.has_avx());
    }

    #[test]
    fn test_x86_64_v3() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "x86-64-v3", "");
        assert!(st.has_avx2());
        assert!(st.has_bmi());
        assert!(st.has_fma());
        assert!(!st.has_avx512f());
    }

    #[test]
    fn test_x86_64_v4() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "x86-64-v4", "");
        assert!(st.has_avx512f());
        assert!(st.has_avx512bw());
        assert!(st.has_avx512dq());
        assert!(st.has_avx512vl());
    }

    // ------------------------------------------------------------------
    // Feature string parsing (+/- overrides)
    // ------------------------------------------------------------------

    #[test]
    fn test_feature_string_enable() {
        // Start with generic (no AVX) and explicitly enable it.
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "+avx,+fma");
        assert!(st.has_avx());
        assert!(st.has_fma());
        assert!(st.has_sse2()); // still on from generic
    }

    #[test]
    fn test_feature_string_disable() {
        // Start with Haswell (has AVX2) and disable AVX.
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "haswell", "-avx,-avx2");
        assert!(!st.has_avx());
        assert!(!st.has_avx2());
        assert!(st.has_sse2()); // unaffected
    }

    #[test]
    fn test_feature_string_mixed() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "nehalem", "+avx,-popcnt,+rdrnd");
        assert!(st.has_avx());
        assert!(!st.has_popcnt());
        assert!(st.has_rdrnd());
        assert!(st.has_sse42()); // still on from nehalem
    }

    #[test]
    fn test_feature_string_bare_names() {
        // Bare "avx" (no +) same as "+avx"
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "avx,bmi");
        assert!(st.has_avx());
        assert!(st.has_bmi());
    }

    #[test]
    fn test_feature_string_empty() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert!(st.has_sse2());
    }

    #[test]
    fn test_feature_string_alias_sse41() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "+sse41,+sse42");
        assert!(st.has_sse41());
        assert!(st.has_sse42());
    }

    #[test]
    fn test_feature_string_whitespace() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", " +avx , -sse2 ");
        assert!(st.has_avx());
        // sse2 was enabled by default for 64-bit → now disabled.
        assert!(!st.has_sse2());
    }

    // ------------------------------------------------------------------
    // Stack alignment
    // ------------------------------------------------------------------

    #[test]
    fn test_stack_alignment_64bit() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert_eq!(st.get_stack_alignment(), 16);
    }

    #[test]
    fn test_stack_alignment_32bit_no_avx() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "generic", "");
        assert_eq!(st.get_stack_alignment(), 4);
    }

    #[test]
    fn test_stack_alignment_32bit_with_avx() {
        // Enable AVX on 32-bit → stack should be 16-byte aligned.
        let st = X86Subtarget::new("i686-pc-linux-gnu", "generic", "+avx");
        assert_eq!(st.get_stack_alignment(), 16);
    }

    // ------------------------------------------------------------------
    // Vector width
    // ------------------------------------------------------------------

    #[test]
    fn test_vector_width_none() {
        let st = X86Subtarget::new("i386-unknown-linux-gnu", "i386", "");
        assert_eq!(st.get_pref_vector_width(), 0);
    }

    #[test]
    fn test_vector_width_sse2() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert_eq!(st.get_pref_vector_width(), 128);
    }

    #[test]
    fn test_vector_width_avx() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "sandybridge", "");
        assert_eq!(st.get_pref_vector_width(), 256);
    }

    #[test]
    fn test_vector_width_avx512() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "skylake-avx512", "");
        assert_eq!(st.get_pref_vector_width(), 512);
    }

    // ------------------------------------------------------------------
    // Inline threshold
    // ------------------------------------------------------------------

    #[test]
    fn test_inline_threshold_haswell() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "haswell", "");
        assert_eq!(st.get_max_inline_size_threshold(), 250);
    }

    #[test]
    fn test_inline_threshold_atom() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "atom", "");
        assert_eq!(st.get_max_inline_size_threshold(), 150);
    }

    #[test]
    fn test_inline_threshold_nehalem() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "nehalem", "");
        assert_eq!(st.get_max_inline_size_threshold(), 200);
    }

    // ------------------------------------------------------------------
    // Soft float
    // ------------------------------------------------------------------

    #[test]
    fn test_soft_float_default() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert!(!st.use_soft_float());
    }

    // ------------------------------------------------------------------
    // Feature set query
    // ------------------------------------------------------------------

    #[test]
    fn test_has_feature() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "haswell", "");
        assert!(st.has_feature("avx2"));
        assert!(!st.has_feature("avx512f"));
    }

    #[test]
    fn test_get_feature_set() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let fs = st.get_feature_set();
        assert!(fs.contains("sse2"));
        assert!(fs.contains("cmov"));
    }

    // ------------------------------------------------------------------
    // Feature string formatting
    // ------------------------------------------------------------------

    #[test]
    fn test_feature_string_output() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let s = st.feature_string();
        assert!(s.contains("sse2"));
        assert!(s.contains("cmov"));
    }

    // ------------------------------------------------------------------
    // Unknown CPU fallback
    // ------------------------------------------------------------------

    #[test]
    fn test_unknown_cpu_fallback() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "madeup-cpu-name", "");
        // Should fall back to generic baseline.
        assert!(st.has_sse2());
        assert!(st.has_cmov());
        assert!(!st.has_avx());
    }

    // ------------------------------------------------------------------
    // Aliased CPU names
    // ------------------------------------------------------------------

    #[test]
    fn test_cpu_alias_snb() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "snb", "");
        assert!(st.has_avx());
        assert!(!st.has_avx2());
    }

    #[test]
    fn test_cpu_alias_skx() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "skx", "");
        assert!(st.has_avx512f());
    }

    // ------------------------------------------------------------------
    // AMD-specific features
    // ------------------------------------------------------------------

    #[test]
    fn test_amd_znver1_clzero() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver1", "");
        assert!(st.has_feature("clzero"));
        assert!(st.has_feature("mwaitx"));
        assert!(!st.has_clwb()); // added in znver2
    }

    #[test]
    fn test_amd_znver2_clwb() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver2", "");
        assert!(st.has_clwb());
        assert!(!st.has_pku()); // added in znver3
    }

    #[test]
    fn test_bdver4_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "bdver4", "");
        assert!(st.has_avx2());
        assert!(st.has_mwaitx());
        assert!(!st.has_avx512f());
    }

    // ------------------------------------------------------------------
    // tls / sahf variants
    // ------------------------------------------------------------------

    #[test]
    fn test_lahf_sahf() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "x86-64-v2", "");
        assert!(st.has_lahf_sahf);
        assert!(st.has_feature("lahf_sahf"));
    }

    #[test]
    fn test_feature_alias_lahf_sahf() {
        let mut fs = HashSet::new();
        parse_feature_string(&mut fs, "+sahf_lahf");
        assert!(fs.contains("lahf_sahf"));
    }

    // ------------------------------------------------------------------
    // CET / SGX / SHA / GFNI
    // ------------------------------------------------------------------

    #[test]
    fn test_cannonlake_security_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "cannonlake", "");
        assert!(st.has_sha());
        assert!(st.has_gfni());
        assert!(st.has_vaes());
        assert!(st.has_vpclmulqdq());
    }

    #[test]
    fn test_alderlake_cet() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "alderlake", "");
        assert!(st.has_cet());
        assert!(st.has_sha());
    }

    // ------------------------------------------------------------------
    // parse_feature_string edge cases
    // ------------------------------------------------------------------

    #[test]
    fn test_parse_feature_comma_only() {
        let mut fs = HashSet::new();
        parse_feature_string(&mut fs, ",,,");
        assert!(fs.is_empty());
    }

    #[test]
    fn test_parse_feature_unknown_ignored() {
        let mut fs = HashSet::new();
        fs.insert("sse2".to_string());
        parse_feature_string(&mut fs, "+bogus-feature");
        // "bogus-feature" is unknown but we still insert it.
        assert!(fs.contains("bogus-feature"));
    }

    #[test]
    fn test_parse_feature_leading_trailing_spaces() {
        let mut fs = HashSet::new();
        parse_feature_string(&mut fs, "  +avx  ,  -mmx  ");
        assert!(fs.contains("avx"));
        assert!(!fs.contains("mmx"));
    }

    #[test]
    fn test_triple_is_64_bit_various() {
        assert!(triple_is_64_bit("x86_64-unknown-linux-gnu"));
        assert!(triple_is_64_bit("AMD64-pc-windows-msvc"));
        assert!(triple_is_64_bit("x86-64-apple-darwin"));
        assert!(!triple_is_64_bit("i686-pc-linux-gnu"));
        assert!(!triple_is_64_bit("i386-unknown-freebsd"));
        assert!(!triple_is_64_bit("x86-pc-windows"));
    }

    // ------------------------------------------------------------------
    // misc helpers
    // ------------------------------------------------------------------

    #[test]
    fn test_has_feature_negative() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert!(!st.has_feature("avx512f"));
        assert!(!st.has_feature("nonexistent"));
    }

    #[test]
    fn test_inline_threshold_unknown_cpu() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "some-future-cpu", "");
        // Falls to default 180.
        assert_eq!(st.get_max_inline_size_threshold(), 180);
    }

    #[test]
    fn test_feature_string_default_64bit_sse2() {
        // User -sse2 takes priority over the 64-bit baseline.
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "-sse2");
        // The user override should take priority.
        assert!(!st.has_sse2());
        // But if no override is given, 64-bit baseline provides sse2.
        let st2 = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        assert!(st2.has_sse2());
    }

    #[test]
    fn test_default_64bit_cmov() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "i386", "");
        // 64-bit constructor forces cmov even for i386 CPU.
        assert!(st.has_cmov());
    }

    // ------------------------------------------------------------------
    // New CPU tests (Phase 10 expansion)
    // ------------------------------------------------------------------

    #[test]
    fn test_sapphirerapids_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "sapphirerapids", "");
        assert!(st.has_avx512f());
        assert!(st.has_amx_tile);
        assert!(st.has_amx_bf16);
        assert!(st.has_amx_int8);
        assert!(st.has_amx_fp16);
        assert!(st.has_serialize);
        assert!(st.has_tsxldtrk);
        assert!(st.has_enqcmd);
        assert!(st.has_movdiri);
        assert!(st.has_movdir64b);
    }

    #[test]
    fn test_emeraldrapids_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "emeraldrapids", "");
        assert!(st.has_avx512f());
        assert!(st.has_amx_tile);
        assert!(st.has_cmpccxadd);
        assert!(st.has_avx_ne_convert);
        assert!(st.has_prefetchiti);
        assert!(st.has_raoint);
    }

    #[test]
    fn test_graniterapids_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "graniterapids", "");
        assert!(st.has_avx512f());
        assert!(st.has_amx_tile);
        assert!(st.has_sha512);
        assert!(st.has_sm3);
        assert!(st.has_sm4);
        assert!(st.has_cmpccxadd);
        assert!(st.has_prefetchiti);
    }

    #[test]
    fn test_graniterapids_d_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "graniterapids-d", "");
        assert!(st.has_avx512f());
        assert!(st.has_sha512);
        assert!(st.has_sm3);
        assert!(st.has_sm4);
    }

    #[test]
    fn test_znver5_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver5", "");
        assert!(st.has_avx512f());
        assert!(st.has_avx512bw());
        assert!(st.has_avx512dq());
        assert!(st.has_avx512vl());
        assert!(st.has_avx_vnni);
        assert!(st.has_avx_ifma);
        assert!(st.has_avx_ne_convert);
        assert!(st.has_avx_vnni_int8);
        assert!(st.has_prefetchiti);
        assert!(st.has_gfni());
        assert!(st.has_vaes());
        assert!(st.has_clwb());
        assert!(st.has_pku);
    }

    #[test]
    fn test_tigerlake_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "tigerlake", "");
        assert!(st.has_avx512f());
        assert!(st.has_avx512bw());
        assert!(st.has_avx512dq());
        assert!(st.has_avx512vl());
        assert!(st.has_avx_vnni);
        assert!(st.has_sha());
        assert!(st.has_gfni());
        assert!(st.has_vaes());
    }

    #[test]
    fn test_alderlake_n_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "alderlake-n", "");
        assert!(st.has_avx2());
        assert!(!st.has_avx512f());
        assert!(!st.has_rtm);
        assert!(st.has_sha());
        assert!(st.has_cet);
    }

    #[test]
    fn test_via_c3_features() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "c3", "");
        assert!(st.has_sse());
        assert!(!st.has_sse2());
        assert!(st.has_cmov());
        assert!(st.has_mmx());
    }

    #[test]
    fn test_via_c7_features() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "c7", "");
        assert!(st.has_sse2());
        assert!(st.has_sse3());
        assert!(!st.has_ssse3());
    }

    #[test]
    fn test_via_nano_features() {
        let st = X86Subtarget::new("i686-pc-linux-gnu", "nano", "");
        assert!(st.has_ssse3());
        assert!(st.has_sse41());
        assert!(!st.has_sse42());
    }

    #[test]
    fn test_zhaoxin_zxc_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "zxc", "");
        assert!(st.has_sse42());
        assert!(st.has_popcnt());
        assert!(!st.has_avx());
    }

    #[test]
    fn test_zhaoxin_zxd_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "zxd", "");
        assert!(st.has_avx());
        assert!(st.has_f16c());
        assert!(!st.has_avx2());
    }

    // ------------------------------------------------------------------
    // Extended feature flag tests
    // ------------------------------------------------------------------

    #[test]
    fn test_extended_feature_flags_skx() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "skx", "");
        assert!(st.has_avx512f());
        // SKX doesn't have AMX, SHA, GFNI, CET
        assert!(!st.has_amx_tile);
        assert!(!st.has_sha512);
        assert!(!st.has_sm3);
        assert!(!st.has_sm4);
        assert!(!st.has_cet);
    }

    #[test]
    fn test_feature_enable_aes_pclmul() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "+aes,+pclmul");
        assert!(st.has_aes);
        assert!(st.has_pclmul);
    }

    #[test]
    fn test_feature_enable_fsgsbase() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "+fsgsbase");
        assert!(st.has_fsgsbase);
    }

    #[test]
    fn test_feature_enable_smap_smep() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "+smap,+smep");
        assert!(st.has_smap);
        assert!(st.has_smep);
    }

    #[test]
    fn test_feature_enable_invpcid() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "+invpcid");
        assert!(st.has_invpcid);
    }

    #[test]
    fn test_feature_enable_xop_fma4() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "+avx,+xop,+fma4");
        assert!(st.has_avx());
        assert!(st.has_xop);
        assert!(st.has_fma4);
    }

    // ------------------------------------------------------------------
    // Tuning flags tests
    // ------------------------------------------------------------------

    #[test]
    fn test_tuning_skylake_uses_cmov() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "skylake", "");
        assert!(st.use_cmov_branch);
        assert!(!st.slow_divide_32);
        assert!(!st.slow_divide_64);
        assert!(st.use_inc_dec);
    }

    #[test]
    fn test_tuning_nehalem_slow_div() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "nehalem", "");
        assert!(st.slow_divide_32);
        assert!(st.slow_divide_64);
        assert!(st.lea_uses_ag);
    }

    #[test]
    fn test_tuning_core2_slow_unaligned() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "core2", "");
        assert!(st.slow_unaligned_mem_16);
        assert!(st.slow_unaligned_mem_32);
    }

    #[test]
    fn test_tuning_znver3_sqrt_recip_est() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver3", "");
        assert!(st.use_sqrt_estimate);
        assert!(st.use_reciprocal_estimate);
        assert!(st.pad_short_functions);
        assert!(st.lea_uses_ag);
    }

    #[test]
    fn test_tuning_znver5_sqrt_recip_est() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver5", "");
        assert!(st.use_sqrt_estimate);
        assert!(st.use_reciprocal_estimate);
        assert!(st.pad_short_functions);
    }

    #[test]
    fn test_tuning_atom_opt_for_size() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "atom", "");
        assert!(st.opt_for_size);
        assert!(!st.use_cmov_branch);
    }

    // ------------------------------------------------------------------
    // Scheduling model tests
    // ------------------------------------------------------------------

    #[test]
    fn test_sched_skylake_dispatch_width() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "skylake", "");
        assert_eq!(st.dispatch_width, 6);
        assert_eq!(st.issue_width, 8);
        assert_eq!(st.retire_width, 8);
        assert_eq!(st.reorder_buffer_size, 224);
    }

    #[test]
    fn test_sched_haswell_dispatch_width() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "haswell", "");
        assert_eq!(st.dispatch_width, 4);
        assert_eq!(st.issue_width, 8);
        assert_eq!(st.retire_width, 4);
        assert_eq!(st.reorder_buffer_size, 192);
    }

    #[test]
    fn test_sched_sapphirerapids_dispatch_width() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "sapphirerapids", "");
        assert_eq!(st.dispatch_width, 6);
        assert_eq!(st.issue_width, 12);
        assert_eq!(st.reorder_buffer_size, 512);
    }

    #[test]
    fn test_sched_graniterapids_dispatch_width() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "graniterapids", "");
        assert_eq!(st.dispatch_width, 8);
        assert_eq!(st.issue_width, 12);
        assert_eq!(st.reorder_buffer_size, 576);
    }

    #[test]
    fn test_sched_znver1_dispatch_width() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver1", "");
        assert_eq!(st.dispatch_width, 6);
        assert_eq!(st.retire_width, 8);
        assert_eq!(st.reorder_buffer_size, 192);
    }

    #[test]
    fn test_sched_znver4_dispatch_width() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver4", "");
        assert_eq!(st.dispatch_width, 9);
        assert_eq!(st.retire_width, 9);
        assert_eq!(st.reorder_buffer_size, 320);
    }

    #[test]
    fn test_sched_znver5_dispatch_width() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "znver5", "");
        assert_eq!(st.dispatch_width, 10);
        assert_eq!(st.retire_width, 10);
        assert_eq!(st.reorder_buffer_size, 448);
    }

    #[test]
    fn test_sched_atom_small() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "atom", "");
        assert_eq!(st.dispatch_width, 2);
        assert_eq!(st.issue_width, 2);
        assert_eq!(st.reorder_buffer_size, 0); // Bonnell has no ROB
    }

    #[test]
    fn test_sched_tremont_rob() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "tremont", "");
        assert_eq!(st.dispatch_width, 2);
        assert_eq!(st.issue_width, 4);
        assert_eq!(st.reorder_buffer_size, 208);
    }

    // ------------------------------------------------------------------
    // Feature dependency chain tests
    // ------------------------------------------------------------------

    #[test]
    fn test_feature_dep_avx2_implies_avx() {
        let mut fs = HashSet::new();
        fs.insert("avx2".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("avx"));
        assert!(fs.contains("sse4.2"));
        assert!(fs.contains("sse"));
    }

    #[test]
    fn test_feature_dep_avx512bw_implies_f() {
        let mut fs = HashSet::new();
        fs.insert("avx512bw".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("avx512f"));
    }

    #[test]
    fn test_feature_dep_avx512vl_implies_f() {
        let mut fs = HashSet::new();
        fs.insert("avx512vl".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("avx512f"));
    }

    #[test]
    fn test_feature_dep_fma_implies_avx() {
        let mut fs = HashSet::new();
        fs.insert("fma".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("avx"));
    }

    #[test]
    fn test_feature_dep_amx_tile_implies_avx512f() {
        let mut fs = HashSet::new();
        fs.insert("amx-tile".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("avx512f"));
    }

    #[test]
    fn test_feature_dep_bmi2_implies_bmi() {
        let mut fs = HashSet::new();
        fs.insert("bmi2".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("bmi"));
    }

    #[test]
    fn test_feature_dep_xop_implies_avx() {
        let mut fs = HashSet::new();
        fs.insert("xop".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("avx"));
    }

    #[test]
    fn test_feature_dep_fma4_implies_avx() {
        let mut fs = HashSet::new();
        fs.insert("fma4".to_string());
        apply_feature_dependencies(&mut fs);
        assert!(fs.contains("avx"));
    }

    #[test]
    fn test_feature_dep_chains_len() {
        let chains = feature_dependency_chains();
        assert!(!chains.is_empty());
        // Verify specific entries
        let sse42_chain = chains.iter().find(|(k, _)| *k == "sse4.2");
        assert!(sse42_chain.is_some());
        assert_eq!(sse42_chain.unwrap().1, vec!["sse4.1"]);
    }

    // ------------------------------------------------------------------
    // Scheduling model name tests
    // ------------------------------------------------------------------

    #[test]
    fn test_sched_model_names() {
        assert_eq!(X86SchedModel::SkylakeClient.name(), "SkylakeClient");
        assert_eq!(X86SchedModel::Haswell.name(), "Haswell");
        assert_eq!(X86SchedModel::Zen1.name(), "Zen1");
        assert_eq!(X86SchedModel::Zen5.name(), "Zen5");
        assert_eq!(X86SchedModel::SapphireRapids.name(), "SapphireRapids");
        assert_eq!(X86SchedModel::GraniteRapids.name(), "GraniteRapids");
        assert_eq!(X86SchedModel::Generic.name(), "Generic");
    }

    #[test]
    fn test_sched_info_generic() {
        let info = sched_info_for_model(X86SchedModel::Generic);
        assert_eq!(info.dispatch, 1);
        assert_eq!(info.issue, 1);
        assert_eq!(info.retire, 1);
    }

    #[test]
    fn test_sched_info_zen4() {
        let info = sched_info_for_model(X86SchedModel::Zen4);
        assert_eq!(info.dispatch, 9);
        assert_eq!(info.rob_size, 320);
    }

    #[test]
    fn test_meteorlake_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "meteorlake", "");
        assert!(st.has_avx2());
        assert!(!st.has_avx512f());
        assert!(st.has_sha());
        assert!(st.has_cet);
    }

    #[test]
    fn test_arrowlake_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "arrowlake", "");
        assert!(st.has_avx2());
        assert!(!st.has_avx512f());
        assert!(st.has_avx_vnni);
        assert!(st.has_avx_ifma);
        assert!(st.has_avx_ne_convert);
    }

    #[test]
    fn test_lunarlake_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "lunarlake", "");
        assert!(st.has_avx2());
        assert!(!st.has_avx512f());
        assert!(st.has_avx_vnni);
        assert!(st.has_cet);
    }

    #[test]
    fn test_pantherlake_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "pantherlake", "");
        assert!(st.has_avx2());
        assert!(!st.has_avx512f());
        assert!(st.has_avx_vnni);
        assert!(st.has_cet);
    }

    #[test]
    fn test_gracemont_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "gracemont", "");
        assert!(st.has_avx2());
        assert!(!st.has_avx512f());
        assert!(st.has_waitpkg);
        assert!(st.has_cldemote);
        assert!(st.has_serialize);
        assert!(st.has_cet);
    }

    #[test]
    fn test_larrabee_features() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "larrabee", "");
        assert!(st.has_avx());
        assert!(!st.has_avx2());
        assert!(st.has_sse42());
    }
}