analyssa 0.4.1

Target-agnostic SSA IR, analyses, and optimization pipeline
Documentation
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
//! Checked SSA editing utilities for optimization passes.
//!
//! This module provides [`SsaEditor`], a lightweight mutation facade for
//! [`SsaFunction`]. The editor is designed for pass bodies
//! that want local safety checks while avoiding verifier or repair work after
//! every individual edit.
//!
//! # Design
//!
//! Editing follows a two-stage model:
//!
//! 1. Individual editor operations perform cheap local checks before mutating
//!    the function.
//! 2. [`SsaFunction::edit`] performs one repair or rebuild step at the edit
//!    boundary, followed by optional validation.
//!
//! This keeps common optimization passes fast while moving correctness checks
//! such as type compatibility, self-reference prevention, and same-block
//! use-before-def prevention into shared infrastructure.

use std::collections::BTreeMap;

use crate::{
    BitSet,
    analysis::{
        cfg::SsaCfg,
        verifier::{SsaVerifier, VerifierError, VerifyLevel},
    },
    error::{Error, Result},
    graph::{
        NodeId, RootedGraph,
        algorithms::{DominatorTree, compute_dominators},
    },
    ir::{
        block::{ReplaceResult, SsaBlock},
        function::{CopyPropagationResult, SsaFunction},
        instruction::SsaInstruction,
        ops::SsaOp,
        phi::{PhiNode, PhiOperand},
        variable::{DefSite, SsaVarId, UseSite, VariableOrigin},
    },
    target::Target,
};

/// Required cleanup after a sequence of SSA edits.
///
/// Scopes are ordered from cheapest to most invasive. [`SsaEditor`] widens the
/// current scope automatically as edits are performed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum SsaEditScope {
    /// No mutation has occurred.
    None,
    /// Only instruction operands were changed.
    ///
    /// Finishing this scope recomputes use-site metadata but does not strip
    /// instructions or compact variables.
    UsesOnly,
    /// Instructions were replaced, nopped, inserted, or removed without
    /// changing the control-flow graph.
    ///
    /// Finishing this scope runs [`SsaFunction::repair_ssa`].
    InstructionsOnly,
    /// The control-flow graph may have changed.
    ///
    /// Finishing this scope runs [`SsaFunction::rebuild_ssa`].
    ///
    /// This subsumes what was previously a separate `StructuredCfg` scope for
    /// "the CFG changed but the caller maintained phi edges". That variant ran
    /// `rebuild_ssa` too, so it was behaviourally identical while its
    /// documentation promised a cheaper boundary — an edge edit can invalidate
    /// dominance, liveness, and definition placement regardless of whether phi
    /// operands were kept consistent, so there was nothing cheaper to do.
    CfgModifying,
}

/// Rollback behavior for [`SsaFunction::edit`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SsaRollbackPolicy {
    /// Never clone the original function and never roll back automatically.
    Never,
    /// Clone the original function and restore it if the edit, repair, rebuild,
    /// or optional verifier step fails.
    OnFailure,
}

/// Options controlling a checked SSA edit session.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SsaEditOptions {
    /// Whether to run the verifier after boundary repair.
    pub verify: bool,
    /// Which invariants boundary verification checks.
    ///
    /// Defaults to [`VerifyLevel::Full`]. The cheaper [`VerifyLevel::Standard`]
    /// omits the dominance check, and dominance is exactly what a mis-scoped
    /// edit breaks: `check_defined_before_use` only asks whether a definition
    /// *exists*, so IR with a use above its own definition passes. A caller that
    /// has already opted into verification is unlikely to want the one check
    /// that catches the most common way an edit goes wrong; opt down explicitly
    /// with [`Self::with_verify_level`] where the O(n²) dominance pass is too
    /// expensive.
    pub verify_level: VerifyLevel,
    /// Whether to restore the original function when the edit fails.
    pub rollback: SsaRollbackPolicy,
}

impl SsaEditOptions {
    /// Creates edit options with no verification and no rollback.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            verify: false,
            verify_level: VerifyLevel::Full,
            rollback: SsaRollbackPolicy::Never,
        }
    }

    /// Sets which invariants boundary verification checks.
    ///
    /// Only consulted when verification is enabled via [`Self::with_verify`].
    #[must_use]
    pub const fn with_verify_level(mut self, level: VerifyLevel) -> Self {
        self.verify_level = level;
        self
    }

    /// Enables or disables boundary verification.
    #[must_use]
    pub const fn with_verify(mut self, verify: bool) -> Self {
        self.verify = verify;
        self
    }

    /// Sets rollback behavior for the edit session.
    #[must_use]
    pub const fn with_rollback(mut self, rollback: SsaRollbackPolicy) -> Self {
        self.rollback = rollback;
        self
    }
}

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

/// Summary returned after a checked SSA edit session finishes successfully.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SsaEditReport {
    /// Whether any editor operation changed the function.
    pub changed: bool,
    /// The strongest cleanup scope required by the operations that ran.
    pub scope: SsaEditScope,
}

/// Reason a checked replacement was skipped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReplacementSkipReason {
    /// The old variable and replacement variable are identical.
    SameVariable,
    /// The variable being replaced is not registered in the function.
    MissingOldVariable,
    /// The replacement variable is not registered in the function.
    MissingNewVariable,
    /// The variable types are incompatible.
    TypeMismatch,
    /// The requested instruction does not use the variable being replaced.
    UseNotFound,
    /// The replacement would make an instruction read its own destination.
    SelfReference,
    /// The replacement source is defined later than the use in the same block.
    SameBlockUseBeforeDef,
    /// The replacement source does not dominate the instruction use.
    DominanceViolation,
    /// The requested use is a phi operand and must use phi-specific APIs.
    PhiOperand,
    /// The requested block or instruction index does not exist.
    MissingInstruction,
}

/// A skipped checked replacement at a concrete use site.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkippedReplacement {
    /// The use site where replacement was not performed.
    pub site: UseSite,
    /// The reason replacement was not performed.
    pub reason: ReplacementSkipReason,
}

/// Result of a checked replacement operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CheckedReplaceResult {
    /// Number of operand occurrences replaced.
    pub replaced: usize,
    /// Use sites skipped by the shared safety checks.
    pub skipped: Vec<SkippedReplacement>,
}

impl CheckedReplaceResult {
    /// Returns `true` if no replacements were skipped.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.skipped.is_empty()
    }

    /// Converts this result into the legacy aggregate replacement result.
    #[must_use]
    pub fn as_replace_result(&self) -> ReplaceResult {
        ReplaceResult {
            replaced: self.replaced,
            skipped: self.skipped.len(),
        }
    }
}

/// Checked mutation facade for an [`SsaFunction`].
///
/// The editor performs local checks before mutating and records the strongest
/// repair scope required by the performed edits. It does not repair or verify
/// after each operation; [`SsaFunction::edit`] performs that boundary work once.
pub struct SsaEditor<'a, T: Target> {
    /// Function being edited.
    ssa: &'a mut SsaFunction<T>,
    /// Whether any operation changed the function.
    changed: bool,
    /// Strongest repair scope required so far.
    scope: SsaEditScope,
    /// Whether every variable's use list is currently exact.
    ///
    /// Cleared by any mutation that does not maintain them, and re-established
    /// by a single `recompute_uses` before the first indexed replacement. This
    /// is what lets [`Self::replace_uses_checked_with`] consult the use index
    /// instead of scanning the function on every call.
    uses_indexed: bool,
}

