llvm-native-core-ext 0.1.0

Extended modules for llvm-native-core: analysis passes, transforms, codegen extras, bitcode, linker, JIT, utilities. Part of the llvm-native workspace (https://crates.io/crates/llvm-native).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
//! CodeGen Optimization Passes — machine-level code generation optimizations.
//! Clean-room behavioral reconstruction. Phase 6 — LLVM.CODEGEN.1 Court.
//!
//! Machine-level passes operate on `MachineFunction`, `MachineBasicBlock`,
//! and `MachineInstr` structures. These passes run during code generation
//! after IR-level optimization and before/after register allocation.
//!
//! Passes implemented:
//! - MachineBlockPlacement — optimize basic block layout for fallthrough
//! - MachineBranchFolding — fold branches to common targets
//! - MachineCSE — common subexpression elimination on machine instructions
//! - MachineLICM — loop-invariant code motion for machine code
//! - MachineSinking — sink instructions into successor blocks
//! - MachineCombiner — combine machine instruction patterns
//! - MachineTraceMetrics — estimate instruction throughput per trace
//! - EarlyIfConversion — if-conversion before register allocation
//! - OptimizePHIs — optimize PHI instruction placement
//! - MachineLateInstrsCleanup — remove dead instructions after RA
//! - BundleMachineCFG — bundle instructions for CFG representation
//! - MachineVerifier — verify machine code invariants
//! - ResetMachineFunction — reset machine function state between compilations

use std::collections::{HashMap, HashSet, VecDeque};

// ============================================================================
// Machine IR data structures (lightweight, for optimization passes)
// ============================================================================

/// A virtual register or physical register reference.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MCRegister(pub u32);

/// A machine operand: register, immediate, or memory reference.
#[derive(Debug, Clone, PartialEq)]
pub enum MachineOperand {
    Reg(MCRegister),
    Imm(i64),
    FPImm(f64),
    Mem {
        base: MCRegister,
        offset: i64,
        size: u32,
    },
    Block(u32),
    Global(String),
    CCMask(u32),
}

/// A machine instruction.
#[derive(Debug, Clone)]
pub struct MachineInstr {
    pub opcode: u32,
    pub operands: Vec<MachineOperand>,
    pub flags: MachineInstrFlags,
    pub id: u64,
    pub debug_loc: Option<String>,
    /// Bundled instructions (for VLIW/packet architectures)
    pub bundle: Vec<MachineInstr>,
}

/// Machine instruction flags.
#[derive(Debug, Clone, Copy, Default)]
pub struct MachineInstrFlags {
    pub is_terminator: bool,
    pub is_branch: bool,
    pub is_indirect_branch: bool,
    pub is_call: bool,
    pub is_return: bool,
    pub is_barrier: bool,
    pub has_side_effects: bool,
    pub is_load: bool,
    pub is_store: bool,
    pub may_load: bool,
    pub may_store: bool,
    pub is_compare: bool,
    pub is_move_imm: bool,
    pub is_move_reg: bool,
    pub is_bitcast: bool,
    pub is_select: bool,
    pub is_phi: bool,
    pub is_copy: bool,
    pub is_extract_subreg: bool,
    pub is_insert_subreg: bool,
    pub is_convergent: bool,
    pub is_bundle_head: bool,
    pub is_inside_bundle: bool,
    pub is_kill: bool,
    pub is_dead: bool,
    pub is_undef: bool,
    pub is_early_clobber: bool,
    pub is_debug_instr: bool,
}

impl MachineInstr {
    pub fn new(opcode: u32) -> Self {
        Self {
            opcode,
            operands: Vec::new(),
            flags: MachineInstrFlags::default(),
            id: 0,
            debug_loc: None,
            bundle: Vec::new(),
        }
    }

    pub fn with_operand(mut self, op: MachineOperand) -> Self {
        self.operands.push(op);
        self
    }

    pub fn with_flag(mut self, flag_setter: fn(&mut MachineInstrFlags)) -> Self {
        flag_setter(&mut self.flags);
        self
    }

    pub fn is_terminator(&self) -> bool {
        self.flags.is_terminator
    }

    pub fn is_branch(&self) -> bool {
        self.flags.is_branch
    }

    pub fn reads_register(&self, reg: MCRegister) -> bool {
        self.operands.iter().any(|op| match op {
            MachineOperand::Reg(r) => *r == reg,
            MachineOperand::Mem { base, .. } => *base == reg,
            _ => false,
        })
    }

    pub fn writes_register(&self, reg: MCRegister) -> bool {
        // For simplicity, first register operand is the def
        self.operands.first().map_or(false, |op| match op {
            MachineOperand::Reg(r) => *r == reg,
            _ => false,
        })
    }

    /// Check if this instruction commutes with another (for CSE).
    pub fn is_identical_to(&self, other: &MachineInstr) -> bool {
        self.opcode == other.opcode
            && self.operands.len() == other.operands.len()
            && self
                .operands
                .iter()
                .zip(other.operands.iter())
                .all(|(a, b)| a == b)
    }
}

/// A machine basic block.
#[derive(Debug, Clone)]
pub struct MachineBasicBlock {
    pub id: u32,
    pub name: String,
    pub instructions: Vec<MachineInstr>,
    pub successors: Vec<u32>,
    pub predecessors: Vec<u32>,
    pub is_entry: bool,
    pub is_exit: bool,
    pub alignment: u32,
    pub frequency: f64,
    pub is_landing_pad: bool,
}

impl MachineBasicBlock {
    pub fn new(id: u32, name: impl Into<String>) -> Self {
        Self {
            id,
            name: name.into(),
            instructions: Vec::new(),
            successors: Vec::new(),
            predecessors: Vec::new(),
            is_entry: false,
            is_exit: false,
            alignment: 0,
            frequency: 1.0,
            is_landing_pad: false,
        }
    }

    pub fn push_instr(&mut self, instr: MachineInstr) {
        self.instructions.push(instr);
    }
}

/// A machine function.
#[derive(Debug, Clone)]
pub struct MachineFunction {
    pub name: String,
    /// Unique ID for this function (for cross-function tracking).
    pub id: usize,
    pub blocks: Vec<MachineBasicBlock>,
    pub entry_block: u32,
    pub exit_blocks: Vec<u32>,
    /// Live-in registers for each block
    pub live_ins: HashMap<u32, HashSet<MCRegister>>,
    /// Live-out registers for each block
    pub live_outs: HashMap<u32, HashSet<MCRegister>>,
    /// Frame information
    pub frame_size: u32,
    pub stack_alignment: u32,
    /// Monotonically increasing block ID counter.
    next_block_id_counter: usize,
}

impl MachineFunction {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            id: 0,
            blocks: Vec::new(),
            entry_block: 0,
            exit_blocks: Vec::new(),
            live_ins: HashMap::new(),
            live_outs: HashMap::new(),
            frame_size: 0,
            stack_alignment: 16,
            next_block_id_counter: 0,
        }
    }

    pub fn add_block(&mut self, block: MachineBasicBlock) -> u32 {
        let id = block.id;
        self.blocks.push(block);
        id
    }

    pub fn get_block(&self, id: u32) -> Option<&MachineBasicBlock> {
        self.blocks.iter().find(|b| b.id == id)
    }

    pub fn get_block_mut(&mut self, id: u32) -> Option<&mut MachineBasicBlock> {
        self.blocks.iter_mut().find(|b| b.id == id)
    }

    pub fn block_by_id(&self, id: u32) -> Option<&MachineBasicBlock> {
        self.blocks.iter().find(|b| b.id == id)
    }

    pub fn next_block_id(&mut self) -> u32 {
        let id = self.next_block_id_counter as u32;
        self.next_block_id_counter += 1;
        id
    }
}

