dotscope 0.7.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
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
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
//! Opaque predicate detection and removal pass.
//!
//! Opaque predicates are conditional expressions that always evaluate to the same
//! value at runtime, but appear complex to static analysis. Obfuscators use them
//! to confuse decompilers and analysis tools.
//!
//! # Detection Strategies
//!
//! ## Basic Patterns
//! - **Self-comparison**: `x == x`, `x != x`, `x < x`, `x > x`
//! - **Identity operations**: `x ^ x == 0`, `x - x == 0`
//! - **Zero operations**: `x * 0`, `x & 0`, `x % 1`
//!
//! ## Number-Theoretic Predicates
//! - **Consecutive integers**: `(x * (x + 1)) % 2 == 0` (always true)
//! - **Square properties**: `x² >= 0` (always true for integers)
//! - **Modular arithmetic**: `(x² - x) % 2 == 0` (always true)
//!
//! ## Type-Based Predicates
//! - **Null checks**: `obj != null` after `newobj` (always true)
//! - **Array length**: `arr.Length >= 0` (always true)
//!
//! ## Range-Based Predicates
//! - **Unsigned bounds**: `unsigned_x >= 0` (always true)
//! - **Correlated conditions**: `if (x > 5) { if (x < 3) { dead } }`
//!
//! # Example
//!
//! Before:
//! ```text
//! v0 = 5
//! v1 = ceq v0, v0    // Always true
//! branch v1, B1, B2  // Always goes to B1
//! ```
//!
//! After:
//! ```text
//! v0 = 5
//! v1 = true
//! jump B1
//! ```

use std::collections::BTreeMap;

use crate::{
    analysis::{
        ConstValue, DefUseIndex, SsaEvaluator, SsaFunction, SsaInstruction, SsaOp, SsaVarId,
        ValueRange,
    },
    compiler::{
        pass::{PassCapability, SsaPass},
        CompilerContext, EventKind, EventLog,
    },
    metadata::{token::Token, typesystem::PointerSize},
    utils::BitSet,
    CilObject, Result,
};

/// Result of analyzing a potential opaque predicate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PredicateResult {
    /// The predicate always evaluates to true.
    AlwaysTrue,
    /// The predicate always evaluates to false.
    AlwaysFalse,
    /// Cannot determine the predicate's value.
    Unknown,
}

impl PredicateResult {
    /// Converts to an optional boolean.
    ///
    /// # Returns
    ///
    /// `Some(true)` for `AlwaysTrue`, `Some(false)` for `AlwaysFalse`, `None` for `Unknown`.
    #[must_use]
    pub fn as_bool(self) -> Option<bool> {
        match self {
            Self::AlwaysTrue => Some(true),
            Self::AlwaysFalse => Some(false),
            Self::Unknown => None,
        }
    }

    /// Negates the predicate result.
    ///
    /// `AlwaysTrue` becomes `AlwaysFalse` and vice versa. `Unknown` stays `Unknown`.
    #[must_use]
    pub fn negate(self) -> Self {
        match self {
            Self::AlwaysTrue => Self::AlwaysFalse,
            Self::AlwaysFalse => Self::AlwaysTrue,
            Self::Unknown => Self::Unknown,
        }
    }
}

/// Result of analyzing a comparison for algebraic simplification.
///
/// Unlike `PredicateResult` which determines if a comparison is always true/false,
/// this enum represents transformations that simplify comparisons while preserving
/// their runtime behavior.
#[derive(Debug, Clone)]
enum ComparisonSimplification {
    /// Replace with a simpler comparison operation.
    SimplerOp { new_op: SsaOp, reason: &'static str },
    /// Replace with a copy of another variable (e.g., `(cmp) == 1` → `cmp`).
    Copy {
        dest: SsaVarId,
        src: SsaVarId,
        reason: &'static str,
    },
}

/// Cached definition information for efficient predicate analysis.
///
/// Wraps [`DefUseIndex`] for basic definition lookups and augments it with
/// specialized tracking that `DefUseIndex` does not provide: phi-defined
/// variables, non-null provenance (from `NewObj`/`NewArr`/`Box`/`LoadToken`),
/// array-length provenance, and computed [`ValueRange`]s for constant and
/// non-negative variables.
struct DefinitionCache {
    /// Index for definition lookups (block, instruction, operation).
    index: DefUseIndex,
    /// Variables defined by phi nodes.
    phi_defs: BitSet,
    /// Variables that are known to be non-null (produced by `NewObj`, `NewArr`, `Box`, or `LoadToken`).
    non_null_vars: BitSet,
    /// Variables that come from `ArrayLength` operations.
    array_length_vars: BitSet,
    /// Computed value ranges for variables (constants get exact ranges, array lengths get non-negative).
    ranges: BTreeMap<SsaVarId, ValueRange>,
}

impl DefinitionCache {
    /// Builds the definition cache from an SSA function.
    ///
    /// Performs a single pass over all blocks to populate:
    /// - `index`: delegated to [`DefUseIndex::build_with_ops`] for var-to-op mapping.
    /// - `phi_defs`: bitset of variables defined by phi nodes (not tracked by `DefUseIndex`).
    /// - `non_null_vars`: bitset of variables produced by `NewObj`, `NewArr`, `Box`, or `LoadToken`.
    /// - `array_length_vars`: bitset of variables from `ArrayLength` ops.
    /// - `ranges`: [`ValueRange::constant`] for `Const` ops, [`ValueRange::non_negative`] for
    ///   `ArrayLength` ops.
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function to build the cache from.
    ///
    /// # Returns
    ///
    /// A fully populated `DefinitionCache` with definition index, phi-def tracking,
    /// non-null provenance, array-length provenance, and value ranges.
    fn build(ssa: &SsaFunction) -> Self {
        // Use DefUseIndex for basic definition tracking
        let index = DefUseIndex::build_with_ops(ssa);

        let var_count = ssa.variable_count();
        let mut phi_defs = BitSet::new(var_count);
        let mut non_null_vars = BitSet::new(var_count);
        let mut array_length_vars = BitSet::new(var_count);
        let mut ranges = BTreeMap::new();

        for (_block_idx, block) in ssa.iter_blocks() {
            // Process phi nodes (not covered by DefUseIndex)
            for phi in block.phi_nodes() {
                phi_defs.insert(phi.result().index());
            }

            // Process instructions for specialized tracking
            for instr in block.instructions() {
                let op = instr.op();
                if let Some(dest) = op.dest() {
                    // Track non-null producing operations and value ranges
                    match op {
                        SsaOp::NewObj { .. }
                        | SsaOp::NewArr { .. }
                        | SsaOp::Box { .. }
                        | SsaOp::LoadToken { .. } => {
                            // Non-null tracked separately (not a numeric range)
                            non_null_vars.insert(dest.index());
                        }
                        SsaOp::ArrayLength { .. } => {
                            array_length_vars.insert(dest.index());
                            ranges.insert(dest, ValueRange::non_negative());
                        }
                        SsaOp::Const { value, .. } => {
                            if let Some(v) = value.as_i64() {
                                ranges.insert(dest, ValueRange::constant(v));
                            }
                        }
                        _ => {}
                    }
                }
            }
        }

        Self {
            index,
            phi_defs,
            non_null_vars,
            array_length_vars,
            ranges,
        }
    }

    /// Gets the defining operation for a variable.
    fn get_definition(&self, var: SsaVarId) -> Option<&SsaOp> {
        self.index.def_op(var)
    }

    /// Checks if a variable is defined by a phi node.
    fn is_phi_defined(&self, var: SsaVarId) -> bool {
        self.phi_defs.contains(var.index())
    }

    /// Checks if a variable is known to be non-null.
    fn is_non_null(&self, var: SsaVarId) -> bool {
        self.non_null_vars.contains(var.index())
    }

    /// Gets the value range for a variable.
    fn get_range(&self, var: SsaVarId) -> Option<&ValueRange> {
        self.ranges.get(&var)
    }
}

/// Opaque predicate detection and removal pass.
pub struct OpaquePredicatePass;

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

impl OpaquePredicatePass {
    /// Creates a new opaque predicate pass.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Maximum recursion depth for nested predicate analysis.
    ///
    /// Each level corresponds to one SSA instruction defining a comparison result
    /// that feeds into another comparison. 16 levels handles deeply nested opaque
    /// predicates from advanced obfuscators (e.g., PureLogs multi-level chains)
    /// while preventing stack overflow on pathological inputs.
    const MAX_PREDICATE_DEPTH: usize = 16;