impl<T: Target> SsaFunction<T> {
    /// Runs a checked edit session and repairs the function once at the end.
    ///
    /// The closure receives an [`SsaEditor`] that exposes checked mutation
    /// helpers. If the closure succeeds and changed the function, this method
    /// applies the repair required by the editor's accumulated
    /// [`SsaEditScope`]. Optional verification runs after repair.
    ///
    /// # Errors
    ///
    /// Returns an error if the edit closure fails, SSA rebuild fails, or
    /// verification is enabled and detects malformed SSA. If
    /// [`SsaRollbackPolicy::OnFailure`] is selected, the original function is
    /// restored before returning the error.
    pub fn edit<F>(&mut self, options: SsaEditOptions, edit: F) -> Result<SsaEditReport>
    where
        F: FnOnce(&mut SsaEditor<T>) -> Result<()>,
    {
        let original = if matches!(options.rollback, SsaRollbackPolicy::OnFailure) {
            Some(self.clone())
        } else {
            None
        };

        let edit_result = {
            let mut editor = SsaEditor {
                ssa: self,
                changed: false,
                scope: SsaEditScope::None,
                // Pessimistic: a caller may have mutated the function directly
                // before opening this session, so the first indexed
                // replacement pays one `recompute_uses` to establish the index.
                uses_indexed: false,
            };
            let result = edit(&mut editor);
            result.map(|_| SsaEditReport {
                changed: editor.changed,
                scope: editor.scope,
            })
        };

        let report = match edit_result {
            Ok(report) => report,
            Err(error) => {
                restore_on_failure(self, original);
                return Err(error);
            }
        };

        if report.changed
            && let Err(error) = finish_edit_scope(self, report.scope)
        {
            restore_on_failure(self, original);
            return Err(error);
        }

        if options.verify {
            let errors = SsaVerifier::new(self).verify(options.verify_level);
            if !errors.is_empty() {
                restore_on_failure(self, original);
                return Err(Error::new(format_verifier_errors(&errors)));
            }
        }

        Ok(report)
    }

    /// Replaces instruction uses of `old_var` with `new_var` after shared checks.
    ///
    /// This method mutates instruction operands directly and does not run repair
    /// or recompute use metadata. Callers that use it outside [`SsaFunction::edit`]
    /// should call [`SsaFunction::recompute_uses`] or the appropriate repair
    /// method before relying on def-use metadata.
    #[must_use]
    pub fn replace_uses_checked(
        &mut self,
        old_var: SsaVarId,
        new_var: SsaVarId,
    ) -> CheckedReplaceResult {
        let needs_dominance = self
            .variable(new_var)
            .map(|new| {
                let def_block = new.def_site().block;
                self.blocks().iter().enumerate().any(|(block_idx, block)| {
                    block_idx != def_block
                        && block
                            .instructions()
                            .iter()
                            .any(|instr| instr.op().uses_var(old_var))
                })
            })
            .unwrap_or(false);
        let dominators = if needs_dominance && self.block_count() > 0 {
            let cfg = SsaCfg::from_ssa(self);
            Some(compute_dominators(&cfg, cfg.entry()))
        } else {
            None
        };

        self.replace_uses_checked_with(old_var, new_var, dominators.as_ref())
    }

    /// Performs a checked use replacement against a caller-supplied dominator
    /// tree, skipping the per-call CFG/dominator construction.
    ///
    /// Replacing instruction uses never changes any terminator, so a single
    /// dominator tree stays valid across a batch of replacements. Callers that
    /// rewrite many copies (e.g. copy propagation) should build the tree once
    /// and reuse it via this method instead of calling
    /// [`replace_uses_checked`](Self::replace_uses_checked) per pair.
    #[must_use]
    /// Checked use replacement driven by `old_var`'s recorded use sites rather
    /// than a scan of the whole function.
    ///
    /// Same result as [`Self::replace_uses_checked_with`], at O(uses of
    /// `old_var`) instead of O(function). The scanning form is called once per
    /// redundant pair by GVN and once per copy by copy propagation, which makes
    /// those passes O(pairs x function) — the dominant cost on large functions.
    ///
    /// # Correctness
    ///
    /// Requires `old_var`'s use list to be current; [`SsaEditor`] guarantees
    /// that by recomputing once before its first indexed replacement and then
    /// keeping the lists exact, which this method does by moving each rewritten
    /// site from `old_var` to `new_var`.
    pub(in crate::ir::function) fn replace_uses_checked_indexed(
        &mut self,
        old_var: SsaVarId,
        new_var: SsaVarId,
        dominators: Option<&DominatorTree>,
    ) -> CheckedReplaceResult {
        // Instruction uses only, mirroring the scanning form; phi operands are
        // handled by the phi-specific paths.
        let candidates: Vec<UseSite> = self
            .variable(old_var)
            .map(|var| {
                var.uses()
                    .iter()
                    .filter(|site| !site.is_phi_operand)
                    .copied()
                    .collect()
            })
            .unwrap_or_default();

        let mut allowed = Vec::new();
        let mut skipped = Vec::new();
        for site in candidates {
            // The list can name a site whose op no longer reads `old_var` (a
            // previous rewrite in this session); skip those rather than trust it
            // blindly.
            let still_uses = self
                .block(site.block)
                .and_then(|block| block.instruction(site.instruction))
                .is_some_and(|instr| instr.op().uses_var(old_var));
            if !still_uses {
                continue;
            }
            match can_replace_instruction_use_with_dominators(
                self,
                old_var,
                new_var,
                site.block,
                site.instruction,
                dominators,
            ) {
                Ok(()) => allowed.push(site),
                Err(reason) => skipped.push(SkippedReplacement { site, reason }),
            }
        }

        let mut replaced = 0usize;
        let mut moved: Vec<UseSite> = Vec::new();
        for site in allowed {
            if let Some(instr) = self
                .block_mut(site.block)
                .and_then(|block| block.instruction_mut(site.instruction))
            {
                let n = instr.op_mut().replace_uses(old_var, new_var);
                if n > 0 {
                    replaced = replaced.saturating_add(n);
                    moved.push(site);
                }
            }
        }

        // Keep both use lists exact so the next indexed replacement stays valid.
        if !moved.is_empty() {
            if let Some(old) = self.variable_mut(old_var) {
                old.retain_uses(|site| {
                    !moved
                        .iter()
                        .any(|m| m.block == site.block && m.instruction == site.instruction)
                });
            }
            if let Some(new) = self.variable_mut(new_var) {
                for site in moved {
                    new.add_use(site);
                }
            }
        }

        CheckedReplaceResult { replaced, skipped }
    }

    pub(in crate::ir::function) fn replace_uses_checked_with(
        &mut self,
        old_var: SsaVarId,
        new_var: SsaVarId,
        dominators: Option<&DominatorTree>,
    ) -> CheckedReplaceResult {
        let mut allowed = Vec::new();
        let mut skipped = Vec::new();

        for (block_idx, block) in self.blocks().iter().enumerate() {
            for (instr_idx, instr) in block.instructions().iter().enumerate() {
                if !instr.op().uses_var(old_var) {
                    continue;
                }

                match can_replace_instruction_use_with_dominators(
                    self, old_var, new_var, block_idx, instr_idx, dominators,
                ) {
                    Ok(()) => allowed.push((block_idx, instr_idx)),
                    Err(reason) => skipped.push(SkippedReplacement {
                        site: UseSite::instruction(block_idx, instr_idx),
                        reason,
                    }),
                }
            }
        }

        let mut replaced = 0usize;
        for (block_idx, instr_idx) in allowed {
            if let Some(instr) = self
                .block_mut(block_idx)
                .and_then(|block| block.instruction_mut(instr_idx))
            {
                replaced = replaced.saturating_add(instr.op_mut().replace_uses(old_var, new_var));
            }
        }

        CheckedReplaceResult { replaced, skipped }
    }

    /// Checks whether one instruction use can be replaced safely.
    ///
    /// This method is intentionally cheap enough for optimization passes. It
    /// validates variable existence, type compatibility, self-reference, same
    /// block ordering, and cross-block dominance for instruction uses.
    ///
    /// # Errors
    ///
    /// Returns a [`ReplacementSkipReason`] describing why the replacement would
    /// be unsafe or inapplicable.
    pub fn can_replace_instruction_use(
        &self,
        old_var: SsaVarId,
        new_var: SsaVarId,
        block_idx: usize,
        instr_idx: usize,
    ) -> std::result::Result<(), ReplacementSkipReason> {
        let needs_dominance = self
            .variable(new_var)
            .is_some_and(|new| new.def_site().block != block_idx);
        let dominators = if needs_dominance && self.block_count() > 0 {
            let cfg = SsaCfg::from_ssa(self);
            Some(compute_dominators(&cfg, cfg.entry()))
        } else {
            None
        };
        can_replace_instruction_use_with_dominators(
            self,
            old_var,
            new_var,
            block_idx,
            instr_idx,
            dominators.as_ref(),
        )
    }
}