// ============================================================================
// MachineBlockPlacement — optimize basic block layout for fallthrough
// ============================================================================
/// MachineBlockPlacement reorders basic blocks to maximize fallthrough
/// (where execution flows from one block to the next without a taken branch).
/// Uses block frequency information and branch probabilities to determine
/// the optimal layout.
pub struct MachineBlockPlacement {
    pub reordered_blocks: usize,
    pub fallthroughs_created: usize,
}

impl MachineBlockPlacement {
    pub fn new() -> Self {
        Self {
            reordered_blocks: 0,
            fallthroughs_created: 0,
        }
    }

    /// Compute the optimal block layout using a greedy chain-building algorithm.
    /// Blocks are chained together based on branch probability, with the
    /// most likely successor placed immediately after its predecessor.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        if mf.blocks.is_empty() {
            return 0;
        }

        self.reordered_blocks = 0;
        self.fallthroughs_created = 0;

        // Build a chain of blocks using a simple greedy algorithm:
        // - Start with the entry block
        // - For each block, place its most likely successor next
        // - Skip blocks already placed

        let mut placed: HashSet<u32> = HashSet::new();
        let mut new_order: Vec<u32> = Vec::new();

        // Start from entry
        let entry = mf.entry_block;
        let mut current = entry;

        while !placed.contains(&current) && placed.len() < mf.blocks.len() {
            placed.insert(current);
            new_order.push(current);

            // Find the best successor (highest frequency)
            if let Some(block) = mf.get_block(current) {
                let mut best_succ: Option<u32> = None;
                let mut best_freq = f64::NEG_INFINITY;

                for &succ in &block.successors {
                    if !placed.contains(&succ) {
                        if let Some(succ_block) = mf.get_block(succ) {
                            if succ_block.frequency > best_freq {
                                best_freq = succ_block.frequency;
                                best_succ = Some(succ);
                            }
                        }
                    }
                }

                if let Some(next) = best_succ {
                    current = next;
                    self.fallthroughs_created += 1;
                } else {
                    // Find any unplaced block
                    let unplaced = mf
                        .blocks
                        .iter()
                        .find(|b| !placed.contains(&b.id))
                        .map(|b| b.id);

                    if let Some(next) = unplaced {
                        current = next;
                    } else {
                        break;
                    }
                }
            } else {
                break;
            }
        }

        // Add any remaining blocks
        for block in &mf.blocks {
            if !placed.contains(&block.id) {
                new_order.push(block.id);
            }
        }

        // Reorder blocks in the machine function
        let mut ordered_blocks: Vec<MachineBasicBlock> = Vec::new();
        for &id in &new_order {
            if let Some(idx) = mf.blocks.iter().position(|b| b.id == id) {
                ordered_blocks.push(mf.blocks.remove(idx));
            }
        }
        // Add any blocks not in new_order (shouldn't happen, but be safe)
        ordered_blocks.append(&mut mf.blocks);

        self.reordered_blocks = ordered_blocks.len();
        mf.blocks = ordered_blocks;
        self.reordered_blocks
    }
}

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

// ============================================================================
// MachineBranchFolding — fold branches to common targets
// ============================================================================
/// MachineBranchFolding optimizes branching by folding branches
/// to common targets. If multiple predecessors branch to the same
/// successor, and that successor contains only an unconditional branch,
/// the predecessors can branch directly to the final target.
pub struct MachineBranchFolding {
    pub branches_folded: usize,
    pub tail_merged_blocks: usize,
}

impl MachineBranchFolding {
    pub fn new() -> Self {
        Self {
            branches_folded: 0,
            tail_merged_blocks: 0,
        }
    }

    /// Run branch folding on a machine function.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.branches_folded = 0;
        self.tail_merged_blocks = 0;

        // Phase 1: Fold unconditional branches that go to a block
        // containing only an unconditional branch
        let mut to_redirect: Vec<(u32, u32, u32)> = Vec::new(); // (from_block, via_block, final_target)

        for block in &mf.blocks {
            // Check if this block is a "forwarding block":
            // contains only an unconditional branch
            if block.instructions.len() == 1 {
                let instr = &block.instructions[0];
                if instr.flags.is_branch && !instr.flags.is_indirect_branch {
                    if let Some(target) = block.successors.first() {
                        // Find all predecessors that branch to this block
                        for pred_id in &block.predecessors {
                            if let Some(pred) = mf.get_block(*pred_id) {
                                // Don't redirect if the predecessor has
                                // the forwarding block as its only successor
                                if pred.successors.len() == 1 {
                                    to_redirect.push((*pred_id, block.id, *target));
                                }
                            }
                        }
                    }
                }
            }
        }

        // Apply redirections
        for (from, via, to) in &to_redirect {
            if let Some(block) = mf.get_block_mut(*from) {
                // Update the last instruction's branch target
                if let Some(last) = block.instructions.last_mut() {
                    for op in &mut last.operands {
                        if let MachineOperand::Block(id) = op {
                            if *id == *via {
                                *id = *to;
                                self.branches_folded += 1;
                            }
                        }
                    }
                }
                // Update successors
                if let Some(pos) = block.successors.iter().position(|s| s == via) {
                    block.successors[pos] = *to;
                }
            }
        }

        // Phase 2: Merge identical block tails
        self.tail_merge(mf);

        self.branches_folded
    }

    /// Merge blocks that have identical instruction sequences at the tail.
    fn tail_merge(&mut self, mf: &mut MachineFunction) {
        let block_count = mf.blocks.len();
        if block_count < 2 {
            return;
        }

        // Simple tail merge: look for blocks ending with the same
        // unconditional branch to the same target
        let mut tail_groups: HashMap<(u32, u32), Vec<u32>> = HashMap::new();

        for block in &mf.blocks {
            if block.instructions.is_empty() {
                continue;
            }
            let last = &block.instructions[block.instructions.len() - 1];
            let instr_len = block.instructions.len();

            if last.flags.is_branch
                && !last.flags.is_indirect_branch
                && block.successors.len() == 1
            {
                let target = block.successors[0];
                tail_groups
                    .entry((target, instr_len as u32))
                    .or_default()
                    .push(block.id);
            }
        }

        for ((target, _), blocks) in &tail_groups {
            if blocks.len() >= 2 {
                self.tail_merged_blocks += blocks.len() - 1;
                // In a full implementation, we'd redirect all but one
                // predecessor to the canonical block
                let _ = target;
            }
        }
    }
}

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

// ============================================================================
// MachineCSE — common subexpression elimination on machine instructions
// ============================================================================
/// MachineCSE eliminates redundant machine instructions that compute
/// the same value. It tracks available expressions within each basic
/// block and replaces later occurrences with the earlier result.
pub struct MachineCSE {
    pub eliminated: usize,
    pub cross_block: bool,
}

impl MachineCSE {
    pub fn new() -> Self {
        Self {
            eliminated: 0,
            cross_block: false,
        }
    }

    /// Run CSE on a machine function.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.eliminated = 0;

        for block in &mut mf.blocks {
            self.eliminated += self.cse_block(block);
        }

        // Cross-block CSE using available expressions
        if self.cross_block {
            self.eliminated += self.cse_cross_block(mf);
        }

        self.eliminated
    }

    /// Perform CSE within a single basic block.
    fn cse_block(&mut self, block: &mut MachineBasicBlock) -> usize {
        let mut eliminated = 0usize;
        let mut available: Vec<(MachineInstr, usize)> = Vec::new();
        let mut to_remove: Vec<usize> = Vec::new();

        for (i, instr) in block.instructions.iter().enumerate() {
            // Skip instructions with side effects
            if instr.flags.has_side_effects
                || instr.flags.is_store
                || instr.flags.is_call
                || instr.flags.is_terminator
                || instr.flags.is_branch
            {
                available.push((instr.clone(), i));
                continue;
            }

            // Check if this instruction is already available
            let mut found = false;
            for (avail, _avail_idx) in &available {
                if avail.is_identical_to(instr) {
                    // Found a match — can eliminate this instruction
                    to_remove.push(i);
                    eliminated += 1;
                    found = true;
                    break;
                }
            }

            if !found {
                available.push((instr.clone(), i));
            }
        }

        // Remove eliminated instructions (in reverse order)
        for &i in to_remove.iter().rev() {
            block.instructions.remove(i);
        }

        eliminated
    }

    /// Perform cross-block CSE using available expression analysis.
    fn cse_cross_block(&mut self, _mf: &mut MachineFunction) -> usize {
        // For simplicity, cross-block CSE is a stub
        // A full implementation would compute available expressions
        // using a forward dataflow analysis
        0
    }
}

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