    /// Analyzes a predicate operation with full context, dispatching by `SsaOp` kind.
    ///
    /// Pattern-matching cascade:
    /// 1. **Self-comparison** (`Ceq`/`Clt`/`Cgt` where `left == right`): immediate result.
    /// 2. **Equality analysis** (`Ceq`): delegates to [`analyze_equality`](Self::analyze_equality)
    ///    for XOR==0, SUB==0, MUL*0==0, AND&0==0, number-theoretic, constant, null, and nested patterns.
    /// 3. **Less-than / greater-than** (`Clt`/`Cgt`): delegates to range-based and constant analysis.
    /// 4. **Zero-producing ops** (`Xor`/`Sub` with `left==right`): returns `Unknown` since the
    ///    result only becomes meaningful when used in a comparison (handled at that level).
    /// 5. **Remainder / Multiplication / And**: delegates to specialized analyzers.
    ///
    /// Supports recursion up to [`MAX_PREDICATE_DEPTH`](Self::MAX_PREDICATE_DEPTH) for nested
    /// predicates (e.g., `ceq(ceq(x, x), 1)`).
    ///
    /// # Arguments
    ///
    /// * `op` - The SSA operation to analyze (typically a comparison or arithmetic op).
    /// * `cache` - Pre-built definition cache for efficient variable resolution.
    /// * `depth` - Current recursion depth (0 at the top level).
    ///
    /// # Returns
    ///
    /// [`PredicateResult::AlwaysTrue`] or [`AlwaysFalse`](PredicateResult::AlwaysFalse) if the
    /// predicate can be statically determined, [`Unknown`](PredicateResult::Unknown) otherwise.
    fn analyze_predicate_with_cache(
        op: &SsaOp,
        cache: &DefinitionCache,
        depth: usize,
    ) -> PredicateResult {
        if depth > Self::MAX_PREDICATE_DEPTH {
            return PredicateResult::Unknown;
        }

        match op {
            // Self-comparison patterns
            SsaOp::Ceq { left, right, .. } => {
                if left == right {
                    return PredicateResult::AlwaysTrue;
                }
                Self::analyze_equality(*left, *right, cache, depth)
            }

            SsaOp::Clt {
                left,
                right,
                unsigned,
                ..
            } => {
                if left == right {
                    return PredicateResult::AlwaysFalse;
                }
                Self::analyze_less_than(*left, *right, *unsigned, cache, depth)
            }

            SsaOp::Cgt {
                left,
                right,
                unsigned,
                ..
            } => {
                if left == right {
                    return PredicateResult::AlwaysFalse;
                }
                Self::analyze_greater_than(*left, *right, *unsigned, cache, depth)
            }

            // Operations that produce zero
            SsaOp::Xor { left, right, .. } if left == right => {
                // x ^ x = 0, handled when used in comparison
                PredicateResult::Unknown
            }

            SsaOp::Sub { left, right, .. } if left == right => {
                // x - x = 0, handled when used in comparison
                PredicateResult::Unknown
            }

            SsaOp::Rem { left, right, .. } => Self::analyze_remainder(*left, *right, cache, depth),

            SsaOp::Mul { left, right, .. } => {
                Self::analyze_multiplication(*left, *right, cache, depth)
            }

            SsaOp::And { left, right, .. } => Self::analyze_and(*left, *right, cache, depth),

            _ => PredicateResult::Unknown,
        }
    }

    /// Analyzes an equality comparison (`Ceq`) for opaque predicate patterns.
    ///
    /// Checks the following patterns (each with symmetric left/right variants):
    /// - `(x ^ x) == 0` -- XOR self-cancellation, always true.
    /// - `(x - x) == 0` -- subtraction self-cancellation, always true.
    /// - `(x * 0) == 0` or `(0 * x) == 0` -- zero-producing multiplication, always true.
    /// - `(x & 0) == 0` or `(0 & x) == 0` -- zero-producing AND, always true.
    /// - Number-theoretic: `(x*(x+1)) % 2 == 0` and factored forms, always true.
    /// - Constant equality: both sides are constants with known values.
    /// - Null checks: non-null variable (from `NewObj` etc.) compared to null, always false.
    /// - **Nested analysis fallback**: if the left operand is itself a predicate (comparison),
    ///   recursively analyzes it. If the result is known and compared to 1, returns that result;
    ///   if compared to 0, returns the negation.
    ///
    /// # Arguments
    ///
    /// * `left` - Left operand of the `Ceq`.
    /// * `right` - Right operand of the `Ceq`.
    /// * `cache` - Definition cache for resolving variable definitions.
    /// * `depth` - Current recursion depth for nested predicate analysis.
    ///
    /// # Returns
    ///
    /// [`PredicateResult::AlwaysTrue`] or [`AlwaysFalse`](PredicateResult::AlwaysFalse) if the
    /// equality can be statically determined, [`Unknown`](PredicateResult::Unknown) otherwise.
    fn analyze_equality(
        left: SsaVarId,
        right: SsaVarId,
        cache: &DefinitionCache,
        depth: usize,
    ) -> PredicateResult {
        let left_def = cache.get_definition(left);
        let right_def = cache.get_definition(right);

        // Check for (x ^ x) == 0 pattern
        if let Some(SsaOp::Xor {
            left: xl,
            right: xr,
            ..
        }) = left_def
        {
            if xl == xr {
                if let Some(r) = right_def {
                    if Self::is_zero_constant(r) {
                        return PredicateResult::AlwaysTrue;
                    }
                }
            }
        }

        // Symmetric check
        if let Some(SsaOp::Xor {
            left: xl,
            right: xr,
            ..
        }) = right_def
        {
            if xl == xr {
                if let Some(l) = left_def {
                    if Self::is_zero_constant(l) {
                        return PredicateResult::AlwaysTrue;
                    }
                }
            }
        }

        // Check for (x - x) == 0 pattern
        if let Some(SsaOp::Sub {
            left: sl,
            right: sr,
            ..
        }) = left_def
        {
            if sl == sr {
                if let Some(r) = right_def {
                    if Self::is_zero_constant(r) {
                        return PredicateResult::AlwaysTrue;
                    }
                }
            }
        }

        // Symmetric check
        if let Some(SsaOp::Sub {
            left: sl,
            right: sr,
            ..
        }) = right_def
        {
            if sl == sr {
                if let Some(l) = left_def {
                    if Self::is_zero_constant(l) {
                        return PredicateResult::AlwaysTrue;
                    }
                }
            }
        }

        // Check for (x * 0) == 0 pattern
        if Self::is_zero_producing_mul(left_def, cache) {
            if let Some(r) = right_def {
                if Self::is_zero_constant(r) {
                    return PredicateResult::AlwaysTrue;
                }
            }
        }

        // Symmetric check
        if Self::is_zero_producing_mul(right_def, cache) {
            if let Some(l) = left_def {
                if Self::is_zero_constant(l) {
                    return PredicateResult::AlwaysTrue;
                }
            }
        }

        // Check for (x & 0) == 0 pattern
        if Self::is_zero_producing_and(left_def, cache) {
            if let Some(r) = right_def {
                if Self::is_zero_constant(r) {
                    return PredicateResult::AlwaysTrue;
                }
            }
        }

        // Symmetric check
        if Self::is_zero_producing_and(right_def, cache) {
            if let Some(l) = left_def {
                if Self::is_zero_constant(l) {
                    return PredicateResult::AlwaysTrue;
                }
            }
        }

        // Check for number-theoretic predicates that always evaluate to zero:
        //   (x * (x + 1)) % 2 == 0    — consecutive integer product is always even
        //   (x * x - x) % 2 == 0      — x²-x = x(x-1), consecutive product factored
        if Self::is_always_even_expression(left_def, cache) {
            if let Some(r) = right_def {
                if Self::is_zero_constant(r) {
                    return PredicateResult::AlwaysTrue;
                }
            }
        }

        // Check constant equality
        if let (Some(SsaOp::Const { value: lval, .. }), Some(SsaOp::Const { value: rval, .. })) =
            (left_def, right_def)
        {
            if let (Some(l), Some(r)) = (lval.as_i64(), rval.as_i64()) {
                return if l == r {
                    PredicateResult::AlwaysTrue
                } else {
                    PredicateResult::AlwaysFalse
                };
            }
        }

        // Check non-null equality with null
        if cache.is_non_null(left) {
            if let Some(r) = right_def {
                if Self::is_null_constant(r) {
                    return PredicateResult::AlwaysFalse;
                }
            }
        }

        if cache.is_non_null(right) {
            if let Some(l) = left_def {
                if Self::is_null_constant(l) {
                    return PredicateResult::AlwaysFalse;
                }
            }
        }

        // Nested analysis
        if let Some(left_op) = left_def {
            let left_result = Self::analyze_predicate_with_cache(left_op, cache, depth + 1);
            if left_result != PredicateResult::Unknown {
                if let Some(r) = right_def {
                    if Self::is_one_constant(r) {
                        return left_result;
                    }
                    if Self::is_zero_constant(r) {
                        return left_result.negate();
                    }
                }
            }
        }

        PredicateResult::Unknown
    }