impl<'a, T: Target> SsaEditor<'a, T> {
    /// Returns an immutable view of the function being edited.
    #[must_use]
    pub const fn function(&self) -> &SsaFunction<T> {
        self.ssa
    }

    /// Returns whether this edit session has changed the function.
    #[must_use]
    pub const fn changed(&self) -> bool {
        self.changed
    }

    /// Returns the strongest repair scope required so far.
    #[must_use]
    pub const fn scope(&self) -> SsaEditScope {
        self.scope
    }

    /// Replaces instruction uses after shared safety checks.
    ///
    /// The edit scope is widened to [`SsaEditScope::UsesOnly`] when at least
    /// one operand occurrence is replaced.
    pub fn replace_uses_checked(
        &mut self,
        old_var: SsaVarId,
        new_var: SsaVarId,
    ) -> CheckedReplaceResult {
        let result = self.ssa.replace_uses_checked(old_var, new_var);
        if result.replaced > 0 {
            self.mark_changed(SsaEditScope::UsesOnly);
        }
        result
    }

    /// Replaces instruction uses of `old_var` with `new_var` against a
    /// caller-supplied dominator tree, skipping the per-call CFG + dominator
    /// construction that [`replace_uses_checked`](Self::replace_uses_checked)
    /// performs internally.
    ///
    /// Replacing instruction uses never rewrites a terminator, so a single
    /// dominator tree stays valid across an entire batch of replacements. A
    /// pass that rewrites many variables (e.g. GVN) should build the tree once
    /// from [`function`](Self::function) and reuse it here, turning a per-pair
    /// O(blocks) dominator rebuild into one build for the whole batch.
    ///
    /// The edit scope is widened to [`SsaEditScope::UsesOnly`] when at least one
    /// operand occurrence is replaced.
    pub fn replace_uses_checked_with(
        &mut self,
        old_var: SsaVarId,
        new_var: SsaVarId,
        dominators: Option<&DominatorTree>,
    ) -> CheckedReplaceResult {
        // One `recompute_uses` per edit session, then O(uses) per replacement.
        // The scanning form is O(function) *per call*, and callers such as GVN
        // and copy propagation invoke this once per candidate pair.
        if !self.uses_indexed {
            self.ssa.recompute_uses();
            self.uses_indexed = true;
        }
        let result = self
            .ssa
            .replace_uses_checked_indexed(old_var, new_var, dominators);
        if result.replaced > 0 {
            self.mark_changed(SsaEditScope::UsesOnly);
            // `replace_uses_checked_indexed` moved the rewritten sites between
            // the two variables, so the lists are still exact.
            self.uses_indexed = true;
        }
        result
    }

    /// Propagates copy mappings through instruction operands.
    ///
    /// The edit scope is widened to [`SsaEditScope::UsesOnly`] when at least
    /// one operand occurrence is replaced.
    pub fn propagate_copies(
        &mut self,
        copies: &BTreeMap<SsaVarId, SsaVarId>,
    ) -> CopyPropagationResult {
        let result = self.ssa.propagate_copies(copies);
        if result.total_replaced > 0 {
            self.mark_changed(SsaEditScope::UsesOnly);
        }
        result
    }

    /// Replaces the copy instruction defining `dest` with [`SsaOp::Nop`].
    ///
    /// Boundary repair strips the nop and compacts variables once the edit
    /// session finishes.
    pub fn nop_copy_defining(&mut self, dest: SsaVarId) -> bool {
        let changed = self.ssa.nop_copy_defining(dest);
        if changed {
            self.mark_changed(SsaEditScope::InstructionsOnly);
        }
        changed
    }

    /// Replaces an instruction operation and marks instruction repair required.
    ///
    /// If the new operation defines a variable, the variable's definition site
    /// is updated to the instruction location. The final edit boundary repair
    /// handles stale use metadata and any variables orphaned by the old
    /// operation.
    ///
    /// # Errors
    ///
    /// Returns an error if the target instruction does not exist.
    pub fn replace_instruction_op(
        &mut self,
        block_idx: usize,
        instr_idx: usize,
        new_op: SsaOp<T>,
    ) -> Result<()> {
        let defs: Vec<SsaVarId> = new_op.defs().collect();
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };
        let Some(instr) = block.instruction_mut(instr_idx) else {
            return Err(Error::new(format!(
                "missing instruction B{block_idx}:I{instr_idx}"
            )));
        };

