stator_jse 0.3.5

Stator JavaScript engine core — parser, bytecode compiler, Maglev JIT, interpreter, GC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
//! Maglev IR optimisation passes.
//!
//! Nine passes are implemented and composed by [`optimize`]:
//!
//! 1. **Constant folding** — replaces arithmetic/comparison nodes whose *all*
//!    inputs resolve to compile-time constant nodes with a new constant node,
//!    eliminating run-time computation.  Covered operations:
//!    - `Int32` binary arithmetic: `Add`, `Subtract`, `Multiply`, `Divide`,
//!      `Modulus` and the corresponding `CheckedSmi*` variants.
//!    - `Float64` binary arithmetic: `Add`, `Subtract`, `Multiply`, `Divide`,
//!      `Modulus`.
//!    - `Int32` / `Float64` unary: `Negate`, `CheckedSmiIncrement`,
//!      `CheckedSmiDecrement`, `Int32Increment`, `Int32Decrement`.
//!
//! 2. **Strength reduction** — replaces `Int32Multiply` nodes where one
//!    operand is a compile-time power-of-two constant (≥ 2) with an
//!    equivalent `Int32ShiftLeft` by the corresponding shift amount.  This
//!    turns an expensive IMUL into a cheap SHL for common loop-index
//!    scaling patterns like `i * 2`, `i * 4`, `i * 8`.
//!
//! 3. **Range analysis** — tracks integer `[min, max]` intervals through
//!    the graph and replaces `CheckedSmi*` nodes whose output provably fits
//!    `i32` with unchecked `Int32*` equivalents (eliminating deopt overhead).
//!    See [`crate::compiler::maglev::range_analysis`].
//!
//! 4. **Trivial Phi elimination** — replaces Phi nodes whose non-self
//!    inputs are all the same value with that value.  This is essential for
//!    enabling LICM: loop-header Phis like `Phi(X, self)` for variables
//!    that are never modified in the loop body are replaced by `X`, moving
//!    their definition out of the loop and unblocking hoisting.
//!
//! 5. **Loop-invariant code motion (LICM)** — detects natural loops via
//!    back-edges and hoists pure nodes whose inputs are all defined outside
//!    the loop into the preheader block.
//!    See [`crate::compiler::maglev::licm`].
//!
//! 6. **Redundant type-guard elimination** — removes `CheckSmi` guards on
//!    values already known to be Smis.  A value is *known Smi* when it is
//!    produced by `SmiConstant` or any `CheckedSmi*` node, or when a prior
//!    `CheckSmi` for the same receiver already appears in the same block.
//!
//! 7. **Redundant `CheckMaps` removal** — within each basic block, a
//!    `CheckMaps { receiver, feedback_slot }` node is redundant if an
//!    identical guard for the *same* (receiver, feedback_slot) pair has
//!    already been emitted earlier in the same block.  The duplicate is
//!    replaced by a [`ValueNode::UndefinedConstant`] placeholder and the
//!    relevant ID is remapped so all consumers still compile correctly.
//!
//! 8. **Inlining analysis** — scans for `CallKnownFunction` nodes with
//!    small argument counts and marks them as inlining candidates for a
//!    future inlining pass.
//!
//! 9. **Dead-code elimination** — removes `ValueNode`s whose [`NodeId`]
//!    is never referenced by any other node (value or control) in the graph.
//!    Pure side-effect-free nodes that produce a value which nobody consumes
//!    are safe to drop.  Nodes with observable side-effects (stores, calls,
//!    allocations, guards/checks) are always considered *live* and are kept
//!    even when their result value is unused.
//!
//! # Usage
//!
//! ```
//! use stator_jse::compiler::maglev::ir::{
//!     BasicBlock, ControlNode, MaglevGraph, ValueNode,
//! };
//! use stator_jse::compiler::maglev::optimizer::optimize;
//!
//! let mut graph = MaglevGraph::new(0);
//! let mut block = BasicBlock::new(0);
//! // 3 + 4  — both operands are compile-time constants.
//! let c3 = block.push_value(ValueNode::Int32Constant { value: 3 });
//! let c4 = block.push_value(ValueNode::Int32Constant { value: 4 });
//! let add = block.push_value(ValueNode::Int32Add { left: c3, right: c4 });
//! block.set_control(ControlNode::Return { value: add });
//! graph.add_block(block);
//!
//! let before = graph.blocks()[0].nodes.len();
//! optimize(&mut graph);
//! // The Int32Add node has been folded; node count is reduced.
//! assert!(graph.blocks()[0].nodes.len() < before);
//! ```

use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicU32, Ordering};

use crate::compiler::maglev::ir::{BasicBlock, ControlNode, MaglevGraph, NodeId, ValueNode};
use crate::compiler::maglev::licm;

// ── Global promotion diagnostics ─────────────────────────────────────────────

/// Total globals promoted by the optimizer (LoadGlobal/StoreGlobal → Phi).
static OPT_GLOBALS_PROMOTED: AtomicU32 = AtomicU32::new(0);
/// Loops where promotion was skipped because of user calls.
static OPT_GLOBALS_SKIPPED: AtomicU32 = AtomicU32::new(0);

/// Return (promoted, skipped) diagnostic counters for global promotion.
pub fn globals_promoted_diagnostics() -> (u32, u32) {
    (
        OPT_GLOBALS_PROMOTED.load(Ordering::Relaxed),
        OPT_GLOBALS_SKIPPED.load(Ordering::Relaxed),
    )
}

// ─────────────────────────────────────────────────────────────────────────────
// Public entry-point
// ─────────────────────────────────────────────────────────────────────────────

/// Run all optimisation passes on `graph` in place.
///
/// Passes are applied in the order: constant folding → strength reduction →
/// range analysis → strength reduction → trivial Phi elimination → LICM →
/// redundant type-guard elimination → inlining analysis →
/// redundant-CheckMaps removal → DCE.  Strength reduction runs twice: once
/// before range analysis for existing Int32 nodes, and once after for newly
/// lowered Int32 nodes (e.g. `GenericMultiply` → `Int32Multiply`).
///
/// Multiple rounds are *not* performed; a single sweep of each pass is
/// sufficient for the patterns targeted here.
/// Run the full optimization pipeline on the Maglev graph.
pub fn optimize(graph: &mut MaglevGraph) {
    let _block_count = graph.blocks().len();
    let _node_count: usize = graph.blocks().iter().map(|b| b.nodes.len()).sum();

    fold_constants(graph);

    let _truncations = propagate_int32_truncation(graph);
    simplify_identities(graph);
    reassociate_arithmetic(graph);
    strength_reduce(graph);
    crate::compiler::maglev::range_analysis::eliminate_overflow_checks(graph);
    // Re-run strength reduction: range analysis may have lowered
    // GenericMultiply→Int32Multiply, exposing power-of-two patterns.
    strength_reduce(graph);
    // Re-run reassociation: range analysis lowered Generic→Int32 ops,
    // exposing patterns like (a + x*K1) - x*K2 → a + x*(K1-K2) that
    // the first reassociation pass could not see.
    reassociate_arithmetic(graph);
    eliminate_trivial_phis(graph);
    let _licm_hoisted = crate::compiler::maglev::licm::hoist_loop_invariants(graph);
    // TODO: implement loop peeling — execute the first iteration outside the
    // loop to establish type information (e.g. from CheckSmi guards), then
    // specialise the loop body based on proven types.  This requires
    // duplicating the loop header and body, which is non-trivial with the
    // current graph structure.
    eliminate_common_subexpressions(graph);
    let _globals_promoted = promote_loop_globals_counted(graph);
    eliminate_trivial_phis(graph);
    let _licm_hoisted2 = crate::compiler::maglev::licm::hoist_loop_invariants(graph);
    // Re-run range analysis after global promotion: promotion replaces
    // LoadGlobal/StoreGlobal with Phi nodes, exposing induction variable
    // and accumulator patterns that enable CheckedSmi→Int32 conversion.
    crate::compiler::maglev::range_analysis::eliminate_overflow_checks(graph);
    // Re-run strength reduction after second range analysis pass.
    strength_reduce(graph);
    // Re-run reassociation after second range analysis pass.
    reassociate_arithmetic(graph);
    // Fold invariant addition chains: after LICM has hoisted property loads
    // and range analysis has lowered Generic→Int32, loop bodies may contain
    // chains of Int32Add with loop-invariant operands (e.g. hoisted loads).
    // This groups them into a single pre-computed sum in the preheader,
    // reducing per-iteration additions from N to 1.
    let _inv_chains = crate::compiler::maglev::licm::fold_invariant_addition_chains(graph);
    // Re-run truncation after global promotion: promotion reduces
    // use-counts on arithmetic nodes — allowing CheckedSmi→Int32.
    let _truncations2 = propagate_int32_truncation(graph);
    eliminate_identity_operations_global(graph);
    eliminate_redundant_type_guards(graph);
    specialize_closure_calls(graph);
    fuse_call_loops(graph);
    // Full loop fusion: replace both push loops (SpeculativePushFusion) and
    // sum loops (SpeculativeSumFusion) with batch runtime calls.  When both
    // trigger on the same array, no per-element IC lookups are needed.
    fuse_all_loops(graph);
    mark_inlining_candidates(graph);
    remove_redundant_check_maps(graph);
    fuse_object_literal_stores(graph);
    forward_invariant_object_properties(graph);
    eliminate_store_to_load(graph);
    // Store-to-load forwarding resolves freshly-created arrays loaded through
    // globals (e.g. sieve) to their allocation nodes, exposing fill loops that
    // are not safe to match earlier.
    fuse_fill_true_loops(graph);
    fuse_count_truthy_loops(graph);
    // Re-run constant folding: store-to-load forwarding may replace
    // LoadNamedGeneric with SmiConstants, enabling preheader GenericAdd
    // chains (from fold_invariant_addition_chains) to constant-fold.
    fold_constants(graph);
    // Re-run invariant chain folding: now that store-to-load has revealed
    // SmiConstants for property loads, chains like `sum + obj.a + obj.b + …`
    // become `sum + SmiConst(1) + SmiConst(2) + …` — all addends are
    // loop-invariant.  This folds them into a single preheader sum.
    crate::compiler::maglev::licm::fold_invariant_addition_chains(graph);
    // Fold the new preheader additions (e.g. GenericAdd(1,2) → 3).
    fold_constants(graph);
    // Remove dead intermediate chain nodes left by fold_invariant_addition_chains.
    // Without this, use-count checks in accumulator detection see stale references
    // to Phi nodes from dead chain links, incorrectly inflating use counts and
    // preventing scalar evolution from firing (e.g. property_access_1k).
    eliminate_dead_code(graph);
    // Targeted late lowering: after store_to_load + chain folding, some
    // loops have `GenericAdd(accumulator_phi, SmiConstant(K))` where the
    // constant was only revealed by forwarding.  Convert these to Int32Add
    // when init and addend are known-safe Smi constants.
    lower_constant_accumulator_adds(graph);
    // Loop scalar evolution: replace simple counted loops with constant
    // accumulators by a closed-form computation.  E.g. a loop that does
    // `sum += 15` for 1000 iterations becomes `sum = 0 + 15*1000 = 15000`.
    // This must run AFTER lower_constant_accumulator_adds (which converts
    // GenericAdd→Int32Add) and BEFORE unrolling (which would complicate
    // the loop structure).
    eliminate_constant_accumulator_loops(graph);
    // NOTE: eliminate_trivial_phis + eliminate_dead_code + eliminate_dead_counted_loops
    // were previously here but interacted badly with loop-aware passes
    // (forward_loop_object_properties, unroll_simple_loops) that
    // pattern-match on loop structure.  Moved to AFTER all loop-aware
    // passes so the loop structure is consumed before removal.
    forward_loop_object_properties(graph);
    eliminate_dead_object_stores(graph);
    eliminate_dead_allocations(graph);
    replace_dead_arguments(graph);
    // NOTE: IV strength reduction disabled — caused 38% arithmetic regression
    // (8.2µs → 11.3µs) due to extra register pressure and dependency chains.
    // strength_reduce_induction_variables(graph);
    unroll_simple_loops(graph);
    // After unrolling, re-run reassociation to defer constant adjustments.
    // Unrolled bodies have chains like (((n + a) - K) + b) - K) — the
    // constant-deferral pattern pushes all -K subtractions to the end,
    // then the combining pattern merges them (e.g. 4×(-1) → -4).
    reassociate_arithmetic(graph);
    // Clean up identity ops (x+0, x-0) created by reassociation combining.
    eliminate_identity_operations_global(graph);
    eliminate_dead_code(graph);
    // Now that all loop-aware passes have completed, it's safe to
    // eliminate dead counted loops created by scalar evolution.  The
    // trivial-phi pass resolves accumulator Phis that became identities
    // (init == back) after scalar evolution replaced them.
    eliminate_trivial_phis(graph);
    eliminate_dead_counted_loops(graph);
    eliminate_dead_code(graph);
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 1 — Constant folding
// ─────────────────────────────────────────────────────────────────────────────

/// Replace arithmetic nodes with constant inputs by a single constant node.
///
/// The pass walks every block linearly, maintaining a map from [`NodeId`] →
/// known constant value for all nodes emitted so far.  When a binary or unary
/// arithmetic node is found whose operand(s) are all in the constant map, the
/// node is replaced in-place with the folded constant; the old `NodeId` for
/// that position is re-used so downstream references remain valid.
fn fold_constants(graph: &mut MaglevGraph) {
    // Pre-populate constant map from ALL blocks so cross-block constant
    // operands can be folded (e.g. SmiConstants defined in the entry block
    // used by chain-folded GenericAdds placed in the preheader by LICM).
    let mut consts: HashMap<NodeId, ConstVal> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            match node {
                ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } => {
                    consts.insert(*id, ConstVal::I32(*value));
                }
                ValueNode::Float64Constant { value } => {
                    consts.insert(*id, ConstVal::F64(*value));
                }
                _ => {}
            }
        }
    }
    for block in graph.blocks_mut() {
        fold_block_constants(block, &mut consts);
    }
}

/// Represents a compile-time-known scalar value.
#[derive(Clone, Copy)]
enum ConstVal {
    I32(i32),
    F64(f64),
}

/// Fold constants within a single [`BasicBlock`], using a shared constant map
/// that spans all blocks so cross-block references to literal constants (and
/// previously folded results) are visible.
fn fold_block_constants(block: &mut BasicBlock, consts: &mut HashMap<NodeId, ConstVal>) {
    for (id, node) in &mut block.nodes {
        // Seed the constant map from literal constant nodes in this block.
        match node {
            ValueNode::Int32Constant { value } => {
                consts.insert(*id, ConstVal::I32(*value));
                continue;
            }
            ValueNode::SmiConstant { value } => {
                consts.insert(*id, ConstVal::I32(*value));
                continue;
            }
            ValueNode::Float64Constant { value } => {
                consts.insert(*id, ConstVal::F64(*value));
                continue;
            }
            _ => {}
        }

        // Try to fold binary i32 operations.
        let folded: Option<ValueNode> = match node {
            // ── Binary Int32 / CheckedSmi ─────────────────────────────────
            ValueNode::Int32Add { left, right } | ValueNode::CheckedSmiAdd { left, right } => {
                fold_i32_bin(left, right, consts, |a, b| a.wrapping_add(b))
            }
            ValueNode::Int32Subtract { left, right }
            | ValueNode::CheckedSmiSubtract { left, right } => {
                fold_i32_bin(left, right, consts, |a, b| a.wrapping_sub(b))
            }
            ValueNode::Int32Multiply { left, right }
            | ValueNode::CheckedSmiMultiply { left, right } => {
                fold_i32_bin(left, right, consts, |a, b| a.wrapping_mul(b))
            }
            ValueNode::Int32Divide { left, right }
            | ValueNode::CheckedSmiDivide { left, right } => {
                if let (Some(ConstVal::I32(a)), Some(ConstVal::I32(b))) =
                    (consts.get(left), consts.get(right))
                {
                    let (a, b) = (*a, *b);
                    if b != 0 {
                        Some(ValueNode::Int32Constant { value: a / b })
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            ValueNode::Int32Modulus { left, right }
            | ValueNode::CheckedSmiModulus { left, right } => {
                if let (Some(ConstVal::I32(a)), Some(ConstVal::I32(b))) =
                    (consts.get(left), consts.get(right))
                {
                    let (a, b) = (*a, *b);
                    if b != 0 {
                        Some(ValueNode::Int32Constant { value: a % b })
                    } else {
                        None
                    }
                } else {
                    None
                }
            }

            // ── Unary Int32 / CheckedSmi ──────────────────────────────────
            ValueNode::Int32Negate { value } => {
                if let Some(ConstVal::I32(v)) = consts.get(value) {
                    Some(ValueNode::Int32Constant {
                        value: v.wrapping_neg(),
                    })
                } else {
                    None
                }
            }
            ValueNode::Int32Increment { value } | ValueNode::CheckedSmiIncrement { value } => {
                if let Some(ConstVal::I32(v)) = consts.get(value) {
                    Some(ValueNode::Int32Constant {
                        value: v.wrapping_add(1),
                    })
                } else {
                    None
                }
            }
            ValueNode::Int32Decrement { value } | ValueNode::CheckedSmiDecrement { value } => {
                if let Some(ConstVal::I32(v)) = consts.get(value) {
                    Some(ValueNode::Int32Constant {
                        value: v.wrapping_sub(1),
                    })
                } else {
                    None
                }
            }

            // ── Binary Float64 ────────────────────────────────────────────
            ValueNode::Float64Add { left, right } => {
                fold_f64_bin(left, right, consts, |a, b| a + b)
            }
            ValueNode::Float64Subtract { left, right } => {
                fold_f64_bin(left, right, consts, |a, b| a - b)
            }
            ValueNode::Float64Multiply { left, right } => {
                fold_f64_bin(left, right, consts, |a, b| a * b)
            }
            ValueNode::Float64Divide { left, right } => {
                fold_f64_bin(left, right, consts, |a, b| a / b)
            }
            ValueNode::Float64Modulus { left, right } => {
                fold_f64_bin(left, right, consts, |a, b| a % b)
            }

            // ── Unary Float64 ─────────────────────────────────────────────
            ValueNode::Float64Negate { value } => {
                if let Some(ConstVal::F64(v)) = consts.get(value) {
                    Some(ValueNode::Float64Constant { value: -*v })
                } else {
                    None
                }
            }

            // ── Int32 bitwise ────────────────────────────────────────────
            ValueNode::Int32BitwiseOr { left, right } => {
                fold_i32_bin(left, right, consts, |a, b| a | b)
            }
            ValueNode::Int32BitwiseAnd { left, right } => {
                fold_i32_bin(left, right, consts, |a, b| a & b)
            }
            ValueNode::Int32BitwiseXor { left, right } => {
                fold_i32_bin(left, right, consts, |a, b| a ^ b)
            }

            // ── Generic arithmetic (same as Int32 when both inputs are i32 constants)
            ValueNode::GenericAdd { left, right, .. } => {
                fold_smi_bin(left, right, consts, |a, b| a.wrapping_add(b))
            }
            ValueNode::GenericSubtract { left, right, .. } => {
                fold_smi_bin(left, right, consts, |a, b| a.wrapping_sub(b))
            }
            ValueNode::GenericMultiply { left, right, .. } => {
                fold_smi_bin(left, right, consts, |a, b| a.wrapping_mul(b))
            }
            ValueNode::GenericIncrement { value, .. } => {
                if let Some(ConstVal::I32(v)) = consts.get(value) {
                    Some(ValueNode::SmiConstant {
                        value: v.wrapping_add(1),
                    })
                } else {
                    None
                }
            }
            ValueNode::GenericDecrement { value, .. } => {
                if let Some(ConstVal::I32(v)) = consts.get(value) {
                    Some(ValueNode::SmiConstant {
                        value: v.wrapping_sub(1),
                    })
                } else {
                    None
                }
            }
            ValueNode::GenericNegate { value, .. } => {
                if let Some(ConstVal::I32(v)) = consts.get(value) {
                    Some(ValueNode::SmiConstant {
                        value: v.wrapping_neg(),
                    })
                } else {
                    None
                }
            }

            _ => None,
        };

        if let Some(replacement) = folded {
            // Update the constant map so later nodes can fold through this one.
            match &replacement {
                ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } => {
                    consts.insert(*id, ConstVal::I32(*value));
                }
                ValueNode::Float64Constant { value } => {
                    consts.insert(*id, ConstVal::F64(*value));
                }
                _ => {}
            }
            *node = replacement;
        }
    }
}

/// Attempt to fold a binary `i32` operation given a constant-value map.
fn fold_i32_bin(
    left: &NodeId,
    right: &NodeId,
    consts: &HashMap<NodeId, ConstVal>,
    op: impl Fn(i32, i32) -> i32,
) -> Option<ValueNode> {
    if let (Some(ConstVal::I32(a)), Some(ConstVal::I32(b))) = (consts.get(left), consts.get(right))
    {
        Some(ValueNode::Int32Constant { value: op(*a, *b) })
    } else {
        None
    }
}

/// Fold a Generic arithmetic operation on two i32-constant inputs into a
/// `SmiConstant` (preserves the Smi representation for downstream passes).
fn fold_smi_bin(
    left: &NodeId,
    right: &NodeId,
    consts: &HashMap<NodeId, ConstVal>,
    op: impl Fn(i32, i32) -> i32,
) -> Option<ValueNode> {
    if let (Some(ConstVal::I32(a)), Some(ConstVal::I32(b))) = (consts.get(left), consts.get(right))
    {
        Some(ValueNode::SmiConstant { value: op(*a, *b) })
    } else {
        None
    }
}

/// Attempt to fold a binary `f64` operation given a constant-value map.
fn fold_f64_bin(
    left: &NodeId,
    right: &NodeId,
    consts: &HashMap<NodeId, ConstVal>,
    op: impl Fn(f64, f64) -> f64,
) -> Option<ValueNode> {
    let lv = match consts.get(left) {
        Some(ConstVal::F64(v)) => *v,
        Some(ConstVal::I32(v)) => *v as f64,
        None => return None,
    };
    let rv = match consts.get(right) {
        Some(ConstVal::F64(v)) => *v,
        Some(ConstVal::I32(v)) => *v as f64,
        None => return None,
    };
    Some(ValueNode::Float64Constant { value: op(lv, rv) })
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 1.3 — Algebraic identity simplification
// ─────────────────────────────────────────────────────────────────────────────
// Pass 1.3 — Int32 truncation propagation
// ─────────────────────────────────────────────────────────────────────────────

/// When a `CheckedSmi*` node's result flows exclusively into a bitwise
/// operation (which truncates to `i32`), the overflow check is unnecessary
/// and can be replaced with the cheaper unchecked `Int32*` variant.
///
/// JavaScript bitwise operators (`|`, `&`, `^`, `<<`, `>>`, `>>>`) convert
/// their operands via `ToInt32`, which wraps to 32 bits.  A common pattern is
/// `(a + b) | 0` to force int32 semantics.  By detecting that the consumer
/// is a truncating op, we eliminate the deopt-on-overflow guard.
///
/// Only nodes whose *sole* consumer is on a truncating path are rewritten,
/// ensuring correctness when a value is also used by non-truncating ops.
fn propagate_int32_truncation(graph: &mut MaglevGraph) -> usize {
    // 1. Build use-count map: NodeId → number of consuming nodes.
    //    CheckSmi guards and StoreGlobal nodes are excluded because they are
    //    "transparent" for truncation purposes — a value consumed only by
    //    truncation ops + CheckSmi guards + StoreGlobal exit-stores is still
    //    safe to convert to unchecked Int32.  StoreGlobal at loop exits just
    //    materialises the (already-truncated) value back to global storage;
    //    it does not observe the overflow semantics.
    let mut use_counts: HashMap<NodeId, usize> = HashMap::new();
    for block in graph.blocks() {
        for (_, node) in &block.nodes {
            if matches!(
                node,
                ValueNode::CheckSmi { .. } | ValueNode::StoreGlobal { .. }
            ) {
                continue;
            }
            visit_value_node_inputs(node, &mut |id| {
                *use_counts.entry(id).or_insert(0) += 1;
            });
        }
        if let Some(ctrl) = &block.control {
            visit_control_inputs(ctrl, &mut |id| {
                *use_counts.entry(id).or_insert(0) += 1;
            });
        }
    }

    // 2. Build NodeId → ValueNode snapshot for backward walking.
    let mut node_map: HashMap<NodeId, ValueNode> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            node_map.insert(*id, node.clone());
        }
    }

    // 3. Find truncation points (bitwise/shift ops) and walk backward.
    //    Both Int32 and Generic bitwise/shift ops truncate to i32 —
    //    Generic variants still call ToInt32 on their operands, which
    //    wraps to 32 bits.  Recognizing Generic truncation points lets
    //    the pass convert `(a + b) | 0` patterns where the graph
    //    builder emitted GenericAdd + GenericBitwiseOr.
    let mut replacements: HashMap<NodeId, ValueNode> = HashMap::new();
    for block in graph.blocks() {
        for (_node_id, node) in &block.nodes {
            match node {
                ValueNode::Int32BitwiseOr { left, right }
                | ValueNode::Int32BitwiseXor { left, right }
                | ValueNode::Int32BitwiseAnd { left, right }
                | ValueNode::Int32ShiftLeft { left, right }
                | ValueNode::Int32ShiftRight { left, right }
                | ValueNode::Int32ShiftRightLogical { left, right }
                | ValueNode::GenericBitwiseOr { left, right, .. }
                | ValueNode::GenericBitwiseXor { left, right, .. }
                | ValueNode::GenericBitwiseAnd { left, right, .. }
                | ValueNode::GenericShiftLeft { left, right, .. }
                | ValueNode::GenericShiftRight { left, right, .. }
                | ValueNode::GenericShiftRightLogical { left, right, .. } => {
                    mark_truncated(*left, &use_counts, &node_map, &mut replacements);
                    mark_truncated(*right, &use_counts, &node_map, &mut replacements);
                }
                _ => {}
            }
        }
    }

    // 4. Apply replacements in-place.
    let count = replacements.len();
    if !replacements.is_empty() {
        for block in graph.blocks_mut() {
            for (id, node) in &mut block.nodes {
                if let Some(new_node) = replacements.remove(id) {
                    *node = new_node;
                }
            }
        }
    }
    count
}

/// Walk backward from a truncation point, replacing `CheckedSmi*` and
/// `Generic*` arithmetic with unchecked `Int32*`.  Only touches nodes
/// whose non-transparent use count is exactly one (the truncating path).
///
/// `Generic*` conversions are guarded: both inputs must be provably
/// integer-valued (Smi constants, Int32 ops, bitwise ops that always
/// produce i32, Phi nodes in smi loops, or nodes already converted by
/// this pass).  This prevents miscompilation when a Generic op's input
/// could be a string or object at runtime.
fn mark_truncated(
    id: NodeId,
    use_counts: &HashMap<NodeId, usize>,
    node_map: &HashMap<NodeId, ValueNode>,
    replacements: &mut HashMap<NodeId, ValueNode>,
) {
    if replacements.contains_key(&id) {
        return;
    }
    let uc = use_counts.get(&id);
    if uc != Some(&1) {
        return;
    }
    let Some(node) = node_map.get(&id) else {
        return;
    };

    // CheckedSmi* → Int32*: always safe (inputs already Smi-checked).
    // Convert immediately and recurse into inputs.
    match node {
        ValueNode::CheckedSmiAdd { left, right } => {
            replacements.insert(
                id,
                ValueNode::Int32Add {
                    left: *left,
                    right: *right,
                },
            );
            mark_truncated(*left, use_counts, node_map, replacements);
            mark_truncated(*right, use_counts, node_map, replacements);
            return;
        }
        ValueNode::CheckedSmiSubtract { left, right } => {
            replacements.insert(
                id,
                ValueNode::Int32Subtract {
                    left: *left,
                    right: *right,
                },
            );
            mark_truncated(*left, use_counts, node_map, replacements);
            mark_truncated(*right, use_counts, node_map, replacements);
            return;
        }
        ValueNode::CheckedSmiMultiply { left, right } => {
            replacements.insert(
                id,
                ValueNode::Int32Multiply {
                    left: *left,
                    right: *right,
                },
            );
            mark_truncated(*left, use_counts, node_map, replacements);
            mark_truncated(*right, use_counts, node_map, replacements);
            return;
        }
        ValueNode::CheckedSmiIncrement { value } => {
            replacements.insert(id, ValueNode::Int32Increment { value: *value });
            mark_truncated(*value, use_counts, node_map, replacements);
            return;
        }
        ValueNode::CheckedSmiDecrement { value } => {
            replacements.insert(id, ValueNode::Int32Decrement { value: *value });
            mark_truncated(*value, use_counts, node_map, replacements);
            return;
        }
        _ => {}
    }

    // Generic* → Int32*: recurse into inputs FIRST so they get converted
    // before we check is_provably_i32.  This allows chains like
    //   GenericBitwiseOr → GenericSub → GenericAdd → GenericMul → Phi
    // to be fully converted in a single backward walk.
    match node {
        ValueNode::GenericAdd { left, right, .. }
        | ValueNode::GenericSubtract { left, right, .. }
        | ValueNode::GenericMultiply { left, right, .. } => {
            mark_truncated(*left, use_counts, node_map, replacements);
            mark_truncated(*right, use_counts, node_map, replacements);
            if !is_provably_i32(*left, replacements, node_map)
                || !is_provably_i32(*right, replacements, node_map)
            {
                return;
            }
            let new_node = match node {
                ValueNode::GenericAdd { left, right, .. } => ValueNode::Int32Add {
                    left: *left,
                    right: *right,
                },
                ValueNode::GenericSubtract { left, right, .. } => ValueNode::Int32Subtract {
                    left: *left,
                    right: *right,
                },
                ValueNode::GenericMultiply { left, right, .. } => ValueNode::Int32Multiply {
                    left: *left,
                    right: *right,
                },
                _ => unreachable!(),
            };
            replacements.insert(id, new_node);
        }
        ValueNode::GenericIncrement { value, .. } => {
            mark_truncated(*value, use_counts, node_map, replacements);
            if !is_provably_i32(*value, replacements, node_map) {
                return;
            }
            replacements.insert(id, ValueNode::Int32Increment { value: *value });
        }
        ValueNode::GenericDecrement { value, .. } => {
            mark_truncated(*value, use_counts, node_map, replacements);
            if !is_provably_i32(*value, replacements, node_map) {
                return;
            }
            replacements.insert(id, ValueNode::Int32Decrement { value: *value });
        }
        _ => {}
    }
}

/// Returns `true` when `id` is known to produce an i32 value, making it
/// safe to feed into a wrapping `Int32*` operation.
fn is_provably_i32(
    id: NodeId,
    replacements: &HashMap<NodeId, ValueNode>,
    node_map: &HashMap<NodeId, ValueNode>,
) -> bool {
    // Already converted to Int32 by this pass.
    if replacements.contains_key(&id) {
        return true;
    }
    let Some(node) = node_map.get(&id) else {
        return false;
    };
    matches!(
        node,
        // Constants are always i32.
        ValueNode::SmiConstant { .. }
            | ValueNode::Int32Constant { .. }
            // Int32 operations always produce i32.
            | ValueNode::Int32Add { .. }
            | ValueNode::Int32Subtract { .. }
            | ValueNode::Int32Multiply { .. }
            | ValueNode::Int32Negate { .. }
            | ValueNode::Int32Increment { .. }
            | ValueNode::Int32Decrement { .. }
            | ValueNode::Int32BitwiseOr { .. }
            | ValueNode::Int32BitwiseAnd { .. }
            | ValueNode::Int32BitwiseXor { .. }
            | ValueNode::Int32ShiftLeft { .. }
            | ValueNode::Int32ShiftRight { .. }
            | ValueNode::Int32ShiftRightLogical { .. }
            // JS bitwise ops always produce i32 (ToInt32 per spec).
            | ValueNode::GenericBitwiseOr { .. }
            | ValueNode::GenericBitwiseAnd { .. }
            | ValueNode::GenericBitwiseXor { .. }
            | ValueNode::GenericBitwiseNot { .. }
            // Phi in smi loops: preheader is SmiConstant, back-edge is
            // i32 result (BitwiseOr or smi-guarded arithmetic).  Safe
            // because the smi-guarded load point already deopts on
            // non-integer values.
            | ValueNode::Phi { .. }
    )
}

/// Visit all [`NodeId`] inputs of a [`ValueNode`], calling `f` for each.
///
/// Uses a catch-all arm for zero-input nodes and any new variants that
/// don't have NodeId inputs, which is conservative for use-counting.
#[allow(clippy::too_many_lines)]
fn visit_value_node_inputs(node: &ValueNode, f: &mut impl FnMut(NodeId)) {
    match node {
        // ── Nodes with no inputs ─────────────────────────────────────────
        ValueNode::SmiConstant { .. }
        | ValueNode::Float64Constant { .. }
        | ValueNode::Int32Constant { .. }
        | ValueNode::Uint32Constant { .. }
        | ValueNode::BigIntConstant { .. }
        | ValueNode::TrueConstant
        | ValueNode::FalseConstant
        | ValueNode::NullConstant
        | ValueNode::UndefinedConstant
        | ValueNode::RootConstant { .. }
        | ValueNode::ExternalConstant { .. }
        | ValueNode::StringConstant { .. }
        | ValueNode::ConstantPoolEntry { .. }
        | ValueNode::Parameter { .. }
        | ValueNode::RegisterInput { .. }
        | ValueNode::ArgumentsLength
        | ValueNode::RestLength
        | ValueNode::LoadGlobal { .. }
        | ValueNode::LoadCurrentContextSlot { .. }
        | ValueNode::ArgumentsElements { .. }
        | ValueNode::RestElements { .. }
        | ValueNode::VirtualObject { .. }
        | ValueNode::CreateFunctionContext { .. }
        | ValueNode::CreateBlockContext { .. }
        | ValueNode::CreateShallowObjectLiteral { .. }
        | ValueNode::CreateShallowArrayLiteral { .. }
        | ValueNode::CreateObjectLiteral { .. }
        | ValueNode::CreateArrayLiteral { .. }
        | ValueNode::CreateEmptyObjectLiteral
        | ValueNode::CreateEmptyArrayLiteral { .. }
        | ValueNode::CreateMappedArguments
        | ValueNode::CreateUnmappedArguments
        | ValueNode::CreateRestParameter
        | ValueNode::CreateRegExpLiteral { .. }
        | ValueNode::CreateClosure { .. }
        | ValueNode::FastCreateClosure { .. }
        | ValueNode::GetTemplateObject { .. }
        | ValueNode::Debugger
        | ValueNode::Abort { .. } => {}

        // ── Single-input value nodes ─────────────────────────────────────
        ValueNode::GetArgument { index } => f(*index),
        ValueNode::CheckedSmiIncrement { value }
        | ValueNode::CheckedSmiDecrement { value }
        | ValueNode::Int32Negate { value }
        | ValueNode::Int32Increment { value }
        | ValueNode::Int32Decrement { value }
        | ValueNode::Float64Negate { value }
        | ValueNode::Float64Ieee754Unary { value, .. }
        | ValueNode::GenericBitwiseNot { value, .. }
        | ValueNode::GenericNegate { value, .. }
        | ValueNode::GenericIncrement { value, .. }
        | ValueNode::GenericDecrement { value, .. }
        | ValueNode::ChangeInt32ToFloat64 { input: value }
        | ValueNode::ChangeUint32ToFloat64 { input: value }
        | ValueNode::ChangeFloat64ToInt32 { input: value }
        | ValueNode::CheckedFloat64ToInt32 { input: value }
        | ValueNode::ChangeInt32ToTagged { input: value }
        | ValueNode::ChangeUint32ToTagged { input: value }
        | ValueNode::ChangeFloat64ToTagged { input: value }
        | ValueNode::ChangeTaggedToInt32 { input: value }
        | ValueNode::ChangeTaggedToUint32 { input: value }
        | ValueNode::ChangeTaggedToFloat64 { input: value }
        | ValueNode::CheckedTaggedToInt32 { input: value }
        | ValueNode::CheckedTaggedToFloat64 { input: value }
        | ValueNode::ToBoolean { value }
        | ValueNode::TestNullOrUndefined { value }
        | ValueNode::ToString { value, .. }
        | ValueNode::ToObject { value, .. }
        | ValueNode::ToName { value, .. }
        | ValueNode::ToNumber { value, .. }
        | ValueNode::ToNumberOrNumeric { value, .. }
        | ValueNode::TypeOf { value }
        | ValueNode::NumberToString { value, .. } => f(*value),

        ValueNode::CheckSmi { receiver }
        | ValueNode::CheckNumber { receiver }
        | ValueNode::CheckHeapObject { receiver }
        | ValueNode::CheckSymbol { receiver }
        | ValueNode::CheckString { receiver }
        | ValueNode::CheckStringOrStringWrapper { receiver }
        | ValueNode::CheckSeqOneByteString { receiver }
        | ValueNode::CheckMaps { receiver, .. }
        | ValueNode::CheckMapsWithMigration { receiver, .. }
        | ValueNode::CheckValue { receiver, .. } => f(*receiver),

        ValueNode::CheckDynamicValue { receiver, expected } => {
            f(*receiver);
            f(*expected);
        }

        ValueNode::CheckInt32IsSmi { input }
        | ValueNode::CheckUint32IsSmi { input }
        | ValueNode::CheckHoleyFloat64IsSmi { input }
        | ValueNode::CheckFloat64IsNan { input } => f(*input),

        ValueNode::TestTypeOf { value, .. } | ValueNode::TestUndetectable { value } => f(*value),

        // ── Object/field loads ───────────────────────────────────────────
        ValueNode::LoadField { object, .. }
        | ValueNode::LoadTaggedField { object, .. }
        | ValueNode::LoadDoubleField { object, .. }
        | ValueNode::LoadNamedGeneric { object, .. }
        | ValueNode::ForInPrepare {
            enumerator: object, ..
        }
        | ValueNode::StringLength { string: object } => f(*object),

        ValueNode::LoadEnumCacheLength { map } => f(*map),

        ValueNode::LoadKeyedGeneric { object, key, .. } => {
            f(*object);
            f(*key);
        }

        ValueNode::HasInPrototypeChain { object, prototype } => {
            f(*object);
            f(*prototype);
        }

        ValueNode::StoreField { object, value, .. } => {
            f(*object);
            f(*value);
        }

        ValueNode::StoreCurrentContextSlot { value, .. } | ValueNode::StoreGlobal { value, .. } => {
            f(*value)
        }

        ValueNode::LoadContextSlot { context, .. } => f(*context),
        ValueNode::StoreContextSlot { context, value, .. } => {
            f(*context);
            f(*value);
        }

        ValueNode::PushContext { context } | ValueNode::PopContext { context } => f(*context),

        // ── Fixed-array loads / stores ───────────────────────────────────
        ValueNode::LoadFixedArrayElement { elements, index }
        | ValueNode::LoadFixedDoubleArrayElement { elements, index }
        | ValueNode::LoadHoleyFixedDoubleArrayElement { elements, index } => {
            f(*elements);
            f(*index);
        }

        ValueNode::StoreFixedArrayElement {
            elements,
            index,
            value,
        }
        | ValueNode::StoreFixedDoubleArrayElement {
            elements,
            index,
            value,
        } => {
            f(*elements);
            f(*index);
            f(*value);
        }

        ValueNode::StoreNamedGeneric { object, value, .. } => {
            f(*object);
            f(*value);
        }

        ValueNode::CreateObjectLiteralWithProperties { values, .. } => {
            for &v in values {
                f(v);
            }
        }

        ValueNode::StoreKeyedGeneric {
            object, key, value, ..
        } => {
            f(*object);
            f(*key);
            f(*value);
        }

        // ── Binary arithmetic / comparisons ──────────────────────────────
        ValueNode::CheckedSmiAdd { left, right }
        | ValueNode::CheckedSmiSubtract { left, right }
        | ValueNode::CheckedSmiMultiply { left, right }
        | ValueNode::CheckedSmiDivide { left, right }
        | ValueNode::CheckedSmiModulus { left, right }
        | ValueNode::Int32Add { left, right }
        | ValueNode::Int32Subtract { left, right }
        | ValueNode::Int32Multiply { left, right }
        | ValueNode::Int32Divide { left, right }
        | ValueNode::Int32Modulus { left, right }
        | ValueNode::Int32BitwiseAnd { left, right }
        | ValueNode::Int32BitwiseOr { left, right }
        | ValueNode::Int32BitwiseXor { left, right }
        | ValueNode::Int32ShiftLeft { left, right }
        | ValueNode::Int32ShiftRight { left, right }
        | ValueNode::Int32ShiftRightLogical { left, right }
        | ValueNode::Uint32Add { left, right }
        | ValueNode::Uint32Subtract { left, right }
        | ValueNode::Uint32Multiply { left, right }
        | ValueNode::Uint32Divide { left, right }
        | ValueNode::Uint32Modulus { left, right }
        | ValueNode::Float64Add { left, right }
        | ValueNode::Float64Subtract { left, right }
        | ValueNode::Float64Multiply { left, right }
        | ValueNode::Float64Divide { left, right }
        | ValueNode::Float64Modulus { left, right }
        | ValueNode::Float64Exponentiate { left, right }
        | ValueNode::Int32Equal { left, right }
        | ValueNode::Int32StrictEqual { left, right }
        | ValueNode::Int32LessThan { left, right }
        | ValueNode::Int32LessThanOrEqual { left, right }
        | ValueNode::Int32GreaterThan { left, right }
        | ValueNode::Int32GreaterThanOrEqual { left, right }
        | ValueNode::Float64Equal { left, right }
        | ValueNode::Float64LessThan { left, right }
        | ValueNode::Float64LessThanOrEqual { left, right }
        | ValueNode::Float64GreaterThan { left, right }
        | ValueNode::Float64GreaterThanOrEqual { left, right }
        | ValueNode::StringConcat { left, right }
        | ValueNode::StringEqual { left, right }
        | ValueNode::GenericAdd { left, right, .. }
        | ValueNode::GenericSubtract { left, right, .. }
        | ValueNode::GenericMultiply { left, right, .. }
        | ValueNode::GenericDivide { left, right, .. }
        | ValueNode::GenericModulus { left, right, .. }
        | ValueNode::GenericExponentiate { left, right, .. }
        | ValueNode::GenericBitwiseAnd { left, right, .. }
        | ValueNode::GenericBitwiseOr { left, right, .. }
        | ValueNode::GenericBitwiseXor { left, right, .. }
        | ValueNode::GenericShiftLeft { left, right, .. }
        | ValueNode::GenericShiftRight { left, right, .. }
        | ValueNode::GenericShiftRightLogical { left, right, .. }
        | ValueNode::TaggedEqual { left, right, .. }
        | ValueNode::TaggedNotEqual { left, right, .. } => {
            f(*left);
            f(*right);
        }

        ValueNode::CheckInt32Condition { left, right, .. } => {
            f(*left);
            f(*right);
        }

        ValueNode::CheckCacheIndicesNotCleared { receiver, indices } => {
            f(*receiver);
            f(*indices);
        }

        ValueNode::TestInstanceOf {
            object, callable, ..
        } => {
            f(*object);
            f(*callable);
        }
        ValueNode::TestIn { key, object, .. } => {
            f(*key);
            f(*object);
        }

        ValueNode::StringAt { string, index } => {
            f(*string);
            f(*index);
        }

        ValueNode::ForInNext {
            receiver,
            cache_index,
            cache_array,
            ..
        } => {
            f(*receiver);
            f(*cache_index);
            f(*cache_array);
        }

        ValueNode::DeleteProperty { object, key, .. } => {
            f(*object);
            f(*key);
        }

        ValueNode::CreateCatchContext { exception, .. } => f(*exception),
        ValueNode::CreateWithContext { object, .. } => f(*object),

        // ── Call / Construct nodes ────────────────────────────────────────
        ValueNode::Call {
            callee,
            receiver,
            args,
            ..
        }
        | ValueNode::CallWithSpread {
            callee,
            receiver,
            args,
            ..
        }
        | ValueNode::CallKnownFunction {
            callee,
            receiver,
            args,
        } => {
            f(*callee);
            f(*receiver);
            for a in args {
                f(*a);
            }
        }
        // CallArrayPush codegen ignores callee — not an input.
        ValueNode::CallArrayPush { receiver, args, .. } => {
            f(*receiver);
            for a in args {
                f(*a);
            }
        }

        ValueNode::CallBuiltin { args, .. } | ValueNode::CallRuntime { args, .. } => {
            for a in args {
                f(*a);
            }
        }

        ValueNode::SpeculativeCallFusion { callee, .. } => f(*callee),
        ValueNode::SpeculativeSumFusion { array } => f(*array),
        ValueNode::SpeculativePushFusion { array, .. } => f(*array),
        ValueNode::SpeculativeFillTrueFusion { array, count } => {
            f(*array);
            f(*count);
        }
        ValueNode::SpeculativeCountTruthyFusion { array, count } => {
            f(*array);
            f(*count);
        }

        ValueNode::Construct {
            constructor, args, ..
        }
        | ValueNode::ConstructWithSpread {
            constructor, args, ..
        } => {
            f(*constructor);
            for a in args {
                f(*a);
            }
        }

        // ── Phi ──────────────────────────────────────────────────────────
        ValueNode::Phi { inputs } => {
            for id in inputs {
                f(*id);
            }
        }
    }
}

/// Visit all [`NodeId`] inputs of a [`ControlNode`], calling `f` for each.
fn visit_control_inputs(ctrl: &ControlNode, f: &mut impl FnMut(NodeId)) {
    match ctrl {
        ControlNode::Return { value } => f(*value),
        ControlNode::Branch { condition, .. } => f(*condition),
        ControlNode::Jump { .. } | ControlNode::Deoptimize { .. } => {}
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Trivial Phi elimination
// ─────────────────────────────────────────────────────────────────────────────

/// Eliminate trivial Phi nodes whose value is statically determined.
///
/// A Phi node is *trivial* when every non-self input refers to the same
/// [`NodeId`] `X`.  Such a Phi always evaluates to `X` regardless of which
/// predecessor is taken, so all uses of the Phi can be replaced by `X`.
/// This commonly arises in loop headers for variables that are never
/// modified inside the loop body (e.g. `Phi(root, self)` for an object
/// reference that is only read).
///
/// Eliminating trivial Phis is a prerequisite for effective LICM: a
/// [`ValueNode::LoadNamedGeneric`] whose receiver is a trivial Phi in the
/// loop header appears to depend on a loop-body definition.  Once the Phi
/// is replaced, the load's input lives in the preheader and becomes
/// eligible for hoisting.
///
/// The pass iterates to a fixed point so that cascading trivial Phis
/// (where one Phi's sole external input is another trivial Phi) are fully
/// resolved.
fn eliminate_trivial_phis(graph: &mut MaglevGraph) {
    // Iterate to a fixed point for cascading trivial Phis.
    loop {
        // Step 1: Scan all blocks for trivial Phi nodes.
        let mut subst: HashMap<NodeId, NodeId> = HashMap::new();
        for block in graph.blocks() {
            for &(id, ref node) in &block.nodes {
                if let ValueNode::Phi { inputs } = node {
                    // Collect unique non-self inputs.
                    let mut unique_external = None;
                    let mut is_trivial = true;
                    for &inp in inputs {
                        if inp == id {
                            continue; // skip self-references
                        }
                        match unique_external {
                            None => unique_external = Some(inp),
                            Some(prev) if prev == inp => {} // same as before
                            Some(_) => {
                                is_trivial = false;
                                break;
                            }
                        }
                    }
                    if is_trivial && let Some(replacement) = unique_external {
                        subst.insert(id, replacement);
                    }
                    // If unique_external is None, all inputs are self-refs
                    // (unreachable Phi) — leave it for DCE.
                }
            }
        }

        if subst.is_empty() {
            break;
        }

        // Transitively resolve substitution chains: if A→B and B→C, then
        // A→C. Limit iterations to avoid infinite loops from malformed IR.
        for _ in 0..subst.len() {
            let mut changed = false;
            let keys: Vec<NodeId> = subst.keys().copied().collect();
            for key in keys {
                if let Some(&further) = subst.get(&subst[&key])
                    && subst[&key] != further
                {
                    subst.insert(key, further);
                    changed = true;
                }
            }
            if !changed {
                break;
            }
        }

        // Step 2: Replace trivial Phi nodes with dead placeholders.
        for block in graph.blocks_mut() {
            for (id, node) in &mut block.nodes {
                if subst.contains_key(id) {
                    *node = ValueNode::UndefinedConstant;
                }
            }
        }

        // Step 3: Apply substitution to all node inputs and control nodes.
        for block in graph.blocks_mut() {
            apply_subst_to_block(block, &subst);
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────

/// Simplify algebraic identities: `x + 0 → x`, `x * 1 → x`, `x - 0 → x`,
/// `x * 0 → 0`, `x | 0 → x`, `x ^ 0 → x`, `x & -1 → x`.
///
/// Replaced nodes are turned into [`ValueNode::UndefinedConstant`] placeholders
/// and a substitution table redirects all downstream references to the identity
/// operand.  DCE cleans up the dead placeholders.
fn simplify_identities(graph: &mut MaglevGraph) {
    for block in graph.blocks_mut() {
        simplify_identities_in_block(block);
    }
}

/// Perform algebraic identity simplification within a single [`BasicBlock`].
fn simplify_identities_in_block(block: &mut BasicBlock) {
    // Collect compile-time integer constants visible in this block.
    let mut consts: HashMap<NodeId, i32> = HashMap::new();
    for (id, node) in &block.nodes {
        match node {
            ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } => {
                consts.insert(*id, *value);
            }
            _ => {}
        }
    }

    if consts.is_empty() {
        return;
    }

    // Build substitution map for identity patterns.
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();

    for (id, node) in &mut block.nodes {
        let replacement: Option<NodeId> = match node {
            // x + 0 → x, 0 + x → x
            ValueNode::Int32Add { left, right } | ValueNode::CheckedSmiAdd { left, right } => {
                if consts.get(right) == Some(&0) {
                    Some(*left)
                } else if consts.get(left) == Some(&0) {
                    Some(*right)
                } else {
                    None
                }
            }
            // x - 0 → x
            ValueNode::Int32Subtract { left, right }
            | ValueNode::CheckedSmiSubtract { left, right } => {
                if consts.get(right) == Some(&0) {
                    Some(*left)
                } else {
                    None
                }
            }
            // x * 1 → x, 1 * x → x, x * 0 → 0, 0 * x → 0
            ValueNode::Int32Multiply { left, right }
            | ValueNode::CheckedSmiMultiply { left, right } => {
                if consts.get(right) == Some(&1) {
                    Some(*left)
                } else if consts.get(left) == Some(&1) || consts.get(right) == Some(&0) {
                    Some(*right)
                } else if consts.get(left) == Some(&0) {
                    Some(*left)
                } else {
                    None
                }
            }
            // x | 0 → x, 0 | x → x  (Int32 only)
            //
            // GenericBitwiseOr is intentionally NOT folded here: it serves
            // as a ToInt32 truncation point for `propagate_int32_truncation`.
            // The first truncation pass may not be able to convert the chain
            // (e.g. when LoadGlobal is not provably i32).  After
            // `promote_loop_globals_counted` replaces LoadGlobal with Phi
            // nodes, the *second* truncation pass needs the BitwiseOr marker
            // still present to walk backward and convert GenericAdd/Sub/Mul
            // to their Int32 equivalents.  `eliminate_identity_operations_global`
            // cleans up any remaining `GenericBitwiseOr(x, 0)` afterward.
            ValueNode::Int32BitwiseOr { left, right } => {
                if consts.get(right) == Some(&0) {
                    Some(*left)
                } else if consts.get(left) == Some(&0) {
                    Some(*right)
                } else {
                    None
                }
            }
            // x ^ 0 → x, 0 ^ x → x
            ValueNode::Int32BitwiseXor { left, right }
            | ValueNode::GenericBitwiseXor { left, right, .. } => {
                if consts.get(right) == Some(&0) {
                    Some(*left)
                } else if consts.get(left) == Some(&0) {
                    Some(*right)
                } else {
                    None
                }
            }
            // x & -1 → x, -1 & x → x
            ValueNode::Int32BitwiseAnd { left, right }
            | ValueNode::GenericBitwiseAnd { left, right, .. } => {
                if consts.get(right) == Some(&-1) {
                    Some(*left)
                } else if consts.get(left) == Some(&-1) {
                    Some(*right)
                } else {
                    None
                }
            }
            _ => None,
        };

        if let Some(canonical_id) = replacement {
            subst.insert(*id, canonical_id);
            *node = ValueNode::UndefinedConstant;
        }
    }

    if !subst.is_empty() {
        apply_subst_to_block(block, &subst);
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 1.25b — Cross-block identity elimination
// ─────────────────────────────────────────────────────────────────────────────

/// Eliminate identity operations whose constant operand lives in a different
/// block than the arithmetic node.
///
/// The block-local [`simplify_identities`] pass only sees constants defined
/// within the same basic block.  This global pass collects *all* constants
/// across the entire graph and then replaces identity patterns such as
/// `x | 0`, `x & -1`, `x + 0`, `x - 0`, and `x * 1` regardless of which
/// block the constant was defined in.  Remaining dead placeholders are
/// cleaned up by the later DCE pass.
fn eliminate_identity_operations_global(graph: &mut MaglevGraph) {
    // Step 1: Collect all integer constants across every block.
    let mut consts: HashMap<NodeId, i32> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            match node {
                ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } => {
                    consts.insert(*id, *value);
                }
                _ => {}
            }
        }
    }

    if consts.is_empty() {
        return;
    }

    // Step 2: Scan all blocks for identity patterns, building a global
    // substitution map and replacing matched nodes with dead placeholders.
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();
    for block in graph.blocks_mut() {
        for (id, node) in &mut block.nodes {
            let replacement: Option<NodeId> = match node {
                // x | 0 → x, 0 | x → x
                ValueNode::Int32BitwiseOr { left, right }
                | ValueNode::GenericBitwiseOr { left, right, .. } => {
                    if consts.get(right) == Some(&0) {
                        Some(*left)
                    } else if consts.get(left) == Some(&0) {
                        Some(*right)
                    } else {
                        None
                    }
                }
                // x & -1 → x, -1 & x → x
                ValueNode::Int32BitwiseAnd { left, right }
                | ValueNode::GenericBitwiseAnd { left, right, .. } => {
                    if consts.get(right) == Some(&-1) {
                        Some(*left)
                    } else if consts.get(left) == Some(&-1) {
                        Some(*right)
                    } else {
                        None
                    }
                }
                // x + 0 → x, 0 + x → x
                ValueNode::Int32Add { left, right }
                | ValueNode::CheckedSmiAdd { left, right }
                | ValueNode::GenericAdd { left, right, .. } => {
                    if consts.get(right) == Some(&0) {
                        Some(*left)
                    } else if consts.get(left) == Some(&0) {
                        Some(*right)
                    } else {
                        None
                    }
                }
                // x - 0 → x
                ValueNode::Int32Subtract { left, right }
                | ValueNode::CheckedSmiSubtract { left, right }
                | ValueNode::GenericSubtract { left, right, .. } => {
                    if consts.get(right) == Some(&0) {
                        Some(*left)
                    } else {
                        None
                    }
                }
                // x * 1 → x, 1 * x → x
                ValueNode::Int32Multiply { left, right }
                | ValueNode::CheckedSmiMultiply { left, right }
                | ValueNode::GenericMultiply { left, right, .. } => {
                    if consts.get(right) == Some(&1) {
                        Some(*left)
                    } else if consts.get(left) == Some(&1) {
                        Some(*right)
                    } else {
                        None
                    }
                }
                _ => None,
            };

            if let Some(canonical_id) = replacement {
                subst.insert(*id, canonical_id);
                *node = ValueNode::UndefinedConstant;
            }
        }
    }

    if subst.is_empty() {
        return;
    }

    // Step 3: Apply substitutions across all blocks so every reference to
    // an eliminated node is redirected to its non-constant operand.
    for block in graph.blocks_mut() {
        apply_subst_to_block(block, &subst);
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 1.35 — Arithmetic reassociation
// ─────────────────────────────────────────────────────────────────────────────

/// Combine multiply-add/sub patterns into simpler multiplies.
///
/// Recognized patterns (all for `Int32` variants):
///
/// - `(x * K) + x` → `x * (K + 1)`
/// - `x + (x * K)` → `x * (K + 1)`
/// - `(x * K) - x` → `x * (K - 1)` when K > 1
/// - `(a - x) + (x * K)` → `a + x * (K - 1)` when K > 1
/// - `(a + (x * K1)) + x` → `a + x * (K1 + 1)`
/// - `x + (a + (x * K1))` → `a + x * (K1 + 1)`
///
/// The pass runs iteratively until convergence so that chained patterns
/// (e.g. `i*3 - i + i*2` → `i*4`) are fully simplified.
fn reassociate_arithmetic(graph: &mut MaglevGraph) {
    loop {
        let changed = reassociate_arithmetic_once(graph);
        if changed == 0 {
            break;
        }
    }
}

fn reassociate_arithmetic_once(graph: &mut MaglevGraph) -> usize {
    let mut next_id = graph
        .blocks()
        .iter()
        .flat_map(|b| b.nodes.iter())
        .map(|(id, _)| id.0)
        .max()
        .map_or(0, |m| m + 1);

    // Pre-populate constant map from ALL blocks so cross-block constant
    // operands are visible (e.g. SmiConstant(1) in entry block used by
    // Int32Subtract in the loop body for constant-deferral patterns).
    let mut global_consts: HashMap<NodeId, i32> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            if let ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } = node {
                global_consts.insert(*id, *value);
            }
        }
    }

    let mut count = 0;
    for block in graph.blocks_mut() {
        count += reassociate_block(block, &mut next_id, &mut global_consts);
    }
    count
}

/// Describes a single reassociation replacement.
struct Reassoc {
    pos: usize,
    new_node: ValueNode,
    prefix: Vec<(NodeId, ValueNode)>,
}

/// Build helper maps and apply one reassociation pattern within one block.
fn reassociate_block(
    block: &mut BasicBlock,
    next_id: &mut u32,
    global_consts: &mut HashMap<NodeId, i32>,
) -> usize {
    let mut mul_info: HashMap<NodeId, (NodeId, i32)> = HashMap::new();
    let mut node_defs: HashMap<NodeId, ValueNode> = HashMap::new();

    for (id, node) in &block.nodes {
        // Register any new constants created by prior reassociation passes
        if let ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } = node {
            global_consts.insert(*id, *value);
        }
        if let ValueNode::Int32Multiply { left, right } = node {
            if let Some(&k) = global_consts.get(right) {
                mul_info.insert(*id, (*left, k));
            } else if let Some(&k) = global_consts.get(left) {
                mul_info.insert(*id, (*right, k));
            }
        }
        node_defs.insert(*id, node.clone());
    }

    for (pos, (_id, node)) in block.nodes.iter().enumerate() {
        if let Some(r) = try_reassociate(node, pos, global_consts, &mul_info, &node_defs, next_id) {
            return apply_reassoc(block, r);
        }
    }
    0
}

/// Try to match a reassociation pattern for the node at `pos`.
fn try_reassociate(
    node: &ValueNode,
    pos: usize,
    consts: &HashMap<NodeId, i32>,
    mul_info: &HashMap<NodeId, (NodeId, i32)>,
    node_defs: &HashMap<NodeId, ValueNode>,
    next_id: &mut u32,
) -> Option<Reassoc> {
    match node {
        ValueNode::Int32Add { left, right } => {
            // (x * K) + x  ->  x * (K+1)
            if let Some(&(base, k)) = mul_info.get(left)
                && base == *right
            {
                return build_multiply_reassoc(pos, base, k + 1, consts, next_id);
            }
            // x + (x * K)  ->  x * (K+1)
            if let Some(&(base, k)) = mul_info.get(right)
                && base == *left
            {
                return build_multiply_reassoc(pos, base, k + 1, consts, next_id);
            }
            // (a - x) + (x * K)  ->  a + x * (K-1)  when K > 1
            if let Some(ValueNode::Int32Subtract {
                left: sub_a,
                right: sub_x,
            }) = node_defs.get(left)
                && let Some(&(base, k)) = mul_info.get(right)
                && base == *sub_x
                && k > 1
            {
                return build_add_mul_reassoc(pos, *sub_a, base, k - 1, consts, next_id);
            }
            // (a + x*K) + x  ->  a + x*(K+1)
            if let Some(ValueNode::Int32Add {
                left: add_a,
                right: add_b,
            }) = node_defs.get(left)
            {
                if let Some(&(base, k)) = mul_info.get(add_b)
                    && base == *right
                {
                    return build_add_mul_reassoc(pos, *add_a, base, k + 1, consts, next_id);
                }
                if let Some(&(base, k)) = mul_info.get(add_a)
                    && base == *right
                {
                    return build_add_mul_reassoc(pos, *add_b, base, k + 1, consts, next_id);
                }
            }
            // x + (a + x*K)  ->  a + x*(K+1)
            if let Some(ValueNode::Int32Add {
                left: add_a,
                right: add_b,
            }) = node_defs.get(right)
            {
                if let Some(&(base, k)) = mul_info.get(add_b)
                    && base == *left
                {
                    return build_add_mul_reassoc(pos, *add_a, base, k + 1, consts, next_id);
                }
                if let Some(&(base, k)) = mul_info.get(add_a)
                    && base == *left
                {
                    return build_add_mul_reassoc(pos, *add_b, base, k + 1, consts, next_id);
                }
            }
            // (a - K) + b  ->  (a + b) - K  when K is constant
            // Defers constant adjustments past independent additions.
            // After unrolling, this pushes per-copy constant subs to
            // the end where fold_constants combines them (e.g. 4×(-1) → -4).
            if let Some(ValueNode::Int32Subtract {
                left: sub_a,
                right: sub_k,
            }) = node_defs.get(left)
                && consts.contains_key(sub_k)
            {
                let add_id = NodeId(*next_id);
                *next_id += 1;
                return Some(Reassoc {
                    pos,
                    new_node: ValueNode::Int32Subtract {
                        left: add_id,
                        right: *sub_k,
                    },
                    prefix: vec![(
                        add_id,
                        ValueNode::Int32Add {
                            left: *sub_a,
                            right: *right,
                        },
                    )],
                });
            }
            // a + (b - K)  ->  (a + b) - K  when K is constant
            if let Some(ValueNode::Int32Subtract {
                left: sub_b,
                right: sub_k,
            }) = node_defs.get(right)
                && consts.contains_key(sub_k)
            {
                let add_id = NodeId(*next_id);
                *next_id += 1;
                return Some(Reassoc {
                    pos,
                    new_node: ValueNode::Int32Subtract {
                        left: add_id,
                        right: *sub_k,
                    },
                    prefix: vec![(
                        add_id,
                        ValueNode::Int32Add {
                            left: *left,
                            right: *sub_b,
                        },
                    )],
                });
            }
            None
        }
        ValueNode::Int32Subtract { left, right } => {
            // (x * K) - x  ->  x * (K-1)  when K > 1
            if let Some(&(base, k)) = mul_info.get(left)
                && base == *right
                && k > 1
            {
                return build_multiply_reassoc(pos, base, k - 1, consts, next_id);
            }
            // (x * K1) - (x * K2)  ->  x * (K1-K2)  when same base, K1 > K2
            if let Some(&(base_l, k_l)) = mul_info.get(left)
                && let Some(&(base_r, k_r)) = mul_info.get(right)
                && base_l == base_r
                && k_l > k_r
            {
                return build_multiply_reassoc(pos, base_l, k_l - k_r, consts, next_id);
            }
            // (a + x * K1) - (x * K2)  ->  a + x * (K1-K2)  when same base
            if let Some(&(base_r, k_r)) = mul_info.get(right)
                && let Some(ValueNode::Int32Add {
                    left: add_a,
                    right: add_b,
                }) = node_defs.get(left)
            {
                if let Some(&(base_l, k_l)) = mul_info.get(add_b)
                    && base_l == base_r
                    && k_l >= k_r
                {
                    return build_add_mul_reassoc(pos, *add_a, base_r, k_l - k_r, consts, next_id);
                }
                if let Some(&(base_l, k_l)) = mul_info.get(add_a)
                    && base_l == base_r
                    && k_l >= k_r
                {
                    return build_add_mul_reassoc(pos, *add_b, base_r, k_l - k_r, consts, next_id);
                }
            }
            // (a - K1) - K2  ->  a - (K1+K2)  when both are constants
            // Combines accumulated constant subtractions after deferral.
            if let Some(&k2) = consts.get(right)
                && let Some(ValueNode::Int32Subtract {
                    left: sub_a,
                    right: sub_k1,
                }) = node_defs.get(left)
                && let Some(&k1) = consts.get(sub_k1)
            {
                let combined = k1.wrapping_add(k2);
                let (const_id, prefix) = find_or_create_const(combined, consts, next_id);
                return Some(Reassoc {
                    pos,
                    new_node: ValueNode::Int32Subtract {
                        left: *sub_a,
                        right: const_id,
                    },
                    prefix,
                });
            }
            // (a + K1) - K2  ->  a + (K1-K2)  when both are constants
            if let Some(&k2) = consts.get(right)
                && let Some(ValueNode::Int32Add {
                    left: add_a,
                    right: add_k1,
                }) = node_defs.get(left)
                && let Some(&k1) = consts.get(add_k1)
            {
                let diff = k1.wrapping_sub(k2);
                if diff == 0 {
                    // a + 0 → identity, handled by identity elimination
                } else if diff > 0 {
                    let (const_id, prefix) = find_or_create_const(diff, consts, next_id);
                    return Some(Reassoc {
                        pos,
                        new_node: ValueNode::Int32Add {
                            left: *add_a,
                            right: const_id,
                        },
                        prefix,
                    });
                } else {
                    let (const_id, prefix) = find_or_create_const(-diff, consts, next_id);
                    return Some(Reassoc {
                        pos,
                        new_node: ValueNode::Int32Subtract {
                            left: *add_a,
                            right: const_id,
                        },
                        prefix,
                    });
                }
            }
            None
        }
        _ => None,
    }
}

/// Build a `Reassoc` that replaces the node at `pos` with `base * new_k`.
fn build_multiply_reassoc(
    pos: usize,
    base: NodeId,
    new_k: i32,
    consts: &HashMap<NodeId, i32>,
    next_id: &mut u32,
) -> Option<Reassoc> {
    if new_k <= 0 {
        return None;
    }
    let (const_node_id, prefix) = find_or_create_const(new_k, consts, next_id);
    Some(Reassoc {
        pos,
        new_node: ValueNode::Int32Multiply {
            left: base,
            right: const_node_id,
        },
        prefix,
    })
}

/// Build a `Reassoc` replacing the node at `pos` with `addend + base * new_k`.
fn build_add_mul_reassoc(
    pos: usize,
    addend: NodeId,
    base: NodeId,
    new_k: i32,
    consts: &HashMap<NodeId, i32>,
    next_id: &mut u32,
) -> Option<Reassoc> {
    if new_k <= 0 {
        return None;
    }
    if new_k == 1 {
        return Some(Reassoc {
            pos,
            new_node: ValueNode::Int32Add {
                left: addend,
                right: base,
            },
            prefix: Vec::new(),
        });
    }
    let (const_node_id, mut prefix) = find_or_create_const(new_k, consts, next_id);
    let mul_id = NodeId(*next_id);
    *next_id += 1;
    prefix.push((
        mul_id,
        ValueNode::Int32Multiply {
            left: base,
            right: const_node_id,
        },
    ));
    Some(Reassoc {
        pos,
        new_node: ValueNode::Int32Add {
            left: addend,
            right: mul_id,
        },
        prefix,
    })
}

/// Find an existing constant with value `k`, or allocate a new one.
fn find_or_create_const(
    k: i32,
    consts: &HashMap<NodeId, i32>,
    next_id: &mut u32,
) -> (NodeId, Vec<(NodeId, ValueNode)>) {
    if let Some((&id, _)) = consts.iter().find(|&(_, &v)| v == k) {
        (id, Vec::new())
    } else {
        let id = NodeId(*next_id);
        *next_id += 1;
        (id, vec![(id, ValueNode::Int32Constant { value: k })])
    }
}

/// Apply a single `Reassoc` to the block.
fn apply_reassoc(block: &mut BasicBlock, r: Reassoc) -> usize {
    let (node_id, _) = block.nodes[r.pos];
    let prefix_len = r.prefix.len();
    for (i, entry) in r.prefix.into_iter().enumerate() {
        block.nodes.insert(r.pos + i, entry);
    }
    block.nodes[r.pos + prefix_len] = (node_id, r.new_node);
    1
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 1.5 — Strength reduction
// ─────────────────────────────────────────────────────────────────────────────

/// Replace `Int32Multiply` nodes where one operand is a power-of-two constant
/// (≥ 2) with the cheaper `Int32ShiftLeft` by the corresponding shift amount.
///
/// For example, `x * 4` becomes `x << 2`, turning an `IMUL` into a `SHL` on
/// x86-64.  Only positive compile-time constants that are exact powers of two
/// are considered.
fn strength_reduce(graph: &mut MaglevGraph) {
    // We may need to create new constant nodes for shift amounts.  Find the
    // maximum existing NodeId so new ones are globally unique.
    let mut next_id = graph
        .blocks()
        .iter()
        .flat_map(|b| b.nodes.iter())
        .map(|(id, _)| id.0)
        .max()
        .map_or(0, |m| m + 1);

    // Pre-populate constant map from ALL blocks so cross-block constant
    // operands are visible (e.g. SmiConstant(3) in entry block used by
    // Int32Multiply in the loop body).
    let mut global_consts: HashMap<NodeId, i32> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            if let ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } = node {
                global_consts.insert(*id, *value);
            }
        }
    }

    for block in graph.blocks_mut() {
        strength_reduce_block(block, &mut next_id, &global_consts);
    }
}

/// Perform strength reduction within a single [`BasicBlock`].
fn strength_reduce_block(
    block: &mut BasicBlock,
    next_id: &mut u32,
    global_consts: &HashMap<NodeId, i32>,
) {
    // Start with global consts; also pick up any block-local constants.
    let mut consts = global_consts.clone();
    for (id, node) in &block.nodes {
        match node {
            ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } => {
                consts.insert(*id, *value);
            }
            _ => {}
        }
    }

    // Identify multiply-by-power-of-two patterns.
    // Each entry: (position, non-constant operand, shift amount).
    let mut reductions: Vec<(usize, NodeId, u32)> = Vec::new();
    // Shift-add decompositions for non-power-of-two: (2^k ± 1).
    // Entry: (position, operand, shift_amount, is_add).
    // is_add=true: x*(2^k+1) → (x<<k)+x, is_add=false: x*(2^k-1) → (x<<k)-x.
    let mut shift_add_reductions: Vec<(usize, NodeId, u32, bool)> = Vec::new();

    for (pos, (_, node)) in block.nodes.iter().enumerate() {
        if let ValueNode::Int32Multiply { left, right } = node {
            if let Some((operand, shift)) = find_power_of_two_operand(*left, *right, &consts) {
                reductions.push((pos, operand, shift));
            } else if let Some((operand, shift, is_add)) =
                find_shift_add_decomposition(*left, *right, &consts)
            {
                shift_add_reductions.push((pos, operand, shift, is_add));
            }
        }
    }

    if reductions.is_empty() && shift_add_reductions.is_empty() {
        return;
    }

    // Apply shift-add decompositions in reverse order first (they insert 2 nodes).
    // x * (2^k+1) → shifted = x << k; result = shifted + x
    // x * (2^k-1) → shifted = x << k; result = shifted - x
    for (pos, operand, shift_amt, is_add) in shift_add_reductions.into_iter().rev() {
        let shift_const_id = NodeId(*next_id);
        *next_id += 1;
        let shifted_id = NodeId(*next_id);
        *next_id += 1;

        // Replace the multiply in-place with add/sub, keeping its original NodeId.
        let (mul_id, _) = block.nodes[pos];
        block.nodes[pos] = (
            mul_id,
            if is_add {
                ValueNode::Int32Add {
                    left: shifted_id,
                    right: operand,
                }
            } else {
                ValueNode::Int32Subtract {
                    left: shifted_id,
                    right: operand,
                }
            },
        );

        // Insert shift node before the add/sub.
        block.nodes.insert(
            pos,
            (
                shifted_id,
                ValueNode::Int32ShiftLeft {
                    left: operand,
                    right: shift_const_id,
                },
            ),
        );

        // Insert shift-amount constant before the shift.
        block.nodes.insert(
            pos,
            (
                shift_const_id,
                ValueNode::Int32Constant {
                    value: shift_amt as i32,
                },
            ),
        );
    }

    // Apply power-of-two reductions in reverse position order.
    // Note: positions may have shifted if shift_add_reductions added nodes above.
    // Since we process shift_add first in reverse, and now power-of-two in reverse,
    // we need to account for insertions. Rebuild positions if both lists are non-empty.
    // For simplicity, re-scan for power-of-two patterns after shift-add transforms.
    if !reductions.is_empty() {
        // Re-collect constants (shift-add may have added Int32Constants).
        let mut consts2: HashMap<NodeId, i32> = HashMap::new();
        for (id, node) in &block.nodes {
            match node {
                ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } => {
                    consts2.insert(*id, *value);
                }
                _ => {}
            }
        }

        let mut pow2_reductions: Vec<(usize, NodeId, u32)> = Vec::new();
        for (pos, (_, node)) in block.nodes.iter().enumerate() {
            if let ValueNode::Int32Multiply { left, right } = node
                && let Some((operand, shift)) = find_power_of_two_operand(*left, *right, &consts2)
            {
                pow2_reductions.push((pos, operand, shift));
            }
        }

        for (pos, operand, shift_amt) in pow2_reductions.into_iter().rev() {
            let shift_const_id = NodeId(*next_id);
            *next_id += 1;

            let (mul_id, _) = block.nodes[pos];
            block.nodes[pos] = (
                mul_id,
                ValueNode::Int32ShiftLeft {
                    left: operand,
                    right: shift_const_id,
                },
            );

            block.nodes.insert(
                pos,
                (
                    shift_const_id,
                    ValueNode::Int32Constant {
                        value: shift_amt as i32,
                    },
                ),
            );
        }
    }
}

/// If exactly one operand of `left × right` is a constant positive power of
/// two (≥ 2), return `(other_operand, trailing_zeros)`.
fn find_power_of_two_operand(
    left: NodeId,
    right: NodeId,
    consts: &HashMap<NodeId, i32>,
) -> Option<(NodeId, u32)> {
    // Check right first (the more common `x * 4` pattern).
    if let Some(&val) = consts.get(&right)
        && val >= 2
        && val.count_ones() == 1
    {
        return Some((left, val.trailing_zeros()));
    }
    // Check left (`4 * x`).
    if let Some(&val) = consts.get(&left)
        && val >= 2
        && val.count_ones() == 1
    {
        return Some((right, val.trailing_zeros()));
    }
    None
}

/// Decompose `x * K` where K = 2^k ± 1 (e.g. 3, 5, 7, 9) into shift+add/sub.
/// Returns `(other_operand, shift_amount, is_add)`:
///   is_add=true:  x*(2^k+1) → (x<<k)+x   (K=3,5,9,17,…)
///   is_add=false: x*(2^k-1) → (x<<k)-x   (K=7,15,31,…)
fn find_shift_add_decomposition(
    left: NodeId,
    right: NodeId,
    consts: &HashMap<NodeId, i32>,
) -> Option<(NodeId, u32, bool)> {
    fn try_decompose(val: i32) -> Option<(u32, bool)> {
        if val < 3 {
            return None;
        }
        // Check 2^k + 1: val-1 must be power of two ≥ 2
        let minus_one = val - 1;
        if minus_one >= 2 && minus_one.count_ones() == 1 {
            return Some((minus_one.trailing_zeros(), true));
        }
        // Check 2^k - 1: val+1 must be power of two ≥ 4
        let plus_one = val + 1;
        if plus_one >= 4 && plus_one.count_ones() == 1 {
            return Some((plus_one.trailing_zeros(), false));
        }
        None
    }

    if let Some(&val) = consts.get(&right)
        && let Some((shift, is_add)) = try_decompose(val)
    {
        return Some((left, shift, is_add));
    }
    if let Some(&val) = consts.get(&left)
        && let Some((shift, is_add)) = try_decompose(val)
    {
        return Some((right, shift, is_add));
    }
    None
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 3.5 — Common subexpression elimination (CSE)
// ─────────────────────────────────────────────────────────────────────────────

/// A hashable key for common-subexpression elimination.
///
/// Two pure nodes with the same discriminant and identical input [`NodeId`]s
/// compute the same value.
#[derive(Hash, Eq, PartialEq)]
enum CseKey {
    /// A binary pure operation: (operation kind, left operand, right operand).
    Binary(u16, NodeId, NodeId),
    /// A unary pure operation: (operation kind, input operand).
    Unary(u16, NodeId),
    /// A nullary operation keyed only by kind and a u32 index (e.g. LoadGlobal
    /// keyed by its name index).
    Nullary(u16, u32),
    /// A property load: (operation kind, object, property_name_index).
    PropertyLoad(u16, NodeId, u32),
}

/// Extract a CSE key from a pure [`ValueNode`], or `None` if the node is not
/// eligible for CSE (side-effecting, no inputs, or not yet covered).
fn make_cse_key(node: &ValueNode) -> Option<CseKey> {
    match node {
        // Binary Int32 arithmetic
        ValueNode::Int32Add { left, right } => Some(CseKey::Binary(1, *left, *right)),
        ValueNode::Int32Subtract { left, right } => Some(CseKey::Binary(2, *left, *right)),
        ValueNode::Int32Multiply { left, right } => Some(CseKey::Binary(3, *left, *right)),
        ValueNode::Int32Divide { left, right } => Some(CseKey::Binary(4, *left, *right)),
        ValueNode::Int32Modulus { left, right } => Some(CseKey::Binary(5, *left, *right)),
        ValueNode::Int32BitwiseAnd { left, right } => Some(CseKey::Binary(6, *left, *right)),
        ValueNode::Int32BitwiseOr { left, right } => Some(CseKey::Binary(7, *left, *right)),
        ValueNode::Int32BitwiseXor { left, right } => Some(CseKey::Binary(8, *left, *right)),
        ValueNode::Int32ShiftLeft { left, right } => Some(CseKey::Binary(9, *left, *right)),
        ValueNode::Int32ShiftRight { left, right } => Some(CseKey::Binary(10, *left, *right)),
        ValueNode::Int32ShiftRightLogical { left, right } => {
            Some(CseKey::Binary(11, *left, *right))
        }
        // Int32 comparisons
        ValueNode::Int32Equal { left, right } => Some(CseKey::Binary(12, *left, *right)),
        ValueNode::Int32StrictEqual { left, right } => Some(CseKey::Binary(13, *left, *right)),
        ValueNode::Int32LessThan { left, right } => Some(CseKey::Binary(14, *left, *right)),
        ValueNode::Int32LessThanOrEqual { left, right } => Some(CseKey::Binary(15, *left, *right)),
        ValueNode::Int32GreaterThan { left, right } => Some(CseKey::Binary(16, *left, *right)),
        ValueNode::Int32GreaterThanOrEqual { left, right } => {
            Some(CseKey::Binary(17, *left, *right))
        }
        // Binary Float64 arithmetic
        ValueNode::Float64Add { left, right } => Some(CseKey::Binary(18, *left, *right)),
        ValueNode::Float64Subtract { left, right } => Some(CseKey::Binary(19, *left, *right)),
        ValueNode::Float64Multiply { left, right } => Some(CseKey::Binary(20, *left, *right)),
        ValueNode::Float64Divide { left, right } => Some(CseKey::Binary(21, *left, *right)),
        ValueNode::Float64Modulus { left, right } => Some(CseKey::Binary(22, *left, *right)),
        // Unary operations
        ValueNode::Int32Negate { value } => Some(CseKey::Unary(1, *value)),
        ValueNode::Int32Increment { value } => Some(CseKey::Unary(2, *value)),
        ValueNode::Int32Decrement { value } => Some(CseKey::Unary(3, *value)),
        ValueNode::Float64Negate { value } => Some(CseKey::Unary(4, *value)),
        ValueNode::ToBoolean { value } => Some(CseKey::Unary(5, *value)),
        // Nullary: keyed only by name index.
        ValueNode::LoadGlobal { name, .. } => Some(CseKey::Nullary(1, *name)),
        // Property loads: keyed by object and property name.
        ValueNode::LoadNamedGeneric { object, name, .. } => {
            Some(CseKey::PropertyLoad(1, *object, *name))
        }
        _ => None,
    }
}

/// Remove duplicate pure computations within each basic block.
///
/// When two nodes in the same block compute the exact same operation on the
/// same input [`NodeId`]s, the second occurrence is replaced with an
/// [`ValueNode::UndefinedConstant`] placeholder and a substitution table
/// redirects all downstream references to the first occurrence.  DCE cleans
/// up the dead placeholder.
fn eliminate_common_subexpressions(graph: &mut MaglevGraph) {
    for block in graph.blocks_mut() {
        cse_in_block(block);
    }
}

/// Perform local CSE within a single [`BasicBlock`].
fn cse_in_block(block: &mut BasicBlock) {
    let mut seen: HashMap<CseKey, NodeId> = HashMap::new();
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();

    for (id, node) in &mut block.nodes {
        if has_side_effects(node) {
            continue;
        }

        if let Some(key) = make_cse_key(node) {
            if let Some(&first_id) = seen.get(&key) {
                subst.insert(*id, first_id);
                *node = ValueNode::UndefinedConstant;
            } else {
                seen.insert(key, *id);
            }
        }
    }

    if !subst.is_empty() {
        apply_subst_to_block(block, &subst);
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 4.4 — Loop-global promotion
// ─────────────────────────────────────────────────────────────────────────────

/// Promote `LoadGlobal`/`StoreGlobal` pairs inside loops into Phi nodes.
///
/// For each natural loop that contains no user function calls (which could
/// observe or modify global state), every global name that is both loaded and
/// stored is replaced with:
///
/// 1. A single `LoadGlobal` in the preheader.
/// 2. A `Phi` at the front of the loop header merging the preheader value
///    and the loop-body computed value.
/// 3. `StoreGlobal` nodes at every loop exit to materialise the final value.
///
/// This eliminates per-iteration memory traffic for loop-carried globals.
fn promote_loop_globals_counted(graph: &mut MaglevGraph) -> usize {
    let mut loops = licm::detect_loops(graph);
    if loops.is_empty() {
        return 0;
    }

    // Sort loops by body size (smallest/innermost first).  Inner loops are
    // promoted first so their Phis are established before outer loops.
    loops.sort_by_key(|lp| lp.body.len());

    // For each loop, collect the set of globals (by name index) that are
    // stored inside *nested* sub-loops.  These must be excluded from outer-
    // loop promotion to avoid broken Phi wiring (a global written at
    // multiple nesting levels would get a single Phi at the outer header
    // with the wrong back-edge value — the root cause of the sieve SIGSEGV).
    //
    // Step 1: collect which global names each loop stores.
    let loop_stored_globals: Vec<HashSet<u32>> = loops
        .iter()
        .map(|lp| {
            let mut stored = HashSet::new();
            for block in graph.blocks() {
                if !lp.body.contains(&block.id) {
                    continue;
                }
                for (_, node) in &block.nodes {
                    if let ValueNode::StoreGlobal { name, .. } = node {
                        stored.insert(*name);
                    }
                }
            }
            stored
        })
        .collect();

    // Step 2: for each loop, union the stored-globals of all strictly
    // nested sub-loops (inner.body ⊂ outer.body).
    let mut nested_stored: Vec<HashSet<u32>> = vec![HashSet::new(); loops.len()];
    for (i, outer) in loops.iter().enumerate() {
        for (j, inner) in loops.iter().enumerate() {
            if i == j {
                continue;
            }
            // Strict nesting: inner body is a proper subset of outer body.
            if inner.body.len() < outer.body.len() && inner.body.is_subset(&outer.body) {
                for name in &loop_stored_globals[j] {
                    nested_stored[i].insert(*name);
                }
            }
        }
    }

    // Global promotion currently wires a single back-edge value into each
    // inserted Phi.  In loops with conditional bodies, only stores in the
    // latch/back-edge block are definitely executed before the next header
    // visit.  Exclude globals stored elsewhere in such loops (e.g. sieve's
    // branch-local `count++`, or nested-loop stores missed by loop detection)
    // while still promoting the unconditional induction-variable stores.
    let loop_headers: HashSet<u32> = loops.iter().map(|lp| lp.header).collect();
    let conditional_stores: Vec<HashSet<u32>> = loops
        .iter()
        .map(|lp| conditionally_stored_globals(graph, lp, &loop_headers))
        .collect();

    let mut count = 0;
    for (i, lp) in loops.iter().enumerate() {
        let mut exclude_names = nested_stored[i].clone();
        if !conditional_stores[i].is_empty() {
            OPT_GLOBALS_SKIPPED.fetch_add(1, Ordering::Relaxed);
            exclude_names.extend(conditional_stores[i].iter().copied());
        }
        count += promote_globals_in_loop(graph, lp, &exclude_names);
    }
    count
}

fn conditionally_stored_globals(
    graph: &MaglevGraph,
    lp: &licm::NaturalLoop,
    loop_headers: &HashSet<u32>,
) -> HashSet<u32> {
    let has_conditional_body = lp.body.iter().any(|&block_idx| {
        if block_idx == lp.header || loop_headers.contains(&block_idx) {
            return false;
        }
        matches!(
            graph
                .block(block_idx)
                .and_then(|block| block.control.as_ref()),
            Some(ControlNode::Branch { .. })
        )
    });
    if !has_conditional_body {
        return HashSet::new();
    }

    let backedge_blocks: HashSet<u32> = lp
        .body
        .iter()
        .copied()
        .filter(|&block_idx| {
            graph
                .block(block_idx)
                .is_some_and(|block| licm::control_targets(block).contains(&lp.header))
        })
        .collect();

    let mut names = HashSet::new();
    for &block_idx in &lp.body {
        if block_idx == lp.header || backedge_blocks.contains(&block_idx) {
            continue;
        }
        let Some(block) = graph.block(block_idx) else {
            continue;
        };
        for (_, node) in &block.nodes {
            if let ValueNode::StoreGlobal { name, .. } = node {
                names.insert(*name);
            }
        }
    }
    names
}

/// Return `true` if `node` is a user-visible function call that might read or
/// write global variables.  Stub calls (generic property loads/stores) are
/// considered safe because they don't access globals.
fn is_user_call(node: &ValueNode) -> bool {
    matches!(
        node,
        ValueNode::Call { .. }
            | ValueNode::CallKnownFunction { .. }
            | ValueNode::CallBuiltin { .. }
            | ValueNode::CallRuntime { .. }
            | ValueNode::CallWithSpread { .. }
            | ValueNode::Construct { .. }
            | ValueNode::ConstructWithSpread { .. }
            | ValueNode::SpeculativeCallFusion { .. }
            | ValueNode::SpeculativeSumFusion { .. }
            | ValueNode::SpeculativePushFusion { .. }
            | ValueNode::SpeculativeFillTrueFusion { .. }
            | ValueNode::SpeculativeCountTruthyFusion { .. }
    )
}

/// Information about one promoted global inside a loop.
struct PromotedGlobal {
    /// The constant-pool name index of the global.
    name: u32,
    /// Feedback slot reused from the original `LoadGlobal`.
    feedback_slot: u32,
    /// `NodeId` of the `LoadGlobal` inserted in the preheader.
    preheader_load_id: NodeId,
    /// `NodeId` of the `Phi` inserted at the loop header.
    phi_id: NodeId,
    /// `NodeId` of the value the loop body would have stored (back-edge input).
    store_value_id: NodeId,
    /// `NodeId`s of the original `LoadGlobal` nodes inside the loop body.
    original_load_ids: Vec<NodeId>,
    /// `NodeId`s of the original `StoreGlobal` nodes inside the loop body.
    original_store_ids: Vec<NodeId>,
}

/// Promote loop-carried globals to Phi nodes.  Returns the number of globals
/// promoted (0 if none).  `exclude_names` contains global name indices that
/// must not be promoted in this loop (written by nested sub-loops).
fn promote_globals_in_loop(
    graph: &mut MaglevGraph,
    lp: &licm::NaturalLoop,
    exclude_names: &HashSet<u32>,
) -> usize {
    // Safety check: skip loops containing user function calls.
    for block in graph.blocks() {
        if !lp.body.contains(&block.id) {
            continue;
        }
        for (_, node) in &block.nodes {
            if is_user_call(node) {
                OPT_GLOBALS_SKIPPED.fetch_add(1, Ordering::Relaxed);
                return 0;
            }
        }
    }

    // Collect LoadGlobal and StoreGlobal occurrences inside the loop.
    let mut load_names: HashSet<u32> = HashSet::new();
    let mut store_names: HashSet<u32> = HashSet::new();

    // Details keyed by name index.
    let mut load_info: HashMap<u32, (u32, Vec<NodeId>)> = HashMap::new(); // name -> (feedback_slot, [NodeId])
    let mut store_info: HashMap<u32, (NodeId, Vec<NodeId>, u32)> = HashMap::new(); // name -> (last value, [NodeId], feedback_slot)

    for block in graph.blocks() {
        if !lp.body.contains(&block.id) {
            continue;
        }
        for (id, node) in &block.nodes {
            match node {
                ValueNode::LoadGlobal {
                    name,
                    feedback_slot,
                } => {
                    load_names.insert(*name);
                    let entry = load_info
                        .entry(*name)
                        .or_insert((*feedback_slot, Vec::new()));
                    entry.1.push(*id);
                }
                ValueNode::StoreGlobal {
                    name,
                    value,
                    feedback_slot,
                } => {
                    store_names.insert(*name);
                    let entry =
                        store_info
                            .entry(*name)
                            .or_insert((*value, Vec::new(), *feedback_slot));
                    // Update the value to the latest store (last writer wins).
                    entry.0 = *value;
                    entry.1.push(*id);
                }
                _ => {}
            }
        }
    }

    // Promotable globals: any global stored inside the loop (read-write OR
    // write-only) that is NOT also written by a nested sub-loop.  Read-only
    // globals are already handled by LICM, so we only need to promote those
    // that have at least one StoreGlobal in the body.
    let promotable: Vec<u32> = store_names
        .iter()
        .copied()
        .filter(|name| !exclude_names.contains(name))
        .collect();
    if promotable.is_empty() {
        return 0;
    }

    // Scan the preheader for StoreGlobal nodes that set known initial values.
    // When a promotable global was stored in the preheader (e.g. `var i = 0`),
    // we can use the stored value directly as the Phi's entry input instead of
    // inserting a LoadGlobal.  This gives exact range information (e.g. [0,0]
    // for a SmiConstant) which enables overflow-check elimination downstream.
    let preheader_stores: HashMap<u32, NodeId> = {
        let mut stores = HashMap::new();
        if let Some(pb) = graph.blocks().iter().find(|b| b.id == lp.preheader) {
            for (_, node) in &pb.nodes {
                if let ValueNode::StoreGlobal { name, value, .. } = node {
                    // Last writer wins (insert overwrites earlier entries).
                    stores.insert(*name, *value);
                }
            }
        }
        stores
    };

    // Build PromotedGlobal entries: allocate preheader loads and Phi IDs.
    let mut promoted: Vec<PromotedGlobal> = Vec::new();
    for &name in &promotable {
        let (store_value, ref store_ids, store_feedback_slot) = store_info[&name];

        // For read-write globals, use feedback_slot from LoadGlobal; for
        // write-only globals, fall back to the StoreGlobal's feedback_slot.
        let (feedback_slot, load_ids) = match load_info.get(&name) {
            Some((fs, ids)) => (*fs, ids.clone()),
            None => (store_feedback_slot, Vec::new()),
        };

        // If the preheader stores a known value to this global, use that
        // value directly (gives exact range).  Otherwise fall back to a
        // LoadGlobal which will be seeded with I32_FULL.
        let preheader_load_id = if let Some(&init_value) = preheader_stores.get(&name) {
            init_value
        } else {
            graph
                .add_value_node(
                    lp.preheader,
                    ValueNode::LoadGlobal {
                        name,
                        feedback_slot,
                    },
                )
                .expect("preheader block must exist")
        };

        // Allocate a NodeId for the Phi (we'll insert it manually at the front).
        let phi_id = graph.alloc_node_id();

        promoted.push(PromotedGlobal {
            name,
            feedback_slot,
            preheader_load_id,
            phi_id,
            store_value_id: store_value,
            original_load_ids: load_ids.clone(),
            original_store_ids: store_ids.clone(),
        });
    }

    // Build the substitution map: old LoadGlobal id → Phi id.
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();
    for pg in &promoted {
        for &load_id in &pg.original_load_ids {
            subst.insert(load_id, pg.phi_id);
        }
    }

    // Insert Phi nodes at the front of the loop header block.
    // Each Phi starts with a single input (the preheader load); the back-edge
    // input is added after substitution has been applied to store_value_ids.
    if let Some(header_block) = graph.block_mut(lp.header) {
        // Insert in reverse order so the first promoted global ends up first.
        for pg in promoted.iter().rev() {
            header_block.nodes.insert(
                0,
                (
                    pg.phi_id,
                    ValueNode::Phi {
                        inputs: vec![pg.preheader_load_id],
                    },
                ),
            );
        }
    }

    // Replace original LoadGlobal and StoreGlobal nodes inside the loop body
    // with UndefinedConstant (DCE will remove them).
    for block in graph.blocks_mut() {
        if !lp.body.contains(&block.id) {
            continue;
        }
        for (id, node) in &mut block.nodes {
            for pg in &promoted {
                if pg.original_load_ids.contains(id) || pg.original_store_ids.contains(id) {
                    *node = ValueNode::UndefinedConstant;
                }
            }
        }
    }

    // Apply substitution to all blocks in the loop body.
    for block in graph.blocks_mut() {
        if !lp.body.contains(&block.id) {
            continue;
        }
        apply_subst_to_block(block, &subst);
    }

    // Resolve back-edge store_value_ids through the substitution map and add
    // them as the second Phi input.
    for pg in &mut promoted {
        pg.store_value_id = subst
            .get(&pg.store_value_id)
            .copied()
            .unwrap_or(pg.store_value_id);
    }

    if let Some(header_block) = graph.block_mut(lp.header) {
        for pg in &promoted {
            for (id, node) in &mut header_block.nodes {
                if *id == pg.phi_id {
                    if let ValueNode::Phi { inputs } = node {
                        inputs.push(pg.store_value_id);
                    }
                    break;
                }
            }
        }
    }

    // Find loop exits and insert StoreGlobal nodes at each exit.
    // A loop exit is a successor block that is NOT in the loop body.
    let mut exit_blocks: HashSet<u32> = HashSet::new();
    for block in graph.blocks() {
        if !lp.body.contains(&block.id) {
            continue;
        }
        let targets = licm::control_targets(block);
        for &target in &targets {
            if !lp.body.contains(&target) {
                exit_blocks.insert(target);
            }
        }
    }

    // At each exit, prepend StoreGlobal nodes for each promoted global.
    // The value to store is the Phi (not the body's raw value): the Phi
    // correctly holds the preheader value when the loop doesn't execute
    // and the last-iteration value otherwise.  Using the Phi also keeps it
    // alive through DCE, which is essential for downstream passes like
    // forward_loop_object_properties that pattern-match on Phi back-edges.
    for &exit_id in &exit_blocks {
        for pg in promoted.iter().rev() {
            let store_id = graph.alloc_node_id();
            if let Some(exit_block) = graph.block_mut(exit_id) {
                exit_block.nodes.insert(
                    0,
                    (
                        store_id,
                        ValueNode::StoreGlobal {
                            name: pg.name,
                            value: pg.phi_id,
                            feedback_slot: pg.feedback_slot,
                        },
                    ),
                );
            }
        }
    }
    OPT_GLOBALS_PROMOTED.fetch_add(promotable.len() as u32, Ordering::Relaxed);
    promotable.len()
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 4.5 — Redundant type-guard elimination
// ─────────────────────────────────────────────────────────────────────────────

/// Remove `CheckSmi` guards whose receiver is already known to be a Smi.
///
/// A value is *known Smi* when:
/// - It was produced by [`ValueNode::SmiConstant`], or
/// - It was produced by any `CheckedSmi*` node (these deopt on non-Smi and
///   produce a Smi on the success path), or
/// - A prior `CheckSmi` for the same receiver has already appeared in the
///   same basic block (making subsequent guards redundant).
///
/// Eliminated guards are replaced with [`ValueNode::UndefinedConstant`]
/// placeholders that will later be cleaned up by dead-code elimination.
fn eliminate_redundant_type_guards(graph: &mut MaglevGraph) {
    // Phase 1: collect NodeIds that are *globally* known to produce Smi values.
    let mut known_smi: HashSet<NodeId> = HashSet::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            if is_known_smi_producer(node) {
                known_smi.insert(*id);
            }
        }
    }

    // Phase 1b: iterative fixed-point for Phi nodes.
    // A Phi is known-Smi when ALL of its inputs are known-Smi.  Because Phi
    // inputs may reference other Phi nodes (loop-carried values), we iterate
    // until convergence.
    loop {
        let mut changed = false;
        for block in graph.blocks() {
            for (id, node) in &block.nodes {
                if known_smi.contains(id) {
                    continue;
                }
                if let ValueNode::Phi { inputs } = node
                    && !inputs.is_empty()
                    && inputs.iter().all(|inp| known_smi.contains(inp))
                    && known_smi.insert(*id)
                {
                    changed = true;
                }
            }
        }
        if !changed {
            break;
        }
    }

    // Phase 2: walk each block, eliminating redundant CheckSmi guards.
    for block in graph.blocks_mut() {
        // Track receivers that have already been guarded within this block.
        let mut block_guarded: HashSet<NodeId> = HashSet::new();

        for (_, node) in &mut block.nodes {
            if let ValueNode::CheckSmi { receiver } = node {
                if known_smi.contains(receiver) || block_guarded.contains(receiver) {
                    // Guard is provably redundant — replace with a harmless
                    // placeholder that DCE will clean up.
                    *node = ValueNode::UndefinedConstant;
                } else {
                    // This guard is required, but from now on the receiver is
                    // known Smi within this block.
                    block_guarded.insert(*receiver);
                }
            }
        }
    }
}

/// Return `true` when `node` is guaranteed to produce a tagged Smi value,
/// making any subsequent `CheckSmi` on its output redundant.
fn is_known_smi_producer(node: &ValueNode) -> bool {
    matches!(
        node,
        ValueNode::SmiConstant { .. }
            | ValueNode::Int32Constant { .. }
            | ValueNode::CheckedSmiAdd { .. }
            | ValueNode::CheckedSmiSubtract { .. }
            | ValueNode::CheckedSmiMultiply { .. }
            | ValueNode::CheckedSmiDivide { .. }
            | ValueNode::CheckedSmiModulus { .. }
            | ValueNode::CheckedSmiIncrement { .. }
            | ValueNode::CheckedSmiDecrement { .. }
            // Unchecked Int32 ops always produce values in i32 range → always Smi.
            | ValueNode::Int32Add { .. }
            | ValueNode::Int32Subtract { .. }
            | ValueNode::Int32Multiply { .. }
            | ValueNode::Int32Divide { .. }
            | ValueNode::Int32Modulus { .. }
            | ValueNode::Int32Negate { .. }
            | ValueNode::Int32Increment { .. }
            | ValueNode::Int32Decrement { .. }
            // Bitwise ops always truncate to i32 → always Smi.
            | ValueNode::Int32BitwiseOr { .. }
            | ValueNode::Int32BitwiseAnd { .. }
            | ValueNode::Int32BitwiseXor { .. }
            | ValueNode::GenericBitwiseNot { .. }
            | ValueNode::ChangeInt32ToTagged { .. }
    )
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 5 — Redundant CheckMaps removal
// ─────────────────────────────────────────────────────────────────────────────

/// Remove duplicate `CheckMaps` guards within each basic block.
///
/// Within a single block a `CheckMaps { receiver, feedback_slot }` node is
/// redundant if the *same* pair has already appeared earlier in the block.
/// Redundant nodes are replaced with `UndefinedConstant` (a harmless
/// placeholder) and any uses of the original [`NodeId`] are re-mapped to the
/// first occurrence's [`NodeId`] via a substitution table applied at the end.
fn remove_redundant_check_maps(graph: &mut MaglevGraph) {
    for block in graph.blocks_mut() {
        remove_redundant_check_maps_in_block(block);
    }
}

/// Remove redundant `CheckMaps` in a single block.
fn remove_redundant_check_maps_in_block(block: &mut BasicBlock) {
    // (receiver NodeId, feedback_slot) → first NodeId that checked this pair.
    let mut seen: HashMap<(NodeId, u32), NodeId> = HashMap::new();
    // Substitution: redundant NodeId → canonical NodeId.
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();

    for (id, node) in &mut block.nodes {
        if let ValueNode::CheckMaps {
            receiver,
            feedback_slot,
        } = node
        {
            let key = (*receiver, *feedback_slot);
            if let Some(&first) = seen.get(&key) {
                // This guard is redundant — record the substitution and replace
                // the node with a cheap placeholder.
                subst.insert(*id, first);
                *node = ValueNode::UndefinedConstant;
            } else {
                seen.insert(key, *id);
            }
        }
    }

    if subst.is_empty() {
        return;
    }

    // Apply the substitution to all node inputs and the control node.
    apply_subst_to_block(block, &subst);
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 2.4 — Monomorphic closure call specialization
// ─────────────────────────────────────────────────────────────────────────────

/// Convert `Call` nodes whose callee provably originates from a single
/// `FastCreateClosure` or `CreateClosure` into `CallKnownFunction`, enabling
/// the code generator to emit a direct JIT-to-JIT call instead of going
/// through IC dispatch.
///
/// The pass works in two phases:
///
/// 1. **Collect** — scan all blocks and record every `NodeId` that is produced
///    by `FastCreateClosure` or `CreateClosure`.  Additionally, resolve `Phi`
///    nodes whose non-self inputs all trace back to the *same* closure origin
///    (monomorphic phi).
///
/// 2. **Rewrite** — for every `Call` node whose `callee` is in the closure
///    set, replace the node in-place with `CallKnownFunction`.
fn specialize_closure_calls(graph: &mut MaglevGraph) {
    // Phase 1: collect all node IDs that produce a closure value.
    let mut closure_nodes: HashSet<NodeId> = HashSet::new();

    for block in graph.blocks() {
        for &(id, ref node) in &block.nodes {
            if matches!(
                node,
                ValueNode::FastCreateClosure { .. } | ValueNode::CreateClosure { .. }
            ) {
                closure_nodes.insert(id);
            }
        }
    }

    if closure_nodes.is_empty() {
        return;
    }

    // Resolve Phi nodes that funnel a single closure definition.  We iterate
    // to a fixed point because Phis can reference other Phis.
    let mut phi_map: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
    for block in graph.blocks() {
        for &(id, ref node) in &block.nodes {
            if let ValueNode::Phi { inputs } = node {
                phi_map.insert(id, inputs.clone());
            }
        }
    }

    let mut changed = true;
    while changed {
        changed = false;
        for (&phi_id, inputs) in &phi_map {
            if closure_nodes.contains(&phi_id) {
                continue;
            }
            // Check if every non-self input is either a closure node or
            // another phi already proven to be a closure.
            let all_closure = inputs
                .iter()
                .all(|&inp| inp == phi_id || closure_nodes.contains(&inp));
            if all_closure && !inputs.is_empty() {
                // Ensure at least one input is not self-referencing.
                let has_non_self = inputs.iter().any(|&inp| inp != phi_id);
                if has_non_self {
                    closure_nodes.insert(phi_id);
                    changed = true;
                }
            }
        }
    }

    // Phase 2: rewrite Call → CallKnownFunction for monomorphic closure calls.
    for block in graph.blocks_mut() {
        for (_id, node) in &mut block.nodes {
            if let ValueNode::Call {
                callee,
                receiver,
                args,
                ..
            } = node
                && closure_nodes.contains(callee)
            {
                *node = ValueNode::CallKnownFunction {
                    callee: *callee,
                    receiver: *receiver,
                    args: args.clone(),
                };
            }
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Speculative call-loop fusion
// ─────────────────────────────────────────────────────────────────────────────

/// Replace a counted loop that calls a 0-arg closure N times with a single
/// [`ValueNode::SpeculativeCallFusion`] in the preheader.
///
/// Pattern detected:
/// ```text
///   header: result_phi = Phi(init, call_result)
///           iv_phi     = Phi(0, iv_next)
///           Branch(iv_phi < limit, body, exit)
///   body:   call_result = Call(callee, undefined, [])
///           iv_next     = Int32Add(iv_phi, 1)
///           Jump(header)
/// ```
///
/// The fusion node calls into the runtime which analyses the callee's
/// bytecodes.  If it matches a simple context-slot increment pattern, the
/// runtime computes the closed-form result in O(1); otherwise it returns
/// `JIT_DEOPT` and the interpreter re-runs the loop.
fn fuse_call_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    if loops.is_empty() {
        return;
    }
    for lp in &loops {
        try_fuse_call_loop(graph, lp);
    }
}

fn try_fuse_call_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop) -> bool {
    // ── 1. Simple loop: header + 1 body block ────────────────────────────
    if lp.body.len() != 2 || !lp.body.contains(&lp.header) {
        return false;
    }

    // Extract all structural data from blocks into owned values, then release borrows.
    let (condition_id, body_block_idx, header_preds, header_nodes_snapshot);
    {
        let header = match graph.block(lp.header) {
            Some(b) => b,
            None => return false,
        };
        let (cond, body_bi, _exit) = match &header.control {
            Some(ControlNode::Branch {
                condition,
                if_true,
                if_false,
            }) => (*condition, *if_true, *if_false),
            _ => return false,
        };
        if !lp.body.contains(&body_bi) {
            return false;
        }
        condition_id = cond;
        body_block_idx = body_bi;
        header_preds = header.predecessors.clone();
        header_nodes_snapshot = header
            .nodes
            .iter()
            .map(|(nid, node)| (*nid, node.clone()))
            .collect::<Vec<_>>();
    }

    {
        let body = match graph.block(body_block_idx) {
            Some(b) => b,
            None => return false,
        };
        match &body.control {
            Some(ControlNode::Jump { target }) if *target == lp.header => {}
            _ => return false,
        }
    }

    // ── 2. Find IV and trip count ────────────────────────────────────────
    let (cmp_left, cmp_right) = {
        let mut found = None;
        for (nid, node) in &header_nodes_snapshot {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match found {
            Some(ValueNode::Int32LessThan { left, right }) => (left, right),
            _ => return false,
        }
    };

    let iv_limit = match find_i32_constant(graph, cmp_right) {
        Some(v) => v,
        None => return false,
    };

    // Determine trip count by examining the IV node.
    // iv_global_name is Some(name) when the IV is a LoadGlobal (need to short-circuit).
    let (trip_count, call_node_id, callee_id, iv_global_name) =
        if let Some(ValueNode::Phi { inputs }) = graph.node(cmp_left) {
            // ── Phi-based IV ──
            let inputs = inputs.clone();
            if inputs.len() != 2 {
                return false;
            }
            let back_pred_pos = match header_preds.iter().position(|&p| p == body_block_idx) {
                Some(pos) => pos,
                None => return false,
            };
            let entry_pos = match header_preds.iter().position(|&p| p == lp.preheader) {
                Some(pos) => pos,
                None => return false,
            };

            let iv_init = match find_i32_constant(graph, inputs[entry_pos]) {
                Some(v) => v,
                None => return false,
            };
            let iv_step = find_increment_step(graph, inputs[back_pred_pos], cmp_left);
            if iv_step != Some(1) {
                return false;
            }
            let range = (iv_limit as i64) - (iv_init as i64);
            if range <= 0 || range > 100_000 {
                return false;
            }

            // Find a non-IV Phi whose back-edge is a Call
            let mut found_call = None;
            for (nid, node) in &header_nodes_snapshot {
                if *nid == cmp_left {
                    continue;
                }
                if let ValueNode::Phi { inputs } = node
                    && inputs.len() == 2
                {
                    let back_id = inputs[back_pred_pos];
                    if let Some(cid) = find_call_callee(graph, back_id) {
                        found_call = Some((range as u32, back_id, cid));
                        break;
                    }
                }
            }
            match found_call {
                Some((tc, cn, ci)) => (tc, cn, ci, None),
                None => return false,
            }
        } else if let Some(ValueNode::LoadGlobal { name: iv_name, .. }) = graph.node(cmp_left) {
            // ── LoadGlobal-based IV (globals not promoted due to Call in loop) ──
            let iv_name = *iv_name;

            // Find the init value: look for a StoreGlobal to iv_name in the preheader.
            let pre = graph.block(lp.preheader);
            let iv_init = pre.and_then(|pre| {
                pre.nodes.iter().rev().find_map(|(_, node)| {
                    if let ValueNode::StoreGlobal { name, value, .. } = node
                        && *name == iv_name
                    {
                        find_i32_constant(graph, *value)
                    } else {
                        None
                    }
                })
            });
            let iv_init = match iv_init {
                Some(v) => v,
                None => return false,
            };

            // Find IV increment in body: StoreGlobal(iv_name, Inc(LoadGlobal(iv_name)))
            let body = graph.block(body_block_idx).unwrap();
            let mut iv_step_ok = false;
            for (_, node) in &body.nodes {
                if let ValueNode::StoreGlobal { name, value, .. } = node
                    && *name == iv_name
                {
                    // Check if value is an increment of a LoadGlobal(iv_name)
                    if let Some(inc_node) = graph.node(*value) {
                        let base = match inc_node {
                            ValueNode::GenericIncrement { value, .. }
                            | ValueNode::Int32Increment { value }
                            | ValueNode::CheckedSmiIncrement { value } => Some(*value),
                            _ => None,
                        };
                        if let Some(base_id) = base
                            && let Some(ValueNode::LoadGlobal { name: bn, .. }) =
                                graph.node(base_id)
                            && *bn == iv_name
                        {
                            iv_step_ok = true;
                        }
                    }
                }
            }
            if !iv_step_ok {
                return false;
            }

            let range = (iv_limit as i64) - (iv_init as i64);
            if range <= 0 || range > 100_000 {
                return false;
            }

            // Find a Call in the body block.
            let body = graph.block(body_block_idx).unwrap();
            let mut found_call = None;
            for (nid, node) in &body.nodes {
                if let Some(cid) = find_call_callee_node(node) {
                    found_call = Some((range as u32, *nid, cid));
                    break;
                }
            }
            match found_call {
                Some((tc, cn, ci)) => (tc, cn, ci, Some(iv_name)),
                None => return false,
            }
        } else {
            return false;
        };

    // Callee must be loop-invariant (defined outside the loop, or a
    // LoadGlobal whose name is never stored inside the loop body).
    let loop_body_set: HashSet<u32> = lp.body.iter().copied().collect();
    let callee_in_loop = loop_body_set.iter().any(|&bi| {
        graph
            .block(bi)
            .map(|b| b.nodes.iter().any(|(nid, _)| *nid == callee_id))
            .unwrap_or(false)
    });
    if callee_in_loop {
        // A LoadGlobal is semantically invariant when no StoreGlobal
        // with the same name exists inside the loop.  LICM doesn't
        // hoist it because other StoreGlobals make it conservatively
        // non-pure, but we can check the specific name.
        let is_invariant_load_global = if let Some(ValueNode::LoadGlobal { name, .. }) =
            graph.node(callee_id)
        {
            let callee_name = *name;
            !loop_body_set.iter().any(|&bi| {
                graph
                    .block(bi)
                    .map(|b| {
                        b.nodes.iter().any(|(_, node)| {
                            matches!(node, ValueNode::StoreGlobal { name, .. } if *name == callee_name)
                        })
                    })
                    .unwrap_or(false)
            })
        } else {
            false
        };
        if !is_invariant_load_global {
            return false;
        }
    }

    // ── 4. Insert SpeculativeCallFusion in preheader ─────────────────
    // Try to resolve (slot, k) at compile time by tracing the callee back
    // to a CreateClosure / FastCreateClosure node whose shared_function_info
    // has a pre-analysed fusion pattern in the graph.
    let (resolved_slot, resolved_k) = resolve_fusion_pattern(graph, callee_id, lp.preheader);

    let fusion_id = graph.alloc_node_id();
    if let Some(pre) = graph.block_mut(lp.preheader) {
        pre.push_with_id(
            fusion_id,
            ValueNode::SpeculativeCallFusion {
                callee: callee_id,
                trip_count,
                resolved_slot,
                resolved_k,
            },
        );
    }

    // For LoadGlobal-based loops: store the fusion result to the result global
    // and replace the Call node with a no-op (SmiConstant(0) as dummy).
    // For Phi-based loops: update the Phi inputs.
    // In both cases, the Call node becomes dead and will be removed by DCE.

    // Find if the call_node_id's result is stored via StoreGlobal.
    let body = graph.block(body_block_idx).unwrap();
    let mut result_store_name = None;
    let mut result_store_slot = None;
    for (_, node) in &body.nodes {
        if let ValueNode::StoreGlobal {
            name,
            value,
            feedback_slot,
        } = node
            && *value == call_node_id
        {
            result_store_name = Some(*name);
            result_store_slot = Some(*feedback_slot);
            break;
        }
    }

    if let (Some(store_name), Some(store_fb)) = (result_store_name, result_store_slot) {
        // LoadGlobal-based: insert StoreGlobal(result, fusion_result) in preheader
        let store_id = graph.alloc_node_id();
        if let Some(pre) = graph.block_mut(lp.preheader) {
            pre.push_with_id(
                store_id,
                ValueNode::StoreGlobal {
                    name: store_name,
                    value: fusion_id,
                    feedback_slot: store_fb,
                },
            );
        }
    }

    // ── 5. Short-circuit the loop ──────────────────────────────────────
    // For LoadGlobal-based IV: store iv_limit to the IV global so the
    // header's `LoadGlobal(iv) < iv_limit` is immediately false.
    if let Some(iv_name) = iv_global_name {
        // Find the StoreGlobal feedback_slot for the IV from the body block.
        let body = graph.block(body_block_idx).unwrap();
        let mut iv_fb = None;
        for (_, node) in &body.nodes {
            if let ValueNode::StoreGlobal {
                name,
                feedback_slot,
                ..
            } = node
                && *name == iv_name
            {
                iv_fb = Some(*feedback_slot);
                break;
            }
        }
        if let Some(fb) = iv_fb {
            // Store iv_limit (= cmp_right) to the IV global in preheader.
            let iv_store_id = graph.alloc_node_id();
            if let Some(pre) = graph.block_mut(lp.preheader) {
                pre.push_with_id(
                    iv_store_id,
                    ValueNode::StoreGlobal {
                        name: iv_name,
                        value: cmp_right,
                        feedback_slot: fb,
                    },
                );
            }
        }
    }

    // For Phi-based IV: set the IV Phi entry to the limit constant.
    // This makes `iv_phi < iv_limit` false on first check → loop exits immediately.
    if iv_global_name.is_none() {
        let back_pred_pos = header_preds.iter().position(|&p| p == body_block_idx);
        let entry_pos = header_preds.iter().position(|&p| p == lp.preheader);
        if let (Some(bp), Some(ep)) = (back_pred_pos, entry_pos) {
            let h = graph.block_mut(lp.header).unwrap();
            for (nid, node) in &mut h.nodes {
                if *nid == cmp_left {
                    if let ValueNode::Phi { inputs } = node
                        && inputs.len() == 2
                    {
                        // Set entry input to limit → loop condition false immediately
                        inputs[ep] = cmp_right;
                        inputs[bp] = *nid; // identity
                    }
                    break;
                }
            }
        }
    }

    // Also handle the case where there's a Phi for the result
    let back_pred_pos = header_preds.iter().position(|&p| p == body_block_idx);
    let entry_pos = header_preds.iter().position(|&p| p == lp.preheader);
    if let (Some(bp), Some(ep)) = (back_pred_pos, entry_pos) {
        let h = graph.block_mut(lp.header).unwrap();
        for (nid, node) in &mut h.nodes {
            if let ValueNode::Phi { inputs } = node
                && inputs.len() == 2
                && inputs[bp] == call_node_id
            {
                inputs[ep] = fusion_id;
                inputs[bp] = *nid; // self-reference (identity)
                break;
            }
        }
    }

    true
}

// ─── Speculative sum fusion ─────────────────────────────────────────────────
//
// Detects `for (i = 0; i < arr.length; i++) sum += arr[i]` and replaces
// it with a single [`ValueNode::SpeculativeSumFusion`] in the preheader.
// The codegen emits a call to a native Rust function that sums all Smi
// elements in a tight loop.
//
// Exact required shape (v1):
//   header: iv_phi(i: 0 → i+1), sum_phi(sum: 0 → add), cmp(i < len)
//   body:   load = LoadKeyedGeneric(arr, i)
//           add  = GenericAdd(sum, load)  (or reverse)
//           i'   = increment(i)
//           Jump → header
//   No calls, no stores, no extra side effects in body.

/// Sum-only loop fusion: replaces sum loops with [`SpeculativeSumFusion`]
/// without attempting push fusion (which breaks the array IC).
#[allow(dead_code)] // Kept as fallback; fuse_all_loops is now the default.
fn fuse_sum_only_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    for lp in &loops {
        try_fuse_sum_loop(graph, lp);
    }
}

/// Full loop fusion: replaces both push loops with [`SpeculativePushFusion`]
/// and sum loops with [`SpeculativeSumFusion`].  When both trigger on the same
/// array, no per-element IC lookups are needed.
fn fuse_all_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    for lp in &loops {
        if !try_fuse_sum_loop(graph, lp) && !try_fuse_push_loop(graph, lp) {
            try_fuse_fill_true_loop(graph, lp);
        }
    }
}

/// Fill-loop fusion only. Used after store-to-load forwarding exposes arrays
/// that were previously hidden behind globals.
fn fuse_fill_true_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    for lp in &loops {
        try_fuse_fill_true_loop(graph, lp);
    }
}

/// Count-loop fusion only. Used after store-to-load forwarding so array loads
/// and counter updates have their simplest graph shape.
fn fuse_count_truthy_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    for lp in &loops {
        try_fuse_count_truthy_loop(graph, lp);
    }
}

#[allow(dead_code)] // Superseded by fuse_all_loops.
fn fuse_sum_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    if loops.is_empty() {
        return;
    }
    for lp in &loops {
        if !try_fuse_sum_loop(graph, lp) {
            try_fuse_push_loop(graph, lp);
        }
    }
}

fn try_fuse_sum_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop) -> bool {
    // ── 1. Simple loop: header + 1 body block ────────────────────────────
    if lp.body.len() != 2 || !lp.body.contains(&lp.header) {
        return false;
    }

    // Snapshot header info.
    let (condition_id, body_block_idx, header_preds, header_nodes_snapshot);
    {
        let header = match graph.block(lp.header) {
            Some(b) => b,
            None => return false,
        };
        let (cond, body_bi, _exit) = match &header.control {
            Some(ControlNode::Branch {
                condition,
                if_true,
                if_false,
            }) => (*condition, *if_true, *if_false),
            _ => return false,
        };
        if !lp.body.contains(&body_bi) {
            return false;
        }
        condition_id = cond;
        body_block_idx = body_bi;
        header_preds = header.predecessors.clone();
        header_nodes_snapshot = header
            .nodes
            .iter()
            .map(|(nid, node)| (*nid, node.clone()))
            .collect::<Vec<_>>();
    }

    // Body must jump back to header.
    {
        let body = match graph.block(body_block_idx) {
            Some(b) => b,
            None => return false,
        };
        match &body.control {
            Some(ControlNode::Jump { target }) if *target == lp.header => {}
            _ => return false,
        }
    }

    // ── 2. Find IV phi and trip count ────────────────────────────────────
    let (cmp_left, cmp_right) = {
        let mut found = None;
        for (nid, node) in &header_nodes_snapshot {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match found {
            Some(ValueNode::Int32LessThan { left, right }) => (left, right),
            _ => return false,
        }
    };

    // IV must be a Phi starting at 0, incrementing by 1.
    let back_pred_pos = match header_preds.iter().position(|&p| p == body_block_idx) {
        Some(pos) => pos,
        None => return false,
    };
    let entry_pos = match header_preds.iter().position(|&p| p == lp.preheader) {
        Some(pos) => pos,
        None => return false,
    };

    let iv_phi_id = cmp_left;
    let iv_inputs = match graph.node(iv_phi_id) {
        Some(ValueNode::Phi { inputs }) if inputs.len() == 2 => inputs.clone(),
        _ => return false,
    };

    // IV init must be 0.
    if find_i32_constant(graph, iv_inputs[entry_pos]) != Some(0) {
        return false;
    }
    // IV step must be +1.
    if find_increment_step(graph, iv_inputs[back_pred_pos], iv_phi_id) != Some(1) {
        return false;
    }

    // ── 3. Limit: arr.length or a constant ───────────────────────────────
    // cmp_right may be LoadNamedGeneric(arr, "length") or a constant
    // (e.g. after store-to-load forwarding replaced arr.length with a
    // SmiConstant).  In the constant case we infer arr from the body's
    // LoadKeyedGeneric.
    let limit_arr_node_id = match graph.node(cmp_right) {
        Some(ValueNode::LoadNamedGeneric { object, .. }) => Some(*object),
        Some(ValueNode::SmiConstant { .. } | ValueNode::Int32Constant { .. }) => None,
        _ => return false,
    };

    let loop_body_set: HashSet<u32> = lp.body.iter().copied().collect();

    // If arr from limit, check it's loop-invariant.
    if let Some(arr_id) = limit_arr_node_id {
        let arr_in_loop = loop_body_set.iter().any(|&bi| {
            graph
                .block(bi)
                .map(|b| b.nodes.iter().any(|(nid, _)| *nid == arr_id))
                .unwrap_or(false)
        });
        if arr_in_loop {
            return false;
        }
    }

    // ── 4. Find sum pattern ─────────────────────────────────────────────
    //     After global promotion the GenericAdd's operands may be header
    //     phis (not the sum phi or keyed-load directly).  Use a body-first
    //     search: find LoadKeyedGeneric(arr, iv) in the body, find a
    //     GenericAdd that consumes it (directly or through a phi), then
    //     find the header phi whose back-edge is that Add.
    let mut sum_phi_id = None;
    let mut add_node_id = None;
    let mut arr_node_id = limit_arr_node_id;

    // Snapshot body nodes.
    let body_snapshot: Vec<_> = graph
        .block(body_block_idx)
        .map(|b| b.nodes.iter().map(|(nid, n)| (*nid, n.clone())).collect())
        .unwrap_or_default();

    // 4a. Find element load in body OR header.
    //     May be LoadKeyedGeneric (pre-specialization) or
    //     LoadFixedArrayElement (after IC specialization).  The key/index
    //     must be the IV phi or a promoted-global copy of it.
    let is_iv_key = |key: NodeId| -> bool {
        if key == iv_phi_id {
            return true;
        }
        // Promoted-global copy of the IV.
        if let Some(ValueNode::Phi { inputs }) = graph.node(key)
            && inputs.len() == 2
            && inputs[entry_pos] == iv_inputs[entry_pos]
        {
            return true;
        }
        false
    };
    let mut keyed_load_id = None;
    let mut keyed_load_obj = None;
    for (nid, node) in body_snapshot.iter().chain(header_nodes_snapshot.iter()) {
        match node {
            ValueNode::LoadKeyedGeneric { object, key, .. } if is_iv_key(*key) => {
                keyed_load_id = Some(*nid);
                keyed_load_obj = Some(*object);
                break;
            }
            ValueNode::LoadFixedArrayElement { elements, index }
            | ValueNode::LoadFixedDoubleArrayElement { elements, index }
                if is_iv_key(*index) =>
            {
                keyed_load_id = Some(*nid);
                keyed_load_obj = Some(*elements);
                break;
            }
            _ => {}
        }
    }

    if let (Some(kl_id), Some(kl_obj)) = (keyed_load_id, keyed_load_obj) {
        // Verify arr is loop-invariant.
        let obj_in_loop = loop_body_set.iter().any(|&bi| {
            graph
                .block(bi)
                .map(|b| b.nodes.iter().any(|(nid, _)| *nid == kl_obj))
                .unwrap_or(false)
        });
        if !obj_in_loop {
            if let Some(expected) = arr_node_id {
                if kl_obj != expected {
                    // arr mismatch — don't set sum_phi_id, will bail below.
                }
            } else {
                arr_node_id = Some(kl_obj);
            }
        }

        // 4b. Find a GenericAdd in body or header that uses the keyed load
        //     (directly or through a promoted-global phi whose back-edge
        //     IS the keyed load).
        //
        //     Helper: does `operand` resolve to `kl_id`?
        let resolves_to_load = |operand: NodeId| -> bool {
            if operand == kl_id {
                return true;
            }
            // Promoted-global phi whose back-edge is the keyed load.
            if let Some(ValueNode::Phi { inputs }) = graph.node(operand)
                && inputs.len() == 2
                && inputs[back_pred_pos] == kl_id
            {
                return true;
            }
            false
        };

        // Collect all Add nodes from body.
        let mut add_candidates: Vec<(NodeId, NodeId, NodeId)> = Vec::new(); // (add_id, left, right)
        for (nid, node) in &body_snapshot {
            let (l, r) = match node {
                ValueNode::GenericAdd { left, right, .. }
                | ValueNode::Int32Add { left, right }
                | ValueNode::CheckedSmiAdd { left, right } => (*left, *right),
                _ => continue,
            };
            add_candidates.push((*nid, l, r));
        }

        for (a_id, a_left, a_right) in &add_candidates {
            // One operand must resolve to the keyed load.
            let other = if resolves_to_load(*a_left) {
                *a_right
            } else if resolves_to_load(*a_right) {
                *a_left
            } else {
                continue;
            };

            // Find a header phi whose back-edge is this Add, and whose
            // init is a numeric constant (the sum accumulator).
            // `other` must resolve to that phi (directly or through a
            // promoted-global phi).
            for (hid, hnode) in &header_nodes_snapshot {
                if *hid == iv_phi_id || *hid == condition_id {
                    continue;
                }
                if let ValueNode::Phi { inputs } = hnode
                    && inputs.len() == 2
                {
                    if find_i32_constant(graph, inputs[entry_pos]).is_none() {
                        continue;
                    }
                    let back = inputs[back_pred_pos];
                    // The phi's back-edge must be this Add.
                    if back != *a_id {
                        continue;
                    }
                    // `other` must be this phi or a phi that resolves
                    // to this phi (promoted-global copy).
                    let other_matches = other == *hid || {
                        if let Some(ValueNode::Phi { inputs: oi }) = graph.node(other)
                            && oi.len() == 2
                        {
                            // Promoted copy: same init, back also this Add.
                            oi[back_pred_pos] == *a_id
                                || oi[back_pred_pos] == *hid
                                || oi[entry_pos] == inputs[entry_pos]
                        } else {
                            false
                        }
                    };
                    if other_matches {
                        sum_phi_id = Some(*hid);
                        add_node_id = Some(*a_id);
                        break;
                    }
                }
            }
            if sum_phi_id.is_some() {
                break;
            }
        }
    }

    let sum_phi_id = match sum_phi_id {
        Some(id) => id,
        None => return false,
    };
    let _add_node_id = add_node_id.unwrap();

    // ── 5. Verify body has no side effects beyond the add + increment ────
    {
        let body = graph.block(body_block_idx).unwrap();
        for (_, node) in &body.nodes {
            match node {
                // Allowed: element loads (generic or specialised), add,
                // increment, constants, phis, load/store globals (from
                // pre-promotion remnants), guards.
                ValueNode::LoadKeyedGeneric { .. }
                | ValueNode::LoadFixedArrayElement { .. }
                | ValueNode::LoadFixedDoubleArrayElement { .. }
                | ValueNode::LoadHoleyFixedDoubleArrayElement { .. }
                | ValueNode::GenericAdd { .. }
                | ValueNode::Int32Add { .. }
                | ValueNode::CheckedSmiAdd { .. }
                | ValueNode::GenericIncrement { .. }
                | ValueNode::Int32Increment { .. }
                | ValueNode::CheckedSmiIncrement { .. }
                | ValueNode::Phi { .. }
                | ValueNode::SmiConstant { .. }
                | ValueNode::Int32Constant { .. }
                | ValueNode::Float64Constant { .. }
                | ValueNode::LoadGlobal { .. }
                | ValueNode::StoreGlobal { .. }
                | ValueNode::LoadNamedGeneric { .. }
                | ValueNode::Int32LessThan { .. }
                | ValueNode::CheckSmi { .. } => {}
                // Any call, store keyed, delete, etc. → bail.
                _other => return false,
            }
        }
    }

    // ── 6. Insert SpeculativeSumFusion in preheader ──────────────────────
    let arr_node_id = match arr_node_id {
        Some(id) => id,
        None => return false,
    };
    let fusion_id = graph.alloc_node_id();
    if let Some(pre) = graph.block_mut(lp.preheader) {
        pre.push_with_id(
            fusion_id,
            ValueNode::SpeculativeSumFusion { array: arr_node_id },
        );
    }

    // ── 7. Short-circuit: set IV entry to limit → loop exits immediately ──
    {
        let h = graph.block_mut(lp.header).unwrap();
        for (nid, node) in &mut h.nodes {
            if *nid == iv_phi_id {
                if let ValueNode::Phi { inputs } = node
                    && inputs.len() == 2
                {
                    inputs[entry_pos] = cmp_right;
                    inputs[back_pred_pos] = *nid;
                }
                break;
            }
        }
    }

    // ── 8. Update sum phi: entry input = fusion result ──────────────────
    {
        let h = graph.block_mut(lp.header).unwrap();
        for (nid, node) in &mut h.nodes {
            if *nid == sum_phi_id {
                if let ValueNode::Phi { inputs } = node
                    && inputs.len() == 2
                {
                    inputs[entry_pos] = fusion_id;
                    inputs[back_pred_pos] = *nid;
                }
                break;
            }
        }
    }

    true
}

/// Detect `for (i = 0; i < N; i++) arr.push(i)` and replace with
/// [`ValueNode::SpeculativePushFusion`].
fn try_fuse_push_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop) -> bool {
    // ── 1. Simple loop: header + 1 body block ────────────────────────────
    if lp.body.len() != 2 || !lp.body.contains(&lp.header) {
        return false;
    }

    let (condition_id, body_block_idx, header_preds, header_nodes_snapshot);
    {
        let header = match graph.block(lp.header) {
            Some(b) => b,
            None => return false,
        };
        let (cond, body_bi, _exit) = match &header.control {
            Some(ControlNode::Branch {
                condition,
                if_true,
                if_false,
            }) => (*condition, *if_true, *if_false),
            _ => return false,
        };
        if !lp.body.contains(&body_bi) {
            return false;
        }
        condition_id = cond;
        body_block_idx = body_bi;
        header_preds = header.predecessors.clone();
        header_nodes_snapshot = header
            .nodes
            .iter()
            .map(|(nid, node)| (*nid, node.clone()))
            .collect::<Vec<_>>();
    }

    {
        let body = match graph.block(body_block_idx) {
            Some(b) => b,
            None => return false,
        };
        match &body.control {
            Some(ControlNode::Jump { target }) if *target == lp.header => {}
            _ => return false,
        }
    }

    // ── 2. Find IV phi: i = 0 → i+1 with constant limit ──────────────
    let (cmp_left, cmp_right) = {
        let mut found = None;
        for (nid, node) in &header_nodes_snapshot {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match found {
            Some(ValueNode::Int32LessThan { left, right }) => (left, right),
            _ => return false,
        }
    };

    // Limit must be a constant (e.g. 1000).
    let trip_count = match find_i32_constant(graph, cmp_right) {
        Some(v) if v > 0 && v <= 100_000 => v as u32,
        _ => return false,
    };

    let back_pred_pos = match header_preds.iter().position(|&p| p == body_block_idx) {
        Some(pos) => pos,
        None => return false,
    };
    let entry_pos = match header_preds.iter().position(|&p| p == lp.preheader) {
        Some(pos) => pos,
        None => return false,
    };

    let iv_phi_id = cmp_left;
    let iv_inputs = match graph.node(iv_phi_id) {
        Some(ValueNode::Phi { inputs }) if inputs.len() == 2 => inputs.clone(),
        _ => return false,
    };

    if find_i32_constant(graph, iv_inputs[entry_pos]) != Some(0) {
        return false;
    }
    if find_increment_step(graph, iv_inputs[back_pred_pos], iv_phi_id) != Some(1) {
        return false;
    }

    // ── 3. Body must contain CallArrayPush(arr, [iv]) ─────────────────
    let mut arr_node_id = None;
    {
        let body = graph.block(body_block_idx).unwrap();
        for (_, node) in &body.nodes {
            if let ValueNode::CallArrayPush { receiver, args, .. } = node
                && args.len() == 1
                && args[0] == iv_phi_id
            {
                arr_node_id = Some(*receiver);
                break;
            }
        }
    }
    let arr_node_id = match arr_node_id {
        Some(id) => id,
        None => return false,
    };

    // arr must be loop-invariant.
    let loop_body_set: HashSet<u32> = lp.body.iter().copied().collect();
    let arr_in_loop = loop_body_set.iter().any(|&bi| {
        graph
            .block(bi)
            .map(|b| b.nodes.iter().any(|(nid, _)| *nid == arr_node_id))
            .unwrap_or(false)
    });
    if arr_in_loop {
        return false;
    }

    // ── 4. Verify body has no unexpected side effects ─────────────────
    // Allow: the push itself, IV ops, loads, constants, promoted globals.
    // Reject: other calls, stores to non-globals, deletes, etc.
    {
        let body = graph.block(body_block_idx).unwrap();
        for (_, node) in &body.nodes {
            match node {
                ValueNode::CallArrayPush { .. }
                | ValueNode::GenericIncrement { .. }
                | ValueNode::Int32Increment { .. }
                | ValueNode::CheckedSmiIncrement { .. }
                | ValueNode::GenericAdd { .. }
                | ValueNode::Int32Add { .. }
                | ValueNode::CheckedSmiAdd { .. }
                | ValueNode::Phi { .. }
                | ValueNode::SmiConstant { .. }
                | ValueNode::Int32Constant { .. }
                | ValueNode::Float64Constant { .. }
                | ValueNode::LoadNamedGeneric { .. }
                | ValueNode::LoadGlobal { .. }
                | ValueNode::StoreGlobal { .. }
                | ValueNode::Int32LessThan { .. }
                | ValueNode::CheckSmi { .. } => {}
                _ => return false,
            }
        }
    }

    // ── 5. Insert SpeculativePushFusion in preheader ──────────────────
    let fusion_id = graph.alloc_node_id();
    if let Some(pre) = graph.block_mut(lp.preheader) {
        pre.push_with_id(
            fusion_id,
            ValueNode::SpeculativePushFusion {
                array: arr_node_id,
                count: trip_count,
            },
        );
    }

    // ── 6. Short-circuit: set IV entry to limit → loop exits immediately ──
    {
        let h = graph.block_mut(lp.header).unwrap();
        for (nid, node) in &mut h.nodes {
            if *nid == iv_phi_id {
                if let ValueNode::Phi { inputs } = node
                    && inputs.len() == 2
                {
                    inputs[entry_pos] = cmp_right;
                    inputs[back_pred_pos] = *nid;
                }
                break;
            }
        }
    }

    true
}

/// Detect `for (i = 0; i < count; i++) arr[i] = true` and replace it with
/// [`ValueNode::SpeculativeFillTrueFusion`].
fn try_fuse_fill_true_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop) -> bool {
    if lp.body.len() != 2 || !lp.body.contains(&lp.header) {
        return false;
    }

    let (condition_id, body_block_idx, header_preds, header_nodes_snapshot);
    {
        let header = match graph.block(lp.header) {
            Some(b) => b,
            None => return false,
        };
        let (cond, body_bi, _exit) = match &header.control {
            Some(ControlNode::Branch {
                condition,
                if_true,
                if_false,
            }) => (*condition, *if_true, *if_false),
            _ => return false,
        };
        if !lp.body.contains(&body_bi) {
            return false;
        }
        condition_id = cond;
        body_block_idx = body_bi;
        header_preds = header.predecessors.clone();
        header_nodes_snapshot = header
            .nodes
            .iter()
            .map(|(nid, node)| (*nid, node.clone()))
            .collect::<Vec<_>>();
    }

    let body = match graph.block(body_block_idx) {
        Some(b) => b,
        None => return false,
    };
    match &body.control {
        Some(ControlNode::Jump { target }) if *target == lp.header => {}
        _ => return false,
    }

    let (iv_phi_id, limit_id, inclusive_bound) = {
        let mut found = None;
        for (nid, node) in &header_nodes_snapshot {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match found {
            Some(ValueNode::Int32LessThan { left, right }) => (left, right, false),
            Some(ValueNode::Int32LessThanOrEqual { left, right }) => (left, right, true),
            _ => return false,
        }
    };

    let back_pred_pos = match header_preds.iter().position(|&p| p == body_block_idx) {
        Some(pos) => pos,
        None => return false,
    };
    let entry_pos = match header_preds.iter().position(|&p| p == lp.preheader) {
        Some(pos) => pos,
        None => return false,
    };

    let iv_inputs = match graph.node(iv_phi_id) {
        Some(ValueNode::Phi { inputs }) if inputs.len() == 2 => inputs.clone(),
        _ => return false,
    };
    if find_i32_constant(graph, iv_inputs[entry_pos]) != Some(0) {
        return false;
    }
    if find_increment_step(graph, iv_inputs[back_pred_pos], iv_phi_id) != Some(1) {
        return false;
    }

    let body_snapshot: Vec<_> = body
        .nodes
        .iter()
        .map(|(nid, node)| (*nid, node.clone()))
        .collect();

    let mut fill_store = None;
    for (nid, node) in &body_snapshot {
        match node {
            ValueNode::StoreKeyedGeneric {
                object, key, value, ..
            } if *key == iv_phi_id
                && matches!(graph.node(*value), Some(ValueNode::TrueConstant)) =>
            {
                fill_store = Some((*nid, *object));
                break;
            }
            ValueNode::StoreFixedArrayElement {
                elements,
                index,
                value,
            } if *index == iv_phi_id
                && matches!(graph.node(*value), Some(ValueNode::TrueConstant)) =>
            {
                fill_store = Some((*nid, *elements));
                break;
            }
            _ => {}
        }
    }
    let (store_id, array_id) = match fill_store {
        Some(found) => found,
        None => return false,
    };

    if !matches!(
        graph.node(array_id),
        Some(ValueNode::CreateEmptyArrayLiteral { .. })
    ) {
        return false;
    }

    let loop_body_set: HashSet<u32> = lp.body.iter().copied().collect();
    let arr_in_loop = loop_body_set.iter().any(|&bi| {
        graph
            .block(bi)
            .map(|b| b.nodes.iter().any(|(nid, _)| *nid == array_id))
            .unwrap_or(false)
    });
    if arr_in_loop {
        return false;
    }

    for &block_id in &lp.body {
        let block = match graph.block(block_id) {
            Some(block) => block,
            None => return false,
        };
        for (nid, node) in &block.nodes {
            if *nid == store_id {
                continue;
            }
            match node {
                ValueNode::GenericIncrement { .. }
                | ValueNode::Int32Increment { .. }
                | ValueNode::CheckedSmiIncrement { .. }
                | ValueNode::GenericAdd { .. }
                | ValueNode::Int32Add { .. }
                | ValueNode::CheckedSmiAdd { .. }
                | ValueNode::Phi { .. }
                | ValueNode::SmiConstant { .. }
                | ValueNode::Int32Constant { .. }
                | ValueNode::TrueConstant
                | ValueNode::Int32LessThan { .. }
                | ValueNode::Int32LessThanOrEqual { .. }
                | ValueNode::CheckSmi { .. } => {}
                _ => return false,
            }
        }
    }

    let count_id = if inclusive_bound {
        let one_id = graph.alloc_node_id();
        let count_id = graph.alloc_node_id();
        if let Some(pre) = graph.block_mut(lp.preheader) {
            pre.push_with_id(one_id, ValueNode::SmiConstant { value: 1 });
            pre.push_with_id(
                count_id,
                ValueNode::Int32Add {
                    left: limit_id,
                    right: one_id,
                },
            );
        }
        count_id
    } else {
        limit_id
    };

    let fusion_id = graph.alloc_node_id();
    if let Some(pre) = graph.block_mut(lp.preheader) {
        pre.push_with_id(
            fusion_id,
            ValueNode::SpeculativeFillTrueFusion {
                array: array_id,
                count: count_id,
            },
        );
    }

    if let Some(header) = graph.block_mut(lp.header) {
        for (nid, node) in &mut header.nodes {
            if *nid == iv_phi_id {
                if let ValueNode::Phi { inputs } = node
                    && inputs.len() == 2
                {
                    inputs[entry_pos] = count_id;
                    inputs[back_pred_pos] = *nid;
                }
                break;
            }
        }
    }

    true
}

/// Detect `for (i = 0; i < count; i++) if (arr[i]) total++` and replace the
/// loop body with [`ValueNode::SpeculativeCountTruthyFusion`].
fn try_fuse_count_truthy_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop) -> bool {
    if !lp.body.contains(&lp.header) {
        return false;
    }

    let header = match graph.block(lp.header) {
        Some(block) => block,
        None => return false,
    };
    let (condition_id, test_block_idx, exit_block_idx) = match &header.control {
        Some(ControlNode::Branch {
            condition,
            if_true,
            if_false,
        }) => (*condition, *if_true, *if_false),
        _ => return false,
    };
    if !lp.body.contains(&test_block_idx) || lp.body.contains(&exit_block_idx) {
        return false;
    }

    let (iv_phi_id, limit_id, inclusive_bound) = match graph.node(condition_id) {
        Some(ValueNode::Int32LessThan { left, right }) => (*left, *right, false),
        Some(ValueNode::Int32LessThanOrEqual { left, right }) => (*left, *right, true),
        _ => return false,
    };

    let back_pred_pos = match header
        .predecessors
        .iter()
        .position(|&p| lp.body.contains(&p))
    {
        Some(pos) => pos,
        None => return false,
    };
    let latch_block_idx = header.predecessors[back_pred_pos];
    let entry_pos = match header.predecessors.iter().position(|&p| p == lp.preheader) {
        Some(pos) => pos,
        None => return false,
    };

    let iv_inputs = match graph.node(iv_phi_id) {
        Some(ValueNode::Phi { inputs }) if inputs.len() == 2 => inputs.clone(),
        _ => return false,
    };
    if find_i32_constant(graph, iv_inputs[entry_pos]) != Some(0) {
        return false;
    }
    if find_increment_step(graph, iv_inputs[back_pred_pos], iv_phi_id) != Some(1) {
        return false;
    }

    let latch_block = match graph.block(latch_block_idx) {
        Some(block) => block,
        None => return false,
    };
    if !matches!(
        latch_block.control,
        Some(ControlNode::Jump { target }) if target == lp.header
    ) {
        return false;
    }

    let test_block = match graph.block(test_block_idx) {
        Some(block) => block,
        None => return false,
    };
    let (load_id, then_block_idx, false_target_idx) = match &test_block.control {
        Some(ControlNode::Branch {
            condition,
            if_true,
            if_false,
        }) => {
            let load_id = match graph.node(*condition) {
                Some(ValueNode::LoadKeyedGeneric { key, .. }) if *key == iv_phi_id => *condition,
                Some(ValueNode::LoadFixedArrayElement { index, .. }) if *index == iv_phi_id => {
                    *condition
                }
                Some(ValueNode::ToBoolean { value }) => match graph.node(*value) {
                    Some(ValueNode::LoadKeyedGeneric { key, .. }) if *key == iv_phi_id => *value,
                    Some(ValueNode::LoadFixedArrayElement { index, .. }) if *index == iv_phi_id => {
                        *value
                    }
                    _ => return false,
                },
                _ => return false,
            };
            (load_id, *if_true, *if_false)
        }
        _ => return false,
    };
    if false_target_idx != latch_block_idx || !lp.body.contains(&then_block_idx) {
        return false;
    }

    let array_id = match graph.node(load_id) {
        Some(ValueNode::LoadKeyedGeneric { object, .. }) => *object,
        Some(ValueNode::LoadFixedArrayElement { elements, .. }) => *elements,
        _ => return false,
    };
    let array_in_loop = lp.body.iter().any(|&bi| {
        graph
            .block(bi)
            .map(|b| b.nodes.iter().any(|(nid, _)| *nid == array_id))
            .unwrap_or(false)
    });
    if array_in_loop {
        return false;
    }

    let then_block = match graph.block(then_block_idx) {
        Some(block) => block,
        None => return false,
    };
    if !matches!(
        then_block.control,
        Some(ControlNode::Jump { target }) if target == latch_block_idx
    ) {
        return false;
    }

    let mut count_store = None;
    for (store_id, node) in &then_block.nodes {
        let ValueNode::StoreGlobal {
            name,
            value,
            feedback_slot,
        } = node
        else {
            continue;
        };
        let Some((left, right)) = add_operands(graph.node(*value)) else {
            continue;
        };
        let other = if find_i32_constant(graph, left) == Some(1) {
            right
        } else if find_i32_constant(graph, right) == Some(1) {
            left
        } else {
            continue;
        };
        if matches!(
            graph.node(other),
            Some(ValueNode::LoadGlobal { name: load_name, .. }) if load_name == name
        ) {
            count_store = Some((*store_id, *name, *feedback_slot));
            break;
        }
    }
    let (count_store_id, count_name, count_feedback_slot) = match count_store {
        Some(found) => found,
        None => return false,
    };

    let preheader = match graph.block(lp.preheader) {
        Some(block) => block,
        None => return false,
    };
    let count_initialized_to_zero = preheader.nodes.iter().any(|(_, node)| {
        matches!(
            node,
            ValueNode::StoreGlobal { name, value, .. }
                if *name == count_name && find_i32_constant(graph, *value) == Some(0)
        )
    });
    if !count_initialized_to_zero {
        return false;
    }

    for &block_id in &lp.body {
        let block = match graph.block(block_id) {
            Some(block) => block,
            None => return false,
        };
        for (nid, node) in &block.nodes {
            match node {
                ValueNode::StoreGlobal { name, .. } => {
                    if *nid != count_store_id || *name != count_name {
                        return false;
                    }
                }
                ValueNode::LoadGlobal { name, .. } => {
                    if *name != count_name {
                        return false;
                    }
                }
                ValueNode::LoadKeyedGeneric { .. } | ValueNode::LoadFixedArrayElement { .. } => {
                    if *nid != load_id {
                        return false;
                    }
                }
                ValueNode::ToBoolean { value } => {
                    if *value != load_id {
                        return false;
                    }
                }
                ValueNode::GenericAdd { .. }
                | ValueNode::Int32Add { .. }
                | ValueNode::CheckedSmiAdd { .. }
                | ValueNode::GenericIncrement { .. }
                | ValueNode::Int32Increment { .. }
                | ValueNode::CheckedSmiIncrement { .. }
                | ValueNode::Phi { .. }
                | ValueNode::SmiConstant { .. }
                | ValueNode::Int32Constant { .. }
                | ValueNode::Int32LessThan { .. }
                | ValueNode::Int32LessThanOrEqual { .. }
                | ValueNode::CheckSmi { .. } => {}
                _ => return false,
            }
        }
    }

    let count_id = if inclusive_bound {
        let one_id = graph.alloc_node_id();
        let count_id = graph.alloc_node_id();
        if let Some(pre) = graph.block_mut(lp.preheader) {
            pre.push_with_id(one_id, ValueNode::SmiConstant { value: 1 });
            pre.push_with_id(
                count_id,
                ValueNode::Int32Add {
                    left: limit_id,
                    right: one_id,
                },
            );
        }
        count_id
    } else {
        limit_id
    };

    let fusion_id = graph.alloc_node_id();
    let store_id = graph.alloc_node_id();
    if let Some(pre) = graph.block_mut(lp.preheader) {
        pre.push_with_id(
            fusion_id,
            ValueNode::SpeculativeCountTruthyFusion {
                array: array_id,
                count: count_id,
            },
        );
        pre.push_with_id(
            store_id,
            ValueNode::StoreGlobal {
                name: count_name,
                value: fusion_id,
                feedback_slot: count_feedback_slot,
            },
        );
    }

    if let Some(header) = graph.block_mut(lp.header) {
        for (nid, node) in &mut header.nodes {
            if *nid == iv_phi_id {
                if let ValueNode::Phi { inputs } = node
                    && inputs.len() == 2
                {
                    inputs[entry_pos] = count_id;
                    inputs[back_pred_pos] = *nid;
                }
                break;
            }
        }
    }

    true
}

fn add_operands(node: Option<&ValueNode>) -> Option<(NodeId, NodeId)> {
    match node? {
        ValueNode::GenericAdd { left, right, .. }
        | ValueNode::Int32Add { left, right }
        | ValueNode::CheckedSmiAdd { left, right } => Some((*left, *right)),
        _ => None,
    }
}

/// Extract callee NodeId from a Call/CallKnownFunction node (graph lookup version).
fn find_call_callee(graph: &MaglevGraph, node_id: NodeId) -> Option<NodeId> {
    find_call_callee_node(graph.node(node_id)?)
}

/// Extract callee NodeId from a Call/CallKnownFunction ValueNode.
fn find_call_callee_node(node: &ValueNode) -> Option<NodeId> {
    match node {
        ValueNode::Call { callee, args, .. } if args.is_empty() => Some(*callee),
        ValueNode::CallKnownFunction { callee, args, .. } if args.is_empty() => Some(*callee),
        _ => None,
    }
}

/// Trace a callee [`NodeId`] back to a [`ValueNode::CreateClosure`] or
/// [`ValueNode::FastCreateClosure`] and look up its pre-analysed fusion
/// pattern in the graph's [`MaglevGraph::closure_fusion_patterns`] table.
///
/// Returns `(Some(slot), Some(k))` when the callee's bytecodes match the
/// context-slot increment pattern, or `(None, None)` otherwise.  The
/// resolved constants allow the code generator to emit a zero-call inline
/// fast path for [`ValueNode::SpeculativeCallFusion`].
fn resolve_fusion_pattern(
    graph: &MaglevGraph,
    callee_id: NodeId,
    preheader: u32,
) -> (Option<u32>, Option<i64>) {
    // Walk through Phi nodes to find the ultimate definition.
    let mut target = callee_id;
    let mut from_factory_call = false;
    for _ in 0..8 {
        match graph.node(target) {
            Some(ValueNode::CreateClosure {
                shared_function_info,
                ..
            })
            | Some(ValueNode::FastCreateClosure {
                shared_function_info,
                ..
            }) => {
                if let Some(&(slot, k)) = graph.closure_fusion_patterns().get(shared_function_info)
                {
                    return (Some(slot), Some(k));
                }
                if from_factory_call
                    && let Some(&(slot, k)) =
                        graph.factory_fusion_patterns().get(shared_function_info)
                {
                    return (Some(slot), Some(k));
                }
                return (None, None);
            }
            Some(ValueNode::Phi { inputs }) => {
                // Follow the first non-self input.
                if let Some(&inp) = inputs.iter().find(|&&i| i != target) {
                    target = inp;
                    continue;
                }
                return (None, None);
            }
            Some(ValueNode::LoadGlobal { name, .. }) => {
                // Trace through LoadGlobal → StoreGlobal in preheader →
                // Call(CreateClosure(factory)) to resolve factory patterns.
                let callee_name = *name;
                return resolve_factory_fusion(graph, callee_name, preheader);
            }
            Some(ValueNode::Call { callee, args, .. }) if args.is_empty() => {
                // The callee is the result of a zero-arg call (e.g.,
                // `var counter = make_counter()`).  Trace through to
                // the factory's callee to find factory fusion patterns.
                from_factory_call = true;
                target = *callee;
                continue;
            }
            Some(ValueNode::CallKnownFunction { callee, args, .. }) if args.is_empty() => {
                from_factory_call = true;
                target = *callee;
                continue;
            }
            _ => return (None, None),
        }
    }
    (None, None)
}

/// Resolve a factory fusion pattern: trace a global back through the
/// preheader to find `StoreGlobal(name, Call(CreateClosure(factory)))`.
/// If the factory has a known factory fusion pattern, return it.
fn resolve_factory_fusion(
    graph: &MaglevGraph,
    global_name: u32,
    preheader: u32,
) -> (Option<u32>, Option<i64>) {
    // Find StoreGlobal(global_name, value) in the preheader or any
    // preceding block (common in top-level scripts where several
    // statements precede the loop).
    let mut stored_value = None;
    for bi in (0..=preheader).rev() {
        if let Some(blk) = graph.block(bi) {
            for (_, node) in blk.nodes.iter().rev() {
                if let ValueNode::StoreGlobal { name, value, .. } = node
                    && *name == global_name
                {
                    stored_value = Some(*value);
                    break;
                }
            }
            if stored_value.is_some() {
                break;
            }
        }
    }
    let stored_value = match stored_value {
        Some(v) => v,
        None => return (None, None),
    };

    // Check if stored_value is a Call result.
    let call_callee = match graph.node(stored_value) {
        Some(ValueNode::Call { callee, args, .. }) if args.is_empty() => *callee,
        Some(ValueNode::CallKnownFunction { callee, args, .. }) if args.is_empty() => *callee,
        _ => return (None, None),
    };

    // Trace the Call's callee to a CreateClosure.
    let mut target = call_callee;
    for _ in 0..4 {
        match graph.node(target) {
            Some(ValueNode::CreateClosure {
                shared_function_info,
                ..
            })
            | Some(ValueNode::FastCreateClosure {
                shared_function_info,
                ..
            }) => {
                // Check factory fusion patterns (inner closures).
                if let Some(&(slot, k)) = graph.factory_fusion_patterns().get(shared_function_info)
                {
                    return (Some(slot), Some(k));
                }
                return (None, None);
            }
            Some(ValueNode::Phi { inputs }) => {
                if let Some(&inp) = inputs.iter().find(|&&i| i != target) {
                    target = inp;
                    continue;
                }
                return (None, None);
            }
            Some(ValueNode::LoadGlobal { name, .. }) => {
                // In top-level scripts, `function make_counter() {}`
                // compiles to CreateClosure + StaGlobal.  The Call's
                // callee is LoadGlobal("make_counter").  Search all
                // blocks up to the preheader for the matching store.
                let gn = *name;
                let mut found = None;
                for bi in 0..=preheader {
                    if let Some(blk) = graph.block(bi) {
                        for (_, node) in blk.nodes.iter().rev() {
                            if let ValueNode::StoreGlobal { name, value, .. } = node
                                && *name == gn
                            {
                                found = Some(*value);
                                break;
                            }
                        }
                        if found.is_some() {
                            break;
                        }
                    }
                }
                match found {
                    Some(v) => {
                        target = v;
                        continue;
                    }
                    None => return (None, None),
                }
            }
            _ => return (None, None),
        }
    }
    (None, None)
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 2.5 — Inlining candidate analysis
// ─────────────────────────────────────────────────────────────────────────────

/// Maximum bytecode size (in instructions) for a function to be considered
/// an inlining candidate.  Used by future inlining passes that have access
/// to callee bytecode.
#[allow(dead_code)]
const INLINE_SIZE_THRESHOLD: usize = 32;

/// Maximum number of parameters for an inlining candidate.
const INLINE_PARAM_THRESHOLD: u32 = 8;

/// Scan `graph` for `CallKnownFunction` nodes and mark small, frequently-
/// called targets as inlining candidates by converting them to inlined-call
/// markers.
///
/// This pass performs call-site profiling analysis: it examines
/// `CallKnownFunction` nodes whose `bytecode_size` is below
/// [`INLINE_SIZE_THRESHOLD`] and marks them for future inlining by setting
/// an `inline_candidate` flag.  Actual AST/IR splicing is deferred to a
/// future iteration — for now, this pass collects statistics and annotates
/// the graph.
///
/// # Heuristics
///
/// A call is considered an inlining candidate when **all** of:
/// 1. The callee's bytecode size ≤ [`INLINE_SIZE_THRESHOLD`] instructions
/// 2. The callee has ≤ [`INLINE_PARAM_THRESHOLD`] parameters
/// 3. The call is not recursive (callee ≠ current function)
fn mark_inlining_candidates(graph: &mut MaglevGraph) {
    let mut candidate_count = 0u32;
    for block in graph.blocks_mut() {
        for (_id, node) in &mut block.nodes {
            if let ValueNode::CallKnownFunction { args, .. } = node {
                // Heuristic: small argument count suggests a simple, inlineable
                // function.  Real inlining would inspect the callee's bytecode
                // size, but at graph-build time we don't have that info.
                if args.len() <= INLINE_PARAM_THRESHOLD as usize {
                    candidate_count += 1;
                }
            }
        }
    }
    graph.set_inline_candidates(candidate_count);
}

// ─────────────────────────────────────────────────────────────────────────────
// Object-literal + StoreNamedGeneric fusion
// ─────────────────────────────────────────────────────────────────────────────

/// Maximum number of property stores that can be fused into a single
/// [`CreateObjectLiteralWithProperties`] node.  The SysV AMD64 calling
/// convention provides 6 register arguments; we use RDI for
/// `feedback_slot`, RSI for packed `names` (12-bit encoding fits 5
/// indices), RDX/RCX/R8/R9 for 4 values, and the stack for the 5th.
///
/// Name indices must fit in 12 bits (≤ 4095); properties with larger
/// constant-pool indices are not fused.
pub const MAX_FUSED_OBJECT_PROPS: usize = 5;

/// Fuse `CreateObjectLiteral` + consecutive `StoreNamedGeneric` into a
/// single [`CreateObjectLiteralWithProperties`] node.
///
/// The pattern detected is:
///
/// ```text
/// %obj = CreateObjectLiteral { feedback_slot, flags, .. }
/// StoreNamedGeneric { object: %obj, name: n0, value: v0, .. }
/// StoreNamedGeneric { object: %obj, name: n1, value: v1, .. }
////// ```
///
/// The fused stores are removed and the `CreateObjectLiteral` is replaced
/// in-place with a `CreateObjectLiteralWithProperties` that carries the
/// property names and values.  The runtime stub creates the object and
/// fills all properties in a single call, eliminating per-store TLS
/// accesses and function-call overhead.
fn fuse_object_literal_stores(graph: &mut MaglevGraph) {
    for block in graph.blocks_mut() {
        fuse_object_literal_stores_in_block(block);
    }
}

/// Per-block implementation of object-literal store fusion.
fn fuse_object_literal_stores_in_block(block: &mut BasicBlock) {
    // Indices of StoreNamedGeneric nodes consumed by fusion (to remove).
    let mut remove_indices: Vec<usize> = Vec::new();
    // Replacements: (index, new_node).
    let mut replacements: Vec<(usize, ValueNode)> = Vec::new();

    let nodes = &block.nodes;
    let len = nodes.len();
    let mut i = 0;
    while i < len {
        let (create_id, create_node) = &nodes[i];

        let (feedback_slot, flags) = match create_node {
            ValueNode::CreateObjectLiteral {
                feedback_slot,
                flags,
                ..
            } => (*feedback_slot, *flags),
            // Extend fusion to CreateEmptyObjectLiteral.  Use u32::MAX as
            // a sentinel so the codegen emits feedback_slot = -1, which
            // tells the runtime stub to skip the template cache entirely.
            // Without this, every fused CreateEmptyObjectLiteral shares
            // feedback_slot 0, causing template-cache collisions when
            // multiple empty-object literals appear in the same function
            // (e.g. nested object initializers like `{ a: { b: ... } }`).
            ValueNode::CreateEmptyObjectLiteral => (u32::MAX, 0),
            _ => {
                i += 1;
                continue;
            }
        };

        // Scan consecutive StoreNamedGeneric nodes that target this object.
        let create_node_id = *create_id;
        let mut names: Vec<u32> = Vec::new();
        let mut values: Vec<NodeId> = Vec::new();
        let mut j = i + 1;

        // Track the actual indices of the StoreNamedGeneric nodes we consume,
        // since pure-value nodes (SmiConstant, etc.) may be interleaved.
        let mut store_indices: Vec<usize> = Vec::new();

        while j < len && names.len() < MAX_FUSED_OBJECT_PROPS {
            let node = &nodes[j].1;
            match node {
                ValueNode::StoreNamedGeneric {
                    object,
                    name,
                    value,
                    ..
                } if *object == create_node_id => {
                    // Name index must fit in the 12-bit packed encoding.
                    if *name > 0xFFF {
                        break;
                    }
                    names.push(*name);
                    values.push(*value);
                    store_indices.push(j);
                    j += 1;
                }
                // Skip any interleaved node that does NOT reference the
                // newly created object.  The previous code only skipped
                // constant nodes, which broke fusion when value
                // computations (Add, Mul, …) appeared between stores —
                // e.g. `{ x: i, y: i + 1, z: i * 2 }`.  Because the
                // object is freshly created and only reachable via the
                // stores being fused, any node that doesn't mention it
                // cannot observe or mutate it, so it is safe to skip.
                _ => {
                    let mut refs_object = false;
                    visit_value_node_inputs(node, &mut |id| {
                        if id == create_node_id {
                            refs_object = true;
                        }
                    });
                    if refs_object {
                        // Some non-store node uses the object — stop.
                        break;
                    }
                    j += 1;
                }
            }
        }

        if names.is_empty() {
            i += 1;
            continue;
        }

        // Record indices of consumed StoreNamedGeneric nodes for removal.
        remove_indices.extend_from_slice(&store_indices);

        // Replace CreateObjectLiteral with the fused node.
        replacements.push((
            i,
            ValueNode::CreateObjectLiteralWithProperties {
                feedback_slot,
                flags,
                names,
                values,
            },
        ));

        // Skip past the fused stores.
        i = j;
    }

    // Apply replacements (non-overlapping with removals).
    for (idx, new_node) in replacements {
        block.nodes[idx].1 = new_node;
    }
    // Remove consumed stores in reverse order to keep indices valid.
    remove_indices.sort_unstable();
    for &idx in remove_indices.iter().rev() {
        block.nodes.remove(idx);
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 2b′ — Forward invariant object-literal properties
// ─────────────────────────────────────────────────────────────────────────────

/// Forward property loads from object literals whose properties are never
/// modified after creation.  Unlike the general store-to-load pass, this
/// works across block boundaries (including loop headers) because it proves
/// that the properties are immutable throughout the function.
///
/// For each `CreateObjectLiteralWithProperties` that has NO `StoreNamedGeneric`
/// targeting it anywhere in the graph, every `LoadNamedGeneric` on the same
/// object and property name is replaced with the creation-time value.
fn forward_invariant_object_properties(graph: &mut MaglevGraph) {
    // Phase 1: Collect object literals and their property maps.
    let mut obj_props: HashMap<NodeId, HashMap<u32, NodeId>> = HashMap::new();
    // Track StoreGlobal(name, value) → value for global aliasing.
    let mut global_to_obj: HashMap<u32, NodeId> = HashMap::new();

    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            match node {
                ValueNode::CreateObjectLiteralWithProperties { names, values, .. } => {
                    let mut props = HashMap::new();
                    for (name, value) in names.iter().zip(values.iter()) {
                        props.insert(*name, *value);
                    }
                    obj_props.insert(*id, props);
                }
                // Track globals that point to object literals.
                ValueNode::StoreGlobal { name, value, .. } => {
                    if obj_props.contains_key(value) {
                        global_to_obj.insert(*name, *value);
                    }
                }
                _ => {}
            }
        }
    }

    if obj_props.is_empty() {
        return;
    }

    // Phase 2: Remove any object whose properties are modified (StoreNamedGeneric).
    for block in graph.blocks() {
        for (_, node) in &block.nodes {
            if let ValueNode::StoreNamedGeneric { object, .. } = node {
                obj_props.remove(object);
                for (&_gname, &obj_id) in &global_to_obj {
                    if *object == obj_id {
                        obj_props.remove(&obj_id);
                    }
                }
            }
        }
    }

    if obj_props.is_empty() {
        return;
    }

    // Build a map from LoadGlobal IDs to the object they alias.
    let mut load_global_alias: HashMap<NodeId, NodeId> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            if let ValueNode::LoadGlobal { name, .. } = node
                && let Some(&obj_id) = global_to_obj.get(name)
                && obj_props.contains_key(&obj_id)
            {
                load_global_alias.insert(*id, obj_id);
            }
        }
    }

    // Phase 3: Collect substitutions for LoadNamedGeneric on immutable objects.
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            if let ValueNode::LoadNamedGeneric { object, name, .. } = node {
                let mut resolved = load_global_alias.get(object).copied().unwrap_or(*object);
                // Chain through prior substitutions for nested object access
                // (e.g. root.a.b.c.d.e where each level is a
                // CreateObjectLiteralWithProperties).
                if !obj_props.contains_key(&resolved)
                    && let Some(&sub) = subst.get(&resolved)
                {
                    resolved = sub;
                }
                if let Some(props) = obj_props.get(&resolved)
                    && let Some(&value) = props.get(name)
                {
                    subst.insert(*id, value);
                }
            }
        }
    }

    if subst.is_empty() {
        return;
    }

    // Phase 4: Apply substitutions.
    for block in graph.blocks_mut() {
        for (id, node) in &mut block.nodes {
            if subst.contains_key(id) {
                *node = ValueNode::UndefinedConstant;
            }
        }
        apply_subst_to_block(block, &subst);
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 2b — Store-to-load forwarding
// ─────────────────────────────────────────────────────────────────────────────

/// Forward values from `StoreNamedGeneric` (and `CreateObjectLiteralWithProperties`)
/// to subsequent `LoadNamedGeneric` on the same object and property name,
/// eliminating redundant FFI round-trips.
///
/// Within a single basic block, we track the most recent value stored to each
/// `(object, name)` pair.  When a `LoadNamedGeneric` is encountered whose
/// `(object, name)` is in the map, the load is replaced with an
/// `UndefinedConstant` placeholder and all references to its result are
/// redirected to the stored value.
///
/// The map is conservatively invalidated on any side-effecting node that is
/// not itself a `StoreNamedGeneric` or `CreateObjectLiteralWithProperties`,
/// since calls, other stores, etc. may mutate arbitrary object properties.
fn eliminate_store_to_load(graph: &mut MaglevGraph) {
    let mut global_subst: HashMap<NodeId, NodeId> = HashMap::new();

    // Propagate store maps, global maps, and alias maps across blocks for
    // cross-block forwarding.  For single-predecessor blocks we inherit
    // the predecessor's final maps, enabling stores in the entry block to
    // be forwarded to loads in the preheader/loop body.  For merge points
    // (multiple predecessors) we conservatively start empty.
    let mut block_store_maps: HashMap<u32, HashMap<(NodeId, u32), NodeId>> = HashMap::new();
    let mut block_global_maps: HashMap<u32, HashMap<u32, NodeId>> = HashMap::new();
    let mut block_alias_maps: HashMap<u32, HashMap<NodeId, NodeId>> = HashMap::new();

    // Phase 1: compute substitutions (read-only access to blocks).
    for block in graph.blocks() {
        let single_pred = if block.predecessors.len() == 1 {
            Some(block.predecessors[0])
        } else {
            None
        };
        let mut store_map: HashMap<(NodeId, u32), NodeId> = single_pred
            .and_then(|p| block_store_maps.get(&p).cloned())
            .unwrap_or_default();
        let mut global_map: HashMap<u32, NodeId> = single_pred
            .and_then(|p| block_global_maps.get(&p).cloned())
            .unwrap_or_default();
        let mut alias_map: HashMap<NodeId, NodeId> = single_pred
            .and_then(|p| block_alias_maps.get(&p).cloned())
            .unwrap_or_default();

        let subst = compute_store_to_load_subst_with_map(
            block,
            &mut store_map,
            &mut global_map,
            &mut alias_map,
        );
        block_store_maps.insert(block.id, store_map);
        block_global_maps.insert(block.id, global_map);
        block_alias_maps.insert(block.id, alias_map);
        global_subst.extend(subst);
    }

    if global_subst.is_empty() {
        return;
    }

    // Phase 2: replace forwarded loads with dead placeholders and apply
    // substitutions to ALL blocks so cross-block references are updated.
    for block in graph.blocks_mut() {
        for (id, node) in &mut block.nodes {
            if global_subst.contains_key(id) {
                *node = ValueNode::UndefinedConstant;
            }
        }
        apply_subst_to_block(block, &global_subst);
    }
}

/// Compute store-to-load forwarding substitutions for a block, using the
/// supplied `store_map` (which may be pre-seeded with entries from dominating
/// blocks).  The map is updated in place so callers can propagate it to
/// successor blocks.
fn compute_store_to_load_subst_with_map(
    block: &BasicBlock,
    store_map: &mut HashMap<(NodeId, u32), NodeId>,
    global_map: &mut HashMap<u32, NodeId>,
    alias_map: &mut HashMap<NodeId, NodeId>,
) -> HashMap<NodeId, NodeId> {
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();

    for (id, node) in &block.nodes {
        match node {
            // Seed the map from fused object creation.
            ValueNode::CreateObjectLiteralWithProperties { names, values, .. } => {
                let obj_id = *id;
                for (name, value) in names.iter().zip(values.iter()) {
                    store_map.insert((obj_id, *name), *value);
                }
            }

            // Track global stores for global forwarding.
            ValueNode::StoreGlobal { name, value, .. } => {
                global_map.insert(*name, *value);
            }

            // Forward global loads.
            ValueNode::LoadGlobal { name, .. } => {
                if let Some(&stored_value) = global_map.get(name) {
                    subst.insert(*id, stored_value);
                    alias_map.insert(*id, stored_value);
                }
            }

            // Track named property stores.
            ValueNode::StoreNamedGeneric {
                object,
                name,
                value,
                ..
            } => {
                // Resolve aliases: if object was a forwarded LoadGlobal,
                // use the original value for the store_map key.
                let resolved_obj = alias_map.get(object).copied().unwrap_or(*object);
                store_map.insert((resolved_obj, *name), *value);
            }

            // Forward named property loads.
            ValueNode::LoadNamedGeneric { object, name, .. } => {
                // Resolve aliases: if object was a forwarded LoadGlobal or
                // a prior forwarded LoadNamedGeneric, look up properties
                // on the original object.
                let resolved_obj = alias_map.get(object).copied().unwrap_or(*object);
                if let Some(&stored_value) = store_map.get(&(resolved_obj, *name)) {
                    subst.insert(*id, stored_value);
                    // Record the alias so chained property accesses can
                    // resolve through this load.  E.g. for `root.a.b`,
                    // after forwarding `root.a` → inner_obj, the load of
                    // `.b` needs to resolve `root.a` → inner_obj so it
                    // can look up (inner_obj, "b") in the store_map.
                    alias_map.insert(*id, stored_value);
                }
            }

            // Nodes that can mutate named properties invalidate the store map.
            // Nodes with any side effect invalidate the global map (globals
            // can change through deopts or calls).
            other => {
                if can_invalidate_named_stores(other) {
                    store_map.clear();
                    global_map.clear();
                } else if has_side_effects(other) {
                    global_map.clear();
                }
                // Keep alias_map — aliases are structural, not invalidated
                // by side effects.
            }
        }
    }

    subst
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 2c.0 — Loop dead-object forwarding
// ─────────────────────────────────────────────────────────────────────────────

/// Forward property loads on objects created inside counted loops to their
/// final-iteration constant values, enabling subsequent dead-allocation
/// elimination.
///
/// Pattern: a counted loop creates an object literal every iteration and
/// stores it to a global that is never read inside the loop.  After the loop,
/// the global is loaded and only its properties are accessed.  Because only
/// the last iteration's object matters, we can compute the final property
/// values at compile time (for affine expressions of the IV) and replace the
/// post-loop `LoadNamedGeneric` nodes with constants.  This makes the in-loop
/// `CreateObjectLiteralWithProperties` + `StoreGlobal` dead, which existing
/// passes then eliminate.
fn forward_loop_object_properties(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    if loops.is_empty() {
        return;
    }

    // Build a node → constant map.
    let mut consts: HashMap<NodeId, i32> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            match node {
                ValueNode::SmiConstant { value } | ValueNode::Int32Constant { value } => {
                    consts.insert(*id, *value);
                }
                _ => {}
            }
        }
    }

    for lp in &loops {
        try_forward_loop_object(graph, lp, &consts);
    }
}

/// Try to forward object property loads for a single counted loop.
///
/// After `promote_loop_globals_counted`, a write-only global like `last` in
/// `for (...) { last = { x: i, ... }; }` becomes a header Phi whose back-edge
/// input is a `CreateObjectLiteralWithProperties`.  Post-loop property loads
/// appear as `LoadNamedGeneric(obj_phi, "x")`.
///
/// This pass detects that pattern, evaluates each property value at the final
/// IV value (limit - step), and replaces the post-loop property loads with
/// SmiConstants.  If the object Phi has no remaining uses after replacement,
/// the in-loop allocation is killed.
fn try_forward_loop_object(
    graph: &mut MaglevGraph,
    lp: &licm::NaturalLoop,
    consts: &HashMap<NodeId, i32>,
) -> bool {
    // ── 1. Verify counted loop structure ─────────────────────────────────
    let header = match graph.block(lp.header) {
        Some(b) => b,
        None => return false,
    };

    let (condition_id, body_block_idx, exit_block_idx) = match &header.control {
        Some(ControlNode::Branch {
            condition,
            if_true,
            if_false,
        }) => (*condition, *if_true, *if_false),
        _ => return false,
    };

    if !lp.body.contains(&body_block_idx) {
        return false;
    }
    if lp.body.contains(&exit_block_idx) {
        return false;
    }
    // Simple loop: header + 1 body block.
    if lp.body.len() != 2 || !lp.body.contains(&lp.header) {
        return false;
    }

    let body = match graph.block(body_block_idx) {
        Some(b) => b,
        None => return false,
    };
    match &body.control {
        Some(ControlNode::Jump { target }) if *target == lp.header => {}
        _ => return false,
    }

    // ── 2. Find IV Phi and compute trip count ────────────────────────────
    let (cmp_left, cmp_right) = {
        let mut found = None;
        for (nid, node) in &header.nodes {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match &found {
            Some(ValueNode::Int32LessThan { left, right }) => (*left, *right),
            _ => return false,
        }
    };

    let iv_phi_id = cmp_left;
    let header = graph.block(lp.header).unwrap();
    let iv_phi_inputs = {
        let mut found = None;
        for (nid, node) in &header.nodes {
            if *nid == iv_phi_id {
                if let ValueNode::Phi { inputs } = node {
                    found = Some(inputs.clone());
                }
                break;
            }
        }
        match found {
            Some(inputs) if inputs.len() == 2 => inputs,
            _ => return false,
        }
    };

    let back_pred_pos = match header
        .predecessors
        .iter()
        .position(|&p| p == body_block_idx)
    {
        Some(pos) => pos,
        None => return false,
    };
    let entry_pos = match header.predecessors.iter().position(|&p| p == lp.preheader) {
        Some(pos) => pos,
        None => return false,
    };

    let iv_init = match consts.get(&iv_phi_inputs[entry_pos]) {
        Some(&v) => v,
        None => match find_i32_constant(graph, iv_phi_inputs[entry_pos]) {
            Some(v) => v,
            None => return false,
        },
    };
    let iv_limit = match consts.get(&cmp_right) {
        Some(&v) => v,
        None => match find_i32_constant(graph, cmp_right) {
            Some(v) => v,
            None => return false,
        },
    };

    let iv_back_id = iv_phi_inputs[back_pred_pos];
    let iv_step = match find_increment_step(graph, iv_back_id, iv_phi_id) {
        Some(s) if s > 0 => s,
        _ => return false,
    };

    let range = (iv_limit as i64) - (iv_init as i64);
    let step = iv_step as i64;
    if range <= 0 || range % step != 0 {
        return false;
    }
    if range / step == 0 {
        return false;
    }

    let iv_final = iv_limit - iv_step;

    // ── 3. Find "object Phi" in header whose back-edge is CreateObjWithProps
    let header = graph.block(lp.header).unwrap();
    let mut obj_phi_id: Option<NodeId> = None;
    let mut create_obj_id: Option<NodeId> = None;
    let mut create_names: Vec<u32> = Vec::new();
    let mut create_values: Vec<NodeId> = Vec::new();

    for (nid, node) in &header.nodes {
        if *nid == iv_phi_id {
            continue;
        }
        if let ValueNode::Phi { inputs } = node
            && inputs.len() == 2
        {
            let back_edge_id = inputs[back_pred_pos];
            if let Some(ValueNode::CreateObjectLiteralWithProperties { names, values, .. }) =
                graph.node(back_edge_id)
            {
                obj_phi_id = Some(*nid);
                create_obj_id = Some(back_edge_id);
                create_names = names.clone();
                create_values = values.clone();
                break;
            }
        }
    }

    let obj_phi_id = match obj_phi_id {
        Some(id) => id,
        None => return false,
    };
    let create_obj_id = create_obj_id.unwrap();

    // ── 4. Evaluate property values at IV = iv_final ─────────────────────
    let mut final_values: Vec<Option<i32>> = Vec::new();
    for &val_id in &create_values {
        final_values.push(eval_at_iv(graph, val_id, iv_phi_id, iv_final, consts));
    }

    if final_values.iter().any(|v| v.is_none()) {
        return false;
    }

    // ── 5. Find post-loop LoadNamedGeneric that loads from obj_phi ────────
    // Build property name → final constant value map.
    let mut prop_finals: HashMap<u32, i32> = HashMap::new();
    for (name, val) in create_names.iter().zip(final_values.iter()) {
        prop_finals.insert(*name, val.unwrap());
    }

    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();
    let mut new_constants: Vec<(NodeId, i32)> = Vec::new();

    // Collect replacements: LoadNamedGeneric(obj_phi_id, prop_name) → const.
    let mut pending: Vec<(NodeId, i32)> = Vec::new();
    for block in graph.blocks() {
        if lp.body.contains(&block.id) {
            continue;
        }
        for (nid, node) in &block.nodes {
            if let ValueNode::LoadNamedGeneric { object, name, .. } = node
                && *object == obj_phi_id
                && let Some(&final_val) = prop_finals.get(name)
            {
                pending.push((*nid, final_val));
            }
        }
    }

    if pending.is_empty() {
        return false;
    }

    for (load_nid, final_val) in &pending {
        let const_id = graph.alloc_node_id();
        new_constants.push((const_id, *final_val));
        subst.insert(*load_nid, const_id);
    }

    // ── 6. Safety: check that obj_phi has no uses outside the replaced loads
    //      and exit StoreGlobal nodes.  Exit StoreGlobals that store the
    //      obj_phi can be killed (their only purpose was materialising the
    //      object for post-loop reads, which are now constants).
    let replaced_set: HashSet<NodeId> = subst.keys().copied().collect();
    let mut exit_store_globals_to_kill: Vec<NodeId> = Vec::new();
    let mut truly_escapes = false;
    for block in graph.blocks() {
        for (nid, node) in &block.nodes {
            if replaced_set.contains(nid) || *nid == obj_phi_id {
                continue;
            }
            let refs = node_operands(node);
            if refs.contains(&obj_phi_id) {
                // Allow StoreGlobal in exit blocks — these just materialise
                // the object for post-loop reads that are already forwarded.
                if !lp.body.contains(&block.id) && matches!(node, ValueNode::StoreGlobal { .. }) {
                    exit_store_globals_to_kill.push(*nid);
                    continue;
                }
                truly_escapes = true;
                break;
            }
        }
        if truly_escapes {
            break;
        }
        if let Some(ctrl) = &block.control {
            let ctrl_refs = control_operands(ctrl);
            if ctrl_refs.contains(&obj_phi_id) {
                truly_escapes = true;
                break;
            }
        }
    }
    if truly_escapes {
        return apply_load_replacements_only(graph, lp, exit_block_idx, &new_constants, &subst);
    }

    // ── 7. Apply: insert constants, replace loads, kill body allocs ──────
    // Insert SmiConstants at the start of the exit block.
    if let Some(exit_block) = graph.block_mut(exit_block_idx) {
        for (idx, (const_id, val)) in new_constants.iter().enumerate() {
            exit_block
                .nodes
                .insert(idx, (*const_id, ValueNode::SmiConstant { value: *val }));
        }
    }

    // Replace LoadNamedGeneric nodes with UndefinedConstant.
    for block in graph.blocks_mut() {
        if lp.body.contains(&block.id) {
            continue;
        }
        for (nid, node) in &mut block.nodes {
            if subst.contains_key(nid) {
                *node = ValueNode::UndefinedConstant;
            }
            // Kill exit StoreGlobal nodes that stored the now-dead obj_phi.
            if exit_store_globals_to_kill.contains(nid) {
                *node = ValueNode::UndefinedConstant;
            }
        }
        apply_subst_to_block(block, &subst);
    }

    // Kill CreateObjectLiteralWithProperties in the body (obj_phi has no
    // remaining uses, so the allocation is dead).
    for block in graph.blocks_mut() {
        if !lp.body.contains(&block.id) || block.id == lp.header {
            continue;
        }
        for (nid, node) in &mut block.nodes {
            if *nid == create_obj_id {
                *node = ValueNode::UndefinedConstant;
            }
        }
    }

    // Update the obj_phi's back-edge input to UndefinedConstant sentinel.
    // The Phi itself is now dead (no uses) and will be cleaned by DCE.
    if let Some(hdr) = graph.block_mut(lp.header) {
        for (nid, node) in &mut hdr.nodes {
            if *nid == obj_phi_id
                && let ValueNode::Phi { inputs } = node
            {
                inputs[back_pred_pos] = inputs[entry_pos];
            }
        }
    }

    true
}

/// Apply only the load→constant replacements without killing the allocation.
/// Used when the object Phi escapes (has uses beyond the forwarded loads).
fn apply_load_replacements_only(
    graph: &mut MaglevGraph,
    lp: &licm::NaturalLoop,
    exit_block_idx: u32,
    new_constants: &[(NodeId, i32)],
    subst: &HashMap<NodeId, NodeId>,
) -> bool {
    if let Some(exit_block) = graph.block_mut(exit_block_idx) {
        for (idx, (const_id, val)) in new_constants.iter().enumerate() {
            exit_block
                .nodes
                .insert(idx, (*const_id, ValueNode::SmiConstant { value: *val }));
        }
    }
    for block in graph.blocks_mut() {
        if lp.body.contains(&block.id) {
            continue;
        }
        for (nid, node) in &mut block.nodes {
            if subst.contains_key(nid) {
                *node = ValueNode::UndefinedConstant;
            }
        }
        apply_subst_to_block(block, subst);
    }
    true
}

/// Collect all NodeId operands of a value node.
fn node_operands(node: &ValueNode) -> Vec<NodeId> {
    match node {
        ValueNode::Phi { inputs } => inputs.clone(),
        ValueNode::Int32Add { left, right }
        | ValueNode::Int32Subtract { left, right }
        | ValueNode::Int32Multiply { left, right }
        | ValueNode::Int32LessThan { left, right }
        | ValueNode::Int32BitwiseOr { left, right }
        | ValueNode::Int32BitwiseAnd { left, right }
        | ValueNode::Int32ShiftLeft { left, right }
        | ValueNode::Int32ShiftRight { left, right }
        | ValueNode::CheckedSmiAdd { left, right }
        | ValueNode::CheckedSmiSubtract { left, right }
        | ValueNode::CheckedSmiMultiply { left, right }
        | ValueNode::GenericAdd { left, right, .. }
        | ValueNode::GenericSubtract { left, right, .. }
        | ValueNode::GenericMultiply { left, right, .. } => vec![*left, *right],
        ValueNode::LoadNamedGeneric { object, .. } => vec![*object],
        ValueNode::StoreNamedGeneric { object, value, .. } => vec![*object, *value],
        ValueNode::StoreGlobal { value, .. } => vec![*value],
        ValueNode::Call {
            callee,
            receiver,
            args,
            ..
        } => {
            let mut v = vec![*callee, *receiver];
            v.extend(args);
            v
        }
        ValueNode::CallKnownFunction {
            callee,
            receiver,
            args,
            ..
        } => {
            let mut v = vec![*callee, *receiver];
            v.extend(args);
            v
        }
        ValueNode::CreateObjectLiteralWithProperties { values, .. } => values.clone(),
        ValueNode::CheckMaps { receiver, .. } => vec![*receiver],
        _ => Vec::new(),
    }
}

/// Collect all NodeId operands of a control node.
fn control_operands(ctrl: &ControlNode) -> Vec<NodeId> {
    match ctrl {
        ControlNode::Branch { condition, .. } => vec![*condition],
        ControlNode::Return { value } => vec![*value],
        _ => Vec::new(),
    }
}

/// Evaluate a pure expression tree at a specific IV value.
///
/// Returns `Some(value)` if the expression is a constant or a pure affine
/// function of the given IV Phi.  Returns `None` for anything else (calls,
/// loads, side-effecting nodes, etc.).
fn eval_at_iv(
    graph: &MaglevGraph,
    node_id: NodeId,
    iv_phi: NodeId,
    iv_value: i32,
    consts: &HashMap<NodeId, i32>,
) -> Option<i32> {
    if node_id == iv_phi {
        return Some(iv_value);
    }

    // Check constant map first.
    if let Some(&v) = consts.get(&node_id) {
        return Some(v);
    }

    let node = graph.node(node_id)?;
    match node {
        ValueNode::SmiConstant { value } | ValueNode::Int32Constant { value } => Some(*value),

        // Pure binary arithmetic (Int32)
        ValueNode::Int32Add { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_add(r)
        }
        ValueNode::Int32Subtract { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_sub(r)
        }
        ValueNode::Int32Multiply { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_mul(r)
        }

        // Checked Smi variants (same semantics for constant eval)
        ValueNode::CheckedSmiAdd { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_add(r)
        }
        ValueNode::CheckedSmiSubtract { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_sub(r)
        }
        ValueNode::CheckedSmiMultiply { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_mul(r)
        }

        // Generic variants with feedback (same constant eval)
        ValueNode::GenericAdd { left, right, .. } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_add(r)
        }
        ValueNode::GenericSubtract { left, right, .. } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_sub(r)
        }
        ValueNode::GenericMultiply { left, right, .. } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            l.checked_mul(r)
        }

        // Int32 bitwise OR (used in `| 0` truncation pattern)
        ValueNode::Int32BitwiseOr { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            Some(l | r)
        }
        ValueNode::Int32ShiftLeft { left, right } => {
            let l = eval_at_iv(graph, *left, iv_phi, iv_value, consts)?;
            let r = eval_at_iv(graph, *right, iv_phi, iv_value, consts)?;
            Some(l.wrapping_shl(r as u32))
        }

        _ => None,
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 2c — Dead allocation elimination (escape analysis lite)
// ─────────────────────────────────────────────────────────────────────────────

/// Eliminate stores to non-escaping objects.
///
/// After store-to-load forwarding eliminates property loads, an allocation may
/// only be referenced by `StoreNamedGeneric` nodes.  These stores are dead
/// because the object never escapes.  This pass replaces such stores with
/// `UndefinedConstant` placeholders so the subsequent dead-allocation pass can
/// remove the allocation itself.
fn eliminate_dead_object_stores(graph: &mut MaglevGraph) {
    // Step 1: Find all allocation NodeIds.
    let mut alloc_ids: HashSet<NodeId> = HashSet::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            if matches!(
                node,
                ValueNode::CreateObjectLiteral { .. }
                    | ValueNode::CreateObjectLiteralWithProperties { .. }
                    | ValueNode::CreateEmptyObjectLiteral
                    | ValueNode::CreateArrayLiteral { .. }
                    | ValueNode::CreateEmptyArrayLiteral { .. }
                    | ValueNode::CreateShallowObjectLiteral { .. }
                    | ValueNode::CreateShallowArrayLiteral { .. }
            ) {
                alloc_ids.insert(*id);
            }
        }
    }
    if alloc_ids.is_empty() {
        return;
    }

    // Step 2: For each allocation, collect which nodes reference it and
    // determine if it "escapes" (referenced by anything other than
    // StoreNamedGeneric targeting it as the object).
    let mut escapes: HashSet<NodeId> = HashSet::new();
    let mut stores_per_alloc: HashMap<NodeId, Vec<(u32, usize)>> = HashMap::new();

    for block in graph.blocks() {
        for (idx, (_id, node)) in block.nodes.iter().enumerate() {
            match node {
                ValueNode::StoreNamedGeneric { object, .. } if alloc_ids.contains(object) => {
                    stores_per_alloc
                        .entry(*object)
                        .or_default()
                        .push((block.id, idx));
                    // The store's value operand doesn't count as an escape
                    // of the allocation (it's the value being stored, not
                    // the object).  But check if *id* itself is an alloc
                    // used elsewhere — handled by other branches.
                }
                _ => {
                    // Check if this node references any allocation in a
                    // non-store capacity (load, call arg, return, etc.).
                    visit_value_node_inputs(node, &mut |input| {
                        if alloc_ids.contains(&input) {
                            // If this node is a StoreNamedGeneric targeting
                            // this alloc, it was handled above.  Otherwise
                            // the alloc escapes.
                            escapes.insert(input);
                        }
                    });
                    // If the node IS a StoreNamedGeneric but its object is
                    // not an allocation, inputs were already visited above.
                }
            }
        }
        // Also check control nodes for escaping allocations.
        if let Some(ctrl) = &block.control {
            collect_control_node_inputs(ctrl, &mut escapes);
        }
    }

    // Step 3: For non-escaping allocations, replace their stores with
    // UndefinedConstant.
    for (alloc_id, store_locs) in &stores_per_alloc {
        if escapes.contains(alloc_id) {
            continue;
        }
        // All references to this allocation are StoreNamedGeneric — safe to
        // eliminate both the stores and (later) the allocation.
        for &(block_id, node_idx) in store_locs {
            if let Some(block) = graph.blocks_mut().iter_mut().find(|b| b.id == block_id) {
                block.nodes[node_idx].1 = ValueNode::UndefinedConstant;
            }
        }
    }
}

/// Eliminate object/array literal allocations whose results are never used.
///
/// After store-to-load forwarding, property loads from short-lived objects are
/// replaced with the stored values directly.  If the allocation's [`NodeId`]
/// is no longer referenced by any live node, the object never "escapes" and
/// the allocation is dead.  This pass replaces such allocations with cheap
/// [`ValueNode::UndefinedConstant`] placeholders (in-place, to preserve graph
/// structure for the register allocator).
fn eliminate_dead_allocations(graph: &mut MaglevGraph) {
    let live = collect_live_ids(graph);

    for block in graph.blocks_mut() {
        for (id, node) in &mut block.nodes {
            let is_dead_alloc = matches!(
                node,
                ValueNode::CreateObjectLiteral { .. }
                    | ValueNode::CreateObjectLiteralWithProperties { .. }
                    | ValueNode::CreateEmptyObjectLiteral
                    | ValueNode::CreateArrayLiteral { .. }
                    | ValueNode::CreateEmptyArrayLiteral { .. }
                    | ValueNode::CreateShallowObjectLiteral { .. }
                    | ValueNode::CreateShallowArrayLiteral { .. }
            ) && !live.contains(id);

            if is_dead_alloc {
                *node = ValueNode::UndefinedConstant;
            }
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 3 — Dead-code elimination
// ─────────────────────────────────────────────────────────────────────────────

/// Replace dead `CreateMapped/UnmappedArguments` and `CreateRestParameter`
/// nodes with cheap [`ValueNode::UndefinedConstant`] placeholders.
///
/// Unlike full DCE (which **removes** nodes), this pass **replaces** the node
/// in-place, preserving graph structure (node count, positions, register
/// allocation).  This avoids a known issue where node removal changes
/// interference patterns in the register allocator, triggering latent bugs
/// on Linux in release mode.
fn replace_dead_arguments(graph: &mut MaglevGraph) {
    let live = collect_live_ids(graph);

    for block in graph.blocks_mut() {
        for (id, node) in &mut block.nodes {
            if matches!(
                node,
                ValueNode::CreateMappedArguments
                    | ValueNode::CreateUnmappedArguments
                    | ValueNode::CreateRestParameter
            ) && !live.contains(id)
            {
                *node = ValueNode::UndefinedConstant;
            }
        }
    }
}

/// Remove `ValueNode`s that are never used and have no observable side-effects.
///
/// Algorithm:
/// Repeats the local cleanup until stable so dead pure expression chains are
/// removed transitively.  A single pass removes only the final unused node in a
/// chain, leaving stale intermediate uses that can block later loop analysis.
fn eliminate_dead_code(graph: &mut MaglevGraph) {
    loop {
        let live = collect_live_ids(graph);
        let mut removed = 0usize;

        for block in graph.blocks_mut() {
            let before = block.nodes.len();
            block.nodes.retain(|(id, node)| {
                // Always keep nodes with side-effects.
                if has_side_effects(node) {
                    return true;
                }
                // Keep pure nodes only if their result is consumed somewhere.
                live.contains(id)
            });
            removed += before - block.nodes.len();
        }

        if removed == 0 {
            break;
        }
    }
}

/// Collect every [`NodeId`] that appears as an *input* in any node or control
/// node across all blocks.
fn collect_live_ids(graph: &MaglevGraph) -> HashSet<NodeId> {
    let mut live = HashSet::new();

    for block in graph.blocks() {
        for (_id, node) in &block.nodes {
            collect_value_node_inputs(node, &mut live);
        }
        if let Some(ctrl) = &block.control {
            collect_control_node_inputs(ctrl, &mut live);
        }
    }

    live
}

/// Enumerate all [`NodeId`] operands referenced by a [`ValueNode`].
#[allow(clippy::too_many_lines)]
fn collect_value_node_inputs(node: &ValueNode, live: &mut HashSet<NodeId>) {
    match node {
        // ── Nodes with no inputs ─────────────────────────────────────────────
        ValueNode::SmiConstant { .. }
        | ValueNode::Float64Constant { .. }
        | ValueNode::Int32Constant { .. }
        | ValueNode::Uint32Constant { .. }
        | ValueNode::BigIntConstant { .. }
        | ValueNode::TrueConstant
        | ValueNode::FalseConstant
        | ValueNode::NullConstant
        | ValueNode::UndefinedConstant
        | ValueNode::RootConstant { .. }
        | ValueNode::ExternalConstant { .. }
        | ValueNode::StringConstant { .. }
        | ValueNode::ConstantPoolEntry { .. }
        | ValueNode::Parameter { .. }
        | ValueNode::RegisterInput { .. }
        | ValueNode::ArgumentsLength
        | ValueNode::RestLength
        | ValueNode::LoadGlobal { .. }
        | ValueNode::LoadCurrentContextSlot { .. }
        | ValueNode::ArgumentsElements { .. }
        | ValueNode::RestElements { .. }
        | ValueNode::VirtualObject { .. }
        | ValueNode::CreateFunctionContext { .. }
        | ValueNode::CreateBlockContext { .. }
        | ValueNode::CreateShallowObjectLiteral { .. }
        | ValueNode::CreateShallowArrayLiteral { .. }
        | ValueNode::CreateObjectLiteral { .. }
        | ValueNode::CreateArrayLiteral { .. }
        | ValueNode::CreateEmptyObjectLiteral
        | ValueNode::CreateEmptyArrayLiteral { .. }
        | ValueNode::CreateMappedArguments
        | ValueNode::CreateUnmappedArguments
        | ValueNode::CreateRestParameter
        | ValueNode::CreateRegExpLiteral { .. }
        | ValueNode::CreateClosure { .. }
        | ValueNode::FastCreateClosure { .. }
        | ValueNode::GetTemplateObject { .. }
        | ValueNode::Debugger
        | ValueNode::Abort { .. } => {}

        // ── Single-input nodes ────────────────────────────────────────────────
        ValueNode::GetArgument { index } => {
            live.insert(*index);
        }
        ValueNode::CheckedSmiIncrement { value }
        | ValueNode::CheckedSmiDecrement { value }
        | ValueNode::Int32Negate { value }
        | ValueNode::Int32Increment { value }
        | ValueNode::Int32Decrement { value }
        | ValueNode::Float64Negate { value }
        | ValueNode::Float64Ieee754Unary { value, .. }
        | ValueNode::GenericBitwiseNot { value, .. }
        | ValueNode::GenericNegate { value, .. }
        | ValueNode::GenericIncrement { value, .. }
        | ValueNode::GenericDecrement { value, .. }
        | ValueNode::ChangeInt32ToFloat64 { input: value }
        | ValueNode::ChangeUint32ToFloat64 { input: value }
        | ValueNode::ChangeFloat64ToInt32 { input: value }
        | ValueNode::CheckedFloat64ToInt32 { input: value }
        | ValueNode::ChangeInt32ToTagged { input: value }
        | ValueNode::ChangeUint32ToTagged { input: value }
        | ValueNode::ChangeFloat64ToTagged { input: value }
        | ValueNode::ChangeTaggedToInt32 { input: value }
        | ValueNode::ChangeTaggedToUint32 { input: value }
        | ValueNode::ChangeTaggedToFloat64 { input: value }
        | ValueNode::CheckedTaggedToInt32 { input: value }
        | ValueNode::CheckedTaggedToFloat64 { input: value }
        | ValueNode::ToBoolean { value }
        | ValueNode::TestNullOrUndefined { value }
        | ValueNode::ToString { value, .. }
        | ValueNode::ToObject { value, .. }
        | ValueNode::ToName { value, .. }
        | ValueNode::ToNumber { value, .. }
        | ValueNode::ToNumberOrNumeric { value, .. }
        | ValueNode::TypeOf { value }
        | ValueNode::NumberToString { value, .. } => {
            live.insert(*value);
        }

        ValueNode::CheckSmi { receiver }
        | ValueNode::CheckNumber { receiver }
        | ValueNode::CheckHeapObject { receiver }
        | ValueNode::CheckSymbol { receiver }
        | ValueNode::CheckString { receiver }
        | ValueNode::CheckStringOrStringWrapper { receiver }
        | ValueNode::CheckSeqOneByteString { receiver }
        | ValueNode::CheckMaps { receiver, .. }
        | ValueNode::CheckMapsWithMigration { receiver, .. }
        | ValueNode::CheckValue { receiver, .. } => {
            live.insert(*receiver);
        }

        ValueNode::CheckDynamicValue { receiver, expected } => {
            live.insert(*receiver);
            live.insert(*expected);
        }

        ValueNode::CheckInt32IsSmi { input }
        | ValueNode::CheckUint32IsSmi { input }
        | ValueNode::CheckHoleyFloat64IsSmi { input }
        | ValueNode::CheckFloat64IsNan { input } => {
            live.insert(*input);
        }

        ValueNode::LoadField { object, .. }
        | ValueNode::LoadTaggedField { object, .. }
        | ValueNode::LoadDoubleField { object, .. }
        | ValueNode::LoadNamedGeneric { object, .. }
        | ValueNode::ForInPrepare {
            enumerator: object, ..
        }
        | ValueNode::StringLength { string: object } => {
            live.insert(*object);
        }

        ValueNode::LoadEnumCacheLength { map } => {
            live.insert(*map);
        }

        ValueNode::LoadKeyedGeneric { object, key, .. } => {
            live.insert(*object);
            live.insert(*key);
        }

        ValueNode::HasInPrototypeChain { object, prototype } => {
            live.insert(*object);
            live.insert(*prototype);
        }

        ValueNode::StoreField { object, value, .. } => {
            live.insert(*object);
            live.insert(*value);
        }

        ValueNode::StoreCurrentContextSlot { value, .. } | ValueNode::StoreGlobal { value, .. } => {
            live.insert(*value);
        }

        ValueNode::LoadContextSlot { context, .. } => {
            live.insert(*context);
        }
        ValueNode::StoreContextSlot { context, value, .. } => {
            live.insert(*context);
            live.insert(*value);
        }

        ValueNode::PushContext { context } | ValueNode::PopContext { context } => {
            live.insert(*context);
        }

        ValueNode::LoadFixedArrayElement { elements, index }
        | ValueNode::LoadFixedDoubleArrayElement { elements, index }
        | ValueNode::LoadHoleyFixedDoubleArrayElement { elements, index } => {
            live.insert(*elements);
            live.insert(*index);
        }

        ValueNode::StoreFixedArrayElement {
            elements,
            index,
            value,
        }
        | ValueNode::StoreFixedDoubleArrayElement {
            elements,
            index,
            value,
        } => {
            live.insert(*elements);
            live.insert(*index);
            live.insert(*value);
        }

        ValueNode::StoreNamedGeneric { object, value, .. } => {
            live.insert(*object);
            live.insert(*value);
        }

        ValueNode::CreateObjectLiteralWithProperties { values, .. } => {
            for &v in values {
                live.insert(v);
            }
        }

        ValueNode::StoreKeyedGeneric {
            object, key, value, ..
        } => {
            live.insert(*object);
            live.insert(*key);
            live.insert(*value);
        }

        // ── Binary nodes ──────────────────────────────────────────────────────
        ValueNode::CheckedSmiAdd { left, right }
        | ValueNode::CheckedSmiSubtract { left, right }
        | ValueNode::CheckedSmiMultiply { left, right }
        | ValueNode::CheckedSmiDivide { left, right }
        | ValueNode::CheckedSmiModulus { left, right }
        | ValueNode::Int32Add { left, right }
        | ValueNode::Int32Subtract { left, right }
        | ValueNode::Int32Multiply { left, right }
        | ValueNode::Int32Divide { left, right }
        | ValueNode::Int32Modulus { left, right }
        | ValueNode::Int32BitwiseAnd { left, right }
        | ValueNode::Int32BitwiseOr { left, right }
        | ValueNode::Int32BitwiseXor { left, right }
        | ValueNode::Int32ShiftLeft { left, right }
        | ValueNode::Int32ShiftRight { left, right }
        | ValueNode::Int32ShiftRightLogical { left, right }
        | ValueNode::Uint32Add { left, right }
        | ValueNode::Uint32Subtract { left, right }
        | ValueNode::Uint32Multiply { left, right }
        | ValueNode::Uint32Divide { left, right }
        | ValueNode::Uint32Modulus { left, right }
        | ValueNode::Float64Add { left, right }
        | ValueNode::Float64Subtract { left, right }
        | ValueNode::Float64Multiply { left, right }
        | ValueNode::Float64Divide { left, right }
        | ValueNode::Float64Modulus { left, right }
        | ValueNode::Float64Exponentiate { left, right }
        | ValueNode::Int32Equal { left, right }
        | ValueNode::Int32StrictEqual { left, right }
        | ValueNode::Int32LessThan { left, right }
        | ValueNode::Int32LessThanOrEqual { left, right }
        | ValueNode::Int32GreaterThan { left, right }
        | ValueNode::Int32GreaterThanOrEqual { left, right }
        | ValueNode::Float64Equal { left, right }
        | ValueNode::Float64LessThan { left, right }
        | ValueNode::Float64LessThanOrEqual { left, right }
        | ValueNode::Float64GreaterThan { left, right }
        | ValueNode::Float64GreaterThanOrEqual { left, right }
        | ValueNode::StringConcat { left, right }
        | ValueNode::StringEqual { left, right }
        | ValueNode::GenericAdd { left, right, .. }
        | ValueNode::GenericSubtract { left, right, .. }
        | ValueNode::GenericMultiply { left, right, .. }
        | ValueNode::GenericDivide { left, right, .. }
        | ValueNode::GenericModulus { left, right, .. }
        | ValueNode::GenericExponentiate { left, right, .. }
        | ValueNode::GenericBitwiseAnd { left, right, .. }
        | ValueNode::GenericBitwiseOr { left, right, .. }
        | ValueNode::GenericBitwiseXor { left, right, .. }
        | ValueNode::GenericShiftLeft { left, right, .. }
        | ValueNode::GenericShiftRight { left, right, .. }
        | ValueNode::GenericShiftRightLogical { left, right, .. }
        | ValueNode::TaggedEqual { left, right, .. }
        | ValueNode::TaggedNotEqual { left, right, .. } => {
            live.insert(*left);
            live.insert(*right);
        }

        ValueNode::CheckInt32Condition { left, right, .. } => {
            live.insert(*left);
            live.insert(*right);
        }

        ValueNode::CheckCacheIndicesNotCleared { receiver, indices } => {
            live.insert(*receiver);
            live.insert(*indices);
        }

        ValueNode::TestInstanceOf {
            object, callable, ..
        } => {
            live.insert(*object);
            live.insert(*callable);
        }
        ValueNode::TestIn { key, object, .. } => {
            live.insert(*key);
            live.insert(*object);
        }
        ValueNode::TestUndetectable { value } | ValueNode::TestTypeOf { value, .. } => {
            live.insert(*value);
        }

        ValueNode::StringAt { string, index } => {
            live.insert(*string);
            live.insert(*index);
        }

        ValueNode::ForInNext {
            receiver,
            cache_index,
            cache_array,
            ..
        } => {
            live.insert(*receiver);
            live.insert(*cache_index);
            live.insert(*cache_array);
        }

        ValueNode::DeleteProperty { object, key, .. } => {
            live.insert(*object);
            live.insert(*key);
        }

        ValueNode::CreateCatchContext { exception, .. } => {
            live.insert(*exception);
        }
        ValueNode::CreateWithContext { object, .. } => {
            live.insert(*object);
        }

        ValueNode::Call {
            callee,
            receiver,
            args,
            ..
        }
        | ValueNode::CallWithSpread {
            callee,
            receiver,
            args,
            ..
        }
        | ValueNode::CallKnownFunction {
            callee,
            receiver,
            args,
        } => {
            live.insert(*callee);
            live.insert(*receiver);
            for &a in args {
                live.insert(a);
            }
        }
        // CallArrayPush codegen ignores the callee (uses inline push
        // or direct stub call without method resolution).  Marking
        // callee dead lets DCE remove the LoadNamedGeneric(arr,"push")
        // that would otherwise be emitted as an FFI call per iteration.
        ValueNode::CallArrayPush { receiver, args, .. } => {
            live.insert(*receiver);
            for &a in args {
                live.insert(a);
            }
        }

        ValueNode::CallBuiltin { args, .. } | ValueNode::CallRuntime { args, .. } => {
            for &a in args {
                live.insert(a);
            }
        }

        ValueNode::SpeculativeCallFusion { callee, .. } => {
            live.insert(*callee);
        }
        ValueNode::SpeculativeSumFusion { array } => {
            live.insert(*array);
        }
        ValueNode::SpeculativePushFusion { array, .. } => {
            live.insert(*array);
        }
        ValueNode::SpeculativeFillTrueFusion { array, count } => {
            live.insert(*array);
            live.insert(*count);
        }
        ValueNode::SpeculativeCountTruthyFusion { array, count } => {
            live.insert(*array);
            live.insert(*count);
        }

        ValueNode::Construct {
            constructor, args, ..
        }
        | ValueNode::ConstructWithSpread {
            constructor, args, ..
        } => {
            live.insert(*constructor);
            for &a in args {
                live.insert(a);
            }
        }

        ValueNode::Phi { inputs } => {
            for &inp in inputs {
                live.insert(inp);
            }
        }
    }
}

/// Collect all [`NodeId`] operands referenced by a [`ControlNode`].
fn collect_control_node_inputs(ctrl: &ControlNode, live: &mut HashSet<NodeId>) {
    match ctrl {
        ControlNode::Return { value } => {
            live.insert(*value);
        }
        ControlNode::Branch { condition, .. } => {
            live.insert(*condition);
        }
        ControlNode::Jump { .. } | ControlNode::Deoptimize { .. } => {}
    }
}

/// Return `true` if this node has observable side-effects and must never be
/// removed by DCE regardless of whether its result value is consumed.
fn has_side_effects(node: &ValueNode) -> bool {
    matches!(
        node,
        // Stores always have side-effects.
        ValueNode::StoreField { .. }
            | ValueNode::StoreFixedArrayElement { .. }
            | ValueNode::StoreFixedDoubleArrayElement { .. }
            | ValueNode::StoreNamedGeneric { .. }
            | ValueNode::StoreKeyedGeneric { .. }
            | ValueNode::StoreGlobal { .. }
            | ValueNode::StoreContextSlot { .. }
            | ValueNode::StoreCurrentContextSlot { .. }
            // Calls may have side-effects.
            | ValueNode::Call { .. }
            | ValueNode::CallArrayPush { .. }
            | ValueNode::CallKnownFunction { .. }
            | ValueNode::CallBuiltin { .. }
            | ValueNode::CallRuntime { .. }
            | ValueNode::CallWithSpread { .. }
            | ValueNode::Construct { .. }
            | ValueNode::ConstructWithSpread { .. }
            | ValueNode::SpeculativeCallFusion { .. }
            | ValueNode::SpeculativeSumFusion { .. }
            | ValueNode::SpeculativePushFusion { .. }
            | ValueNode::SpeculativeFillTrueFusion { .. }
            | ValueNode::SpeculativeCountTruthyFusion { .. }
            // Allocations have side-effects (GC pressure / observable identity).
            | ValueNode::CreateObjectLiteral { .. }
            | ValueNode::CreateObjectLiteralWithProperties { .. }
            | ValueNode::CreateArrayLiteral { .. }
            | ValueNode::CreateShallowObjectLiteral { .. }
            | ValueNode::CreateShallowArrayLiteral { .. }
            | ValueNode::CreateFunctionContext { .. }
            | ValueNode::CreateBlockContext { .. }
            | ValueNode::CreateCatchContext { .. }
            | ValueNode::CreateWithContext { .. }
            | ValueNode::PushContext { .. }
            | ValueNode::PopContext { .. }
            | ValueNode::CreateClosure { .. }
            | ValueNode::FastCreateClosure { .. }
            | ValueNode::CreateEmptyObjectLiteral
            | ValueNode::CreateEmptyArrayLiteral { .. }
            // CreateMappedArguments / CreateUnmappedArguments / CreateRestParameter
            // MUST be treated as side-effecting.  Removing them via DCE changes
            // the Maglev graph shape, which alters register allocation and
            // exposes latent regalloc bugs on Linux release builds (SIGSEGV /
            // wrong results).  Codegen routes them through the runtime
            // trampoline which creates the arguments object successfully.
            | ValueNode::CreateMappedArguments
            | ValueNode::CreateUnmappedArguments
            | ValueNode::CreateRestParameter
            | ValueNode::CreateRegExpLiteral { .. }
            // Guards deoptimise on failure — side-effecting.
            | ValueNode::CheckSmi { .. }
            | ValueNode::CheckNumber { .. }
            | ValueNode::CheckHeapObject { .. }
            | ValueNode::CheckSymbol { .. }
            | ValueNode::CheckString { .. }
            | ValueNode::CheckStringOrStringWrapper { .. }
            | ValueNode::CheckSeqOneByteString { .. }
            | ValueNode::CheckMaps { .. }
            | ValueNode::CheckMapsWithMigration { .. }
            | ValueNode::CheckValue { .. }
            | ValueNode::CheckDynamicValue { .. }
            | ValueNode::CheckInt32IsSmi { .. }
            | ValueNode::CheckUint32IsSmi { .. }
            | ValueNode::CheckHoleyFloat64IsSmi { .. }
            | ValueNode::CheckInt32Condition { .. }
            | ValueNode::CheckCacheIndicesNotCleared { .. }
            | ValueNode::CheckFloat64IsNan { .. }
            | ValueNode::CheckedSmiAdd { .. }
            | ValueNode::CheckedSmiSubtract { .. }
            | ValueNode::CheckedSmiMultiply { .. }
            | ValueNode::CheckedSmiDivide { .. }
            | ValueNode::CheckedSmiModulus { .. }
            | ValueNode::CheckedSmiIncrement { .. }
            | ValueNode::CheckedSmiDecrement { .. }
            | ValueNode::CheckedFloat64ToInt32 { .. }
            | ValueNode::CheckedTaggedToInt32 { .. }
            | ValueNode::CheckedTaggedToFloat64 { .. }
            // Debugger / abort are always live.
            | ValueNode::Debugger
            | ValueNode::Abort { .. }
            // Property mutation.
            | ValueNode::DeleteProperty { .. }
            // For-in side-effects (cache invalidation).
            | ValueNode::ForInPrepare { .. }
            | ValueNode::ForInNext { .. }
    )
}

/// Return `true` for nodes that may mutate named properties of heap objects,
/// requiring the store-to-load forwarding map to be invalidated.
///
/// This is more targeted than [`has_side_effects`]: guards and checked
/// arithmetic can deopt but never mutate object properties, so they do NOT
/// invalidate the store/global maps.  Only memory-writing operations (stores,
/// calls, constructs, property deletion) require invalidation.
fn can_invalidate_named_stores(node: &ValueNode) -> bool {
    matches!(
        node,
        // Stores write to heap memory.
        ValueNode::StoreField { .. }
            | ValueNode::StoreFixedArrayElement { .. }
            | ValueNode::StoreFixedDoubleArrayElement { .. }
            | ValueNode::StoreNamedGeneric { .. }
            | ValueNode::StoreKeyedGeneric { .. }
            | ValueNode::StoreGlobal { .. }
            | ValueNode::StoreContextSlot { .. }
            | ValueNode::StoreCurrentContextSlot { .. }
            // Calls may execute arbitrary JS that mutates properties.
            | ValueNode::Call { .. }
            | ValueNode::CallArrayPush { .. }
            | ValueNode::CallKnownFunction { .. }
            | ValueNode::CallBuiltin { .. }
            | ValueNode::CallRuntime { .. }
            | ValueNode::CallWithSpread { .. }
            | ValueNode::Construct { .. }
            | ValueNode::ConstructWithSpread { .. }
            | ValueNode::SpeculativeCallFusion { .. }
            | ValueNode::SpeculativeSumFusion { .. }
            | ValueNode::SpeculativePushFusion { .. }
            | ValueNode::SpeculativeFillTrueFusion { .. }
            | ValueNode::SpeculativeCountTruthyFusion { .. }
            // Property deletion mutates objects.
            | ValueNode::DeleteProperty { .. }
            // For-in can invalidate caches.
            | ValueNode::ForInPrepare { .. }
            | ValueNode::ForInNext { .. }
    )
}

// ─────────────────────────────────────────────────────────────────────────────
// Substitution helper
// ─────────────────────────────────────────────────────────────────────────────

/// Rewrite every [`NodeId`] operand in `block` using the `subst` table.
///
/// This is used after the redundant-CheckMaps pass to redirect consumers of
/// the removed node ID to the canonical first-occurrence ID.
fn apply_subst_to_block(block: &mut BasicBlock, subst: &HashMap<NodeId, NodeId>) {
    let resolve = |id: NodeId| *subst.get(&id).unwrap_or(&id);

    for (_id, node) in &mut block.nodes {
        apply_subst_to_value_node(node, &resolve);
    }
    if let Some(ctrl) = &mut block.control {
        apply_subst_to_control_node(ctrl, &resolve);
    }
}

/// Apply a substitution function to every [`NodeId`] operand in a [`ValueNode`].
#[allow(clippy::too_many_lines)]
fn apply_subst_to_value_node(node: &mut ValueNode, resolve: &impl Fn(NodeId) -> NodeId) {
    match node {
        ValueNode::SmiConstant { .. }
        | ValueNode::Float64Constant { .. }
        | ValueNode::Int32Constant { .. }
        | ValueNode::Uint32Constant { .. }
        | ValueNode::BigIntConstant { .. }
        | ValueNode::TrueConstant
        | ValueNode::FalseConstant
        | ValueNode::NullConstant
        | ValueNode::UndefinedConstant
        | ValueNode::RootConstant { .. }
        | ValueNode::ExternalConstant { .. }
        | ValueNode::StringConstant { .. }
        | ValueNode::ConstantPoolEntry { .. }
        | ValueNode::Parameter { .. }
        | ValueNode::RegisterInput { .. }
        | ValueNode::ArgumentsLength
        | ValueNode::RestLength
        | ValueNode::LoadGlobal { .. }
        | ValueNode::LoadCurrentContextSlot { .. }
        | ValueNode::ArgumentsElements { .. }
        | ValueNode::RestElements { .. }
        | ValueNode::VirtualObject { .. }
        | ValueNode::CreateFunctionContext { .. }
        | ValueNode::CreateBlockContext { .. }
        | ValueNode::CreateShallowObjectLiteral { .. }
        | ValueNode::CreateShallowArrayLiteral { .. }
        | ValueNode::CreateObjectLiteral { .. }
        | ValueNode::CreateArrayLiteral { .. }
        | ValueNode::CreateEmptyObjectLiteral
        | ValueNode::CreateEmptyArrayLiteral { .. }
        | ValueNode::CreateMappedArguments
        | ValueNode::CreateUnmappedArguments
        | ValueNode::CreateRestParameter
        | ValueNode::CreateRegExpLiteral { .. }
        | ValueNode::CreateClosure { .. }
        | ValueNode::FastCreateClosure { .. }
        | ValueNode::GetTemplateObject { .. }
        | ValueNode::Debugger
        | ValueNode::Abort { .. } => {}

        ValueNode::GetArgument { index } => *index = resolve(*index),

        ValueNode::CheckedSmiIncrement { value }
        | ValueNode::CheckedSmiDecrement { value }
        | ValueNode::Int32Negate { value }
        | ValueNode::Int32Increment { value }
        | ValueNode::Int32Decrement { value }
        | ValueNode::Float64Negate { value }
        | ValueNode::Float64Ieee754Unary { value, .. }
        | ValueNode::GenericBitwiseNot { value, .. }
        | ValueNode::GenericNegate { value, .. }
        | ValueNode::GenericIncrement { value, .. }
        | ValueNode::GenericDecrement { value, .. }
        | ValueNode::ChangeInt32ToFloat64 { input: value }
        | ValueNode::ChangeUint32ToFloat64 { input: value }
        | ValueNode::ChangeFloat64ToInt32 { input: value }
        | ValueNode::CheckedFloat64ToInt32 { input: value }
        | ValueNode::ChangeInt32ToTagged { input: value }
        | ValueNode::ChangeUint32ToTagged { input: value }
        | ValueNode::ChangeFloat64ToTagged { input: value }
        | ValueNode::ChangeTaggedToInt32 { input: value }
        | ValueNode::ChangeTaggedToUint32 { input: value }
        | ValueNode::ChangeTaggedToFloat64 { input: value }
        | ValueNode::CheckedTaggedToInt32 { input: value }
        | ValueNode::CheckedTaggedToFloat64 { input: value }
        | ValueNode::ToBoolean { value }
        | ValueNode::ToString { value, .. }
        | ValueNode::ToObject { value, .. }
        | ValueNode::ToName { value, .. }
        | ValueNode::ToNumber { value, .. }
        | ValueNode::ToNumberOrNumeric { value, .. }
        | ValueNode::TypeOf { value }
        | ValueNode::NumberToString { value, .. }
        | ValueNode::TestUndetectable { value }
        | ValueNode::TestNullOrUndefined { value }
        | ValueNode::TestTypeOf { value, .. } => *value = resolve(*value),

        ValueNode::CheckSmi { receiver }
        | ValueNode::CheckNumber { receiver }
        | ValueNode::CheckHeapObject { receiver }
        | ValueNode::CheckSymbol { receiver }
        | ValueNode::CheckString { receiver }
        | ValueNode::CheckStringOrStringWrapper { receiver }
        | ValueNode::CheckSeqOneByteString { receiver }
        | ValueNode::CheckMaps { receiver, .. }
        | ValueNode::CheckMapsWithMigration { receiver, .. }
        | ValueNode::CheckValue { receiver, .. } => *receiver = resolve(*receiver),

        ValueNode::CheckDynamicValue { receiver, expected } => {
            *receiver = resolve(*receiver);
            *expected = resolve(*expected);
        }

        ValueNode::CheckInt32IsSmi { input }
        | ValueNode::CheckUint32IsSmi { input }
        | ValueNode::CheckHoleyFloat64IsSmi { input }
        | ValueNode::CheckFloat64IsNan { input } => *input = resolve(*input),

        ValueNode::LoadField { object, .. }
        | ValueNode::LoadTaggedField { object, .. }
        | ValueNode::LoadDoubleField { object, .. }
        | ValueNode::LoadNamedGeneric { object, .. }
        | ValueNode::LoadEnumCacheLength { map: object }
        | ValueNode::StringLength { string: object } => *object = resolve(*object),

        ValueNode::LoadKeyedGeneric { object, key, .. } => {
            *object = resolve(*object);
            *key = resolve(*key);
        }

        ValueNode::HasInPrototypeChain { object, prototype } => {
            *object = resolve(*object);
            *prototype = resolve(*prototype);
        }

        ValueNode::StoreField { object, value, .. } => {
            *object = resolve(*object);
            *value = resolve(*value);
        }

        ValueNode::StoreCurrentContextSlot { value, .. } | ValueNode::StoreGlobal { value, .. } => {
            *value = resolve(*value);
        }

        ValueNode::LoadContextSlot { context, .. } => *context = resolve(*context),
        ValueNode::StoreContextSlot { context, value, .. } => {
            *context = resolve(*context);
            *value = resolve(*value);
        }
        ValueNode::PushContext { context } | ValueNode::PopContext { context } => {
            *context = resolve(*context);
        }

        ValueNode::LoadFixedArrayElement { elements, index }
        | ValueNode::LoadFixedDoubleArrayElement { elements, index }
        | ValueNode::LoadHoleyFixedDoubleArrayElement { elements, index } => {
            *elements = resolve(*elements);
            *index = resolve(*index);
        }

        ValueNode::StoreFixedArrayElement {
            elements,
            index,
            value,
        }
        | ValueNode::StoreFixedDoubleArrayElement {
            elements,
            index,
            value,
        } => {
            *elements = resolve(*elements);
            *index = resolve(*index);
            *value = resolve(*value);
        }

        ValueNode::StoreNamedGeneric { object, value, .. } => {
            *object = resolve(*object);
            *value = resolve(*value);
        }

        ValueNode::CreateObjectLiteralWithProperties { values, .. } => {
            for v in values {
                *v = resolve(*v);
            }
        }

        ValueNode::StoreKeyedGeneric {
            object, key, value, ..
        } => {
            *object = resolve(*object);
            *key = resolve(*key);
            *value = resolve(*value);
        }

        ValueNode::CheckedSmiAdd { left, right }
        | ValueNode::CheckedSmiSubtract { left, right }
        | ValueNode::CheckedSmiMultiply { left, right }
        | ValueNode::CheckedSmiDivide { left, right }
        | ValueNode::CheckedSmiModulus { left, right }
        | ValueNode::Int32Add { left, right }
        | ValueNode::Int32Subtract { left, right }
        | ValueNode::Int32Multiply { left, right }
        | ValueNode::Int32Divide { left, right }
        | ValueNode::Int32Modulus { left, right }
        | ValueNode::Int32BitwiseAnd { left, right }
        | ValueNode::Int32BitwiseOr { left, right }
        | ValueNode::Int32BitwiseXor { left, right }
        | ValueNode::Int32ShiftLeft { left, right }
        | ValueNode::Int32ShiftRight { left, right }
        | ValueNode::Int32ShiftRightLogical { left, right }
        | ValueNode::Uint32Add { left, right }
        | ValueNode::Uint32Subtract { left, right }
        | ValueNode::Uint32Multiply { left, right }
        | ValueNode::Uint32Divide { left, right }
        | ValueNode::Uint32Modulus { left, right }
        | ValueNode::Float64Add { left, right }
        | ValueNode::Float64Subtract { left, right }
        | ValueNode::Float64Multiply { left, right }
        | ValueNode::Float64Divide { left, right }
        | ValueNode::Float64Modulus { left, right }
        | ValueNode::Float64Exponentiate { left, right }
        | ValueNode::Int32Equal { left, right }
        | ValueNode::Int32StrictEqual { left, right }
        | ValueNode::Int32LessThan { left, right }
        | ValueNode::Int32LessThanOrEqual { left, right }
        | ValueNode::Int32GreaterThan { left, right }
        | ValueNode::Int32GreaterThanOrEqual { left, right }
        | ValueNode::Float64Equal { left, right }
        | ValueNode::Float64LessThan { left, right }
        | ValueNode::Float64LessThanOrEqual { left, right }
        | ValueNode::Float64GreaterThan { left, right }
        | ValueNode::Float64GreaterThanOrEqual { left, right }
        | ValueNode::StringConcat { left, right }
        | ValueNode::StringEqual { left, right }
        | ValueNode::GenericAdd { left, right, .. }
        | ValueNode::GenericSubtract { left, right, .. }
        | ValueNode::GenericMultiply { left, right, .. }
        | ValueNode::GenericDivide { left, right, .. }
        | ValueNode::GenericModulus { left, right, .. }
        | ValueNode::GenericExponentiate { left, right, .. }
        | ValueNode::GenericBitwiseAnd { left, right, .. }
        | ValueNode::GenericBitwiseOr { left, right, .. }
        | ValueNode::GenericBitwiseXor { left, right, .. }
        | ValueNode::GenericShiftLeft { left, right, .. }
        | ValueNode::GenericShiftRight { left, right, .. }
        | ValueNode::GenericShiftRightLogical { left, right, .. }
        | ValueNode::TaggedEqual { left, right, .. }
        | ValueNode::TaggedNotEqual { left, right, .. } => {
            *left = resolve(*left);
            *right = resolve(*right);
        }

        ValueNode::CheckInt32Condition { left, right, .. } => {
            *left = resolve(*left);
            *right = resolve(*right);
        }

        ValueNode::CheckCacheIndicesNotCleared { receiver, indices } => {
            *receiver = resolve(*receiver);
            *indices = resolve(*indices);
        }

        ValueNode::TestInstanceOf {
            object, callable, ..
        } => {
            *object = resolve(*object);
            *callable = resolve(*callable);
        }
        ValueNode::TestIn { key, object, .. } => {
            *key = resolve(*key);
            *object = resolve(*object);
        }

        ValueNode::StringAt { string, index } => {
            *string = resolve(*string);
            *index = resolve(*index);
        }

        ValueNode::ForInPrepare { enumerator, .. } => *enumerator = resolve(*enumerator),
        ValueNode::ForInNext {
            receiver,
            cache_index,
            cache_array,
            ..
        } => {
            *receiver = resolve(*receiver);
            *cache_index = resolve(*cache_index);
            *cache_array = resolve(*cache_array);
        }

        ValueNode::DeleteProperty { object, key, .. } => {
            *object = resolve(*object);
            *key = resolve(*key);
        }

        ValueNode::CreateCatchContext { exception, .. } => *exception = resolve(*exception),
        ValueNode::CreateWithContext { object, .. } => *object = resolve(*object),

        ValueNode::Call {
            callee,
            receiver,
            args,
            ..
        }
        | ValueNode::CallArrayPush {
            callee,
            receiver,
            args,
            ..
        }
        | ValueNode::CallWithSpread {
            callee,
            receiver,
            args,
            ..
        }
        | ValueNode::CallKnownFunction {
            callee,
            receiver,
            args,
        } => {
            *callee = resolve(*callee);
            *receiver = resolve(*receiver);
            for a in args.iter_mut() {
                *a = resolve(*a);
            }
        }

        ValueNode::CallBuiltin { args, .. } | ValueNode::CallRuntime { args, .. } => {
            for a in args.iter_mut() {
                *a = resolve(*a);
            }
        }

        ValueNode::SpeculativeCallFusion { callee, .. } => {
            *callee = resolve(*callee);
        }
        ValueNode::SpeculativeSumFusion { array } => {
            *array = resolve(*array);
        }
        ValueNode::SpeculativePushFusion { array, .. } => {
            *array = resolve(*array);
        }
        ValueNode::SpeculativeFillTrueFusion { array, count } => {
            *array = resolve(*array);
            *count = resolve(*count);
        }
        ValueNode::SpeculativeCountTruthyFusion { array, count } => {
            *array = resolve(*array);
            *count = resolve(*count);
        }

        ValueNode::Construct {
            constructor, args, ..
        }
        | ValueNode::ConstructWithSpread {
            constructor, args, ..
        } => {
            *constructor = resolve(*constructor);
            for a in args.iter_mut() {
                *a = resolve(*a);
            }
        }

        ValueNode::Phi { inputs } => {
            for inp in inputs.iter_mut() {
                *inp = resolve(*inp);
            }
        }
    }
}

/// Apply a substitution to the operands of a [`ControlNode`].
fn apply_subst_to_control_node(ctrl: &mut ControlNode, resolve: &impl Fn(NodeId) -> NodeId) {
    match ctrl {
        ControlNode::Return { value } => *value = resolve(*value),
        ControlNode::Branch { condition, .. } => *condition = resolve(*condition),
        ControlNode::Jump { .. } | ControlNode::Deoptimize { .. } => {}
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass 10 — Simple counted-loop unrolling (2×)
// ─────────────────────────────────────────────────────────────────────────────

/// Unroll simple counted for-loops by a factor of 2.
///
/// The pass targets loops with the following shape:
///
/// ```text
///   header:  Phi_i, Phi_n, …  |  cmp Phi_i < LIMIT  |  branch(body, exit)
///   body:    … new_i = Phi_i + 1 …  |  jump(header)
/// ```
///
/// When the trip count (`LIMIT − init`) is even, the body block is duplicated
/// inline: the second copy uses the first copy's outputs as inputs, halving
/// the number of back-edge comparisons.
fn unroll_simple_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    for lp in &loops {
        // Try 4x first (better for large loops like arithmetic_loop_10k),
        // fall back to 2x if trip count isn't divisible by 4.
        if !try_unroll_counted_loop(graph, lp, 4) {
            try_unroll_counted_loop(graph, lp, 2);
        }
    }
}

/// Attempt to unroll a single natural loop by the given `factor`.
///
/// Returns `true` if the loop was successfully unrolled.
fn try_unroll_counted_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop, factor: u32) -> bool {
    if factor < 2 {
        return false;
    }

    let header_idx = lp.header;
    let header = match graph.block(header_idx) {
        Some(b) => b,
        None => return false,
    };

    // ── 1. The header must end with a Branch on an Int32 comparison ──────
    let (condition_id, body_block_idx, _exit_block_idx) = match &header.control {
        Some(ControlNode::Branch {
            condition,
            if_true,
            if_false,
        }) => (*condition, *if_true, *if_false),
        _ => return false,
    };

    // The body block must be inside the loop.
    if !lp.body.contains(&body_block_idx) {
        return false;
    }

    // ── 2. Simple loop: body is a single block jumping back to header ────
    if lp.body.len() != 2 || !lp.body.contains(&header_idx) {
        return false;
    }

    let body = match graph.block(body_block_idx) {
        Some(b) => b,
        None => return false,
    };

    // Body must jump back to the header (back-edge).
    match &body.control {
        Some(ControlNode::Jump { target }) if *target == header_idx => {}
        _ => return false,
    }

    // ── 2b. Reject loops whose body contains method calls (CallProperty) ─
    // Unrolling loops with `arr.push(i)` or similar method calls creates
    // multiple runtime-stub invocations per iteration.  These stubs can
    // reallocate array backing stores without updating the JIT's inline
    // array IC, causing stale-pointer SIGSEGV when a later keyed-load
    // loop reads from the same array.  Bail out to keep such loops at 1×
    // where the interaction is safe.
    let has_method_call = body.nodes.iter().any(|(_, node)| {
        matches!(
            node,
            ValueNode::Call { .. }
                | ValueNode::CallKnownFunction { .. }
                | ValueNode::CallWithSpread { .. }
                | ValueNode::CallBuiltin { .. }
                | ValueNode::CallRuntime { .. }
        )
    });
    if has_method_call {
        return false;
    }

    // ── 3. Find the comparison and its operands ─────────────────────────
    let (cmp_left, cmp_right) = {
        let mut found = None;
        for (nid, node) in &header.nodes {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match found {
            Some(ValueNode::Int32LessThan { left, right }) => (left, right),
            _ => return false,
        }
    };

    // ── 4. Identify Phi nodes, induction variable, and constants ─────────
    let back_edge_pred_pos = {
        let h = graph.block(header_idx).unwrap();
        match h.predecessors.iter().position(|&p| p == body_block_idx) {
            Some(pos) => pos,
            None => return false,
        }
    };

    let preheader_pred_pos = {
        let h = graph.block(header_idx).unwrap();
        match h.predecessors.iter().position(|&p| p == lp.preheader) {
            Some(pos) => pos,
            None => return false,
        }
    };

    // Check that cmp_left is a Phi in the header (the induction variable).
    let induction_phi_inputs = {
        let h = graph.block(header_idx).unwrap();
        let mut found = None;
        for (nid, node) in &h.nodes {
            if *nid == cmp_left {
                if let ValueNode::Phi { inputs } = node {
                    found = Some(inputs.clone());
                }
                break;
            }
        }
        match found {
            Some(inputs) => inputs,
            None => return false,
        }
    };

    // Get the initial value and the limit as constants.
    let init_id = induction_phi_inputs[preheader_pred_pos];
    let limit_id = cmp_right;

    let init_value = find_i32_constant(graph, init_id);
    let limit_value = find_i32_constant(graph, limit_id);

    let (init_val, limit_val) = match (init_value, limit_value) {
        (Some(i), Some(l)) => (i, l),
        _ => return false,
    };

    // ── 5. Find the induction variable increment in the body ────────────
    let back_edge_input = induction_phi_inputs[back_edge_pred_pos];
    let step = find_increment_step(graph, back_edge_input, cmp_left);
    let step_val = match step {
        Some(s) if s > 0 => s,
        _ => return false,
    };

    // ── 6. Check trip count divisibility ─────────────────────────────────
    let trip_count = (limit_val - init_val) as i64;
    if trip_count <= 0 {
        return false;
    }
    let effective_step = (step_val as i64) * (factor as i64);
    if trip_count % effective_step != 0 {
        return false;
    }

    // Don't unroll very small loops (< 2*factor iterations).
    if trip_count < effective_step * 2 {
        return false;
    }

    // ── 7. Collect Phi → back-edge-input mappings ────────────────────────
    let header_phis: Vec<(NodeId, Vec<NodeId>)> = {
        let h = graph.block(header_idx).unwrap();
        h.nodes
            .iter()
            .filter_map(|(nid, node)| {
                if let ValueNode::Phi { inputs } = node {
                    Some((*nid, inputs.clone()))
                } else {
                    None
                }
            })
            .collect()
    };

    let phi_to_back_input: HashMap<NodeId, NodeId> = header_phis
        .iter()
        .filter_map(|(phi_id, inputs)| {
            inputs
                .get(back_edge_pred_pos)
                .map(|&back_input| (*phi_id, back_input))
        })
        .collect();

    // ── 8. Clone the body nodes for additional unrolled copies ───────────
    let body_nodes: Vec<(NodeId, ValueNode)> = graph.block(body_block_idx).unwrap().nodes.clone();

    let mut prev_phi_to_output = phi_to_back_input.clone();

    for _copy in 1..factor {
        let mut subst: HashMap<NodeId, NodeId> = HashMap::new();
        for (phi_id, output_id) in &prev_phi_to_output {
            subst.insert(*phi_id, *output_id);
        }

        let mut old_to_new: HashMap<NodeId, NodeId> = HashMap::new();
        for (old_id, _node) in &body_nodes {
            let new_id = graph.alloc_node_id();
            old_to_new.insert(*old_id, new_id);
        }

        let resolve = |id: NodeId| -> NodeId {
            if let Some(&new_id) = old_to_new.get(&id) {
                new_id
            } else if let Some(&output_id) = subst.get(&id) {
                output_id
            } else {
                id
            }
        };

        let mut cloned_nodes: Vec<(NodeId, ValueNode)> = Vec::new();
        for (old_id, node) in &body_nodes {
            let new_id = old_to_new[old_id];
            let mut cloned = node.clone();
            apply_subst_to_value_node(&mut cloned, &resolve);
            cloned_nodes.push((new_id, cloned));
        }

        let body_block = graph.block_mut(body_block_idx).unwrap();
        for (id, node) in &cloned_nodes {
            body_block.push_with_id(*id, node.clone());
        }

        let mut new_phi_to_output: HashMap<NodeId, NodeId> = HashMap::new();
        for (phi_id, back_input) in &phi_to_back_input {
            if let Some(&new_id) = old_to_new.get(back_input) {
                new_phi_to_output.insert(*phi_id, new_id);
            } else {
                new_phi_to_output.insert(*phi_id, *back_input);
            }
        }
        prev_phi_to_output = new_phi_to_output;
    }

    // ── 9. Update header Phi back-edge inputs to last copy's outputs ─────
    let header_block = graph.block_mut(header_idx).unwrap();
    for (nid, node) in &mut header_block.nodes {
        if let ValueNode::Phi { inputs } = node
            && let Some(&new_output) = prev_phi_to_output.get(nid)
            && let Some(back_input) = inputs.get_mut(back_edge_pred_pos)
        {
            *back_input = new_output;
        }
    }

    true
}

/// Find the compile-time i32 constant value for a [`NodeId`], if any.
fn find_i32_constant(graph: &MaglevGraph, id: NodeId) -> Option<i32> {
    for block in graph.blocks() {
        for (nid, node) in &block.nodes {
            if *nid == id {
                return match node {
                    ValueNode::Int32Constant { value } | ValueNode::SmiConstant { value } => {
                        Some(*value)
                    }
                    _ => None,
                };
            }
        }
    }
    None
}

/// Determine the increment step of an induction variable.
///
/// Returns `Some(step)` if `result_id` is computed as `base_id + step`.
fn find_increment_step(graph: &MaglevGraph, result_id: NodeId, base_id: NodeId) -> Option<i32> {
    let node = graph.node(result_id)?;
    match node {
        ValueNode::Int32Increment { value }
        | ValueNode::CheckedSmiIncrement { value }
        | ValueNode::GenericIncrement { value, .. } => {
            if *value == base_id {
                Some(1)
            } else {
                None
            }
        }
        ValueNode::Int32Add { left, right }
        | ValueNode::CheckedSmiAdd { left, right }
        | ValueNode::GenericAdd { left, right, .. } => {
            if *left == base_id {
                find_i32_constant(graph, *right)
            } else if *right == base_id {
                find_i32_constant(graph, *left)
            } else {
                None
            }
        }
        _ => None,
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Affine analysis helpers for loop scalar evolution
// ─────────────────────────────────────────────────────────────────────────────

/// Try to decompose `expr_id` as `a * iv_phi + b` where a, b are constants.
/// Returns `Some((a, b))` with i128 coefficients for overflow-safe arithmetic.
fn extract_affine_addend(
    graph: &MaglevGraph,
    expr_id: NodeId,
    iv_phi_id: NodeId,
    consts: &HashMap<NodeId, i32>,
    depth: u32,
) -> Option<(i128, i128)> {
    if depth > 8 {
        return None;
    }
    if expr_id == iv_phi_id {
        return Some((1, 0));
    }
    if let Some(&k) = consts.get(&expr_id) {
        return Some((0, k as i128));
    }
    if let Some(k) = find_i32_constant(graph, expr_id) {
        return Some((0, k as i128));
    }
    let node = graph.node(expr_id)?.clone();
    match &node {
        ValueNode::Int32Add { left, right } => {
            let (a1, b1) = extract_affine_addend(graph, *left, iv_phi_id, consts, depth + 1)?;
            let (a2, b2) = extract_affine_addend(graph, *right, iv_phi_id, consts, depth + 1)?;
            Some((a1 + a2, b1 + b2))
        }
        ValueNode::Int32Subtract { left, right } => {
            let (a1, b1) = extract_affine_addend(graph, *left, iv_phi_id, consts, depth + 1)?;
            let (a2, b2) = extract_affine_addend(graph, *right, iv_phi_id, consts, depth + 1)?;
            Some((a1 - a2, b1 - b2))
        }
        ValueNode::Int32Multiply { left, right } => {
            let (a1, b1) = extract_affine_addend(graph, *left, iv_phi_id, consts, depth + 1)?;
            let (a2, b2) = extract_affine_addend(graph, *right, iv_phi_id, consts, depth + 1)?;
            if a1 == 0 {
                Some((b1 * a2, b1 * b2))
            } else if a2 == 0 {
                Some((a1 * b2, b1 * b2))
            } else {
                None // quadratic — bail
            }
        }
        ValueNode::Int32ShiftLeft { left, right } => {
            let (a, b) = extract_affine_addend(graph, *left, iv_phi_id, consts, depth + 1)?;
            let shift = consts
                .get(right)
                .copied()
                .or_else(|| find_i32_constant(graph, *right))?;
            if (0..32).contains(&shift) {
                let factor = 1i128 << (shift as u32);
                Some((a * factor, b * factor))
            } else {
                None
            }
        }
        ValueNode::Int32Negate { value } => {
            let (a, b) = extract_affine_addend(graph, *value, iv_phi_id, consts, depth + 1)?;
            Some((-a, -b))
        }
        _ => None,
    }
}

/// Given a back-edge node, decompose it as `phi_acc + (a * iv + b)`.
/// Returns `Some((a, b))` for the per-iteration affine addend.
fn peel_accumulator_addend(
    graph: &MaglevGraph,
    back_id: NodeId,
    phi_acc_id: NodeId,
    iv_phi_id: NodeId,
    consts: &HashMap<NodeId, i32>,
    depth: u32,
) -> Option<(i128, i128)> {
    if depth > 4 {
        return None;
    }
    let node = graph.node(back_id)?.clone();
    match &node {
        ValueNode::Int32Add { left, right } => {
            if *left == phi_acc_id {
                return extract_affine_addend(graph, *right, iv_phi_id, consts, 0);
            }
            if *right == phi_acc_id {
                return extract_affine_addend(graph, *left, iv_phi_id, consts, 0);
            }
            // Neither is phi directly — try recursing
            if let Some((a, b)) =
                peel_accumulator_addend(graph, *left, phi_acc_id, iv_phi_id, consts, depth + 1)
                && let Some((a2, b2)) = extract_affine_addend(graph, *right, iv_phi_id, consts, 0)
            {
                return Some((a + a2, b + b2));
            }
            if let Some((a, b)) =
                peel_accumulator_addend(graph, *right, phi_acc_id, iv_phi_id, consts, depth + 1)
                && let Some((a2, b2)) = extract_affine_addend(graph, *left, iv_phi_id, consts, 0)
            {
                return Some((a + a2, b + b2));
            }
            None
        }
        ValueNode::Int32Subtract { left, right } => {
            // phi_acc must be in the left subtree (subtracting phi would negate it)
            let (a, b) =
                peel_accumulator_addend(graph, *left, phi_acc_id, iv_phi_id, consts, depth + 1)?;
            let (a2, b2) = extract_affine_addend(graph, *right, iv_phi_id, consts, 0)?;
            Some((a - a2, b - b2))
        }
        _ => None,
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass — Late constant-accumulator lowering
// ─────────────────────────────────────────────────────────────────────────────

/// After store-to-load forwarding and constant folding, some loop accumulator
/// patterns become `GenericAdd(Phi, SmiConstant(K))` where the constant was
/// only revealed by forwarding property loads.  This targeted pass converts
/// these to `Int32Add` when:
///
/// 1. The back-edge input of a Phi is `GenericAdd(phi, SmiConstant)`.
/// 2. The Phi init (entry-edge) is `SmiConstant(init_val)`.
/// 3. The total accumulation `init + K * max_iterations` fits in i32.
///
/// Unlike the full `eliminate_overflow_checks`, this pass does NOT attempt to
/// detect induction variables or complex chained patterns.
fn lower_constant_accumulator_adds(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    if loops.is_empty() {
        return;
    }

    // Build a node → constant map.
    let mut consts: HashMap<NodeId, i32> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            match node {
                ValueNode::SmiConstant { value } | ValueNode::Int32Constant { value } => {
                    consts.insert(*id, *value);
                }
                _ => {}
            }
        }
    }

    // For each loop, look for GenericAdd(Phi, SmiConstant) accumulators.
    let mut rewrites: Vec<(u32, usize, NodeId)> = Vec::new();

    for lp in &loops {
        let header = &graph.blocks()[lp.header as usize];
        let back_pred_pos = match header
            .predecessors
            .iter()
            .position(|&p| lp.body.contains(&p) && p != lp.header)
        {
            Some(pos) => pos,
            None => continue,
        };
        let entry_pos = if back_pred_pos == 0 { 1 } else { 0 };

        // Estimate max iterations from the loop structure.
        let max_iters = estimate_max_iterations(graph, lp, &consts);

        for (phi_id, node) in &header.nodes {
            let ValueNode::Phi { inputs } = node else {
                continue;
            };
            if inputs.len() != 2 {
                continue;
            }

            let init_id = inputs[entry_pos];
            let back_id = inputs[back_pred_pos];

            // Init must be a known constant.
            let Some(&init_val) = consts.get(&init_id) else {
                continue;
            };

            // Find back-edge node: must be GenericAdd(phi, const) in a body block.
            let back_node = find_node_in_blocks(graph, &back_id, &lp.body);
            let Some((block_idx, node_idx, back_value)) = back_node else {
                continue;
            };

            let addend_val = match &back_value {
                ValueNode::GenericAdd { left, right, .. } if *left == *phi_id => {
                    consts.get(right).copied()
                }
                ValueNode::GenericAdd { left, right, .. } if *right == *phi_id => {
                    consts.get(left).copied()
                }
                _ => None,
            };

            let Some(k) = addend_val else {
                continue;
            };

            // Check total accumulation fits i32.
            let iters = max_iters.unwrap_or(100_000) as i64;
            let total_min = (init_val as i64) + (k as i64) * iters;
            let total_max = (init_val as i64) + (k as i64) * iters;
            // Check both positive and negative constant accumulation.
            let (lo, hi) = if k >= 0 {
                (init_val as i64, total_max)
            } else {
                (total_min, init_val as i64)
            };
            if lo < i32::MIN as i64 || hi > i32::MAX as i64 {
                continue;
            }

            rewrites.push((block_idx, node_idx, back_id));
        }
    }

    // Apply rewrites.
    for (block_idx, node_idx, _back_id) in &rewrites {
        if let Some(block) = graph.block_mut(*block_idx)
            && let Some((_, node)) = block.nodes.get_mut(*node_idx)
            && let ValueNode::GenericAdd { left, right, .. } = node
        {
            *node = ValueNode::Int32Add {
                left: *left,
                right: *right,
            };
        }
    }
}

/// Find a node by ID in the given set of blocks, returning (block_idx, node_idx, node).
fn find_node_in_blocks(
    graph: &MaglevGraph,
    node_id: &NodeId,
    block_indices: &HashSet<u32>,
) -> Option<(u32, usize, ValueNode)> {
    for &bi in block_indices {
        if let Some(block) = graph.block(bi) {
            for (pos, (id, node)) in block.nodes.iter().enumerate() {
                if *id == *node_id {
                    return Some((bi, pos, node.clone()));
                }
            }
        }
    }
    None
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass — Loop scalar evolution (constant-accumulator elimination)
// ─────────────────────────────────────────────────────────────────────────────

/// Replace simple counted loops that only accumulate a constant addend with
/// a closed-form computation.
///
/// Pattern detected:
///   header: phi_acc = Phi(init, back_edge)
///           phi_iv  = Phi(iv_init, iv_step)
///           Branch(Int32LessThan(phi_iv, limit), body, exit)
///   body:   back_edge = Int32Add(phi_acc, K)  [K is constant]
///           iv_step   = Int32Add(phi_iv, step) [step is constant]
///           Jump(header)
///
/// Replacement:
///   preheader: trip_count = (limit - iv_init) / step
///              result = init + K * trip_count
///   The accumulator Phi's init input is replaced with `result` and the
///   loop body's accumulator update becomes an identity (phi feeds itself),
///   effectively making the loop a no-op for the accumulator.
///
/// This eliminates the entire loop for benchmarks like property_access
/// (`sum += 15` × 1000 → `sum = 15000`) and deep_object (`sum += 99` × 1000).
fn eliminate_constant_accumulator_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    if loops.is_empty() {
        return;
    }

    // Build a node → constant map.
    let mut consts: HashMap<NodeId, i32> = HashMap::new();
    for block in graph.blocks() {
        for (id, node) in &block.nodes {
            match node {
                ValueNode::SmiConstant { value } | ValueNode::Int32Constant { value } => {
                    consts.insert(*id, *value);
                }
                _ => {}
            }
        }
    }

    for lp in &loops {
        try_eliminate_constant_accumulator(graph, lp, &consts);
    }
}

/// Try to replace a single loop's constant accumulator with a closed-form value.
fn try_eliminate_constant_accumulator(
    graph: &mut MaglevGraph,
    lp: &licm::NaturalLoop,
    consts: &HashMap<NodeId, i32>,
) -> bool {
    let header = match graph.block(lp.header) {
        Some(b) => b,
        None => return false,
    };

    // ── 1. Header must end with Branch on Int32LessThan ──────────────────
    let (condition_id, body_block_idx, _exit_block_idx) = match &header.control {
        Some(ControlNode::Branch {
            condition,
            if_true,
            if_false,
        }) => (*condition, *if_true, *if_false),
        _ => return false,
    };

    if !lp.body.contains(&body_block_idx) {
        return false;
    }

    // Simple loop: header + 1 body block.
    if lp.body.len() != 2 || !lp.body.contains(&lp.header) {
        return false;
    }

    let body = match graph.block(body_block_idx) {
        Some(b) => b,
        None => return false,
    };

    // Body must jump back to header.
    match &body.control {
        Some(ControlNode::Jump { target }) if *target == lp.header => {}
        _ => return false,
    }

    // ── 2. Find comparison and induction variable ────────────────────────
    let (cmp_left, cmp_right) = {
        let mut found = None;
        for (nid, node) in &header.nodes {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match found {
            Some(ValueNode::Int32LessThan { left, right }) => (left, right),
            _ => return false,
        }
    };

    // ── 3. Identify predecessor positions ────────────────────────────────
    let back_pred_pos = match header
        .predecessors
        .iter()
        .position(|&p| p == body_block_idx)
    {
        Some(pos) => pos,
        None => return false,
    };
    let entry_pos = match header.predecessors.iter().position(|&p| p == lp.preheader) {
        Some(pos) => pos,
        None => return false,
    };

    // ── 4. Find the induction variable Phi ───────────────────────────────
    let iv_phi_id = cmp_left;
    let iv_phi_inputs = {
        let h = graph.block(lp.header).unwrap();
        let mut found = None;
        for (nid, node) in &h.nodes {
            if *nid == iv_phi_id {
                if let ValueNode::Phi { inputs } = node {
                    found = Some(inputs.clone());
                }
                break;
            }
        }
        match found {
            Some(inputs) if inputs.len() == 2 => inputs,
            _ => return false,
        }
    };

    let iv_init_id = iv_phi_inputs[entry_pos];
    let iv_limit_id = cmp_right;

    let iv_init = match consts.get(&iv_init_id) {
        Some(&v) => v,
        None => match find_i32_constant(graph, iv_init_id) {
            Some(v) => v,
            None => return false,
        },
    };
    let iv_limit = match consts.get(&iv_limit_id) {
        Some(&v) => v,
        None => match find_i32_constant(graph, iv_limit_id) {
            Some(v) => v,
            None => return false,
        },
    };

    // Find IV step.
    let iv_back_id = iv_phi_inputs[back_pred_pos];
    let iv_step = find_increment_step(graph, iv_back_id, iv_phi_id);
    let iv_step_val = match iv_step {
        Some(s) if s > 0 => s,
        _ => return false,
    };

    // Compute trip count using widened arithmetic.  Require exact
    // divisibility to avoid off-by-one errors.
    let range = (iv_limit as i64) - (iv_init as i64);
    let step = iv_step_val as i64;
    if range <= 0 || range % step != 0 {
        return false;
    }
    let trip_count = range / step;

    // ── 5. Find accumulator Phis with Int32Add(phi, const) back-edges ───
    let h = graph.block(lp.header).unwrap();
    let header_phis: Vec<(NodeId, Vec<NodeId>)> = h
        .nodes
        .iter()
        .filter_map(|(nid, node)| {
            if let ValueNode::Phi { inputs } = node {
                Some((*nid, inputs.clone()))
            } else {
                None
            }
        })
        .collect();

    // Collect accumulators: (phi_id, init_val, affine_a, affine_b, back_node_id)
    // affine_a and affine_b represent the per-iteration addend: a * iv + b
    let mut accumulators: Vec<(NodeId, i32, i128, i128, NodeId)> = Vec::new();

    // Build use-count map for the loop body to verify accumulators are
    // only used by their own recurrence update.
    let mut loop_use_counts: HashMap<NodeId, usize> = HashMap::new();
    for &bi in &lp.body {
        if let Some(block) = graph.block(bi) {
            for (_, node) in &block.nodes {
                visit_value_node_inputs(node, &mut |id| {
                    *loop_use_counts.entry(id).or_insert(0) += 1;
                });
            }
        }
    }

    for (phi_id, inputs) in &header_phis {
        if *phi_id == iv_phi_id {
            continue; // Skip the induction variable itself.
        }
        if inputs.len() != 2 {
            continue;
        }

        let init_id = inputs[entry_pos];
        let back_id = inputs[back_pred_pos];

        // Init must be a known constant.
        let init_val = match consts.get(&init_id) {
            Some(&v) => v,
            None => match find_i32_constant(graph, init_id) {
                Some(v) => v,
                None => continue,
            },
        };

        // Back-edge must decompose as phi_acc + (a * iv + b) using only
        // Int32 operations.  This covers both constant addends (a=0) and
        // IV-dependent addends like `i*3 - 1` (a=3, b=-1).
        let Some((aff_a, aff_b)) =
            peel_accumulator_addend(graph, back_id, *phi_id, iv_phi_id, consts, 0)
        else {
            continue;
        };

        // Safety: the accumulator Phi must only be used inside the loop
        // by its own recurrence update.  If anything else reads the
        // accumulator within the loop, we cannot replace it.
        let in_loop_uses = loop_use_counts.get(phi_id).copied().unwrap_or(0);
        if in_loop_uses > 1 {
            continue;
        }

        // Compute closed-form result using i128 to avoid overflow:
        //   result = init + N*(a*iv_init + b) + a*step*N*(N-1)/2
        let n = trip_count as i128;
        let iv0 = iv_init as i128;
        let s = iv_step_val as i128;
        let result = (init_val as i128) + n * (aff_a * iv0 + aff_b) + aff_a * s * n * (n - 1) / 2;
        if result < i32::MIN as i128 || result > i32::MAX as i128 {
            continue;
        }

        accumulators.push((*phi_id, init_val, aff_a, aff_b, back_id));
    }

    if accumulators.is_empty() {
        return false;
    }

    // ── 6. Apply: replace Phi init with the closed-form result ──────────
    // Create a SmiConstant for the result in the preheader.
    for (phi_id, init_val, aff_a, aff_b, _back_id) in &accumulators {
        let n = trip_count as i128;
        let iv0 = iv_init as i128;
        let s = iv_step_val as i128;
        let result =
            (*init_val as i128) + n * (*aff_a * iv0 + *aff_b) + *aff_a * s * n * (n - 1) / 2;
        let result_val = result as i32;

        // Create a new SmiConstant node for the result.
        let result_node_id = graph.alloc_node_id();
        if let Some(pre) = graph.block_mut(lp.preheader) {
            pre.push_with_id(result_node_id, ValueNode::SmiConstant { value: result_val });
        }

        // Update the Phi's init input to point to the computed result.
        let h = graph.block_mut(lp.header).unwrap();
        for (nid, node) in &mut h.nodes {
            if *nid == *phi_id {
                if let ValueNode::Phi { inputs } = node {
                    inputs[entry_pos] = result_node_id;
                    // Also make the back-edge point to the phi itself,
                    // making the accumulator an identity within the loop.
                    inputs[back_pred_pos] = *phi_id;
                }
                break;
            }
        }
    }

    // The loop's induction variable has a statically known value after the
    // loop exits.  Rewrite only post-loop uses; the loop body and header still
    // see the real Phi so side effects remain ordered if the loop is live.
    let final_iv = (iv_init as i128) + (iv_step_val as i128) * (trip_count as i128);
    if (i32::MIN as i128..=i32::MAX as i128).contains(&final_iv) {
        let final_iv_id = graph.alloc_node_id();
        if let Some(pre) = graph.block_mut(lp.preheader) {
            pre.push_with_id(
                final_iv_id,
                ValueNode::SmiConstant {
                    value: final_iv as i32,
                },
            );
        }
        replace_uses_outside_loop(graph, lp, iv_phi_id, final_iv_id);
    }

    true
}

/// Replace uses of `from` in blocks outside `lp`.
fn replace_uses_outside_loop(
    graph: &mut MaglevGraph,
    lp: &licm::NaturalLoop,
    from: NodeId,
    to: NodeId,
) {
    let subst = HashMap::from([(from, to)]);
    for block in graph.blocks_mut() {
        if lp.body.contains(&block.id) {
            continue;
        }
        apply_subst_to_block(block, &subst);
    }
}

/// Estimate the maximum number of iterations for a loop by examining
/// the induction variable pattern (Phi with Int32LessThan comparison).
fn estimate_max_iterations(
    graph: &MaglevGraph,
    lp: &licm::NaturalLoop,
    consts: &HashMap<NodeId, i32>,
) -> Option<u64> {
    let header = &graph.blocks()[lp.header as usize];

    // Header must end with Branch on a comparison.
    let condition_id = match &header.control {
        Some(ControlNode::Branch { condition, .. }) => *condition,
        _ => return None,
    };

    // Find the comparison node.
    let cmp_node = header
        .nodes
        .iter()
        .find(|(id, _)| *id == condition_id)
        .map(|(_, n)| n)?;

    let (left, right) = match cmp_node {
        ValueNode::Int32LessThan { left, right } => (*left, *right),
        _ => return None,
    };

    // One side should be a Phi (IV), the other a constant (limit).
    let limit = consts.get(&right).or_else(|| consts.get(&left))?;
    Some(*limit as u64)
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass — Dead counted loop elimination
// ─────────────────────────────────────────────────────────────────────────────

/// Remove loops whose body has no side effects and whose values are not
/// referenced by any node outside the loop.
///
/// After scalar evolution resolves accumulator Phis to constants and trivial
/// phi elimination propagates those constants to external consumers, a counted
/// loop may become entirely dead — its only remaining work is incrementing the
/// induction variable, which nothing outside reads.  This pass detects such
/// loops and short-circuits the preheader to jump directly to the exit block,
/// eliminating all loop overhead.
#[allow(dead_code)] // Disabled in optimize() — see SIGSEGV note there.
fn eliminate_dead_counted_loops(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    for lp in &loops {
        try_eliminate_dead_loop(graph, lp);
    }
}

/// Attempt to eliminate a single dead loop.
///
/// Returns `true` if the loop was removed.
#[allow(dead_code)] // Disabled in optimize() — see SIGSEGV note there.
fn try_eliminate_dead_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop) -> bool {
    // ── 1. Find the exit block ──────────────────────────────────────────────
    let exit_block_idx = {
        let header = match graph.block(lp.header) {
            Some(b) => b,
            None => return false,
        };
        match &header.control {
            Some(ControlNode::Branch {
                if_true, if_false, ..
            }) => {
                if lp.body.contains(if_true) && !lp.body.contains(if_false) {
                    *if_false
                } else if lp.body.contains(if_false) && !lp.body.contains(if_true) {
                    *if_true
                } else {
                    return false;
                }
            }
            _ => return false,
        }
    };

    // ── 2. Collect all NodeIds defined inside the loop ──────────────────────
    let mut loop_node_ids: HashSet<NodeId> = HashSet::new();
    for &block_idx in &lp.body {
        if let Some(block) = graph.block(block_idx) {
            for (id, _) in &block.nodes {
                loop_node_ids.insert(*id);
            }
        }
    }

    // ── 3. No side effects allowed ──────────────────────────────────────────
    for &block_idx in &lp.body {
        if let Some(block) = graph.block(block_idx) {
            for (_, node) in &block.nodes {
                if has_side_effects(node) {
                    return false;
                }
            }
        }
    }

    // ── 4. No loop-internal value may be referenced from outside ────────────
    for block in graph.blocks() {
        if lp.body.contains(&block.id) {
            continue;
        }
        for (_, node) in &block.nodes {
            let mut refs_loop = false;
            visit_value_node_inputs(node, &mut |id| {
                if loop_node_ids.contains(&id) {
                    refs_loop = true;
                }
            });
            if refs_loop {
                return false;
            }
        }
        if let Some(ctrl) = &block.control {
            let mut live = HashSet::new();
            collect_control_node_inputs(ctrl, &mut live);
            if live.iter().any(|id| loop_node_ids.contains(id)) {
                return false;
            }
        }
    }

    // ── 5. Bail out if the exit block has Phis (predecessor rewrite is unsafe) ─
    if let Some(exit_block) = graph.block(exit_block_idx) {
        for (_, node) in &exit_block.nodes {
            if matches!(node, ValueNode::Phi { .. }) {
                return false;
            }
        }
    }

    // ── 6. Redirect preheader → exit ────────────────────────────────────────
    if let Some(preheader) = graph.block_mut(lp.preheader) {
        preheader.control = Some(ControlNode::Jump {
            target: exit_block_idx,
        });
    }

    // Update exit block predecessors.
    if let Some(exit_block) = graph.block_mut(exit_block_idx) {
        exit_block.predecessors.retain(|p| !lp.body.contains(p));
        if !exit_block.predecessors.contains(&lp.preheader) {
            exit_block.predecessors.push(lp.preheader);
        }
    }

    // Clear dead loop blocks.
    for &block_idx in &lp.body {
        if let Some(block) = graph.block_mut(block_idx) {
            block.nodes.clear();
            block.is_loop_header = false;
            block.control = Some(ControlNode::Jump {
                target: exit_block_idx,
            });
        }
    }

    true
}

// ─────────────────────────────────────────────────────────────────────────────
// Pass — Induction variable strength reduction
// ─────────────────────────────────────────────────────────────────────────────

/// Replace `Int32Multiply(iv_phi, K)` inside counted loops with an additive
/// derived induction variable: `derived_phi(init*K, derived_phi + step*K)`.
///
/// This eliminates multiplies from the loop body's critical path, replacing
/// them with adds.  For example, `i * 3` becomes a new IV that increments
/// by 3 each iteration.
#[allow(dead_code)]
fn strength_reduce_induction_variables(graph: &mut MaglevGraph) {
    let loops = licm::detect_loops(graph);
    for lp in &loops {
        strength_reduce_iv_in_loop(graph, lp);
    }
}

#[allow(dead_code)]
fn strength_reduce_iv_in_loop(graph: &mut MaglevGraph, lp: &licm::NaturalLoop) -> bool {
    let header_idx = lp.header;

    // ── 1. Header must end with Branch on Int32LessThan ─────────────────
    let (condition_id, body_block_idx) = {
        let header = match graph.block(header_idx) {
            Some(b) => b,
            None => return false,
        };
        match &header.control {
            Some(ControlNode::Branch {
                condition, if_true, ..
            }) => (*condition, *if_true),
            _ => return false,
        }
    };

    if !lp.body.contains(&body_block_idx) {
        return false;
    }

    // Simple loop: body is a single block jumping back to header.
    if lp.body.len() != 2 || !lp.body.contains(&header_idx) {
        return false;
    }

    match graph.block(body_block_idx).and_then(|b| b.control.as_ref()) {
        Some(ControlNode::Jump { target }) if *target == header_idx => {}
        _ => return false,
    }

    // ── 2. Identify the induction variable ──────────────────────────────
    let cmp_left = {
        let header = graph.block(header_idx).unwrap();
        let mut found = None;
        for (nid, node) in &header.nodes {
            if *nid == condition_id {
                found = Some(node.clone());
                break;
            }
        }
        match found {
            Some(ValueNode::Int32LessThan { left, .. }) => left,
            _ => return false,
        }
    };

    let back_edge_pred_pos = {
        let h = graph.block(header_idx).unwrap();
        match h.predecessors.iter().position(|&p| p == body_block_idx) {
            Some(pos) => pos,
            None => return false,
        }
    };
    let preheader_pred_pos = {
        let h = graph.block(header_idx).unwrap();
        match h.predecessors.iter().position(|&p| p == lp.preheader) {
            Some(pos) => pos,
            None => return false,
        }
    };

    let iv_phi_id = cmp_left;
    let iv_phi_inputs = {
        let h = graph.block(header_idx).unwrap();
        let mut found = None;
        for (nid, node) in &h.nodes {
            if *nid == iv_phi_id {
                if let ValueNode::Phi { inputs } = node {
                    found = Some(inputs.clone());
                }
                break;
            }
        }
        match found {
            Some(inputs) => inputs,
            None => return false,
        }
    };

    let init_id = iv_phi_inputs[preheader_pred_pos];
    let init_val = match find_i32_constant(graph, init_id) {
        Some(v) => v,
        None => return false,
    };

    let back_edge_input = iv_phi_inputs[back_edge_pred_pos];
    let step_val = match find_increment_step(graph, back_edge_input, iv_phi_id) {
        Some(s) if s > 0 => s,
        _ => return false,
    };

    // ── 3. Find Int32Multiply(iv_phi, K) in the loop body ───────────────
    // Deduplicate by K: multiple `i*3` uses share one derived IV.
    let mut mul_targets: HashMap<i32, Vec<NodeId>> = HashMap::new();
    {
        let body = graph.block(body_block_idx).unwrap();
        for (nid, node) in &body.nodes {
            if let ValueNode::Int32Multiply { left, right } = node {
                let (const_input, is_iv) = if *left == iv_phi_id {
                    (*right, true)
                } else if *right == iv_phi_id {
                    (*left, true)
                } else {
                    (NodeId(0), false)
                };
                if !is_iv {
                    continue;
                }
                if let Some(k) = find_i32_constant(graph, const_input)
                    && k != 0
                    && k != 1
                {
                    mul_targets.entry(k).or_default().push(*nid);
                }
            }
        }
    }

    if mul_targets.is_empty() {
        return false;
    }

    // ── 4. Create derived IVs ───────────────────────────────────────────
    let mut subst: HashMap<NodeId, NodeId> = HashMap::new();

    for (k, mul_ids) in &mul_targets {
        let derived_init = init_val.wrapping_mul(*k);
        let derived_step = step_val.wrapping_mul(*k);

        // Allocate node IDs.
        let init_const_id = graph.alloc_node_id();
        let step_const_id = graph.alloc_node_id();
        let derived_phi_id = graph.alloc_node_id();
        let derived_add_id = graph.alloc_node_id();

        // Add constants to preheader.
        let preheader = graph.block_mut(lp.preheader).unwrap();
        preheader.push_with_id(
            init_const_id,
            ValueNode::Int32Constant {
                value: derived_init,
            },
        );
        preheader.push_with_id(
            step_const_id,
            ValueNode::Int32Constant {
                value: derived_step,
            },
        );

        // Add derived Phi to header — insert among existing Phis.
        let mut phi_inputs = vec![NodeId(0); iv_phi_inputs.len()];
        phi_inputs[preheader_pred_pos] = init_const_id;
        phi_inputs[back_edge_pred_pos] = derived_add_id;
        {
            let header = graph.block_mut(header_idx).unwrap();
            // Find position after last existing Phi.
            let insert_pos = header
                .nodes
                .iter()
                .position(|(_, n)| !matches!(n, ValueNode::Phi { .. }))
                .unwrap_or(header.nodes.len());
            header.nodes.insert(
                insert_pos,
                (derived_phi_id, ValueNode::Phi { inputs: phi_inputs }),
            );
        }

        // Add Int32Add to body (before any existing back-edge increment).
        let body = graph.block_mut(body_block_idx).unwrap();
        body.push_with_id(
            derived_add_id,
            ValueNode::Int32Add {
                left: derived_phi_id,
                right: step_const_id,
            },
        );

        // Map all multiply IDs to the derived Phi.
        for mul_id in mul_ids {
            subst.insert(*mul_id, derived_phi_id);
        }
    }

    // ── 5. Replace all references to old multiplies ─────────────────────
    for block in graph.blocks_mut() {
        apply_subst_to_block(block, &subst);
    }

    true
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compiler::maglev::ir::{BasicBlock, ControlNode, MaglevGraph, ValueNode};

    // ── Helpers ───────────────────────────────────────────────────────────────

    /// Count the total number of value nodes across all blocks.
    fn total_node_count(graph: &MaglevGraph) -> usize {
        graph.blocks().iter().map(|b| b.nodes.len()).sum()
    }

    /// Return the single value node in block 0 at position `pos`.
    fn node_at(graph: &MaglevGraph, pos: usize) -> &ValueNode {
        &graph.blocks()[0].nodes[pos].1
    }

    fn count_store_globals(graph: &MaglevGraph) -> usize {
        graph
            .blocks()
            .iter()
            .flat_map(|block| block.nodes.iter())
            .filter(|(_, node)| matches!(node, ValueNode::StoreGlobal { .. }))
            .count()
    }

    fn count_store_global_name(graph: &MaglevGraph, expected_name: u32) -> usize {
        graph
            .blocks()
            .iter()
            .flat_map(|block| block.nodes.iter())
            .filter(|(_, node)| {
                matches!(node, ValueNode::StoreGlobal { name, .. } if *name == expected_name)
            })
            .count()
    }

    fn graph_has_header_phi(graph: &MaglevGraph, header: u32) -> bool {
        graph
            .block(header)
            .is_some_and(|block| matches!(block.nodes.first(), Some((_, ValueNode::Phi { .. }))))
    }

    #[test]
    fn test_promote_loop_globals_unconditional_loop() {
        let mut graph = MaglevGraph::new(0);
        for id in 0..4 {
            graph.add_block(BasicBlock::new(id));
        }

        let init = graph
            .add_value_node(0, ValueNode::SmiConstant { value: 0 })
            .unwrap();
        graph
            .add_value_node(
                0,
                ValueNode::StoreGlobal {
                    name: 7,
                    value: init,
                    feedback_slot: 0,
                },
            )
            .unwrap();
        graph
            .block_mut(0)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        graph.block_mut(1).unwrap().add_predecessor(0);
        graph.block_mut(1).unwrap().add_predecessor(2);
        let cond = graph.add_value_node(1, ValueNode::TrueConstant).unwrap();
        graph
            .block_mut(1)
            .unwrap()
            .set_control(ControlNode::Branch {
                condition: cond,
                if_true: 2,
                if_false: 3,
            });

        graph.block_mut(2).unwrap().add_predecessor(1);
        let load = graph
            .add_value_node(
                2,
                ValueNode::LoadGlobal {
                    name: 7,
                    feedback_slot: 0,
                },
            )
            .unwrap();
        let inc = graph
            .add_value_node(2, ValueNode::Int32Increment { value: load })
            .unwrap();
        graph
            .add_value_node(
                2,
                ValueNode::StoreGlobal {
                    name: 7,
                    value: inc,
                    feedback_slot: 0,
                },
            )
            .unwrap();
        graph
            .block_mut(2)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        graph.block_mut(3).unwrap().add_predecessor(1);
        let undef = graph
            .add_value_node(3, ValueNode::UndefinedConstant)
            .unwrap();
        graph
            .block_mut(3)
            .unwrap()
            .set_control(ControlNode::Return { value: undef });

        assert_eq!(promote_loop_globals_counted(&mut graph), 1);
        assert!(graph_has_header_phi(&graph, 1));
    }

    #[test]
    fn test_promote_loop_globals_excludes_conditional_stores() {
        let mut graph = MaglevGraph::new(0);
        for id in 0..6 {
            graph.add_block(BasicBlock::new(id));
        }

        let init = graph
            .add_value_node(0, ValueNode::SmiConstant { value: 0 })
            .unwrap();
        graph
            .add_value_node(
                0,
                ValueNode::StoreGlobal {
                    name: 8,
                    value: init,
                    feedback_slot: 0,
                },
            )
            .unwrap();
        graph
            .block_mut(0)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        graph.block_mut(1).unwrap().add_predecessor(0);
        graph.block_mut(1).unwrap().add_predecessor(4);
        let header_cond = graph.add_value_node(1, ValueNode::TrueConstant).unwrap();
        graph
            .block_mut(1)
            .unwrap()
            .set_control(ControlNode::Branch {
                condition: header_cond,
                if_true: 2,
                if_false: 5,
            });

        graph.block_mut(2).unwrap().add_predecessor(1);
        let body_cond = graph.add_value_node(2, ValueNode::TrueConstant).unwrap();
        graph
            .block_mut(2)
            .unwrap()
            .set_control(ControlNode::Branch {
                condition: body_cond,
                if_true: 3,
                if_false: 4,
            });

        graph.block_mut(3).unwrap().add_predecessor(2);
        graph
            .add_value_node(
                3,
                ValueNode::StoreGlobal {
                    name: 9,
                    value: init,
                    feedback_slot: 0,
                },
            )
            .unwrap();
        graph
            .block_mut(3)
            .unwrap()
            .set_control(ControlNode::Jump { target: 4 });

        graph.block_mut(4).unwrap().add_predecessor(2);
        graph.block_mut(4).unwrap().add_predecessor(3);
        graph
            .add_value_node(
                4,
                ValueNode::StoreGlobal {
                    name: 8,
                    value: init,
                    feedback_slot: 0,
                },
            )
            .unwrap();
        graph
            .block_mut(4)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        graph.block_mut(5).unwrap().add_predecessor(1);
        let undef = graph
            .add_value_node(5, ValueNode::UndefinedConstant)
            .unwrap();
        graph
            .block_mut(5)
            .unwrap()
            .set_control(ControlNode::Return { value: undef });

        let stores_before = count_store_globals(&graph);
        assert_eq!(promote_loop_globals_counted(&mut graph), 1);
        assert_eq!(count_store_globals(&graph), stores_before);
        assert_eq!(count_store_global_name(&graph, 9), 1);
        assert!(graph_has_header_phi(&graph, 1));
    }

    // ── Constant folding — Int32 ──────────────────────────────────────────────

    #[test]
    fn test_fold_int32_add() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c3 = block.push_value(ValueNode::Int32Constant { value: 3 });
        let c4 = block.push_value(ValueNode::Int32Constant { value: 4 });
        let add = block.push_value(ValueNode::Int32Add {
            left: c3,
            right: c4,
        });
        block.set_control(ControlNode::Return { value: add });
        graph.add_block(block);

        optimize(&mut graph);

        // The Int32Add node must have been folded to Int32Constant(7).
        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 7 });
    }

    #[test]
    fn test_fold_int32_subtract() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c10 = block.push_value(ValueNode::Int32Constant { value: 10 });
        let c3 = block.push_value(ValueNode::Int32Constant { value: 3 });
        let sub = block.push_value(ValueNode::Int32Subtract {
            left: c10,
            right: c3,
        });
        block.set_control(ControlNode::Return { value: sub });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 7 });
    }

    #[test]
    fn test_fold_int32_multiply() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c6 = block.push_value(ValueNode::Int32Constant { value: 6 });
        let c7 = block.push_value(ValueNode::Int32Constant { value: 7 });
        let mul = block.push_value(ValueNode::Int32Multiply {
            left: c6,
            right: c7,
        });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 42 });
    }

    #[test]
    fn test_fold_int32_divide() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c12 = block.push_value(ValueNode::Int32Constant { value: 12 });
        let c4 = block.push_value(ValueNode::Int32Constant { value: 4 });
        let div = block.push_value(ValueNode::Int32Divide {
            left: c12,
            right: c4,
        });
        block.set_control(ControlNode::Return { value: div });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 3 });
    }

    #[test]
    fn test_fold_int32_divide_by_zero_not_folded() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c5 = block.push_value(ValueNode::Int32Constant { value: 5 });
        let c0 = block.push_value(ValueNode::Int32Constant { value: 0 });
        let div = block.push_value(ValueNode::Int32Divide {
            left: c5,
            right: c0,
        });
        block.set_control(ControlNode::Return { value: div });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // Div-by-zero must NOT be folded (the node stays as Int32Divide).
        assert!(matches!(node_at(&graph, 2), ValueNode::Int32Divide { .. }));
        // Node count should not decrease (DCE keeps the divide because its
        // result is consumed by the Return).
        assert_eq!(total_node_count(&graph), before);
    }

    #[test]
    fn test_fold_int32_modulus() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c10 = block.push_value(ValueNode::Int32Constant { value: 10 });
        let c3 = block.push_value(ValueNode::Int32Constant { value: 3 });
        let rem = block.push_value(ValueNode::Int32Modulus {
            left: c10,
            right: c3,
        });
        block.set_control(ControlNode::Return { value: rem });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 1 });
    }

    #[test]
    fn test_fold_int32_negate() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c5 = block.push_value(ValueNode::Int32Constant { value: 5 });
        let neg = block.push_value(ValueNode::Int32Negate { value: c5 });
        block.set_control(ControlNode::Return { value: neg });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: -5 });
    }

    #[test]
    fn test_fold_int32_increment() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c9 = block.push_value(ValueNode::Int32Constant { value: 9 });
        let inc = block.push_value(ValueNode::Int32Increment { value: c9 });
        block.set_control(ControlNode::Return { value: inc });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 10 });
    }

    #[test]
    fn test_fold_int32_decrement() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c10 = block.push_value(ValueNode::Int32Constant { value: 10 });
        let dec = block.push_value(ValueNode::Int32Decrement { value: c10 });
        block.set_control(ControlNode::Return { value: dec });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 9 });
    }

    // ── Constant folding — CheckedSmi variants ────────────────────────────────

    #[test]
    fn test_fold_checked_smi_add() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let a = block.push_value(ValueNode::SmiConstant { value: 2 });
        let b = block.push_value(ValueNode::SmiConstant { value: 3 });
        let sum = block.push_value(ValueNode::CheckedSmiAdd { left: a, right: b });
        block.set_control(ControlNode::Return { value: sum });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 5 });
    }

    #[test]
    fn test_fold_checked_smi_increment() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c = block.push_value(ValueNode::SmiConstant { value: 41 });
        let inc = block.push_value(ValueNode::CheckedSmiIncrement { value: c });
        block.set_control(ControlNode::Return { value: inc });
        graph.add_block(block);

        optimize(&mut graph);

        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 42 });
    }

    // ── Constant folding — Float64 ────────────────────────────────────────────

    #[test]
    fn test_fold_float64_add() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let x = block.push_value(ValueNode::Float64Constant { value: 1.5 });
        let y = block.push_value(ValueNode::Float64Constant { value: 2.5 });
        let add = block.push_value(ValueNode::Float64Add { left: x, right: y });
        block.set_control(ControlNode::Return { value: add });
        graph.add_block(block);

        optimize(&mut graph);

        if let ValueNode::Float64Constant { value } = node_at(&graph, 0) {
            assert!((value - 4.0).abs() < f64::EPSILON);
        } else {
            panic!("expected Float64Constant after folding");
        }
    }

    #[test]
    fn test_fold_float64_multiply() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let x = block.push_value(ValueNode::Float64Constant { value: 3.0 });
        let y = block.push_value(ValueNode::Float64Constant { value: 4.0 });
        let mul = block.push_value(ValueNode::Float64Multiply { left: x, right: y });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        if let ValueNode::Float64Constant { value } = node_at(&graph, 0) {
            assert!((value - 12.0).abs() < f64::EPSILON);
        } else {
            panic!("expected Float64Constant after folding");
        }
    }

    #[test]
    fn test_fold_float64_negate() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let x = block.push_value(ValueNode::Float64Constant { value: 2.5 });
        let neg = block.push_value(ValueNode::Float64Negate { value: x });
        block.set_control(ControlNode::Return { value: neg });
        graph.add_block(block);

        optimize(&mut graph);

        if let ValueNode::Float64Constant { value } = node_at(&graph, 0) {
            assert!((value - (-2.5)).abs() < f64::EPSILON);
        } else {
            panic!("expected Float64Constant after folding");
        }
    }

    // ── Constant folding — no fold when operand is dynamic ────────────────────

    #[test]
    fn test_no_fold_when_operand_is_parameter() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c = block.push_value(ValueNode::Int32Constant { value: 1 });
        let add = block.push_value(ValueNode::Int32Add { left: p, right: c });
        block.set_control(ControlNode::Return { value: add });
        graph.add_block(block);

        optimize(&mut graph);

        // The add node should remain unchanged.
        assert!(matches!(node_at(&graph, 2), ValueNode::Int32Add { .. }));
    }

    // ── Dead-code elimination ─────────────────────────────────────────────────

    #[test]
    fn test_dce_removes_unused_pure_node() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        // This constant is never used.
        block.push_value(ValueNode::Int32Constant { value: 99 });
        // This one is returned.
        let ret_val = block.push_value(ValueNode::SmiConstant { value: 0 });
        block.set_control(ControlNode::Return { value: ret_val });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // The unused constant should have been removed.
        assert!(total_node_count(&graph) < before);
    }

    #[test]
    fn test_dce_keeps_side_effect_node() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let obj = block.push_value(ValueNode::Parameter { index: 0 });
        let val = block.push_value(ValueNode::Int32Constant { value: 1 });
        // Store has a side-effect: must be kept even if nobody reads its result.
        block.push_value(ValueNode::StoreField {
            object: obj,
            offset: 8,
            value: val,
        });
        let ret_val = block.push_value(ValueNode::UndefinedConstant);
        block.set_control(ControlNode::Return { value: ret_val });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // Store must not be removed.
        assert_eq!(total_node_count(&graph), before);
    }

    #[test]
    fn test_dce_keeps_check_maps_node() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        // CheckMaps is a guard with side-effects (deopt on failure).
        let _check = block.push_value(ValueNode::CheckMaps {
            receiver: p,
            feedback_slot: 0,
        });
        let field = block.push_value(ValueNode::LoadField {
            object: p,
            offset: 8,
        });
        block.set_control(ControlNode::Return { value: field });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // CheckMaps must survive DCE.
        assert_eq!(total_node_count(&graph), before);
    }

    // ── Redundant CheckMaps removal ───────────────────────────────────────────

    #[test]
    fn test_redundant_check_maps_removed() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        // First check — canonical.
        let _check1 = block.push_value(ValueNode::CheckMaps {
            receiver: p,
            feedback_slot: 0,
        });
        // Second identical check — redundant.
        let _check2 = block.push_value(ValueNode::CheckMaps {
            receiver: p,
            feedback_slot: 0,
        });
        let field = block.push_value(ValueNode::LoadField {
            object: p,
            offset: 8,
        });
        block.set_control(ControlNode::Return { value: field });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // The redundant CheckMaps gets replaced by UndefinedConstant; DCE then
        // removes it (it's pure and unused).  Net effect: node count decreases.
        assert!(total_node_count(&graph) < before);
    }

    #[test]
    fn test_different_receivers_both_kept() {
        let mut graph = MaglevGraph::new(2);
        let mut block = BasicBlock::new(0);
        let p0 = block.push_value(ValueNode::Parameter { index: 0 });
        let p1 = block.push_value(ValueNode::Parameter { index: 1 });
        // Two checks with *different* receivers — both must be kept.
        block.push_value(ValueNode::CheckMaps {
            receiver: p0,
            feedback_slot: 0,
        });
        block.push_value(ValueNode::CheckMaps {
            receiver: p1,
            feedback_slot: 0,
        });
        let f0 = block.push_value(ValueNode::LoadField {
            object: p0,
            offset: 8,
        });
        let f1 = block.push_value(ValueNode::LoadField {
            object: p1,
            offset: 8,
        });
        let add = block.push_value(ValueNode::Int32Add {
            left: f0,
            right: f1,
        });
        block.set_control(ControlNode::Return { value: add });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // Neither check is redundant — count must not decrease.
        assert_eq!(total_node_count(&graph), before);
    }

    #[test]
    fn test_different_slots_both_kept() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        // Same receiver, different feedback slots — both must be kept.
        block.push_value(ValueNode::CheckMaps {
            receiver: p,
            feedback_slot: 0,
        });
        block.push_value(ValueNode::CheckMaps {
            receiver: p,
            feedback_slot: 1,
        });
        let f = block.push_value(ValueNode::LoadField {
            object: p,
            offset: 8,
        });
        block.set_control(ControlNode::Return { value: f });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        assert_eq!(total_node_count(&graph), before);
    }

    // ── Combined passes ───────────────────────────────────────────────────────

    #[test]
    fn test_combined_fold_and_dce_reduces_node_count() {
        // Build: return (3 + 4)
        // After constant folding → return Int32Constant(7)
        // After DCE → constants 3 and 4 are dead.
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c3 = block.push_value(ValueNode::Int32Constant { value: 3 });
        let c4 = block.push_value(ValueNode::Int32Constant { value: 4 });
        let add = block.push_value(ValueNode::Int32Add {
            left: c3,
            right: c4,
        });
        block.set_control(ControlNode::Return { value: add });
        graph.add_block(block);

        let before = total_node_count(&graph); // 3 nodes
        optimize(&mut graph);
        let after = total_node_count(&graph);
        // We expect only Int32Constant(7) to remain; 3 and 4 are dead.
        assert!(after < before, "expected fewer nodes after optimisation");
        // Exactly one node should survive (the folded result).
        assert_eq!(after, 1);
        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 7 });
    }

    #[test]
    fn test_chain_folding() {
        // (1 + 2) * (3 + 4) = 3 * 7 = 21
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let c1 = block.push_value(ValueNode::Int32Constant { value: 1 });
        let c2 = block.push_value(ValueNode::Int32Constant { value: 2 });
        let c3 = block.push_value(ValueNode::Int32Constant { value: 3 });
        let c4 = block.push_value(ValueNode::Int32Constant { value: 4 });
        let add1 = block.push_value(ValueNode::Int32Add {
            left: c1,
            right: c2,
        });
        let add2 = block.push_value(ValueNode::Int32Add {
            left: c3,
            right: c4,
        });
        let mul = block.push_value(ValueNode::Int32Multiply {
            left: add1,
            right: add2,
        });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        // All intermediates should be DCE'd; only one node left.
        assert_eq!(total_node_count(&graph), 1);
        assert_eq!(node_at(&graph, 0), &ValueNode::Int32Constant { value: 21 });
    }

    // ── Strength reduction ────────────────────────────────────────────────────

    #[test]
    fn test_strength_reduce_multiply_by_2() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c2 = block.push_value(ValueNode::Int32Constant { value: 2 });
        let mul = block.push_value(ValueNode::Int32Multiply { left: p, right: c2 });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        // The multiply should have been replaced with a shift-left.
        let has_shift = graph.blocks()[0]
            .nodes
            .iter()
            .any(|(_, n)| matches!(n, ValueNode::Int32ShiftLeft { .. }));
        assert!(
            has_shift,
            "expected Int32ShiftLeft after strength reduction"
        );
    }

    #[test]
    fn test_strength_reduce_multiply_by_4() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c4 = block.push_value(ValueNode::Int32Constant { value: 4 });
        let mul = block.push_value(ValueNode::Int32Multiply { left: p, right: c4 });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        // The shift amount should be 2 (since 4 == 2^2).
        let shift_const = graph.blocks()[0].nodes.iter().find_map(|(_, n)| {
            if let ValueNode::Int32ShiftLeft { right, .. } = n {
                // Find the constant node that `right` refers to.
                graph.blocks()[0].nodes.iter().find_map(|(id, cn)| {
                    if *id == *right {
                        if let ValueNode::Int32Constant { value } = cn {
                            Some(*value)
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                })
            } else {
                None
            }
        });
        assert_eq!(shift_const, Some(2), "shift amount should be 2 for * 4");
    }

    #[test]
    fn test_strength_reduce_not_applied_to_non_decomposable() {
        // Multiply by 6 is neither a power of two nor 2^k±1 — node should stay.
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c6 = block.push_value(ValueNode::Int32Constant { value: 6 });
        let mul = block.push_value(ValueNode::Int32Multiply { left: p, right: c6 });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        // Multiply by 6 is not a power of two nor 2^k±1 — node should stay as multiply.
        assert!(
            graph.blocks()[0]
                .nodes
                .iter()
                .any(|(_, n)| matches!(n, ValueNode::Int32Multiply { .. }))
        );
    }

    #[test]
    fn test_strength_reduce_multiply_by_1_simplified() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c1 = block.push_value(ValueNode::Int32Constant { value: 1 });
        let mul = block.push_value(ValueNode::Int32Multiply { left: p, right: c1 });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        // Multiply by 1 is identity-simplified (x * 1 → x), so the multiply
        // node should no longer be present.
        let has_mul = graph.blocks()[0]
            .nodes
            .iter()
            .any(|(_, n)| matches!(n, ValueNode::Int32Multiply { .. }));
        assert!(
            !has_mul,
            "expected Int32Multiply(x, 1) to be identity-simplified away"
        );
    }

    // ── Redundant type-guard elimination ──────────────────────────────────────

    #[test]
    fn test_eliminate_check_smi_on_smi_constant() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let smi = block.push_value(ValueNode::SmiConstant { value: 42 });
        let _check = block.push_value(ValueNode::CheckSmi { receiver: smi });
        block.set_control(ControlNode::Return { value: smi });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // The CheckSmi on a SmiConstant should be eliminated (replaced then
        // cleaned up by DCE).
        assert!(total_node_count(&graph) < before);
    }

    #[test]
    fn test_eliminate_duplicate_check_smi_same_block() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let _c1 = block.push_value(ValueNode::CheckSmi { receiver: p });
        let _c2 = block.push_value(ValueNode::CheckSmi { receiver: p });
        block.set_control(ControlNode::Return { value: p });
        graph.add_block(block);

        optimize(&mut graph);

        // At most one CheckSmi should survive (the first one).
        let check_count = graph.blocks()[0]
            .nodes
            .iter()
            .filter(|(_, n)| matches!(n, ValueNode::CheckSmi { .. }))
            .count();
        assert!(
            check_count <= 1,
            "expected at most 1 CheckSmi, got {check_count}"
        );
    }

    // ── Algebraic identity simplification ─────────────────────────────────────

    #[test]
    fn test_identity_add_zero_right() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c0 = block.push_value(ValueNode::Int32Constant { value: 0 });
        let add = block.push_value(ValueNode::Int32Add { left: p, right: c0 });
        block.set_control(ControlNode::Return { value: add });
        graph.add_block(block);

        optimize(&mut graph);

        // x + 0 → x; the add should be eliminated and return uses the param.
        let has_add = graph.blocks()[0]
            .nodes
            .iter()
            .any(|(_, n)| matches!(n, ValueNode::Int32Add { .. }));
        assert!(!has_add, "expected Int32Add to be simplified away");
    }

    #[test]
    fn test_identity_multiply_by_one() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c1 = block.push_value(ValueNode::Int32Constant { value: 1 });
        let mul = block.push_value(ValueNode::Int32Multiply { left: p, right: c1 });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        let has_mul = graph.blocks()[0]
            .nodes
            .iter()
            .any(|(_, n)| matches!(n, ValueNode::Int32Multiply { .. }));
        assert!(!has_mul, "expected Int32Multiply(x, 1) to be simplified");
    }

    #[test]
    fn test_identity_multiply_by_zero() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c0 = block.push_value(ValueNode::Int32Constant { value: 0 });
        let mul = block.push_value(ValueNode::Int32Multiply { left: p, right: c0 });
        block.set_control(ControlNode::Return { value: mul });
        graph.add_block(block);

        optimize(&mut graph);

        let has_mul = graph.blocks()[0]
            .nodes
            .iter()
            .any(|(_, n)| matches!(n, ValueNode::Int32Multiply { .. }));
        assert!(!has_mul, "expected Int32Multiply(x, 0) to be simplified");
    }

    #[test]
    fn test_identity_subtract_zero() {
        let mut graph = MaglevGraph::new(1);
        let mut block = BasicBlock::new(0);
        let p = block.push_value(ValueNode::Parameter { index: 0 });
        let c0 = block.push_value(ValueNode::Int32Constant { value: 0 });
        let sub = block.push_value(ValueNode::Int32Subtract { left: p, right: c0 });
        block.set_control(ControlNode::Return { value: sub });
        graph.add_block(block);

        optimize(&mut graph);

        let has_sub = graph.blocks()[0]
            .nodes
            .iter()
            .any(|(_, n)| matches!(n, ValueNode::Int32Subtract { .. }));
        assert!(!has_sub, "expected Int32Subtract(x, 0) to be simplified");
    }

    // ── Common subexpression elimination ──────────────────────────────────────

    #[test]
    fn test_cse_duplicate_add_eliminated() {
        let mut graph = MaglevGraph::new(2);
        let mut block = BasicBlock::new(0);
        let p0 = block.push_value(ValueNode::Parameter { index: 0 });
        let p1 = block.push_value(ValueNode::Parameter { index: 1 });
        let add1 = block.push_value(ValueNode::Int32Add {
            left: p0,
            right: p1,
        });
        let add2 = block.push_value(ValueNode::Int32Add {
            left: p0,
            right: p1,
        });
        let sum = block.push_value(ValueNode::Int32Add {
            left: add1,
            right: add2,
        });
        block.set_control(ControlNode::Return { value: sum });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // The duplicate add should have been eliminated.
        assert!(
            total_node_count(&graph) < before,
            "expected CSE to reduce node count"
        );
    }

    #[test]
    fn test_cse_different_ops_not_eliminated() {
        let mut graph = MaglevGraph::new(2);
        let mut block = BasicBlock::new(0);
        let p0 = block.push_value(ValueNode::Parameter { index: 0 });
        let p1 = block.push_value(ValueNode::Parameter { index: 1 });
        let add = block.push_value(ValueNode::Int32Add {
            left: p0,
            right: p1,
        });
        let sub = block.push_value(ValueNode::Int32Subtract {
            left: p0,
            right: p1,
        });
        let result = block.push_value(ValueNode::Int32Add {
            left: add,
            right: sub,
        });
        block.set_control(ControlNode::Return { value: result });
        graph.add_block(block);

        let before = total_node_count(&graph);
        optimize(&mut graph);
        // Different operations on same inputs — no CSE should occur.
        assert_eq!(
            total_node_count(&graph),
            before,
            "different ops should not be CSE'd"
        );
    }

    #[test]
    fn test_eliminate_check_smi_on_checked_smi_add_result() {
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let a = block.push_value(ValueNode::SmiConstant { value: 1 });
        let b = block.push_value(ValueNode::SmiConstant { value: 2 });
        let sum = block.push_value(ValueNode::CheckedSmiAdd { left: a, right: b });
        let _check = block.push_value(ValueNode::CheckSmi { receiver: sum });
        block.set_control(ControlNode::Return { value: sum });
        graph.add_block(block);

        optimize(&mut graph);

        // The CheckSmi on the output of CheckedSmiAdd should be eliminated.
        let check_count = graph.blocks()[0]
            .nodes
            .iter()
            .filter(|(_, n)| matches!(n, ValueNode::CheckSmi { .. }))
            .count();
        assert_eq!(
            check_count, 0,
            "CheckSmi on CheckedSmiAdd output should be eliminated"
        );
    }

    // ── Trivial Phi elimination ──────────────────────────────────────────────

    #[test]
    fn test_trivial_phi_self_referencing_eliminated() {
        // Simulate a loop-header identity Phi: Phi(X, self) → should become X.
        //
        //   Block 0 (preheader):
        //     NodeId(0) = CreateEmptyObjectLiteral   ← the "root" object
        //     → Jump to block 1
        //
        //   Block 1 (loop header):
        //     NodeId(1) = Phi([NodeId(0), NodeId(1)])  ← trivial!
        //     NodeId(2) = LoadNamedGeneric(NodeId(1), "a")
        //     → Return NodeId(2)
        //
        // After trivial-Phi elimination, NodeId(2) should reference NodeId(0)
        // directly, and the Phi should be dead.
        let mut graph = MaglevGraph::new(0);

        // Block 0: preheader
        let mut b0 = BasicBlock::new(0);
        let root = graph.alloc_node_id(); // NodeId(0)
        b0.push_with_id(root, ValueNode::CreateEmptyObjectLiteral);
        b0.set_control(ControlNode::Jump { target: 1 });
        graph.add_block(b0);

        // Block 1: loop header with identity Phi
        let mut b1 = BasicBlock::new(1);
        b1.predecessors = vec![0, 1]; // entry + back-edge
        b1.is_loop_header = true;
        let phi_id = graph.alloc_node_id(); // NodeId(1)
        b1.push_with_id(
            phi_id,
            ValueNode::Phi {
                inputs: vec![root, phi_id],
            },
        );
        let load = graph.alloc_node_id(); // NodeId(2)
        b1.push_with_id(
            load,
            ValueNode::LoadNamedGeneric {
                object: phi_id,
                name: 0,
                feedback_slot: 0,
            },
        );
        b1.set_control(ControlNode::Return { value: load });
        graph.add_block(b1);

        // Run the full optimizer (which includes trivial Phi elimination).
        optimize(&mut graph);

        // The LoadNamedGeneric should now reference the preheader's root
        // (NodeId(0)) directly, not the Phi (NodeId(1)).
        let load_node = graph.blocks()[1]
            .nodes
            .iter()
            .find(|(_, n)| matches!(n, ValueNode::LoadNamedGeneric { .. }));
        if let Some((_, ValueNode::LoadNamedGeneric { object, .. })) = load_node {
            assert_eq!(
                *object, root,
                "LoadNamedGeneric should reference preheader root after Phi elimination"
            );
        } else {
            // The load might have been moved to block 0 by LICM (even better!)
            let load_in_b0 = graph.blocks()[0]
                .nodes
                .iter()
                .any(|(_, n)| matches!(n, ValueNode::LoadNamedGeneric { .. }));
            assert!(
                load_in_b0,
                "LoadNamedGeneric should be hoisted to preheader by LICM"
            );
        }
    }

    #[test]
    fn test_trivial_phi_non_trivial_preserved() {
        // A real Phi with two distinct non-self inputs should NOT be eliminated.
        //
        //   Block 0: NodeId(0) = SmiConstant(0)  → Jump to block 1
        //   Block 1: NodeId(2) = Phi([NodeId(0), NodeId(1)])
        //            NodeId(1) = GenericIncrement(NodeId(2))
        //            → Return NodeId(2)
        let mut graph = MaglevGraph::new(0);

        let mut b0 = BasicBlock::new(0);
        let zero = graph.alloc_node_id();
        b0.push_with_id(zero, ValueNode::SmiConstant { value: 0 });
        b0.set_control(ControlNode::Jump { target: 1 });
        graph.add_block(b0);

        let mut b1 = BasicBlock::new(1);
        b1.predecessors = vec![0, 1];
        b1.is_loop_header = true;
        let phi_id = graph.alloc_node_id();
        let inc_id = graph.alloc_node_id();
        b1.push_with_id(
            phi_id,
            ValueNode::Phi {
                inputs: vec![zero, inc_id],
            },
        );
        b1.push_with_id(
            inc_id,
            ValueNode::GenericIncrement {
                value: phi_id,
                feedback_slot: 0,
            },
        );
        b1.set_control(ControlNode::Return { value: phi_id });
        graph.add_block(b1);

        optimize(&mut graph);

        // The Phi should still exist (it's not trivial — two distinct inputs).
        let phi_exists = graph
            .blocks()
            .iter()
            .flat_map(|b| b.nodes.iter())
            .any(|(_, n)| matches!(n, ValueNode::Phi { .. }));
        assert!(phi_exists, "Non-trivial Phi should be preserved");
    }

    // ── Store-to-load forwarding ──────────────────────────────────────────────

    #[test]
    fn test_store_to_load_forwarding_basic() {
        // StoreNamedGeneric(obj, "x", val) followed by LoadNamedGeneric(obj, "x")
        // should forward val, eliminating the load.
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let obj = block.push_value(ValueNode::Parameter { index: 0 });
        let val = block.push_value(ValueNode::SmiConstant { value: 42 });
        let _store = block.push_value(ValueNode::StoreNamedGeneric {
            object: obj,
            name: 5,
            value: val,
            feedback_slot: 0,
        });
        let load = block.push_value(ValueNode::LoadNamedGeneric {
            object: obj,
            name: 5,
            feedback_slot: 1,
        });
        block.set_control(ControlNode::Return { value: load });
        graph.add_block(block);

        eliminate_store_to_load(&mut graph);

        // The return should now reference val directly, not the load.
        match &graph.blocks()[0].control {
            Some(ControlNode::Return { value }) => {
                assert_eq!(*value, val, "Load should be forwarded to stored value");
            }
            other => panic!("Expected Return, got {:?}", other),
        }
    }

    #[test]
    fn test_store_to_load_forwarding_fused_create() {
        // CreateObjectLiteralWithProperties seeds the map so subsequent loads
        // are forwarded.
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let val_x = block.push_value(ValueNode::SmiConstant { value: 10 });
        let val_y = block.push_value(ValueNode::SmiConstant { value: 20 });
        let obj = block.push_value(ValueNode::CreateObjectLiteralWithProperties {
            feedback_slot: 0,
            flags: 0,
            names: vec![1, 2],
            values: vec![val_x, val_y],
        });
        let load_x = block.push_value(ValueNode::LoadNamedGeneric {
            object: obj,
            name: 1,
            feedback_slot: 1,
        });
        let load_y = block.push_value(ValueNode::LoadNamedGeneric {
            object: obj,
            name: 2,
            feedback_slot: 2,
        });
        let sum = block.push_value(ValueNode::Int32Add {
            left: load_x,
            right: load_y,
        });
        block.set_control(ControlNode::Return { value: sum });
        graph.add_block(block);

        eliminate_store_to_load(&mut graph);

        // The Int32Add should now reference val_x and val_y directly.
        let add_node = graph.blocks()[0]
            .nodes
            .iter()
            .find(|(_, n)| matches!(n, ValueNode::Int32Add { .. }));
        match add_node {
            Some((_, ValueNode::Int32Add { left, right })) => {
                assert_eq!(*left, val_x, "load_x should be forwarded to val_x");
                assert_eq!(*right, val_y, "load_y should be forwarded to val_y");
            }
            other => panic!("Expected Int32Add, got {:?}", other),
        }
    }

    #[test]
    fn test_store_to_load_invalidated_by_call() {
        // A Call between store and load should prevent forwarding.
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let obj = block.push_value(ValueNode::Parameter { index: 0 });
        let val = block.push_value(ValueNode::SmiConstant { value: 42 });
        let _store = block.push_value(ValueNode::StoreNamedGeneric {
            object: obj,
            name: 5,
            value: val,
            feedback_slot: 0,
        });
        // A call could mutate obj's properties.
        let _call = block.push_value(ValueNode::Call {
            callee: obj,
            args: vec![],
            receiver: obj,
            feedback_slot: 0,
        });
        let load = block.push_value(ValueNode::LoadNamedGeneric {
            object: obj,
            name: 5,
            feedback_slot: 1,
        });
        block.set_control(ControlNode::Return { value: load });
        graph.add_block(block);

        eliminate_store_to_load(&mut graph);

        // The return should still reference the load (not forwarded).
        match &graph.blocks()[0].control {
            Some(ControlNode::Return { value }) => {
                assert_eq!(*value, load, "Load should NOT be forwarded past a call");
            }
            other => panic!("Expected Return, got {:?}", other),
        }
    }

    #[test]
    fn test_dead_allocation_elimination_after_forwarding() {
        // After store-to-load forwarding eliminates all loads from an object,
        // the allocation itself becomes dead and should be replaced.
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let val_x = block.push_value(ValueNode::SmiConstant { value: 10 });
        let val_y = block.push_value(ValueNode::SmiConstant { value: 20 });
        let obj = block.push_value(ValueNode::CreateObjectLiteralWithProperties {
            feedback_slot: 0,
            flags: 0,
            names: vec![1, 2],
            values: vec![val_x, val_y],
        });
        let load_x = block.push_value(ValueNode::LoadNamedGeneric {
            object: obj,
            name: 1,
            feedback_slot: 1,
        });
        let load_y = block.push_value(ValueNode::LoadNamedGeneric {
            object: obj,
            name: 2,
            feedback_slot: 2,
        });
        let sum = block.push_value(ValueNode::Int32Add {
            left: load_x,
            right: load_y,
        });
        block.set_control(ControlNode::Return { value: sum });
        graph.add_block(block);

        // Forward loads first.
        eliminate_store_to_load(&mut graph);
        // Now the allocation should be dead (only loads referenced it).
        eliminate_dead_allocations(&mut graph);

        // The CreateObjectLiteralWithProperties should be replaced with
        // UndefinedConstant since nothing references it anymore.
        let obj_node = graph.blocks()[0].nodes.iter().find(|(id, _)| *id == obj);
        match obj_node {
            Some((_, ValueNode::UndefinedConstant)) => {} // expected
            other => panic!(
                "Expected dead allocation to be replaced with UndefinedConstant, got {:?}",
                other
            ),
        }
    }

    #[test]
    fn test_live_allocation_preserved() {
        // If the object escapes (e.g., returned), the allocation must stay.
        let mut graph = MaglevGraph::new(0);
        let mut block = BasicBlock::new(0);
        let val = block.push_value(ValueNode::SmiConstant { value: 42 });
        let obj = block.push_value(ValueNode::CreateObjectLiteralWithProperties {
            feedback_slot: 0,
            flags: 0,
            names: vec![1],
            values: vec![val],
        });
        // Object escapes via return.
        block.set_control(ControlNode::Return { value: obj });
        graph.add_block(block);

        eliminate_dead_allocations(&mut graph);

        // Allocation should NOT be replaced.
        let obj_node = graph.blocks()[0].nodes.iter().find(|(id, _)| *id == obj);
        assert!(
            matches!(
                obj_node,
                Some((_, ValueNode::CreateObjectLiteralWithProperties { .. }))
            ),
            "Live allocation should be preserved"
        );
    }

    #[test]
    fn test_store_to_load_cross_block_forwarding() {
        // Store in block 0 (entry), load in block 1 (single predecessor).
        // Cross-block forwarding should forward the load to the stored value.
        //
        //   Block 0:
        //     obj = Parameter(0)
        //     val = SmiConstant(42)
        //     StoreNamedGeneric(obj, "x", val)
        //     → Jump to block 1
        //
        //   Block 1 (predecessors: [0]):
        //     load = LoadNamedGeneric(obj, "x")
        //     → Return load
        //
        // After forwarding, the return should reference val directly.
        let mut graph = MaglevGraph::new(0);

        let mut b0 = BasicBlock::new(0);
        let obj = graph.alloc_node_id();
        b0.push_with_id(obj, ValueNode::Parameter { index: 0 });
        let val = graph.alloc_node_id();
        b0.push_with_id(val, ValueNode::SmiConstant { value: 42 });
        let store = graph.alloc_node_id();
        b0.push_with_id(
            store,
            ValueNode::StoreNamedGeneric {
                object: obj,
                name: 5,
                value: val,
                feedback_slot: 0,
            },
        );
        b0.set_control(ControlNode::Jump { target: 1 });
        graph.add_block(b0);

        let mut b1 = BasicBlock::new(1);
        b1.predecessors = vec![0];
        let load = graph.alloc_node_id();
        b1.push_with_id(
            load,
            ValueNode::LoadNamedGeneric {
                object: obj,
                name: 5,
                feedback_slot: 1,
            },
        );
        b1.set_control(ControlNode::Return { value: load });
        graph.add_block(b1);

        eliminate_store_to_load(&mut graph);

        // The return in block 1 should now reference val directly.
        match &graph.blocks()[1].control {
            Some(ControlNode::Return { value }) => {
                assert_eq!(
                    *value, val,
                    "Cross-block load should be forwarded to stored value"
                );
            }
            other => panic!("Expected Return, got {:?}", other),
        }
    }

    #[test]
    fn test_store_to_load_cross_block_merge_conservative() {
        // Stores in two predecessors should NOT be forwarded at a merge point.
        //
        //   Block 0: Store(obj, "x", val_a) → Jump 2
        //   Block 1: Store(obj, "x", val_b) → Jump 2
        //   Block 2 (predecessors: [0, 1]): Load(obj, "x") → Return
        //
        // At the merge point we cannot know which predecessor ran, so the
        // load must NOT be forwarded.
        let mut graph = MaglevGraph::new(0);

        let mut b0 = BasicBlock::new(0);
        let obj = graph.alloc_node_id();
        b0.push_with_id(obj, ValueNode::Parameter { index: 0 });
        let val_a = graph.alloc_node_id();
        b0.push_with_id(val_a, ValueNode::SmiConstant { value: 1 });
        let store_a = graph.alloc_node_id();
        b0.push_with_id(
            store_a,
            ValueNode::StoreNamedGeneric {
                object: obj,
                name: 5,
                value: val_a,
                feedback_slot: 0,
            },
        );
        b0.set_control(ControlNode::Jump { target: 2 });
        graph.add_block(b0);

        let mut b1 = BasicBlock::new(1);
        let val_b = graph.alloc_node_id();
        b1.push_with_id(val_b, ValueNode::SmiConstant { value: 2 });
        let store_b = graph.alloc_node_id();
        b1.push_with_id(
            store_b,
            ValueNode::StoreNamedGeneric {
                object: obj,
                name: 5,
                value: val_b,
                feedback_slot: 0,
            },
        );
        b1.set_control(ControlNode::Jump { target: 2 });
        graph.add_block(b1);

        let mut b2 = BasicBlock::new(2);
        b2.predecessors = vec![0, 1];
        let load = graph.alloc_node_id();
        b2.push_with_id(
            load,
            ValueNode::LoadNamedGeneric {
                object: obj,
                name: 5,
                feedback_slot: 1,
            },
        );
        b2.set_control(ControlNode::Return { value: load });
        graph.add_block(b2);

        eliminate_store_to_load(&mut graph);

        // The return in block 2 should still reference the load (not forwarded).
        match &graph.blocks()[2].control {
            Some(ControlNode::Return { value }) => {
                assert_eq!(*value, load, "Load at merge point should NOT be forwarded");
            }
            other => panic!("Expected Return, got {:?}", other),
        }
    }

    // ── Loop unrolling ────────────────────────────────────────────────────────

    #[test]
    fn test_loop_unroll_even_trip_count() {
        // for (var i = 0; i < 10; i++) { n = n + i; }
        // Use graph-global NodeIds to avoid block-local ID collisions.
        let mut graph = MaglevGraph::new(0);

        // block 0 (preheader)
        graph.add_block(BasicBlock::new(0));
        let init_i = graph
            .add_value_node(0, ValueNode::Int32Constant { value: 0 })
            .unwrap();
        let init_n = graph
            .add_value_node(0, ValueNode::Int32Constant { value: 0 })
            .unwrap();
        let limit = graph
            .add_value_node(0, ValueNode::Int32Constant { value: 10 })
            .unwrap();
        graph
            .block_mut(0)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        // block 1 (header) — Phi back-edge inputs patched below
        let mut b1 = BasicBlock::new(1);
        b1.is_loop_header = true;
        b1.predecessors = vec![0, 2];
        graph.add_block(b1);
        let phi_i = graph
            .add_value_node(
                1,
                ValueNode::Phi {
                    inputs: vec![init_i, NodeId(999)],
                },
            )
            .unwrap();
        let phi_n = graph
            .add_value_node(
                1,
                ValueNode::Phi {
                    inputs: vec![init_n, NodeId(999)],
                },
            )
            .unwrap();
        let cmp = graph
            .add_value_node(
                1,
                ValueNode::Int32LessThan {
                    left: phi_i,
                    right: limit,
                },
            )
            .unwrap();
        graph
            .block_mut(1)
            .unwrap()
            .set_control(ControlNode::Branch {
                condition: cmp,
                if_true: 2,
                if_false: 3,
            });

        // block 2 (body)
        let mut b2 = BasicBlock::new(2);
        b2.predecessors = vec![1];
        graph.add_block(b2);
        let new_n = graph
            .add_value_node(
                2,
                ValueNode::Int32Add {
                    left: phi_n,
                    right: phi_i,
                },
            )
            .unwrap();
        let new_i = graph
            .add_value_node(2, ValueNode::Int32Increment { value: phi_i })
            .unwrap();
        graph
            .block_mut(2)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        // block 3 (exit)
        let mut b3 = BasicBlock::new(3);
        b3.predecessors = vec![1];
        graph.add_block(b3);
        graph
            .block_mut(3)
            .unwrap()
            .set_control(ControlNode::Return { value: phi_n });

        // Patch Phi back-edge inputs.
        if let ValueNode::Phi { inputs } = &mut graph.block_mut(1).unwrap().nodes[0].1 {
            inputs[1] = new_i;
        }
        if let ValueNode::Phi { inputs } = &mut graph.block_mut(1).unwrap().nodes[1].1 {
            inputs[1] = new_n;
        }

        let body_nodes_before = graph.block(2).unwrap().nodes.len();
        unroll_simple_loops(&mut graph);
        let body_nodes_after = graph.block(2).unwrap().nodes.len();
        assert_eq!(body_nodes_after, body_nodes_before * 2);

        // Phi back-edge inputs should point to cloned nodes.
        if let ValueNode::Phi { inputs } = &graph.block(1).unwrap().nodes[0].1 {
            assert_ne!(inputs[1], new_i);
        }
        if let ValueNode::Phi { inputs } = &graph.block(1).unwrap().nodes[1].1 {
            assert_ne!(inputs[1], new_n);
        }
    }

    #[test]
    fn test_loop_unroll_skips_odd_trip() {
        // Trip count 7 — not divisible by 2, should skip unrolling.
        let mut graph = MaglevGraph::new(0);

        graph.add_block(BasicBlock::new(0));
        let init_i = graph
            .add_value_node(0, ValueNode::Int32Constant { value: 0 })
            .unwrap();
        let init_n = graph
            .add_value_node(0, ValueNode::Int32Constant { value: 0 })
            .unwrap();
        let limit = graph
            .add_value_node(0, ValueNode::Int32Constant { value: 7 })
            .unwrap();
        graph
            .block_mut(0)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        let mut b1 = BasicBlock::new(1);
        b1.is_loop_header = true;
        b1.predecessors = vec![0, 2];
        graph.add_block(b1);
        let phi_i = graph
            .add_value_node(
                1,
                ValueNode::Phi {
                    inputs: vec![init_i, NodeId(999)],
                },
            )
            .unwrap();
        let phi_n = graph
            .add_value_node(
                1,
                ValueNode::Phi {
                    inputs: vec![init_n, NodeId(999)],
                },
            )
            .unwrap();
        let cmp = graph
            .add_value_node(
                1,
                ValueNode::Int32LessThan {
                    left: phi_i,
                    right: limit,
                },
            )
            .unwrap();
        graph
            .block_mut(1)
            .unwrap()
            .set_control(ControlNode::Branch {
                condition: cmp,
                if_true: 2,
                if_false: 3,
            });

        let mut b2 = BasicBlock::new(2);
        b2.predecessors = vec![1];
        graph.add_block(b2);
        let new_n = graph
            .add_value_node(
                2,
                ValueNode::Int32Add {
                    left: phi_n,
                    right: phi_i,
                },
            )
            .unwrap();
        let new_i = graph
            .add_value_node(2, ValueNode::Int32Increment { value: phi_i })
            .unwrap();
        graph
            .block_mut(2)
            .unwrap()
            .set_control(ControlNode::Jump { target: 1 });

        let mut b3 = BasicBlock::new(3);
        b3.predecessors = vec![1];
        graph.add_block(b3);
        graph
            .block_mut(3)
            .unwrap()
            .set_control(ControlNode::Return { value: phi_n });

        if let ValueNode::Phi { inputs } = &mut graph.block_mut(1).unwrap().nodes[0].1 {
            inputs[1] = new_i;
        }
        if let ValueNode::Phi { inputs } = &mut graph.block_mut(1).unwrap().nodes[1].1 {
            inputs[1] = new_n;
        }

        let body_before = graph.block(2).unwrap().nodes.len();
        unroll_simple_loops(&mut graph);
        assert_eq!(graph.block(2).unwrap().nodes.len(), body_before);
    }

    #[test]
    fn test_forward_invariant_object_properties_via_global() {
        // Simulates: var obj = { a: 1 }; ... LoadGlobal("obj") → LoadNamedGeneric(_, "a")
        // Entry block: CreateObjectLiteralWithProperties + StoreGlobal
        // Second block: LoadGlobal + LoadNamedGeneric (should resolve to constant)
        let mut graph = MaglevGraph::new(0);

        // Block 0: entry
        let mut entry = BasicBlock::new(0);
        let val_a = entry.push_value(ValueNode::SmiConstant { value: 42 });
        let _create = entry.push_value(ValueNode::CreateObjectLiteralWithProperties {
            feedback_slot: 0,
            flags: 0,
            names: vec![5], // name index 5 = "a"
            values: vec![val_a],
        });
        entry.push_value(ValueNode::StoreGlobal {
            name: 10, // global name index 10 = "obj"
            value: _create,
            feedback_slot: 1,
        });
        entry.set_control(ControlNode::Jump { target: 1 });
        graph.add_block(entry);

        // Block 1: uses LoadGlobal + LoadNamedGeneric
        let mut body = BasicBlock::new(1);
        let obj_load = body.push_value(ValueNode::LoadGlobal {
            name: 10, // same global name
            feedback_slot: 2,
        });
        let prop_load = body.push_value(ValueNode::LoadNamedGeneric {
            object: obj_load,
            name: 5, // same property name
            feedback_slot: 3,
        });
        body.set_control(ControlNode::Return { value: prop_load });
        graph.add_block(body);

        // Run just the forwarding pass (not full optimize, to isolate)
        fuse_object_literal_stores(&mut graph);
        forward_invariant_object_properties(&mut graph);

        // The LoadNamedGeneric should have been substituted.
        // Check that prop_load's uses now point to val_a.
        let block1 = &graph.blocks()[1];
        let ret = block1.control.as_ref().unwrap();
        if let ControlNode::Return { value } = ret {
            // The return should now reference val_a (the SmiConstant 42)
            assert_eq!(
                *value, val_a,
                "Expected LoadNamedGeneric to be forwarded to SmiConstant(42), got {:?}",
                value
            );
        } else {
            panic!("Expected Return control node");
        }
    }

    #[test]
    fn test_forward_nested_object_properties() {
        // Simulates: var root = { a: { b: 99 } };
        // LoadGlobal("root") → LoadNamedGeneric(_, "a") → LoadNamedGeneric(_, "b")
        // Should resolve the entire chain to SmiConstant(99).
        let mut graph = MaglevGraph::new(0);

        // Block 0: entry
        let mut entry = BasicBlock::new(0);
        let val_b = entry.push_value(ValueNode::SmiConstant { value: 99 });
        let inner = entry.push_value(ValueNode::CreateObjectLiteralWithProperties {
            feedback_slot: u32::MAX,
            flags: 0,
            names: vec![7], // name index 7 = "b"
            values: vec![val_b],
        });
        let outer = entry.push_value(ValueNode::CreateObjectLiteralWithProperties {
            feedback_slot: u32::MAX,
            flags: 0,
            names: vec![5], // name index 5 = "a"
            values: vec![inner],
        });
        entry.push_value(ValueNode::StoreGlobal {
            name: 10,
            value: outer,
            feedback_slot: 1,
        });
        entry.set_control(ControlNode::Jump { target: 1 });
        graph.add_block(entry);

        // Block 1: chained property access
        let mut body = BasicBlock::new(1);
        let root_load = body.push_value(ValueNode::LoadGlobal {
            name: 10,
            feedback_slot: 2,
        });
        let load_a = body.push_value(ValueNode::LoadNamedGeneric {
            object: root_load,
            name: 5, // "a"
            feedback_slot: 3,
        });
        let load_b = body.push_value(ValueNode::LoadNamedGeneric {
            object: load_a,
            name: 7, // "b"
            feedback_slot: 4,
        });
        body.set_control(ControlNode::Return { value: load_b });
        graph.add_block(body);

        fuse_object_literal_stores(&mut graph);
        forward_invariant_object_properties(&mut graph);

        // load_b should resolve to val_b (SmiConstant 99)
        let block1 = &graph.blocks()[1];
        let ret = block1.control.as_ref().unwrap();
        if let ControlNode::Return { value } = ret {
            assert_eq!(
                *value, val_b,
                "Expected nested LoadNamedGeneric to resolve to SmiConstant(99), got {:?}",
                value
            );
        } else {
            panic!("Expected Return control node");
        }
    }
}