    /// Analyzes a less-than comparison (`Clt`) for opaque predicate patterns.
    ///
    /// Checks in order:
    /// 1. **Constant comparison**: both operands are constants, evaluate directly (signed or unsigned).
    /// 2. **Range-based**: if both operands have known [`ValueRange`]s, checks whether
    ///    `left.max < right.min` (always true) or `left.min >= right.max` (always false).
    /// 3. **Left range vs. constant right**: uses [`ValueRange::always_less_than`].
    /// 4. **Unsigned bounds**: `x <.un 0` is always false (no unsigned value is less than zero).
    /// 5. **Non-negative check**: if `left` is known non-negative (e.g., `ArrayLength`),
    ///    then `left < 0` is always false.
    ///
    /// # Arguments
    ///
    /// * `left` - Left operand of the comparison.
    /// * `right` - Right operand of the comparison.
    /// * `unsigned` - Whether this is an unsigned comparison (`clt.un`).
    /// * `cache` - Definition cache for resolving variable definitions and ranges.
    /// * `_depth` - Unused (less-than analysis is non-recursive).
    ///
    /// # Returns
    ///
    /// [`PredicateResult::AlwaysTrue`] or [`AlwaysFalse`](PredicateResult::AlwaysFalse) if the
    /// less-than comparison can be statically determined,
    /// [`Unknown`](PredicateResult::Unknown) otherwise.
    fn analyze_less_than(
        left: SsaVarId,
        right: SsaVarId,
        unsigned: bool,
        cache: &DefinitionCache,
        _depth: usize,
    ) -> PredicateResult {
        let left_def = cache.get_definition(left);
        let right_def = cache.get_definition(right);

        // Constant comparison
        if let (Some(SsaOp::Const { value: lval, .. }), Some(SsaOp::Const { value: rval, .. })) =
            (left_def, right_def)
        {
            if unsigned {
                if let (Some(l), Some(r)) = (lval.as_u64(), rval.as_u64()) {
                    return if l < r {
                        PredicateResult::AlwaysTrue
                    } else {
                        PredicateResult::AlwaysFalse
                    };
                }
            } else if let (Some(l), Some(r)) = (lval.as_i64(), rval.as_i64()) {
                return if l < r {
                    PredicateResult::AlwaysTrue
                } else {
                    PredicateResult::AlwaysFalse
                };
            }
        }

        // Range-based analysis
        if let Some(left_range) = cache.get_range(left) {
            if let Some(right_range) = cache.get_range(right) {
                // left.max < right.min => always true
                if let (Some(l_max), Some(r_min)) = (left_range.max(), right_range.min()) {
                    if l_max < r_min {
                        return PredicateResult::AlwaysTrue;
                    }
                }
                // left.min >= right.max => always false
                if let (Some(l_min), Some(r_max)) = (left_range.min(), right_range.max()) {
                    if l_min >= r_max {
                        return PredicateResult::AlwaysFalse;
                    }
                }
            }

            // Check if left < constant
            if let Some(SsaOp::Const { value: rval, .. }) = right_def {
                if let Some(r) = rval.as_i64() {
                    if let Some(result) = left_range.always_less_than(r) {
                        return if result {
                            PredicateResult::AlwaysTrue
                        } else {
                            PredicateResult::AlwaysFalse
                        };
                    }
                }
            }
        }

        // Unsigned comparison: x < 0 is always false
        if unsigned {
            if let Some(SsaOp::Const { value: rval, .. }) = right_def {
                if rval.as_u64() == Some(0) {
                    return PredicateResult::AlwaysFalse;
                }
            }
        }

        // Non-negative < 0 is always false
        if let Some(left_range) = cache.get_range(left) {
            if left_range.is_always_non_negative() {
                if let Some(SsaOp::Const { value: rval, .. }) = right_def {
                    if rval.as_i64() == Some(0) {
                        return PredicateResult::AlwaysFalse;
                    }
                }
            }
        }

        PredicateResult::Unknown
    }

    /// Analyzes a greater-than comparison (`Cgt`) for opaque predicate patterns.
    ///
    /// Checks in order:
    /// 1. **Constant comparison**: both operands are constants, evaluate directly (signed or unsigned).
    /// 2. **Range-based**: if both operands have known [`ValueRange`]s, checks whether
    ///    `left.min > right.max` (always true) or `left.max <= right.min` (always false).
    /// 3. **Left range vs. constant right**: uses [`ValueRange::always_greater_than`].
    /// 4. **Unsigned bounds**: `0 >.un x` is always false (zero is never greater than any unsigned value).
    /// 5. **Non-negative vs. negative**: if `left` is known non-negative and `right` is a
    ///    negative constant, returns always true.
    ///
    /// # Arguments
    ///
    /// * `left` - Left operand of the comparison.
    /// * `right` - Right operand of the comparison.
    /// * `unsigned` - Whether this is an unsigned comparison (`cgt.un`).
    /// * `cache` - Definition cache for resolving variable definitions and ranges.
    /// * `_depth` - Unused (greater-than analysis is non-recursive).
    ///
    /// # Returns
    ///
    /// [`PredicateResult::AlwaysTrue`] or [`AlwaysFalse`](PredicateResult::AlwaysFalse) if the
    /// greater-than comparison can be statically determined,
    /// [`Unknown`](PredicateResult::Unknown) otherwise.
    fn analyze_greater_than(
        left: SsaVarId,
        right: SsaVarId,
        unsigned: bool,
        cache: &DefinitionCache,
        _depth: usize,
    ) -> PredicateResult {
        let left_def = cache.get_definition(left);
        let right_def = cache.get_definition(right);

        // Constant comparison
        if let (Some(SsaOp::Const { value: lval, .. }), Some(SsaOp::Const { value: rval, .. })) =
            (left_def, right_def)
        {
            if unsigned {
                if let (Some(l), Some(r)) = (lval.as_u64(), rval.as_u64()) {
                    return if l > r {
                        PredicateResult::AlwaysTrue
                    } else {
                        PredicateResult::AlwaysFalse
                    };
                }
            } else if let (Some(l), Some(r)) = (lval.as_i64(), rval.as_i64()) {
                return if l > r {
                    PredicateResult::AlwaysTrue
                } else {
                    PredicateResult::AlwaysFalse
                };
            }
        }

        // Range-based analysis
        if let Some(left_range) = cache.get_range(left) {
            if let Some(right_range) = cache.get_range(right) {
                // left.min > right.max => always true
                if let (Some(l_min), Some(r_max)) = (left_range.min(), right_range.max()) {
                    if l_min > r_max {
                        return PredicateResult::AlwaysTrue;
                    }
                }
                // left.max <= right.min => always false
                if let (Some(l_max), Some(r_min)) = (left_range.max(), right_range.min()) {
                    if l_max <= r_min {
                        return PredicateResult::AlwaysFalse;
                    }
                }
            }

            // Check if left > constant
            if let Some(SsaOp::Const { value: rval, .. }) = right_def {
                if let Some(r) = rval.as_i64() {
                    if let Some(result) = left_range.always_greater_than(r) {
                        return if result {
                            PredicateResult::AlwaysTrue
                        } else {
                            PredicateResult::AlwaysFalse
                        };
                    }
                }
            }
        }

        // Unsigned: 0 > x is always false
        if unsigned {
            if let Some(SsaOp::Const { value: lval, .. }) = left_def {
                if lval.as_u64() == Some(0) {
                    return PredicateResult::AlwaysFalse;
                }
            }
        }

        // Non-negative value >= 0 is always true (x > -1 equivalent)
        if let Some(left_range) = cache.get_range(left) {
            if left_range.is_always_non_negative() {
                if let Some(SsaOp::Const { value: rval, .. }) = right_def {
                    if rval.as_i64().is_some_and(|r| r < 0) {
                        return PredicateResult::AlwaysTrue;
                    }
                }
            }
        }

        PredicateResult::Unknown
    }

    /// Analyzes a remainder operation (`Rem`) for `x % 1` which always produces zero.
    ///
    /// Returns `Unknown` rather than `AlwaysTrue`/`AlwaysFalse` because the zero result
    /// is only meaningful when subsequently compared (handled by the equality analysis).
    ///
    /// # Arguments
    ///
    /// * `_left` - Left operand of the remainder (unused; any value mod 1 is zero).
    /// * `right` - Right operand of the remainder (checked for constant 1).
    /// * `cache` - Definition cache for resolving variable definitions.
    /// * `_depth` - Unused (remainder analysis is non-recursive).
    ///
    /// # Returns
    ///
    /// Always returns [`PredicateResult::Unknown`] because the zero result is only
    /// meaningful when used in a subsequent comparison.
    fn analyze_remainder(
        _left: SsaVarId,
        right: SsaVarId,
        cache: &DefinitionCache,
        _depth: usize,
    ) -> PredicateResult {
        // x % 1 == 0 is always true
        if let Some(SsaOp::Const { value: rval, .. }) = cache.get_definition(right) {
            if rval.as_i64() == Some(1) {
                // Result is always 0
                return PredicateResult::Unknown; // Handled when compared to 0
            }
        }
        PredicateResult::Unknown
    }

    /// Analyzes a multiplication (`Mul`) for zero-producing patterns (`x * 0` or `0 * x`).
    ///
    /// Returns `Unknown` because the zero result is only meaningful when subsequently
    /// compared (handled by the equality analysis at the comparison level).
    ///
    /// # Arguments
    ///
    /// * `left` - Left operand of the multiplication.
    /// * `right` - Right operand of the multiplication.
    /// * `cache` - Definition cache for resolving variable definitions.
    /// * `_depth` - Unused (multiplication analysis is non-recursive).
    ///
    /// # Returns
    ///
    /// Always returns [`PredicateResult::Unknown`] because the zero result is only
    /// meaningful when used in a subsequent comparison.
    fn analyze_multiplication(
        left: SsaVarId,
        right: SsaVarId,
        cache: &DefinitionCache,
        _depth: usize,
    ) -> PredicateResult {
        // x * 0 = 0
        if let Some(SsaOp::Const { value: lval, .. }) = cache.get_definition(left) {
            if lval.is_zero() {
                return PredicateResult::Unknown; // Result is 0
            }
        }
        if let Some(SsaOp::Const { value: rval, .. }) = cache.get_definition(right) {
            if rval.is_zero() {
                return PredicateResult::Unknown; // Result is 0
            }
        }
        PredicateResult::Unknown
    }