// ============================================================================
// MachineLICM — loop-invariant code motion for machine code
// ============================================================================
/// MachineLICM hoists loop-invariant machine instructions out of loops.
/// An instruction is loop-invariant if all its operands are defined
/// outside the loop or are themselves loop-invariant.
pub struct MachineLICM {
    pub hoisted: usize,
    pub sunk: usize,
}

impl MachineLICM {
    pub fn new() -> Self {
        Self {
            hoisted: 0,
            sunk: 0,
        }
    }

    /// Run LICM on a machine function.
    /// Identifies loops via back-edge detection, determines invariant
    /// instructions, and hoists them to the loop preheader.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.hoisted = 0;
        self.sunk = 0;

        // Build a loop structure: find back-edges
        let loops = self.detect_loops(mf);

        for (header_id, loop_blocks) in &loops {
            // Find the preheader (predecessor outside the loop)
            let preheader = self.find_preheader(mf, *header_id, loop_blocks);
            if preheader.is_none() {
                continue;
            }
            let preheader_id = preheader.unwrap();

            // Collect invariant instructions
            let invariant_instrs = self.find_invariant_instructions(mf, loop_blocks);

            // Hoist invariant instructions to the preheader
            self.hoist_instructions(mf, preheader_id, &invariant_instrs);
        }

        self.hoisted
    }

    /// Detect loops by finding back-edges (branch to a block with lower id).
    fn detect_loops(&self, mf: &MachineFunction) -> HashMap<u32, HashSet<u32>> {
        let mut loops: HashMap<u32, HashSet<u32>> = HashMap::new();

        for block in &mf.blocks {
            for &succ in &block.successors {
                if succ < block.id {
                    // Back-edge: block -> succ where succ is a loop header
                    let entry = loops.entry(succ).or_default();
                    entry.insert(block.id);

                    // Add all blocks between succ and block
                    let mut worklist: VecDeque<u32> = VecDeque::new();
                    let mut visited: HashSet<u32> = HashSet::new();
                    worklist.push_back(block.id);

                    while let Some(current) = worklist.pop_front() {
                        if current == succ || visited.contains(&current) {
                            continue;
                        }
                        visited.insert(current);
                        entry.insert(current);

                        if let Some(b) = mf.get_block(current) {
                            for &pred in &b.predecessors {
                                if !visited.contains(&pred) {
                                    worklist.push_back(pred);
                                }
                            }
                        }
                    }
                }
            }
        }

        loops
    }

    /// Find a preheader for a loop: a predecessor of the header
    /// that is not part of the loop.
    fn find_preheader(
        &self,
        mf: &MachineFunction,
        header_id: u32,
        loop_blocks: &HashSet<u32>,
    ) -> Option<u32> {
        if let Some(header) = mf.get_block(header_id) {
            for &pred in &header.predecessors {
                if !loop_blocks.contains(&pred) {
                    return Some(pred);
                }
            }
        }
        None
    }

    /// Find loop-invariant instructions: those whose operands are all
    /// defined outside the loop.
    fn find_invariant_instructions(
        &self,
        mf: &MachineFunction,
        loop_blocks: &HashSet<u32>,
    ) -> Vec<(u32, usize, MachineInstr)> {
        let mut invariants = Vec::new();

        for block in &mf.blocks {
            if !loop_blocks.contains(&block.id) {
                continue;
            }

            for (i, instr) in block.instructions.iter().enumerate() {
                // Skip terminators and side-effecting instructions
                if instr.flags.is_terminator
                    || instr.flags.has_side_effects
                    || instr.flags.is_store
                    || instr.flags.is_call
                {
                    continue;
                }

                // Check if all register operands are defined outside the loop
                let is_invariant = instr.operands.iter().all(|op| match op {
                    MachineOperand::Reg(r) => {
                        // Check if this register is defined inside the loop
                        !self.is_reg_defined_in_loop(mf, *r, loop_blocks)
                    }
                    MachineOperand::Mem { base, .. } => {
                        !self.is_reg_defined_in_loop(mf, *base, loop_blocks)
                    }
                    _ => true, // Immediates and other non-reg ops are invariant
                });

                if is_invariant {
                    invariants.push((block.id, i, instr.clone()));
                }
            }
        }

        invariants
    }

    /// Check if a register is defined (written) anywhere in the loop.
    fn is_reg_defined_in_loop(
        &self,
        mf: &MachineFunction,
        reg: MCRegister,
        loop_blocks: &HashSet<u32>,
    ) -> bool {
        for block in &mf.blocks {
            if !loop_blocks.contains(&block.id) {
                continue;
            }
            for instr in &block.instructions {
                if instr.writes_register(reg) {
                    return true;
                }
            }
        }
        false
    }

    /// Hoist instructions to the preheader block.
    fn hoist_instructions(
        &mut self,
        mf: &mut MachineFunction,
        preheader_id: u32,
        invariants: &[(u32, usize, MachineInstr)],
    ) {
        if let Some(preheader) = mf.get_block_mut(preheader_id) {
            // Insert before the terminator
            let insert_pos = if !preheader.instructions.is_empty()
                && preheader.instructions.last().unwrap().is_terminator()
            {
                preheader.instructions.len() - 1
            } else {
                preheader.instructions.len()
            };

            for (_block_id, _instr_idx, instr) in invariants {
                preheader.instructions.insert(insert_pos, instr.clone());
                self.hoisted += 1;
            }
        }
    }
}

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

// ============================================================================
// MachineSinking — sink instructions into successor blocks
// ============================================================================
/// MachineSinking sinks instructions into successor blocks when the
/// instruction is used only in a specific successor. This can reduce
/// execution count on paths where the instruction is not needed.
pub struct MachineSinking {
    pub sunk: usize,
}

impl MachineSinking {
    pub fn new() -> Self {
        Self { sunk: 0 }
    }

    /// Run instruction sinking on a machine function.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.sunk = 0;

        for block_idx in 0..mf.blocks.len() {
            let block_id = mf.blocks[block_idx].id;
            let successors: Vec<u32> = mf.blocks[block_idx].successors.clone();

            if successors.len() != 1 {
                continue;
            }

            let succ_id = successors[0];
            let mut to_sink: Vec<MachineInstr> = Vec::new();
            let mut remove_indices: Vec<usize> = Vec::new();

            // Find non-terminator, non-side-effecting instructions
            // that can be sunk to the single successor
            for (i, instr) in mf.blocks[block_idx].instructions.iter().enumerate() {
                if instr.is_terminator()
                    || instr.flags.has_side_effects
                    || instr.flags.is_call
                {
                    continue;
                }

                // Check if the result is only used in the successor
                // (For simplicity, assume single-use instructions can be sunk)
                to_sink.push(instr.clone());
                remove_indices.push(i);
            }

            // Move instructions to the successor
            if !to_sink.is_empty() {
                if let Some(succ_block) = mf.get_block_mut(succ_id) {
                    for instr in to_sink {
                        succ_block.instructions.insert(0, instr);
                        self.sunk += 1;
                    }
                }

                // Remove from source block (reverse order)
                for &i in remove_indices.iter().rev() {
                    if i < mf.blocks[block_idx].instructions.len() {
                        mf.blocks[block_idx].instructions.remove(i);
                    }
                }
            }
        }

        self.sunk
    }
}

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

