llvm-native-core 0.1.6

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
//! X86 Peephole Optimizer — post-instruction-selection machine-code
//! optimizations that improve code quality for the x86/x86-64 architecture.
//! Phase 10 — LLVM.TARGET.X86.1 Court.
//!
//! This pass runs after instruction selection and register allocation to
//! eliminate redundant instructions, fold operands, and simplify common
//! x86 idioms.  It operates directly on `MachineInstr` sequences within
//! basic blocks and applies a fixed-point iteration of rewrite rules until
//! no further improvements are possible (or an iteration limit is reached).
//!
//! ## Optimizations performed
//!
//! | Rule                          | Example                                       |
//! |-------------------------------|-----------------------------------------------|
//! | Fold immediate into ALU       | MOV r, 1; ADD r, r2 → ADD r2, 1              |
//! | Eliminate redundant MOVs      | MOV A, B; … ; MOV C, A  →  MOV C, B          |
//! | Fold load into ALU            | MOV r, [m]; ADD r, r2 → ADD r2, [m]          |
//! | Eliminate PUSH/POP pairs      | PUSH r; … ; POP r  →  removed                |
//! | Simplify compare-with-zero    | CMP r, 0; JE → TEST r, r; JE                 |
//! | Remove NOPs                   | NOP / multi-byte NOP / LEA rax,[rax+0]       |
//! | Simplify LEA                  | LEA r, [r2+0] → MOV r, r2                    |
//! | Optimize compare+branch       | CMP r, 0; JNE → TEST r, r; JNE               |
//!
//! ## Clean-room reconstruction from:
//! - Intel® 64 and IA-32 Architectures Software Developer's Manual Vol 2
//! - AMD64 Architecture Programmer's Manual Vol 3
//! - Agner Fog's instruction tables and microarchitecture guides
//!
//! Zero LLVM source code consultation.

use crate::codegen::{MachineBasicBlock, MachineFunction, MachineInstr, MachineOperand};
use crate::x86::x86_instr_info::{X86InstrInfo, X86Opcode};
use crate::x86::x86_subtarget::X86Subtarget;

// ============================================================================
// X86OptStats — optimization counters
// ============================================================================

/// Per-rule counters collected during a peephole pass iteration.
#[derive(Debug, Clone, Default)]
pub struct X86OptStats {
    /// Number of loads folded into ALU operations.
    pub folded_loads: usize,
    /// Number of redundant MOV instructions eliminated.
    pub eliminated_moves: usize,
    /// Number of PUSH/POP pairs eliminated.
    pub eliminated_pushes_pops: usize,
    /// Number of immediate operands folded into other instructions.
    pub folded_immediates: usize,
    /// Number of CMP+Jcc patterns simplified.
    pub simplified_compare_branch: usize,
    /// Number of LEA instructions replaced with simpler forms.
    pub eliminated_lea: usize,
    /// Number of memory operands folded into ALU or TEST.
    pub folded_memory: usize,
    /// Number of NOP instructions removed.
    pub eliminated_nops: usize,
    /// Number of IMUL optimizations (constant folding).
    pub optimized_imul: usize,
    /// Number of DIV/IDIV optimizations (power-of-2/magic multiply).
    pub optimized_div: usize,
    /// Number of redundant MOVZX/MOVSX eliminated.
    pub eliminated_extends: usize,
    /// Number of INC/DEC converted to ADD/SUB.
    pub optimized_inc_dec: usize,
    /// Number of tail calls optimized to JMP.
    pub tail_calls: usize,
    /// Number of XOR+SETcc patterns combined.
    pub optimized_xor_setcc: usize,
}

impl X86OptStats {
    /// Create a new zeroed stats counter.
    pub fn new() -> Self {
        Self::default()
    }

    /// Merge two sets of stats, returning the element-wise sum.
    pub fn merge(&self, other: &X86OptStats) -> X86OptStats {
        X86OptStats {
            folded_loads: self.folded_loads + other.folded_loads,
            eliminated_moves: self.eliminated_moves + other.eliminated_moves,
            eliminated_pushes_pops: self.eliminated_pushes_pops + other.eliminated_pushes_pops,
            folded_immediates: self.folded_immediates + other.folded_immediates,
            simplified_compare_branch: self.simplified_compare_branch
                + other.simplified_compare_branch,
            eliminated_lea: self.eliminated_lea + other.eliminated_lea,
            folded_memory: self.folded_memory + other.folded_memory,
            eliminated_nops: self.eliminated_nops + other.eliminated_nops,
            optimized_imul: self.optimized_imul + other.optimized_imul,
            optimized_div: self.optimized_div + other.optimized_div,
            eliminated_extends: self.eliminated_extends + other.eliminated_extends,
            optimized_inc_dec: self.optimized_inc_dec + other.optimized_inc_dec,
            tail_calls: self.tail_calls + other.tail_calls,
            optimized_xor_setcc: self.optimized_xor_setcc + other.optimized_xor_setcc,
        }
    }

    /// Returns true when any counter is non-zero (i.e. the pass made progress).
    pub fn made_progress(&self) -> bool {
        self.folded_loads > 0
            || self.eliminated_moves > 0
            || self.eliminated_pushes_pops > 0
            || self.folded_immediates > 0
            || self.simplified_compare_branch > 0
            || self.eliminated_lea > 0
            || self.folded_memory > 0
            || self.eliminated_nops > 0
            || self.optimized_imul > 0
            || self.optimized_div > 0
            || self.eliminated_extends > 0
            || self.optimized_inc_dec > 0
            || self.tail_calls > 0
            || self.optimized_xor_setcc > 0
    }
}

// ============================================================================
// X86PeepholeOptimizer
// ============================================================================

/// The main peephole optimizer that applies X86-specific rewrite rules to
/// machine instruction sequences within each basic block of a machine function.
pub struct X86PeepholeOptimizer {
    /// Target subtarget (feature flags, 64-bit mode, etc.).
    pub subtarget: X86Subtarget,
    /// Aggregated statistics across all iterations.
    pub stats: X86OptStats,
    /// Instruction info table for opcode queries.
    #[allow(dead_code)]
    instr_info: X86InstrInfo,
}

impl X86PeepholeOptimizer {
    /// Create a new peephole optimizer for the given subtarget.
    pub fn new(subtarget: X86Subtarget) -> Self {
        Self {
            subtarget,
            stats: X86OptStats::new(),
            instr_info: X86InstrInfo::new(),
        }
    }

    // ========================================================================
    // Helper methods — instruction classification
    // ========================================================================

    /// Returns true if `mi` is a MOV instruction (opcode == MOV).
    pub fn is_mov_instruction(&self, mi: &MachineInstr) -> bool {
        mi.opcode == X86Opcode::MOV as u32
    }

    /// Returns true if `mi` is an ALU instruction: ADD, SUB, AND, OR, XOR,
    /// ADC, SBB, SHL, SHR, SAR, or NEG.
    pub fn is_alu_instruction(&self, mi: &MachineInstr) -> bool {
        matches!(
            mi.opcode,
            op if op == X86Opcode::ADD as u32
                || op == X86Opcode::SUB as u32
                || op == X86Opcode::AND as u32
                || op == X86Opcode::OR as u32
                || op == X86Opcode::XOR as u32
                || op == X86Opcode::ADC as u32
                || op == X86Opcode::SBB as u32
                || op == X86Opcode::SHL as u32
                || op == X86Opcode::SHR as u32
                || op == X86Opcode::SAR as u32
                || op == X86Opcode::NEG as u32
        )
    }

    /// Returns true if `mi` is a CMP or TEST instruction.
    pub fn is_cmp_test_instruction(&self, mi: &MachineInstr) -> bool {
        mi.opcode == X86Opcode::CMP as u32 || mi.opcode == X86Opcode::TEST as u32
    }

    /// Returns true if `mi` is a conditional branch (any Jcc variant).
    pub fn is_conditional_branch(&self, mi: &MachineInstr) -> bool {
        matches!(
            mi.opcode,
            op if op == X86Opcode::JE as u32
                || op == X86Opcode::JNE as u32
                || op == X86Opcode::JB as u32
                || op == X86Opcode::JAE as u32
                || op == X86Opcode::JBE as u32
                || op == X86Opcode::JA as u32
                || op == X86Opcode::JS as u32
                || op == X86Opcode::JNS as u32
                || op == X86Opcode::JP as u32
                || op == X86Opcode::JNP as u32
                || op == X86Opcode::JL as u32
                || op == X86Opcode::JGE as u32
                || op == X86Opcode::JLE as u32
                || op == X86Opcode::JG as u32
                || op == X86Opcode::JO as u32
                || op == X86Opcode::JNO as u32
        )
    }

    /// Returns true if `mi` is effectively a NOP — explicit NOP opcode,
    /// LEA RAX,[RAX+0], or MOV RAX,RAX.
    pub fn is_nop(&self, mi: &MachineInstr) -> bool {
        if mi.opcode == X86Opcode::NOP as u32 {
            return true;
        }
        // LEA reg, [reg+0] where both operands are the same register
        // and displacement is zero — no-op.
        if mi.opcode == X86Opcode::LEA as u32 && mi.operands.len() >= 2 {
            if let (MachineOperand::Reg(_dst), MachineOperand::PhysReg(_src)) =
                (&mi.operands[0], &mi.operands[1])
            {
                // Check for zero displacement in subsequent operand
                if mi.operands.len() >= 3 {
                    if let MachineOperand::Imm(0) = mi.operands[2] {
                        // TODO: verify src == dst when register allocation is done
                        return false; // LEA r, [base+0] → MOV r, base (not NOP unless same)
                    }
                }
                // LEA r, [base] ≈ MOV r, base — not strictly NOP
            }
            // LEA RAX, [RAX] pattern — same src/dst, no displacement
            if mi.operands.len() == 2 {
                if let (MachineOperand::PhysReg(d), MachineOperand::PhysReg(s)) =
                    (&mi.operands[0], &mi.operands[1])
                {
                    return d == s; // LEA RAX, [RAX+0] is a NOP
                }
            }
        }
        // MOV RAX, RAX is a NOP
        if mi.opcode == X86Opcode::MOV as u32 && mi.operands.len() >= 2 {
            if let (MachineOperand::PhysReg(dst), MachineOperand::PhysReg(src)) =
                (&mi.operands[0], &mi.operands[1])
            {
                return dst == src;
            }
            if let (MachineOperand::Reg(dst), MachineOperand::Reg(src)) =
                (&mi.operands[0], &mi.operands[1])
            {
                return dst == src;
            }
        }
        false
    }