    /// Analyzes a bitwise AND (`And`) for zero-producing patterns (`x & 0` or `0 & x`).
    ///
    /// Returns `Unknown` because the zero result is only meaningful when subsequently
    /// compared (handled by the equality analysis at the comparison level).
    ///
    /// # Arguments
    ///
    /// * `left` - Left operand of the AND.
    /// * `right` - Right operand of the AND.
    /// * `cache` - Definition cache for resolving variable definitions.
    /// * `_depth` - Unused (AND analysis is non-recursive).
    ///
    /// # Returns
    ///
    /// Always returns [`PredicateResult::Unknown`] because the zero result is only
    /// meaningful when used in a subsequent comparison.
    fn analyze_and(
        left: SsaVarId,
        right: SsaVarId,
        cache: &DefinitionCache,
        _depth: usize,
    ) -> PredicateResult {
        // x & 0 = 0
        if let Some(SsaOp::Const { value: lval, .. }) = cache.get_definition(left) {
            if lval.is_zero() {
                return PredicateResult::Unknown;
            }
        }
        if let Some(SsaOp::Const { value: rval, .. }) = cache.get_definition(right) {
            if rval.is_zero() {
                return PredicateResult::Unknown;
            }
        }
        PredicateResult::Unknown
    }

    /// Checks if an operation produces a constant zero.
    fn is_zero_constant(op: &SsaOp) -> bool {
        matches!(op, SsaOp::Const { value, .. } if value.is_zero())
    }

    /// Checks if an operation produces a constant one.
    fn is_one_constant(op: &SsaOp) -> bool {
        matches!(op, SsaOp::Const { value, .. } if value.is_one())
    }

    /// Checks if an operation produces a null constant.
    fn is_null_constant(op: &SsaOp) -> bool {
        matches!(op, SsaOp::Const { value, .. } if value.is_null())
    }

    /// Checks if an operation produces a constant -1.
    fn is_minus_one_constant(op: &SsaOp) -> bool {
        matches!(op, SsaOp::Const { value, .. } if value.is_minus_one())
    }

    /// Returns `true` if the operation is a `Mul` where either operand is a constant zero.
    ///
    /// # Arguments
    ///
    /// * `op` - The operation to check, or `None` if the variable has no definition.
    /// * `cache` - Definition cache for resolving the multiplication operands.
    ///
    /// # Returns
    ///
    /// `true` if `op` is a `Mul` with at least one constant-zero operand, `false` otherwise.
    fn is_zero_producing_mul(op: Option<&SsaOp>, cache: &DefinitionCache) -> bool {
        if let Some(SsaOp::Mul { left, right, .. }) = op {
            if let Some(l) = cache.get_definition(*left) {
                if Self::is_zero_constant(l) {
                    return true;
                }
            }
            if let Some(r) = cache.get_definition(*right) {
                if Self::is_zero_constant(r) {
                    return true;
                }
            }
        }
        false
    }

    /// Returns `true` if the operation is an `And` where either operand is a constant zero.
    ///
    /// # Arguments
    ///
    /// * `op` - The operation to check, or `None` if the variable has no definition.
    /// * `cache` - Definition cache for resolving the AND operands.
    ///
    /// # Returns
    ///
    /// `true` if `op` is an `And` with at least one constant-zero operand, `false` otherwise.
    fn is_zero_producing_and(op: Option<&SsaOp>, cache: &DefinitionCache) -> bool {
        if let Some(SsaOp::And { left, right, .. }) = op {
            if let Some(l) = cache.get_definition(*left) {
                if Self::is_zero_constant(l) {
                    return true;
                }
            }
            if let Some(r) = cache.get_definition(*right) {
                if Self::is_zero_constant(r) {
                    return true;
                }
            }
        }
        false
    }

    /// Checks if an operation is an expression modulo 2 that always evaluates to 0.
    ///
    /// Detects number-theoretic opaque predicates based on the mathematical
    /// property that the product of two consecutive integers is always even:
    ///
    /// - `(x * (x + 1)) % 2` — direct consecutive product
    /// - `(x * (x - 1)) % 2` — reversed consecutive product
    /// - `(x * x - x) % 2` — factored form: x^2-x = x(x-1)
    /// - `(x * x + x) % 2` — factored form: x^2+x = x(x+1)
    ///
    /// # Arguments
    ///
    /// * `op` - The operation to check, or `None` if the variable has no definition.
    /// * `cache` - Definition cache for resolving operand definitions.
    ///
    /// # Returns
    ///
    /// `true` if the expression is a `Rem` by 2 whose dividend is always even, `false` otherwise.
    fn is_always_even_expression(op: Option<&SsaOp>, cache: &DefinitionCache) -> bool {
        let Some(SsaOp::Rem {
            left: rem_left,
            right: rem_right,
            ..
        }) = op
        else {
            return false;
        };

        // Divisor must be 2
        let is_mod2 = cache
            .get_definition(*rem_right)
            .is_some_and(|d| matches!(d, SsaOp::Const { value, .. } if value.as_i64() == Some(2)));
        if !is_mod2 {
            return false;
        }

        let dividend_def = cache.get_definition(*rem_left);

        // Pattern 1: x * (x +/- 1) — consecutive product
        if let Some(SsaOp::Mul {
            left: mul_left,
            right: mul_right,
            ..
        }) = dividend_def
        {
            if Self::is_consecutive_pair(*mul_left, *mul_right, cache) {
                return true;
            }
        }

        // Pattern 2: (x * x) -/+ x — factored consecutive product
        // x^2-x = x(x-1), x^2+x = x(x+1), both always even
        if let Some(SsaOp::Sub {
            left: op_left,
            right: op_right,
            ..
        })
        | Some(SsaOp::Add {
            left: op_left,
            right: op_right,
            ..
        }) = dividend_def
        {
            if Self::is_self_square(*op_left, *op_right, cache)
                || Self::is_self_square(*op_right, *op_left, cache)
            {
                return true;
            }
        }

        false
    }

    /// Checks if `square_var` is defined as `other * other` (i.e., `other^2`).
    ///
    /// # Arguments
    ///
    /// * `square_var` - The variable suspected to be a square.
    /// * `other` - The variable that should appear as both operands of the multiplication.
    /// * `cache` - Definition cache for resolving the definition of `square_var`.
    ///
    /// # Returns
    ///
    /// `true` if `square_var` is defined as `Mul { left: other, right: other }`, `false` otherwise.
    fn is_self_square(square_var: SsaVarId, other: SsaVarId, cache: &DefinitionCache) -> bool {
        matches!(
            cache.get_definition(square_var),
            Some(SsaOp::Mul { left, right, .. }) if *left == other && *right == other
        )
    }

    /// Checks if two variables form a consecutive integer pair (`n` and `n+1`).
    ///
    /// Performs three symmetric checks:
    /// - `b = a + 1` (either operand order of the `Add`).
    /// - `a = b + 1` (symmetric: `a` is the incremented one).
    /// - `b = a - (-1)` (subtraction of -1 is equivalent to adding 1).
    ///
    /// # Arguments
    ///
    /// * `a` - First variable of the potential consecutive pair.
    /// * `b` - Second variable of the potential consecutive pair.
    /// * `cache` - Definition cache for resolving variable definitions.
    ///
    /// # Returns
    ///
    /// `true` if one variable is defined as the other plus one, `false` otherwise.
    fn is_consecutive_pair(a: SsaVarId, b: SsaVarId, cache: &DefinitionCache) -> bool {
        // Check if b = a + 1
        if let Some(SsaOp::Add {
            left: add_left,
            right: add_right,
            ..
        }) = cache.get_definition(b)
        {
            if *add_left == a {
                if let Some(SsaOp::Const { value: rval, .. }) = cache.get_definition(*add_right) {
                    if rval.as_i64() == Some(1) {
                        return true;
                    }
                }
            }
            if *add_right == a {
                if let Some(SsaOp::Const { value: lval, .. }) = cache.get_definition(*add_left) {
                    if lval.as_i64() == Some(1) {
                        return true;
                    }
                }
            }
        }

        // Check if a = b + 1 (symmetric)
        if let Some(SsaOp::Add {
            left: add_left,
            right: add_right,
            ..
        }) = cache.get_definition(a)
        {
            if *add_left == b {
                if let Some(SsaOp::Const { value: rval, .. }) = cache.get_definition(*add_right) {
                    if rval.as_i64() == Some(1) {
                        return true;
                    }
                }
            }
            if *add_right == b {
                if let Some(SsaOp::Const { value: lval, .. }) = cache.get_definition(*add_left) {
                    if lval.as_i64() == Some(1) {
                        return true;
                    }
                }
            }
        }

        // Check if b = a - (-1) which is also a + 1
        if let Some(SsaOp::Sub {
            left: sub_left,
            right: sub_right,
            ..
        }) = cache.get_definition(b)
        {
            if *sub_left == a {
                if let Some(SsaOp::Const { value: rval, .. }) = cache.get_definition(*sub_right) {
                    if rval.as_i64() == Some(-1) {
                        return true;
                    }
                }
            }
        }

        false
    }