        instr.set_op_preserving_type(new_op);
        for dest in defs {
            if let Some(var) = self.ssa.variable_mut(dest) {
                var.set_def_site(DefSite::instruction(block_idx, instr_idx));
            }
        }
        self.mark_changed(SsaEditScope::InstructionsOnly);
        Ok(())
    }

    /// Replaces an instruction with [`SsaOp::Nop`].
    ///
    /// This is the checked editor equivalent of instruction removal. Boundary
    /// repair strips the nop and compacts variables once the edit session
    /// finishes.
    ///
    /// # Errors
    ///
    /// Returns an error if the target instruction does not exist.
    pub fn nop_instruction(&mut self, block_idx: usize, instr_idx: usize) -> Result<()> {
        self.replace_instruction_op(block_idx, instr_idx, SsaOp::Nop)
    }

    /// Replaces the terminator operation for a block.
    ///
    /// The terminator is represented by the block's last instruction. This
    /// helper marks the edit as [`SsaEditScope::CfgModifying`] because changing
    /// a terminator can change successor edges even when the instruction count
    /// stays fixed.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist or has no instructions.
    pub fn replace_terminator_op(&mut self, block_idx: usize, new_op: SsaOp<T>) -> Result<()> {
        let Some(block) = self.ssa.block(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };
        let Some(instr_idx) = block.instructions().len().checked_sub(1) else {
            return Err(Error::new(format!("block B{block_idx} has no terminator")));
        };

        self.replace_instruction_op(block_idx, instr_idx, new_op)?;
        self.mark_changed(SsaEditScope::CfgModifying);
        Ok(())
    }

    /// Removes all instructions in a block starting at `start_idx`.
    ///
    /// This is intended for dead-tail cleanup after a terminator. The function
    /// truncates the instruction list and marks instruction repair required so
    /// shifted or removed definition sites are reconciled at the edit boundary.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn remove_instruction_tail(&mut self, block_idx: usize, start_idx: usize) -> Result<usize> {
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        let original_len = block.instructions().len();
        if start_idx >= original_len {
            return Ok(0);
        }
        block.instructions_mut().truncate(start_idx);
        let removed = original_len.saturating_sub(start_idx);
        if removed > 0 {
            self.mark_changed(SsaEditScope::InstructionsOnly);
        }
        Ok(removed)
    }

    /// Clears all phis and instructions from a block.
    ///
    /// Clearing a block changes CFG structure by removing its terminator and
    /// definitions, so boundary cleanup uses full SSA rebuild.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn clear_block(&mut self, block_idx: usize) -> Result<bool> {
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        if block.is_empty() {
            return Ok(false);
        }

        block.clear();
        self.mark_changed(SsaEditScope::CfgModifying);
        Ok(true)
    }

    /// Folds a block's terminator to a new op and repairs the phi operands of
    /// any successor whose incoming edge that removes.
    ///
    /// This is the cheap path for branch folding — replacing
    /// `Branch(cond, A, B)` with `Jump(A)`. Such an edit is *not* a general CFG
    /// mutation and does not need [`SsaFunction::rebuild_ssa`]:
    ///
    /// - Removing an edge only ever **increases** dominance, because it removes
    ///   paths. Every definition that dominated a use still does, so no
    ///   def-use violation can be introduced and **no new phi can ever be
    ///   required** — existing phis can only become redundant.
    /// - The one thing that *does* need fixing is each dropped successor's phi
    ///   operands for this predecessor, which this method prunes directly.
    ///
    /// The edit therefore settles at [`SsaEditScope::InstructionsOnly`]
    /// (`repair_ssa`) rather than escalating the whole function to a 19-phase
    /// Cytron reconstruction. Measured, `rebuild_ssa` costs 23-54x a full IR
    /// clone, so avoiding one is worth more than eliminating every per-pass
    /// snapshot in the same sweep.
    ///
    /// Returns the number of phi operands pruned.
    ///
    /// # Errors
    ///
    /// Returns an error if the block or its terminator does not exist, or if
    /// `new_op` would **add** a successor edge — the reasoning above holds only
    /// for edge removal, so an edge-adding fold is rejected rather than
    /// under-repaired.
    pub fn fold_terminator_pruning_phis(
        &mut self,
        block_idx: usize,
        new_op: SsaOp<T>,
    ) -> Result<usize> {
        let Some(block) = self.ssa.block(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };
        let Some(last_idx) = block.instructions().len().checked_sub(1) else {
            return Err(Error::new(format!("block B{block_idx} has no terminator")));
        };

        let before: Vec<usize> = block.successors();
        let after: Vec<usize> = new_op.successors();

        // The whole justification for staying at `InstructionsOnly` is that this
        // edit only *removes* edges: removing paths can only increase dominance,
        // so no new phi can ever be required. An edit that adds an edge breaks
        // that argument — it can make a definition stop dominating a use, and it
        // can require a phi that does not exist — so it is rejected here rather
        // than silently under-repaired. Use
        // [`SsaEditor::redirect_terminator_target`] or
        // [`SsaEditor::replace_terminator_op`] for the general case; both record
        // [`SsaEditScope::CfgModifying`].
        if let Some(added) = after.iter().find(|target| !before.contains(target)) {
            return Err(Error::new(format!(
                "fold_terminator_pruning_phis on B{block_idx} would add edge to B{added}; \
                 this path only prunes removed edges — use `replace_terminator_op` \
                 for an edit that adds one"
            )));
        }

        self.replace_instruction_op(block_idx, last_idx, new_op)?;

        // Prune only the edges this fold actually removed. A successor that
        // still has another edge from this block (e.g. `Branch(c, A, A)`)
        // keeps its operand.
        let mut pruned = 0usize;
        for successor in before {
            if after.contains(&successor) {
                continue;
            }
            if let Some(target) = self.ssa.block_mut(successor) {
                for phi in target.phi_nodes_mut() {
                    let operands = phi.operands_mut();
                    let original = operands.len();
                    // Never leave a phi empty; a single-operand phi is handled
                    // by trivial-phi simplification, not here.
                    if operands
                        .iter()
                        .filter(|op| op.predecessor() != block_idx)
                        .count()
                        == 0
                    {
                        continue;
                    }
                    operands.retain(|op| op.predecessor() != block_idx);
                    pruned = pruned.saturating_add(original.saturating_sub(operands.len()));
                }
            }
        }

        self.mark_changed(SsaEditScope::InstructionsOnly);
        Ok(pruned)
    }

    /// Redirects one successor edge in a block terminator.
    ///
    /// The block's last instruction is treated as its terminator. If that
    /// operation references `old_target`, all matching target fields are
    /// rewritten to `new_target` through [`SsaOp::redirect_target`].
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist or has no instructions.
    pub fn redirect_terminator_target(
        &mut self,
        block_idx: usize,
        old_target: usize,
        new_target: usize,
    ) -> Result<bool> {
        let Some(mut op) = self
            .ssa
            .block(block_idx)
            .and_then(|block| block.instructions().last())
            .map(|instr| instr.op().clone())
        else {
            return Err(Error::new(format!("block B{block_idx} has no terminator")));
        };

        if !op.redirect_target(old_target, new_target) {
            return Ok(false);
        }

        self.replace_terminator_op(block_idx, op)?;
        Ok(true)
    }

    /// Redirects one terminator target, for a caller that also fixes up the
    /// affected successor phi operands in the same edit session.
    ///
    /// Behaviourally identical to [`SsaEditor::redirect_terminator_target`] —
    /// both record [`SsaEditScope::CfgModifying`], because an edge edit
    /// invalidates dominance and definition placement whether or not phi
    /// operands were kept consistent. Retained as a distinct name so call sites
    /// that *do* maintain phis stay self-documenting.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist or has no instructions.
    pub fn redirect_terminator_target_structured(
        &mut self,
        block_idx: usize,
        old_target: usize,
        new_target: usize,
    ) -> Result<bool> {
        let Some(mut op) = self
            .ssa
            .block(block_idx)
            .and_then(|block| block.instructions().last())
            .map(|instr| instr.op().clone())
        else {
            return Err(Error::new(format!("block B{block_idx} has no terminator")));
        };

        if !op.redirect_target(old_target, new_target) {
            return Ok(false);
        }

        let instr_idx = self
            .ssa
            .block(block_idx)
            .and_then(|block| block.instructions().len().checked_sub(1))
            .ok_or_else(|| Error::new(format!("block B{block_idx} has no terminator")))?;
        self.replace_instruction_op(block_idx, instr_idx, op)?;
        self.mark_changed(SsaEditScope::CfgModifying);
        Ok(true)
    }

    /// Replaces one predecessor in phi operands with one or more predecessors.
    ///
    /// For each phi in `block_idx`, the first operand whose predecessor is
    /// `old_pred` is rewritten to the first value in `new_preds`. Additional
    /// predecessors receive duplicate operands carrying the same value. This
    /// preserves phi value attribution when multiple incoming edges are
    /// redirected through a single removed block.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn expand_phi_predecessor(
        &mut self,
        block_idx: usize,
        old_pred: usize,
        new_preds: &[usize],
    ) -> Result<usize> {
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };
        let Some((&first_pred, extra_preds)) = new_preds.split_first() else {
            return Ok(0);
        };

        let mut updated = 0usize;
        for phi in block.phi_nodes_mut() {
            let Some(value) = phi
                .operands()
                .iter()
                .find(|operand| operand.predecessor() == old_pred)
                .map(|operand| operand.value())
            else {
                continue;
            };

            if let Some(operand) = phi
                .operands_mut()
                .iter_mut()
                .find(|operand| operand.predecessor() == old_pred)
            {
                operand.set_predecessor(first_pred);
                updated = updated.saturating_add(1);
            }

            for &pred in extra_preds {
                phi.add_operand(PhiOperand::new(value, pred));
                updated = updated.saturating_add(1);
            }
        }

        if updated > 0 {
            self.mark_changed(SsaEditScope::CfgModifying);
        }
        Ok(updated)
    }

    /// Rewrites phi operands that name one predecessor to another predecessor.
    ///
    /// This is useful after block coalescing, where successor phis that pointed
    /// at the removed block must instead point at the block that absorbed it.
    ///
    /// When the phi already carries an operand for `new_pred`, retargeting would
    /// leave two operands on one edge — which has no defined meaning, and which
    /// consumers disagree about: [`PhiNode::operand_from`] returns the first and
    /// discards the rest, while SCCP meets them all and yields Bottom. Both
    /// edges merging into one carry the same value only if their operands agree;
    /// when they do, the redundant operand is dropped, and when they do not the
    /// merge is not semantics-preserving and is rejected.
    ///
    /// [`PhiNode::operand_from`]: crate::ir::phi::PhiNode::operand_from
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist, or if the two edges being
    /// merged carry different values for the same phi.
    pub fn replace_phi_predecessor(
        &mut self,
        block_idx: usize,
        old_pred: usize,
        new_pred: usize,
    ) -> Result<usize> {
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        let mut updated = 0usize;
        for (phi_idx, phi) in block.phi_nodes_mut().iter_mut().enumerate() {
            let value_on = |pred: usize| {
                phi.operands()
                    .iter()
                    .find(|operand| operand.predecessor() == pred)
                    .map(PhiOperand::value)
            };
            match (value_on(new_pred), value_on(old_pred)) {
                (Some(existing), Some(retargeted)) if existing != retargeted => {
                    return Err(Error::new(format!(
                        "B{block_idx} phi {phi_idx}: cannot redirect B{old_pred} to B{new_pred},                          the edges carry different values ({retargeted} and {existing})"
                    )));
                }
                (Some(_), Some(_)) => {
                    // Both edges already agree, so the retargeted operand is
                    // redundant rather than a second value for the edge.
                    phi.operands_mut()
                        .retain(|operand| operand.predecessor() != old_pred);
                    updated = updated.saturating_add(1);
                }
                (None, Some(_)) => {
                    for operand in phi.operands_mut() {
                        if operand.predecessor() == old_pred {
                            operand.set_predecessor(new_pred);
                            updated = updated.saturating_add(1);
                        }
                    }
                }
                (_, None) => {}
            }
        }

        if updated > 0 {
            self.mark_changed(SsaEditScope::CfgModifying);
        }
        Ok(updated)
    }

    /// Coalesces a block into its unconditional predecessor.
    ///
    /// `predecessor_idx` must end with `Jump { target: successor_idx }`.
    /// The predecessor's jump is removed, successor phis are converted into
    /// `Copy` instructions, successor instructions are appended, self-targets
    /// inside the moved instructions are redirected to the predecessor, and
    /// the successor block is cleared. Phi operands in other blocks that
    /// referenced the successor are rewritten to the predecessor.
    ///
    /// # Errors
    ///
    /// Returns an error if either block is missing, or if the predecessor does
    /// not end in an unconditional jump to the successor.
    pub fn coalesce_unconditional_successor(
        &mut self,
        predecessor_idx: usize,
        successor_idx: usize,
    ) -> Result<bool> {
        let Some(predecessor) = self.ssa.block(predecessor_idx) else {
            return Err(Error::new(format!("missing block B{predecessor_idx}")));
        };
        if !matches!(
            predecessor.terminator_op(),
            Some(SsaOp::Jump { target }) if *target == successor_idx
        ) {
            return Err(Error::new(format!(
                "block B{predecessor_idx} does not jump to B{successor_idx}"
            )));
        }

        let Some(successor) = self.ssa.block(successor_idx) else {
            return Err(Error::new(format!("missing block B{successor_idx}")));
        };
        if successor.instructions().is_empty() {
            return Ok(false);
        }

        let phi_copies: Vec<SsaInstruction<T>> = successor
            .phi_nodes()
            .iter()
            .filter_map(|phi| {
                let operand = phi.operands().first()?;
                let dest = phi.result();
                let src = operand.value();
                if dest == src {
                    return None;
                }
                Some(SsaInstruction::synthetic(SsaOp::Copy { dest, src }))
            })
            .collect();
        let successor_instrs = successor.instructions().to_vec();

        let Some(predecessor) = self.ssa.block_mut(predecessor_idx) else {
            return Err(Error::new(format!("missing block B{predecessor_idx}")));
        };
        let instrs = predecessor.instructions_mut();
        instrs.pop();
        instrs.extend(phi_copies);
        instrs.extend(successor_instrs);
        for instr in instrs {
            instr
                .op_mut()
                .redirect_target(successor_idx, predecessor_idx);
        }

        self.clear_block(successor_idx)?;

        let block_count = self.ssa.block_count();
        for block_idx in 0..block_count {
            if block_idx == predecessor_idx || block_idx == successor_idx {
                continue;
            }
            self.replace_phi_predecessor(block_idx, successor_idx, predecessor_idx)?;
        }

        self.mark_changed(SsaEditScope::CfgModifying);
        Ok(true)
    }

    /// Appends a basic block to the function.
    ///
    /// The block id must match the next dense block index. This keeps block
    /// creation explicit and avoids silently introducing an inconsistent block
    /// id that would later require canonicalization to interpret.
    ///
    /// # Errors
    ///
    /// Returns an error if the block id is not equal to the current block
    /// count.
    pub fn append_block(&mut self, block: SsaBlock<T>) -> Result<usize> {
        let block_idx = self.ssa.block_count();
        if block.id() != block_idx {
            return Err(Error::new(format!(
                "new block id B{} does not match next block B{block_idx}",
                block.id()
            )));
        }

        self.ssa.add_block(block);
        self.mark_changed(SsaEditScope::CfgModifying);
        Ok(block_idx)
    }

    /// Creates a variable using the function's registered origin type.
    ///
    /// This is the editor-facing equivalent of
    /// [`SsaFunction::create_variable_for_origin`]. Creating a variable does
    /// not by itself require repair, because the caller must still insert the
    /// corresponding definition before the edit boundary.
    #[must_use]
    pub fn create_variable_for_origin(
        &mut self,
        origin: VariableOrigin,
        version: u32,
        def_site: DefSite,
    ) -> SsaVarId {
        self.ssa
            .create_variable_for_origin(origin, version, def_site)
    }

    /// Appends a phi node to a block.
    ///
    /// If the phi result already exists in the function variable table, its
    /// definition site is updated to the destination block. Boundary repair
    /// refreshes uses and removes any stale definitions if later edits discard
    /// the phi.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn add_phi(&mut self, block_idx: usize, phi: PhiNode) -> Result<usize> {
        let result = phi.result();
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        let phi_idx = block.phi_nodes().len();
        block.phi_nodes_mut().push(phi);
        if let Some(var) = self.ssa.variable_mut(result) {
            var.set_def_site(DefSite::phi(block_idx));
        }
        self.mark_changed(SsaEditScope::CfgModifying);
        Ok(phi_idx)
    }

    /// Replaces phi operands from a predecessor group with one operand.
    ///
    /// Every operand whose predecessor appears in `old_preds` is removed. If
    /// at least one operand was removed, `new_operand` is appended. This models
    /// canonical CFG insertion such as replacing several incoming header edges
    /// with a single preheader or unified latch edge.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn replace_phi_predecessor_group(
        &mut self,
        block_idx: usize,
        old_preds: &[usize],
        new_operand: PhiOperand,
    ) -> Result<bool> {
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        let mut changed = false;
        for phi in block.phi_nodes_mut() {
            let before = phi.operands().len();
            phi.operands_mut()
                .retain(|operand| !old_preds.contains(&operand.predecessor()));
            if phi.operands().len() != before {
                phi.add_operand(new_operand);
                changed = true;
            }
        }

        if changed {
            self.mark_changed(SsaEditScope::CfgModifying);
        }
        Ok(changed)
    }

    /// Replaces one origin's phi operands from a predecessor group.
    ///
    /// Only the phi node whose [`PhiNode::origin`] equals `origin` is updated.
    /// This lets loop canonicalization rewrite each header phi to the value
    /// produced by the matching inserted preheader or latch phi.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn replace_phi_predecessor_group_for_origin(
        &mut self,
        block_idx: usize,
        origin: VariableOrigin,
        old_preds: &[usize],
        new_operand: PhiOperand,
    ) -> Result<bool> {
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        let mut changed = false;
        for phi in block.phi_nodes_mut() {
            if phi.origin() != origin {
                continue;
            }
            let before = phi.operands().len();
            phi.operands_mut()
                .retain(|operand| !old_preds.contains(&operand.predecessor()));
            if phi.operands().len() != before {
                phi.add_operand(new_operand);
                changed = true;
            }
        }

        if changed {
            self.mark_changed(SsaEditScope::CfgModifying);
        }
        Ok(changed)
    }

    /// Prunes phi operands that refer to unreachable predecessors.
    ///
    /// This is the editor-facing wrapper around
    /// [`SsaFunction::prune_phi_operands`]. It marks the edit as
    /// [`SsaEditScope::UsesOnly`] when operands are removed, so the boundary
    /// refreshes use metadata without simplifying phis before the calling pass
    /// can account for them.
    #[must_use]
    pub fn prune_phi_operands(&mut self, reachable: &BitSet) -> usize {
        let pruned = self.ssa.prune_phi_operands(reachable);
        if pruned > 0 {
            self.mark_changed(SsaEditScope::UsesOnly);
        }
        pruned
    }

    /// Simplifies a phi node by replacing its result with a source variable.
    ///
    /// This is intended for phi nodes already proven trivial by analysis.
    /// Instruction uses are replaced through dominance-aware checks, and the
    /// phi is removed only when no remaining uses of its result exist outside
    /// the phi being removed.
    ///
    /// # Errors
    ///
    /// Returns an error if the block or phi node does not exist.
    pub fn simplify_phi_to_copy(
        &mut self,
        block_idx: usize,
        phi_idx: usize,
        source: SsaVarId,
    ) -> Result<()> {
        if self
            .ssa
            .block(block_idx)
            .and_then(|block| block.phi_nodes().get(phi_idx))
            .is_none()
        {
            return Err(Error::new(format!("missing phi B{block_idx}:P{phi_idx}")));
        }

        if self.ssa.simplify_phi_to_copy(block_idx, phi_idx, source) {
            self.mark_changed(SsaEditScope::InstructionsOnly);
            Ok(())
        } else {
            Err(Error::new(format!(
                "failed to simplify phi B{block_idx}:P{phi_idx}"
            )))
        }
    }

    /// Inserts an instruction before the block terminator.
    ///
    /// If the block has no terminator, the instruction is appended. If the
    /// inserted instruction defines a variable, its definition site is updated
    /// to the inserted location.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn insert_before_terminator(
        &mut self,
        block_idx: usize,
        instr: SsaInstruction<T>,
    ) -> Result<usize> {
        let defs: Vec<SsaVarId> = instr.defs().collect();
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        let insert_idx = block
            .instructions()
            .iter()
            .position(SsaInstruction::is_terminator)
            .unwrap_or_else(|| block.instructions().len());
        block.instructions_mut().insert(insert_idx, instr);

        for dest in defs {
            if let Some(var) = self.ssa.variable_mut(dest) {
                var.set_def_site(DefSite::instruction(block_idx, insert_idx));
            }
        }
        self.mark_changed(SsaEditScope::InstructionsOnly);
        Ok(insert_idx)
    }

    /// Inserts an instruction at a specific index within a block.
    ///
    /// The index is clamped to the end of the instruction list, so callers can
    /// use a large index to append. If the inserted instruction defines a
    /// variable, that variable's definition site is updated to the insertion
    /// location. Boundary repair fixes definition sites for later instructions
    /// shifted by the insertion.
    ///
    /// # Errors
    ///
    /// Returns an error if the block does not exist.
    pub fn insert_instruction(
        &mut self,
        block_idx: usize,
        instr_idx: usize,
        instr: SsaInstruction<T>,
    ) -> Result<usize> {
        let defs: Vec<SsaVarId> = instr.defs().collect();
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };

        let insert_idx = instr_idx.min(block.instructions().len());
        block.instructions_mut().insert(insert_idx, instr);

        for dest in defs {
            if let Some(var) = self.ssa.variable_mut(dest) {
                var.set_def_site(DefSite::instruction(block_idx, insert_idx));
            }
        }
        self.mark_changed(SsaEditScope::InstructionsOnly);
        Ok(insert_idx)
    }

    /// Removes a phi node at a specific block-local index.
    ///
    /// Boundary repair removes any orphaned phi result variables and refreshes
    /// def-use metadata. This helper intentionally does not replace uses of the
    /// phi result; callers must insert or otherwise provide a replacement
    /// definition when uses remain.
    ///
    /// # Errors
    ///
    /// Returns an error if the block or phi index does not exist.
    pub fn remove_phi(&mut self, block_idx: usize, phi_idx: usize) -> Result<()> {
        let Some(block) = self.ssa.block_mut(block_idx) else {
            return Err(Error::new(format!("missing block B{block_idx}")));
        };
        if phi_idx >= block.phi_nodes().len() {
            return Err(Error::new(format!("missing phi B{block_idx}:P{phi_idx}")));
        }

        block.phi_nodes_mut().remove(phi_idx);
        self.mark_changed(SsaEditScope::InstructionsOnly);
        Ok(())
    }

    /// Marks that the edit session changed the control-flow graph.
    ///
    /// Low-level CFG transformations can call this after performing block or
    /// edge edits through existing APIs. The edit boundary will run
    /// [`SsaFunction::rebuild_ssa`].
    pub fn mark_cfg_changed(&mut self) {
        self.mark_changed(SsaEditScope::CfgModifying);
    }

    /// Widens the accumulated edit scope and records that a mutation occurred.
    fn mark_changed(&mut self, scope: SsaEditScope) {
        self.changed = true;
        self.scope = self.scope.max(scope);
        // Conservative: only the replacement path below knows how to keep the
        // use lists exact, and it re-sets this immediately afterwards.
        self.uses_indexed = false;
    }
}