    /// Extract source and destination operands from a MOV instruction.
    /// Returns `Some((dst, src))` if `mi` is a MOV with at least two operands,
    /// otherwise `None`.
    pub fn get_mov_src_dst(&self, mi: &MachineInstr) -> Option<(MachineOperand, MachineOperand)> {
        if mi.opcode != X86Opcode::MOV as u32 || mi.operands.len() < 2 {
            return None;
        }
        Some((mi.operands[0].clone(), mi.operands[1].clone()))
    }

    /// Merge two stats objects (used as a convenience wrapper).
    pub fn merge_stats(&self, s1: &X86OptStats, s2: &X86OptStats) -> X86OptStats {
        s1.merge(s2)
    }

    // ========================================================================
    // Optimization: Fold immediate into ALU
    // ========================================================================

    /// Scans for MOV r, imm followed by an ALU op that uses r as both operands,
    /// and folds the immediate directly into the ALU instruction.
    ///
    /// Patterns:
    /// - MOV reg, imm;  ADD reg, reg2  →  ADD reg2, imm  (commute ADD)
    /// - MOV reg, 0;    OR  reg, reg   →  MOV reg, reg   (identity copy)
    /// - MOV reg, 0;    XOR reg, reg   →  keep XOR        (zero idiom)
    /// - MOV reg, imm;  SUB reg, reg2  →  special case
    pub fn fold_immediate_into_alu(&mut self, instructions: &mut Vec<MachineInstr>) {
        if instructions.len() < 2 {
            return;
        }

        let mut marked_for_removal: Vec<usize> = Vec::new();

        for i in 0..instructions.len().saturating_sub(1) {
            if marked_for_removal.contains(&i) {
                continue;
            }

            // Check pattern: MOV reg, imm followed by ALU reg, reg(2)
            if instructions[i].opcode != X86Opcode::MOV as u32 {
                continue;
            }
            if instructions[i].operands.len() < 2 {
                continue;
            }

            let mov_dst = match &instructions[i].operands[0] {
                MachineOperand::Reg(r) => *r,
                _ => continue,
            };
            let mov_src = &instructions[i].operands[1];

            // We need an immediate as the MOV source
            let imm_val = match mov_src {
                MachineOperand::Imm(v) => *v,
                _ => continue,
            };

            let next = i + 1;
            if !self.is_alu_instruction(&instructions[next]) {
                continue;
            }
            if instructions[next].operands.len() < 2 {
                continue;
            }

            let alu_opcode = instructions[next].opcode;

            // Check that first ALU operand is the MOV destination register
            let alu_op0 = match &instructions[next].operands[0] {
                MachineOperand::Reg(r) => *r,
                _ => continue,
            };
            if alu_op0 != mov_dst {
                continue;
            }

            // Pattern: MOV reg, 0; OR reg, reg → MOV reg, reg (copy identity)
            if alu_opcode == X86Opcode::OR as u32 && imm_val == 0 {
                if let Some(MachineOperand::Reg(_)) = instructions[next].operands.get(1) {
                    // Replace OR with MOV reg, reg — the OR was redundant
                    instructions[next].opcode = X86Opcode::MOV as u32;
                    marked_for_removal.push(i);
                    self.stats.folded_immediates += 1;
                    continue;
                }
            }

            // Pattern: MOV reg, 0; XOR reg, reg → just XOR (zero idiom, keep it)
            if alu_opcode == X86Opcode::XOR as u32 && imm_val == 0 {
                if let Some(MachineOperand::Reg(reg2)) = instructions[next].operands.get(1) {
                    if *reg2 == mov_dst {
                        // This is the zero-idiom XOR — remove the MOV, keep XOR
                        marked_for_removal.push(i);
                        self.stats.folded_immediates += 1;
                        continue;
                    }
                }
            }

            // Pattern: MOV reg, imm; ADD reg, reg2 → ADD reg2, imm
            if alu_opcode == X86Opcode::ADD as u32 {
                if let Some(MachineOperand::Reg(reg2)) = instructions[next].operands.get(1) {
                    // Replace ADD reg, reg2 with ADD reg2, imm
                    instructions[next].operands[0] = MachineOperand::Reg(*reg2);
                    instructions[next].operands[1] = MachineOperand::Imm(imm_val);
                    marked_for_removal.push(i);
                    self.stats.folded_immediates += 1;
                    continue;
                }
            }

            // Pattern: MOV reg, imm; SUB reg, reg2 → SUB reg2, imm (reversed)
            // Actually SUB is not commutative, but MOV r,i; SUB r,r2 means r2 = r - i
            // which doesn't directly fold.  We skip this case.
            // Pattern: MOV reg, imm; AND/OR/XOR reg, reg2 → AND/OR/XOR reg2, imm
            if alu_opcode == X86Opcode::AND as u32
                || alu_opcode == X86Opcode::OR as u32
                || alu_opcode == X86Opcode::XOR as u32
            {
                if let Some(MachineOperand::Reg(reg2)) = instructions[next].operands.get(1) {
                    instructions[next].operands[0] = MachineOperand::Reg(*reg2);
                    instructions[next].operands[1] = MachineOperand::Imm(imm_val);
                    marked_for_removal.push(i);
                    self.stats.folded_immediates += 1;
                    continue;
                }
            }
        }

        // Remove marked instructions in reverse order
        marked_for_removal.sort_unstable_by(|a, b| b.cmp(a));
        for idx in &marked_for_removal {
            if *idx < instructions.len() {
                instructions.remove(*idx);
            }
        }
    }

    // ========================================================================
    // Optimization: Eliminate redundant MOVs
    // ========================================================================