    /// Analyzes a branch condition variable to determine if it is an opaque predicate.
    ///
    /// Follows `Copy` chains iteratively with a [`BitSet`]-based cycle detector to handle
    /// SSA copies from phi nodes or obfuscated control flow. At each step:
    /// 1. If the variable has no definition in `DefUseIndex` but is phi-defined, checks
    ///    range info for a constant-zero equivalence (all-zero phi = always false).
    /// 2. Delegates to [`analyze_predicate_with_cache`](Self::analyze_predicate_with_cache) for
    ///    comparison operations.
    /// 3. For `Copy` ops, advances to the source variable and continues the loop.
    /// 4. For all other operations, falls back to [`analyze_branch_op`](Self::analyze_branch_op)
    ///    which checks direct truthiness of arithmetic and constant operations.
    ///
    /// # Arguments
    ///
    /// * `condition` - The SSA variable used as the branch condition.
    /// * `cache` - Definition cache for resolving the condition's definition chain.
    ///
    /// # Returns
    ///
    /// [`PredicateResult::AlwaysTrue`] if the branch always takes the true path,
    /// [`AlwaysFalse`](PredicateResult::AlwaysFalse) if it always takes the false path,
    /// [`Unknown`](PredicateResult::Unknown) if the condition cannot be statically resolved.
    fn analyze_branch(condition: SsaVarId, cache: &DefinitionCache) -> PredicateResult {
        // Follow Copy chain iteratively with cycle detection to prevent infinite recursion.
        // This is needed because SSA can have Copy cycles (e.g., from phi nodes or
        // obfuscated control flow patterns).
        let mut current = condition;
        let mut visited = BitSet::new(cache.phi_defs.len());

        loop {
            // Cycle detection: if we've seen this variable before, bail out
            if !visited.insert(current.index()) {
                return PredicateResult::Unknown;
            }

            let Some(cond_op) = cache.get_definition(current) else {
                // Check if it's a phi node - analyze all operands
                if cache.is_phi_defined(current) {
                    // For phi nodes, we'd need to check if all operands lead to the same result
                    // This is complex, so we return Unknown for now unless we have range info
                    if let Some(range) = cache.get_range(current) {
                        if let Some(result) = range.always_equal_to(0) {
                            return if result {
                                PredicateResult::AlwaysFalse
                            } else {
                                PredicateResult::AlwaysTrue
                            };
                        }
                    }
                }
                return PredicateResult::Unknown;
            };

            // First, check if it's a direct comparison predicate
            let predicate_result = Self::analyze_predicate_with_cache(cond_op, cache, 0);
            if predicate_result != PredicateResult::Unknown {
                return predicate_result;
            }

            // Check if the condition is a Copy - trace through to the source iteratively
            if let SsaOp::Copy { src, .. } = cond_op {
                current = *src;
                continue;
            }

            // Not a Copy, break out and analyze the operation
            return Self::analyze_branch_op(cond_op, cache);
        }
    }

    /// Analyzes a non-Copy, non-comparison operation for direct truthiness in a branch.
    ///
    /// Called after the `Copy` chain has been resolved by [`analyze_branch`](Self::analyze_branch).
    /// Checks:
    /// - `x ^ x` = 0 (always false in `brtrue`).
    /// - `x - x` = 0 (always false).
    /// - `x & 0` or `x * 0` = 0 (always false).
    /// - `x | -1` = all-bits-set (always true).
    /// - `Const`: zero/null/false is always false; non-zero numeric or `true` is always true.
    ///
    /// # Arguments
    ///
    /// * `cond_op` - The resolved operation producing the branch condition value.
    /// * `cache` - Definition cache for resolving operands of the condition operation.
    ///
    /// # Returns
    ///
    /// [`PredicateResult::AlwaysTrue`] if the operation always produces a non-zero value,
    /// [`AlwaysFalse`](PredicateResult::AlwaysFalse) if it always produces zero,
    /// [`Unknown`](PredicateResult::Unknown) if the truthiness cannot be determined.
    fn analyze_branch_op(cond_op: &SsaOp, cache: &DefinitionCache) -> PredicateResult {
        // Check operations that produce known zero values
        match cond_op {
            // x ^ x = 0, so brtrue on this result never jumps
            SsaOp::Xor { left, right, .. } if left == right => PredicateResult::AlwaysFalse,

            // x - x = 0, so brtrue on this result never jumps
            SsaOp::Sub { left, right, .. } if left == right => PredicateResult::AlwaysFalse,

            // x & 0 = 0, x * 0 = 0
            SsaOp::And { left, right, .. } | SsaOp::Mul { left, right, .. } => {
                let is_left_zero = cache
                    .get_definition(*left)
                    .is_some_and(Self::is_zero_constant);
                let is_right_zero = cache
                    .get_definition(*right)
                    .is_some_and(Self::is_zero_constant);

                if is_left_zero || is_right_zero {
                    PredicateResult::AlwaysFalse
                } else {
                    PredicateResult::Unknown
                }
            }

            // x | -1 = -1 (all bits set), so brtrue always jumps
            SsaOp::Or { left, right, .. } => {
                let is_left_minus_one = cache
                    .get_definition(*left)
                    .is_some_and(Self::is_minus_one_constant);
                let is_right_minus_one = cache
                    .get_definition(*right)
                    .is_some_and(Self::is_minus_one_constant);

                if is_left_minus_one || is_right_minus_one {
                    PredicateResult::AlwaysTrue
                } else {
                    PredicateResult::Unknown
                }
            }

            // Constant values: 0/null/false is always false, non-zero is always true
            SsaOp::Const { value, .. } => {
                if value.is_zero() || value.is_null() {
                    PredicateResult::AlwaysFalse
                } else if value.as_i64().is_some() || value.as_bool().is_some() {
                    // Non-zero numeric or true boolean
                    PredicateResult::AlwaysTrue
                } else {
                    PredicateResult::Unknown
                }
            }

            // All other operations have unknown truthiness
            // Note: ArrayLength is always >= 0, but we can't prove non-empty
            _ => PredicateResult::Unknown,
        }
    }

    /// Analyzes a comparison operation for algebraic simplification opportunities.
    ///
    /// This checks for patterns like:
    /// - `(x - y) == 0` → `x == y`
    /// - `(x - y) < 0` → `x < y`
    /// - `(x - y) > 0` → `x > y`
    /// - `(x ^ y) == 0` → `x == y`
    /// - `(cmp) == 1` → `cmp`
    ///
    /// # Arguments
    ///
    /// * `op` - The SSA comparison operation to analyze (`Ceq`, `Clt`, or `Cgt`).
    /// * `cache` - Definition cache for resolving operand definitions.
    ///
    /// # Returns
    ///
    /// `Some(ComparisonSimplification)` if the comparison can be algebraically simplified,
    /// `None` if no simplification applies or the operation is not a comparison.
    fn analyze_comparison_simplification(
        op: &SsaOp,
        cache: &DefinitionCache,
    ) -> Option<ComparisonSimplification> {
        match op {
            SsaOp::Ceq { dest, left, right } => {
                Self::analyze_ceq_simplification(*dest, *left, *right, cache)
            }
            SsaOp::Clt {
                dest,
                left,
                right,
                unsigned,
            } => Self::analyze_clt_simplification(*dest, *left, *right, *unsigned, cache),
            SsaOp::Cgt {
                dest,
                left,
                right,
                unsigned,
            } => Self::analyze_cgt_simplification(*dest, *left, *right, *unsigned, cache),
            _ => None,
        }
    }

    /// Checks if a variable is defined as a constant zero.
    fn is_zero_var(var: SsaVarId, cache: &DefinitionCache) -> bool {
        cache
            .get_definition(var)
            .is_some_and(Self::is_zero_constant)
    }

    /// Checks if a variable is defined as a constant with value 1.
    fn is_one_var(var: SsaVarId, cache: &DefinitionCache) -> bool {
        cache.get_definition(var).is_some_and(Self::is_one_constant)
    }

    /// Analyzes a `Ceq` operation for algebraic simplification.
    ///
    /// Detects three patterns:
    /// - `(x - y) == 0` simplifies to `x == y` (subtraction-zero, skip self-subtraction).
    /// - `(x ^ y) == 0` simplifies to `x == y` (XOR-zero, skip self-XOR).
    /// - `(cmp) == 1` simplifies to `Copy(cmp)` when the other operand is a `Ceq`/`Clt`/`Cgt`
    ///   result, since CIL comparisons already produce 0 or 1.
    ///
    /// # Arguments
    ///
    /// * `dest` - Destination variable of the `Ceq` (preserved in the simplified op).
    /// * `left` - Left operand of the `Ceq`.
    /// * `right` - Right operand of the `Ceq`.
    /// * `cache` - Definition cache for resolving operand definitions.
    ///
    /// # Returns
    ///
    /// `Some(ComparisonSimplification)` if a simplification pattern matches, `None` otherwise.
    fn analyze_ceq_simplification(
        dest: SsaVarId,
        left: SsaVarId,
        right: SsaVarId,
        cache: &DefinitionCache,
    ) -> Option<ComparisonSimplification> {
        // Check if comparing to zero
        let (other_var, is_comparing_to_zero) = if Self::is_zero_var(right, cache) {
            (left, true)
        } else if Self::is_zero_var(left, cache) {
            (right, true)
        } else {
            (left, false)
        };

        if is_comparing_to_zero {
            if let Some(def_op) = cache.get_definition(other_var) {
                // Pattern: (x - y) == 0 → x == y
                if let SsaOp::Sub {
                    left: sub_left,
                    right: sub_right,
                    ..
                } = def_op
                {
                    // Skip self-subtraction - that's handled by PredicateResult (always true)
                    if sub_left != sub_right {
                        return Some(ComparisonSimplification::SimplerOp {
                            new_op: SsaOp::Ceq {
                                dest,
                                left: *sub_left,
                                right: *sub_right,
                            },
                            reason: "(x - y) == 0 simplified to x == y",
                        });
                    }
                }

                // Pattern: (x ^ y) == 0 → x == y
                if let SsaOp::Xor {
                    left: xor_left,
                    right: xor_right,
                    ..
                } = def_op
                {
                    // Skip self-XOR - that's handled by PredicateResult (always true)
                    if xor_left != xor_right {
                        return Some(ComparisonSimplification::SimplerOp {
                            new_op: SsaOp::Ceq {
                                dest,
                                left: *xor_left,
                                right: *xor_right,
                            },
                            reason: "(x ^ y) == 0 simplified to x == y",
                        });
                    }
                }
            }
        }

        // Check if comparing to one (true in CIL)
        let (other_var, is_comparing_to_one) = if Self::is_one_var(right, cache) {
            (left, true)
        } else if Self::is_one_var(left, cache) {
            (right, true)
        } else {
            (left, false)
        };

        if is_comparing_to_one {
            if let Some(def_op) = cache.get_definition(other_var) {
                // Pattern: (cmp) == 1 → copy cmp
                if matches!(
                    def_op,
                    SsaOp::Ceq { .. } | SsaOp::Clt { .. } | SsaOp::Cgt { .. }
                ) {
                    return Some(ComparisonSimplification::Copy {
                        dest,
                        src: other_var,
                        reason: "(cmp) == 1 simplified to cmp",
                    });
                }
            }
        }

        None
    }

