tldr-core 0.1.4

Core analysis engine for TLDR code analysis tool
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
//! SSA-Based Analyses
//!
//! Provides analyses that leverage SSA form:
//! - Value Numbering (Global Value Numbering)
//! - SCCP (Sparse Conditional Constant Propagation)
//! - Dead Code Detection
//! - Live Variables Analysis
//! - Available Expressions

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

use crate::types::{CfgInfo, Language};
use crate::TldrResult;

use super::types::{SsaFunction, SsaNameId};

// =============================================================================
// Live Variables Analysis
// =============================================================================

/// Live variables at each program point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiveVariables {
    /// Function name
    pub function: String,
    /// For each block, variables live at entry and exit
    pub blocks: HashMap<usize, LiveSets>,
}

/// Live variable sets for a block
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiveSets {
    /// Variables live at block entry
    pub live_in: HashSet<String>,
    /// Variables live at block exit
    pub live_out: HashSet<String>,
}

/// Compute live variables (backward may-analysis)
///
/// # Dataflow Equations
/// ```text
/// OUT[B] = union IN[S] for all successors S
/// IN[B] = USE[B] union (OUT[B] - DEF[B])
///
/// USE[B] = variables used before defined in B
/// DEF[B] = variables defined in B
/// ```
///
/// # Algorithm
/// 1. Build line-to-block mapping
/// 2. Compute USE and DEF sets for each block
/// 3. Iterate backward dataflow until fixed point
pub fn compute_live_variables(
    cfg: &CfgInfo,
    refs: &[crate::types::VarRef],
) -> TldrResult<LiveVariables> {
    use crate::types::RefType;

    // Build line-to-block mapping
    let line_to_block: HashMap<u32, usize> = cfg
        .blocks
        .iter()
        .flat_map(|block| (block.lines.0..=block.lines.1).map(move |line| (line, block.id)))
        .collect();

    // Build successor map for looking up successors by block ID
    let mut successors: HashMap<usize, Vec<usize>> = HashMap::new();
    for block in &cfg.blocks {
        successors.entry(block.id).or_default();
    }
    for edge in &cfg.edges {
        successors.entry(edge.from).or_default().push(edge.to);
    }

    // Compute USE and DEF sets for each block
    // USE[B] = variables used before being defined in B
    // DEF[B] = variables defined in B
    let mut use_sets: HashMap<usize, HashSet<String>> = HashMap::new();
    let mut def_sets: HashMap<usize, HashSet<String>> = HashMap::new();

    for block in &cfg.blocks {
        use_sets.insert(block.id, HashSet::new());
        def_sets.insert(block.id, HashSet::new());
    }

    // Group refs by block
    let mut block_refs: HashMap<usize, Vec<&crate::types::VarRef>> = HashMap::new();
    for var_ref in refs {
        if let Some(&block_id) = line_to_block.get(&var_ref.line) {
            block_refs.entry(block_id).or_default().push(var_ref);
        }
    }

    // Process each block to compute USE and DEF
    for (&block_id, refs_in_block) in &block_refs {
        // Sort refs by line number to process in order
        let mut sorted_refs: Vec<_> = refs_in_block.iter().collect();
        sorted_refs.sort_by_key(|r| r.line);

        let use_set = use_sets.get_mut(&block_id).unwrap();
        let def_set = def_sets.get_mut(&block_id).unwrap();

        for var_ref in sorted_refs {
            match var_ref.ref_type {
                RefType::Use => {
                    // If not already defined in this block, it's a USE
                    if !def_set.contains(&var_ref.name) {
                        use_set.insert(var_ref.name.clone());
                    }
                }
                RefType::Definition => {
                    def_set.insert(var_ref.name.clone());
                }
                RefType::Update => {
                    // Update is USE then DEF
                    // First, it's a use if not already defined
                    if !def_set.contains(&var_ref.name) {
                        use_set.insert(var_ref.name.clone());
                    }
                    // Then it's a definition
                    def_set.insert(var_ref.name.clone());
                }
            }
        }
    }

    // Initialize live_in and live_out sets
    let mut live_in: HashMap<usize, HashSet<String>> = HashMap::new();
    let mut live_out: HashMap<usize, HashSet<String>> = HashMap::new();

    for block in &cfg.blocks {
        live_in.insert(block.id, HashSet::new());
        live_out.insert(block.id, HashSet::new());
    }

    // Backward dataflow iteration until fixed point
    // Process in reverse topological order for faster convergence
    let block_ids: Vec<usize> = cfg.blocks.iter().map(|b| b.id).collect();
    let max_iterations = block_ids.len() * 2 + 10; // Safety limit

    let mut changed = true;
    let mut iterations = 0;

    while changed && iterations < max_iterations {
        changed = false;
        iterations += 1;

        // Process blocks in reverse order (approximation of reverse postorder)
        for &block_id in block_ids.iter().rev() {
            // OUT[B] = union of IN[S] for all successors S
            let mut new_out: HashSet<String> = HashSet::new();
            if let Some(succs) = successors.get(&block_id) {
                for &succ_id in succs {
                    if let Some(succ_in) = live_in.get(&succ_id) {
                        new_out.extend(succ_in.iter().cloned());
                    }
                }
            }

            // IN[B] = USE[B] union (OUT[B] - DEF[B])
            let use_b = use_sets.get(&block_id).cloned().unwrap_or_default();
            let def_b = def_sets.get(&block_id).cloned().unwrap_or_default();

            let out_minus_def: HashSet<String> = new_out
                .difference(&def_b)
                .cloned()
                .collect();

            let mut new_in = use_b;
            new_in.extend(out_minus_def);

            // Check for changes
            if &new_in != live_in.get(&block_id).unwrap() {
                changed = true;
                live_in.insert(block_id, new_in);
            }
            if &new_out != live_out.get(&block_id).unwrap() {
                changed = true;
                live_out.insert(block_id, new_out);
            }
        }
    }

    // Build result
    let mut blocks_result = HashMap::new();
    for block in &cfg.blocks {
        blocks_result.insert(
            block.id,
            LiveSets {
                live_in: live_in.get(&block.id).cloned().unwrap_or_default(),
                live_out: live_out.get(&block.id).cloned().unwrap_or_default(),
            },
        );
    }

    Ok(LiveVariables {
        function: cfg.function.clone(),
        blocks: blocks_result,
    })
}

/// Check if a variable is live at the entry of a block
impl LiveVariables {
    /// Returns true if the variable is live at the entry of the given block
    pub fn is_live_in(&self, block: usize, var: &str) -> bool {
        self.blocks
            .get(&block)
            .map(|sets| sets.live_in.contains(var))
            .unwrap_or(false)
    }

    /// Returns true if the variable is live at the exit of the given block
    pub fn is_live_out(&self, block: usize, var: &str) -> bool {
        self.blocks
            .get(&block)
            .map(|sets| sets.live_out.contains(var))
            .unwrap_or(false)
    }
}

// =============================================================================
// Value Numbering
// =============================================================================

/// Value numbering result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValueNumbering {
    /// Function name
    pub function: String,
    /// Value number for each SSA name
    pub value_numbers: HashMap<SsaNameId, u32>,
    /// SSA names with the same value number (potential CSE)
    pub equivalences: HashMap<u32, Vec<SsaNameId>>,
}

/// Canonical expression for value numbering
/// Expressions are normalized for comparison (e.g., commutative ops sorted)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum CanonicalExpr {
    /// Parameter or constant with no operands
    Leaf(String),
    /// Binary operation with canonicalized operands (sorted for commutative ops)
    BinaryOp {
        op: String,
        /// Value numbers of operands (sorted for commutative ops)
        operands: (u32, u32),
    },
    /// Unary operation
    UnaryOp {
        op: String,
        operand: u32,
    },
    /// Phi function with value numbers of sources
    Phi(Vec<u32>),
    /// Call (each call gets unique number - not CSE-able)
    Call(u32),
}