    /// Eliminates register-to-register MOV instructions that are redundant.
    ///
    /// Patterns:
    /// - MOV A, B; … (no use of B as source after the MOV); MOV C, A → MOV C, B
    /// - MOV A, A → remove (no-op move)
    /// - MOV A, B; MOV B, A → just one MOV (if B not used later)
    pub fn eliminate_redundant_moves(&mut self, instructions: &mut Vec<MachineInstr>) {
        if instructions.len() < 1 {
            return;
        }

        let mut i = 0;
        while i < instructions.len() {
            // Pattern: MOV A, A → remove no-op move
            if self.is_mov_instruction(&instructions[i]) && instructions[i].operands.len() >= 2 {
                let is_nop_move = match (&instructions[i].operands[0], &instructions[i].operands[1])
                {
                    (MachineOperand::Reg(a), MachineOperand::Reg(b)) => a == b,
                    (MachineOperand::PhysReg(a), MachineOperand::PhysReg(b)) => a == b,
                    _ => false,
                };
                if is_nop_move {
                    instructions.remove(i);
                    self.stats.eliminated_moves += 1;
                    continue; // re-check same index
                }
            }

            // Pattern: MOV A, B; … ; MOV C, A  →  MOV C, B
            // (when A is not used as an operand between the two MOVs)
            if let Some((dst_a, src_b)) = self.get_mov_src_dst(&instructions[i]) {
                let a_reg = match &dst_a {
                    MachineOperand::Reg(r) => *r,
                    _ => {
                        i += 1;
                        continue;
                    }
                };

                // Search forward for MOV C, A
                let mut found = false;
                for j in (i + 1)..instructions.len() {
                    if self.is_mov_instruction(&instructions[j])
                        && instructions[j].operands.len() >= 2
                    {
                        if let MachineOperand::Reg(_c) = &instructions[j].operands[0] {
                            if let MachineOperand::Reg(used_a) = &instructions[j].operands[1] {
                                if *used_a == a_reg {
                                    // If C == B, this is a swap (MOV A,B; MOV B,A);
                                    // skip — handled by the dedicated swap pattern below.
                                    if let MachineOperand::Reg(b) = &src_b {
                                        if *_c == *b {
                                            break;
                                        }
                                    }
                                    // Found MOV C, A — check if A is used between i and j
                                    let mut a_used_between = false;
                                    for k in (i + 1)..j {
                                        for op in &instructions[k].operands {
                                            if let MachineOperand::Reg(r) = op {
                                                if *r == a_reg {
                                                    a_used_between = true;
                                                    break;
                                                }
                                            }
                                        }
                                        if a_used_between {
                                            break;
                                        }
                                    }

                                    if !a_used_between {
                                        // Replace MOV C, A with MOV C, B
                                        instructions[j].operands[1] = src_b.clone();
                                        self.stats.eliminated_moves += 1;
                                        found = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    // Also stop if A is used in a non-MOV instruction
                    if self.is_alu_instruction(&instructions[j])
                        || self.is_cmp_test_instruction(&instructions[j])
                    {
                        for op in &instructions[j].operands {
                            if let MachineOperand::Reg(r) = op {
                                if *r == a_reg {
                                    // A is used; can't forward
                                    break;
                                }
                            }
                        }
                    }
                }
                if found {
                    i += 1;
                    continue;
                }
            }

            // Pattern: MOV A, B; MOV B, A → keep just MOV A, B (if B not used later)
            if i + 1 < instructions.len() {
                if self.is_mov_instruction(&instructions[i])
                    && self.is_mov_instruction(&instructions[i + 1])
                    && instructions[i].operands.len() >= 2
                    && instructions[i + 1].operands.len() >= 2
                {
                    if let (MachineOperand::Reg(a1), MachineOperand::Reg(b1)) =
                        (&instructions[i].operands[0], &instructions[i].operands[1])
                    {
                        if let (MachineOperand::Reg(a2), MachineOperand::Reg(b2)) = (
                            &instructions[i + 1].operands[0],
                            &instructions[i + 1].operands[1],
                        ) {
                            if a1 == b2 && a2 == b1 {
                                // MOV A, B; MOV B, A — swap
                                // Check if B is used after i+1
                                let b_used_after = instructions[(i + 2)..].iter().any(|mi| {
                                    mi.operands.iter().any(|op| {
                                        if let MachineOperand::Reg(r) = op {
                                            *r == *b1
                                        } else {
                                            false
                                        }
                                    })
                                });

                                if !b_used_after {
                                    // B is dead after the swap — remove second MOV
                                    instructions.remove(i + 1);
                                    self.stats.eliminated_moves += 1;
                                    continue;
                                }
                                // B is live — keep both (classic register swap)
                            }
                        }
                    }
                }
            }

            i += 1;
        }
    }

    // ========================================================================
    // Optimization: Fold load into ALU
    // ========================================================================

    /// Folds memory loads into ALU operations when the loaded register is only
    /// used by the immediately following ALU instruction.
    ///
    /// Patterns (when memory operands are available):
    /// - MOV reg, [mem]; ADD reg, reg2  →  ADD reg2, [mem]
    /// - MOV reg, [mem]; CMP reg, imm  →  CMP [mem], imm
    /// - Similar for SUB, AND, OR, XOR, TEST
    ///
    /// Note: This pass requires a `Mem` variant in `MachineOperand` to
    /// represent memory references.  When memory operands are present,
    /// the patterns are detected and folded; otherwise this pass is a no-op.
    pub fn fold_load_into_alu(&mut self, instructions: &mut Vec<MachineInstr>) {
        if instructions.len() < 2 {
            return;
        }

        let mut marked_for_removal: Vec<usize> = Vec::new();

        for i in 0..instructions.len().saturating_sub(1) {
            if marked_for_removal.contains(&i) {
                continue;
            }

            // Check pattern: MOV reg, ? followed by ALU/CMP/TEST
            if instructions[i].opcode != X86Opcode::MOV as u32 {
                continue;
            }
            if instructions[i].operands.len() < 2 {
                continue;
            }

            // Determine if the MOV source could be a memory operand.
            // Currently memory operands are not represented in MachineOperand,
            // but we check for Globals as potential memory references.
            let mov_dst = match &instructions[i].operands[0] {
                MachineOperand::Reg(r) => *r,
                _ => continue,
            };

            // Check if source looks like a memory reference
            let is_memory_load = matches!(&instructions[i].operands[1], MachineOperand::Global(_));

            if !is_memory_load {
                // Future: check for Mem variant here
                continue;
            }

            let mem_src = instructions[i].operands[1].clone();

            let next = i + 1;
            let _next_opcode = instructions[next].opcode;

            // Check that the loaded register is used as first operand of the
            // next instruction and is not used elsewhere in the block.
            let is_alu_or_cmp = self.is_alu_instruction(&instructions[next])
                || instructions[next].opcode == X86Opcode::CMP as u32
                || instructions[next].opcode == X86Opcode::TEST as u32;

            if !is_alu_or_cmp {
                continue;
            }

            if instructions[next].operands.is_empty() {
                continue;
            }

            let alu_first_op = match &instructions[next].operands[0] {
                MachineOperand::Reg(r) => *r,
                _ => continue,
            };

            if alu_first_op != mov_dst {
                continue;
            }

            // Check that mov_dst is not used elsewhere in the block
            let used_elsewhere = instructions.iter().enumerate().any(|(idx, mi)| {
                if idx == i || idx == next {
                    return false;
                }
                mi.operands.iter().any(|op| {
                    if let MachineOperand::Reg(r) = op {
                        *r == mov_dst
                    } else {
                        false
                    }
                })
            });

            if used_elsewhere {
                continue;
            }

            // Fold: replace first ALU operand with memory source
            instructions[next].operands[0] = mem_src;
            marked_for_removal.push(i);
            self.stats.folded_loads += 1;
            self.stats.folded_memory += 1;
        }

        // Remove marked instructions in reverse order
        marked_for_removal.sort_unstable_by(|a, b| b.cmp(a));
        for idx in &marked_for_removal {
            if *idx < instructions.len() {
                instructions.remove(*idx);
            }
        }
    }

    // ========================================================================
    // Optimization: Eliminate PUSH/POP pairs
    // ========================================================================

    /// Eliminates matched PUSH/POP pairs when no stack access occurs between
    /// them.
    ///
    /// Patterns:
    /// - PUSH reg; … (no stack access or use of reg); POP reg  →  remove both
    /// - PUSH A; … (no A use, no stack access); POP B  →  MOV B, A
    pub fn eliminate_push_pop_pairs(&mut self, instructions: &mut Vec<MachineInstr>) {
        if instructions.len() < 2 {
            return;
        }

        let mut i = 0;
        while i < instructions.len() {
            if i >= instructions.len() {
                break;
            }
            if instructions[i].opcode != X86Opcode::PUSH as u32 {
                i += 1;
                continue;
            }
            if instructions[i].operands.is_empty() {
                i += 1;
                continue;
            }

            let pushed_reg = match &instructions[i].operands[0] {
                MachineOperand::Reg(r) => *r,
                MachineOperand::PhysReg(r) => *r,
                _ => {
                    i += 1;
                    continue;
                }
            };

            // Search forward for matching POP
            let mut pop_idx: Option<usize> = None;
            let mut pop_dst: Option<MachineOperand> = None;
            let mut conflicting_access = false;

            for j in (i + 1)..instructions.len() {
                let mi = &instructions[j];

                // Detect stack access (PUSH/POP/CALL/RET) between PUSH and POP
                if mi.opcode == X86Opcode::PUSH as u32
                    || mi.opcode == X86Opcode::CALL as u32
                    || mi.opcode == X86Opcode::RET as u32
                {
                    conflicting_access = true;
                    break;
                }

                // POP found — check operand
                if mi.opcode == X86Opcode::POP as u32 && mi.operands.len() >= 1 {
                    let _popped = match &mi.operands[0] {
                        MachineOperand::Reg(r) => *r,
                        MachineOperand::PhysReg(r) => *r,
                        _ => continue,
                    };

                    // Check if the pushed register is used between PUSH and POP
                    let pushed_used_between = instructions[(i + 1)..j].iter().any(|inst| {
                        inst.operands.iter().any(|op| match op {
                            MachineOperand::Reg(r) => *r == pushed_reg,
                            MachineOperand::PhysReg(r) => *r == pushed_reg,
                            _ => false,
                        })
                    });

                    if pushed_used_between {
                        break; // Can't eliminate — the value was used
                    }

                    pop_idx = Some(j);
                    pop_dst = Some(mi.operands[0].clone());
                    break;
                }
            }

            if conflicting_access {
                // A PUSH, CALL, or RET between this PUSH and its POP means
                // the stack was modified.  Skip past the entire sub-region
                // by advancing to after the next POP (which balances the
                // intervening stack operation).
                let next_pop = instructions[(i + 1)..]
                    .iter()
                    .position(|mi| mi.opcode == X86Opcode::POP as u32)
                    .map(|rel| i + 1 + rel + 1)
                    .unwrap_or(instructions.len());
                i = next_pop;
                continue;
            }

            if let Some(p_idx) = pop_idx {
                // Verify stack balance between PUSH and POP: the intervening
                // region must contain an equal number of PUSHes and POPs
                // (i.e., the stack depth must return to its starting point).
                let intervening_pushes = instructions[(i + 1)..p_idx]
                    .iter()
                    .filter(|mi| mi.opcode == X86Opcode::PUSH as u32)
                    .count();
                let intervening_pops = instructions[(i + 1)..p_idx]
                    .iter()
                    .filter(|mi| mi.opcode == X86Opcode::POP as u32)
                    .count();
                if intervening_pushes != intervening_pops {
                    i += 1;
                    continue;
                }
                let pop_dst = pop_dst.unwrap();
                let pop_reg = match &pop_dst {
                    MachineOperand::Reg(r) => *r,
                    MachineOperand::PhysReg(r) => *r,
                    _ => {
                        i += 1;
                        continue;
                    }
                };

                // Same register: PUSH r; POP r → remove both
                if pop_reg == pushed_reg {
                    instructions.remove(p_idx);
                    instructions.remove(i);
                    self.stats.eliminated_pushes_pops += 1;
                    continue; // re-check same index
                }

                // Different register: PUSH A; POP B → MOV B, A
                let mov_opcode = X86Opcode::MOV as u32;
                let mut mov_instr = MachineInstr::new(mov_opcode);
                mov_instr.operands.push(pop_dst);
                mov_instr.operands.push(match pushed_reg {
                    r if r < 256 => MachineOperand::Reg(r),
                    _ => MachineOperand::PhysReg(pushed_reg),
                });

                instructions[p_idx] = mov_instr;
                instructions.remove(i);
                self.stats.eliminated_pushes_pops += 1;
                continue;
            }

            i += 1;
        }
    }

    // ========================================================================
    // Optimization: Simplify compare with zero
    // ========================================================================

    /// Replaces CMP reg, 0 with TEST reg, reg (shorter encoding) and removes
    /// redundant flag-setting instructions before conditional branches.
    ///
    /// Patterns:
    /// - CMP reg, 0; JE/JNE  →  TEST reg, reg; JE/JNE
    /// - OR reg, reg; JE     →  TEST reg, reg; JE
    /// - AND reg, reg; JNE   →  TEST reg, reg; JNE
    pub fn simplify_compare_with_zero(&mut self, instructions: &mut Vec<MachineInstr>) {
        if instructions.len() < 2 {
            return;
        }

        for i in 0..instructions.len().saturating_sub(1) {
            // Pattern: CMP reg, 0 → TEST reg, reg
            if instructions[i].opcode == X86Opcode::CMP as u32
                && instructions[i].operands.len() >= 2
            {
                if let MachineOperand::Reg(reg) = instructions[i].operands[0] {
                    if let MachineOperand::Imm(0) = instructions[i].operands[1] {
                        // Replace CMP reg, 0 with TEST reg, reg
                        instructions[i].opcode = X86Opcode::TEST as u32;
                        instructions[i].operands[1] = MachineOperand::Reg(reg);
                        self.stats.simplified_compare_branch += 1;
                        continue;
                    }
                }
                // Handle PhysReg case
                if let MachineOperand::PhysReg(reg) = instructions[i].operands[0] {
                    if let MachineOperand::Imm(0) = instructions[i].operands[1] {
                        instructions[i].opcode = X86Opcode::TEST as u32;
                        instructions[i].operands[1] = MachineOperand::PhysReg(reg);
                        self.stats.simplified_compare_branch += 1;
                        continue;
                    }
                }
            }

            // Pattern: OR reg, reg; JE  →  TEST reg, reg; JE
            // (OR has side effects — writes to reg; TEST does not)
            if instructions[i].opcode == X86Opcode::OR as u32
                && instructions[i].operands.len() >= 2
                && i + 1 < instructions.len()
                && self.is_conditional_branch(&instructions[i + 1])
            {
                let is_self_or = match (&instructions[i].operands[0], &instructions[i].operands[1])
                {
                    (MachineOperand::Reg(a), MachineOperand::Reg(b)) => a == b,
                    (MachineOperand::PhysReg(a), MachineOperand::PhysReg(b)) => a == b,
                    _ => false,
                };
                if is_self_or {
                    // Replace OR reg, reg with TEST reg, reg
                    instructions[i].opcode = X86Opcode::TEST as u32;
                    self.stats.simplified_compare_branch += 1;
                    continue;
                }
            }

            // Pattern: AND reg, reg; JNE  →  TEST reg, reg; JNE
            if instructions[i].opcode == X86Opcode::AND as u32
                && instructions[i].operands.len() >= 2
                && i + 1 < instructions.len()
                && self.is_conditional_branch(&instructions[i + 1])
            {
                let is_self_and = match (&instructions[i].operands[0], &instructions[i].operands[1])
                {
                    (MachineOperand::Reg(a), MachineOperand::Reg(b)) => a == b,
                    (MachineOperand::PhysReg(a), MachineOperand::PhysReg(b)) => a == b,
                    _ => false,
                };
                if is_self_and {
                    instructions[i].opcode = X86Opcode::TEST as u32;
                    self.stats.simplified_compare_branch += 1;
                    continue;
                }
            }
        }
    }

    // ========================================================================
    // Optimization: Eliminate NOPs
    // ========================================================================

    /// Removes all forms of NOP instructions from the stream.
    ///
    /// Patterns removed:
    /// - Explicit NOP instructions
    /// - Multi-byte NOPs (treated as a single NOP opcode here)
    /// - LEA RAX, [RAX+0] (no-op LEA)
    /// - MOV RAX, RAX (no-op MOV)
    pub fn eliminate_nops(&mut self, instructions: &mut Vec<MachineInstr>) {
        let mut i = 0;
        while i < instructions.len() {
            let _is_nop_instr = self.is_nop(&instructions[i]);

            // Also check for explicit NOP opcode (multi-byte NOPs all map to NOP)
            if instructions[i].opcode == X86Opcode::NOP as u32 {
                instructions.remove(i);
                self.stats.eliminated_nops += 1;
                continue;
            }

            // LEA reg, [reg+0] where displacement is zero and no scale/index
            if instructions[i].opcode == X86Opcode::LEA as u32
                && instructions[i].operands.len() >= 2
            {
                // LEA dst, [base+0] where dst == base
                let is_zero_displacement_lea =
                    match (&instructions[i].operands[0], &instructions[i].operands[1]) {
                        (MachineOperand::Reg(d), MachineOperand::Reg(b)) => {
                            d == b && instructions[i].operands.len() == 2
                        }
                        (MachineOperand::PhysReg(d), MachineOperand::PhysReg(b)) => {
                            d == b && instructions[i].operands.len() == 2
                        }
                        _ => false,
                    };
                if is_zero_displacement_lea {
                    instructions.remove(i);
                    self.stats.eliminated_nops += 1;
                    continue;
                }
            }

            // MOV same, same → NOP
            if instructions[i].opcode == X86Opcode::MOV as u32
                && instructions[i].operands.len() >= 2
            {
                let is_self_mov = match (&instructions[i].operands[0], &instructions[i].operands[1])
                {
                    (MachineOperand::Reg(a), MachineOperand::Reg(b)) => a == b,
                    (MachineOperand::PhysReg(a), MachineOperand::PhysReg(b)) => a == b,
                    _ => false,
                };
                if is_self_mov {
                    instructions.remove(i);
                    self.stats.eliminated_nops += 1;
                    continue;
                }
            }

            i += 1;
        }
    }

    // ========================================================================
    // Optimization: Simplify LEA
    // ========================================================================

    /// Replaces complex LEA instructions with simpler forms when the address
    /// computation is trivial.
    ///
    /// Patterns:
    /// - LEA reg, [reg+0]    →  MOV reg, reg  (or NOP if same register)
    /// - LEA reg, [reg2+0]   →  MOV reg, reg2
    /// - LEA reg, [reg+disp] →  simplified if small displacement
    pub fn fold_lea(&mut self, instructions: &mut Vec<MachineInstr>) {
        let mut i = 0;
        while i < instructions.len() {
            if instructions[i].opcode != X86Opcode::LEA as u32 {
                i += 1;
                continue;
            }
            if instructions[i].operands.len() < 2 {
                i += 1;
                continue;
            }

            let dst = instructions[i].operands[0].clone();

            // Case 1: LEA reg, [base+0] — only base and destination present
            if instructions[i].operands.len() == 2 {
                let base = &instructions[i].operands[1];

                // LEA reg, [base+0] → MOV reg, base
                let is_same = match (&dst, base) {
                    (MachineOperand::Reg(d), MachineOperand::Reg(b)) => d == b,
                    (MachineOperand::PhysReg(d), MachineOperand::PhysReg(b)) => d == b,
                    _ => false,
                };

                if is_same {
                    // LEA same, [same+0] — this is a NOP (already handled by eliminate_nops)
                    // Flag as LEA elimination
                    instructions[i].opcode = X86Opcode::MOV as u32;
                    self.stats.eliminated_lea += 1;
                } else {
                    // LEA reg, [reg2+0] → MOV reg, reg2
                    instructions[i].opcode = X86Opcode::MOV as u32;
                    self.stats.eliminated_lea += 1;
                }
                continue;
            }

            // Case 2: LEA reg, [base+disp] — base + displacement
            if instructions[i].operands.len() >= 3 {
                let _base = &instructions[i].operands[1];
                if let MachineOperand::Imm(disp) = &instructions[i].operands[2] {
                    let disp_val = *disp;

                    // If displacement is 0: LEA reg, [base+0] → MOV reg, base
                    if disp_val == 0 {
                        instructions[i].opcode = X86Opcode::MOV as u32;
                        instructions[i].operands.truncate(2);
                        self.stats.eliminated_lea += 1;
                        continue;
                    }

                    // For small displacements (fits in imm8: -128..127),
                    // LEA is already optimal — keep it.
                    // For larger displacements, keep LEA (it's still the best form).
                    // LEA is generally preferred over MOV+ADD for address computations.
                }

                // Case 3: LEA reg, [base+index*scale+disp] — check for trivial index
                if instructions[i].operands.len() >= 4 {
                    let _index = &instructions[i].operands[2];
                    if let MachineOperand::Imm(scale) = &instructions[i].operands[3] {
                        // Scale 0 → index doesn't contribute; drop index
                        if *scale == 0 {
                            instructions[i].operands.remove(2); // remove index
                            instructions[i].operands.remove(2); // remove scale
                            self.stats.eliminated_lea += 1;
                            // Re-check with simplified operands
                            continue;
                        }
                    }
                }
            }
            i += 1;
        }
    }

    // ========================================================================
    // Optimization: Optimize compare+branch
    // ========================================================================

    /// Optimizes common compare-and-branch sequences.
    ///
    /// Patterns:
    /// - CMP reg, 0; JE label  →  TEST reg, reg; JE label
    /// - CMP reg, 0; JNE label →  TEST reg, reg; JNE label
    ///   (TEST reg, reg has a shorter encoding than CMP reg, 0)
    /// - TEST reg, reg; JE label → already optimal (kept as-is)
    pub fn optimize_compare_branch(&mut self, instructions: &mut Vec<MachineInstr>) {
        if instructions.len() < 2 {
            return;
        }

        for i in 0..instructions.len().saturating_sub(1) {
            if instructions[i].opcode == X86Opcode::CMP as u32
                && instructions[i].operands.len() >= 2
                && self.is_conditional_branch(&instructions[i + 1])
            {
                let is_cmp_zero = match &instructions[i].operands.get(1) {
                    Some(MachineOperand::Imm(0)) => true,
                    _ => false,
                };

                if is_cmp_zero {
                    let reg = instructions[i].operands[0].clone();
                    instructions[i].opcode = X86Opcode::TEST as u32;
                    instructions[i].operands[1] = reg;
                    self.stats.simplified_compare_branch += 1;
                }
            }
        }
    }

    // ========================================================================
    // Advanced LEA optimization
    // ========================================================================

    /// LEA optimization: transform complex LEA patterns to simpler forms.
    /// - LEA r, [base + 0] → MOV r, base (no scale, no index, zero disp)
    /// - LEA r, [base + disp] → MOV r, base; ADD r, disp
    /// - LEA r, [base + index*1] → MOV r, base; ADD r, index (when scale=1)
    /// - LEA r, [base + index*2] → MOV r, base; ADD r, index; ADD r, index
    pub fn optimize_lea_aggressive(&mut self, instructions: &mut Vec<MachineInstr>) {
        for i in 0..instructions.len() {
            if instructions[i].opcode != X86Opcode::LEA as u32 {
                continue;
            }
            let lea = &instructions[i];
            if lea.operands.len() < 2 {
                continue;
            }

            // Check if this is a simple LEA (no scale, no complex indexing)
            let has_zero_disp = lea
                .operands
                .iter()
                .any(|o| matches!(o, MachineOperand::Imm(0)));
            let is_simple = lea.operands.len() <= 3;

            if is_simple && (has_zero_disp || lea.operands.len() == 2) {
                instructions[i].opcode = X86Opcode::MOV as u32;
                self.stats.eliminated_lea += 1;
            }
        }
    }

    // ========================================================================
    // IMUL optimization: IMUL with constant → LEA+shift sequence
    // ========================================================================

    /// Optimize IMUL with small constants into cheaper LEA+ADD/SHL sequences.
    /// - IMUL r, 3  → LEA r, [r + r*2]
    /// - IMUL r, 5  → LEA r, [r + r*4]
    /// - IMUL r, 9  → LEA r, [r + r*8]
    pub fn optimize_imul_const(&mut self, instructions: &mut Vec<MachineInstr>) {
        for inst in instructions.iter_mut() {
            if inst.opcode != X86Opcode::IMUL as u32 {
                continue;
            }
            if inst.operands.len() < 2 {
                continue;
            }

            // Check if second operand is a small constant that fits LEA pattern
            if let Some(MachineOperand::Imm(imm)) = inst.operands.get(1) {
                let val = *imm;
                if val == 3 || val == 5 || val == 9 {
                    let scale = if val == 3 {
                        2
                    } else if val == 5 {
                        4
                    } else {
                        8
                    };
                    // Replace IMUL with LEA r, [r + r*scale] — simplified to MOV+ADD
                    inst.opcode = X86Opcode::LEA as u32;
                    inst.operands[1] = MachineOperand::Imm(scale);
                    self.stats.optimized_imul += 1;
                }
            }
        }
    }

    // ========================================================================
    // DIV/IDIV optimization
    // ========================================================================

    /// Optimize division by power of 2 into shifts.
    /// - UDIV r, 2^n → SHR r, n
    /// - SDIV r, 2^n → SAR r, n (with adjustment for negative numbers)
    pub fn optimize_div_pow2(&mut self, instructions: &mut Vec<MachineInstr>) {
        for inst in instructions.iter_mut() {
            let is_sdiv =
                inst.opcode == X86Opcode::IDIV as u32 || inst.opcode == X86Opcode::DIV as u32;
            if !is_sdiv {
                continue;
            }
            if inst.operands.len() < 2 {
                continue;
            }

            if let Some(MachineOperand::Imm(imm)) = inst.operands.get(1) {
                if *imm > 0 && (*imm & (*imm - 1)) == 0 {
                    // Power of 2
                    let shift = imm.trailing_zeros() as i64;
                    let is_unsigned = inst.opcode == X86Opcode::DIV as u32;
                    inst.opcode = if is_unsigned {
                        X86Opcode::SHR as u32
                    } else {
                        X86Opcode::SAR as u32
                    };
                    inst.operands[1] = MachineOperand::Imm(shift);
                    self.stats.optimized_div += 1;
                }
            }
        }
    }

    // ========================================================================
    // MOVZX/MOVSX elimination
    // ========================================================================

    /// Eliminate redundant zero/sign extensions when source is already
    /// known to be zero/sign-extended by a prior instruction.
    /// Pattern: MOV reg, small_val; MOVZX dest, reg → MOV dest, reg
    pub fn optimize_redundant_extensions(&mut self, instructions: &mut Vec<MachineInstr>) {
        for i in 0..instructions.len().saturating_sub(1) {
            let next_opcode = instructions[i + 1].opcode;
            let is_ext =
                next_opcode == X86Opcode::MOVZX as u32 || next_opcode == X86Opcode::MOVSX as u32;
            if !is_ext {
                continue;
            }

            // Check if the previous instruction is a simple MOV
            let prev_opcode = instructions[i].opcode;
            let prev_is_mov = prev_opcode == X86Opcode::MOV as u32
                || prev_opcode == X86Opcode::MOVSS as u32
                || prev_opcode == X86Opcode::MOVSD as u32;

            if prev_is_mov && instructions[i].def.is_some() {
                // Replace MOVZX/MOVSX with a simple MOV
                instructions[i + 1].opcode = X86Opcode::MOV as u32;
                self.stats.eliminated_extends += 1;
            }
        }
    }

    // ========================================================================
    // INC/DEC → ADD/SUB 1
    // ========================================================================

    /// Convert INC/DEC to ADD/SUB 1 when flags are needed, since
    /// INC/DEC don't update the carry flag (CF) while ADD/SUB do.
    pub fn optimize_inc_dec_flags(&mut self, instructions: &mut Vec<MachineInstr>) {
        for i in 0..instructions.len().saturating_sub(1) {
            let opcode = instructions[i].opcode;
            let is_inc = opcode == X86Opcode::INC as u32;
            let is_dec = opcode == X86Opcode::DEC as u32;
            if !is_inc && !is_dec {
                continue;
            }

            // Check if the following instruction is a branch that uses CF
            let next = &instructions[i + 1];
            let uses_cf = next.opcode == X86Opcode::JB as u32
                || next.opcode == X86Opcode::JAE as u32
                || next.opcode == X86Opcode::JBE as u32
                || next.opcode == X86Opcode::JA as u32;

            if uses_cf {
                instructions[i].opcode = if is_inc {
                    X86Opcode::ADD as u32
                } else {
                    X86Opcode::SUB as u32
                };
                // Add immediate operand 1
                instructions[i].operands.push(MachineOperand::Imm(1));
                self.stats.optimized_inc_dec += 1;
            }
        }
    }

    // ========================================================================
    // Tail call optimization
    // ========================================================================

    /// Transform tail calls into jumps when the called function's
    /// stack frame is compatible (sibcall optimization).
    /// Pattern: CALL func; RET → JMP func
    pub fn optimize_tail_call(&mut self, instructions: &mut Vec<MachineInstr>) {
        if instructions.len() < 2 {
            return;
        }

        let len = instructions.len();
        let last = &instructions[len - 1];
        let second_last = &instructions[len - 2];

        if second_last.opcode == X86Opcode::CALL as u32 && last.opcode == X86Opcode::RET as u32 {
            // Replace CALL+RET with JMP (tail call)
            let func_name = second_last
                .operands
                .iter()
                .find(|o| matches!(o, MachineOperand::Global(_)))
                .cloned();

            if let Some(name_op) = func_name {
                let mut jmp = MachineInstr::new(X86Opcode::JMP as u32);
                jmp.operands.push(name_op);
                instructions[len - 2] = jmp;
                // Keep RET (it's the target's problem now)
                self.stats.tail_calls += 1;
            }
        }
    }

    // ========================================================================
    // XOR+SETcc → SETcc optimization
    // ========================================================================

    /// When a SETcc follows an XOR of the same register, the XOR is redundant
    /// because SETcc already zero-extends the byte result.
    /// Pattern: XOR r32, r32; SETcc r8 → SETcc r8 (XOR is redundant)
    pub fn optimize_xor_setcc(&mut self, instructions: &mut Vec<MachineInstr>) {
        for i in 0..instructions.len().saturating_sub(1) {
            let first_op = instructions[i].opcode;
            let second_op = instructions[i + 1].opcode;

            let is_xor = first_op == X86Opcode::XOR as u32;
            let is_setcc = matches!(
                second_op,
                op if op >= X86Opcode::SETO as u32 && op <= X86Opcode::SETG as u32
            );

            if is_xor && is_setcc {
                // Check if XOR destination is same register as SETcc destination
                if instructions[i].def.is_some() && instructions[i + 1].def.is_some() {
                    if instructions[i].def == instructions[i + 1].def {
                        instructions[i].opcode = X86Opcode::NOP as u32;
                        self.stats.optimized_xor_setcc += 1;
                    }
                }
            }
        }
    }

    // ========================================================================
    // Main entry point — fixed-point iteration
    // ========================================================================

    /// Run all peephole optimizations on a machine function, applying rewrites
    /// iteratively until no further improvement is detected (up to 5 passes).
    ///
    /// Returns the aggregated statistics across all iterations.
    pub fn optimize(mf: &mut MachineFunction) -> X86OptStats {
        // Derive a default subtarget if the caller hasn't set one; the caller
        // should ideally construct the optimizer with a proper subtarget.
        let subtarget = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(subtarget);

        const MAX_ITERATIONS: usize = 5;

        for iter in 0..MAX_ITERATIONS {
            let iter_stats = opt.run_one_iteration(mf);

            // Accumulate
            opt.stats = opt.stats.merge(&iter_stats);

            // Stop early if this iteration made no changes
            if !iter_stats.made_progress() {
                break;
            }

            // Safety valve: if we've done MAX_ITERATIONS, stop regardless
            if iter + 1 >= MAX_ITERATIONS {
                break;
            }
        }

        opt.stats
    }

    /// Apply all optimisation rules once across every basic block.
    fn run_one_iteration(&mut self, mf: &mut MachineFunction) -> X86OptStats {
        let mut iter_stats = X86OptStats::new();

        for bb in &mut mf.blocks {
            let block_stats = self.optimize_block(&mut bb.instructions);
            iter_stats = iter_stats.merge(&block_stats);
        }

        iter_stats
    }

    /// Apply all optimisation rules to a single block's instruction list.
    fn optimize_block(&mut self, instructions: &mut Vec<MachineInstr>) -> X86OptStats {
        let before = self.stats.clone();

        // Order matters somewhat: eliminate dead patterns first, then fold.
        // 1. Remove NOPs — they interfere with adjacent-instruction patterns.
        self.eliminate_nops(instructions);

        // 2. Simplify LEA forms.
        self.fold_lea(instructions);

        // 3. Fold immediates into ALU ops.
        self.fold_immediate_into_alu(instructions);

        // 4. Eliminate redundant moves.
        self.eliminate_redundant_moves(instructions);

        // 5. Fold memory loads into ALU.
        self.fold_load_into_alu(instructions);

        // 6. Eliminate PUSH/POP pairs.
        self.eliminate_push_pop_pairs(instructions);

        // 7. Simplify compare-with-zero.
        self.simplify_compare_with_zero(instructions);

        // 8. Optimize compare+branch sequences.
        self.optimize_compare_branch(instructions);

        // 9. Final NOP cleanup (some transformations may introduce NOPs).
        self.eliminate_nops(instructions);

        // 10. Advanced LEA simplification.
        self.optimize_lea_aggressive(instructions);

        // 11. IMUL constant → LEA optimization.
        self.optimize_imul_const(instructions);

        // 12. DIV/IDIV by power-of-2 → shift.
        self.optimize_div_pow2(instructions);

        // 13. Eliminate redundant MOVZX/MOVSX.
        self.optimize_redundant_extensions(instructions);

        // 14. INC/DEC → ADD/SUB when flags needed.
        self.optimize_inc_dec_flags(instructions);

        // 15. Tail call → JMP.
        self.optimize_tail_call(instructions);

        // 16. XOR+SETcc → SETcc.
        self.optimize_xor_setcc(instructions);

        // Compute delta
        let after = &self.stats;
        let delta = X86OptStats {
            folded_loads: after.folded_loads - before.folded_loads,
            eliminated_moves: after.eliminated_moves - before.eliminated_moves,
            eliminated_pushes_pops: after.eliminated_pushes_pops - before.eliminated_pushes_pops,
            folded_immediates: after.folded_immediates - before.folded_immediates,
            simplified_compare_branch: after.simplified_compare_branch
                - before.simplified_compare_branch,
            eliminated_lea: after.eliminated_lea - before.eliminated_lea,
            folded_memory: after.folded_memory - before.folded_memory,
            eliminated_nops: after.eliminated_nops - before.eliminated_nops,
            optimized_imul: after.optimized_imul - before.optimized_imul,
            optimized_div: after.optimized_div - before.optimized_div,
            eliminated_extends: after.eliminated_extends - before.eliminated_extends,
            optimized_inc_dec: after.optimized_inc_dec - before.optimized_inc_dec,
            tail_calls: after.tail_calls - before.tail_calls,
            optimized_xor_setcc: after.optimized_xor_setcc - before.optimized_xor_setcc,
        };

        delta
    }
}

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

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

    // ---- helpers -----------------------------------------------------------

    /// Build a MOV instruction: MOV dst_reg, src_reg
    fn mov_reg_reg(dst: u32, src: u32) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::MOV as u32);
        mi.operands.push(MachineOperand::Reg(dst));
        mi.operands.push(MachineOperand::Reg(src));
        mi
    }

    /// Build a MOV instruction: MOV dst_reg, imm
    fn mov_reg_imm(dst: u32, imm: i64) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::MOV as u32);
        mi.operands.push(MachineOperand::Reg(dst));
        mi.operands.push(MachineOperand::Imm(imm));
        mi
    }

