dotscope 0.6.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
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
//! Value tracking for SSA variables.
//!
//! This module provides abstract value representation for SSA variables,
//! enabling constant propagation, value numbering, and range analysis.
//!
//! # Lattice Structure
//!
//! The `AbstractValue` type forms a lattice for dataflow analysis:
//!
//! ```text
//!              Top (no information)
//!               |
//!     +---------+---------+
//!     |         |         |
//!   Const    Range     NonNull
//!     |         |         |
//!     +---------+---------+
//!               |
//!            Bottom (conflicting info)
//! ```
//!
//! - `Top`: No information known yet (initial state)
//! - `Constant`: Known compile-time constant
//! - `Range`: Value in a bounded range
//! - `NonNull`: Known to be non-null (for references)
//! - `Bottom`: Multiple conflicting values (cannot be constant)

use std::fmt;

use crate::{
    analysis::ssa::{
        types::{FieldRef, MethodRef, TypeRef},
        SsaType, SsaVarId,
    },
    assembly::Immediate,
    metadata::typesystem::PointerSize,
    Error,
};

/// Constant values that can appear in SSA form.
///
/// These represent compile-time constants that can be tracked through
/// the SSA graph for constant propagation and folding.
#[derive(Debug, Clone, PartialEq)]
pub enum ConstValue {
    /// 8-bit signed integer.
    I8(i8),

    /// 16-bit signed integer.
    I16(i16),

    /// 32-bit signed integer.
    I32(i32),

    /// 64-bit signed integer.
    I64(i64),

    /// 8-bit unsigned integer.
    U8(u8),

    /// 16-bit unsigned integer.
    U16(u16),

    /// 32-bit unsigned integer.
    U32(u32),

    /// 64-bit unsigned integer.
    U64(u64),

    /// Native integer (pointer-sized).
    NativeInt(i64),

    /// Native unsigned integer (pointer-sized).
    NativeUInt(u64),

    /// 32-bit floating point.
    F32(f32),

    /// 64-bit floating point.
    F64(f64),

    /// String constant (index into #US heap).
    String(u32),

    /// Decrypted string value (actual string content, not a heap index).
    /// Used by deobfuscation passes to store strings that were decrypted at analysis time.
    DecryptedString(String),

    /// Null reference.
    Null,

    /// Boolean true.
    True,

    /// Boolean false.
    False,

    /// Runtime type handle (typeof result).
    Type(TypeRef),

    /// Runtime method handle.
    MethodHandle(MethodRef),

    /// Runtime field handle.
    FieldHandle(FieldRef),
}