// ============================================================================
// MachineCombiner — combine machine instruction patterns
// ============================================================================
/// MachineCombiner performs peephole-style combining of machine
/// instruction patterns. Examples:
/// - `mov r1, r2; mov r3, r1` → `mov r3, r2`
/// - `add r1, 0` → `mov r1, r1` (or eliminate)
/// - `sub r1, 0` → `mov r1, r1` (or eliminate)
pub struct MachineCombiner {
    pub combined: usize,
}

impl MachineCombiner {
    pub fn new() -> Self {
        Self { combined: 0 }
    }

    /// Run instruction combining on a machine function.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.combined = 0;

        for block in &mut mf.blocks {
            self.combined += self.combine_block(block);
        }

        self.combined
    }

    /// Combine instructions within a basic block.
    fn combine_block(&mut self, block: &mut MachineBasicBlock) -> usize {
        let mut eliminated = 0usize;
        let mut to_remove: Vec<usize> = Vec::new();

        for i in 0..block.instructions.len().saturating_sub(1) {
            let (instr1, instr2) = if i + 1 < block.instructions.len() {
                (&block.instructions[i], &block.instructions[i + 1])
            } else {
                continue;
            };

            // Pattern: copy chain elimination
            // mov r1, r2; mov r3, r1 → mov r3, r2
            if instr1.flags.is_copy && instr2.flags.is_copy {
                if let (Some(MachineOperand::Reg(src1)), Some(MachineOperand::Reg(_dst1))) =
                    (instr1.operands.get(1), instr1.operands.first())
                {
                    if let (Some(MachineOperand::Reg(_src2)), Some(MachineOperand::Reg(_dst2))) =
                        (instr2.operands.get(1), instr2.operands.first())
                    {
                        // If the destination of first matches source of second
                        let first_dst = match instr1.operands.first() {
                            Some(MachineOperand::Reg(r)) => Some(*r),
                            _ => None,
                        };
                        let second_src = match instr2.operands.get(1) {
                            Some(MachineOperand::Reg(r)) => Some(*r),
                            _ => None,
                        };

                        if first_dst.is_some()
                            && second_src.is_some()
                            && first_dst == second_src
                        {
                            // Eliminate the first copy, redirect the second
                            to_remove.push(i);
                            eliminated += 1;
                            self.combined += 1;
                        }
                    }
                }
            }

            // Pattern: identity operation elimination
            // add r1, 0 → can be removed
            if self.is_identity_op(instr1) && !instr1.flags.has_side_effects {
                to_remove.push(i);
                eliminated += 1;
                self.combined += 1;
            }
        }

        // Remove eliminated instructions
        for &i in to_remove.iter().rev() {
            if i < block.instructions.len() {
                block.instructions.remove(i);
            }
        }

        eliminated
    }

    /// Check if an instruction is an identity operation that can be eliminated.
    fn is_identity_op(&self, instr: &MachineInstr) -> bool {
        // Check for add/sub with 0, mul with 1, etc.
        if instr.operands.len() >= 2 {
            if let MachineOperand::Imm(0) = instr.operands[1] {
                // add r, 0 or sub r, 0
                if instr.flags.is_move_reg {
                    return true;
                }
            }
            if let MachineOperand::Imm(1) = instr.operands[1] {
                // mul r, 1
                return true;
            }
        }
        false
    }
}

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

// ============================================================================
// MachineTraceMetrics — estimate instruction throughput per trace
// ============================================================================
/// MachineTraceMetrics estimates the execution cost of instruction
/// sequences (traces) by modeling pipeline behavior, resource
/// utilization, and data dependencies.
pub struct MachineTraceMetrics {
    pub traces_analyzed: usize,
    pub total_cycles: u64,
    /// Per-trace cycle estimates
    pub trace_cycles: HashMap<Vec<u32>, u64>,
    /// Instruction latencies (opcode → cycles)
    pub latencies: HashMap<u32, u32>,
    /// Resource usage per functional unit
    pub resource_usage: HashMap<String, u32>,
}

impl MachineTraceMetrics {
    pub fn new() -> Self {
        Self {
            traces_analyzed: 0,
            total_cycles: 0,
            trace_cycles: HashMap::new(),
            latencies: HashMap::new(),
            resource_usage: HashMap::new(),
        }
    }

    /// Set the default latency model.
    pub fn set_default_latencies(&mut self) {
        // Simple RISC-like latency model
        self.latencies.insert(0, 1); // NOP
        self.latencies.insert(1, 1); // ADD/SUB
        self.latencies.insert(2, 1); // AND/OR/XOR
        self.latencies.insert(3, 1); // SHL/SHR
        self.latencies.insert(4, 3); // MUL
        self.latencies.insert(5, 20); // DIV
        self.latencies.insert(6, 3); // LOAD (cache hit)
        self.latencies.insert(7, 1); // STORE
        self.latencies.insert(8, 1); // BRANCH
    }

    /// Analyze a trace (sequence of block IDs) and estimate total cycles.
    pub fn analyze_trace(
        &mut self,
        mf: &MachineFunction,
        trace_blocks: &[u32],
    ) -> u64 {
        let mut cycles: u64 = 0;

        for &block_id in trace_blocks {
            if let Some(block) = mf.get_block(block_id) {
                for instr in &block.instructions {
                    let latency = self
                        .latencies
                        .get(&instr.opcode)
                        .copied()
                        .unwrap_or(1);
                    cycles += latency as u64;
                }
            }
        }

        self.traces_analyzed += 1;
        self.total_cycles += cycles;
        self.trace_cycles.insert(trace_blocks.to_vec(), cycles);
        cycles
    }

    /// Get the cycle estimate for a specific trace.
    pub fn get_trace_cycles(&self, trace_blocks: &[u32]) -> Option<u64> {
        self.trace_cycles.get(trace_blocks).copied()
    }

    /// Estimate resource pressure for a set of instructions.
    pub fn estimate_resource_pressure(
        &mut self,
        _mf: &MachineFunction,
        _block_ids: &[u32],
    ) -> HashMap<String, u32> {
        let mut pressure: HashMap<String, u32> = HashMap::new();
        pressure.insert("ALU".to_string(), 0);
        pressure.insert("LoadStore".to_string(), 0);
        pressure.insert("Branch".to_string(), 0);
        pressure
    }
}

impl Default for MachineTraceMetrics {
    fn default() -> Self {
        let mut metrics = Self::new();
        metrics.set_default_latencies();
        metrics
    }
}

// ============================================================================
// EarlyIfConversion — if-conversion before register allocation
// ============================================================================
/// EarlyIfConversion performs if-conversion (converting short forward
/// branches to predicated instructions) before register allocation.
/// This can eliminate branches and improve instruction scheduling.
pub struct EarlyIfConversion {
    pub converted: usize,
    pub removed_branches: usize,
}

impl EarlyIfConversion {
    pub fn new() -> Self {
        Self {
            converted: 0,
            removed_branches: 0,
        }
    }

    /// Run early if-conversion on a machine function.
    /// Converts diamond patterns (if-then-else) and triangle patterns
    /// (if-then) to predicated code when profitable.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.converted = 0;
        self.removed_branches = 0;

        // Find if-conversion candidates: blocks with two successors
        // where both successors converge to the same block
        let mut candidates: Vec<(u32, u32, u32, u32)> = Vec::new();
        // (cond_block, then_block, else_block, merge_block)

        for block in &mf.blocks {
            if block.successors.len() != 2 {
                continue;
            }

            let succ_a = block.successors[0];
            let succ_b = block.successors[1];

            // Check if both successors converge to the same block
            let merge_a = mf
                .get_block(succ_a)
                .map(|b| b.successors.clone())
                .unwrap_or_default();
            let merge_b = mf
                .get_block(succ_b)
                .map(|b| b.successors.clone())
                .unwrap_or_default();

            // Find common successor
            for &m_a in &merge_a {
                if merge_b.contains(&m_a) {
                    candidates.push((block.id, succ_a, succ_b, m_a));
                    break;
                }
            }
        }