/// Restores the original function when a rollback snapshot exists.
fn restore_on_failure<T: Target>(ssa: &mut SsaFunction<T>, original: Option<SsaFunction<T>>) {
    if let Some(original) = original {
        *ssa = original;
    }
}

/// Applies boundary cleanup for an edit scope.
fn finish_edit_scope<T: Target>(ssa: &mut SsaFunction<T>, scope: SsaEditScope) -> Result<()> {
    match scope {
        SsaEditScope::None => {}
        SsaEditScope::UsesOnly => ssa.recompute_uses(),
        SsaEditScope::InstructionsOnly => ssa.repair_ssa(),
        SsaEditScope::CfgModifying => ssa.rebuild_ssa()?,
    }
    Ok(())
}

/// Formats verifier errors for an edit-session failure.
fn format_verifier_errors(errors: &[VerifierError]) -> String {
    let details = errors
        .iter()
        .map(ToString::to_string)
        .collect::<Vec<_>>()
        .join("; ");
    format!("checked SSA edit produced invalid SSA: {details}")
}

/// Returns whether two SSA types can be treated as compatible for replacement.
fn types_compatible<T: Target>(old_type: &T::Type, new_type: &T::Type) -> bool {
    old_type == new_type || T::is_unknown(old_type) || T::is_unknown(new_type)
}