impl ConstValue {
    /// Returns `true` if this is the null constant.
    #[must_use]
    pub const fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }

    /// Returns `true` if this is a boolean constant.
    #[must_use]
    pub const fn is_bool(&self) -> bool {
        matches!(self, Self::True | Self::False)
    }

    /// Returns `true` if this is an integer constant (signed or unsigned).
    #[must_use]
    pub const fn is_integer(&self) -> bool {
        matches!(
            self,
            Self::I8(_)
                | Self::I16(_)
                | Self::I32(_)
                | Self::I64(_)
                | Self::U8(_)
                | Self::U16(_)
                | Self::U32(_)
                | Self::U64(_)
                | Self::NativeInt(_)
                | Self::NativeUInt(_)
        )
    }

    /// Returns `true` if this is a signed integer constant.
    #[must_use]
    pub const fn is_signed(&self) -> bool {
        matches!(
            self,
            Self::I8(_) | Self::I16(_) | Self::I32(_) | Self::I64(_) | Self::NativeInt(_)
        )
    }

    /// Returns `true` if this is an unsigned integer constant.
    #[must_use]
    pub const fn is_unsigned(&self) -> bool {
        matches!(
            self,
            Self::U8(_) | Self::U16(_) | Self::U32(_) | Self::U64(_) | Self::NativeUInt(_)
        )
    }

    /// Returns `true` if this is a floating-point constant.
    #[must_use]
    pub const fn is_float(&self) -> bool {
        matches!(self, Self::F32(_) | Self::F64(_))
    }

    /// Returns the SSA type corresponding to this constant value.
    #[must_use]
    pub const fn ssa_type(&self) -> SsaType {
        match self {
            Self::I8(_) => SsaType::I8,
            Self::I16(_) => SsaType::I16,
            Self::I32(_) => SsaType::I32,
            Self::I64(_) => SsaType::I64,
            Self::U8(_) => SsaType::U8,
            Self::U16(_) => SsaType::U16,
            Self::U32(_) => SsaType::U32,
            Self::U64(_) => SsaType::U64,
            Self::F32(_) => SsaType::F32,
            Self::F64(_) => SsaType::F64,
            Self::NativeInt(_) | Self::Type(_) | Self::MethodHandle(_) | Self::FieldHandle(_) => {
                SsaType::NativeInt
            }
            Self::NativeUInt(_) => SsaType::NativeUInt,
            Self::True | Self::False => SsaType::Bool,
            Self::Null | Self::String(_) | Self::DecryptedString(_) => SsaType::Object,
        }
    }

    /// Returns the constant as an i32 if applicable.
    #[must_use]
    pub const fn as_i32(&self) -> Option<i32> {
        match self {
            Self::I8(v) => Some(*v as i32),
            Self::I16(v) => Some(*v as i32),
            Self::I32(v) => Some(*v),
            Self::U8(v) => Some(*v as i32),
            Self::U16(v) => Some(*v as i32),
            Self::True => Some(1),
            Self::False => Some(0),
            _ => None,
        }
    }

    /// Returns the constant as an i64 if applicable.
    #[must_use]
    #[allow(clippy::match_same_arms)] // NativeInt is semantically different from I64
    pub const fn as_i64(&self) -> Option<i64> {
        match self {
            Self::I8(v) => Some(*v as i64),
            Self::I16(v) => Some(*v as i64),
            Self::I32(v) => Some(*v as i64),
            Self::I64(v) => Some(*v),
            Self::U8(v) => Some(*v as i64),
            Self::U16(v) => Some(*v as i64),
            Self::U32(v) => Some(*v as i64),
            Self::NativeInt(v) => Some(*v),
            Self::True => Some(1),
            Self::False => Some(0),
            _ => None,
        }
    }

    /// Returns the constant as a u64 if applicable (for unsigned operations).
    #[must_use]
    #[allow(clippy::cast_sign_loss)] // Guarded by >= 0 checks
    #[allow(clippy::match_same_arms)] // NativeUInt is semantically different from U64
    pub const fn as_u64(&self) -> Option<u64> {
        match self {
            Self::U8(v) => Some(*v as u64),
            Self::U16(v) => Some(*v as u64),
            Self::U32(v) => Some(*v as u64),
            Self::U64(v) => Some(*v),
            Self::NativeUInt(v) => Some(*v),
            Self::I8(v) if *v >= 0 => Some(*v as u64),
            Self::I16(v) if *v >= 0 => Some(*v as u64),
            Self::I32(v) if *v >= 0 => Some(*v as u64),
            Self::I64(v) if *v >= 0 => Some(*v as u64),
            Self::True => Some(1),
            Self::False => Some(0),
            _ => None,
        }
    }

    /// Returns the constant as a u32 if applicable (for unsigned operations).
    #[must_use]
    #[allow(clippy::cast_sign_loss)] // Guarded by >= 0 checks
    pub const fn as_u32(&self) -> Option<u32> {
        match self {
            Self::U8(v) => Some(*v as u32),
            Self::U16(v) => Some(*v as u32),
            Self::U32(v) => Some(*v),
            Self::I8(v) if *v >= 0 => Some(*v as u32),
            Self::I16(v) if *v >= 0 => Some(*v as u32),
            Self::I32(v) if *v >= 0 => Some(*v as u32),
            Self::True => Some(1),
            Self::False => Some(0),
            _ => None,
        }
    }

    /// Returns the constant as an f32 if it's stored as F32.
    #[must_use]
    pub const fn as_f32(&self) -> Option<f32> {
        match self {
            Self::F32(v) => Some(*v),
            _ => None,
        }
    }

    /// Returns the constant as an f64 if it's stored as F64.
    #[must_use]
    pub const fn as_f64(&self) -> Option<f64> {
        match self {
            Self::F64(v) => Some(*v),
            _ => None,
        }
    }

    /// Returns the constant as a bool if applicable.
    #[must_use]
    pub const fn as_bool(&self) -> Option<bool> {
        match self {
            Self::False
            | Self::Null
            | Self::I8(0)
            | Self::I16(0)
            | Self::I32(0)
            | Self::I64(0)
            | Self::U8(0)
            | Self::U16(0)
            | Self::U32(0)
            | Self::U64(0) => Some(false),
            Self::True
            | Self::I8(_)
            | Self::I16(_)
            | Self::I32(_)
            | Self::I64(_)
            | Self::U8(_)
            | Self::U16(_)
            | Self::U32(_)
            | Self::U64(_) => Some(true),
            _ => None,
        }
    }

    /// Creates a boolean constant from a bool value.
    #[must_use]
    pub const fn from_bool(value: bool) -> Self {
        if value {
            Self::True
        } else {
            Self::False
        }
    }

    /// Returns `true` if this constant represents zero.
    ///
    /// This includes all numeric zero values and `False`.
    /// Useful for opaque predicate detection where `x ^ x`, `x - x`, `x * 0`, etc. produce zero.
    #[must_use]
    pub const fn is_zero(&self) -> bool {
        matches!(
            self,
            Self::I8(0)
                | Self::I16(0)
                | Self::I32(0)
                | Self::I64(0)
                | Self::U8(0)
                | Self::U16(0)
                | Self::U32(0)
                | Self::U64(0)
                | Self::NativeInt(0)
                | Self::NativeUInt(0)
                | Self::False
        )
    }

    /// Returns `true` if this constant represents one.
    ///
    /// This includes all numeric one values and `True`.
    /// Useful for identity operations and opaque predicate detection.
    #[must_use]
    pub const fn is_one(&self) -> bool {
        matches!(
            self,
            Self::I8(1)
                | Self::I16(1)
                | Self::I32(1)
                | Self::I64(1)
                | Self::U8(1)
                | Self::U16(1)
                | Self::U32(1)
                | Self::U64(1)
                | Self::NativeInt(1)
                | Self::NativeUInt(1)
                | Self::True
        )
    }

    /// Returns `true` if this constant represents negative one (-1).
    ///
    /// This is useful for detecting `x | -1 = -1` patterns in opaque predicates.
    #[must_use]
    pub const fn is_minus_one(&self) -> bool {
        matches!(
            self,
            Self::I8(-1) | Self::I16(-1) | Self::I32(-1) | Self::I64(-1) | Self::NativeInt(-1)
        )
    }

    /// Returns `true` if this constant has all bits set (e.g., -1 for signed, MAX for unsigned).
    ///
    /// This is useful for detecting `x & -1 = x` and `x | -1 = -1` patterns.
    #[must_use]
    pub const fn is_all_ones(&self) -> bool {
        matches!(
            self,
            Self::I8(-1)
                | Self::I16(-1)
                | Self::I32(-1)
                | Self::I64(-1)
                | Self::NativeInt(-1)
                | Self::U8(u8::MAX)
                | Self::U16(u16::MAX)
                | Self::U32(u32::MAX)
                | Self::U64(u64::MAX)
                | Self::NativeUInt(u64::MAX)
        )
    }

    /// Returns a zero constant of the same type as this constant.
    ///
    /// Useful for algebraic simplifications like `x * 0 = 0` where the result
    /// should preserve the type of the operands.
    #[must_use]
    pub const fn zero_of_same_type(&self) -> Self {
        match self {
            Self::I8(_) => Self::I8(0),
            Self::I16(_) => Self::I16(0),
            Self::I64(_) => Self::I64(0),
            Self::U8(_) => Self::U8(0),
            Self::U16(_) => Self::U16(0),
            Self::U32(_) => Self::U32(0),
            Self::U64(_) => Self::U64(0),
            Self::NativeInt(_) => Self::NativeInt(0),
            Self::NativeUInt(_) => Self::NativeUInt(0),
            Self::F32(_) => Self::F32(0.0),
            Self::F64(_) => Self::F64(0.0),
            // For non-numeric types (including I32), default to i32
            _ => Self::I32(0),
        }
    }

    /// Attempts to negate this constant.
    #[must_use]
    pub fn negate(&self, ptr_size: PointerSize) -> Option<Self> {
        match self {
            Self::I8(v) => Some(Self::I8(v.wrapping_neg())),
            Self::I16(v) => Some(Self::I16(v.wrapping_neg())),
            Self::I32(v) => Some(Self::I32(v.wrapping_neg())),
            Self::I64(v) => Some(Self::I64(v.wrapping_neg())),
            Self::NativeInt(v) => Some(Self::NativeInt(v.wrapping_neg())),
            Self::F32(v) => Some(Self::F32(-v)),
            Self::F64(v) => Some(Self::F64(-v)),
            // Unsigned negation wraps
            Self::U8(v) => Some(Self::U8(v.wrapping_neg())),
            Self::U16(v) => Some(Self::U16(v.wrapping_neg())),
            Self::U32(v) => Some(Self::U32(v.wrapping_neg())),
            Self::U64(v) => Some(Self::U64(v.wrapping_neg())),
            Self::NativeUInt(v) => Some(Self::NativeUInt(v.wrapping_neg())),
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to perform bitwise NOT on this constant.
    #[must_use]
    pub fn bitwise_not(&self, ptr_size: PointerSize) -> Option<Self> {
        match self {
            Self::I8(v) => Some(Self::I8(!v)),
            Self::I16(v) => Some(Self::I16(!v)),
            Self::I32(v) => Some(Self::I32(!v)),
            Self::I64(v) => Some(Self::I64(!v)),
            Self::U8(v) => Some(Self::U8(!v)),
            Self::U16(v) => Some(Self::U16(!v)),
            Self::U32(v) => Some(Self::U32(!v)),
            Self::U64(v) => Some(Self::U64(!v)),
            Self::NativeInt(v) => Some(Self::NativeInt(!v)),
            Self::NativeUInt(v) => Some(Self::NativeUInt(!v)),
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to perform bitwise AND on two constants.
    #[must_use]
    pub fn bitwise_and(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::I8(a & b)),
            (Self::I16(a), Self::I16(b)) => Some(Self::I16(a & b)),
            (Self::I32(a), Self::I32(b)) => Some(Self::I32(a & b)),
            (Self::I64(a), Self::I64(b)) => Some(Self::I64(a & b)),
            (Self::U8(a), Self::U8(b)) => Some(Self::U8(a & b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::U16(a & b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::U32(a & b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::U64(a & b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::NativeInt(a & b)),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::NativeUInt(a & b)),
            // Cross-type: promote to i64 for mixed signed operations
            (Self::I32(a), Self::I64(b)) | (Self::I64(b), Self::I32(a)) => {
                Some(Self::I64(i64::from(*a) & b))
            }
            (Self::U32(a), Self::U64(b)) | (Self::U64(b), Self::U32(a)) => {
                Some(Self::U64(u64::from(*a) & b))
            }
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to perform bitwise OR on two constants.
    #[must_use]
    pub fn bitwise_or(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::I8(a | b)),
            (Self::I16(a), Self::I16(b)) => Some(Self::I16(a | b)),
            (Self::I32(a), Self::I32(b)) => Some(Self::I32(a | b)),
            (Self::I64(a), Self::I64(b)) => Some(Self::I64(a | b)),
            (Self::U8(a), Self::U8(b)) => Some(Self::U8(a | b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::U16(a | b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::U32(a | b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::U64(a | b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::NativeInt(a | b)),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::NativeUInt(a | b)),
            (Self::I32(a), Self::I64(b)) | (Self::I64(b), Self::I32(a)) => {
                Some(Self::I64(i64::from(*a) | b))
            }
            (Self::U32(a), Self::U64(b)) | (Self::U64(b), Self::U32(a)) => {
                Some(Self::U64(u64::from(*a) | b))
            }
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to perform bitwise XOR on two constants.
    #[must_use]
    pub fn bitwise_xor(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::I8(a ^ b)),
            (Self::I16(a), Self::I16(b)) => Some(Self::I16(a ^ b)),
            (Self::I32(a), Self::I32(b)) => Some(Self::I32(a ^ b)),
            (Self::I64(a), Self::I64(b)) => Some(Self::I64(a ^ b)),
            (Self::U8(a), Self::U8(b)) => Some(Self::U8(a ^ b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::U16(a ^ b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::U32(a ^ b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::U64(a ^ b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::NativeInt(a ^ b)),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::NativeUInt(a ^ b)),
            (Self::I32(a), Self::I64(b)) | (Self::I64(b), Self::I32(a)) => {
                Some(Self::I64(i64::from(*a) ^ b))
            }
            (Self::U32(a), Self::U64(b)) | (Self::U64(b), Self::U32(a)) => {
                Some(Self::U64(u64::from(*a) ^ b))
            }
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to shift left.
    #[must_use]
    #[allow(clippy::cast_sign_loss)] // Shift amounts are non-negative by convention
    pub fn shl(&self, amount: &Self, ptr_size: PointerSize) -> Option<Self> {
        let shift = amount.as_i32()? as u32;
        match self {
            Self::I8(v) => Some(Self::I8(v.wrapping_shl(shift))),
            Self::I16(v) => Some(Self::I16(v.wrapping_shl(shift))),
            Self::I32(v) => Some(Self::I32(v.wrapping_shl(shift))),
            Self::I64(v) => Some(Self::I64(v.wrapping_shl(shift))),
            Self::U8(v) => Some(Self::U8(v.wrapping_shl(shift))),
            Self::U16(v) => Some(Self::U16(v.wrapping_shl(shift))),
            Self::U32(v) => Some(Self::U32(v.wrapping_shl(shift))),
            Self::U64(v) => Some(Self::U64(v.wrapping_shl(shift))),
            Self::NativeInt(v) => Some(Self::NativeInt(v.wrapping_shl(shift))),
            Self::NativeUInt(v) => Some(Self::NativeUInt(v.wrapping_shl(shift))),
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to shift right (arithmetic for signed, logical for unsigned).
    #[must_use]
    #[allow(clippy::cast_sign_loss)] // Shift amounts and unsigned shifts use intentional casts
    #[allow(clippy::cast_possible_wrap)] // Wrapping is expected for logical shift operations
    pub fn shr(&self, amount: &Self, unsigned: bool, ptr_size: PointerSize) -> Option<Self> {
        let shift = amount.as_i32()? as u32;
        match self {
            Self::I8(v) => {
                if unsigned {
                    Some(Self::I8((*v as u8).wrapping_shr(shift) as i8))
                } else {
                    Some(Self::I8(v.wrapping_shr(shift)))
                }
            }
            Self::I16(v) => {
                if unsigned {
                    Some(Self::I16((*v as u16).wrapping_shr(shift) as i16))
                } else {
                    Some(Self::I16(v.wrapping_shr(shift)))
                }
            }
            Self::I32(v) => {
                if unsigned {
                    Some(Self::I32((*v as u32).wrapping_shr(shift) as i32))
                } else {
                    Some(Self::I32(v.wrapping_shr(shift)))
                }
            }
            Self::I64(v) => {
                if unsigned {
                    Some(Self::I64((*v as u64).wrapping_shr(shift) as i64))
                } else {
                    Some(Self::I64(v.wrapping_shr(shift)))
                }
            }
            Self::U8(v) => Some(Self::U8(v.wrapping_shr(shift))),
            Self::U16(v) => Some(Self::U16(v.wrapping_shr(shift))),
            Self::U32(v) => Some(Self::U32(v.wrapping_shr(shift))),
            Self::U64(v) => Some(Self::U64(v.wrapping_shr(shift))),
            Self::NativeInt(v) => {
                if unsigned {
                    Some(Self::NativeInt((*v as u64).wrapping_shr(shift) as i64))
                } else {
                    Some(Self::NativeInt(v.wrapping_shr(shift)))
                }
            }
            Self::NativeUInt(v) => Some(Self::NativeUInt(v.wrapping_shr(shift))),
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to add two constants.
    #[must_use]
    pub fn add(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::I8(a.wrapping_add(*b))),
            (Self::I16(a), Self::I16(b)) => Some(Self::I16(a.wrapping_add(*b))),
            (Self::I32(a), Self::I32(b)) => Some(Self::I32(a.wrapping_add(*b))),
            (Self::I64(a), Self::I64(b)) => Some(Self::I64(a.wrapping_add(*b))),
            (Self::U8(a), Self::U8(b)) => Some(Self::U8(a.wrapping_add(*b))),
            (Self::U16(a), Self::U16(b)) => Some(Self::U16(a.wrapping_add(*b))),
            (Self::U32(a), Self::U32(b)) => Some(Self::U32(a.wrapping_add(*b))),
            (Self::U64(a), Self::U64(b)) => Some(Self::U64(a.wrapping_add(*b))),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::NativeInt(a.wrapping_add(*b))),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => {
                Some(Self::NativeUInt(a.wrapping_add(*b)))
            }
            (Self::F32(a), Self::F32(b)) => Some(Self::F32(a + b)),
            (Self::F64(a), Self::F64(b)) => Some(Self::F64(a + b)),
            // Cross-type promotions
            (Self::I32(a), Self::I64(b)) | (Self::I64(b), Self::I32(a)) => {
                Some(Self::I64(i64::from(*a).wrapping_add(*b)))
            }
            (Self::U32(a), Self::U64(b)) | (Self::U64(b), Self::U32(a)) => {
                Some(Self::U64(u64::from(*a).wrapping_add(*b)))
            }
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to subtract two constants.
    #[must_use]
    pub fn sub(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::I8(a.wrapping_sub(*b))),
            (Self::I16(a), Self::I16(b)) => Some(Self::I16(a.wrapping_sub(*b))),
            (Self::I32(a), Self::I32(b)) => Some(Self::I32(a.wrapping_sub(*b))),
            (Self::I64(a), Self::I64(b)) => Some(Self::I64(a.wrapping_sub(*b))),
            (Self::U8(a), Self::U8(b)) => Some(Self::U8(a.wrapping_sub(*b))),
            (Self::U16(a), Self::U16(b)) => Some(Self::U16(a.wrapping_sub(*b))),
            (Self::U32(a), Self::U32(b)) => Some(Self::U32(a.wrapping_sub(*b))),
            (Self::U64(a), Self::U64(b)) => Some(Self::U64(a.wrapping_sub(*b))),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::NativeInt(a.wrapping_sub(*b))),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => {
                Some(Self::NativeUInt(a.wrapping_sub(*b)))
            }
            (Self::F32(a), Self::F32(b)) => Some(Self::F32(a - b)),
            (Self::F64(a), Self::F64(b)) => Some(Self::F64(a - b)),
            (Self::I32(a), Self::I64(b)) => Some(Self::I64(i64::from(*a).wrapping_sub(*b))),
            (Self::I64(a), Self::I32(b)) => Some(Self::I64(a.wrapping_sub(i64::from(*b)))),
            (Self::U32(a), Self::U64(b)) => Some(Self::U64(u64::from(*a).wrapping_sub(*b))),
            (Self::U64(a), Self::U32(b)) => Some(Self::U64(a.wrapping_sub(u64::from(*b)))),
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to multiply two constants.
    #[must_use]
    pub fn mul(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::I8(a.wrapping_mul(*b))),
            (Self::I16(a), Self::I16(b)) => Some(Self::I16(a.wrapping_mul(*b))),
            (Self::I32(a), Self::I32(b)) => Some(Self::I32(a.wrapping_mul(*b))),
            (Self::I64(a), Self::I64(b)) => Some(Self::I64(a.wrapping_mul(*b))),
            (Self::U8(a), Self::U8(b)) => Some(Self::U8(a.wrapping_mul(*b))),
            (Self::U16(a), Self::U16(b)) => Some(Self::U16(a.wrapping_mul(*b))),
            (Self::U32(a), Self::U32(b)) => Some(Self::U32(a.wrapping_mul(*b))),
            (Self::U64(a), Self::U64(b)) => Some(Self::U64(a.wrapping_mul(*b))),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::NativeInt(a.wrapping_mul(*b))),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => {
                Some(Self::NativeUInt(a.wrapping_mul(*b)))
            }
            (Self::F32(a), Self::F32(b)) => Some(Self::F32(a * b)),
            (Self::F64(a), Self::F64(b)) => Some(Self::F64(a * b)),
            (Self::I32(a), Self::I64(b)) | (Self::I64(b), Self::I32(a)) => {
                Some(Self::I64(i64::from(*a).wrapping_mul(*b)))
            }
            (Self::U32(a), Self::U64(b)) | (Self::U64(b), Self::U32(a)) => {
                Some(Self::U64(u64::from(*a).wrapping_mul(*b)))
            }
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to add two constants with overflow checking.
    ///
    /// Returns `None` if the addition would overflow.
    /// When `unsigned` is true, operands are treated as unsigned for overflow detection.
    #[must_use]
    pub fn add_checked(&self, other: &Self, unsigned: bool, ptr_size: PointerSize) -> Option<Self> {
        if unsigned {
            // Unsigned overflow check
            match (self, other) {
                (Self::I32(a), Self::I32(b)) => (*a)
                    .cast_unsigned()
                    .checked_add((*b).cast_unsigned())
                    .map(|r| Self::I32(r.cast_signed())),
                (Self::I64(a), Self::I64(b)) => (*a)
                    .cast_unsigned()
                    .checked_add((*b).cast_unsigned())
                    .map(|r| Self::I64(r.cast_signed())),
                (Self::U8(a), Self::U8(b)) => a.checked_add(*b).map(Self::U8),
                (Self::U16(a), Self::U16(b)) => a.checked_add(*b).map(Self::U16),
                (Self::U32(a), Self::U32(b)) => a.checked_add(*b).map(Self::U32),
                (Self::U64(a), Self::U64(b)) => a.checked_add(*b).map(Self::U64),
                (Self::NativeUInt(a), Self::NativeUInt(b)) => {
                    a.checked_add(*b).map(Self::NativeUInt)
                }
                _ => None,
            }
        } else {
            // Signed overflow check
            match (self, other) {
                (Self::I8(a), Self::I8(b)) => a.checked_add(*b).map(Self::I8),
                (Self::I16(a), Self::I16(b)) => a.checked_add(*b).map(Self::I16),
                (Self::I32(a), Self::I32(b)) => a.checked_add(*b).map(Self::I32),
                (Self::I64(a), Self::I64(b)) => a.checked_add(*b).map(Self::I64),
                (Self::NativeInt(a), Self::NativeInt(b)) => a.checked_add(*b).map(Self::NativeInt),
                _ => None,
            }
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to subtract two constants with overflow checking.
    ///
    /// Returns `None` if the subtraction would overflow.
    /// When `unsigned` is true, operands are treated as unsigned for overflow detection.
    #[must_use]
    pub fn sub_checked(&self, other: &Self, unsigned: bool, ptr_size: PointerSize) -> Option<Self> {
        if unsigned {
            // Unsigned overflow check
            match (self, other) {
                (Self::I32(a), Self::I32(b)) => (*a)
                    .cast_unsigned()
                    .checked_sub((*b).cast_unsigned())
                    .map(|r| Self::I32(r.cast_signed())),
                (Self::I64(a), Self::I64(b)) => (*a)
                    .cast_unsigned()
                    .checked_sub((*b).cast_unsigned())
                    .map(|r| Self::I64(r.cast_signed())),
                (Self::U8(a), Self::U8(b)) => a.checked_sub(*b).map(Self::U8),
                (Self::U16(a), Self::U16(b)) => a.checked_sub(*b).map(Self::U16),
                (Self::U32(a), Self::U32(b)) => a.checked_sub(*b).map(Self::U32),
                (Self::U64(a), Self::U64(b)) => a.checked_sub(*b).map(Self::U64),
                (Self::NativeUInt(a), Self::NativeUInt(b)) => {
                    a.checked_sub(*b).map(Self::NativeUInt)
                }
                _ => None,
            }
        } else {
            // Signed overflow check
            match (self, other) {
                (Self::I8(a), Self::I8(b)) => a.checked_sub(*b).map(Self::I8),
                (Self::I16(a), Self::I16(b)) => a.checked_sub(*b).map(Self::I16),
                (Self::I32(a), Self::I32(b)) => a.checked_sub(*b).map(Self::I32),
                (Self::I64(a), Self::I64(b)) => a.checked_sub(*b).map(Self::I64),
                (Self::NativeInt(a), Self::NativeInt(b)) => a.checked_sub(*b).map(Self::NativeInt),
                _ => None,
            }
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to multiply two constants with overflow checking.
    ///
    /// Returns `None` if the multiplication would overflow.
    /// When `unsigned` is true, operands are treated as unsigned for overflow detection.
    #[must_use]
    pub fn mul_checked(&self, other: &Self, unsigned: bool, ptr_size: PointerSize) -> Option<Self> {
        if unsigned {
            // Unsigned overflow check
            match (self, other) {
                (Self::I32(a), Self::I32(b)) => (*a)
                    .cast_unsigned()
                    .checked_mul((*b).cast_unsigned())
                    .map(|r| Self::I32(r.cast_signed())),
                (Self::I64(a), Self::I64(b)) => (*a)
                    .cast_unsigned()
                    .checked_mul((*b).cast_unsigned())
                    .map(|r| Self::I64(r.cast_signed())),
                (Self::U8(a), Self::U8(b)) => a.checked_mul(*b).map(Self::U8),
                (Self::U16(a), Self::U16(b)) => a.checked_mul(*b).map(Self::U16),
                (Self::U32(a), Self::U32(b)) => a.checked_mul(*b).map(Self::U32),
                (Self::U64(a), Self::U64(b)) => a.checked_mul(*b).map(Self::U64),
                (Self::NativeUInt(a), Self::NativeUInt(b)) => {
                    a.checked_mul(*b).map(Self::NativeUInt)
                }
                _ => None,
            }
        } else {
            // Signed overflow check
            match (self, other) {
                (Self::I8(a), Self::I8(b)) => a.checked_mul(*b).map(Self::I8),
                (Self::I16(a), Self::I16(b)) => a.checked_mul(*b).map(Self::I16),
                (Self::I32(a), Self::I32(b)) => a.checked_mul(*b).map(Self::I32),
                (Self::I64(a), Self::I64(b)) => a.checked_mul(*b).map(Self::I64),
                (Self::NativeInt(a), Self::NativeInt(b)) => a.checked_mul(*b).map(Self::NativeInt),
                _ => None,
            }
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to divide two constants.
    #[must_use]
    pub fn div(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) if *b != 0 => Some(Self::I8(a.wrapping_div(*b))),
            (Self::I16(a), Self::I16(b)) if *b != 0 => Some(Self::I16(a.wrapping_div(*b))),
            (Self::I32(a), Self::I32(b)) if *b != 0 => Some(Self::I32(a.wrapping_div(*b))),
            (Self::I64(a), Self::I64(b)) if *b != 0 => Some(Self::I64(a.wrapping_div(*b))),
            (Self::U8(a), Self::U8(b)) if *b != 0 => Some(Self::U8(a / b)),
            (Self::U16(a), Self::U16(b)) if *b != 0 => Some(Self::U16(a / b)),
            (Self::U32(a), Self::U32(b)) if *b != 0 => Some(Self::U32(a / b)),
            (Self::U64(a), Self::U64(b)) if *b != 0 => Some(Self::U64(a / b)),
            (Self::NativeInt(a), Self::NativeInt(b)) if *b != 0 => {
                Some(Self::NativeInt(a.wrapping_div(*b)))
            }
            (Self::NativeUInt(a), Self::NativeUInt(b)) if *b != 0 => Some(Self::NativeUInt(a / b)),
            (Self::F32(a), Self::F32(b)) => Some(Self::F32(a / b)), // Float div by zero is inf
            (Self::F64(a), Self::F64(b)) => Some(Self::F64(a / b)),
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to compute remainder (modulo) of two constants.
    #[must_use]
    pub fn rem(&self, other: &Self, ptr_size: PointerSize) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) if *b != 0 => Some(Self::I8(a.wrapping_rem(*b))),
            (Self::I16(a), Self::I16(b)) if *b != 0 => Some(Self::I16(a.wrapping_rem(*b))),
            (Self::I32(a), Self::I32(b)) if *b != 0 => Some(Self::I32(a.wrapping_rem(*b))),
            (Self::I64(a), Self::I64(b)) if *b != 0 => Some(Self::I64(a.wrapping_rem(*b))),
            (Self::U8(a), Self::U8(b)) if *b != 0 => Some(Self::U8(a % b)),
            (Self::U16(a), Self::U16(b)) if *b != 0 => Some(Self::U16(a % b)),
            (Self::U32(a), Self::U32(b)) if *b != 0 => Some(Self::U32(a % b)),
            (Self::U64(a), Self::U64(b)) if *b != 0 => Some(Self::U64(a % b)),
            (Self::NativeInt(a), Self::NativeInt(b)) if *b != 0 => {
                Some(Self::NativeInt(a.wrapping_rem(*b)))
            }
            (Self::NativeUInt(a), Self::NativeUInt(b)) if *b != 0 => Some(Self::NativeUInt(a % b)),
            (Self::F32(a), Self::F32(b)) => Some(Self::F32(a % b)),
            (Self::F64(a), Self::F64(b)) => Some(Self::F64(a % b)),
            _ => None,
        }
        .map(|v| v.mask_native(ptr_size))
    }

    /// Attempts to compare two constants for equality.
    #[must_use]
    #[allow(clippy::float_cmp)] // Exact comparison is correct for constant propagation
    #[allow(clippy::match_same_arms)] // NativeInt/NativeUInt are semantically different
    pub fn ceq(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::from_bool(a == b)),
            (Self::I16(a), Self::I16(b)) => Some(Self::from_bool(a == b)),
            (Self::I32(a), Self::I32(b)) => Some(Self::from_bool(a == b)),
            (Self::I64(a), Self::I64(b)) => Some(Self::from_bool(a == b)),
            (Self::U8(a), Self::U8(b)) => Some(Self::from_bool(a == b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::from_bool(a == b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::from_bool(a == b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::from_bool(a == b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::from_bool(a == b)),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::from_bool(a == b)),
            (Self::F32(a), Self::F32(b)) => Some(Self::from_bool(a == b)),
            (Self::F64(a), Self::F64(b)) => Some(Self::from_bool(a == b)),
            (Self::Null, Self::Null) | (Self::True, Self::True) | (Self::False, Self::False) => {
                Some(Self::True)
            }
            (Self::True, Self::False) | (Self::False, Self::True) => Some(Self::False),
            // Cross-type comparisons with promotion
            (Self::I32(a), Self::I64(b)) | (Self::I64(b), Self::I32(a)) => {
                Some(Self::from_bool(i64::from(*a) == *b))
            }
            (Self::U32(a), Self::U64(b)) | (Self::U64(b), Self::U32(a)) => {
                Some(Self::from_bool(u64::from(*a) == *b))
            }
            _ => None,
        }
    }

    /// Attempts to compare two constants for less-than (signed).
    #[must_use]
    #[allow(clippy::match_same_arms)] // NativeInt/NativeUInt are semantically different
    pub fn clt(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::from_bool(a < b)),
            (Self::I16(a), Self::I16(b)) => Some(Self::from_bool(a < b)),
            (Self::I32(a), Self::I32(b)) => Some(Self::from_bool(a < b)),
            (Self::I64(a), Self::I64(b)) => Some(Self::from_bool(a < b)),
            (Self::U8(a), Self::U8(b)) => Some(Self::from_bool(a < b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::from_bool(a < b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::from_bool(a < b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::from_bool(a < b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::from_bool(a < b)),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::from_bool(a < b)),
            (Self::F32(a), Self::F32(b)) => Some(Self::from_bool(a < b)),
            (Self::F64(a), Self::F64(b)) => Some(Self::from_bool(a < b)),
            (Self::I32(a), Self::I64(b)) => Some(Self::from_bool(i64::from(*a) < *b)),
            (Self::I64(a), Self::I32(b)) => Some(Self::from_bool(*a < i64::from(*b))),
            (Self::U32(a), Self::U64(b)) => Some(Self::from_bool(u64::from(*a) < *b)),
            (Self::U64(a), Self::U32(b)) => Some(Self::from_bool(*a < u64::from(*b))),
            _ => None,
        }
    }

    /// Attempts to compare two constants for less-than (unsigned).
    #[must_use]
    #[allow(clippy::cast_sign_loss)] // Unsigned comparison requires interpreting bits as unsigned
    #[allow(clippy::match_same_arms)] // NativeInt/NativeUInt are semantically different
    pub fn clt_un(&self, other: &Self) -> Option<Self> {
        // Treat values as unsigned for comparison
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::from_bool((*a as u8) < (*b as u8))),
            (Self::I16(a), Self::I16(b)) => Some(Self::from_bool((*a as u16) < (*b as u16))),
            (Self::I32(a), Self::I32(b)) => Some(Self::from_bool((*a as u32) < (*b as u32))),
            (Self::I64(a), Self::I64(b)) => Some(Self::from_bool((*a as u64) < (*b as u64))),
            (Self::U8(a), Self::U8(b)) => Some(Self::from_bool(a < b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::from_bool(a < b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::from_bool(a < b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::from_bool(a < b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => {
                Some(Self::from_bool((*a as u64) < (*b as u64)))
            }
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::from_bool(a < b)),
            // For floats, clt.un checks for unordered (NaN) or less than
            (Self::F32(a), Self::F32(b)) => {
                Some(Self::from_bool(a.is_nan() || b.is_nan() || a < b))
            }
            (Self::F64(a), Self::F64(b)) => {
                Some(Self::from_bool(a.is_nan() || b.is_nan() || a < b))
            }
            _ => None,
        }
    }

    /// Attempts to compare two constants for greater-than (signed).
    #[must_use]
    #[allow(clippy::match_same_arms)] // NativeInt/NativeUInt are semantically different
    pub fn cgt(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::from_bool(a > b)),
            (Self::I16(a), Self::I16(b)) => Some(Self::from_bool(a > b)),
            (Self::I32(a), Self::I32(b)) => Some(Self::from_bool(a > b)),
            (Self::I64(a), Self::I64(b)) => Some(Self::from_bool(a > b)),
            (Self::U8(a), Self::U8(b)) => Some(Self::from_bool(a > b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::from_bool(a > b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::from_bool(a > b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::from_bool(a > b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => Some(Self::from_bool(a > b)),
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::from_bool(a > b)),
            (Self::F32(a), Self::F32(b)) => Some(Self::from_bool(a > b)),
            (Self::F64(a), Self::F64(b)) => Some(Self::from_bool(a > b)),
            (Self::I32(a), Self::I64(b)) => Some(Self::from_bool(i64::from(*a) > *b)),
            (Self::I64(a), Self::I32(b)) => Some(Self::from_bool(*a > i64::from(*b))),
            (Self::U32(a), Self::U64(b)) => Some(Self::from_bool(u64::from(*a) > *b)),
            (Self::U64(a), Self::U32(b)) => Some(Self::from_bool(*a > u64::from(*b))),
            _ => None,
        }
    }

    /// Attempts to compare two constants for greater-than (unsigned).
    #[must_use]
    #[allow(clippy::cast_sign_loss)] // Unsigned comparison requires interpreting bits as unsigned
    #[allow(clippy::match_same_arms)] // NativeInt/NativeUInt are semantically different
    pub fn cgt_un(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (Self::I8(a), Self::I8(b)) => Some(Self::from_bool((*a as u8) > (*b as u8))),
            (Self::I16(a), Self::I16(b)) => Some(Self::from_bool((*a as u16) > (*b as u16))),
            (Self::I32(a), Self::I32(b)) => Some(Self::from_bool((*a as u32) > (*b as u32))),
            (Self::I64(a), Self::I64(b)) => Some(Self::from_bool((*a as u64) > (*b as u64))),
            (Self::U8(a), Self::U8(b)) => Some(Self::from_bool(a > b)),
            (Self::U16(a), Self::U16(b)) => Some(Self::from_bool(a > b)),
            (Self::U32(a), Self::U32(b)) => Some(Self::from_bool(a > b)),
            (Self::U64(a), Self::U64(b)) => Some(Self::from_bool(a > b)),
            (Self::NativeInt(a), Self::NativeInt(b)) => {
                Some(Self::from_bool((*a as u64) > (*b as u64)))
            }
            (Self::NativeUInt(a), Self::NativeUInt(b)) => Some(Self::from_bool(a > b)),
            // For floats, cgt.un checks for unordered (NaN) or greater than
            (Self::F32(a), Self::F32(b)) => {
                Some(Self::from_bool(a.is_nan() || b.is_nan() || a > b))
            }
            (Self::F64(a), Self::F64(b)) => {
                Some(Self::from_bool(a.is_nan() || b.is_nan() || a > b))
            }
            _ => None,
        }
    }

    /// Converts this constant to a different type.
    ///
    /// This implements CIL type conversion semantics (conv.* instructions).
    /// For overflow checking conversions, use `convert_to_checked`.
    ///
    /// # Arguments
    ///
    /// * `target` - The target SSA type to convert to.
    /// * `unsigned_source` - If true, treat the source value as unsigned for conversion.
    ///
    /// # Returns
    ///
    /// The converted constant, or `None` if conversion is not possible.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dotscope::analysis::{ConstValue, SsaType};
    /// use dotscope::metadata::typesystem::PointerSize;
    ///
    /// let value = ConstValue::I32(42);
    /// let converted = value.convert_to(&SsaType::I64, false, PointerSize::Bit64);
    /// assert_eq!(converted, Some(ConstValue::I64(42)));
    /// ```
    #[must_use]
    pub fn convert_to(
        &self,
        target: &SsaType,
        unsigned_source: bool,
        ptr_size: PointerSize,
    ) -> Option<Self> {
        // For unsigned source interpretation, get the raw bits as u64
        // For signed source, get as i64
        let (signed_val, unsigned_val) = if unsigned_source {
            let u = self.as_u64()?;
            // Reinterpret as signed for operations that need it
            (i64::from_ne_bytes(u.to_ne_bytes()), u)
        } else {
            let s = self.as_i64()?;
            // Reinterpret as unsigned for operations that need it
            (s, u64::from_ne_bytes(s.to_ne_bytes()))
        };

        // These casts are intentional truncations for type conversion
        #[allow(clippy::cast_possible_truncation)]
        Some(match target {
            // Truncating conversions - use wrapping to get low bits
            SsaType::I8 => Self::I8(signed_val as i8),
            SsaType::U8 => Self::U8(unsigned_val as u8),
            SsaType::I16 => Self::I16(signed_val as i16),
            SsaType::U16 | SsaType::Char => Self::U16(unsigned_val as u16),
            SsaType::I32 => Self::I32(signed_val as i32),
            SsaType::U32 => Self::U32(unsigned_val as u32),
            // Non-truncating conversions
            SsaType::I64 => Self::I64(signed_val),
            SsaType::U64 => Self::U64(unsigned_val),
            SsaType::NativeInt => Self::NativeInt(signed_val),
            SsaType::NativeUInt => Self::NativeUInt(unsigned_val),
            // Float conversions - interpretation matters
            SsaType::F32 =>
            {
                #[allow(clippy::cast_precision_loss)]
                if unsigned_source {
                    Self::F32(unsigned_val as f32)
                } else {
                    Self::F32(signed_val as f32)
                }
            }
            SsaType::F64 =>
            {
                #[allow(clippy::cast_precision_loss)]
                if unsigned_source {
                    Self::F64(unsigned_val as f64)
                } else {
                    Self::F64(signed_val as f64)
                }
            }
            SsaType::Bool => Self::from_bool(signed_val != 0),
            _ => return None,
        })
        .map(|v| v.mask_native(ptr_size))
    }

    /// Converts this constant to a different type with overflow checking.
    ///
    /// This implements CIL overflow-checked conversion semantics (conv.ovf.* instructions).
    /// Returns `None` if the value would overflow the target type.
    ///
    /// # Arguments
    ///
    /// * `target` - The target SSA type to convert to.
    /// * `unsigned_source` - If true, treat the source value as unsigned for conversion.
    ///
    /// # Returns
    ///
    /// The converted constant if no overflow, or `None` if conversion would overflow
    /// or is not possible.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dotscope::analysis::{ConstValue, SsaType};
    /// use dotscope::metadata::typesystem::PointerSize;
    ///
    /// let value = ConstValue::I32(1000);
    /// // 1000 doesn't fit in i8 (-128 to 127)
    /// assert_eq!(value.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64), None);
    ///
    /// let small = ConstValue::I32(42);
    /// assert_eq!(small.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64), Some(ConstValue::I8(42)));
    /// ```
    #[must_use]
    pub fn convert_to_checked(
        &self,
        target: &SsaType,
        unsigned_source: bool,
        ptr_size: PointerSize,
    ) -> Option<Self> {
        // Get both signed and unsigned interpretations
        let (signed_val, unsigned_val) = if unsigned_source {
            let u = self.as_u64()?;
            (i64::from_ne_bytes(u.to_ne_bytes()), u)
        } else {
            let s = self.as_i64()?;
            (s, u64::from_ne_bytes(s.to_ne_bytes()))
        };

        // Check if value fits in target type using try_into for range checking
        let fits = match target {
            SsaType::I8 => i8::try_from(signed_val).is_ok(),
            SsaType::U8 => u8::try_from(unsigned_val).is_ok() && signed_val >= 0,
            SsaType::I16 => i16::try_from(signed_val).is_ok(),
            SsaType::U16 => u16::try_from(unsigned_val).is_ok() && signed_val >= 0,
            SsaType::I32 => i32::try_from(signed_val).is_ok(),
            SsaType::U32 => u32::try_from(unsigned_val).is_ok() && signed_val >= 0,
            SsaType::I64
            | SsaType::NativeInt
            | SsaType::Bool
            | SsaType::Char
            | SsaType::F32
            | SsaType::F64 => true,
            SsaType::U64 | SsaType::NativeUInt => signed_val >= 0,
            _ => return None,
        };

        if !fits {
            return None; // Would overflow
        }

        // Perform the conversion (same as convert_to)
        self.convert_to(target, unsigned_source, ptr_size)
    }

    /// Masks a `ConstValue` to the target pointer width.
    ///
    /// For `NativeInt`, sign-extends from 32-bit on `Bit32`.
    /// For `NativeUInt`, zero-extends from 32-bit on `Bit32`.
    /// All other variants are returned unchanged.
    #[must_use]
    pub fn mask_native(self, ptr_size: PointerSize) -> Self {
        match self {
            Self::NativeInt(v) => Self::NativeInt(ptr_size.mask_signed(v)),
            Self::NativeUInt(v) => Self::NativeUInt(ptr_size.mask_unsigned(v)),
            other => other,
        }
    }
}

impl fmt::Display for ConstValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::I8(v) => write!(f, "{v}i8"),
            Self::I16(v) => write!(f, "{v}i16"),
            Self::I32(v) => write!(f, "{v}"),
            Self::I64(v) => write!(f, "{v}L"),
            Self::U8(v) => write!(f, "{v}u8"),
            Self::U16(v) => write!(f, "{v}u16"),
            Self::U32(v) => write!(f, "{v}u"),
            Self::U64(v) => write!(f, "{v}UL"),
            Self::NativeInt(v) => write!(f, "{v}n"),
            Self::NativeUInt(v) => write!(f, "{v}un"),
            Self::F32(v) => write!(f, "{v}f"),
            Self::F64(v) => write!(f, "{v}"),
            Self::String(idx) => write!(f, "str@{idx}"),
            Self::DecryptedString(s) => write!(f, "\"{}\"", s.escape_default()),
            Self::Null => write!(f, "null"),
            Self::True => write!(f, "true"),
            Self::False => write!(f, "false"),
            Self::Type(t) => write!(f, "typeof({t})"),
            Self::MethodHandle(m) => write!(f, "methodof({m})"),
            Self::FieldHandle(fl) => write!(f, "fieldof({fl})"),
        }
    }
}

/// Attempts to convert a `ConstValue` to an `Immediate` for CIL instruction encoding.
///
/// This conversion handles the numeric `ConstValue` variants, mapping them to
/// their corresponding `Immediate` representations. The conversion follows
/// CIL semantics where:
///
/// - Signed integers map directly to their signed `Immediate` variants
/// - Unsigned integers use bit-preserving casts to signed types (since CIL
///   doesn't distinguish unsigned at the instruction level for most operations)
/// - Floating-point values map directly
/// - Boolean values map to `Int32` (1 for true, 0 for false)
/// - Native integers use 64-bit representations
///
/// # Errors
///
/// Returns [`Error::SsaError`] for non-numeric `ConstValue` variants
/// (`String`, `DecryptedString`, `Null`, `Type`, `MethodHandle`, `FieldHandle`)
/// since these cannot be represented as immediate values. Handle these cases
/// with pattern matching before conversion.
///
/// # Example
///
/// ```rust,ignore
/// use dotscope::analysis::ConstValue;
/// use dotscope::assembly::Immediate;
/// use std::convert::TryFrom;
///
/// let const_val = ConstValue::I32(42);
/// let immediate = Immediate::try_from(&const_val)?;
/// assert!(matches!(immediate, Immediate::Int32(42)));
///
/// // Non-numeric values return an error
/// let null_val = ConstValue::Null;
/// assert!(Immediate::try_from(&null_val).is_err());
/// ```
impl TryFrom<&ConstValue> for Immediate {
    type Error = Error;

    #[allow(clippy::cast_possible_wrap)] // Intentional bit-preserving casts for CIL semantics
    fn try_from(value: &ConstValue) -> Result<Self, Self::Error> {
        match value {
            // Signed integers - direct mapping
            ConstValue::I8(v) => Ok(Immediate::Int8(*v)),
            ConstValue::I16(v) => Ok(Immediate::Int16(*v)),
            ConstValue::I32(v) => Ok(Immediate::Int32(*v)),

            // Unsigned integers - use signed Immediate variants with bit-preserving casts.
            // CIL instructions don't distinguish signed/unsigned for most operations;
            // the bit pattern is what matters.
            ConstValue::U8(v) => Ok(Immediate::Int8(*v as i8)),
            ConstValue::U16(v) => Ok(Immediate::Int16(*v as i16)),
            ConstValue::U32(v) => Ok(Immediate::Int32(*v as i32)),

            // 64-bit integers and native integers use Int64 representation
            // (NativeInt is semantically different but has identical representation)
            ConstValue::I64(v) | ConstValue::NativeInt(v) => Ok(Immediate::Int64(*v)),
            ConstValue::U64(v) | ConstValue::NativeUInt(v) => Ok(Immediate::Int64(*v as i64)),

            // Floating point - direct mapping
            ConstValue::F32(v) => Ok(Immediate::Float32(*v)),
            ConstValue::F64(v) => Ok(Immediate::Float64(*v)),

            // Boolean values - map to Int32 (CIL uses int32 for booleans on stack)
            ConstValue::True => Ok(Immediate::Int32(1)),
            ConstValue::False => Ok(Immediate::Int32(0)),

            // Non-numeric types cannot be converted to immediates
            ConstValue::String(_)
            | ConstValue::DecryptedString(_)
            | ConstValue::Null
            | ConstValue::Type(_)
            | ConstValue::MethodHandle(_)
            | ConstValue::FieldHandle(_) => Err(Error::SsaError(format!(
                "Cannot convert {value:?} to Immediate - use pattern matching to handle this case"
            ))),
        }
    }
}

/// Abstract value for dataflow analysis.
///
/// This represents the abstract state of an SSA variable during analysis.
/// It forms a lattice where values can be refined as more information is gathered.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum AbstractValue {
    /// No information yet (top of lattice).
    ///
    /// This is the initial state before any analysis.
    #[default]
    Top,

    /// Known constant value.
    Constant(ConstValue),

    /// Known to be non-null (for reference types).
    NonNull,

    /// Value in a bounded range [min, max].
    Range {
        /// Minimum value (inclusive).
        min: i64,
        /// Maximum value (inclusive).
        max: i64,
    },

    /// Same value as another SSA variable.
    ///
    /// Used for copy propagation.
    SameAs(SsaVarId),

    /// Result of a specific computation (for CSE).
    Computed(ComputedValue),

    /// Multiple possible values (bottom of lattice for constants).
    ///
    /// This means the value cannot be determined at compile time.
    Bottom,
}

impl AbstractValue {
    /// Returns `true` if this is the top element (no information).
    #[must_use]
    pub const fn is_top(&self) -> bool {
        matches!(self, Self::Top)
    }

    /// Returns `true` if this is the bottom element (conflicting info).
    #[must_use]
    pub const fn is_bottom(&self) -> bool {
        matches!(self, Self::Bottom)
    }

    /// Returns `true` if this is a known constant.
    #[must_use]
    pub const fn is_constant(&self) -> bool {
        matches!(self, Self::Constant(_))
    }

    /// Returns the constant value if this is a constant.
    #[must_use]
    pub const fn as_constant(&self) -> Option<&ConstValue> {
        match self {
            Self::Constant(c) => Some(c),
            _ => None,
        }
    }

    /// Returns `true` if this value is known to be non-null.
    #[must_use]
    pub const fn is_non_null(&self) -> bool {
        matches!(self, Self::NonNull | Self::Constant(_))
    }

    /// Meet operation for the lattice (used at control flow joins).
    ///
    /// Returns the greatest lower bound of `self` and `other`.
    #[must_use]
    #[allow(clippy::match_same_arms)] // Arms kept separate for lattice documentation clarity
    pub fn meet(&self, other: &Self) -> Self {
        match (self, other) {
            // Top meets anything yields the other
            (Self::Top, x) | (x, Self::Top) => x.clone(),

            // Bottom meets anything yields Bottom
            (Self::Bottom, _) | (_, Self::Bottom) => Self::Bottom,

            // Same constants stay constant
            (Self::Constant(a), Self::Constant(b)) if a == b => Self::Constant(a.clone()),

            // Different constants become Bottom
            (Self::Constant(_), Self::Constant(_)) => Self::Bottom,

            // NonNull meets NonNull stays NonNull
            (Self::NonNull, Self::NonNull) => Self::NonNull,

            // NonNull meets Constant stays Constant (constants are non-null if not null)
            (Self::NonNull, Self::Constant(c)) | (Self::Constant(c), Self::NonNull) => {
                if c.is_null() {
                    Self::Bottom // null is not non-null
                } else {
                    Self::Constant(c.clone())
                }
            }

            // Ranges can be merged
            (
                Self::Range {
                    min: a_min,
                    max: a_max,
                },
                Self::Range {
                    min: b_min,
                    max: b_max,
                },
            ) => {
                let new_min = (*a_min).min(*b_min);
                let new_max = (*a_max).max(*b_max);
                Self::Range {
                    min: new_min,
                    max: new_max,
                }
            }

            // SameAs values must match
            (Self::SameAs(a), Self::SameAs(b)) if a == b => Self::SameAs(*a),

            // Computed values must match exactly
            (Self::Computed(a), Self::Computed(b)) if a == b => Self::Computed(a.clone()),

            // Otherwise, Bottom
            _ => Self::Bottom,
        }
    }

    /// Join operation for the lattice.
    ///
    /// Returns the least upper bound of `self` and `other`.
    #[must_use]
    pub fn join(&self, other: &Self) -> Self {
        match (self, other) {
            // Bottom joins anything yields the other
            (Self::Bottom, x) | (x, Self::Bottom) => x.clone(),

            // Top joins anything yields Top
            (Self::Top, _) | (_, Self::Top) => Self::Top,

            // Same values stay the same
            (a, b) if a == b => a.clone(),

            // Otherwise, Top
            _ => Self::Top,
        }
    }
}

impl fmt::Display for AbstractValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Top => write!(f, "⊤"),
            Self::Constant(c) => write!(f, "{c}"),
            Self::NonNull => write!(f, "!null"),
            Self::Range { min, max } => write!(f, "[{min}..{max}]"),
            Self::SameAs(v) => write!(f, "={v}"),
            Self::Computed(c) => write!(f, "{c}"),
            Self::Bottom => write!(f, "⊥"),
        }
    }
}

/// Computed value for common subexpression elimination (CSE).
///
/// This represents the result of a computation, enabling recognition
/// of equivalent expressions that can be eliminated.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ComputedValue {
    /// The operation that produced this value.
    pub op: ComputedOp,
    /// The operands to the operation.
    pub operands: Vec<SsaVarId>,
}

impl ComputedValue {
    /// Creates a new computed value.
    #[must_use]
    pub fn new(op: ComputedOp, operands: Vec<SsaVarId>) -> Self {
        Self { op, operands }
    }

    /// Creates a unary computed value.
    #[must_use]
    pub fn unary(op: ComputedOp, operand: SsaVarId) -> Self {
        Self {
            op,
            operands: vec![operand],
        }
    }

    /// Creates a binary computed value.
    #[must_use]
    pub fn binary(op: ComputedOp, left: SsaVarId, right: SsaVarId) -> Self {
        Self {
            op,
            operands: vec![left, right],
        }
    }

    /// Normalizes commutative operations for better CSE.
    ///
    /// For commutative ops like add/mul, orders operands consistently
    /// so that `a + b` and `b + a` have the same computed value.
    #[must_use]
    pub fn normalized(self) -> Self {
        if self.op.is_commutative() && self.operands.len() == 2 {
            let mut ops = self.operands;
            if ops[0].index() > ops[1].index() {
                ops.swap(0, 1);
            }
            Self {
                op: self.op,
                operands: ops,
            }
        } else {
            self
        }
    }
}

impl fmt::Display for ComputedValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}(", self.op)?;
        for (i, op) in self.operands.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{op}")?;
        }
        write!(f, ")")
    }
}

/// Operations that can be tracked for CSE.
///
/// These represent the pure operations whose results can be reused
/// when the same operation is performed with the same operands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComputedOp {
    // Arithmetic
    /// Addition
    Add,
    /// Subtraction
    Sub,
    /// Multiplication
    Mul,
    /// Division
    Div,
    /// Remainder (modulo)
    Rem,
    /// Negation
    Neg,

    // Bitwise
    /// Bitwise AND
    And,
    /// Bitwise OR
    Or,
    /// Bitwise XOR
    Xor,
    /// Bitwise NOT
    Not,
    /// Shift left
    Shl,
    /// Shift right
    Shr,

    // Comparison
    /// Compare equal
    Ceq,
    /// Compare not equal
    Cne,
    /// Compare less than
    Clt,
    /// Compare greater than
    Cgt,
    /// Compare less than or equal
    Cle,
    /// Compare greater than or equal
    Cge,

    // Conversion
    /// Convert to int8
    ConvI1,
    /// Convert to int16
    ConvI2,
    /// Convert to int32
    ConvI4,
    /// Convert to int64
    ConvI8,
    /// Convert to uint8
    ConvU1,
    /// Convert to uint16
    ConvU2,
    /// Convert to uint32
    ConvU4,
    /// Convert to uint64
    ConvU8,
    /// Convert to float32
    ConvR4,
    /// Convert to float64
    ConvR8,
}

impl ComputedOp {
    /// Returns `true` if this operation is commutative.
    #[must_use]
    pub const fn is_commutative(&self) -> bool {
        matches!(
            self,
            Self::Add | Self::Mul | Self::And | Self::Or | Self::Xor | Self::Ceq | Self::Cne
        )
    }

    /// Returns `true` if this is a comparison operation.
    #[must_use]
    pub const fn is_comparison(&self) -> bool {
        matches!(
            self,
            Self::Ceq | Self::Cne | Self::Clt | Self::Cgt | Self::Cle | Self::Cge
        )
    }
}

impl fmt::Display for ComputedOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::Add => "add",
            Self::Sub => "sub",
            Self::Mul => "mul",
            Self::Div => "div",
            Self::Rem => "rem",
            Self::Neg => "neg",
            Self::And => "and",
            Self::Or => "or",
            Self::Xor => "xor",
            Self::Not => "not",
            Self::Shl => "shl",
            Self::Shr => "shr",
            Self::Ceq => "ceq",
            Self::Cne => "cne",
            Self::Clt => "clt",
            Self::Cgt => "cgt",
            Self::Cle => "cle",
            Self::Cge => "cge",
            Self::ConvI1 => "conv.i1",
            Self::ConvI2 => "conv.i2",
            Self::ConvI4 => "conv.i4",
            Self::ConvI8 => "conv.i8",
            Self::ConvU1 => "conv.u1",
            Self::ConvU2 => "conv.u2",
            Self::ConvU4 => "conv.u4",
            Self::ConvU8 => "conv.u8",
            Self::ConvR4 => "conv.r4",
            Self::ConvR8 => "conv.r8",
        };
        write!(f, "{s}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metadata::typesystem::PointerSize;

    #[test]
    fn test_const_arithmetic() {
        let a = ConstValue::I32(10);
        let b = ConstValue::I32(3);

        assert_eq!(a.add(&b, PointerSize::Bit64), Some(ConstValue::I32(13)));
        assert_eq!(a.sub(&b, PointerSize::Bit64), Some(ConstValue::I32(7)));
        assert_eq!(a.mul(&b, PointerSize::Bit64), Some(ConstValue::I32(30)));
    }

    #[test]
    fn test_const_comparison() {
        let a = ConstValue::I32(10);
        let b = ConstValue::I32(3);
        let c = ConstValue::I32(10);

        assert_eq!(a.ceq(&b), Some(ConstValue::False));
        assert_eq!(a.ceq(&c), Some(ConstValue::True));
        assert_eq!(a.clt(&b), Some(ConstValue::False));
        assert_eq!(b.clt(&a), Some(ConstValue::True));
    }

    #[test]
    fn test_const_bool_conversion() {
        assert_eq!(ConstValue::True.as_bool(), Some(true));
        assert_eq!(ConstValue::False.as_bool(), Some(false));
        assert_eq!(ConstValue::I32(0).as_bool(), Some(false));
        assert_eq!(ConstValue::I32(42).as_bool(), Some(true));
        assert_eq!(ConstValue::Null.as_bool(), Some(false));
    }

    #[test]
    fn test_abstract_value_meet() {
        // Top meets anything yields the other
        assert_eq!(
            AbstractValue::Top.meet(&AbstractValue::Constant(ConstValue::I32(5))),
            AbstractValue::Constant(ConstValue::I32(5))
        );

        // Same constants stay constant
        assert_eq!(
            AbstractValue::Constant(ConstValue::I32(5))
                .meet(&AbstractValue::Constant(ConstValue::I32(5))),
            AbstractValue::Constant(ConstValue::I32(5))
        );

        // Different constants become Bottom
        assert_eq!(
            AbstractValue::Constant(ConstValue::I32(5))
                .meet(&AbstractValue::Constant(ConstValue::I32(10))),
            AbstractValue::Bottom
        );

        // Bottom meets anything yields Bottom
        assert_eq!(
            AbstractValue::Bottom.meet(&AbstractValue::Constant(ConstValue::I32(5))),
            AbstractValue::Bottom
        );
    }

    #[test]
    fn test_range_merge() {
        let r1 = AbstractValue::Range { min: 0, max: 10 };
        let r2 = AbstractValue::Range { min: 5, max: 15 };

        assert_eq!(r1.meet(&r2), AbstractValue::Range { min: 0, max: 15 });
    }

    #[test]
    fn test_computed_value_normalization() {
        let v0 = SsaVarId::new();
        let v1 = SsaVarId::new();

        // add(v1, v0) should normalize to add(v0, v1)
        let cv1 = ComputedValue::binary(ComputedOp::Add, v1, v0).normalized();
        let cv2 = ComputedValue::binary(ComputedOp::Add, v0, v1).normalized();

        assert_eq!(cv1, cv2);

        // sub is not commutative, should not normalize
        let cv3 = ComputedValue::binary(ComputedOp::Sub, v1, v0).normalized();
        let cv4 = ComputedValue::binary(ComputedOp::Sub, v0, v1).normalized();

        assert_ne!(cv3, cv4);
    }

    #[test]
    fn test_is_zero() {
        // All integer zeros
        assert!(ConstValue::I8(0).is_zero());
        assert!(ConstValue::I16(0).is_zero());
        assert!(ConstValue::I32(0).is_zero());
        assert!(ConstValue::I64(0).is_zero());
        assert!(ConstValue::U8(0).is_zero());
        assert!(ConstValue::U16(0).is_zero());
        assert!(ConstValue::U32(0).is_zero());
        assert!(ConstValue::U64(0).is_zero());
        assert!(ConstValue::NativeInt(0).is_zero());
        assert!(ConstValue::NativeUInt(0).is_zero());
        assert!(ConstValue::False.is_zero());

        // Non-zeros
        assert!(!ConstValue::I32(1).is_zero());
        assert!(!ConstValue::I32(-1).is_zero());
        assert!(!ConstValue::True.is_zero());
        assert!(!ConstValue::Null.is_zero()); // Null is not zero
    }

    #[test]
    fn test_is_one() {
        // All integer ones
        assert!(ConstValue::I8(1).is_one());
        assert!(ConstValue::I16(1).is_one());
        assert!(ConstValue::I32(1).is_one());
        assert!(ConstValue::I64(1).is_one());
        assert!(ConstValue::U8(1).is_one());
        assert!(ConstValue::U16(1).is_one());
        assert!(ConstValue::U32(1).is_one());
        assert!(ConstValue::U64(1).is_one());
        assert!(ConstValue::NativeInt(1).is_one());
        assert!(ConstValue::NativeUInt(1).is_one());
        assert!(ConstValue::True.is_one());

        // Non-ones
        assert!(!ConstValue::I32(0).is_one());
        assert!(!ConstValue::I32(2).is_one());
        assert!(!ConstValue::False.is_one());
    }

    #[test]
    fn test_is_minus_one() {
        // Signed -1 values
        assert!(ConstValue::I8(-1).is_minus_one());
        assert!(ConstValue::I16(-1).is_minus_one());
        assert!(ConstValue::I32(-1).is_minus_one());
        assert!(ConstValue::I64(-1).is_minus_one());
        assert!(ConstValue::NativeInt(-1).is_minus_one());

        // Not -1
        assert!(!ConstValue::I32(0).is_minus_one());
        assert!(!ConstValue::I32(1).is_minus_one());
        // Unsigned types cannot represent -1
        assert!(!ConstValue::U32(u32::MAX).is_minus_one());
    }

    #[test]
    fn test_convert_to_widening() {
        // i32 -> i64 (sign extends)
        let v = ConstValue::I32(42);
        assert_eq!(
            v.convert_to(&SsaType::I64, false, PointerSize::Bit64),
            Some(ConstValue::I64(42))
        );

        // i32 -> i64 with negative
        let v = ConstValue::I32(-42);
        assert_eq!(
            v.convert_to(&SsaType::I64, false, PointerSize::Bit64),
            Some(ConstValue::I64(-42))
        );

        // u32 -> u64 (zero extends)
        let v = ConstValue::U32(42);
        assert_eq!(
            v.convert_to(&SsaType::U64, false, PointerSize::Bit64),
            Some(ConstValue::U64(42))
        );
    }

    #[test]
    fn test_convert_to_narrowing() {
        // i32 -> i8 (truncates)
        let v = ConstValue::I32(42);
        assert_eq!(
            v.convert_to(&SsaType::I8, false, PointerSize::Bit64),
            Some(ConstValue::I8(42))
        );

        // i32 -> i8 truncation with overflow
        let v = ConstValue::I32(1000);
        // 1000 = 0x3E8, truncated to i8 = 0xE8 = -24 (signed)
        assert_eq!(
            v.convert_to(&SsaType::I8, false, PointerSize::Bit64),
            Some(ConstValue::I8(-24))
        );

        // i64 -> i32 (truncates)
        let v = ConstValue::I64(0x1_0000_0042);
        assert_eq!(
            v.convert_to(&SsaType::I32, false, PointerSize::Bit64),
            Some(ConstValue::I32(0x42))
        );
    }

    #[test]
    fn test_convert_to_float() {
        // i32 -> f32
        let v = ConstValue::I32(42);
        assert_eq!(
            v.convert_to(&SsaType::F32, false, PointerSize::Bit64),
            Some(ConstValue::F32(42.0))
        );

        // i32 -> f64
        let v = ConstValue::I32(42);
        assert_eq!(
            v.convert_to(&SsaType::F64, false, PointerSize::Bit64),
            Some(ConstValue::F64(42.0))
        );

        // Unsigned source to float
        let v = ConstValue::U32(42);
        assert_eq!(
            v.convert_to(&SsaType::F32, true, PointerSize::Bit64),
            Some(ConstValue::F32(42.0))
        );
    }

    #[test]
    fn test_convert_to_bool() {
        // Non-zero -> true
        let v = ConstValue::I32(42);
        assert_eq!(
            v.convert_to(&SsaType::Bool, false, PointerSize::Bit64),
            Some(ConstValue::True)
        );

        // Zero -> false
        let v = ConstValue::I32(0);
        assert_eq!(
            v.convert_to(&SsaType::Bool, false, PointerSize::Bit64),
            Some(ConstValue::False)
        );
    }

    #[test]
    fn test_convert_to_checked_in_range() {
        // Value fits in target
        let v = ConstValue::I32(100);
        assert_eq!(
            v.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64),
            Some(ConstValue::I8(100))
        );

        // Value at boundary
        let v = ConstValue::I32(127);
        assert_eq!(
            v.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64),
            Some(ConstValue::I8(127))
        );

        let v = ConstValue::I32(-128);
        assert_eq!(
            v.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64),
            Some(ConstValue::I8(-128))
        );
    }

    #[test]
    fn test_convert_to_checked_overflow() {
        // Value overflows target
        let v = ConstValue::I32(1000);
        assert_eq!(
            v.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64),
            None
        );

        // Negative to unsigned
        let v = ConstValue::I32(-1);
        assert_eq!(
            v.convert_to_checked(&SsaType::U8, false, PointerSize::Bit64),
            None
        );
        assert_eq!(
            v.convert_to_checked(&SsaType::U32, false, PointerSize::Bit64),
            None
        );
        assert_eq!(
            v.convert_to_checked(&SsaType::U64, false, PointerSize::Bit64),
            None
        );
    }

    #[test]
    fn test_convert_to_char() {
        // i32 -> char (u16)
        let v = ConstValue::I32(65); // 'A'
        assert_eq!(
            v.convert_to(&SsaType::Char, false, PointerSize::Bit64),
            Some(ConstValue::U16(65))
        );
    }

    #[test]
    fn test_convert_to_native() {
        // i32 -> NativeInt
        let v = ConstValue::I32(42);
        assert_eq!(
            v.convert_to(&SsaType::NativeInt, false, PointerSize::Bit64),
            Some(ConstValue::NativeInt(42))
        );

        // i32 -> NativeUInt
        let v = ConstValue::I32(42);
        assert_eq!(
            v.convert_to(&SsaType::NativeUInt, false, PointerSize::Bit64),
            Some(ConstValue::NativeUInt(42))
        );
    }

    #[test]
    fn test_const_div() {
        let a = ConstValue::I32(100);
        let b = ConstValue::I32(7);
        assert_eq!(a.div(&b, PointerSize::Bit64), Some(ConstValue::I32(14)));

        // Unsigned division (uses U32 type)
        let a = ConstValue::U32(100);
        let b = ConstValue::U32(7);
        assert_eq!(a.div(&b, PointerSize::Bit64), Some(ConstValue::U32(14)));

        // i64 division
        let a = ConstValue::I64(1000);
        let b = ConstValue::I64(33);
        assert_eq!(a.div(&b, PointerSize::Bit64), Some(ConstValue::I64(30)));
    }

    #[test]
    fn test_const_div_by_zero() {
        let a = ConstValue::I32(100);
        let zero = ConstValue::I32(0);
        assert_eq!(a.div(&zero, PointerSize::Bit64), None);

        let a = ConstValue::U64(100);
        let zero = ConstValue::U64(0);
        assert_eq!(a.div(&zero, PointerSize::Bit64), None);
    }

    #[test]
    fn test_const_div_negative() {
        let a = ConstValue::I32(-100);
        let b = ConstValue::I32(7);
        assert_eq!(a.div(&b, PointerSize::Bit64), Some(ConstValue::I32(-14))); // -100 / 7 = -14
    }

    #[test]
    fn test_const_rem() {
        let a = ConstValue::I32(100);
        let b = ConstValue::I32(7);
        assert_eq!(a.rem(&b, PointerSize::Bit64), Some(ConstValue::I32(2))); // 100 % 7 = 2

        // Unsigned remainder
        let a = ConstValue::U32(100);
        let b = ConstValue::U32(7);
        assert_eq!(a.rem(&b, PointerSize::Bit64), Some(ConstValue::U32(2)));
    }

    #[test]
    fn test_const_rem_by_zero() {
        let a = ConstValue::I32(100);
        let zero = ConstValue::I32(0);
        assert_eq!(a.rem(&zero, PointerSize::Bit64), None);
    }

    #[test]
    fn test_const_rem_negative() {
        let a = ConstValue::I32(-100);
        let b = ConstValue::I32(7);
        assert_eq!(a.rem(&b, PointerSize::Bit64), Some(ConstValue::I32(-2)));
        // -100 % 7 = -2
    }

    #[test]
    fn test_const_shl() {
        let a = ConstValue::I32(1);
        let shift = ConstValue::I32(4);
        assert_eq!(a.shl(&shift, PointerSize::Bit64), Some(ConstValue::I32(16))); // 1 << 4 = 16

        // Larger shift
        let a = ConstValue::I32(1);
        let shift = ConstValue::I32(31);
        assert_eq!(
            a.shl(&shift, PointerSize::Bit64),
            Some(ConstValue::I32(i32::MIN))
        ); // 1 << 31 = MIN_VALUE
    }

    #[test]
    fn test_const_shr_signed() {
        let a = ConstValue::I32(-16);
        let shift = ConstValue::I32(2);
        assert_eq!(
            a.shr(&shift, false, PointerSize::Bit64),
            Some(ConstValue::I32(-4))
        ); // Sign preserved

        let b = ConstValue::I32(16);
        let shift = ConstValue::I32(2);
        assert_eq!(
            b.shr(&shift, false, PointerSize::Bit64),
            Some(ConstValue::I32(4))
        );
    }

    #[test]
    fn test_const_shr_unsigned() {
        let a = ConstValue::I32(-1); // All 1s in two's complement
        let shift = ConstValue::I32(1);
        // Unsigned right shift fills with 0
        let result = a.shr(&shift, true, PointerSize::Bit64);
        assert!(result.is_some());
        // -1 >> 1 (unsigned) = 0x7FFFFFFF
        if let Some(ConstValue::I32(v)) = result {
            assert_eq!(v, 0x7FFFFFFF);
        }
    }

    #[test]
    fn test_const_shr_i64() {
        let a = ConstValue::I64(256);
        let shift = ConstValue::I32(4);
        assert_eq!(
            a.shr(&shift, false, PointerSize::Bit64),
            Some(ConstValue::I64(16))
        );
    }

    #[test]
    fn test_const_cgt() {
        let a = ConstValue::I32(10);
        let b = ConstValue::I32(5);
        assert_eq!(a.cgt(&b), Some(ConstValue::True));
        assert_eq!(b.cgt(&a), Some(ConstValue::False));
        assert_eq!(a.cgt(&a), Some(ConstValue::False)); // Equal means not greater
    }

    #[test]
    fn test_const_cgt_i64() {
        let a = ConstValue::I64(1000);
        let b = ConstValue::I64(500);
        assert_eq!(a.cgt(&b), Some(ConstValue::True));
    }

    #[test]
    fn test_const_clt_negative() {
        let a = ConstValue::I32(-10);
        let b = ConstValue::I32(5);
        assert_eq!(a.clt(&b), Some(ConstValue::True)); // -10 < 5
    }

    #[test]
    fn test_const_float_arithmetic() {
        let a = ConstValue::F32(10.5);
        let b = ConstValue::F32(2.5);
        assert_eq!(a.add(&b, PointerSize::Bit64), Some(ConstValue::F32(13.0)));
        assert_eq!(a.sub(&b, PointerSize::Bit64), Some(ConstValue::F32(8.0)));
        assert_eq!(a.mul(&b, PointerSize::Bit64), Some(ConstValue::F32(26.25)));

        let a = ConstValue::F64(100.0);
        let b = ConstValue::F64(4.0);
        assert_eq!(a.div(&b, PointerSize::Bit64), Some(ConstValue::F64(25.0)));
    }

    #[test]
    fn test_const_float_comparison() {
        let a = ConstValue::F64(std::f64::consts::PI);
        let b = ConstValue::F64(std::f64::consts::E);
        assert_eq!(a.cgt(&b), Some(ConstValue::True));
        assert_eq!(a.clt(&b), Some(ConstValue::False));
        assert_eq!(a.ceq(&a), Some(ConstValue::True));
    }

    #[test]
    fn test_const_float_div_by_zero() {
        let a = ConstValue::F64(1.0);
        let b = ConstValue::F64(0.0);
        // Float div by zero returns inf, not None
        let result = a.div(&b, PointerSize::Bit64);
        assert!(result.is_some());
        if let Some(ConstValue::F64(v)) = result {
            assert!(v.is_infinite());
        }
    }

    #[test]
    fn test_is_zero_integer_types() {
        // Test is_zero for integer types (does not include floats)
        assert!(ConstValue::I8(0).is_zero());
        assert!(ConstValue::I16(0).is_zero());
        assert!(ConstValue::I32(0).is_zero());
        assert!(ConstValue::I64(0).is_zero());
        assert!(ConstValue::U8(0).is_zero());
        assert!(ConstValue::U16(0).is_zero());
        assert!(ConstValue::U32(0).is_zero());
        assert!(ConstValue::U64(0).is_zero());
        assert!(ConstValue::NativeInt(0).is_zero());
        assert!(ConstValue::NativeUInt(0).is_zero());
        assert!(ConstValue::False.is_zero());

        // Non-zero values
        assert!(!ConstValue::I32(1).is_zero());
        assert!(!ConstValue::True.is_zero());
    }

    #[test]
    fn test_is_one_integer_types() {
        // Test is_one for integer types (does not include floats)
        assert!(ConstValue::I8(1).is_one());
        assert!(ConstValue::I16(1).is_one());
        assert!(ConstValue::I32(1).is_one());
        assert!(ConstValue::I64(1).is_one());
        assert!(ConstValue::U8(1).is_one());
        assert!(ConstValue::U16(1).is_one());
        assert!(ConstValue::U32(1).is_one());
        assert!(ConstValue::U64(1).is_one());
        assert!(ConstValue::NativeInt(1).is_one());
        assert!(ConstValue::NativeUInt(1).is_one());
        assert!(ConstValue::True.is_one());

        // Non-one values
        assert!(!ConstValue::I32(0).is_one());
        assert!(!ConstValue::False.is_one());
    }

    #[test]
    fn test_is_minus_one_signed_types() {
        // Test is_minus_one for signed integer types only
        assert!(ConstValue::I8(-1).is_minus_one());
        assert!(ConstValue::I16(-1).is_minus_one());
        assert!(ConstValue::I32(-1).is_minus_one());
        assert!(ConstValue::I64(-1).is_minus_one());
        assert!(ConstValue::NativeInt(-1).is_minus_one());

        // Unsigned types cannot be -1
        assert!(!ConstValue::U8(255).is_minus_one());
        assert!(!ConstValue::U32(0xFFFFFFFF).is_minus_one());
    }

    #[test]
    fn test_zero_of_same_type_preserves_type() {
        // Integer types
        assert_eq!(ConstValue::I8(42).zero_of_same_type(), ConstValue::I8(0));
        assert_eq!(ConstValue::I16(42).zero_of_same_type(), ConstValue::I16(0));
        assert_eq!(ConstValue::I32(42).zero_of_same_type(), ConstValue::I32(0));
        assert_eq!(ConstValue::I64(42).zero_of_same_type(), ConstValue::I64(0));
        assert_eq!(ConstValue::U8(42).zero_of_same_type(), ConstValue::U8(0));
        assert_eq!(ConstValue::U16(42).zero_of_same_type(), ConstValue::U16(0));
        assert_eq!(ConstValue::U32(42).zero_of_same_type(), ConstValue::U32(0));
        assert_eq!(ConstValue::U64(42).zero_of_same_type(), ConstValue::U64(0));
        assert_eq!(
            ConstValue::NativeInt(42).zero_of_same_type(),
            ConstValue::NativeInt(0)
        );
        assert_eq!(
            ConstValue::NativeUInt(42).zero_of_same_type(),
            ConstValue::NativeUInt(0)
        );

        // Floating point types
        assert_eq!(
            ConstValue::F32(std::f32::consts::PI).zero_of_same_type(),
            ConstValue::F32(0.0)
        );
        assert_eq!(
            ConstValue::F64(std::f64::consts::PI).zero_of_same_type(),
            ConstValue::F64(0.0)
        );

        // Non-numeric types default to I32
        assert_eq!(ConstValue::Null.zero_of_same_type(), ConstValue::I32(0));
        assert_eq!(ConstValue::True.zero_of_same_type(), ConstValue::I32(0));
    }

    #[test]
    fn test_abstract_value_join() {
        let top = AbstractValue::Top;
        let bottom = AbstractValue::Bottom;
        let const_a = AbstractValue::Constant(ConstValue::I32(42));
        let const_b = AbstractValue::Constant(ConstValue::I32(42));
        let const_c = AbstractValue::Constant(ConstValue::I32(99));

        // Top joins with anything gives Top
        assert_eq!(top.join(&const_a), AbstractValue::Top);
        assert_eq!(const_a.join(&top), AbstractValue::Top);

        // Bottom joins with anything gives that thing
        assert_eq!(bottom.join(&const_a), const_a);
        assert_eq!(const_a.join(&bottom), const_a);

        // Same constants join to same constant
        assert_eq!(const_a.join(&const_b), const_a);

        // Different constants join to Top
        assert_eq!(const_a.join(&const_c), AbstractValue::Top);
    }

    #[test]
    fn test_abstract_value_is_constant() {
        let top = AbstractValue::Top;
        let bottom = AbstractValue::Bottom;
        let const_val = AbstractValue::Constant(ConstValue::I32(42));

        assert!(!top.is_constant());
        assert!(!bottom.is_constant());
        assert!(const_val.is_constant());
    }

    #[test]
    fn test_abstract_value_as_constant() {
        let const_val = AbstractValue::Constant(ConstValue::I32(42));
        assert_eq!(const_val.as_constant(), Some(&ConstValue::I32(42)));

        let top = AbstractValue::Top;
        assert_eq!(top.as_constant(), None);
    }

    #[test]
    fn test_as_i64() {
        // Signed types
        assert_eq!(ConstValue::I8(-100).as_i64(), Some(-100));
        assert_eq!(ConstValue::I16(-30000).as_i64(), Some(-30000));
        assert_eq!(
            ConstValue::I32(-2_000_000_000).as_i64(),
            Some(-2_000_000_000)
        );
        assert_eq!(
            ConstValue::I64(-9_000_000_000_000).as_i64(),
            Some(-9_000_000_000_000)
        );
        assert_eq!(ConstValue::NativeInt(42).as_i64(), Some(42));

        // Unsigned types up to U32 are included
        assert_eq!(ConstValue::U8(200).as_i64(), Some(200));
        assert_eq!(ConstValue::U16(60000).as_i64(), Some(60000));
        assert_eq!(ConstValue::U32(4_000_000_000).as_i64(), Some(4_000_000_000));

        // U64 is not included in as_i64
        assert_eq!(ConstValue::U64(1_000_000).as_i64(), None);

        // Bool values
        assert_eq!(ConstValue::True.as_i64(), Some(1));
        assert_eq!(ConstValue::False.as_i64(), Some(0));
    }

    #[test]
    fn test_as_u64() {
        assert_eq!(ConstValue::U8(200).as_u64(), Some(200));
        assert_eq!(ConstValue::U16(60000).as_u64(), Some(60000));
        assert_eq!(ConstValue::U32(4_000_000_000).as_u64(), Some(4_000_000_000));
        assert_eq!(ConstValue::U64(u64::MAX).as_u64(), Some(u64::MAX));
        // Positive signed values convert fine
        assert_eq!(ConstValue::I32(100).as_u64(), Some(100));
        // Negative signed values don't convert
        assert_eq!(ConstValue::I32(-1).as_u64(), None);
    }

    #[test]
    fn test_cross_type_arithmetic() {
        // I32 + I64 promotes to I64
        let i32_val = ConstValue::I32(10);
        let i64_val = ConstValue::I64(20);
        assert_eq!(
            i32_val.add(&i64_val, PointerSize::Bit64),
            Some(ConstValue::I64(30))
        );

        // U32 + U64 promotes to U64
        let u32_val = ConstValue::U32(100);
        let u64_val = ConstValue::U64(200);
        assert_eq!(
            u32_val.add(&u64_val, PointerSize::Bit64),
            Some(ConstValue::U64(300))
        );
    }

    #[test]
    fn test_incompatible_type_arithmetic() {
        // I32 + F32 returns None (incompatible)
        let i32_val = ConstValue::I32(10);
        let f32_val = ConstValue::F32(std::f32::consts::PI);
        assert!(i32_val.add(&f32_val, PointerSize::Bit64).is_none());

        // U64 + I64 returns None (signed vs unsigned)
        let u64_val = ConstValue::U64(100);
        let i64_val = ConstValue::I64(200);
        assert!(u64_val.add(&i64_val, PointerSize::Bit64).is_none());
    }

    #[test]
    fn test_null_const_value() {
        let null = ConstValue::Null;
        assert!(null.is_null());
        assert!(!null.is_one());
        assert!(!null.is_bool());
    }

    #[test]
    fn test_string_const_value() {
        let s = ConstValue::String(123);
        assert!(!s.is_zero());
        assert!(!s.is_one());
    }

    #[test]
    fn test_bool_const_values() {
        let t = ConstValue::True;
        let f = ConstValue::False;

        assert!(t.is_bool());
        assert!(f.is_bool());
        assert!(t.is_one());
        assert!(f.is_zero());
    }
}