    /// Analyzes a `Clt` operation for algebraic simplification.
    ///
    /// Detects `(x - y) < 0` and simplifies to `x < y` (signed only; unsigned subtraction
    /// has different overflow semantics). Self-subtraction is skipped since it is handled
    /// as a constant predicate (`AlwaysFalse`).
    ///
    /// # Arguments
    ///
    /// * `dest` - Destination variable of the `Clt` (preserved in the simplified op).
    /// * `left` - Left operand of the `Clt`.
    /// * `right` - Right operand of the `Clt`.
    /// * `unsigned` - Whether this is an unsigned comparison; if `true`, no simplification is attempted.
    /// * `cache` - Definition cache for resolving operand definitions.
    ///
    /// # Returns
    ///
    /// `Some(ComparisonSimplification::SimplerOp)` if `(x - y) < 0` is detected, `None` otherwise.
    fn analyze_clt_simplification(
        dest: SsaVarId,
        left: SsaVarId,
        right: SsaVarId,
        unsigned: bool,
        cache: &DefinitionCache,
    ) -> Option<ComparisonSimplification> {
        // Only handle signed comparisons for subtraction patterns
        // (unsigned subtraction has different overflow semantics)
        if unsigned {
            return None;
        }

        // Pattern: (x - y) < 0 → x < y
        if Self::is_zero_var(right, cache) {
            if let Some(SsaOp::Sub {
                left: sub_left,
                right: sub_right,
                ..
            }) = cache.get_definition(left)
            {
                // Skip self-subtraction - that's handled by PredicateResult (always false)
                if sub_left != sub_right {
                    return Some(ComparisonSimplification::SimplerOp {
                        new_op: SsaOp::Clt {
                            dest,
                            left: *sub_left,
                            right: *sub_right,
                            unsigned,
                        },
                        reason: "(x - y) < 0 simplified to x < y",
                    });
                }
            }
        }

        None
    }

    /// Analyzes a `Cgt` operation for algebraic simplification.
    ///
    /// Detects `(x - y) > 0` and simplifies to `x > y` (signed only; unsigned subtraction
    /// has different overflow semantics). Self-subtraction is skipped since it is handled
    /// as a constant predicate (`AlwaysFalse`).
    ///
    /// # Arguments
    ///
    /// * `dest` - Destination variable of the `Cgt` (preserved in the simplified op).
    /// * `left` - Left operand of the `Cgt`.
    /// * `right` - Right operand of the `Cgt`.
    /// * `unsigned` - Whether this is an unsigned comparison; if `true`, no simplification is attempted.
    /// * `cache` - Definition cache for resolving operand definitions.
    ///
    /// # Returns
    ///
    /// `Some(ComparisonSimplification::SimplerOp)` if `(x - y) > 0` is detected, `None` otherwise.
    fn analyze_cgt_simplification(
        dest: SsaVarId,
        left: SsaVarId,
        right: SsaVarId,
        unsigned: bool,
        cache: &DefinitionCache,
    ) -> Option<ComparisonSimplification> {
        // Only handle signed comparisons for subtraction patterns
        if unsigned {
            return None;
        }

        // Pattern: (x - y) > 0 → x > y
        if Self::is_zero_var(right, cache) {
            if let Some(SsaOp::Sub {
                left: sub_left,
                right: sub_right,
                ..
            }) = cache.get_definition(left)
            {
                // Skip self-subtraction - that's handled by PredicateResult (always false)
                if sub_left != sub_right {
                    return Some(ComparisonSimplification::SimplerOp {
                        new_op: SsaOp::Cgt {
                            dest,
                            left: *sub_left,
                            right: *sub_right,
                            unsigned,
                        },
                        reason: "(x - y) > 0 simplified to x > y",
                    });
                }
            }
        }

        None
    }

    /// Fallback evaluator for branch conditions that pattern matching cannot resolve.
    ///
    /// Creates an [`SsaEvaluator`] and runs a forward pass over all blocks from 0 through
    /// `block_idx`, accumulating concrete values via dataflow propagation. If the condition
    /// variable resolves to a constant after evaluation, returns `AlwaysTrue` (non-zero) or
    /// `AlwaysFalse` (zero).
    ///
    /// This catches predicates that require multi-step constant propagation across blocks
    /// (e.g., a value computed in block 0 that flows through assignments to block 5's branch).
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function to evaluate.
    /// * `condition` - The branch condition variable to resolve.
    /// * `block_idx` - The block containing the branch (evaluation covers blocks 0..=block_idx).
    /// * `ptr_size` - Pointer size for the evaluator (affects address arithmetic).
    ///
    /// # Returns
    ///
    /// [`PredicateResult::AlwaysTrue`] or [`AlwaysFalse`](PredicateResult::AlwaysFalse) if the
    /// evaluator resolves the condition to a constant. [`Unknown`](PredicateResult::Unknown) if
    /// the value depends on runtime inputs, loops, or unresolvable phi operands.
    fn evaluate_with_tracked(
        ssa: &SsaFunction,
        condition: SsaVarId,
        block_idx: usize,
        ptr_size: PointerSize,
    ) -> PredicateResult {
        let mut evaluator = SsaEvaluator::new(ssa, ptr_size);

        // Evaluate all blocks up to and including the current block.
        // We use a simple forward pass - in complex cases with loops,
        // this may not capture all values, but it handles linear flows.
        for idx in 0..=block_idx {
            // For blocks that precede our target, we can evaluate them
            // to build up the value state
            evaluator.evaluate_block(idx);
        }

        // Check if we have a concrete value for the condition
        match evaluator.get(condition) {
            Some(expr) if expr.is_constant() => {
                if expr.as_constant().is_some_and(ConstValue::is_zero) {
                    PredicateResult::AlwaysFalse
                } else {
                    PredicateResult::AlwaysTrue
                }
            }
            Some(_) | None => PredicateResult::Unknown,
        }
    }

    /// Detects phi nodes where every operand resolves to the same constant value.
    ///
    /// Iterates over all phi nodes in all blocks. For each phi, looks up each operand's
    /// defining operation: if all are `Const` with identical values, records the mapping
    /// from the phi result variable to that constant. These entries are later used to
    /// replace the phi with a `Const` instruction and to resolve branch conditions that
    /// depend on phi-defined variables.
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function whose phi nodes are analyzed.
    ///
    /// # Returns
    ///
    /// A map from phi result variable to the constant value that all operands agree on.
    /// Empty if no phi nodes have all-constant, all-identical operands.
    fn analyze_phi_constants(ssa: &SsaFunction) -> BTreeMap<SsaVarId, ConstValue> {
        let mut phi_constants = BTreeMap::new();

        for block in ssa.blocks() {
            for phi in block.phi_nodes() {
                let operands: Vec<_> = phi.operands().iter().collect();
                if operands.is_empty() {
                    continue;
                }

                // Check if all operands come from the same constant
                let first_val = operands[0].value();
                let mut all_same_const = true;
                let mut const_value = None;

                for operand in &operands {
                    let var = operand.value();
                    // Look up the definition
                    if let Some(op) = ssa.get_definition(var) {
                        if let SsaOp::Const { value, .. } = op {
                            if const_value.is_none() {
                                const_value = Some(value.clone());
                            } else if const_value.as_ref() != Some(value) {
                                all_same_const = false;
                                break;
                            }
                        } else {
                            all_same_const = false;
                            break;
                        }
                    } else if var != first_val {
                        all_same_const = false;
                        break;
                    }
                }

                if all_same_const {
                    if let Some(value) = const_value {
                        phi_constants.insert(phi.result(), value);
                    }
                }
            }
        }

        phi_constants
    }
}

impl SsaPass for OpaquePredicatePass {
    fn name(&self) -> &'static str {
        "opaque-predicate-removal"
    }