/// Compute value numbers for SSA names
///
/// # Algorithm (Global Value Numbering)
/// ```text
/// For each SSA name n:
///   expr = canonical expression computing n
///   if expr in hash_table:
///     value_number[n] = hash_table[expr]
///   else:
///     value_number[n] = next_number++
///     hash_table[expr] = value_number[n]
/// ```
///
/// # Postconditions
/// - Expressions with same operands get same value number
/// - `equivalences` maps value number to all equivalent SSA names
pub fn compute_value_numbers(ssa: &SsaFunction) -> TldrResult<ValueNumbering> {
    use super::types::SsaInstructionKind;

    let mut value_numbers: HashMap<SsaNameId, u32> = HashMap::new();
    let mut expr_to_number: HashMap<CanonicalExpr, u32> = HashMap::new();
    let mut next_number: u32 = 0;
    let mut call_counter: u32 = 0;

    // Helper to get or create value number for an expression
    let get_or_create_number = |expr: CanonicalExpr,
                                    expr_to_num: &mut HashMap<CanonicalExpr, u32>,
                                    next_num: &mut u32|
     -> u32 {
        if let Some(&num) = expr_to_num.get(&expr) {
            num
        } else {
            let num = *next_num;
            *next_num += 1;
            expr_to_num.insert(expr, num);
            num
        }
    };

    // Process blocks in dominator tree order (approximated by block ID order)
    // This ensures we see definitions before uses
    for block in &ssa.blocks {
        // Process phi functions first
        for phi in &block.phi_functions {
            // Get value numbers of sources (if available)
            let source_numbers: Vec<u32> = phi
                .sources
                .iter()
                .filter_map(|s| value_numbers.get(&s.name).copied())
                .collect();

            // Create canonical expression for phi
            let expr = CanonicalExpr::Phi(source_numbers);
            let vn = get_or_create_number(expr, &mut expr_to_number, &mut next_number);
            value_numbers.insert(phi.target, vn);
        }

        // Process instructions
        for instr in &block.instructions {
            if let Some(target) = instr.target {
                // Get value numbers of operands
                let use_numbers: Vec<u32> = instr
                    .uses
                    .iter()
                    .filter_map(|u| value_numbers.get(u).copied())
                    .collect();

                // Create canonical expression based on instruction kind
                let expr = match &instr.kind {
                    SsaInstructionKind::Param | SsaInstructionKind::Assign => {
                        if use_numbers.is_empty() {
                            // Constant or parameter - unique value based on source text
                            let key = instr
                                .source_text
                                .clone()
                                .unwrap_or_else(|| format!("const_{}", target.0));
                            CanonicalExpr::Leaf(key)
                        } else if use_numbers.len() == 1 {
                            // Simple assignment x = y - same value number as source
                            let source_vn = use_numbers[0];
                            // Reuse source value number directly
                            value_numbers.insert(target, source_vn);
                            continue;
                        } else {
                            // Multiple uses - treat as leaf with unique key
                            CanonicalExpr::Leaf(format!("assign_{}", target.0))
                        }
                    }
                    SsaInstructionKind::BinaryOp => {
                        if use_numbers.len() >= 2 {
                            let (left, right) = (use_numbers[0], use_numbers[1]);
                            // Canonicalize commutative operations (sort operands)
                            let op_name = instr
                                .source_text
                                .as_ref()
                                .and_then(|s| {
                                    // Extract operator from source text like "x = a + b"
                                    if s.contains('+') {
                                        Some("+")
                                    } else if s.contains('*') {
                                        Some("*")
                                    } else if s.contains('-') {
                                        Some("-")
                                    } else if s.contains('/') {
                                        Some("/")
                                    } else {
                                        None
                                    }
                                })
                                .unwrap_or("binop");

                            // Sort operands for commutative operations
                            let is_commutative = op_name == "+" || op_name == "*";
                            let operands = if is_commutative && left > right {
                                (right, left)
                            } else {
                                (left, right)
                            };

                            CanonicalExpr::BinaryOp {
                                op: op_name.to_string(),
                                operands,
                            }
                        } else {
                            CanonicalExpr::Leaf(format!("binop_{}", target.0))
                        }
                    }
                    SsaInstructionKind::UnaryOp => {
                        if !use_numbers.is_empty() {
                            CanonicalExpr::UnaryOp {
                                op: "unary".to_string(),
                                operand: use_numbers[0],
                            }
                        } else {
                            CanonicalExpr::Leaf(format!("unary_{}", target.0))
                        }
                    }
                    SsaInstructionKind::Call => {
                        // Each call gets a unique value number (not CSE-able due to side effects)
                        call_counter += 1;
                        CanonicalExpr::Call(call_counter)
                    }
                    SsaInstructionKind::Return | SsaInstructionKind::Branch => {
                        // These typically don't define values
                        continue;
                    }
                };

                let vn = get_or_create_number(expr, &mut expr_to_number, &mut next_number);
                value_numbers.insert(target, vn);
            }
        }
    }

    // Build equivalences map (group SSA names by value number)
    let mut equivalences: HashMap<u32, Vec<SsaNameId>> = HashMap::new();
    for (&name, &vn) in &value_numbers {
        equivalences.entry(vn).or_default().push(name);
    }

    Ok(ValueNumbering {
        function: ssa.function.clone(),
        value_numbers,
        equivalences,
    })
}

// =============================================================================
// SCCP (Sparse Conditional Constant Propagation)
// =============================================================================

/// SCCP lattice value
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum LatticeValue {
    /// Unknown (top) - not yet analyzed
    Top,
    /// Constant value
    Constant(ConstantValue),
    /// Varying (bottom) - not constant
    Bottom,
}

/// Constant value representation
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ConstantValue {
    /// Integer constant
    Int(i64),
    /// Float constant (as string to avoid comparison issues)
    Float(String),
    /// String constant
    String(String),
    /// Boolean constant
    Bool(bool),
    /// None/null constant
    None,
}

impl std::fmt::Display for ConstantValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConstantValue::Int(i) => write!(f, "{}", i),
            ConstantValue::Float(fl) => write!(f, "{}", fl),
            ConstantValue::String(s) => write!(f, "\"{}\"", s),
            ConstantValue::Bool(b) => write!(f, "{}", b),
            ConstantValue::None => write!(f, "None"),
        }
    }
}

/// SCCP analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SccpResult {
    /// Function name
    pub function: String,
    /// Constant values for SSA names
    pub constants: HashMap<SsaNameId, ConstantValue>,
    /// Unreachable blocks
    pub unreachable_blocks: HashSet<usize>,
    /// Dead SSA names (definitions never used)
    pub dead_names: HashSet<SsaNameId>,
}

/// Lattice meet operation: combines two lattice values
fn lattice_meet(a: &LatticeValue, b: &LatticeValue) -> LatticeValue {
    match (a, b) {
        // Top meet X = X
        (LatticeValue::Top, x) | (x, LatticeValue::Top) => x.clone(),
        // C meet C = C (if same constant)
        (LatticeValue::Constant(c1), LatticeValue::Constant(c2)) if c1 == c2 => {
            LatticeValue::Constant(c1.clone())
        }
        // Otherwise = Bottom
        _ => LatticeValue::Bottom,
    }
}

/// Try to parse a constant value from source text
fn parse_constant(source_text: &str) -> Option<ConstantValue> {
    let trimmed = source_text.trim();

    // Try to extract RHS of assignment (e.g., "x = 1" -> "1")
    let value_part = if let Some(idx) = trimmed.find('=') {
        trimmed[idx + 1..].trim()
    } else {
        trimmed
    };

    // Parse boolean
    if value_part == "True" || value_part == "true" {
        return Some(ConstantValue::Bool(true));
    }
    if value_part == "False" || value_part == "false" {
        return Some(ConstantValue::Bool(false));
    }

    // Parse None/null
    if value_part == "None" || value_part == "null" || value_part == "nil" {
        return Some(ConstantValue::None);
    }

    // Parse integer
    if let Ok(i) = value_part.parse::<i64>() {
        return Some(ConstantValue::Int(i));
    }

    // Parse float (as string to avoid comparison issues)
    if value_part.contains('.') && value_part.parse::<f64>().is_ok() {
        return Some(ConstantValue::Float(value_part.to_string()));
    }

    // Parse string literal
    if (value_part.starts_with('"') && value_part.ends_with('"'))
        || (value_part.starts_with('\'') && value_part.ends_with('\''))
    {
        let inner = &value_part[1..value_part.len() - 1];
        return Some(ConstantValue::String(inner.to_string()));
    }

    None
}

/// Evaluate a binary operation on constants
fn eval_binary_op(op: &str, left: &ConstantValue, right: &ConstantValue) -> Option<ConstantValue> {
    match (left, right) {
        (ConstantValue::Int(l), ConstantValue::Int(r)) => {
            let result = match op {
                "+" => l.checked_add(*r)?,
                "-" => l.checked_sub(*r)?,
                "*" => l.checked_mul(*r)?,
                "/" => {
                    if *r == 0 {
                        return None;
                    }
                    l.checked_div(*r)?
                }
                "%" => {
                    if *r == 0 {
                        return None;
                    }
                    l.checked_rem(*r)?
                }
                "<" => return Some(ConstantValue::Bool(l < r)),
                ">" => return Some(ConstantValue::Bool(l > r)),
                "<=" => return Some(ConstantValue::Bool(l <= r)),
                ">=" => return Some(ConstantValue::Bool(l >= r)),
                "==" => return Some(ConstantValue::Bool(l == r)),
                "!=" => return Some(ConstantValue::Bool(l != r)),
                _ => return None,
            };
            Some(ConstantValue::Int(result))
        }
        (ConstantValue::Bool(l), ConstantValue::Bool(r)) => {
            let result = match op {
                "and" | "&&" => *l && *r,
                "or" | "||" => *l || *r,
                "==" => l == r,
                "!=" => l != r,
                _ => return None,
            };
            Some(ConstantValue::Bool(result))
        }
        _ => None,
    }
}