/// Checks one instruction replacement using optional precomputed dominators.
fn can_replace_instruction_use_with_dominators<T: Target>(
    ssa: &SsaFunction<T>,
    old_var: SsaVarId,
    new_var: SsaVarId,
    block_idx: usize,
    instr_idx: usize,
    dominators: Option<&DominatorTree>,
) -> std::result::Result<(), ReplacementSkipReason> {
    if old_var == new_var {
        return Err(ReplacementSkipReason::SameVariable);
    }

    let Some(old) = ssa.variable(old_var) else {
        return Err(ReplacementSkipReason::MissingOldVariable);
    };
    let Some(new) = ssa.variable(new_var) else {
        return Err(ReplacementSkipReason::MissingNewVariable);
    };

    if !types_compatible::<T>(old.var_type(), new.var_type()) {
        return Err(ReplacementSkipReason::TypeMismatch);
    }

    let Some(instr) = ssa
        .block(block_idx)
        .and_then(|block| block.instruction(instr_idx))
    else {
        return Err(ReplacementSkipReason::MissingInstruction);
    };

    if !instr.op().uses_var(old_var) {
        return Err(ReplacementSkipReason::UseNotFound);
    }

    if instr.op().defs().any(|def| def == new_var) {
        return Err(ReplacementSkipReason::SelfReference);
    }

    let def_site = new.def_site();
    if def_site.block == block_idx {
        if def_site
            .instruction
            .is_some_and(|def_idx| def_idx >= instr_idx)
        {
            return Err(ReplacementSkipReason::SameBlockUseBeforeDef);
        }
        return Ok(());
    }

    if definition_dominates_block(ssa, def_site, block_idx, dominators) {
        Ok(())
    } else {
        Err(ReplacementSkipReason::DominanceViolation)
    }
}