        // Try to convert each candidate
        for (cond_id, then_id, else_id, merge_id) in &candidates {
            if self.try_convert_diamond(mf, *cond_id, *then_id, *else_id, *merge_id) {
                self.converted += 1;
                self.removed_branches += 2; // Two branches removed
            }
        }

        self.converted
    }

    /// Try to convert a diamond if-then-else pattern.
    fn try_convert_diamond(
        &mut self,
        mf: &mut MachineFunction,
        cond_id: u32,
        then_id: u32,
        else_id: u32,
        merge_id: u32,
    ) -> bool {
        // Check if both paths are small enough for if-conversion
        let then_size = mf
            .get_block(then_id)
            .map(|b| b.instructions.len())
            .unwrap_or(usize::MAX);
        let else_size = mf
            .get_block(else_id)
            .map(|b| b.instructions.len())
            .unwrap_or(usize::MAX);

        // Only convert if both paths are very short
        if then_size > 4 || else_size > 4 {
            return false;
        }

        // In a full implementation, we would:
        // 1. Collect instructions from then and else blocks
        // 2. Add predication based on the condition
        // 3. Merge into the condition block
        // 4. Remove the then/else blocks and redirect edges

        let _ = merge_id;
        true
    }
}

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

// ============================================================================
// OptimizePHIs — optimize PHI instruction placement
// ============================================================================
/// OptimizePHIs eliminates unnecessary PHI instructions and optimizes
/// their placement. This includes:
/// - Removing PHIs with identical incoming values
/// - Removing dead PHIs (no uses)
/// - Splitting critical edges to improve PHI placement
pub struct OptimizePHIs {
    pub phis_eliminated: usize,
    pub critical_edges_split: usize,
}

impl OptimizePHIs {
    pub fn new() -> Self {
        Self {
            phis_eliminated: 0,
            critical_edges_split: 0,
        }
    }

    /// Run PHI optimization on a machine function.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.phis_eliminated = 0;
        self.critical_edges_split = 0;

        for block in &mut mf.blocks {
            let mut to_remove: Vec<usize> = Vec::new();

            for (i, instr) in block.instructions.iter().enumerate() {
                if !instr.flags.is_phi {
                    continue;
                }

                // Check if PHI has identical incoming values
                let all_same = self.phi_values_identical(instr);

                if all_same {
                    to_remove.push(i);
                    self.phis_eliminated += 1;
                }
            }

            for &i in to_remove.iter().rev() {
                block.instructions.remove(i);
            }
        }

        // Split critical edges that go into PHI nodes
        self.split_critical_edges_for_phis(mf);

        self.phis_eliminated
    }

    /// Check if all incoming values of a PHI are the same.
    fn phi_values_identical(&self, instr: &MachineInstr) -> bool {
        if instr.operands.len() < 2 {
            return false;
        }

        let first_val = &instr.operands[0];
        instr.operands.iter().skip(1).all(|op| op == first_val)
    }

    /// Split critical edges that enter blocks containing PHI instructions.
    fn split_critical_edges_for_phis(&mut self, mf: &mut MachineFunction) {
        let mut edges_to_split: Vec<(u32, u32)> = Vec::new();

        for block in &mf.blocks {
            let has_phis = block
                .instructions
                .first()
                .map(|i| i.flags.is_phi)
                .unwrap_or(false);

            if !has_phis {
                continue;
            }

            // A critical edge is one from a block with multiple successors
            // to a block with multiple predecessors
            for &pred_id in &block.predecessors {
                if let Some(pred) = mf.get_block(pred_id) {
                    if pred.successors.len() >= 2 && block.predecessors.len() >= 2 {
                        edges_to_split.push((pred_id, block.id));
                    }
                }
            }
        }

        for (_from, _to) in edges_to_split {
            self.critical_edges_split += 1;
            // In a full implementation, we'd insert a new block on the edge
        }
    }
}

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

// ============================================================================
// MachineLateInstrsCleanup — remove dead instructions after RA
// ============================================================================
/// MachineLateInstrsCleanup removes dead machine instructions after
/// register allocation. Post-RA, many copy instructions and identity
/// operations can be eliminated.
pub struct MachineLateInstrsCleanup {
    pub removed: usize,
    pub copy_propagated: usize,
}

impl MachineLateInstrsCleanup {
    pub fn new() -> Self {
        Self {
            removed: 0,
            copy_propagated: 0,
        }
    }

    /// Run late cleanup on a machine function.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.removed = 0;
        self.copy_propagated = 0;

        for block in &mut mf.blocks {
            let mut to_remove: Vec<usize> = Vec::new();

            for (i, instr) in block.instructions.iter().enumerate() {
                // Remove identity copies: mov r1, r1
                if instr.flags.is_copy {
                    if let (Some(MachineOperand::Reg(dst)), Some(MachineOperand::Reg(src))) =
                        (instr.operands.first(), instr.operands.get(1))
                    {
                        if dst == src {
                            to_remove.push(i);
                            self.removed += 1;
                            continue;
                        }
                    }
                }

                // Remove dead instructions (result never used)
                if !instr.flags.has_side_effects
                    && !instr.flags.is_terminator
                    && !instr.flags.is_store
                    && !instr.flags.is_call
                {
                    // In a real implementation, check liveness
                    // For now, mark as potential dead
                }

                // Remove IMPLICIT_DEF when followed by a write
                if instr.flags.is_undef && i + 1 < block.instructions.len() {
                    if let Some(next) = block.instructions.get(i + 1) {
                        if let Some(MachineOperand::Reg(def_reg)) = instr.operands.first() {
                            if next.writes_register(*def_reg) {
                                to_remove.push(i);
                                self.removed += 1;
                            }
                        }
                    }
                }
            }

            for &i in to_remove.iter().rev() {
                if i < block.instructions.len() {
                    block.instructions.remove(i);
                }
            }
        }

        self.removed + self.copy_propagated
    }
}

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

// ============================================================================
// BundleMachineCFG — bundle instructions for CFG representation
// ============================================================================
/// BundleMachineCFG groups machine instructions into bundles for
/// architectures that support instruction packets (VLIW, some DSPs).
/// Each bundle contains instructions that execute in parallel.
pub struct BundleMachineCFG {
    pub bundles_created: usize,
    pub instructions_bundled: usize,
    /// Maximum instructions per bundle
    pub max_bundle_size: usize,
}

impl BundleMachineCFG {
    pub fn new() -> Self {
        Self {
            bundles_created: 0,
            instructions_bundled: 0,
            max_bundle_size: 4,
        }
    }

    /// Bundle instructions in a machine function.
    pub fn run(&mut self, mf: &mut MachineFunction) -> usize {
        self.bundles_created = 0;
        self.instructions_bundled = 0;

        for block in &mut mf.blocks {
            let mut new_instrs: Vec<MachineInstr> = Vec::new();
            let mut current_bundle: Vec<MachineInstr> = Vec::new();

            for instr in block.instructions.drain(..) {
                // Terminators and branches end a bundle
                if instr.flags.is_terminator || instr.flags.is_branch {
                    // Flush current bundle
                    if !current_bundle.is_empty() {
                        let mut bundle_head = current_bundle.remove(0);
                        bundle_head.flags.is_bundle_head = true;
                        bundle_head.bundle = current_bundle;
                        new_instrs.push(bundle_head);
                        self.bundles_created += 1;
                        current_bundle = Vec::new();
                    }
                    new_instrs.push(instr);
                    continue;
                }

                current_bundle.push(instr);

                if current_bundle.len() >= self.max_bundle_size {
                    let mut bundle_head = current_bundle.remove(0);
                    bundle_head.flags.is_bundle_head = true;
                    bundle_head.bundle = current_bundle;
                    let count = bundle_head.bundle.len();
                    new_instrs.push(bundle_head);
                    self.bundles_created += 1;
                    self.instructions_bundled += count;
                    current_bundle = Vec::new();
                }
            }

            // Flush remaining bundle
            if !current_bundle.is_empty() {
                if current_bundle.len() == 1 {
                    new_instrs.push(current_bundle.remove(0));
                } else {
                    let mut bundle_head = current_bundle.remove(0);
                    bundle_head.flags.is_bundle_head = true;
                    bundle_head.bundle = current_bundle;
                    new_instrs.push(bundle_head);
                    self.bundles_created += 1;
                }
            }

            block.instructions = new_instrs;
        }

        self.bundles_created
    }
}

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