    fn description(&self) -> &'static str {
        "Detects and removes opaque predicates (always-true/false conditions)"
    }

    fn provides(&self) -> &[PassCapability] {
        &[PassCapability::SimplifiedPredicates]
    }

    fn requires(&self) -> &[PassCapability] {
        &[PassCapability::RestoredControlFlow]
    }

    /// Runs opaque predicate detection and removal on a single method.
    ///
    /// Operates in two phases:
    ///
    /// **Collection phase** (read-only scan of all blocks):
    /// 1. **Branch simplifications**: for each `Branch` terminator, checks phi-constants first,
    ///    then pattern-matching via [`analyze_branch`](OpaquePredicatePass::analyze_branch), then
    ///    [`evaluate_with_tracked`](OpaquePredicatePass::evaluate_with_tracked) as a dataflow fallback.
    /// 2. **Comparison replacements**: comparison instructions (`Ceq`/`Clt`/`Cgt`) that are
    ///    opaque predicates get replaced with `Const(true)` or `Const(false)`.
    /// 3. **Comparison simplifications**: algebraic rewrites like `(x-y)==0` to `x==y`.
    /// 4. **Phi replacements**: phi nodes where all operands are the same constant.
    ///
    /// **Apply phase** (mutates the SSA in collected order):
    /// - Branch terminators become `Jump` to the always-taken target.
    /// - Comparison instructions become `Const` or `Copy` operations.
    /// - Phi nodes become `Const` instructions (inserted at block start, phi removed).
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function to analyze and mutate.
    /// * `method_token` - Token of the method (for event logging).
    /// * `ctx` - Compiler context for event recording.
    /// * `assembly` - Assembly reference for pointer size detection (used by `SsaEvaluator`).
    ///
    /// # Returns
    ///
    /// `Ok(true)` if structural changes were made (branches simplified, comparisons resolved).
    /// `Ok(false)` if only phi-only changes occurred or no changes at all. Phi-only changes
    /// return `false` to avoid triggering an immediate SSA rebuild that would recreate the phi.
    ///
    /// # Errors
    ///
    /// Returns an error if SSA mutation fails (should not occur in practice).
    fn run_on_method(
        &self,
        ssa: &mut SsaFunction,
        method_token: Token,
        ctx: &CompilerContext,
        assembly: &CilObject,
    ) -> Result<bool> {
        let changes = EventLog::new();

        // Build definition cache for efficient lookup
        let cache = DefinitionCache::build(ssa);

        // Analyze phi nodes for constant values
        let phi_constants = Self::analyze_phi_constants(ssa);

        // Collect branches to simplify
        let mut branch_simplifications: Vec<(usize, usize, bool)> = Vec::new();

        // Collect comparison replacements (opaque predicates that become constant true/false)
        let mut comparison_replacements: Vec<(usize, usize, SsaVarId, bool)> = Vec::new();

        // Collect comparison simplifications (algebraic simplifications like (x-y)==0 → x==y)
        let mut comparison_simplifications: Vec<(usize, usize, ComparisonSimplification)> =
            Vec::new();

        // Collect phi replacements
        let mut phi_replacements: Vec<(usize, usize, SsaVarId, ConstValue)> = Vec::new();

        // Analyze each block
        for (block_idx, block) in ssa.iter_blocks() {
            // Analyze branch terminators
            if let Some(SsaOp::Branch {
                condition,
                true_target,
                false_target,
            }) = block.terminator_op()
            {
                // Check phi constants first
                if let Some(const_val) = phi_constants.get(condition) {
                    let is_true = const_val.as_bool().unwrap_or(false)
                        || const_val.as_i64().is_some_and(|v| v != 0);
                    if is_true {
                        branch_simplifications.push((block_idx, *true_target, true));
                    } else {
                        branch_simplifications.push((block_idx, *false_target, false));
                    }
                    // Can't use continue with iter_blocks in a for loop, collect the data
                } else {
                    let mut result = Self::analyze_branch(*condition, &cache);

                    // If pattern matching couldn't determine the result,
                    // try using SsaEvaluator for dataflow-based analysis
                    if result == PredicateResult::Unknown {
                        let ptr_size = PointerSize::from_pe(assembly.file().pe().is_64bit);
                        result = Self::evaluate_with_tracked(ssa, *condition, block_idx, ptr_size);
                    }

                    match result {
                        PredicateResult::AlwaysTrue => {
                            branch_simplifications.push((block_idx, *true_target, true));
                        }
                        PredicateResult::AlwaysFalse => {
                            branch_simplifications.push((block_idx, *false_target, false));
                        }
                        PredicateResult::Unknown => {}
                    }
                }
            }

            // Analyze comparison instructions
            for (instr_idx, instr) in block.instructions().iter().enumerate() {
                let op = instr.op();
                // First check for opaque predicates (constant true/false)
                let result = Self::analyze_predicate_with_cache(op, &cache, 0);
                if let Some(value) = result.as_bool() {
                    if let Some(dest) = op.dest() {
                        comparison_replacements.push((block_idx, instr_idx, dest, value));
                        continue; // Don't also check for simplification
                    }
                }

                // Then check for algebraic simplifications
                if let Some(simplification) = Self::analyze_comparison_simplification(op, &cache) {
                    comparison_simplifications.push((block_idx, instr_idx, simplification));
                }
            }

            // Check for phi nodes that can be replaced with constants
            for (phi_idx, phi) in block.phi_nodes().iter().enumerate() {
                if let Some(const_val) = phi_constants.get(&phi.result()) {
                    phi_replacements.push((block_idx, phi_idx, phi.result(), const_val.clone()));
                }
            }
        }

        // Track structural changes (branches, comparisons) vs phi-only changes.
        // Phi constant replacement doesn't warrant rebuild_ssa (see comment below).
        let has_structural = !branch_simplifications.is_empty()
            || !comparison_replacements.is_empty()
            || !comparison_simplifications.is_empty();

        // Apply branch simplifications
        for (block_idx, target, is_true) in branch_simplifications {
            if let Some(block) = ssa.block_mut(block_idx) {
                if let Some(last_instr) = block.instructions_mut().last_mut() {
                    last_instr.set_op(SsaOp::Jump { target });
                    changes
                        .record(EventKind::OpaquePredicateRemoved)
                        .at(method_token, block_idx)
                        .message(format!(
                            "removed opaque predicate (always {})",
                            if is_true { "true" } else { "false" }
                        ));
                    changes
                        .record(EventKind::BranchSimplified)
                        .at(method_token, block_idx)
                        .message(format!("simplified to unconditional branch to {target}"));
                }
            }
        }

        // Apply comparison replacements (opaque predicates → constant true/false)
        for (block_idx, instr_idx, dest, value) in comparison_replacements {
            if let Some(block) = ssa.block_mut(block_idx) {
                let const_value = if value {
                    ConstValue::True
                } else {
                    ConstValue::False
                };
                block.instructions_mut()[instr_idx].set_op(SsaOp::Const {
                    dest,
                    value: const_value,
                });
                changes
                    .record(EventKind::ConstantFolded)
                    .at(method_token, instr_idx)
                    .message(format!("opaque predicate → {value}"));
            }
        }

        // Apply comparison simplifications (algebraic transformations)
        for (block_idx, instr_idx, simplification) in comparison_simplifications {
            if let Some(block) = ssa.block_mut(block_idx) {
                match simplification {
                    ComparisonSimplification::SimplerOp { new_op, reason } => {
                        block.instructions_mut()[instr_idx].set_op(new_op);
                        changes
                            .record(EventKind::ConstantFolded)
                            .at(method_token, instr_idx)
                            .message(reason);
                    }
                    ComparisonSimplification::Copy { dest, src, reason } => {
                        block.instructions_mut()[instr_idx].set_op(SsaOp::Copy { dest, src });
                        changes
                            .record(EventKind::ConstantFolded)
                            .at(method_token, instr_idx)
                            .message(reason);
                    }
                }
            }
        }

        // Apply phi replacements: PHIs where all operands are the same constant
        // We replace the PHI with a constant instruction and remove the PHI.
        // Process in reverse order to handle phi_idx correctly when removing.
        let mut phi_removals: Vec<(usize, usize)> = Vec::new();
        for (block_idx, phi_idx, phi_result, const_value) in phi_replacements {
            // Create a constant instruction with the same destination as the PHI
            let const_instr = SsaInstruction::synthetic(SsaOp::Const {
                dest: phi_result,
                value: const_value.clone(),
            });

            // Insert at the beginning of the block's instructions
            if let Some(block) = ssa.block_mut(block_idx) {
                block.instructions_mut().insert(0, const_instr);
            }

            // Mark this phi for removal
            phi_removals.push((block_idx, phi_idx));

            changes
                .record(EventKind::ConstantFolded)
                .at(method_token, block_idx)
                .message(format!("phi with constant operands → {const_value:?}"));
        }

        // Remove the PHIs (in reverse order to maintain correct indices)
        phi_removals.sort_by(|a, b| b.cmp(a)); // Sort descending by (block_idx, phi_idx)
        for (block_idx, phi_idx) in phi_removals {
            if let Some(block) = ssa.block_mut(block_idx) {
                if phi_idx < block.phi_nodes().len() {
                    block.phi_nodes_mut().remove(phi_idx);
                }
            }
        }

        let changed = !changes.is_empty();
        if changed {
            ctx.events.merge(&changes);
        }

        // Only report structural changes for rebuild_ssa. Phi constant
        // replacement is a valid transformation, but triggering rebuild
        // immediately undoes it: rebuild re-creates the phi from the original
        // variable definitions that still exist in other blocks. By not
        // triggering rebuild for phi-only changes, DCE can first clean up
        // the now-dead original definitions. When rebuild eventually runs
        // (triggered by a structural change), the original defs are gone
        // and the phi won't be recreated.
        Ok(has_structural)
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        analysis::{ConstValue, MethodRef, SsaFunctionBuilder, SsaOp, SsaVarId, ValueRange},
        compiler::passes::predicates::{DefinitionCache, OpaquePredicatePass, PredicateResult},
        metadata::token::Token,
    };

    #[test]
    fn test_predicate_result() {
        assert_eq!(PredicateResult::AlwaysTrue.as_bool(), Some(true));
        assert_eq!(PredicateResult::AlwaysFalse.as_bool(), Some(false));
        assert_eq!(PredicateResult::Unknown.as_bool(), None);

        assert_eq!(
            PredicateResult::AlwaysTrue.negate(),
            PredicateResult::AlwaysFalse
        );
        assert_eq!(
            PredicateResult::AlwaysFalse.negate(),
            PredicateResult::AlwaysTrue
        );
        assert_eq!(PredicateResult::Unknown.negate(), PredicateResult::Unknown);
    }

    #[test]
    fn test_self_equality() {
        let (ssa, v0, v1) = {
            let mut v0_out = SsaVarId::from_index(0);
            let mut v1_out = SsaVarId::from_index(1);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        let v = b.const_i32(42);
                        v0_out = v;
                        v1_out = b.ceq(v, v); // v1 = ceq v0, v0 (always true)
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v0_out, v1_out)
        };

        let cache = DefinitionCache::build(&ssa);
        let op = SsaOp::Ceq {
            dest: v1,
            left: v0,
            right: v0,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysTrue
        );
    }

    #[test]
    fn test_self_less_than() {
        let (ssa, v0, v1) = {
            let mut v0_out = SsaVarId::from_index(0);
            let mut v1_out = SsaVarId::from_index(1);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        let v = b.const_i32(42);
                        v0_out = v;
                        v1_out = b.clt(v, v);
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v0_out, v1_out)
        };

        let cache = DefinitionCache::build(&ssa);

        // x < x is always false
        let op = SsaOp::Clt {
            dest: v1,
            left: v0,
            right: v0,
            unsigned: false,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysFalse
        );
    }

    #[test]
    fn test_xor_self_equals_zero() {
        let (ssa, v1, v2, v3) = {
            let mut v1_out = SsaVarId::from_index(0);
            let mut v2_out = SsaVarId::from_index(1);
            let mut v3_out = SsaVarId::from_index(2);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        let v0 = b.const_i32(42);
                        let v1 = b.xor(v0, v0); // v1 = v0 ^ v0 (always 0)
                        v1_out = v1;
                        let v2 = b.const_i32(0);
                        v2_out = v2;
                        v3_out = b.ceq(v1, v2); // v3 = ceq v1, v2 (always true)
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v1_out, v2_out, v3_out)
        };

        let cache = DefinitionCache::build(&ssa);
        let op = SsaOp::Ceq {
            dest: v3,
            left: v1,
            right: v2,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysTrue
        );
    }

    #[test]
    fn test_constant_comparison() {
        let (ssa, v0, v1, v2) = {
            let mut v0_out = SsaVarId::from_index(0);
            let mut v1_out = SsaVarId::from_index(1);
            let mut v2_out = SsaVarId::from_index(2);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        let v0 = b.const_i32(5);
                        v0_out = v0;
                        let v1 = b.const_i32(10);
                        v1_out = v1;
                        v2_out = b.clt(v0, v1);
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v0_out, v1_out, v2_out)
        };

        let cache = DefinitionCache::build(&ssa);

        // 5 < 10 is always true
        let op = SsaOp::Clt {
            dest: v2,
            left: v0,
            right: v1,
            unsigned: false,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysTrue
        );

        // 5 > 10 is always false
        let op = SsaOp::Cgt {
            dest: v2,
            left: v0,
            right: v1,
            unsigned: false,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysFalse
        );
    }

    #[test]
    fn test_unsigned_comparison() {
        let (ssa, v0, v1) = {
            let mut v0_out = SsaVarId::from_index(0);
            let mut v1_out = SsaVarId::from_index(1);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        let v0 = b.const_val(ConstValue::U32(5));
                        v0_out = v0;
                        let v1 = b.const_val(ConstValue::U32(0));
                        v1_out = v1;
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v0_out, v1_out)
        };

        let cache = DefinitionCache::build(&ssa);

        // unsigned x < 0 is always false
        let dest = SsaVarId::from_index(2);
        let op = SsaOp::Clt {
            dest,
            left: v0,
            right: v1,
            unsigned: true,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysFalse
        );
    }

    #[test]
    fn test_newobj_non_null() {
        let (ssa, v0, v1) = {
            let mut v0_out = SsaVarId::from_index(0);
            let mut v1_out = SsaVarId::from_index(1);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        // v0 = newobj (always non-null)
                        let v0 = b.newobj(MethodRef::new(Token::new(0x06000001)), &[]);
                        v0_out = v0;
                        // v1 = null
                        let v1 = b.const_null();
                        v1_out = v1;
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v0_out, v1_out)
        };

        let cache = DefinitionCache::build(&ssa);

        // newobj result == null is always false
        let dest = SsaVarId::from_index(2);
        let op = SsaOp::Ceq {
            dest,
            left: v0,
            right: v1,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysFalse
        );
    }

    #[test]
    fn test_array_length_non_negative() {
        let (ssa, v1, v2) = {
            let mut v1_out = SsaVarId::from_index(0);
            let mut v2_out = SsaVarId::from_index(1);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        // v0 = some array (placeholder)
                        let v0 = b.const_null();
                        // v1 = array.Length (always >= 0)
                        let v1 = b.array_length(v0);
                        v1_out = v1;
                        // v2 = 0
                        let v2 = b.const_i32(0);
                        v2_out = v2;
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v1_out, v2_out)
        };

        let cache = DefinitionCache::build(&ssa);

        // array.Length < 0 is always false
        let dest = SsaVarId::from_index(2);
        let op = SsaOp::Clt {
            dest,
            left: v1,
            right: v2,
            unsigned: false,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysFalse
        );
    }

    #[test]
    fn test_multiply_by_zero() {
        let (ssa, v1, v2) = {
            let mut v1_out = SsaVarId::from_index(0);
            let mut v2_out = SsaVarId::from_index(1);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        let v0 = b.const_i32(42);
                        let v1 = b.const_i32(0);
                        v1_out = v1;
                        let v2 = b.mul(v0, v1); // v2 = v0 * v1 (always 0)
                        v2_out = v2;
                        let _ = b.ceq(v2, v1); // v3 = ceq v2, v1 (always true)
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v1_out, v2_out)
        };

        let cache = DefinitionCache::build(&ssa);
        let dest = SsaVarId::from_index(2);
        let op = SsaOp::Ceq {
            dest,
            left: v2,
            right: v1,
        };

        assert_eq!(
            OpaquePredicatePass::analyze_predicate_with_cache(&op, &cache, 0),
            PredicateResult::AlwaysTrue
        );
    }

    #[test]
    fn test_value_range() {
        let range = ValueRange::constant(5);
        assert_eq!(range.always_less_than(10), Some(true));
        assert_eq!(range.always_less_than(5), Some(false));
        assert_eq!(range.always_less_than(3), Some(false));

        assert_eq!(range.always_greater_than(3), Some(true));
        assert_eq!(range.always_greater_than(5), Some(false));
        assert_eq!(range.always_greater_than(10), Some(false));

        assert_eq!(range.always_equal_to(5), Some(true));
        assert_eq!(range.always_equal_to(3), Some(false));

        let non_neg = ValueRange::non_negative();
        assert!(non_neg.is_always_non_negative());
        assert_eq!(non_neg.always_less_than(0), Some(false));
    }

    #[test]
    fn test_consecutive_pair_detection() {
        let (ssa, v0, v2) = {
            let mut v0_out = SsaVarId::from_index(0);
            let mut v2_out = SsaVarId::from_index(1);
            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        let v0 = b.const_i32(5);
                        v0_out = v0;
                        let v1 = b.const_i32(1);
                        let v2 = b.add(v0, v1); // v2 = v0 + v1 (x + 1)
                        v2_out = v2;
                        b.ret();
                    });
                })
                .unwrap();
            (ssa, v0_out, v2_out)
        };

        let cache = DefinitionCache::build(&ssa);

        // v0 and v2 should be detected as consecutive pair (x and x+1)
        assert!(OpaquePredicatePass::is_consecutive_pair(v0, v2, &cache));
    }

    #[test]
    fn test_phi_constant_analysis() {
        let (ssa, phi_var) = {
            let mut c0_out = SsaVarId::from_index(0);
            let mut c1_out = SsaVarId::from_index(1);
            let mut phi_out = SsaVarId::from_index(2);

            let ssa = SsaFunctionBuilder::new(0, 0)
                .build_with(|f| {
                    f.block(0, |b| {
                        c0_out = b.const_i32(42);
                        b.jump(2);
                    });
                    f.block(1, |b| {
                        c1_out = b.const_i32(42); // Same constant
                        b.jump(2);
                    });
                    f.block(2, |b| {
                        phi_out = b.phi(&[(0, c0_out), (1, c1_out)]);
                    });
                })
                .unwrap();

            (ssa, phi_out)
        };

        let phi_constants = OpaquePredicatePass::analyze_phi_constants(&ssa);

        // The phi should be recognized as constant since both operands are 42
        assert!(phi_constants.contains_key(&phi_var));
        assert_eq!(phi_constants.get(&phi_var), Some(&ConstValue::I32(42)));
    }
}