/// Checks whether a definition site dominates an instruction-use block.
fn definition_dominates_block<T: Target>(
    ssa: &SsaFunction<T>,
    def_site: DefSite,
    use_block: usize,
    dominators: Option<&DominatorTree>,
) -> bool {
    if def_site.block == use_block {
        return true;
    }
    if def_site.block >= ssa.block_count() || use_block >= ssa.block_count() {
        return false;
    }

    dominators
        .is_some_and(|tree| tree.dominates(NodeId::new(def_site.block), NodeId::new(use_block)))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        ir::{
            block::SsaBlock,
            instruction::SsaInstruction,
            ops::SsaOp,
            value::ConstValue,
            variable::{DefSite, VariableOrigin},
        },
        testing::{MockTarget, MockType},
    };

    /// Returns a test variable ID by dense index.
    fn var(index: usize) -> SsaVarId {
        SsaVarId::from_index(index)
    }

    /// Builds a one-block function where `v1` is used before `v2` is defined.
    fn same_block_late_def_function() -> SsaFunction<MockTarget> {
        let mut ssa = SsaFunction::new(0, 0);
        let v0 = ssa.create_variable(
            VariableOrigin::Local(0),
            0,
            DefSite::instruction(0, 0),
            MockType::I32,
        );
        let v1 = ssa.create_variable(
            VariableOrigin::Local(1),
            0,
            DefSite::instruction(0, 1),
            MockType::I32,
        );
        let v2 = ssa.create_variable(
            VariableOrigin::Local(2),
            0,
            DefSite::instruction(0, 3),
            MockType::I32,
        );
        let v3 = ssa.create_variable(
            VariableOrigin::Local(3),
            0,
            DefSite::instruction(0, 2),
            MockType::I32,
        );

        let mut block = SsaBlock::new(0);
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v0,
            value: ConstValue::I32(1),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Copy { dest: v1, src: v0 }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Add {
            dest: v3,
            left: v1,
            right: v1,
            flags: None,
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v2,
            value: ConstValue::I32(2),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: Some(v3) }));
        ssa.add_block(block);
        ssa.recompute_uses();
        ssa
    }

    /// Builds a branching function where a definition in B1 does not dominate B2.
    fn non_dominating_definition_function() -> SsaFunction<MockTarget> {
        let mut ssa = SsaFunction::new(0, 0);
        let cond = ssa.create_variable(
            VariableOrigin::Local(0),
            0,
            DefSite::instruction(0, 0),
            MockType::I32,
        );
        let branch_value = ssa.create_variable(
            VariableOrigin::Local(1),
            0,
            DefSite::instruction(1, 0),
            MockType::I32,
        );
        let old = ssa.create_variable(
            VariableOrigin::Local(2),
            0,
            DefSite::instruction(2, 0),
            MockType::I32,
        );
        let dest = ssa.create_variable(
            VariableOrigin::Local(3),
            0,
            DefSite::instruction(2, 1),
            MockType::I32,
        );

        let mut entry = SsaBlock::new(0);
        entry.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: cond,
            value: ConstValue::I32(1),
        }));
        entry.add_instruction(SsaInstruction::synthetic(SsaOp::Branch {
            condition: cond,
            true_target: 1,
            false_target: 2,
        }));

        let mut left = SsaBlock::new(1);
        left.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: branch_value,
            value: ConstValue::I32(10),
        }));
        left.add_instruction(SsaInstruction::synthetic(SsaOp::Jump { target: 3 }));

        let mut right = SsaBlock::new(2);
        right.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: old,
            value: ConstValue::I32(20),
        }));
        right.add_instruction(SsaInstruction::synthetic(SsaOp::Add {
            dest,
            left: old,
            right: old,
            flags: None,
        }));
        right.add_instruction(SsaInstruction::synthetic(SsaOp::Jump { target: 3 }));

        let mut exit = SsaBlock::new(3);
        exit.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: None }));

        ssa.add_block(entry);
        ssa.add_block(left);
        ssa.add_block(right);
        ssa.add_block(exit);
        ssa.recompute_uses();
        ssa
    }

    /// Builds a function with an otherwise-safe replacement of incompatible types.
    fn type_mismatch_function() -> SsaFunction<MockTarget> {
        let mut ssa = SsaFunction::new(0, 0);
        let old = ssa.create_variable(
            VariableOrigin::Local(0),
            0,
            DefSite::instruction(0, 0),
            MockType::I32,
        );
        let new = ssa.create_variable(
            VariableOrigin::Local(1),
            0,
            DefSite::instruction(0, 1),
            MockType::I64,
        );
        let dest = ssa.create_variable(
            VariableOrigin::Local(2),
            0,
            DefSite::instruction(0, 2),
            MockType::I32,
        );

        let mut block = SsaBlock::new(0);
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: old,
            value: ConstValue::I32(1),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: new,
            value: ConstValue::I64(2),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Add {
            dest,
            left: old,
            right: old,
            flags: None,
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Return {
            value: Some(dest),
        }));
        ssa.add_block(block);
        ssa.recompute_uses();
        ssa
    }

    /// `fold_terminator_pruning_phis` stays at `InstructionsOnly` because
    /// removing an edge can only *increase* dominance, so no new phi can be
    /// required. An edit that adds an edge breaks that argument, so it must be
    /// rejected rather than under-repaired.
    #[test]
    fn folding_a_terminator_rejects_an_edit_that_adds_an_edge() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 3);
        let cond = ssa.create_variable(
            VariableOrigin::Local(0),
            0,
            DefSite::instruction(0, 0),
            MockType::I32,
        );

        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: cond,
            value: ConstValue::I32(1),
        }));
        b0.add_instruction(SsaInstruction::synthetic(SsaOp::Jump { target: 1 }));
        ssa.add_block(b0);

        for id in [1usize, 2] {
            let mut block = SsaBlock::new(id);
            block.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: None }));
            ssa.add_block(block);
        }
        ssa.recompute_uses();

        let outcome = ssa.edit(SsaEditOptions::new(), |editor| {
            // `Jump B1` -> `Branch B1, B2` adds the edge to B2.
            let result = editor.fold_terminator_pruning_phis(
                0,
                SsaOp::Branch {
                    condition: cond,
                    true_target: 1,
                    false_target: 2,
                },
            );
            assert!(
                result.is_err(),
                "an edge-adding fold must be rejected on this path"
            );
            Ok(())
        });
        assert!(outcome.is_ok());

        // Pruning a real edge still works.
        let outcome = ssa.edit(SsaEditOptions::new(), |editor| {
            editor.fold_terminator_pruning_phis(0, SsaOp::Jump { target: 1 })?;
            Ok(())
        });
        assert!(outcome.is_ok());
    }

    #[test]
    fn checked_replacement_rejects_same_block_late_definition() {
        let mut ssa = same_block_late_def_function();

        let result = ssa.replace_uses_checked(var(1), var(2));

        assert_eq!(result.replaced, 0);
        assert_eq!(result.skipped.len(), 1);
        assert_eq!(
            result.skipped[0].reason,
            ReplacementSkipReason::SameBlockUseBeforeDef
        );
    }

    #[test]
    fn checked_replacement_rejects_self_reference() {
        let mut ssa = same_block_late_def_function();

        let result = ssa.replace_uses_checked(var(1), var(3));

        assert_eq!(result.replaced, 0);
        assert_eq!(result.skipped.len(), 1);
        assert_eq!(
            result.skipped[0].reason,
            ReplacementSkipReason::SelfReference
        );
    }

    #[test]
    fn checked_replacement_rejects_non_dominating_definition() {
        let mut ssa = non_dominating_definition_function();

        let result = ssa.replace_uses_checked(var(2), var(1));

        assert_eq!(result.replaced, 0);
        assert_eq!(result.skipped.len(), 1);
        assert_eq!(
            result.skipped[0].reason,
            ReplacementSkipReason::DominanceViolation
        );
    }

    #[test]
    fn checked_replacement_rejects_type_mismatch() {
        let mut ssa = type_mismatch_function();

        let result = ssa.replace_uses_checked(var(0), var(1));

        assert_eq!(result.replaced, 0);
        assert_eq!(result.skipped.len(), 1);
        assert_eq!(
            result.skipped[0].reason,
            ReplacementSkipReason::TypeMismatch
        );
    }

    #[test]
    fn editor_repairs_once_after_instruction_edits() {
        let mut ssa = same_block_late_def_function();

        let report = ssa
            .edit(SsaEditOptions::new(), |editor| {
                editor.nop_instruction(0, 1)?;
                Ok(())
            })
            .unwrap();

        assert!(report.changed);
        assert_eq!(report.scope, SsaEditScope::InstructionsOnly);
        assert!(
            ssa.block(0)
                .unwrap()
                .instructions()
                .iter()
                .all(|instr| !matches!(instr.op(), SsaOp::Nop))
        );
    }

    #[test]
    fn editor_rolls_back_when_verification_fails() {
        let mut ssa = same_block_late_def_function();
        let original = ssa.clone();

        let result = ssa.edit(
            SsaEditOptions::new()
                .with_verify(true)
                .with_rollback(SsaRollbackPolicy::OnFailure),
            |editor| {
                editor.replace_instruction_op(
                    0,
                    4,
                    SsaOp::Return {
                        value: Some(SsaVarId::from_index(999)),
                    },
                )?;
                Ok(())
            },
        );

        assert!(result.is_err());
        assert_eq!(ssa.variable_count(), original.variable_count());
        assert_eq!(
            ssa.block(0).unwrap().instructions().len(),
            original.block(0).unwrap().instructions().len()
        );
        assert_eq!(
            ssa.block(0).unwrap().instructions()[4].uses(),
            original.block(0).unwrap().instructions()[4].uses()
        );
    }

    /// The indexed replacement must be indistinguishable from the scanning one.
    ///
    /// `replace_uses_checked_indexed` exists purely to avoid the O(function)
    /// scan; if it ever diverges — by consulting a stale use list, by missing a
    /// site, or by replacing one the checks should have rejected — it silently
    /// changes optimizer output. This runs both forms against identical copies
    /// of every fixture, for every variable pair, and requires the replacement
    /// count, the skip reasons, and the resulting IR to match exactly.
    #[test]
    fn indexed_replacement_matches_scanning_replacement() {
        let fixtures: Vec<(&str, SsaFunction<MockTarget>)> = vec![
            ("scalar", crate::testing::scalar_rewrite_fixture()),
            ("diamond", crate::testing::diamond_phi_fixture()),
            ("loop", crate::testing::loop_counter_fixture()),
            ("memory", crate::testing::memory_effect_fixture()),
            ("straight_32", crate::testing::straight_line_fixture(32)),
            ("diamonds_8", crate::testing::diamond_chain_fixture(8)),
        ];

        for (name, base) in fixtures {
            let ids: Vec<SsaVarId> = base.variables().iter().map(|v| v.id()).collect();
            for &old_var in &ids {
                for &new_var in &ids {
                    if old_var == new_var {
                        continue;
                    }

                    let mut scanned = base.clone();
                    scanned.recompute_uses();
                    let scan_result = scanned.replace_uses_checked_with(old_var, new_var, None);

                    let mut indexed = base.clone();
                    indexed.recompute_uses();
                    let index_result = indexed.replace_uses_checked_indexed(old_var, new_var, None);

                    assert_eq!(
                        scan_result.replaced, index_result.replaced,
                        "{name}: replaced count differs for {old_var} -> {new_var}"
                    );
                    assert_eq!(
                        scan_result.skipped.len(),
                        index_result.skipped.len(),
                        "{name}: skip count differs for {old_var} -> {new_var}"
                    );

                    // Resulting IR must be identical instruction for instruction.
                    assert_eq!(
                        scanned.block_count(),
                        indexed.block_count(),
                        "{name}: block count differs"
                    );
                    for block_idx in 0..scanned.block_count() {
                        let a = scanned.block(block_idx).map(SsaBlock::instructions);
                        let b = indexed.block(block_idx).map(SsaBlock::instructions);
                        match (a, b) {
                            (Some(a), Some(b)) => {
                                assert_eq!(a.len(), b.len(), "{name}: block {block_idx} length");
                                for (i, (ia, ib)) in a.iter().zip(b.iter()).enumerate() {
                                    assert_eq!(
                                        ia.op(),
                                        ib.op(),
                                        "{name}: block {block_idx} instr {i} differs for {old_var} -> {new_var}"
                                    );
                                }
                            }
                            (None, None) => {}
                            _ => panic!("{name}: block {block_idx} presence differs"),
                        }
                    }
                }
            }
        }
    }

    /// A *sequence* of replacements through one edit session must match the
    /// scanning form applied in the same order.
    ///
    /// This is the invariant the indexed path actually risks. The scanning form
    /// re-derives uses from the IR on every call, so it is inherently immune to
    /// staleness; the indexed form reads a cached list and must therefore keep
    /// that list exact as it rewrites. Chained pairs (`a -> b` then `b -> c`)
    /// are the case that breaks a naive implementation: the second replacement
    /// must see the uses the first one *created*.
    ///
    /// Verified to fail if the use-list maintenance in
    /// `replace_uses_checked_indexed` is removed. The stale-site guard there is
    /// defensive rather than load-bearing — with maintenance correct no stale
    /// site arises, so removing it does not fail this test; it protects against
    /// a future caller mutating the IR mid-session without clearing
    /// `uses_indexed`.
    #[test]
    fn chained_indexed_replacements_match_scanning() {
        let fixtures: Vec<(&str, SsaFunction<MockTarget>)> = vec![
            ("scalar", crate::testing::scalar_rewrite_fixture()),
            ("diamond", crate::testing::diamond_phi_fixture()),
            ("loop", crate::testing::loop_counter_fixture()),
            ("straight_16", crate::testing::straight_line_fixture(16)),
            ("diamonds_4", crate::testing::diamond_chain_fixture(4)),
        ];

        for (name, base) in fixtures {
            let ids: Vec<SsaVarId> = base.variables().iter().map(|v| v.id()).collect();
            if ids.len() < 3 {
                continue;
            }
            // Overlapping chains: each pair's target is the next pair's source,
            // so every step depends on uses the previous step introduced.
            let mut pairs: Vec<(SsaVarId, SsaVarId)> = Vec::new();
            for window in ids.windows(2) {
                if let [a, b] = window {
                    pairs.push((*a, *b));
                }
            }

            // Reference: the scanning form, which never consults a use list.
            let mut scanned = base.clone();
            let mut scan_total = 0usize;
            for &(old_var, new_var) in &pairs {
                scan_total = scan_total.saturating_add(
                    scanned
                        .replace_uses_checked_with(old_var, new_var, None)
                        .replaced,
                );
            }

            // Subject: the indexed form, all inside one editor session so the
            // cached lists must survive every step.
            let mut indexed = base.clone();
            let mut index_total = 0usize;
            let report = indexed.edit(SsaEditOptions::new(), |editor| {
                for &(old_var, new_var) in &pairs {
                    index_total = index_total.saturating_add(
                        editor
                            .replace_uses_checked_with(old_var, new_var, None)
                            .replaced,
                    );
                }
                Ok(())
            });
            assert!(report.is_ok(), "{name}: edit session failed");

            assert_eq!(
                scan_total, index_total,
                "{name}: total replacements differ across the chain"
            );
            for block_idx in 0..scanned.block_count() {
                let a = scanned.block(block_idx).map(SsaBlock::instructions);
                let b = indexed.block(block_idx).map(SsaBlock::instructions);
                if let (Some(a), Some(b)) = (a, b) {
                    for (i, (ia, ib)) in a.iter().zip(b.iter()).enumerate() {
                        assert_eq!(
                            ia.op(),
                            ib.op(),
                            "{name}: block {block_idx} instr {i} diverged after chained replacement"
                        );
                    }
                }
            }
        }
    }
}