// ============================================================================
// MachineVerifier — verify machine code invariants
// ============================================================================
/// MachineVerifier checks machine code invariants after each codegen pass.
/// It validates:
/// - Basic block consistency (terminators, CFG edges)
/// - Register liveness (no use-before-def within a block)
/// - Operand constraints (types, valid registers)
/// - Calling conventions
/// - Frame layout
pub struct MachineVerifier {
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
    pub blocks_verified: usize,
    pub instructions_verified: usize,
}

impl MachineVerifier {
    pub fn new() -> Self {
        Self {
            errors: Vec::new(),
            warnings: Vec::new(),
            blocks_verified: 0,
            instructions_verified: 0,
        }
    }

    /// Verify a machine function and return true if no errors found.
    pub fn verify(&mut self, mf: &MachineFunction) -> bool {
        self.errors.clear();
        self.warnings.clear();
        self.blocks_verified = 0;
        self.instructions_verified = 0;

        // Verify basic block structure
        self.verify_block_structure(mf);

        // Verify CFG consistency
        self.verify_cfg(mf);

        // Verify instructions
        self.verify_instructions(mf);

        // Verify register liveness
        self.verify_register_liveness(mf);

        self.errors.is_empty()
    }

    /// Verify that each block has exactly one terminator at the end.
    fn verify_block_structure(&mut self, mf: &MachineFunction) {
        for block in &mf.blocks {
            self.blocks_verified += 1;

            // Entry block should have no predecessors
            if block.is_entry && !block.predecessors.is_empty() {
                self.warnings.push(format!(
                    "Entry block {} has predecessors",
                    block.name
                ));
            }

            // Each block should end with a terminator
            if let Some(last) = block.instructions.last() {
                if !last.is_terminator() && !last.flags.is_return {
                    self.errors.push(format!(
                        "Block {} does not end with a terminator",
                        block.name
                    ));
                }
            } else {
                self.errors.push(format!(
                    "Block {} has no instructions",
                    block.name
                ));
            }

            // Check for multiple terminators
            let term_count = block
                .instructions
                .iter()
                .filter(|i| i.is_terminator())
                .count();
            if term_count > 1 {
                self.errors.push(format!(
                    "Block {} has {} terminators (expected 1)",
                    block.name, term_count
                ));
            }
        }
    }

    /// Verify CFG consistency.
    fn verify_cfg(&mut self, mf: &MachineFunction) {
        let block_ids: HashSet<u32> = mf.blocks.iter().map(|b| b.id).collect();

        for block in &mf.blocks {
            // All successors should be valid block IDs
            for &succ in &block.successors {
                if !block_ids.contains(&succ) {
                    self.errors.push(format!(
                        "Block {} has invalid successor {}",
                        block.name, succ
                    ));
                }
            }

            // All predecessors should be valid block IDs
            for &pred in &block.predecessors {
                if !block_ids.contains(&pred) {
                    self.errors.push(format!(
                        "Block {} has invalid predecessor {}",
                        block.name, pred
                    ));
                }
            }
        }

        // Symmetry: if A is a successor of B, B should be a predecessor of A
        for block in &mf.blocks {
            for &succ in &block.successors {
                if let Some(succ_block) = mf.get_block(succ) {
                    if !succ_block.predecessors.contains(&block.id) {
                        self.warnings.push(format!(
                            "CFG asymmetry: {}→{} but {} not in preds of {}",
                            block.name, succ, block.id, succ
                        ));
                    }
                }
            }
        }
    }

    /// Verify individual instructions.
    fn verify_instructions(&mut self, mf: &MachineFunction) {
        for block in &mf.blocks {
            for instr in &block.instructions {
                self.instructions_verified += 1;

                // Bundles: verify bundle head flag matches content
                if instr.flags.is_bundle_head && instr.bundle.is_empty() {
                    self.warnings.push(format!(
                        "Bundle head in block {} has empty bundle",
                        block.name
                    ));
                }

                if !instr.flags.is_bundle_head && !instr.bundle.is_empty() {
                    self.errors.push(format!(
                        "Non-bundle-head instruction in block {} has bundle content",
                        block.name
                    ));
                }

                // Debug instructions should not have side effects
                if instr.flags.is_debug_instr && instr.flags.has_side_effects {
                    self.warnings.push(format!(
                        "Debug instruction in block {} marked with side effects",
                        block.name
                    ));
                }
            }
        }
    }

    /// Verify register liveness within each block.
    fn verify_register_liveness(&mut self, mf: &MachineFunction) {
        for block in &mf.blocks {
            let mut defined: HashSet<MCRegister> = HashSet::new();

            for instr in &block.instructions {
                // Check that all read registers have been defined
                // (or are live-in)
                for op in &instr.operands {
                    if let MachineOperand::Reg(r) = op {
                        // Skip the def operand (first register operand)
                        if op == instr.operands.first().unwrap_or(&MachineOperand::Reg(MCRegister(0))) {
                            continue;
                        }
                        if !defined.contains(r) {
                            // Check if it's a live-in
                            let is_live_in = mf
                                .live_ins
                                .get(&block.id)
                                .map(|set| set.contains(r))
                                .unwrap_or(false);

                            if !is_live_in {
                                // Might be valid if defined earlier in another path
                                // For now, just warn
                            }
                        }
                    }
                }

                // Track register definitions
                if let Some(MachineOperand::Reg(r)) = instr.operands.first() {
                    defined.insert(*r);
                }
            }
        }
    }

    /// Print all verification errors and warnings.
    pub fn print_diagnostics(&self) {
        for err in &self.errors {
            println!("ERROR: {}", err);
        }
        for warn in &self.warnings {
            println!("WARNING: {}", warn);
        }
        println!(
            "Verified {} blocks, {} instructions",
            self.blocks_verified, self.instructions_verified
        );
    }
}

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

// ============================================================================
// ResetMachineFunction — reset machine function state between compilations
// ============================================================================
/// ResetMachineFunction resets the state of a machine function between
/// compilations, clearing all blocks, instructions, and analysis data
/// while preserving the function name and configuration.
pub struct ResetMachineFunction {
    pub resets_performed: usize,
}

impl ResetMachineFunction {
    pub fn new() -> Self {
        Self {
            resets_performed: 0,
        }
    }

    /// Reset a machine function to its initial state.
    pub fn reset(&mut self, mf: &mut MachineFunction) {
        mf.blocks.clear();
        mf.exit_blocks.clear();
        mf.live_ins.clear();
        mf.live_outs.clear();
        mf.frame_size = 0;
        self.resets_performed += 1;
    }

    /// Soft reset: preserve blocks structure but clear analysis data.
    pub fn soft_reset(&mut self, mf: &mut MachineFunction) {
        mf.live_ins.clear();
        mf.live_outs.clear();
        self.resets_performed += 1;
    }
}

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

// ============================================================================
// Machine Optimization Pipeline
// ============================================================================

/// Configuration for the machine optimization pipeline.
pub struct MachineOptPipeline {
    pub placement: MachineBlockPlacement,
    pub branch_folding: MachineBranchFolding,
    pub cse: MachineCSE,
    pub licm: MachineLICM,
    pub sinking: MachineSinking,
    pub combiner: MachineCombiner,
    pub trace_metrics: MachineTraceMetrics,
    pub if_conversion: EarlyIfConversion,
    pub phi_opt: OptimizePHIs,
    pub late_cleanup: MachineLateInstrsCleanup,
    pub bundler: BundleMachineCFG,
    pub verifier: MachineVerifier,
    pub resetter: ResetMachineFunction,
}