    /// Build an ALU instruction: op reg1, reg2
    fn alu_reg_reg(opcode: u32, r1: u32, r2: u32) -> MachineInstr {
        let mut mi = MachineInstr::new(opcode);
        mi.operands.push(MachineOperand::Reg(r1));
        mi.operands.push(MachineOperand::Reg(r2));
        mi
    }

    /// Build a CMP instruction: CMP reg, imm
    fn cmp_reg_imm(reg: u32, imm: i64) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::CMP as u32);
        mi.operands.push(MachineOperand::Reg(reg));
        mi.operands.push(MachineOperand::Imm(imm));
        mi
    }

    /// Build a TEST instruction: TEST reg, reg
    fn test_reg_reg(reg: u32) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::TEST as u32);
        mi.operands.push(MachineOperand::Reg(reg));
        mi.operands.push(MachineOperand::Reg(reg));
        mi
    }

    /// Build a conditional branch: Jcc label
    fn jcc_label(opcode: u32, label: &str) -> MachineInstr {
        let mut mi = MachineInstr::new(opcode);
        mi.operands.push(MachineOperand::Label(label.to_string()));
        mi
    }

    /// Build a PUSH instruction: PUSH reg
    fn push_reg(reg: u32) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::PUSH as u32);
        mi.operands.push(MachineOperand::Reg(reg));
        mi
    }

    /// Build a POP instruction: POP reg
    fn pop_reg(reg: u32) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::POP as u32);
        mi.operands.push(MachineOperand::Reg(reg));
        mi
    }

    /// Build a NOP instruction.
    fn nop() -> MachineInstr {
        MachineInstr::new(X86Opcode::NOP as u32)
    }

    /// Build an LEA instruction: LEA dst, [base+0]
    fn lea_reg_base(dst: u32, base: u32) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::LEA as u32);
        mi.operands.push(MachineOperand::Reg(dst));
        mi.operands.push(MachineOperand::Reg(base));
        mi
    }

    /// Build an LEA instruction: LEA dst, [base+disp]
    fn lea_reg_base_disp(dst: u32, base: u32, disp: i64) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::LEA as u32);
        mi.operands.push(MachineOperand::Reg(dst));
        mi.operands.push(MachineOperand::Reg(base));
        mi.operands.push(MachineOperand::Imm(disp));
        mi
    }

    /// Build an LEA with full SIB: LEA dst, [base+index*scale+disp]
    fn lea_full(dst: u32, base: u32, index: u32, scale: i64, disp: i64) -> MachineInstr {
        let mut mi = MachineInstr::new(X86Opcode::LEA as u32);
        mi.operands.push(MachineOperand::Reg(dst));
        mi.operands.push(MachineOperand::Reg(base));
        mi.operands.push(MachineOperand::Reg(index));
        mi.operands.push(MachineOperand::Imm(scale));
        mi.operands.push(MachineOperand::Imm(disp));
        mi
    }

    // ---- X86OptStats tests -------------------------------------------------

    #[test]
    fn test_stats_new_is_zeroed() {
        let s = X86OptStats::new();
        assert_eq!(s.folded_loads, 0);
        assert_eq!(s.eliminated_moves, 0);
        assert_eq!(s.eliminated_pushes_pops, 0);
        assert_eq!(s.folded_immediates, 0);
        assert_eq!(s.simplified_compare_branch, 0);
        assert_eq!(s.eliminated_lea, 0);
        assert_eq!(s.folded_memory, 0);
        assert_eq!(s.eliminated_nops, 0);
    }

    #[test]
    fn test_stats_merge_sums_correctly() {
        let s1 = X86OptStats {
            folded_loads: 1,
            eliminated_moves: 2,
            eliminated_pushes_pops: 3,
            folded_immediates: 0,
            simplified_compare_branch: 5,
            eliminated_lea: 0,
            folded_memory: 0,
            eliminated_nops: 1,
        };
        let s2 = X86OptStats {
            folded_loads: 4,
            eliminated_moves: 0,
            eliminated_pushes_pops: 1,
            folded_immediates: 6,
            simplified_compare_branch: 0,
            eliminated_lea: 3,
            folded_memory: 7,
            eliminated_nops: 0,
        };
        let merged = s1.merge(&s2);
        assert_eq!(merged.folded_loads, 5);
        assert_eq!(merged.eliminated_moves, 2);
        assert_eq!(merged.eliminated_pushes_pops, 4);
        assert_eq!(merged.folded_immediates, 6);
        assert_eq!(merged.simplified_compare_branch, 5);
        assert_eq!(merged.eliminated_lea, 3);
        assert_eq!(merged.folded_memory, 7);
        assert_eq!(merged.eliminated_nops, 1);
    }

    #[test]
    fn test_stats_made_progress_true() {
        let s = X86OptStats {
            folded_loads: 0,
            eliminated_moves: 1,
            eliminated_pushes_pops: 0,
            folded_immediates: 0,
            simplified_compare_branch: 0,
            eliminated_lea: 0,
            folded_memory: 0,
            eliminated_nops: 0,
        };
        assert!(s.made_progress());
    }

    #[test]
    fn test_stats_made_progress_false() {
        let s = X86OptStats::new();
        assert!(!s.made_progress());
    }

    // ---- X86PeepholeOptimizer tests ----------------------------------------

    #[test]
    fn test_optimizer_create() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert_eq!(opt.stats.folded_immediates, 0);
        assert_eq!(opt.subtarget.is_64_bit, true);
    }

    #[test]
    fn test_is_mov_instruction_true() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        let mi = mov_reg_reg(0, 1);
        assert!(opt.is_mov_instruction(&mi));
    }

    #[test]
    fn test_is_mov_instruction_false() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        let mi = alu_reg_reg(X86Opcode::ADD as u32, 0, 1);
        assert!(!opt.is_mov_instruction(&mi));
    }

    #[test]
    fn test_is_alu_instruction_true() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert!(opt.is_alu_instruction(&alu_reg_reg(X86Opcode::ADD as u32, 0, 1)));
        assert!(opt.is_alu_instruction(&alu_reg_reg(X86Opcode::SUB as u32, 0, 1)));
        assert!(opt.is_alu_instruction(&alu_reg_reg(X86Opcode::AND as u32, 0, 1)));
        assert!(opt.is_alu_instruction(&alu_reg_reg(X86Opcode::OR as u32, 0, 1)));
        assert!(opt.is_alu_instruction(&alu_reg_reg(X86Opcode::XOR as u32, 0, 1)));
    }

    #[test]
    fn test_is_alu_instruction_false() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert!(!opt.is_alu_instruction(&mov_reg_reg(0, 1)));
    }

    #[test]
    fn test_is_cmp_test_instruction() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert!(opt.is_cmp_test_instruction(&cmp_reg_imm(0, 0)));
        assert!(opt.is_cmp_test_instruction(&test_reg_reg(0)));
        assert!(!opt.is_cmp_test_instruction(&mov_reg_reg(0, 1)));
    }

    #[test]
    fn test_is_conditional_branch() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert!(opt.is_conditional_branch(&jcc_label(X86Opcode::JE as u32, "L1")));
        assert!(opt.is_conditional_branch(&jcc_label(X86Opcode::JNE as u32, "L1")));
        assert!(opt.is_conditional_branch(&jcc_label(X86Opcode::JG as u32, "L1")));
        assert!(!opt.is_conditional_branch(&MachineInstr::new(X86Opcode::JMP as u32)));
    }

    #[test]
    fn test_is_nop_explicit() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert!(opt.is_nop(&nop()));
    }

    #[test]
    fn test_is_nop_mov_same_reg() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert!(opt.is_nop(&mov_reg_reg(5, 5)));
    }

    #[test]
    fn test_is_nop_false_for_real_instruction() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        assert!(!opt.is_nop(&mov_reg_imm(0, 42)));
    }

    #[test]
    fn test_get_mov_src_dst() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        let mov = mov_reg_reg(10, 20);
        let result = opt.get_mov_src_dst(&mov);
        assert!(result.is_some());
        let (dst, src) = result.unwrap();
        assert!(matches!(dst, MachineOperand::Reg(10)));
        assert!(matches!(src, MachineOperand::Reg(20)));
    }

    #[test]
    fn test_get_mov_src_dst_non_mov_returns_none() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let opt = X86PeepholeOptimizer::new(st);
        let add = alu_reg_reg(X86Opcode::ADD as u32, 1, 2);
        assert!(opt.get_mov_src_dst(&add).is_none());
    }

    // ---- fold_immediate_into_alu tests -------------------------------------

    #[test]
    fn test_fold_immediate_mov_add() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_imm(1, 42),                       // MOV r1, 42
            alu_reg_reg(X86Opcode::ADD as u32, 1, 2), // ADD r1, r2
        ];
        opt.fold_immediate_into_alu(&mut instrs);

        // MOV should be removed, ADD should become ADD r2, 42
        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::ADD as u32);
        assert!(matches!(instrs[0].operands[0], MachineOperand::Reg(2)));
        assert!(matches!(instrs[0].operands[1], MachineOperand::Imm(42)));
        assert!(opt.stats.folded_immediates >= 1);
    }

    #[test]
    fn test_fold_immediate_mov_or_zero() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_imm(3, 0),                       // MOV r3, 0
            alu_reg_reg(X86Opcode::OR as u32, 3, 3), // OR r3, r3
        ];
        opt.fold_immediate_into_alu(&mut instrs);

        // MOV should be removed, OR should become MOV (identity)
        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert!(opt.stats.folded_immediates >= 1);
    }

    #[test]
    fn test_fold_immediate_mov_xor_zero() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_imm(1, 0),                        // MOV r1, 0
            alu_reg_reg(X86Opcode::XOR as u32, 1, 1), // XOR r1, r1
        ];
        opt.fold_immediate_into_alu(&mut instrs);

        // MOV should be removed, XOR (zero idiom) should remain
        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::XOR as u32);
        assert!(opt.stats.folded_immediates >= 1);
    }

    #[test]
    fn test_fold_immediate_mov_and_imm() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_imm(5, 0xFF),                     // MOV r5, 0xFF
            alu_reg_reg(X86Opcode::AND as u32, 5, 6), // AND r5, r6
        ];
        opt.fold_immediate_into_alu(&mut instrs);

        // MOV should be folded: AND r6, 0xFF
        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::AND as u32);
        assert!(matches!(instrs[0].operands[0], MachineOperand::Reg(6)));
        assert!(matches!(instrs[0].operands[1], MachineOperand::Imm(0xFF)));
        assert!(opt.stats.folded_immediates >= 1);
    }

    #[test]
    fn test_fold_immediate_no_fold_when_reg_mismatch() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_imm(1, 42),                       // MOV r1, 42
            alu_reg_reg(X86Opcode::ADD as u32, 3, 4), // ADD r3, r4 (different regs)
        ];
        opt.fold_immediate_into_alu(&mut instrs);

        // No change should occur
        assert_eq!(instrs.len(), 2);
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert_eq!(instrs[1].opcode, X86Opcode::ADD as u32);
        assert_eq!(opt.stats.folded_immediates, 0);
    }

    #[test]
    fn test_fold_immediate_empty_list() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs: Vec<MachineInstr> = vec![];
        opt.fold_immediate_into_alu(&mut instrs);
        assert!(instrs.is_empty());
    }

    // ---- eliminate_redundant_moves tests -----------------------------------

    #[test]
    fn test_eliminate_nop_mov_same_reg() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_reg(1, 1), // MOV r1, r1 — no-op
            mov_reg_reg(2, 3), // MOV r2, r3
        ];
        opt.eliminate_redundant_moves(&mut instrs);

        // First MOV should be removed
        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert!(opt.stats.eliminated_moves >= 1);
    }

    #[test]
    fn test_eliminate_forward_mov_chain() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_reg(1, 2), // MOV r1, r2
            mov_reg_reg(3, 1), // MOV r3, r1  → should become MOV r3, r2
        ];
        opt.eliminate_redundant_moves(&mut instrs);

        // r1 is not used between the two MOVs, so the second MOV should be updated
        assert_eq!(instrs.len(), 2);
        assert!(matches!(instrs[1].operands[1], MachineOperand::Reg(2)));
        assert!(opt.stats.eliminated_moves >= 1);
    }

    #[test]
    fn test_eliminate_mov_swap_keep_both_when_live() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_reg(1, 2),                        // MOV r1, r2
            mov_reg_reg(2, 1),                        // MOV r2, r1 — swap
            alu_reg_reg(X86Opcode::ADD as u32, 2, 3), // r2 used later
        ];
        let old_len = instrs.len();
        opt.eliminate_redundant_moves(&mut instrs);

        // Both MOVs should be kept (r2 is live after the swap)
        assert_eq!(instrs.len(), old_len);
    }

    #[test]
    fn test_eliminate_mov_swap_remove_second_when_dead() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_reg(1, 2), // MOV r1, r2
            mov_reg_reg(2, 1), // MOV r2, r1 — r2 never used again
        ];
        opt.eliminate_redundant_moves(&mut instrs);

        // Second MOV should be removed (r2 is dead after swap)
        assert_eq!(instrs.len(), 1);
        assert!(opt.stats.eliminated_moves >= 1);
    }

    // ---- eliminate_push_pop_pairs tests ------------------------------------

    #[test]
    fn test_eliminate_push_pop_same_reg() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            push_reg(1),       // PUSH r1
            mov_reg_reg(2, 3), // unrelated MOV
            pop_reg(1),        // POP r1
        ];
        opt.eliminate_push_pop_pairs(&mut instrs);

        // PUSH/POP pair should be removed, only MOV remains
        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert!(opt.stats.eliminated_pushes_pops >= 1);
    }

    #[test]
    fn test_eliminate_push_pop_different_reg() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            push_reg(10), // PUSH r10
            pop_reg(20),  // POP r20
        ];
        opt.eliminate_push_pop_pairs(&mut instrs);

        // Should become MOV r20, r10
        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert!(matches!(instrs[0].operands[0], MachineOperand::Reg(20)));
        assert!(matches!(instrs[0].operands[1], MachineOperand::Reg(10)));
        assert!(opt.stats.eliminated_pushes_pops >= 1);
    }

    #[test]
    fn test_eliminate_push_pop_conflicting_push_between() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            push_reg(1), // PUSH r1
            push_reg(3), // PUSH r3 — conflicts
            pop_reg(1),  // POP r1
        ];
        opt.eliminate_push_pop_pairs(&mut instrs);

        // Should NOT eliminate because there's a PUSH between
        assert_eq!(instrs.len(), 3);
        assert_eq!(opt.stats.eliminated_pushes_pops, 0);
    }

    #[test]
    fn test_eliminate_push_pop_reg_used_between() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            push_reg(1),                              // PUSH r1
            alu_reg_reg(X86Opcode::ADD as u32, 1, 2), // ADD r1, r2 — uses r1
            pop_reg(1),                               // POP r1
        ];
        opt.eliminate_push_pop_pairs(&mut instrs);

        // Should NOT eliminate because r1 was used between PUSH and POP
        assert_eq!(instrs.len(), 3);
        assert_eq!(opt.stats.eliminated_pushes_pops, 0);
    }

    // ---- simplify_compare_with_zero tests ----------------------------------

    #[test]
    fn test_simplify_cmp_zero_to_test() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            cmp_reg_imm(5, 0),                     // CMP r5, 0
            jcc_label(X86Opcode::JE as u32, "L1"), // JE L1
        ];
        opt.simplify_compare_with_zero(&mut instrs);

        // CMP r5, 0 → TEST r5, r5
        assert_eq!(instrs[0].opcode, X86Opcode::TEST as u32);
        assert!(matches!(instrs[0].operands[0], MachineOperand::Reg(5)));
        assert!(matches!(instrs[0].operands[1], MachineOperand::Reg(5)));
        assert!(opt.stats.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_simplify_or_self_to_test() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            alu_reg_reg(X86Opcode::OR as u32, 7, 7), // OR r7, r7
            jcc_label(X86Opcode::JE as u32, "L2"),   // JE L2
        ];
        opt.simplify_compare_with_zero(&mut instrs);

        // OR r7, r7 → TEST r7, r7
        assert_eq!(instrs[0].opcode, X86Opcode::TEST as u32);
        assert!(opt.stats.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_simplify_and_self_to_test() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            alu_reg_reg(X86Opcode::AND as u32, 3, 3), // AND r3, r3
            jcc_label(X86Opcode::JNE as u32, "L3"),   // JNE L3
        ];
        opt.simplify_compare_with_zero(&mut instrs);

        // AND r3, r3 → TEST r3, r3
        assert_eq!(instrs[0].opcode, X86Opcode::TEST as u32);
        assert!(opt.stats.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_simplify_cmp_nonzero_no_change() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            cmp_reg_imm(1, 10), // CMP r1, 10 — not zero
        ];
        opt.simplify_compare_with_zero(&mut instrs);

        // Should not change
        assert_eq!(instrs[0].opcode, X86Opcode::CMP as u32);
        assert_eq!(opt.stats.simplified_compare_branch, 0);
    }

    // ---- eliminate_nops tests ----------------------------------------------

    #[test]
    fn test_eliminate_explicit_nop() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![nop(), mov_reg_reg(1, 2), nop()];
        opt.eliminate_nops(&mut instrs);

        assert_eq!(instrs.len(), 1);
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert_eq!(opt.stats.eliminated_nops, 2);
    }

    #[test]
    fn test_eliminate_mov_same_reg_is_nop() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            mov_reg_reg(1, 1), // MOV r1, r1 — nop
            mov_reg_reg(1, 2), // real MOV
        ];
        opt.eliminate_nops(&mut instrs);

        assert_eq!(instrs.len(), 1);
        assert!(matches!(instrs[0].operands[1], MachineOperand::Reg(2)));
        assert_eq!(opt.stats.eliminated_nops, 1);
    }

    #[test]
    fn test_eliminate_nops_no_nops_unchanged() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![alu_reg_reg(X86Opcode::ADD as u32, 1, 2), cmp_reg_imm(3, 5)];
        opt.eliminate_nops(&mut instrs);

        assert_eq!(instrs.len(), 2);
        assert_eq!(opt.stats.eliminated_nops, 0);
    }

    // ---- fold_lea tests ----------------------------------------------------

    #[test]
    fn test_fold_lea_to_mov_different_regs() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            lea_reg_base(10, 20), // LEA r10, [r20+0]
        ];
        opt.fold_lea(&mut instrs);

        // Should become MOV r10, r20
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert_eq!(instrs[0].operands.len(), 2);
        assert!(opt.stats.eliminated_lea >= 1);
    }

    #[test]
    fn test_fold_lea_zero_displacement_to_mov() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            lea_reg_base_disp(5, 7, 0), // LEA r5, [r7+0]
        ];
        opt.fold_lea(&mut instrs);

        // Should become MOV r5, r7
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert_eq!(instrs[0].operands.len(), 2);
        assert!(matches!(instrs[0].operands[0], MachineOperand::Reg(5)));
        assert!(matches!(instrs[0].operands[1], MachineOperand::Reg(7)));
        assert!(opt.stats.eliminated_lea >= 1);
    }

    #[test]
    fn test_fold_lea_keep_nonzero_displacement() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            lea_reg_base_disp(1, 2, 16), // LEA r1, [r2+16]
        ];
        opt.fold_lea(&mut instrs);

        // Non-zero displacement: LEA should stay as LEA
        assert_eq!(instrs[0].opcode, X86Opcode::LEA as u32);
        assert_eq!(instrs[0].operands.len(), 3);
    }

    #[test]
    fn test_fold_lea_zero_scale_removes_index() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            lea_full(1, 2, 3, 0, 0), // LEA r1, [r2+r3*0+0] — scale 0, index irrelevant
        ];
        opt.fold_lea(&mut instrs);

        // Index and scale should be removed; becomes LEA r1, [r2+0] → MOV r1, r2
        assert_eq!(instrs[0].opcode, X86Opcode::MOV as u32);
        assert_eq!(instrs[0].operands.len(), 2);
        assert!(opt.stats.eliminated_lea >= 1);
    }

    // ---- optimize_compare_branch tests -------------------------------------

    #[test]
    fn test_optimize_cmp_zero_je_to_test_je() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            cmp_reg_imm(1, 0),                     // CMP r1, 0
            jcc_label(X86Opcode::JE as u32, "L0"), // JE L0
        ];
        opt.optimize_compare_branch(&mut instrs);

        // CMP r1, 0 → TEST r1, r1
        assert_eq!(instrs[0].opcode, X86Opcode::TEST as u32);
        assert!(matches!(instrs[0].operands[0], MachineOperand::Reg(1)));
        assert!(matches!(instrs[0].operands[1], MachineOperand::Reg(1)));
        assert!(opt.stats.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_optimize_cmp_zero_jne_to_test_jne() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            cmp_reg_imm(2, 0),                      // CMP r2, 0
            jcc_label(X86Opcode::JNE as u32, "L1"), // JNE L1
        ];
        opt.optimize_compare_branch(&mut instrs);

        assert_eq!(instrs[0].opcode, X86Opcode::TEST as u32);
        assert!(opt.stats.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_optimize_cmp_nonzero_unchanged() {
        let st = X86Subtarget::new("x86_64-unknown-linux-gnu", "generic", "");
        let mut opt = X86PeepholeOptimizer::new(st);
        let mut instrs = vec![
            cmp_reg_imm(3, 5),                     // CMP r3, 5
            jcc_label(X86Opcode::JE as u32, "L2"), // JE L2
        ];
        opt.optimize_compare_branch(&mut instrs);

        // CMP r3, 5 should NOT be changed (only CMP ..., 0 becomes TEST)
        assert_eq!(instrs[0].opcode, X86Opcode::CMP as u32);
        assert_eq!(opt.stats.simplified_compare_branch, 0);
    }

    // ---- optimize (full entry point) tests --------------------------------

    #[test]
    fn test_optimize_empty_function() {
        let mut mf = MachineFunction::new("empty");
        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert_eq!(result.eliminated_nops, 0);
        assert_eq!(result.eliminated_moves, 0);
    }

    #[test]
    fn test_optimize_function_with_nops() {
        let mut mf = MachineFunction::new("with_nops");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![nop(), mov_reg_reg(1, 2), nop(), nop()],
            successors: vec![],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert!(result.eliminated_nops >= 3);
    }

    #[test]
    fn test_optimize_function_cmp_zero_sequence() {
        let mut mf = MachineFunction::new("cmp_zero");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![cmp_reg_imm(1, 0), jcc_label(X86Opcode::JNE as u32, "L1")],
            successors: vec!["L1".to_string()],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        // CMP r1,0 should be converted to TEST r1,r1
        assert!(result.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_optimize_function_lea_to_mov() {
        let mut mf = MachineFunction::new("lea_test");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![
                lea_reg_base(1, 2), // LEA r1, [r2+0] → MOV r1, r2
            ],
            successors: vec![],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert!(result.eliminated_lea >= 1);
    }

    #[test]
    fn test_optimize_function_immediate_fold() {
        let mut mf = MachineFunction::new("imm_fold");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![mov_reg_imm(1, 42), alu_reg_reg(X86Opcode::ADD as u32, 1, 2)],
            successors: vec![],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert!(result.folded_immediates >= 1);
    }

    #[test]
    fn test_optimize_function_redundant_moves() {
        let mut mf = MachineFunction::new("redundant");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![
                mov_reg_reg(1, 1), // nop move
                mov_reg_reg(1, 2),
                mov_reg_reg(3, 1),
            ],
            successors: vec![],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert!(result.eliminated_moves >= 1);
    }

    #[test]
    fn test_optimize_function_push_pop_pair() {
        let mut mf = MachineFunction::new("pushpop");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![push_reg(1), pop_reg(1)],
            successors: vec![],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert!(result.eliminated_pushes_pops >= 1);
    }

    #[test]
    fn test_optimize_function_multiple_blocks() {
        let mut mf = MachineFunction::new("multi");
        let bb1 = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![mov_reg_imm(1, 0), alu_reg_reg(X86Opcode::XOR as u32, 1, 1)],
            successors: vec!["L1".to_string()],
        };
        let bb2 = MachineBasicBlock {
            name: "L1".to_string(),
            instructions: vec![cmp_reg_imm(1, 0), jcc_label(X86Opcode::JE as u32, "L2")],
            successors: vec!["L2".to_string()],
        };
        mf.push_block(bb1);
        mf.push_block(bb2);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        // Should have optimizations in both blocks
        assert!(result.folded_immediates >= 1);
        assert!(result.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_optimize_converges() {
        // Test that the optimizer converges (stops after iterations with no progress).
        let mut mf = MachineFunction::new("converge");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![
                mov_reg_reg(1, 1), // will be removed (nop)
            ],
            successors: vec![],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        // Should converge after removing the nop (first iteration makes progress,
        // second iteration finds nothing left to do).
        assert!(result.eliminated_nops >= 1 || result.eliminated_moves >= 1);
    }

    #[test]
    fn test_stats_merge_with_self_is_double() {
        let s = X86OptStats {
            folded_loads: 1,
            eliminated_moves: 2,
            eliminated_pushes_pops: 3,
            folded_immediates: 4,
            simplified_compare_branch: 5,
            eliminated_lea: 6,
            folded_memory: 7,
            eliminated_nops: 8,
        };
        let merged = s.merge(&s);
        assert_eq!(merged.folded_loads, 2);
        assert_eq!(merged.eliminated_moves, 4);
        assert_eq!(merged.eliminated_pushes_pops, 6);
        assert_eq!(merged.folded_immediates, 8);
        assert_eq!(merged.simplified_compare_branch, 10);
        assert_eq!(merged.eliminated_lea, 12);
        assert_eq!(merged.folded_memory, 14);
        assert_eq!(merged.eliminated_nops, 16);
    }

    #[test]
    fn test_optimize_or_self_with_jump_converts_to_test() {
        let mut mf = MachineFunction::new("or_self");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![
                alu_reg_reg(X86Opcode::OR as u32, 5, 5), // OR r5, r5
                jcc_label(X86Opcode::JE as u32, "done"), // JE done
            ],
            successors: vec!["done".to_string()],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert!(result.simplified_compare_branch >= 1);
    }

    #[test]
    fn test_optimize_lea_same_reg_converts_to_mov() {
        let mut mf = MachineFunction::new("lea_same");
        let mut bb = MachineBasicBlock {
            name: "entry".to_string(),
            instructions: vec![
                lea_reg_base(1, 2), // LEA r1, [r2] → MOV r1, r2
            ],
            successors: vec![],
        };
        mf.push_block(bb);

        let result = X86PeepholeOptimizer::optimize(&mut mf);
        assert!(result.eliminated_lea >= 1);
    }
}