/// Sparse Conditional Constant Propagation
///
/// # Algorithm (Wegman-Zadeck)
/// ```text
/// Initialize:
///   All SSA names = Top (unknown)
///   All blocks = unreachable
///   Worklist = {entry block}
///
/// Iterate:
///   While worklist not empty:
///     For executable blocks:
///       Evaluate instructions
///       Meet values: Top meet C = C, C meet C = C, else Bottom
///     For conditional branches:
///       Only add target if condition known true/false or unknown
/// ```
///
/// # Postconditions
/// - `constants` maps SSA names to their constant values
/// - `unreachable_blocks` contains provably dead code
/// - `dead_names` contains SSA names never used
pub fn run_sccp(ssa: &SsaFunction) -> TldrResult<SccpResult> {
    use super::types::SsaInstructionKind;
    use std::collections::VecDeque;

    // Initialize all SSA names to Top
    let mut lattice_values: HashMap<SsaNameId, LatticeValue> = HashMap::new();
    for name in &ssa.ssa_names {
        lattice_values.insert(name.id, LatticeValue::Top);
    }

    // Track executable blocks and edges
    let mut executable_blocks: HashSet<usize> = HashSet::new();
    let mut executable_edges: HashSet<(usize, usize)> = HashSet::new();

    // Two worklists: CFG edges and SSA edges
    let mut cfg_worklist: VecDeque<usize> = VecDeque::new();
    let mut ssa_worklist: VecDeque<SsaNameId> = VecDeque::new();

    // Find entry block
    let entry_block = ssa.blocks.first().map(|b| b.id).unwrap_or(0);
    cfg_worklist.push_back(entry_block);

    // Build block ID to block index map
    let block_index: HashMap<usize, usize> = ssa
        .blocks
        .iter()
        .enumerate()
        .map(|(i, b)| (b.id, i))
        .collect();

    // Build use map: SSA name -> instructions that use it
    let mut use_map: HashMap<SsaNameId, Vec<(usize, usize)>> = HashMap::new(); // name -> (block_id, instr_idx)
    for block in &ssa.blocks {
        for (instr_idx, instr) in block.instructions.iter().enumerate() {
            for &use_name in &instr.uses {
                use_map
                    .entry(use_name)
                    .or_default()
                    .push((block.id, instr_idx));
            }
        }
    }

    // Main SCCP loop
    let max_iterations = ssa.blocks.len() * ssa.ssa_names.len() + 100;
    let mut iterations = 0;

    while (!cfg_worklist.is_empty() || !ssa_worklist.is_empty()) && iterations < max_iterations {
        iterations += 1;

        // Process CFG worklist
        while let Some(block_id) = cfg_worklist.pop_front() {
            if !executable_blocks.insert(block_id) {
                continue; // Already processed
            }

            let Some(&block_idx) = block_index.get(&block_id) else {
                continue;
            };
            let block = &ssa.blocks[block_idx];

            // Process phi functions
            for phi in &block.phi_functions {
                // Evaluate phi considering only executable edges
                let mut result = LatticeValue::Top;
                for source in &phi.sources {
                    if executable_edges.contains(&(source.block, block_id))
                        || executable_blocks.contains(&source.block)
                    {
                        if let Some(source_val) = lattice_values.get(&source.name) {
                            result = lattice_meet(&result, source_val);
                        }
                    }
                }

                // Update if changed
                if let Some(current) = lattice_values.get(&phi.target) {
                    if *current != result {
                        lattice_values.insert(phi.target, result);
                        ssa_worklist.push_back(phi.target);
                    }
                }
            }

            // Process instructions and track if we have a branch
            let mut has_branch = false;

            for instr in &block.instructions {
                if let Some(target) = instr.target {
                    let new_value = evaluate_instruction(instr, &lattice_values);

                    if let Some(current) = lattice_values.get(&target) {
                        if *current != new_value {
                            lattice_values.insert(target, new_value);
                            ssa_worklist.push_back(target);
                        }
                    } else {
                        lattice_values.insert(target, new_value);
                        ssa_worklist.push_back(target);
                    }
                }

                // Handle conditional branches
                if instr.kind == SsaInstructionKind::Branch {
                    has_branch = true;

                    if !instr.uses.is_empty() {
                        let cond_name = instr.uses[0];
                        let cond_value = lattice_values.get(&cond_name);

                        match cond_value {
                            Some(LatticeValue::Constant(ConstantValue::Bool(true))) => {
                                // Only true branch is executable (first successor)
                                if let Some(&succ) = block.successors.first() {
                                    executable_edges.insert((block_id, succ));
                                    cfg_worklist.push_back(succ);
                                }
                            }
                            Some(LatticeValue::Constant(ConstantValue::Bool(false))) => {
                                // Only false branch is executable (second successor)
                                if let Some(&succ) = block.successors.get(1) {
                                    executable_edges.insert((block_id, succ));
                                    cfg_worklist.push_back(succ);
                                }
                            }
                            _ => {
                                // Unknown - both branches executable
                                for &succ in &block.successors {
                                    executable_edges.insert((block_id, succ));
                                    cfg_worklist.push_back(succ);
                                }
                            }
                        }
                    } else {
                        // Unconditional branch - all successors executable
                        for &succ in &block.successors {
                            executable_edges.insert((block_id, succ));
                            cfg_worklist.push_back(succ);
                        }
                    }
                }
            }

            // If no branch instruction, fall through to successors
            if !has_branch {
                for &succ in &block.successors {
                    if !executable_blocks.contains(&succ) {
                        executable_edges.insert((block_id, succ));
                        cfg_worklist.push_back(succ);
                    }
                }
            }
        }

        // Process SSA worklist - propagate constant values to uses
        while let Some(name) = ssa_worklist.pop_front() {
            if let Some(uses) = use_map.get(&name) {
                for &(block_id, _instr_idx) in uses {
                    if executable_blocks.contains(&block_id) {
                        // Re-evaluate this block's instructions
                        cfg_worklist.push_back(block_id);
                    }
                }
            }
        }
    }

    // Collect results
    let mut constants: HashMap<SsaNameId, ConstantValue> = HashMap::new();
    for (name, value) in &lattice_values {
        if let LatticeValue::Constant(c) = value {
            constants.insert(*name, c.clone());
        }
    }

    // Find unreachable blocks
    let all_blocks: HashSet<usize> = ssa.blocks.iter().map(|b| b.id).collect();
    let unreachable_blocks: HashSet<usize> = all_blocks
        .difference(&executable_blocks)
        .copied()
        .collect();

    // Find dead names (not used anywhere)
    let mut used_names: HashSet<SsaNameId> = HashSet::new();
    for block in &ssa.blocks {
        if executable_blocks.contains(&block.id) {
            for instr in &block.instructions {
                used_names.extend(instr.uses.iter().copied());
            }
            for phi in &block.phi_functions {
                for source in &phi.sources {
                    used_names.insert(source.name);
                }
            }
        }
    }

    let all_names: HashSet<SsaNameId> = ssa.ssa_names.iter().map(|n| n.id).collect();
    let dead_names: HashSet<SsaNameId> = all_names.difference(&used_names).copied().collect();

    Ok(SccpResult {
        function: ssa.function.clone(),
        constants,
        unreachable_blocks,
        dead_names,
    })
}