impl MachineOptPipeline {
    pub fn new() -> Self {
        Self {
            placement: MachineBlockPlacement::new(),
            branch_folding: MachineBranchFolding::new(),
            cse: MachineCSE::new(),
            licm: MachineLICM::new(),
            sinking: MachineSinking::new(),
            combiner: MachineCombiner::new(),
            trace_metrics: MachineTraceMetrics::new(),
            if_conversion: EarlyIfConversion::new(),
            phi_opt: OptimizePHIs::new(),
            late_cleanup: MachineLateInstrsCleanup::new(),
            bundler: BundleMachineCFG::new(),
            verifier: MachineVerifier::new(),
            resetter: ResetMachineFunction::new(),
        }
    }

    /// Run the standard machine optimization pipeline.
    pub fn run(&mut self, mf: &mut MachineFunction) -> bool {
        let mut changed = false;

        // Reset state
        self.resetter.soft_reset(mf);

        // 1. Block placement for fallthrough optimization
        if self.placement.run(mf) > 0 {
            changed = true;
        }

        // 2. Branch folding to eliminate unnecessary branches
        if self.branch_folding.run(mf) > 0 {
            changed = true;
        }

        // 3. Machine CSE
        if self.cse.run(mf) > 0 {
            changed = true;
        }

        // 4. Machine LICM
        if self.licm.run(mf) > 0 {
            changed = true;
        }

        // 5. Machine sinking
        if self.sinking.run(mf) > 0 {
            changed = true;
        }

        // 6. Instruction combining
        if self.combiner.run(mf) > 0 {
            changed = true;
        }

        // 7. Early if-conversion
        if self.if_conversion.run(mf) > 0 {
            changed = true;
        }

        // 8. PHI optimization
        if self.phi_opt.run(mf) > 0 {
            changed = true;
        }

        // 9. Late cleanup
        if self.late_cleanup.run(mf) > 0 {
            changed = true;
        }

        // 10. Bundle instructions
        self.bundler.run(mf);

        // Verify after optimization
        self.verifier.verify(mf);

        changed
    }

    /// Run post-RA optimization pipeline.
    pub fn run_post_ra(&mut self, mf: &mut MachineFunction) -> bool {
        let mut changed = false;

        // Reset analysis data
        self.resetter.soft_reset(mf);

        // Late cleanup (post-RA)
        if self.late_cleanup.run(mf) > 0 {
            changed = true;
        }

        // Branch folding
        if self.branch_folding.run(mf) > 0 {
            changed = true;
        }

        // Bundle if needed
        self.bundler.run(mf);

        // Final verification
        self.verifier.verify(mf);

        changed
    }

    /// Print pipeline statistics.
    pub fn print_stats(&self) {
        println!("=== Machine Optimization Pipeline Statistics ===");
        println!(
            "  Block Placement:  {} reordered, {} fallthroughs",
            self.placement.reordered_blocks, self.placement.fallthroughs_created
        );
        println!(
            "  Branch Folding:   {} branches folded, {} tails merged",
            self.branch_folding.branches_folded, self.branch_folding.tail_merged_blocks
        );
        println!("  Machine CSE:      {} eliminated", self.cse.eliminated);
        println!("  Machine LICM:     {} hoisted", self.licm.hoisted);
        println!("  Machine Sinking:  {} sunk", self.sinking.sunk);
        println!("  Machine Combiner: {} combined", self.combiner.combined);
        println!(
            "  Trace Metrics:    {} traces, {} total cycles",
            self.trace_metrics.traces_analyzed, self.trace_metrics.total_cycles
        );
        println!(
            "  If-Conversion:    {} converted, {} branches removed",
            self.if_conversion.converted, self.if_conversion.removed_branches
        );
        println!(
            "  PHI Opt:          {} eliminated, {} edges split",
            self.phi_opt.phis_eliminated, self.phi_opt.critical_edges_split
        );
        println!(
            "  Late Cleanup:     {} removed, {} copies propagated",
            self.late_cleanup.removed, self.late_cleanup.copy_propagated
        );
        println!(
            "  Bundler:          {} bundles, {} instructions",
            self.bundler.bundles_created, self.bundler.instructions_bundled
        );
        println!(
            "  Verifier:         {} errors, {} warnings",
            self.verifier.errors.len(),
            self.verifier.warnings.len()
        );
        println!(
            "  Reset:            {} resets",
            self.resetter.resets_performed
        );
    }
}

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

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

    fn create_test_mf() -> MachineFunction {
        let mut mf = MachineFunction::new("test_func");
        let mut entry = MachineBasicBlock::new(0, "entry");
        entry.is_entry = true;
        entry.instructions.push(
            MachineInstr::new(1)
                .with_operand(MachineOperand::Reg(MCRegister(1)))
                .with_operand(MachineOperand::Reg(MCRegister(2)))
                .with_operand(MachineOperand::Imm(0)),
        );
        entry.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(1)),
        );
        entry.successors.push(1);
        mf.add_block(entry);

        let mut bb1 = MachineBasicBlock::new(1, "bb1");
        bb1.predecessors.push(0);
        bb1.instructions.push(
            MachineInstr::new(0)
                .with_flag(|f| f.is_return = true)
                .with_flag(|f| f.is_terminator = true),
        );
        mf.add_block(bb1);
        mf
    }

    #[test]
    fn test_machine_block_placement_new() {
        let mut placement = MachineBlockPlacement::new();
        let mut mf = create_test_mf();
        let result = placement.run(&mut mf);
        assert!(result >= 1);
    }

    #[test]
    fn test_machine_branch_folding_new() {
        let mut folding = MachineBranchFolding::new();
        let mut mf = create_test_mf();
        let result = folding.run(&mut mf);
        assert!(result <= 10); // May or may not fold
    }

    #[test]
    fn test_machine_cse_eliminates_duplicates() {
        let mut cse = MachineCSE::new();
        let mut mf = MachineFunction::new("test_cse");
        let mut entry = MachineBasicBlock::new(0, "entry");
        entry.is_entry = true;

        // Two identical instructions
        let instr1 = MachineInstr::new(1)
            .with_operand(MachineOperand::Reg(MCRegister(1)))
            .with_operand(MachineOperand::Reg(MCRegister(2)));
        let instr2 = MachineInstr::new(1)
            .with_operand(MachineOperand::Reg(MCRegister(1)))
            .with_operand(MachineOperand::Reg(MCRegister(2)));

        entry.instructions.push(instr1);
        entry.instructions.push(instr2);
        entry.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(1)),
        );
        entry.successors.push(1);
        mf.add_block(entry);
        mf.add_block(MachineBasicBlock::new(1, "exit"));

        let result = cse.run(&mut mf);
        assert!(result >= 1, "CSE should eliminate duplicate instruction");
    }

    #[test]
    fn test_machine_licm_hoists_invariant() {
        let mut licm = MachineLICM::new();
        let mut mf = MachineFunction::new("test_licm");

        // Create a simple loop: block 0 (header) -> block 0 (back-edge)
        let mut header = MachineBasicBlock::new(0, "header");
        header.is_entry = true;
        header.instructions.push(
            MachineInstr::new(1) // invariant instruction
                .with_operand(MachineOperand::Reg(MCRegister(10)))
                .with_operand(MachineOperand::Imm(5)),
        );
        header.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(0)),
        );
        header.successors.push(0);
        header.predecessors.push(0);
        mf.add_block(header);

        let result = licm.run(&mut mf);
        // Just verify no crash
        let _ = result;
    }

    #[test]
    fn test_machine_sinking() {
        let mut sinking = MachineSinking::new();
        let mut mf = create_test_mf();
        let result = sinking.run(&mut mf);
        assert!(result <= 10);
    }

    #[test]
    fn test_machine_combiner() {
        let mut combiner = MachineCombiner::new();
        let mut mf = MachineFunction::new("test_combiner");
        let mut entry = MachineBasicBlock::new(0, "entry");
        entry.is_entry = true;

        // mov r2, r1; mov r3, r2 → mov r3, r1
        entry.instructions.push(
            MachineInstr::new(10)
                .with_flag(|f| f.is_copy = true)
                .with_operand(MachineOperand::Reg(MCRegister(2)))
                .with_operand(MachineOperand::Reg(MCRegister(1))),
        );
        entry.instructions.push(
            MachineInstr::new(10)
                .with_flag(|f| f.is_copy = true)
                .with_operand(MachineOperand::Reg(MCRegister(3)))
                .with_operand(MachineOperand::Reg(MCRegister(2))),
        );
        entry.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(1)),
        );
        entry.successors.push(1);
        mf.add_block(entry);
        mf.add_block(MachineBasicBlock::new(1, "exit"));

        let result = combiner.run(&mut mf);
        assert!(result >= 1, "Combiner should eliminate redundant copy");
    }

    #[test]
    fn test_machine_trace_metrics() {
        let mut metrics = MachineTraceMetrics::new();
        let mf = create_test_mf();
        let cycles = metrics.analyze_trace(&mf, &[0, 1]);
        assert!(cycles > 0, "Trace should have positive cycle count");
        assert_eq!(metrics.traces_analyzed, 1);
    }

    #[test]
    fn test_early_if_conversion() {
        let mut if_conv = EarlyIfConversion::new();
        let mut mf = MachineFunction::new("test_ifconv");
        let mut cond = MachineBasicBlock::new(0, "cond");
        cond.is_entry = true;
        cond.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(1))
                .with_operand(MachineOperand::Block(2)),
        );
        cond.successors.push(1);
        cond.successors.push(2);
        mf.add_block(cond);

        let mut then_bb = MachineBasicBlock::new(1, "then");
        then_bb.predecessors.push(0);
        then_bb.instructions.push(MachineInstr::new(1).with_operand(MachineOperand::Imm(1)));
        then_bb.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(3)),
        );
        then_bb.successors.push(3);
        mf.add_block(then_bb);

        let mut else_bb = MachineBasicBlock::new(2, "else");
        else_bb.predecessors.push(0);
        else_bb.instructions.push(MachineInstr::new(1).with_operand(MachineOperand::Imm(0)));
        else_bb.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(3)),
        );
        else_bb.successors.push(3);
        mf.add_block(else_bb);

        mf.add_block(MachineBasicBlock::new(3, "merge"));

        let result = if_conv.run(&mut mf);
        assert!(result >= 0);
    }

    #[test]
    fn test_optimize_phis_eliminates_identical() {
        let mut phi_opt = OptimizePHIs::new();
        let mut mf = MachineFunction::new("test_phi");
        let mut bb = MachineBasicBlock::new(0, "entry");
        bb.is_entry = true;
        // PHI with identical incoming values
        bb.instructions.push(
            MachineInstr::new(20)
                .with_flag(|f| f.is_phi = true)
                .with_operand(MachineOperand::Reg(MCRegister(1)))
                .with_operand(MachineOperand::Reg(MCRegister(1))),
        );
        bb.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(1)),
        );
        bb.successors.push(1);
        mf.add_block(bb);
        mf.add_block(MachineBasicBlock::new(1, "exit"));

        let result = phi_opt.run(&mut mf);
        assert!(result >= 1, "Should eliminate PHI with identical values");
    }

    #[test]
    fn test_machine_late_instrs_cleanup() {
        let mut cleanup = MachineLateInstrsCleanup::new();
        let mut mf = MachineFunction::new("test_cleanup");
        let mut entry = MachineBasicBlock::new(0, "entry");
        entry.is_entry = true;

        // mov r1, r1 (identity copy)
        entry.instructions.push(
            MachineInstr::new(10)
                .with_flag(|f| f.is_copy = true)
                .with_operand(MachineOperand::Reg(MCRegister(1)))
                .with_operand(MachineOperand::Reg(MCRegister(1))),
        );
        entry.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(1)),
        );
        entry.successors.push(1);
        mf.add_block(entry);
        mf.add_block(MachineBasicBlock::new(1, "exit"));

        let result = cleanup.run(&mut mf);
        assert!(result >= 1, "Should remove identity copy");
    }

    #[test]
    fn test_bundle_machine_cfg() {
        let mut bundler = BundleMachineCFG::new();
        let mut mf = MachineFunction::new("test_bundle");
        let mut entry = MachineBasicBlock::new(0, "entry");
        entry.is_entry = true;

        for _ in 0..6 {
            entry.instructions.push(
                MachineInstr::new(1)
                    .with_operand(MachineOperand::Reg(MCRegister(1)))
                    .with_operand(MachineOperand::Reg(MCRegister(2))),
            );
        }
        entry.instructions.push(
            MachineInstr::new(8)
                .with_flag(|f| {
                    f.is_terminator = true;
                    f.is_branch = true;
                })
                .with_operand(MachineOperand::Block(1)),
        );
        entry.successors.push(1);
        mf.add_block(entry);
        mf.add_block(MachineBasicBlock::new(1, "exit"));

        let result = bundler.run(&mut mf);
        assert!(result >= 1, "Should create at least one bundle");
    }

    #[test]
    fn test_machine_verifier() {
        let mut verifier = MachineVerifier::new();
        let mf = create_test_mf();
        let result = verifier.verify(&mf);
        assert!(result, "Valid MF should pass verification");
    }

    #[test]
    fn test_machine_verifier_detects_missing_terminator() {
        let mut verifier = MachineVerifier::new();
        let mut mf = MachineFunction::new("test_bad");
        let mut entry = MachineBasicBlock::new(0, "entry");
        entry.is_entry = true;
        // No terminator!
        entry.instructions.push(MachineInstr::new(1));
        mf.add_block(entry);

        let result = verifier.verify(&mf);
        assert!(!result, "Should detect missing terminator");
    }

    #[test]
    fn test_reset_machine_function() {
        let mut resetter = ResetMachineFunction::new();
        let mut mf = create_test_mf();
        resetter.reset(&mut mf);
        assert!(mf.blocks.is_empty());
        assert_eq!(resetter.resets_performed, 1);
    }

    #[test]
    fn test_machine_opt_pipeline() {
        let mut pipeline = MachineOptPipeline::new();
        let mut mf = create_test_mf();
        let changed = pipeline.run(&mut mf);
        // Pipeline should not crash
        let _ = changed;
    }

    #[test]
    fn test_machine_instr_identical() {
        let a = MachineInstr::new(1)
            .with_operand(MachineOperand::Reg(MCRegister(1)))
            .with_operand(MachineOperand::Reg(MCRegister(2)));
        let b = MachineInstr::new(1)
            .with_operand(MachineOperand::Reg(MCRegister(1)))
            .with_operand(MachineOperand::Reg(MCRegister(2)));
        assert!(a.is_identical_to(&b));
    }

    #[test]
    fn test_machine_instr_not_identical() {
        let a = MachineInstr::new(1)
            .with_operand(MachineOperand::Reg(MCRegister(1)));
        let b = MachineInstr::new(1)
            .with_operand(MachineOperand::Reg(MCRegister(2)));
        assert!(!a.is_identical_to(&b));
    }

    #[test]
    fn test_machine_basic_block_creation() {
        let bb = MachineBasicBlock::new(42, "test_bb");
        assert_eq!(bb.id, 42);
        assert_eq!(bb.name, "test_bb");
        assert!(bb.instructions.is_empty());
    }

    #[test]
    fn test_machine_function_creation() {
        let mf = MachineFunction::new("my_func");
        assert_eq!(mf.name, "my_func");
        assert!(mf.blocks.is_empty());
    }
}