/// Evaluate an instruction to get its lattice value
fn evaluate_instruction(
    instr: &super::types::SsaInstruction,
    lattice_values: &HashMap<SsaNameId, LatticeValue>,
) -> LatticeValue {
    use super::types::SsaInstructionKind;

    match &instr.kind {
        SsaInstructionKind::Param => {
            // Parameters are not constant (unless we have interprocedural info)
            LatticeValue::Bottom
        }
        SsaInstructionKind::Assign => {
            if instr.uses.is_empty() {
                // Constant assignment
                if let Some(ref src) = instr.source_text {
                    if let Some(c) = parse_constant(src) {
                        return LatticeValue::Constant(c);
                    }
                }
                LatticeValue::Bottom
            } else if instr.uses.len() == 1 {
                // Copy: propagate source value
                lattice_values
                    .get(&instr.uses[0])
                    .cloned()
                    .unwrap_or(LatticeValue::Top)
            } else {
                LatticeValue::Bottom
            }
        }
        SsaInstructionKind::BinaryOp => {
            if instr.uses.len() >= 2 {
                let left = lattice_values
                    .get(&instr.uses[0])
                    .cloned()
                    .unwrap_or(LatticeValue::Top);
                let right = lattice_values
                    .get(&instr.uses[1])
                    .cloned()
                    .unwrap_or(LatticeValue::Top);

                match (&left, &right) {
                    (LatticeValue::Bottom, _) | (_, LatticeValue::Bottom) => LatticeValue::Bottom,
                    (LatticeValue::Top, _) | (_, LatticeValue::Top) => LatticeValue::Top,
                    (LatticeValue::Constant(l), LatticeValue::Constant(r)) => {
                        // Try to evaluate the operation
                        let op = instr.source_text.as_ref().and_then(|s| {
                            if s.contains('+') {
                                Some("+")
                            } else if s.contains('-') && !s.starts_with('-') {
                                Some("-")
                            } else if s.contains('*') {
                                Some("*")
                            } else if s.contains('/') {
                                Some("/")
                            } else if s.contains('<') && s.contains('=') {
                                Some("<=")
                            } else if s.contains('>') && s.contains('=') {
                                Some(">=")
                            } else if s.contains('<') {
                                Some("<")
                            } else if s.contains('>') {
                                Some(">")
                            } else if s.contains("==") {
                                Some("==")
                            } else if s.contains("!=") {
                                Some("!=")
                            } else {
                                None
                            }
                        });

                        if let Some(op) = op {
                            if let Some(result) = eval_binary_op(op, l, r) {
                                return LatticeValue::Constant(result);
                            }
                        }
                        LatticeValue::Bottom
                    }
                }
            } else {
                LatticeValue::Bottom
            }
        }
        SsaInstructionKind::UnaryOp => {
            if !instr.uses.is_empty() {
                let operand = lattice_values
                    .get(&instr.uses[0])
                    .cloned()
                    .unwrap_or(LatticeValue::Top);

                match operand {
                    LatticeValue::Bottom => LatticeValue::Bottom,
                    LatticeValue::Top => LatticeValue::Top,
                    LatticeValue::Constant(c) => {
                        // Try to evaluate unary ops
                        if let Some(ref src) = instr.source_text {
                            if src.contains("not") || src.contains('!') {
                                if let ConstantValue::Bool(b) = c {
                                    return LatticeValue::Constant(ConstantValue::Bool(!b));
                                }
                            }
                            if src.contains('-') {
                                if let ConstantValue::Int(i) = c {
                                    if let Some(neg) = i.checked_neg() {
                                        return LatticeValue::Constant(ConstantValue::Int(neg));
                                    }
                                }
                            }
                        }
                        LatticeValue::Bottom
                    }
                }
            } else {
                LatticeValue::Bottom
            }
        }
        SsaInstructionKind::Call => {
            // Calls are not constant (could have side effects)
            LatticeValue::Bottom
        }
        SsaInstructionKind::Return | SsaInstructionKind::Branch => {
            // These don't produce values
            LatticeValue::Bottom
        }
    }
}

// =============================================================================
// Dead Code Detection
// =============================================================================

/// Find dead code using SSA def-use information
///
/// # Algorithm
/// A definition is dead if:
/// 1. It has no uses (except if it has side effects)
/// 2. Iteratively remove until fixed point (cascading dead code)
///
/// # Returns
/// Vector of dead SSA names
pub fn find_dead_code(ssa: &SsaFunction) -> TldrResult<Vec<SsaNameId>> {
    // Build use count map: how many times each SSA name is used
    let mut use_count: HashMap<SsaNameId, usize> = HashMap::new();

    // Initialize all SSA names with 0 uses
    for name in &ssa.ssa_names {
        use_count.insert(name.id, 0);
    }

    // Count uses from instructions
    for block in &ssa.blocks {
        for instr in &block.instructions {
            for &use_name in &instr.uses {
                *use_count.entry(use_name).or_insert(0) += 1;
            }
        }
        // Count uses from phi functions
        for phi in &block.phi_functions {
            for source in &phi.sources {
                *use_count.entry(source.name).or_insert(0) += 1;
            }
        }
    }

    // Build def map: SSA name -> (block_id, instruction)
    let mut def_info: HashMap<SsaNameId, (usize, Option<&super::types::SsaInstruction>)> =
        HashMap::new();

    for block in &ssa.blocks {
        for instr in &block.instructions {
            if let Some(target) = instr.target {
                def_info.insert(target, (block.id, Some(instr)));
            }
        }
        for phi in &block.phi_functions {
            def_info.insert(phi.target, (block.id, None)); // Phi functions don't have side effects
        }
    }

    // Iteratively find dead code until fixed point
    let mut dead: HashSet<SsaNameId> = HashSet::new();
    let mut changed = true;
    let max_iterations = ssa.ssa_names.len() + 10;
    let mut iterations = 0;

    while changed && iterations < max_iterations {
        changed = false;
        iterations += 1;

        for name in &ssa.ssa_names {
            // Skip if already marked dead
            if dead.contains(&name.id) {
                continue;
            }

            // Get use count
            let uses = use_count.get(&name.id).copied().unwrap_or(0);

            // If no uses, check if it has side effects
            if uses == 0 {
                let has_effects = if let Some((_, Some(instr))) = def_info.get(&name.id) {
                    has_side_effects(&instr.kind)
                } else {
                    false // Phi functions have no side effects
                };

                if !has_effects {
                    // Mark as dead
                    dead.insert(name.id);
                    changed = true;

                    // Decrement use counts of this definition's operands
                    if let Some((block_id, _)) = def_info.get(&name.id) {
                        // Find the instruction/phi that defines this
                        for block in &ssa.blocks {
                            if block.id == *block_id {
                                // Check instructions
                                for instr in &block.instructions {
                                    if instr.target == Some(name.id) {
                                        for &use_name in &instr.uses {
                                            if let Some(count) = use_count.get_mut(&use_name) {
                                                *count = count.saturating_sub(1);
                                            }
                                        }
                                    }
                                }
                                // Check phi functions
                                for phi in &block.phi_functions {
                                    if phi.target == name.id {
                                        for source in &phi.sources {
                                            if let Some(count) = use_count.get_mut(&source.name) {
                                                *count = count.saturating_sub(1);
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    // Convert to sorted vector for deterministic output
    let mut result: Vec<SsaNameId> = dead.into_iter().collect();
    result.sort_by_key(|id| id.0);

    Ok(result)
}

/// Check if an instruction has side effects
pub fn has_side_effects(kind: &super::types::SsaInstructionKind) -> bool {
    matches!(
        kind,
        super::types::SsaInstructionKind::Call | super::types::SsaInstructionKind::Return
    )
}

// =============================================================================
// SSA-Based Liveness
// =============================================================================

/// SSA liveness information for O(1) queries
#[derive(Debug, Clone)]
pub struct SsaLiveness {
    /// Function name
    pub function: String,
    /// Precomputed dominance intervals
    #[allow(dead_code)]
    dom_intervals: HashMap<usize, (u32, u32)>,
    /// Use positions for each SSA name
    #[allow(dead_code)]
    use_positions: HashMap<SsaNameId, Vec<(usize, u32)>>,
}

/// Position within a block
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockPosition {
    /// At block entry (before any instructions)
    Entry,
    /// After instruction at given index
    After(usize),
    /// At block exit (after all instructions)
    Exit,
}

/// Build SSA liveness for O(1) queries
///
/// # Algorithm (Boissinot et al.)
/// - Precompute dominator tree intervals
/// - Live = defined before point AND used after point in some path
/// - O(1) query after O(n) preprocessing
///
/// Simplified version: precompute live ranges for each SSA name
pub fn build_ssa_liveness(ssa: &SsaFunction) -> TldrResult<SsaLiveness> {
    // Build block ordering for comparison
    let mut block_order: HashMap<usize, u32> = HashMap::new();
    for (i, block) in ssa.blocks.iter().enumerate() {
        block_order.insert(block.id, i as u32);
    }

    // Compute dominance intervals (approximation using block order)
    let mut dom_intervals: HashMap<usize, (u32, u32)> = HashMap::new();
    for block in &ssa.blocks {
        let order = block_order.get(&block.id).copied().unwrap_or(0);
        // Simple interval: from this block to end
        // (A proper implementation would use dominator tree DFS numbering)
        dom_intervals.insert(block.id, (order, ssa.blocks.len() as u32));
    }

    // Collect use positions for each SSA name
    // (block_id, instruction_index)
    let mut use_positions: HashMap<SsaNameId, Vec<(usize, u32)>> = HashMap::new();

    for block in &ssa.blocks {
        for (instr_idx, instr) in block.instructions.iter().enumerate() {
            for &use_name in &instr.uses {
                use_positions
                    .entry(use_name)
                    .or_default()
                    .push((block.id, instr_idx as u32));
            }
        }
        // Uses in phi functions
        for phi in &block.phi_functions {
            for source in &phi.sources {
                // Phi uses are at block entry (position 0)
                use_positions
                    .entry(source.name)
                    .or_default()
                    .push((block.id, 0));
            }
        }
    }

    Ok(SsaLiveness {
        function: ssa.function.clone(),
        dom_intervals,
        use_positions,
    })
}

/// Query if SSA name is live at a point
///
/// An SSA name is live at a point if:
/// 1. It is defined before that point
/// 2. It has at least one use after that point (on some path)
pub fn is_live_at(
    liveness: &SsaLiveness,
    name: SsaNameId,
    block: usize,
    position: BlockPosition,
) -> bool {
    // Get use positions for this name
    let Some(uses) = liveness.use_positions.get(&name) else {
        return false; // No uses means not live
    };

    // Convert position to comparable value
    let query_pos = match position {
        BlockPosition::Entry => 0,
        BlockPosition::After(idx) => (idx + 1) as u32,
        BlockPosition::Exit => u32::MAX,
    };

    // Get block order for comparison
    let query_block_order = liveness
        .dom_intervals
        .get(&block)
        .map(|(start, _)| *start)
        .unwrap_or(0);

    // Check if any use is after the query point
    for &(use_block, use_pos) in uses {
        let use_block_order = liveness
            .dom_intervals
            .get(&use_block)
            .map(|(start, _)| *start)
            .unwrap_or(0);

        // Use is after query point if:
        // 1. Use is in a later block, OR
        // 2. Use is in same block but at a later position
        if use_block_order > query_block_order {
            return true;
        }
        if use_block == block && use_pos > query_pos {
            return true;
        }
    }

    false
}

// =============================================================================
// Available Expressions
// =============================================================================

/// Available expressions at each program point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AvailableExpressions {
    /// Function name
    pub function: String,
    /// For each block, expressions available at entry and exit
    pub blocks: HashMap<usize, ExpressionSets>,
    /// All unique expressions
    pub expressions: Vec<Expression>,
}

/// Expression availability sets for a block
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpressionSets {
    /// Expression indices available at block entry
    pub in_set: HashSet<usize>,
    /// Expression indices available at block exit
    pub out_set: HashSet<usize>,
}

/// An expression tracked for availability
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Expression {
    /// Expression text or normalized form
    pub text: String,
    /// Variables used in expression
    pub uses: Vec<String>,
    /// Line where first computed
    pub first_line: u32,
}

impl AvailableExpressions {
    /// Check if expression is available at start of block
    pub fn is_available(&self, block_id: usize, expr_text: &str) -> bool {
        // Find expression index
        let expr_idx = self.expressions.iter().position(|e| e.text == expr_text);
        if let Some(idx) = expr_idx {
            self.blocks
                .get(&block_id)
                .map(|sets| sets.in_set.contains(&idx))
                .unwrap_or(false)
        } else {
            false
        }
    }

    /// Get all available expressions at block entry
    pub fn available_at(&self, block_id: usize) -> Vec<&Expression> {
        self.blocks
            .get(&block_id)
            .map(|sets| {
                sets.in_set
                    .iter()
                    .filter_map(|&idx| self.expressions.get(idx))
                    .collect()
            })
            .unwrap_or_default()
    }
}

impl LiveVariables {
    /// Get all live variables at a specific line within a block
    pub fn live_at_line(&self, cfg: &CfgInfo, line: u32) -> HashSet<String> {
        // Find containing block
        let block = cfg
            .blocks
            .iter()
            .find(|b| b.lines.0 <= line && line <= b.lines.1);

        if let Some(block) = block {
            // Conservative: use live-in for the block
            self.blocks
                .get(&block.id)
                .map(|sets| sets.live_in.clone())
                .unwrap_or_default()
        } else {
            HashSet::new()
        }
    }

    /// Get live range for a variable (blocks where it's live)
    pub fn live_range(&self, var: &str, cfg: &CfgInfo) -> Vec<(u32, u32)> {
        let mut ranges = Vec::new();

        for block in &cfg.blocks {
            if self.is_live_in(block.id, var) || self.is_live_out(block.id, var) {
                ranges.push(block.lines);
            }
        }

        ranges
    }
}

/// Compute available expressions (forward must-analysis)
///
/// # Dataflow Equations
/// ```text
/// IN[B] = intersection OUT[P] for all predecessors P
/// OUT[B] = GEN[B] union (IN[B] - KILL[B])
///
/// GEN[B] = expressions computed in B (not killed later in B)
/// KILL[B] = expressions with operands modified in B
/// ```
///
/// # Algorithm
/// 1. Extract expressions from source using tree-sitter AST (with regex fallback)
/// 2. Compute GEN and KILL sets for each block
/// 3. Iterate forward dataflow until fixed point
///
/// The `language` parameter enables AST-based parsing for all 18 supported languages.
/// If tree-sitter parsing fails for a language, falls back to regex-based extraction.
pub fn compute_available_expressions(
    cfg: &CfgInfo,
    source: &str,
    language: Language,
) -> TldrResult<AvailableExpressions> {
    // Build predecessor map
    let mut predecessors: HashMap<usize, Vec<usize>> = HashMap::new();
    for block in &cfg.blocks {
        predecessors.insert(block.id, Vec::new());
    }
    for edge in &cfg.edges {
        predecessors.entry(edge.to).or_default().push(edge.from);
    }

    // Build line-to-block mapping
    let line_to_block: HashMap<u32, usize> = cfg
        .blocks
        .iter()
        .flat_map(|block| (block.lines.0..=block.lines.1).map(move |line| (line, block.id)))
        .collect();

    // Extract expressions and definitions using AST (with regex fallback)
    let extraction = extract_expressions_and_defs(source, language, &line_to_block, cfg);

    let all_expressions = extraction.all_expressions;
    let expr_to_index = extraction.expr_to_index;
    let block_defs = extraction.block_defs;
    let block_exprs = extraction.block_exprs;
    let defs_by_line = extraction.defs_by_line;

    // If no expressions found, return empty result
    if all_expressions.is_empty() {
        let mut blocks_result = HashMap::new();
        for block in &cfg.blocks {
            blocks_result.insert(
                block.id,
                ExpressionSets {
                    in_set: HashSet::new(),
                    out_set: HashSet::new(),
                },
            );
        }
        return Ok(AvailableExpressions {
            function: cfg.function.clone(),
            blocks: blocks_result,
            expressions: all_expressions,
        });
    }

    // Compute GEN and KILL sets for each block
    let mut gen_sets: HashMap<usize, HashSet<usize>> = HashMap::new();
    let mut kill_sets: HashMap<usize, HashSet<usize>> = HashMap::new();

    for block in &cfg.blocks {
        let mut gen = HashSet::new();
        let mut kill = HashSet::new();

        // KILL: expressions whose operands are modified in this block
        let defs = block_defs.get(&block.id).cloned().unwrap_or_default();
        for (idx, expr) in all_expressions.iter().enumerate() {
            if expr.uses.iter().any(|v| defs.contains(v)) {
                kill.insert(idx);
            }
        }

        // GEN: expressions computed in this block (that aren't killed later in block)
        if let Some(exprs) = block_exprs.get(&block.id) {
            for (line_num, expr) in exprs {
                if let Some(&idx) = expr_to_index.get(&expr.text) {
                    // Check if any operand is redefined after this expression in the same block
                    let killed_after = defs_by_line.iter().any(|(&def_line, def_var)| {
                        def_line > *line_num
                            && def_line <= block.lines.1
                            && expr.uses.contains(def_var)
                    });

                    if !killed_after {
                        gen.insert(idx);
                    }
                }
            }
        }

        gen_sets.insert(block.id, gen);
        kill_sets.insert(block.id, kill);
    }

    // Initialize IN and OUT sets
    // IN[entry] = empty
    // IN[other] = U (all expressions) for must-analysis
    // OUT[B] = GEN[B] for initialization
    let all_expr_indices: HashSet<usize> = (0..all_expressions.len()).collect();

    let mut avail_in: HashMap<usize, HashSet<usize>> = HashMap::new();
    let mut avail_out: HashMap<usize, HashSet<usize>> = HashMap::new();

    for block in &cfg.blocks {
        if block.id == cfg.entry_block {
            avail_in.insert(block.id, HashSet::new());
        } else {
            avail_in.insert(block.id, all_expr_indices.clone());
        }
        avail_out.insert(
            block.id,
            gen_sets.get(&block.id).cloned().unwrap_or_default(),
        );
    }

    // Forward iteration until fixed point
    let block_ids: Vec<usize> = cfg.blocks.iter().map(|b| b.id).collect();
    let max_iterations = block_ids.len() * 2 + 10;
    let mut changed = true;
    let mut _iterations = 0;

    while changed && _iterations < max_iterations {
        changed = false;
        _iterations += 1;

        for &block_id in &block_ids {
            // IN[B] = intersection of OUT[P] for all predecessors P
            let preds = predecessors.get(&block_id).cloned().unwrap_or_default();
            let new_in = if preds.is_empty() {
                HashSet::new()
            } else {
                let mut intersection = all_expr_indices.clone();
                for pred in &preds {
                    if let Some(pred_out) = avail_out.get(pred) {
                        intersection = intersection.intersection(pred_out).copied().collect();
                    }
                }
                intersection
            };

            // OUT[B] = GEN[B] union (IN[B] - KILL[B])
            let gen = gen_sets.get(&block_id).cloned().unwrap_or_default();
            let kill = kill_sets.get(&block_id).cloned().unwrap_or_default();
            let in_minus_kill: HashSet<usize> = new_in.difference(&kill).copied().collect();
            let new_out: HashSet<usize> = gen.union(&in_minus_kill).copied().collect();

            // Check for changes
            if new_in != *avail_in.get(&block_id).unwrap_or(&HashSet::new()) {
                changed = true;
                avail_in.insert(block_id, new_in);
            }
            if new_out != *avail_out.get(&block_id).unwrap_or(&HashSet::new()) {
                changed = true;
                avail_out.insert(block_id, new_out);
            }
        }
    }

    // Build result
    let mut blocks_result = HashMap::new();
    for block in &cfg.blocks {
        blocks_result.insert(
            block.id,
            ExpressionSets {
                in_set: avail_in.get(&block.id).cloned().unwrap_or_default(),
                out_set: avail_out.get(&block.id).cloned().unwrap_or_default(),
            },
        );
    }

    Ok(AvailableExpressions {
        function: cfg.function.clone(),
        blocks: blocks_result,
        expressions: all_expressions,
    })
}

// =============================================================================
// Expression Extraction (AST-based with regex fallback)
// =============================================================================

/// Intermediate result from expression extraction
struct ExpressionExtraction {
    all_expressions: Vec<Expression>,
    expr_to_index: HashMap<String, usize>,
    block_defs: HashMap<usize, HashSet<String>>,
    block_exprs: HashMap<usize, Vec<(u32, Expression)>>,
    defs_by_line: HashMap<u32, String>,
}

/// Extract expressions and definitions from source code.
///
/// Attempts AST-based extraction using tree-sitter for the given language.
/// Falls back to regex-based extraction if AST parsing fails.
fn extract_expressions_and_defs(
    source: &str,
    language: Language,
    line_to_block: &HashMap<u32, usize>,
    cfg: &CfgInfo,
) -> ExpressionExtraction {
    // Try AST-based extraction first
    if let Some(result) = extract_expressions_ast(source, language, line_to_block, cfg) {
        return result;
    }

    // Fallback to regex
    extract_expressions_regex(source, line_to_block, cfg)
}

/// AST-based expression extraction using tree-sitter.
///
/// Uses a two-pass approach:
/// 1. Walk all nodes to find assignments and record definitions (LHS variables)
/// 2. Walk all nodes to find binary expressions inside assignments and record them
///
/// This approach is more robust than trying to match assignment types and extract
/// RHS per language -- instead, we find binary expressions directly and walk UP
/// to find their parent assignment.
fn extract_expressions_ast(
    source: &str,
    language: Language,
    line_to_block: &HashMap<u32, usize>,
    cfg: &CfgInfo,
) -> Option<ExpressionExtraction> {
    use crate::ast::parser::ParserPool;
    use crate::security::ast_utils::{
        binary_expression_node_kinds, walk_descendants,
    };

    let pool = ParserPool::new();
    let tree = pool.parse(source, language).ok()?;
    let src_bytes = source.as_bytes();

    let binop_kinds = binary_expression_node_kinds(language);

    let mut all_expressions: Vec<Expression> = Vec::new();
    let mut expr_to_index: HashMap<String, usize> = HashMap::new();
    let mut defs_by_line: HashMap<u32, String> = HashMap::new();

    let mut block_defs: HashMap<usize, HashSet<String>> = HashMap::new();
    for block in &cfg.blocks {
        block_defs.insert(block.id, HashSet::new());
    }

    let mut block_exprs: HashMap<usize, Vec<(u32, Expression)>> = HashMap::new();
    for block in &cfg.blocks {
        block_exprs.insert(block.id, Vec::new());
    }

    let descendants = walk_descendants(tree.root_node());

    // Pass 1: Find all assignments and record definitions.
    // Use a broad set of assignment-like node kinds per language.
    for node in &descendants {
        if let Some(var_name) = extract_def_from_node(node, src_bytes, language) {
            let line_num = node.start_position().row as u32 + 1;
            if let Some(&block_id) = line_to_block.get(&line_num) {
                block_defs.entry(block_id).or_default().insert(var_name.clone());
                defs_by_line.insert(line_num, var_name);
            }
        }
    }

    // Pass 2: Find all binary expressions that are arithmetic operators
    // (not assignments like `=` or comparisons like `==`).
    for node in &descendants {
        if !binop_kinds.contains(&node.kind()) {
            continue;
        }

        if let Some((left, op, right)) = extract_binop_operands(node, src_bytes, language) {
            // Only track arithmetic operators for CSE
            if !is_arithmetic_op(&op) {
                continue;
            }

            // Check that operands are simple identifiers (not complex expressions)
            if !is_simple_identifier(&left) || !is_simple_identifier(&right) {
                continue;
            }

            let line_num = node.start_position().row as u32 + 1;

            // Canonicalize: sort operands for commutative ops
            let (canonical_left, canonical_right) = if op == "+" || op == "*" {
                if left < right {
                    (left, right)
                } else {
                    (right, left)
                }
            } else {
                (left, right)
            };

            let expr_text = format!("{} {} {}", canonical_left, op, canonical_right);

            let expr_idx = if let Some(&idx) = expr_to_index.get(&expr_text) {
                idx
            } else {
                let idx = all_expressions.len();
                let expr = Expression {
                    text: expr_text.clone(),
                    uses: vec![canonical_left.clone(), canonical_right.clone()],
                    first_line: line_num,
                };
                all_expressions.push(expr.clone());
                expr_to_index.insert(expr_text, idx);
                idx
            };

            if let Some(&block_id) = line_to_block.get(&line_num) {
                let expr = all_expressions[expr_idx].clone();
                block_exprs.entry(block_id).or_default().push((line_num, expr));
            }
        }
    }

    Some(ExpressionExtraction {
        all_expressions,
        expr_to_index,
        block_defs,
        block_exprs,
        defs_by_line,
    })
}

/// Check if an operator is an arithmetic operator (for CSE tracking).
fn is_arithmetic_op(op: &str) -> bool {
    matches!(op, "+" | "-" | "*" | "/" | "%" | "**" | "//" | "<<" | ">>" | "&" | "|" | "^")
}

/// Check if a string is a simple identifier (variable name).
///
/// Rejects complex expressions like function calls, member access, etc.
fn is_simple_identifier(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }
    let s = s.trim_start_matches('$'); // PHP variables
    if s.is_empty() {
        return false;
    }
    let first = s.chars().next().unwrap();
    if !first.is_alphabetic() && first != '_' {
        return false;
    }
    s.chars().all(|c| c.is_alphanumeric() || c == '_')
}

/// Extract a definition (variable name) from any node that defines a variable.
///
/// This is broader than just looking at assignment_node_kinds -- it handles
/// language-specific node structures that define variables.
fn extract_def_from_node(
    node: &tree_sitter::Node,
    source: &[u8],
    language: Language,
) -> Option<String> {
    use crate::security::ast_utils::{assignment_node_kinds, node_text};

    let assign_kinds = assignment_node_kinds(language);
    let kind = node.kind();

    // Check standard assignment node kinds first
    if assign_kinds.contains(&kind) {
        return extract_lhs_from_assignment(node, source, language);
    }

    // Additional assignment-like node kinds not covered by the standard list
    match language {
        Language::TypeScript | Language::JavaScript => {
            // lexical_declaration (const/let) wraps variable_declarator
            if kind == "lexical_declaration" {
                for i in 0..node.child_count() {
                    if let Some(child) = node.child(i) {
                        if child.kind() == "variable_declarator" {
                            if let Some(name) = child.child_by_field_name("name") {
                                return Some(node_text(&name, source).to_string());
                            }
                        }
                    }
                }
            }
        }
        Language::Elixir => {
            // In Elixir, `x = expr` is a binary_operator with `=` operator
            if kind == "binary_operator" {
                if let Some(op) = node.child_by_field_name("operator") {
                    if node_text(&op, source) == "=" {
                        if let Some(left) = node.child_by_field_name("left") {
                            let text = node_text(&left, source).to_string();
                            if is_simple_identifier(&text) {
                                return Some(text);
                            }
                        }
                    }
                }
            }
        }
        _ => {}
    }

    None
}

/// Extract the LHS variable name from a known assignment node.
fn extract_lhs_from_assignment(
    node: &tree_sitter::Node,
    source: &[u8],
    language: Language,
) -> Option<String> {
    use crate::security::ast_utils::node_text;

    // Try common field names first
    if let Some(left) = node.child_by_field_name("left") {
        let text = node_text(&left, source);
        // Handle comma-separated (Go: a, b := ...)
        return text.split(',').next().map(|s| s.trim().to_string());
    }

    // Try pattern field (Rust let, Scala val, OCaml let)
    if let Some(pattern) = node.child_by_field_name("pattern") {
        return Some(node_text(&pattern, source).to_string());
    }

    extract_lhs_language_fallback(node, source, language)
}

fn extract_lhs_language_fallback(
    node: &tree_sitter::Node,
    source: &[u8],
    language: Language,
) -> Option<String> {
    match language {
        Language::TypeScript | Language::JavaScript => extract_name_from_variable_declarator(node, source),
        Language::Java => {
            if node.kind() == "local_variable_declaration" {
                extract_name_from_variable_declarator(node, source)
            } else {
                None
            }
        }
        Language::Kotlin => extract_lhs_kotlin(node, source),
        Language::C | Language::Cpp => extract_lhs_c_family(node, source),
        Language::Rust => extract_lhs_rust(node, source),
        Language::CSharp => extract_lhs_csharp(node, source),
        Language::Swift => extract_lhs_swift(node, source),
        Language::Lua | Language::Luau => extract_lhs_lua(node, source, language),
        Language::Scala => None,
        Language::Elixir | Language::Ocaml => extract_first_child_text(node, source),
        _ => None,
    }
}

fn extract_name_from_variable_declarator(
    node: &tree_sitter::Node,
    source: &[u8],
) -> Option<String> {
    use crate::security::ast_utils::node_text;

    for i in 0..node.child_count() {
        let child = node.child(i)?;
        if child.kind() != "variable_declarator" {
            continue;
        }
        if let Some(name) = child.child_by_field_name("name") {
            return Some(node_text(&name, source).to_string());
        }
    }
    None
}

fn extract_lhs_kotlin(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    use crate::security::ast_utils::node_text;

    for i in 0..node.child_count() {
        let child = node.child(i)?;
        if child.kind() == "variable_declaration" || child.kind() == "simple_identifier" {
            return Some(node_text(&child, source).to_string());
        }
    }
    None
}

fn extract_lhs_c_family(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    use crate::security::ast_utils::node_text;

    if node.kind() != "declaration" {
        return None;
    }
    for i in 0..node.child_count() {
        let child = node.child(i)?;
        if child.kind() != "init_declarator" {
            continue;
        }
        if let Some(decl) = child.child_by_field_name("declarator") {
            let text = node_text(&decl, source);
            return Some(text.trim_start_matches('*').to_string());
        }
    }
    None
}

fn extract_lhs_rust(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    use crate::security::ast_utils::node_text;

    if node.kind() != "let_declaration" {
        return None;
    }
    let pattern = node.child_by_field_name("pattern")?;
    Some(node_text(&pattern, source).to_string())
}

fn extract_lhs_csharp(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    use crate::security::ast_utils::node_text;

    if node.kind() != "variable_declaration" {
        return None;
    }
    for i in 0..node.child_count() {
        let child = node.child(i)?;
        if child.kind() != "variable_declarator" {
            continue;
        }
        if let Some(name) = child.child_by_field_name("name") {
            return Some(node_text(&name, source).to_string());
        }
        for j in 0..child.child_count() {
            let grandchild = child.child(j)?;
            if grandchild.kind() == "identifier" {
                return Some(node_text(&grandchild, source).to_string());
            }
        }
    }
    None
}

fn extract_lhs_swift(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    use crate::security::ast_utils::node_text;

    if node.kind() != "property_declaration" {
        return None;
    }
    for i in 0..node.child_count() {
        let child = node.child(i)?;
        if child.kind() == "pattern" {
            return Some(node_text(&child, source).to_string());
        }
    }
    None
}

fn extract_lhs_lua(
    node: &tree_sitter::Node,
    source: &[u8],
    language: Language,
) -> Option<String> {
    use crate::security::ast_utils::node_text;

    if node.kind() == "variable_declaration" {
        for i in 0..node.child_count() {
            let child = node.child(i)?;
            if child.kind() == "assignment_statement" {
                return extract_lhs_from_assignment(&child, source, language);
            }
        }
    }

    for i in 0..node.child_count() {
        let child = node.child(i)?;
        if child.kind() == "variable_list" || child.kind() == "assignment_variable_list" {
            if let Some(first) = child.child(0) {
                return Some(node_text(&first, source).to_string());
            }
        }
    }

    let first = node.child(0)?;
    if first.is_named() && first.kind() != "local" {
        return Some(node_text(&first, source).to_string());
    }
    None
}

fn extract_first_child_text(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    use crate::security::ast_utils::node_text;

    Some(node_text(&node.child(0)?, source).to_string())
}

/// Extract operands and operator from a binary expression node.
///
/// Returns (left_operand, operator, right_operand) as strings.
/// Extract operands and operator from a binary expression node.
///
/// Returns (left_operand, operator, right_operand) as strings.
/// Uses field-name access first, then falls back to positional access.
fn extract_binop_operands(
    node: &tree_sitter::Node,
    source: &[u8],
    _language: Language,
) -> Option<(String, String, String)> {
    use crate::security::ast_utils::node_text;

    // Strategy 1: Try field-name access (works for most languages)
    if let (Some(left), Some(right), Some(op)) = (
        node.child_by_field_name("left"),
        node.child_by_field_name("right"),
        node.child_by_field_name("operator"),
    ) {
        return Some((
            node_text(&left, source).trim().to_string(),
            node_text(&op, source).trim().to_string(),
            node_text(&right, source).trim().to_string(),
        ));
    }

    // Strategy 2: Positional access -- binary nodes are typically [left, op, right]
    // This handles Kotlin (additive_expression, binary_expression), Scala (infix_expression),
    // OCaml (infix_expression), and languages where operator has no field name.
    if node.child_count() >= 3 {
        let left = node.child(0)?;
        let op = node.child(1)?;
        let right = node.child(2)?;

        // Validate: left and right should be named nodes, op should be an operator
        let op_text = node_text(&op, source).trim().to_string();
        if !op_text.is_empty() {
            return Some((
                node_text(&left, source).trim().to_string(),
                op_text,
                node_text(&right, source).trim().to_string(),
            ));
        }
    }

    // Strategy 3: Try left/right fields only, extract operator from text
    if let (Some(left), Some(right)) = (
        node.child_by_field_name("left"),
        node.child_by_field_name("right"),
    ) {
        // The operator is between left and right in the source text
        // Try to find an unnamed child that's an operator
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if !child.is_named() {
                    let text = node_text(&child, source).trim().to_string();
                    if is_arithmetic_op(&text) {
                        return Some((
                            node_text(&left, source).trim().to_string(),
                            text,
                            node_text(&right, source).trim().to_string(),
                        ));
                    }
                }
            }
        }
    }

    None
}

/// Regex-based expression extraction (fallback when AST parsing fails).
///
/// Uses simple regex patterns to find assignments with binary expressions.
/// Only works reliably for Python-like syntax.
fn extract_expressions_regex(
    source: &str,
    line_to_block: &HashMap<u32, usize>,
    cfg: &CfgInfo,
) -> ExpressionExtraction {
    use regex::Regex;

    let assign_re = Regex::new(r"^\s*(\w+)\s*=\s*(.+)$").unwrap();
    let binop_re = Regex::new(r"(\w+)\s*([+\-*/])\s*(\w+)").unwrap();

    let lines: Vec<&str> = source.lines().collect();
    let mut all_expressions: Vec<Expression> = Vec::new();
    let mut expr_to_index: HashMap<String, usize> = HashMap::new();
    let mut defs_by_line: HashMap<u32, String> = HashMap::new();

    let mut block_defs: HashMap<usize, HashSet<String>> = HashMap::new();
    for block in &cfg.blocks {
        block_defs.insert(block.id, HashSet::new());
    }

    let mut block_exprs: HashMap<usize, Vec<(u32, Expression)>> = HashMap::new();
    for block in &cfg.blocks {
        block_exprs.insert(block.id, Vec::new());
    }

    for (line_idx, line) in lines.iter().enumerate() {
        let line_num = line_idx as u32 + 1;

        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }

        if let Some(caps) = assign_re.captures(trimmed) {
            let var_name = caps.get(1).unwrap().as_str().to_string();
            let rhs = caps.get(2).unwrap().as_str();

            if let Some(&block_id) = line_to_block.get(&line_num) {
                block_defs.entry(block_id).or_default().insert(var_name.clone());
                defs_by_line.insert(line_num, var_name);
            }

            if let Some(binop_caps) = binop_re.captures(rhs) {
                let left = binop_caps.get(1).unwrap().as_str().to_string();
                let op = binop_caps.get(2).unwrap().as_str().to_string();
                let right = binop_caps.get(3).unwrap().as_str().to_string();

                let (canonical_left, canonical_right) = if op == "+" || op == "*" {
                    if left < right {
                        (left, right)
                    } else {
                        (right, left)
                    }
                } else {
                    (left, right)
                };

                let expr_text = format!("{} {} {}", canonical_left, op, canonical_right);

                let expr_idx = if let Some(&idx) = expr_to_index.get(&expr_text) {
                    idx
                } else {
                    let idx = all_expressions.len();
                    let expr = Expression {
                        text: expr_text.clone(),
                        uses: vec![canonical_left.clone(), canonical_right.clone()],
                        first_line: line_num,
                    };
                    all_expressions.push(expr.clone());
                    expr_to_index.insert(expr_text, idx);
                    idx
                };

                if let Some(&block_id) = line_to_block.get(&line_num) {
                    let expr = all_expressions[expr_idx].clone();
                    block_exprs.entry(block_id).or_default().push((line_num, expr));
                }
            }
        }
    }

    ExpressionExtraction {
        all_expressions,
        expr_to_index,
        block_defs,
        block_exprs,
        defs_by_line,
    }
}

/// Compute available expressions from VarRefs (forward must-analysis)
///
/// Alternative implementation that takes pre-extracted VarRefs instead of source code.
/// Useful when VarRefs have already been extracted from AST.
pub fn compute_available_expressions_from_refs(
    cfg: &CfgInfo,
    refs: &[crate::types::VarRef],
) -> TldrResult<AvailableExpressions> {
    use crate::types::RefType;

    // Build predecessor map
    let mut predecessors: HashMap<usize, Vec<usize>> = HashMap::new();
    for block in &cfg.blocks {
        predecessors.insert(block.id, Vec::new());
    }
    for edge in &cfg.edges {
        predecessors.entry(edge.to).or_default().push(edge.from);
    }

    // Build line-to-block mapping
    let line_to_block: HashMap<u32, usize> = cfg
        .blocks
        .iter()
        .flat_map(|block| (block.lines.0..=block.lines.1).map(move |line| (line, block.id)))
        .collect();

    // Extract all expressions and variables defined in each block
    // For now, we track variables with uses (since we don't have full expression parsing)
    // An "expression" is represented by its operands
    let mut all_expressions: Vec<Expression> = Vec::new();
    let mut expr_to_index: HashMap<String, usize> = HashMap::new();

    // Track definitions in each block
    let mut block_defs: HashMap<usize, HashSet<String>> = HashMap::new();
    for block in &cfg.blocks {
        block_defs.insert(block.id, HashSet::new());
    }

    for var_ref in refs {
        if let Some(&block_id) = line_to_block.get(&var_ref.line) {
            if matches!(var_ref.ref_type, RefType::Definition | RefType::Update) {
                block_defs.entry(block_id).or_default().insert(var_ref.name.clone());
            }
        }
    }

    // Create expressions from uses that follow definitions
    // This is a simplified model where we track "x op y" style expressions
    // In a full implementation, we'd parse the source code for actual expressions
    let mut block_uses: HashMap<usize, Vec<&crate::types::VarRef>> = HashMap::new();
    for var_ref in refs {
        if matches!(var_ref.ref_type, RefType::Use) {
            if let Some(&block_id) = line_to_block.get(&var_ref.line) {
                block_uses.entry(block_id).or_default().push(var_ref);
            }
        }
    }

    // For a simplified model: create an expression for each pair of uses on the same line
    // This represents binary operations like "a + b"
    for (&_block_id, uses) in &block_uses {
        // Group uses by line
        let mut uses_by_line: HashMap<u32, Vec<&crate::types::VarRef>> = HashMap::new();
        for &var_ref in uses {
            uses_by_line.entry(var_ref.line).or_default().push(var_ref);
        }

        for (line, line_uses) in uses_by_line {
            if line_uses.len() >= 2 {
                // Create an expression from the first two uses
                let mut operands: Vec<String> = line_uses.iter().map(|r| r.name.clone()).collect();
                operands.sort(); // Canonicalize for commutative operations
                let expr_text = operands.join(" op ");

                if !expr_to_index.contains_key(&expr_text) {
                    let idx = all_expressions.len();
                    expr_to_index.insert(expr_text.clone(), idx);
                    all_expressions.push(Expression {
                        text: expr_text,
                        uses: operands,
                        first_line: line,
                    });
                }
            }
        }

        // Also track single-variable expressions for simple assignments
        for &var_ref in uses {
            let expr_text = format!("use_{}", var_ref.name);
            if !expr_to_index.contains_key(&expr_text) {
                let idx = all_expressions.len();
                expr_to_index.insert(expr_text.clone(), idx);
                all_expressions.push(Expression {
                    text: expr_text,
                    uses: vec![var_ref.name.clone()],
                    first_line: var_ref.line,
                });
            }
        }
    }

    // If no expressions found, return empty result
    if all_expressions.is_empty() {
        let mut blocks_result = HashMap::new();
        for block in &cfg.blocks {
            blocks_result.insert(
                block.id,
                ExpressionSets {
                    in_set: HashSet::new(),
                    out_set: HashSet::new(),
                },
            );
        }
        return Ok(AvailableExpressions {
            function: cfg.function.clone(),
            blocks: blocks_result,
            expressions: all_expressions,
        });
    }

    // Compute GEN and KILL sets for each block
    let mut gen_sets: HashMap<usize, HashSet<usize>> = HashMap::new();
    let mut kill_sets: HashMap<usize, HashSet<usize>> = HashMap::new();

    for block in &cfg.blocks {
        let mut gen = HashSet::new();
        let mut kill = HashSet::new();

        // KILL: expressions whose operands are modified in this block
        let defs = block_defs.get(&block.id).cloned().unwrap_or_default();
        for (idx, expr) in all_expressions.iter().enumerate() {
            if expr.uses.iter().any(|v| defs.contains(v)) {
                kill.insert(idx);
            }
        }

        // GEN: expressions computed in this block (that aren't killed later in block)
        // Find expressions whose first computation is in this block
        for (idx, expr) in all_expressions.iter().enumerate() {
            if expr.first_line >= block.lines.0 && expr.first_line <= block.lines.1 {
                // Check if any operand is redefined after this expression in the same block
                let mut killed_after = false;
                for var_ref in refs {
                    if var_ref.line > expr.first_line
                        && var_ref.line <= block.lines.1
                        && matches!(var_ref.ref_type, RefType::Definition | RefType::Update)
                        && expr.uses.contains(&var_ref.name)
                    {
                        killed_after = true;
                        break;
                    }
                }
                if !killed_after {
                    gen.insert(idx);
                }
            }
        }

        gen_sets.insert(block.id, gen);
        kill_sets.insert(block.id, kill);
    }

    // Initialize IN and OUT sets
    // IN[entry] = empty
    // IN[other] = U (all expressions) for must-analysis
    // OUT[B] = GEN[B] for initialization
    let all_expr_indices: HashSet<usize> = (0..all_expressions.len()).collect();

    let mut avail_in: HashMap<usize, HashSet<usize>> = HashMap::new();
    let mut avail_out: HashMap<usize, HashSet<usize>> = HashMap::new();

    for block in &cfg.blocks {
        if block.id == cfg.entry_block {
            avail_in.insert(block.id, HashSet::new());
        } else {
            avail_in.insert(block.id, all_expr_indices.clone());
        }
        avail_out.insert(
            block.id,
            gen_sets.get(&block.id).cloned().unwrap_or_default(),
        );
    }

    // Forward iteration until fixed point
    let block_ids: Vec<usize> = cfg.blocks.iter().map(|b| b.id).collect();
    let max_iterations = block_ids.len() * 2 + 10;
    let mut changed = true;
    let mut iterations = 0;

    while changed && iterations < max_iterations {
        changed = false;
        iterations += 1;

        for &block_id in &block_ids {
            // IN[B] = intersection of OUT[P] for all predecessors P
            let preds = predecessors.get(&block_id).cloned().unwrap_or_default();
            let new_in = if preds.is_empty() {
                HashSet::new()
            } else {
                let mut intersection = all_expr_indices.clone();
                for pred in &preds {
                    if let Some(pred_out) = avail_out.get(pred) {
                        intersection = intersection.intersection(pred_out).copied().collect();
                    }
                }
                intersection
            };

            // OUT[B] = GEN[B] union (IN[B] - KILL[B])
            let gen = gen_sets.get(&block_id).cloned().unwrap_or_default();
            let kill = kill_sets.get(&block_id).cloned().unwrap_or_default();
            let in_minus_kill: HashSet<usize> = new_in.difference(&kill).copied().collect();
            let new_out: HashSet<usize> = gen.union(&in_minus_kill).copied().collect();

            // Check for changes
            if new_in != *avail_in.get(&block_id).unwrap_or(&HashSet::new()) {
                changed = true;
                avail_in.insert(block_id, new_in);
            }
            if new_out != *avail_out.get(&block_id).unwrap_or(&HashSet::new()) {
                changed = true;
                avail_out.insert(block_id, new_out);
            }
        }
    }

    // Build result
    let mut blocks_result = HashMap::new();
    for block in &cfg.blocks {
        blocks_result.insert(
            block.id,
            ExpressionSets {
                in_set: avail_in.get(&block.id).cloned().unwrap_or_default(),
                out_set: avail_out.get(&block.id).cloned().unwrap_or_default(),
            },
        );
    }

    Ok(AvailableExpressions {
        function: cfg.function.clone(),
        blocks: blocks_result,
        expressions: all_expressions,
    })
}