summaryrefslogtreecommitdiff
path: root/src/jit/valuenum.cpp
blob: 0a38a5f3136800fbfedec568a35405fe0b92115f (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX                                                                           XX
XX                           ValueNum                                        XX
XX                                                                           XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/

#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif

#include "valuenum.h"
#include "ssaconfig.h"

VNFunc GetVNFuncForOper(genTreeOps oper, bool isUnsigned)
{
    if (!isUnsigned || (oper == GT_EQ) || (oper == GT_NE))
    {
        return VNFunc(oper);
    }
    switch (oper)
    {
        case GT_LT:
            return VNF_LT_UN;
        case GT_LE:
            return VNF_LE_UN;
        case GT_GE:
            return VNF_GT_UN;
        case GT_GT:
            return VNF_GT_UN;
        case GT_ADD:
            return VNF_ADD_UN;
        case GT_SUB:
            return VNF_SUB_UN;
        case GT_MUL:
            return VNF_MUL_UN;
        case GT_DIV:
            return VNF_DIV_UN;
        case GT_MOD:
            return VNF_MOD_UN;

        case GT_NOP:
        case GT_COMMA:
            return VNFunc(oper);
        default:
            unreached();
    }
}

ValueNumStore::ValueNumStore(Compiler* comp, IAllocator* alloc)
    : m_pComp(comp)
    , m_alloc(alloc)
    ,
#ifdef DEBUG
    m_numMapSels(0)
    ,
#endif
    m_nextChunkBase(0)
    , m_fixedPointMapSels(alloc, 8)
    , m_chunks(alloc, 8)
    , m_intCnsMap(nullptr)
    , m_longCnsMap(nullptr)
    , m_handleMap(nullptr)
    , m_floatCnsMap(nullptr)
    , m_doubleCnsMap(nullptr)
    , m_byrefCnsMap(nullptr)
    , m_VNFunc0Map(nullptr)
    , m_VNFunc1Map(nullptr)
    , m_VNFunc2Map(nullptr)
    , m_VNFunc3Map(nullptr)
    , m_VNFunc4Map(nullptr)
    , m_uPtrToLocNotAFieldCount(1)
{
    // We have no current allocation chunks.
    for (unsigned i = 0; i < TYP_COUNT; i++)
    {
        for (unsigned j = CEA_None; j <= CEA_Count + MAX_LOOP_NUM; j++)
        {
            m_curAllocChunk[i][j] = NoChunk;
        }
    }

    for (unsigned i = 0; i < SmallIntConstNum; i++)
    {
        m_VNsForSmallIntConsts[i] = NoVN;
    }
    // We will reserve chunk 0 to hold some special constants, like the constant NULL, the "exception" value, and the
    // "zero map."
    Chunk* specialConstChunk = new (m_alloc) Chunk(m_alloc, &m_nextChunkBase, TYP_REF, CEA_Const, MAX_LOOP_NUM);
    specialConstChunk->m_numUsed +=
        SRC_NumSpecialRefConsts; // Implicitly allocate 0 ==> NULL, and 1 ==> Exception, 2 ==> ZeroMap.
    ChunkNum cn = m_chunks.Push(specialConstChunk);
    assert(cn == 0);

    m_mapSelectBudget = JitConfig.JitVNMapSelBudget();
}

// static.
template <typename T>
T ValueNumStore::EvalOp(VNFunc vnf, T v0)
{
    genTreeOps oper = genTreeOps(vnf);

    // Here we handle those unary ops that are the same for integral and floating-point types.
    switch (oper)
    {
        case GT_NEG:
            return -v0;
        default:
            // Must be int-specific
            return EvalOpIntegral(vnf, v0);
    }
}

template <typename T>
T ValueNumStore::EvalOpIntegral(VNFunc vnf, T v0)
{
    genTreeOps oper = genTreeOps(vnf);

    // Here we handle unary ops that are the same for all integral types.
    switch (oper)
    {
        case GT_NOT:
            return ~v0;
        default:
            unreached();
    }
}

// static
template <typename T>
T ValueNumStore::EvalOp(VNFunc vnf, T v0, T v1, ValueNum* pExcSet)
{
    if (vnf < VNF_Boundary)
    {
        genTreeOps oper = genTreeOps(vnf);
        // Here we handle those that are the same for integral and floating-point types.
        switch (oper)
        {
            case GT_ADD:
                return v0 + v1;
            case GT_SUB:
                return v0 - v1;
            case GT_MUL:
                return v0 * v1;
            case GT_DIV:
                if (IsIntZero(v1))
                {
                    *pExcSet = VNExcSetSingleton(VNForFunc(TYP_REF, VNF_DivideByZeroExc));
                    return (T)0;
                }
                if (IsOverflowIntDiv(v0, v1))
                {
                    *pExcSet = VNExcSetSingleton(VNForFunc(TYP_REF, VNF_ArithmeticExc));
                    return (T)0;
                }
                else
                {
                    return v0 / v1;
                }

            default:
                // Must be int-specific
                return EvalOpIntegral(vnf, v0, v1, pExcSet);
        }
    }
    else // must be a VNF_ function
    {
        typedef typename jitstd::make_unsigned<T>::type UT;
        switch (vnf)
        {
            case VNF_GT_UN:
                return T(UT(v0) > UT(v1));
            case VNF_GE_UN:
                return T(UT(v0) >= UT(v1));
            case VNF_LT_UN:
                return T(UT(v0) < UT(v1));
            case VNF_LE_UN:
                return T(UT(v0) <= UT(v1));
            case VNF_ADD_UN:
                return T(UT(v0) + UT(v1));
            case VNF_SUB_UN:
                return T(UT(v0) - UT(v1));
            case VNF_MUL_UN:
                return T(UT(v0) * UT(v1));
            case VNF_DIV_UN:
                if (IsIntZero(v1))
                {
                    *pExcSet = VNExcSetSingleton(VNForFunc(TYP_REF, VNF_DivideByZeroExc));
                    return (T)0;
                }
                else
                {
                    return T(UT(v0) / UT(v1));
                }
            default:
                // Must be int-specific
                return EvalOpIntegral(vnf, v0, v1, pExcSet);
        }
    }
}

// Specialize for double for floating operations, that doesn't involve unsigned.
template <>
double ValueNumStore::EvalOp<double>(VNFunc vnf, double v0, double v1, ValueNum* pExcSet)
{
    genTreeOps oper = genTreeOps(vnf);
    // Here we handle those that are the same for floating-point types.
    switch (oper)
    {
        case GT_ADD:
            return v0 + v1;
        case GT_SUB:
            return v0 - v1;
        case GT_MUL:
            return v0 * v1;
        case GT_DIV:
            return v0 / v1;
        case GT_MOD:
            return fmod(v0, v1);

        default:
            unreached();
    }
}

template <typename T>
int ValueNumStore::EvalComparison(VNFunc vnf, T v0, T v1)
{
    if (vnf < VNF_Boundary)
    {
        genTreeOps oper = genTreeOps(vnf);
        // Here we handle those that are the same for floating-point types.
        switch (oper)
        {
            case GT_EQ:
                return v0 == v1;
            case GT_NE:
                return v0 != v1;
            case GT_GT:
                return v0 > v1;
            case GT_GE:
                return v0 >= v1;
            case GT_LT:
                return v0 < v1;
            case GT_LE:
                return v0 <= v1;
            default:
                unreached();
        }
    }
    else // must be a VNF_ function
    {
        switch (vnf)
        {
            case VNF_GT_UN:
                return unsigned(v0) > unsigned(v1);
            case VNF_GE_UN:
                return unsigned(v0) >= unsigned(v1);
            case VNF_LT_UN:
                return unsigned(v0) < unsigned(v1);
            case VNF_LE_UN:
                return unsigned(v0) <= unsigned(v1);
            default:
                unreached();
        }
    }
}

/* static */
template <typename T>
int ValueNumStore::EvalOrderedComparisonFloat(VNFunc vnf, T v0, T v1)
{
    // !! NOTE !!
    //
    // All comparisons below are ordered comparisons.
    //
    // We should guard this function from unordered comparisons
    // identified by the GTF_RELOP_NAN_UN flag. Either the flag
    // should be bubbled (similar to GTF_UNSIGNED for ints)
    // to this point or we should bail much earlier if any of
    // the operands are NaN.
    //
    genTreeOps oper = genTreeOps(vnf);
    // Here we handle those that are the same for floating-point types.
    switch (oper)
    {
        case GT_EQ:
            return v0 == v1;
        case GT_NE:
            return v0 != v1;
        case GT_GT:
            return v0 > v1;
        case GT_GE:
            return v0 >= v1;
        case GT_LT:
            return v0 < v1;
        case GT_LE:
            return v0 <= v1;
        default:
            unreached();
    }
}

template <>
int ValueNumStore::EvalComparison<double>(VNFunc vnf, double v0, double v1)
{
    return EvalOrderedComparisonFloat(vnf, v0, v1);
}

template <>
int ValueNumStore::EvalComparison<float>(VNFunc vnf, float v0, float v1)
{
    return EvalOrderedComparisonFloat(vnf, v0, v1);
}

template <typename T>
T ValueNumStore::EvalOpIntegral(VNFunc vnf, T v0, T v1, ValueNum* pExcSet)
{
    genTreeOps oper = genTreeOps(vnf);
    switch (oper)
    {
        case GT_EQ:
            return v0 == v1;
        case GT_NE:
            return v0 != v1;
        case GT_GT:
            return v0 > v1;
        case GT_GE:
            return v0 >= v1;
        case GT_LT:
            return v0 < v1;
        case GT_LE:
            return v0 <= v1;
        case GT_OR:
            return v0 | v1;
        case GT_XOR:
            return v0 ^ v1;
        case GT_AND:
            return v0 & v1;
        case GT_LSH:
            return v0 << v1;
        case GT_RSH:
            return v0 >> v1;
        case GT_RSZ:
            if (sizeof(T) == 8)
            {
                return UINT64(v0) >> v1;
            }
            else
            {
                return UINT32(v0) >> v1;
            }
        case GT_ROL:
            if (sizeof(T) == 8)
            {
                return (v0 << v1) | (UINT64(v0) >> (64 - v1));
            }
            else
            {
                return (v0 << v1) | (UINT32(v0) >> (32 - v1));
            }

        case GT_ROR:
            if (sizeof(T) == 8)
            {
                return (v0 << (64 - v1)) | (UINT64(v0) >> v1);
            }
            else
            {
                return (v0 << (32 - v1)) | (UINT32(v0) >> v1);
            }

        case GT_DIV:
        case GT_MOD:
            if (v1 == 0)
            {
                *pExcSet = VNExcSetSingleton(VNForFunc(TYP_REF, VNF_DivideByZeroExc));
            }
            else if (IsOverflowIntDiv(v0, v1))
            {
                *pExcSet = VNExcSetSingleton(VNForFunc(TYP_REF, VNF_ArithmeticExc));
                return 0;
            }
            else // We are not dividing by Zero, so we can calculate the exact result.
            {
                // Perform the appropriate operation.
                if (oper == GT_DIV)
                {
                    return v0 / v1;
                }
                else // Must be GT_MOD
                {
                    return v0 % v1;
                }
            }

        case GT_UDIV:
        case GT_UMOD:
            if (v1 == 0)
            {
                *pExcSet = VNExcSetSingleton(VNForFunc(TYP_REF, VNF_DivideByZeroExc));
                return 0;
            }
            else // We are not dividing by Zero, so we can calculate the exact result.
            {
                typedef typename jitstd::make_unsigned<T>::type UT;
                // We need for force the source operands for the divide or mod operation
                // to be considered unsigned.
                //
                if (oper == GT_UDIV)
                {
                    // This is return unsigned(v0) / unsigned(v1) for both sizes of integers
                    return T(UT(v0) / UT(v1));
                }
                else // Must be GT_UMOD
                {
                    // This is return unsigned(v0) % unsigned(v1) for both sizes of integers
                    return T(UT(v0) % UT(v1));
                }
            }
        default:
            unreached(); // NYI?
    }
}

ValueNum ValueNumStore::VNExcSetSingleton(ValueNum x)
{
    ValueNum res = VNForFunc(TYP_REF, VNF_ExcSetCons, x, VNForEmptyExcSet());
#ifdef DEBUG
    if (m_pComp->verbose)
    {
        printf("    " STR_VN "%x = singleton exc set", res);
        vnDump(m_pComp, x);
        printf("\n");
    }
#endif
    return res;
}

ValueNumPair ValueNumStore::VNPExcSetSingleton(ValueNumPair xp)
{
    return ValueNumPair(VNExcSetSingleton(xp.GetLiberal()), VNExcSetSingleton(xp.GetConservative()));
}

ValueNum ValueNumStore::VNExcSetUnion(ValueNum xs0, ValueNum xs1 DEBUGARG(bool topLevel))
{
    if (xs0 == VNForEmptyExcSet())
    {
        return xs1;
    }
    else if (xs1 == VNForEmptyExcSet())
    {
        return xs0;
    }
    else
    {
        VNFuncApp funcXs0;
        bool      b0 = GetVNFunc(xs0, &funcXs0);
        assert(b0 && funcXs0.m_func == VNF_ExcSetCons); // Precondition: xs0 is an exception set.
        VNFuncApp funcXs1;
        bool      b1 = GetVNFunc(xs1, &funcXs1);
        assert(b1 && funcXs1.m_func == VNF_ExcSetCons); // Precondition: xs1 is an exception set.
        ValueNum res = NoVN;
        if (funcXs0.m_args[0] < funcXs1.m_args[0])
        {
            res = VNForFunc(TYP_REF, VNF_ExcSetCons, funcXs0.m_args[0],
                            VNExcSetUnion(funcXs0.m_args[1], xs1 DEBUGARG(false)));
        }
        else if (funcXs0.m_args[0] == funcXs1.m_args[0])
        {
            // Equal elements; only add one to the result.
            res = VNExcSetUnion(funcXs0.m_args[1], xs1);
        }
        else
        {
            assert(funcXs0.m_args[0] > funcXs1.m_args[0]);
            res = VNForFunc(TYP_REF, VNF_ExcSetCons, funcXs1.m_args[0],
                            VNExcSetUnion(xs0, funcXs1.m_args[1] DEBUGARG(false)));
        }

        return res;
    }
}

ValueNumPair ValueNumStore::VNPExcSetUnion(ValueNumPair xs0vnp, ValueNumPair xs1vnp)
{
    return ValueNumPair(VNExcSetUnion(xs0vnp.GetLiberal(), xs1vnp.GetLiberal()),
                        VNExcSetUnion(xs0vnp.GetConservative(), xs1vnp.GetConservative()));
}

void ValueNumStore::VNUnpackExc(ValueNum vnWx, ValueNum* pvn, ValueNum* pvnx)
{
    assert(vnWx != NoVN);
    VNFuncApp funcApp;
    if (GetVNFunc(vnWx, &funcApp) && funcApp.m_func == VNF_ValWithExc)
    {
        *pvn  = funcApp.m_args[0];
        *pvnx = funcApp.m_args[1];
    }
    else
    {
        *pvn = vnWx;
    }
}

void ValueNumStore::VNPUnpackExc(ValueNumPair vnWx, ValueNumPair* pvn, ValueNumPair* pvnx)
{
    VNUnpackExc(vnWx.GetLiberal(), pvn->GetLiberalAddr(), pvnx->GetLiberalAddr());
    VNUnpackExc(vnWx.GetConservative(), pvn->GetConservativeAddr(), pvnx->GetConservativeAddr());
}

ValueNum ValueNumStore::VNNormVal(ValueNum vn)
{
    VNFuncApp funcApp;
    if (GetVNFunc(vn, &funcApp) && funcApp.m_func == VNF_ValWithExc)
    {
        return funcApp.m_args[0];
    }
    else
    {
        return vn;
    }
}

ValueNumPair ValueNumStore::VNPNormVal(ValueNumPair vnp)
{
    return ValueNumPair(VNNormVal(vnp.GetLiberal()), VNNormVal(vnp.GetConservative()));
}

ValueNum ValueNumStore::VNExcVal(ValueNum vn)
{
    VNFuncApp funcApp;
    if (GetVNFunc(vn, &funcApp) && funcApp.m_func == VNF_ValWithExc)
    {
        return funcApp.m_args[1];
    }
    else
    {
        return VNForEmptyExcSet();
    }
}

ValueNumPair ValueNumStore::VNPExcVal(ValueNumPair vnp)
{
    return ValueNumPair(VNExcVal(vnp.GetLiberal()), VNExcVal(vnp.GetConservative()));
}

// If vn "excSet" is not "VNForEmptyExcSet()", return "VNF_ValWithExc(vn, excSet)".  Otherwise,
// just return "vn".
ValueNum ValueNumStore::VNWithExc(ValueNum vn, ValueNum excSet)
{
    if (excSet == VNForEmptyExcSet())
    {
        return vn;
    }
    else
    {
        ValueNum vnNorm;
        ValueNum vnX = VNForEmptyExcSet();
        VNUnpackExc(vn, &vnNorm, &vnX);
        return VNForFunc(TypeOfVN(vnNorm), VNF_ValWithExc, vnNorm, VNExcSetUnion(vnX, excSet));
    }
}

ValueNumPair ValueNumStore::VNPWithExc(ValueNumPair vnp, ValueNumPair excSetVNP)
{
    return ValueNumPair(VNWithExc(vnp.GetLiberal(), excSetVNP.GetLiberal()),
                        VNWithExc(vnp.GetConservative(), excSetVNP.GetConservative()));
}

bool ValueNumStore::IsKnownNonNull(ValueNum vn)
{
    if (vn == NoVN)
    {
        return false;
    }
    VNFuncApp funcAttr;
    return GetVNFunc(vn, &funcAttr) && (s_vnfOpAttribs[funcAttr.m_func] & VNFOA_KnownNonNull) != 0;
}

bool ValueNumStore::IsSharedStatic(ValueNum vn)
{
    if (vn == NoVN)
    {
        return false;
    }
    VNFuncApp funcAttr;
    return GetVNFunc(vn, &funcAttr) && (s_vnfOpAttribs[funcAttr.m_func] & VNFOA_SharedStatic) != 0;
}

ValueNumStore::Chunk::Chunk(
    IAllocator* alloc, ValueNum* pNextBaseVN, var_types typ, ChunkExtraAttribs attribs, BasicBlock::loopNumber loopNum)
    : m_defs(nullptr), m_numUsed(0), m_baseVN(*pNextBaseVN), m_typ(typ), m_attribs(attribs), m_loopNum(loopNum)
{
    // Allocate "m_defs" here, according to the typ/attribs pair.
    switch (attribs)
    {
        case CEA_None:
            break; // Nothing to do.
        case CEA_Const:
            switch (typ)
            {
                case TYP_INT:
                    m_defs = new (alloc) Alloc<TYP_INT>::Type[ChunkSize];
                    break;
                case TYP_FLOAT:
                    m_defs = new (alloc) Alloc<TYP_FLOAT>::Type[ChunkSize];
                    break;
                case TYP_LONG:
                    m_defs = new (alloc) Alloc<TYP_LONG>::Type[ChunkSize];
                    break;
                case TYP_DOUBLE:
                    m_defs = new (alloc) Alloc<TYP_DOUBLE>::Type[ChunkSize];
                    break;
                case TYP_BYREF:
                    m_defs = new (alloc) Alloc<TYP_BYREF>::Type[ChunkSize];
                    break;
                case TYP_REF:
                    // We allocate space for a single REF constant, NULL, so we can access these values uniformly.
                    // Since this value is always the same, we represent it as a static.
                    m_defs = &s_specialRefConsts[0];
                    break; // Nothing to do.
                default:
                    assert(false); // Should not reach here.
            }
            break;

        case CEA_Handle:
            m_defs = new (alloc) VNHandle[ChunkSize];
            break;

        case CEA_Func0:
            m_defs = new (alloc) VNFunc[ChunkSize];
            break;

        case CEA_Func1:
            m_defs = new (alloc) VNDefFunc1Arg[ChunkSize];
            break;
        case CEA_Func2:
            m_defs = new (alloc) VNDefFunc2Arg[ChunkSize];
            break;
        case CEA_Func3:
            m_defs = new (alloc) VNDefFunc3Arg[ChunkSize];
            break;
        case CEA_Func4:
            m_defs = new (alloc) VNDefFunc4Arg[ChunkSize];
            break;
        default:
            unreached();
    }
    *pNextBaseVN += ChunkSize;
}

ValueNumStore::Chunk* ValueNumStore::GetAllocChunk(var_types              typ,
                                                   ChunkExtraAttribs      attribs,
                                                   BasicBlock::loopNumber loopNum)
{
    Chunk*   res;
    unsigned index;
    if (loopNum == MAX_LOOP_NUM)
    {
        // Loop nest is unknown/irrelevant for this VN.
        index = attribs;
    }
    else
    {
        // Loop nest is interesting.  Since we know this is only true for unique VNs, we know attribs will
        // be CEA_None and can just index based on loop number.
        noway_assert(attribs == CEA_None);
        // Map NOT_IN_LOOP -> MAX_LOOP_NUM to make the index range contiguous [0..MAX_LOOP_NUM]
        index = CEA_Count + (loopNum == BasicBlock::NOT_IN_LOOP ? MAX_LOOP_NUM : loopNum);
    }
    ChunkNum cn = m_curAllocChunk[typ][index];
    if (cn != NoChunk)
    {
        res = m_chunks.Get(cn);
        if (res->m_numUsed < ChunkSize)
        {
            return res;
        }
    }
    // Otherwise, must allocate a new one.
    res                         = new (m_alloc) Chunk(m_alloc, &m_nextChunkBase, typ, attribs, loopNum);
    cn                          = m_chunks.Push(res);
    m_curAllocChunk[typ][index] = cn;
    return res;
}

ValueNum ValueNumStore::VNForIntCon(INT32 cnsVal)
{
    if (IsSmallIntConst(cnsVal))
    {
        unsigned ind = cnsVal - SmallIntConstMin;
        ValueNum vn  = m_VNsForSmallIntConsts[ind];
        if (vn != NoVN)
        {
            return vn;
        }
        vn                          = GetVNForIntCon(cnsVal);
        m_VNsForSmallIntConsts[ind] = vn;
        return vn;
    }
    else
    {
        return GetVNForIntCon(cnsVal);
    }
}

ValueNum ValueNumStore::VNForLongCon(INT64 cnsVal)
{
    ValueNum res;
    if (GetLongCnsMap()->Lookup(cnsVal, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                             = GetAllocChunk(TYP_LONG, CEA_Const);
        unsigned offsetWithinChunk                             = c->AllocVN();
        res                                                    = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<INT64*>(c->m_defs)[offsetWithinChunk] = cnsVal;
        GetLongCnsMap()->Set(cnsVal, res);
        return res;
    }
}

ValueNum ValueNumStore::VNForFloatCon(float cnsVal)
{
    ValueNum res;
    if (GetFloatCnsMap()->Lookup(cnsVal, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                             = GetAllocChunk(TYP_FLOAT, CEA_Const);
        unsigned offsetWithinChunk                             = c->AllocVN();
        res                                                    = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<float*>(c->m_defs)[offsetWithinChunk] = cnsVal;
        GetFloatCnsMap()->Set(cnsVal, res);
        return res;
    }
}

ValueNum ValueNumStore::VNForDoubleCon(double cnsVal)
{
    ValueNum res;
    if (GetDoubleCnsMap()->Lookup(cnsVal, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                              = GetAllocChunk(TYP_DOUBLE, CEA_Const);
        unsigned offsetWithinChunk                              = c->AllocVN();
        res                                                     = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<double*>(c->m_defs)[offsetWithinChunk] = cnsVal;
        GetDoubleCnsMap()->Set(cnsVal, res);
        return res;
    }
}

ValueNum ValueNumStore::VNForByrefCon(INT64 cnsVal)
{
    ValueNum res;
    if (GetByrefCnsMap()->Lookup(cnsVal, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                             = GetAllocChunk(TYP_BYREF, CEA_Const);
        unsigned offsetWithinChunk                             = c->AllocVN();
        res                                                    = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<INT64*>(c->m_defs)[offsetWithinChunk] = cnsVal;
        GetByrefCnsMap()->Set(cnsVal, res);
        return res;
    }
}

ValueNum ValueNumStore::VNForCastOper(var_types castToType, bool srcIsUnsigned /*=false*/)
{
    assert(castToType != TYP_STRUCT);
    INT32 cnsVal = INT32(castToType) << INT32(VCA_BitCount);
    assert((cnsVal & INT32(VCA_ReservedBits)) == 0);

    if (srcIsUnsigned)
    {
        // We record the srcIsUnsigned by or-ing a 0x01
        cnsVal |= INT32(VCA_UnsignedSrc);
    }
    ValueNum result = VNForIntCon(cnsVal);

#ifdef DEBUG
    if (m_pComp->verbose)
    {
        printf("    VNForCastOper(%s%s) is " STR_VN "%x\n", varTypeName(castToType),
               srcIsUnsigned ? ", unsignedSrc" : "", result);
    }
#endif

    return result;
}

ValueNum ValueNumStore::VNForHandle(ssize_t cnsVal, unsigned handleFlags)
{
    assert((handleFlags & ~GTF_ICON_HDL_MASK) == 0);

    ValueNum res;
    VNHandle handle;
    VNHandle::Initialize(&handle, cnsVal, handleFlags);
    if (GetHandleMap()->Lookup(handle, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                                = GetAllocChunk(TYP_I_IMPL, CEA_Handle);
        unsigned offsetWithinChunk                                = c->AllocVN();
        res                                                       = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<VNHandle*>(c->m_defs)[offsetWithinChunk] = handle;
        GetHandleMap()->Set(handle, res);
        return res;
    }
}

// Returns the value number for zero of the given "typ".
// It has an unreached() for a "typ" that has no zero value, such as TYP_BYREF.
ValueNum ValueNumStore::VNZeroForType(var_types typ)
{
    switch (typ)
    {
        case TYP_BOOL:
        case TYP_BYTE:
        case TYP_UBYTE:
        case TYP_CHAR:
        case TYP_SHORT:
        case TYP_USHORT:
        case TYP_INT:
        case TYP_UINT:
            return VNForIntCon(0);
        case TYP_LONG:
        case TYP_ULONG:
            return VNForLongCon(0);
        case TYP_FLOAT:
#if FEATURE_X87_DOUBLES
            return VNForDoubleCon(0.0);
#else
            return VNForFloatCon(0.0f);
#endif
        case TYP_DOUBLE:
            return VNForDoubleCon(0.0);
        case TYP_REF:
        case TYP_ARRAY:
            return VNForNull();
        case TYP_STRUCT:
#ifdef FEATURE_SIMD
        // TODO-CQ: Improve value numbering for SIMD types.
        case TYP_SIMD8:
        case TYP_SIMD12:
        case TYP_SIMD16:
        case TYP_SIMD32:
#endif                             // FEATURE_SIMD
            return VNForZeroMap(); // Recursion!

        // These should be unreached.
        default:
            unreached(); // Should handle all types.
    }
}

// Returns the value number for one of the given "typ".
// It returns NoVN for a "typ" that has no one value, such as TYP_REF.
ValueNum ValueNumStore::VNOneForType(var_types typ)
{
    switch (typ)
    {
        case TYP_BOOL:
        case TYP_BYTE:
        case TYP_UBYTE:
        case TYP_CHAR:
        case TYP_SHORT:
        case TYP_USHORT:
        case TYP_INT:
        case TYP_UINT:
            return VNForIntCon(1);
        case TYP_LONG:
        case TYP_ULONG:
            return VNForLongCon(1);
        case TYP_FLOAT:
            return VNForFloatCon(1.0f);
        case TYP_DOUBLE:
            return VNForDoubleCon(1.0);

        default:
            return NoVN;
    }
}

class Object* ValueNumStore::s_specialRefConsts[] = {nullptr, nullptr, nullptr};

// Nullary operators (i.e., symbolic constants).
ValueNum ValueNumStore::VNForFunc(var_types typ, VNFunc func)
{
    assert(VNFuncArity(func) == 0);

    ValueNum res;

    if (GetVNFunc0Map()->Lookup(func, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                              = GetAllocChunk(typ, CEA_Func0);
        unsigned offsetWithinChunk                              = c->AllocVN();
        res                                                     = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<VNFunc*>(c->m_defs)[offsetWithinChunk] = func;
        GetVNFunc0Map()->Set(func, res);
        return res;
    }
}

ValueNum ValueNumStore::VNForFunc(var_types typ, VNFunc func, ValueNum arg0VN)
{
    assert(arg0VN == VNNormVal(arg0VN)); // Arguments don't carry exceptions.

    ValueNum      res;
    VNDefFunc1Arg fstruct(func, arg0VN);

    // Do constant-folding.
    if (CanEvalForConstantArgs(func) && IsVNConstant(arg0VN))
    {
        return EvalFuncForConstantArgs(typ, func, arg0VN);
    }

    if (GetVNFunc1Map()->Lookup(fstruct, &res))
    {
        return res;
    }
    else
    {
        // Otherwise, create a new VN for this application.
        Chunk*   c                                                     = GetAllocChunk(typ, CEA_Func1);
        unsigned offsetWithinChunk                                     = c->AllocVN();
        res                                                            = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<VNDefFunc1Arg*>(c->m_defs)[offsetWithinChunk] = fstruct;
        GetVNFunc1Map()->Set(fstruct, res);
        return res;
    }
}

ValueNum ValueNumStore::VNForFunc(var_types typ, VNFunc func, ValueNum arg0VN, ValueNum arg1VN)
{
    assert(arg0VN != NoVN && arg1VN != NoVN);
    assert(arg0VN == VNNormVal(arg0VN)); // Arguments carry no exceptions.
    assert(arg1VN == VNNormVal(arg1VN)); // Arguments carry no exceptions.
    assert(VNFuncArity(func) == 2);
    assert(func != VNF_MapSelect); // Precondition: use the special function VNForMapSelect defined for that.

    ValueNum res;

    // Do constant-folding.
    if (CanEvalForConstantArgs(func) && IsVNConstant(arg0VN) && IsVNConstant(arg1VN))
    {
        bool canFold = true; // Normally we will be able to fold this 'func'

        // Special case for VNF_Cast of constant handles
        // Don't allow eval/fold of a GT_CAST(non-I_IMPL, Handle)
        //
        if ((func == VNF_Cast) && (typ != TYP_I_IMPL) && IsVNHandle(arg0VN))
        {
            canFold = false;
        }

        // It is possible for us to have mismatched types (see Bug 750863)
        // We don't try to fold a binary operation when one of the constant operands
        // is a floating-point constant and the other is not.
        //
        bool arg0IsFloating = varTypeIsFloating(TypeOfVN(arg0VN));
        bool arg1IsFloating = varTypeIsFloating(TypeOfVN(arg1VN));
        if (arg0IsFloating != arg1IsFloating)
        {
            canFold = false;
        }

        // NaNs are unordered wrt to other floats. While an ordered
        // comparison would return false, an unordered comparison
        // will return true if any operands are a NaN. We only perform
        // ordered NaN comparison in EvalComparison.
        if ((arg0IsFloating && _isnan(GetConstantDouble(arg0VN))) ||
            (arg1IsFloating && _isnan(GetConstantDouble(arg1VN))))
        {
            canFold = false;
        }

        if (canFold)
        {
            return EvalFuncForConstantArgs(typ, func, arg0VN, arg1VN);
        }
    }
    // We canonicalize commutative operations.
    // (Perhaps should eventually handle associative/commutative [AC] ops -- but that gets complicated...)
    if (VNFuncIsCommutative(func))
    {
        // Order arg0 arg1 by numerical VN value.
        if (arg0VN > arg1VN)
        {
            jitstd::swap(arg0VN, arg1VN);
        }
    }
    VNDefFunc2Arg fstruct(func, arg0VN, arg1VN);
    if (GetVNFunc2Map()->Lookup(fstruct, &res))
    {
        return res;
    }
    else
    {
        // We have ways of evaluating some binary functions.
        if (func < VNF_Boundary)
        {
            if (typ != TYP_BYREF) // We don't want/need to optimize a zero byref
            {
                genTreeOps oper = genTreeOps(func);
                ValueNum   ZeroVN, OneVN; // We may need to create one of these in the switch below.
                switch (oper)
                {
                    case GT_ADD:
                        // This identity does not apply for floating point (when x == -0.0)
                        if (!varTypeIsFloating(typ))
                        {
                            // (x + 0) == (0 + x) => x
                            ZeroVN = VNZeroForType(typ);
                            if (arg0VN == ZeroVN)
                            {
                                return arg1VN;
                            }
                            else if (arg1VN == ZeroVN)
                            {
                                return arg0VN;
                            }
                        }
                        break;

                    case GT_SUB:
                        // (x - 0) => x
                        ZeroVN = VNZeroForType(typ);
                        if (arg1VN == ZeroVN)
                        {
                            return arg0VN;
                        }
                        break;

                    case GT_MUL:
                        // (x * 1) == (1 * x) => x
                        OneVN = VNOneForType(typ);
                        if (OneVN != NoVN)
                        {
                            if (arg0VN == OneVN)
                            {
                                return arg1VN;
                            }
                            else if (arg1VN == OneVN)
                            {
                                return arg0VN;
                            }
                        }

                        if (!varTypeIsFloating(typ))
                        {
                            // (x * 0) == (0 * x) => 0 (unless x is NaN, which we must assume a fp value may be)
                            ZeroVN = VNZeroForType(typ);
                            if (arg0VN == ZeroVN)
                            {
                                return ZeroVN;
                            }
                            else if (arg1VN == ZeroVN)
                            {
                                return ZeroVN;
                            }
                        }
                        break;

                    case GT_DIV:
                    case GT_UDIV:
                        // (x / 1) => x
                        OneVN = VNOneForType(typ);
                        if (OneVN != NoVN)
                        {
                            if (arg1VN == OneVN)
                            {
                                return arg0VN;
                            }
                        }
                        break;

                    case GT_OR:
                    case GT_XOR:
                        // (x | 0) == (0 | x) => x
                        // (x ^ 0) == (0 ^ x) => x
                        ZeroVN = VNZeroForType(typ);
                        if (arg0VN == ZeroVN)
                        {
                            return arg1VN;
                        }
                        else if (arg1VN == ZeroVN)
                        {
                            return arg0VN;
                        }
                        break;

                    case GT_AND:
                        // (x & 0) == (0 & x) => 0
                        ZeroVN = VNZeroForType(typ);
                        if (arg0VN == ZeroVN)
                        {
                            return ZeroVN;
                        }
                        else if (arg1VN == ZeroVN)
                        {
                            return ZeroVN;
                        }
                        break;

                    case GT_LSH:
                    case GT_RSH:
                    case GT_RSZ:
                    case GT_ROL:
                    case GT_ROR:
                        // (x << 0) => x
                        // (x >> 0) => x
                        // (x rol 0) => x
                        // (x ror 0) => x
                        ZeroVN = VNZeroForType(typ);
                        if (arg1VN == ZeroVN)
                        {
                            return arg0VN;
                        }
                        break;

                    case GT_EQ:
                        // (x == x) => true (unless x is NaN)
                        if (!varTypeIsFloating(TypeOfVN(arg0VN)) && (arg0VN != NoVN) && (arg0VN == arg1VN))
                        {
                            return VNOneForType(typ);
                        }
                        if ((arg0VN == VNForNull() && IsKnownNonNull(arg1VN)) ||
                            (arg1VN == VNForNull() && IsKnownNonNull(arg0VN)))
                        {
                            return VNZeroForType(typ);
                        }
                        break;
                    case GT_NE:
                        // (x != x) => false (unless x is NaN)
                        if (!varTypeIsFloating(TypeOfVN(arg0VN)) && (arg0VN != NoVN) && (arg0VN == arg1VN))
                        {
                            return VNZeroForType(typ);
                        }
                        if ((arg0VN == VNForNull() && IsKnownNonNull(arg1VN)) ||
                            (arg1VN == VNForNull() && IsKnownNonNull(arg0VN)))
                        {
                            return VNOneForType(typ);
                        }
                        break;

                    default:
                        break;
                }
            }
        }
        else // must be a VNF_ function
        {
            if (func == VNF_CastClass)
            {
                // In terms of values, a castclass always returns its second argument, the object being cast.
                // The IL operation may also throw an exception
                return VNWithExc(arg1VN, VNExcSetSingleton(VNForFunc(TYP_REF, VNF_InvalidCastExc, arg1VN, arg0VN)));
            }
        }

        // Otherwise, assign a new VN for the function application.
        Chunk*   c                                                     = GetAllocChunk(typ, CEA_Func2);
        unsigned offsetWithinChunk                                     = c->AllocVN();
        res                                                            = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<VNDefFunc2Arg*>(c->m_defs)[offsetWithinChunk] = fstruct;
        GetVNFunc2Map()->Set(fstruct, res);
        return res;
    }
}

//------------------------------------------------------------------------------
// VNForMapStore : Evaluate VNF_MapStore with the given arguments.
//
//
// Arguments:
//    typ  -    Value type
//    arg0VN  - Map value number
//    arg1VN  - Index value number
//    arg2VN  - New value for map[index]
//
// Return Value:
//    Value number for the result of the evaluation.

ValueNum ValueNumStore::VNForMapStore(var_types typ, ValueNum arg0VN, ValueNum arg1VN, ValueNum arg2VN)
{
    ValueNum result = VNForFunc(typ, VNF_MapStore, arg0VN, arg1VN, arg2VN);
#ifdef DEBUG
    if (m_pComp->verbose)
    {
        printf("    VNForMapStore(" STR_VN "%x, " STR_VN "%x, " STR_VN "%x):%s returns ", arg0VN, arg1VN, arg2VN,
               varTypeName(typ));
        m_pComp->vnPrint(result, 1);
        printf("\n");
    }
#endif
    return result;
}

//------------------------------------------------------------------------------
// VNForMapSelect : Evaluate VNF_MapSelect with the given arguments.
//
//
// Arguments:
//    vnk  -    Value number kind
//    typ  -    Value type
//    arg0VN  - Map value number
//    arg1VN  - Index value number
//
// Return Value:
//    Value number for the result of the evaluation.
//
// Notes:
//    This requires a "ValueNumKind" because it will attempt, given "select(phi(m1, ..., mk), ind)", to evaluate
//    "select(m1, ind)", ..., "select(mk, ind)" to see if they agree.  It needs to know which kind of value number
//    (liberal/conservative) to read from the SSA def referenced in the phi argument.

ValueNum ValueNumStore::VNForMapSelect(ValueNumKind vnk, var_types typ, ValueNum arg0VN, ValueNum arg1VN)
{
    unsigned budget          = m_mapSelectBudget;
    bool     usedRecursiveVN = false;
    ValueNum result          = VNForMapSelectWork(vnk, typ, arg0VN, arg1VN, &budget, &usedRecursiveVN);
#ifdef DEBUG
    if (m_pComp->verbose)
    {
        printf("    VNForMapSelect(" STR_VN "%x, " STR_VN "%x):%s returns ", arg0VN, arg1VN, varTypeName(typ));
        m_pComp->vnPrint(result, 1);
        printf("\n");
    }
#endif
    return result;
}

//------------------------------------------------------------------------------
// VNForMapSelectWork : A method that does the work for VNForMapSelect and may call itself recursively.
//
//
// Arguments:
//    vnk  -             Value number kind
//    typ  -             Value type
//    arg0VN  -          Zeroth argument
//    arg1VN  -          First argument
//    pBudget -          Remaining budget for the outer evaluation
//    pUsedRecursiveVN - Out-parameter that is set to true iff RecursiveVN was returned from this method
//                       or from a method called during one of recursive invocations.
//
// Return Value:
//    Value number for the result of the evaluation.
//
// Notes:
//    This requires a "ValueNumKind" because it will attempt, given "select(phi(m1, ..., mk), ind)", to evaluate
//    "select(m1, ind)", ..., "select(mk, ind)" to see if they agree.  It needs to know which kind of value number
//    (liberal/conservative) to read from the SSA def referenced in the phi argument.

ValueNum ValueNumStore::VNForMapSelectWork(
    ValueNumKind vnk, var_types typ, ValueNum arg0VN, ValueNum arg1VN, unsigned* pBudget, bool* pUsedRecursiveVN)
{
TailCall:
    // This label allows us to directly implement a tail call by setting up the arguments, and doing a goto to here.
    assert(arg0VN != NoVN && arg1VN != NoVN);
    assert(arg0VN == VNNormVal(arg0VN)); // Arguments carry no exceptions.
    assert(arg1VN == VNNormVal(arg1VN)); // Arguments carry no exceptions.

    *pUsedRecursiveVN = false;

#ifdef DEBUG
    // Provide a mechanism for writing tests that ensure we don't call this ridiculously often.
    m_numMapSels++;
#if 1
// This printing is sometimes useful in debugging.
// if ((m_numMapSels % 1000) == 0) printf("%d VNF_MapSelect applications.\n", m_numMapSels);
#endif
    unsigned selLim = JitConfig.JitVNMapSelLimit();
    assert(selLim == 0 || m_numMapSels < selLim);
#endif
    ValueNum res;

    VNDefFunc2Arg fstruct(VNF_MapSelect, arg0VN, arg1VN);
    if (GetVNFunc2Map()->Lookup(fstruct, &res))
    {
        return res;
    }
    else
    {

        // Give up if we've run out of budget.
        if (--(*pBudget) == 0)
        {
            // We have to use 'nullptr' for the basic block here, because subsequent expressions
            // in different blocks may find this result in the VNFunc2Map -- other expressions in
            // the IR may "evaluate" to this same VNForExpr, so it is not "unique" in the sense
            // that permits the BasicBlock attribution.
            res = VNForExpr(nullptr, typ);
            GetVNFunc2Map()->Set(fstruct, res);
            return res;
        }

        // If it's recursive, stop the recursion.
        if (SelectIsBeingEvaluatedRecursively(arg0VN, arg1VN))
        {
            *pUsedRecursiveVN = true;
            return RecursiveVN;
        }

        if (arg0VN == VNForZeroMap())
        {
            return VNZeroForType(typ);
        }
        else if (IsVNFunc(arg0VN))
        {
            VNFuncApp funcApp;
            GetVNFunc(arg0VN, &funcApp);
            if (funcApp.m_func == VNF_MapStore)
            {
                // select(store(m, i, v), i) == v
                if (funcApp.m_args[1] == arg1VN)
                {
#if FEATURE_VN_TRACE_APPLY_SELECTORS
                    JITDUMP("      AX1: select([" STR_VN "%x]store(" STR_VN "%x, " STR_VN "%x, " STR_VN "%x), " STR_VN
                            "%x) ==> " STR_VN "%x.\n",
                            funcApp.m_args[0], arg0VN, funcApp.m_args[1], funcApp.m_args[2], arg1VN, funcApp.m_args[2]);
#endif
                    return funcApp.m_args[2];
                }
                // i # j ==> select(store(m, i, v), j) == select(m, j)
                // Currently the only source of distinctions is when both indices are constants.
                else if (IsVNConstant(arg1VN) && IsVNConstant(funcApp.m_args[1]))
                {
                    assert(funcApp.m_args[1] != arg1VN); // we already checked this above.
#if FEATURE_VN_TRACE_APPLY_SELECTORS
                    JITDUMP("      AX2: " STR_VN "%x != " STR_VN "%x ==> select([" STR_VN "%x]store(" STR_VN
                            "%x, " STR_VN "%x, " STR_VN "%x), " STR_VN "%x) ==> select(" STR_VN "%x, " STR_VN "%x).\n",
                            arg1VN, funcApp.m_args[1], arg0VN, funcApp.m_args[0], funcApp.m_args[1], funcApp.m_args[2],
                            arg1VN, funcApp.m_args[0], arg1VN);
#endif
                    // This is the equivalent of the recursive tail call:
                    // return VNForMapSelect(vnk, typ, funcApp.m_args[0], arg1VN);
                    // Make sure we capture any exceptions from the "i" and "v" of the store...
                    arg0VN = funcApp.m_args[0];
                    goto TailCall;
                }
            }
            else if (funcApp.m_func == VNF_PhiDef || funcApp.m_func == VNF_PhiHeapDef)
            {
                unsigned  lclNum = BAD_VAR_NUM;
                bool      isHeap = false;
                VNFuncApp phiFuncApp;
                bool      defArgIsFunc = false;
                if (funcApp.m_func == VNF_PhiDef)
                {
                    lclNum       = unsigned(funcApp.m_args[0]);
                    defArgIsFunc = GetVNFunc(funcApp.m_args[2], &phiFuncApp);
                }
                else
                {
                    assert(funcApp.m_func == VNF_PhiHeapDef);
                    isHeap       = true;
                    defArgIsFunc = GetVNFunc(funcApp.m_args[1], &phiFuncApp);
                }
                if (defArgIsFunc && phiFuncApp.m_func == VNF_Phi)
                {
                    // select(phi(m1, m2), x): if select(m1, x) == select(m2, x), return that, else new fresh.
                    // Get the first argument of the phi.

                    // We need to be careful about breaking infinite recursion.  Record the outer select.
                    m_fixedPointMapSels.Push(VNDefFunc2Arg(VNF_MapSelect, arg0VN, arg1VN));

                    assert(IsVNConstant(phiFuncApp.m_args[0]));
                    unsigned phiArgSsaNum = ConstantValue<unsigned>(phiFuncApp.m_args[0]);
                    ValueNum phiArgVN;
                    if (isHeap)
                    {
                        phiArgVN = m_pComp->GetHeapPerSsaData(phiArgSsaNum)->m_vnPair.Get(vnk);
                    }
                    else
                    {
                        phiArgVN = m_pComp->lvaTable[lclNum].GetPerSsaData(phiArgSsaNum)->m_vnPair.Get(vnk);
                    }
                    if (phiArgVN != ValueNumStore::NoVN)
                    {
                        bool     allSame = true;
                        ValueNum argRest = phiFuncApp.m_args[1];
                        ValueNum sameSelResult =
                            VNForMapSelectWork(vnk, typ, phiArgVN, arg1VN, pBudget, pUsedRecursiveVN);
                        while (allSame && argRest != ValueNumStore::NoVN)
                        {
                            ValueNum  cur = argRest;
                            VNFuncApp phiArgFuncApp;
                            if (GetVNFunc(argRest, &phiArgFuncApp) && phiArgFuncApp.m_func == VNF_Phi)
                            {
                                cur     = phiArgFuncApp.m_args[0];
                                argRest = phiArgFuncApp.m_args[1];
                            }
                            else
                            {
                                argRest = ValueNumStore::NoVN; // Cause the loop to terminate.
                            }
                            assert(IsVNConstant(cur));
                            phiArgSsaNum = ConstantValue<unsigned>(cur);
                            if (isHeap)
                            {
                                phiArgVN = m_pComp->GetHeapPerSsaData(phiArgSsaNum)->m_vnPair.Get(vnk);
                            }
                            else
                            {
                                phiArgVN = m_pComp->lvaTable[lclNum].GetPerSsaData(phiArgSsaNum)->m_vnPair.Get(vnk);
                            }
                            if (phiArgVN == ValueNumStore::NoVN)
                            {
                                allSame = false;
                            }
                            else
                            {
                                bool     usedRecursiveVN = false;
                                ValueNum curResult =
                                    VNForMapSelectWork(vnk, typ, phiArgVN, arg1VN, pBudget, &usedRecursiveVN);
                                *pUsedRecursiveVN |= usedRecursiveVN;
                                if (sameSelResult == ValueNumStore::RecursiveVN)
                                {
                                    sameSelResult = curResult;
                                }
                                if (curResult != ValueNumStore::RecursiveVN && curResult != sameSelResult)
                                {
                                    allSame = false;
                                }
                            }
                        }
                        if (allSame && sameSelResult != ValueNumStore::RecursiveVN)
                        {
                            // Make sure we're popping what we pushed.
                            assert(FixedPointMapSelsTopHasValue(arg0VN, arg1VN));
                            m_fixedPointMapSels.Pop();

                            // To avoid exponential searches, we make sure that this result is memo-ized.
                            // The result is always valid for memoization if we didn't rely on RecursiveVN to get it.
                            // If RecursiveVN was used, we are processing a loop and we can't memo-ize this intermediate
                            // result if, e.g., this block is in a multi-entry loop.
                            if (!*pUsedRecursiveVN)
                            {
                                GetVNFunc2Map()->Set(fstruct, sameSelResult);
                            }

                            return sameSelResult;
                        }
                        // Otherwise, fall through to creating the select(phi(m1, m2), x) function application.
                    }
                    // Make sure we're popping what we pushed.
                    assert(FixedPointMapSelsTopHasValue(arg0VN, arg1VN));
                    m_fixedPointMapSels.Pop();
                }
            }
        }

        // Otherwise, assign a new VN for the function application.
        Chunk*   c                                                     = GetAllocChunk(typ, CEA_Func2);
        unsigned offsetWithinChunk                                     = c->AllocVN();
        res                                                            = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<VNDefFunc2Arg*>(c->m_defs)[offsetWithinChunk] = fstruct;
        GetVNFunc2Map()->Set(fstruct, res);
        return res;
    }
}

ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, ValueNum arg0VN)
{
    assert(CanEvalForConstantArgs(func));
    assert(IsVNConstant(arg0VN));
    switch (TypeOfVN(arg0VN))
    {
        case TYP_INT:
        {
            int resVal = EvalOp(func, ConstantValue<int>(arg0VN));
            // Unary op on a handle results in a handle.
            return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetHandleFlags(arg0VN)) : VNForIntCon(resVal);
        }
        case TYP_LONG:
        {
            INT64 resVal = EvalOp(func, ConstantValue<INT64>(arg0VN));
            // Unary op on a handle results in a handle.
            return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetHandleFlags(arg0VN)) : VNForLongCon(resVal);
        }
        case TYP_FLOAT:
            return VNForFloatCon(EvalOp(func, ConstantValue<float>(arg0VN)));
        case TYP_DOUBLE:
            return VNForDoubleCon(EvalOp(func, ConstantValue<double>(arg0VN)));
        case TYP_REF:
            // If arg0 has a possible exception, it wouldn't have been constant.
            assert(!VNHasExc(arg0VN));
            // Otherwise...
            assert(arg0VN == VNForNull());         // Only other REF constant.
            assert(func == VNFunc(GT_ARR_LENGTH)); // Only function we can apply to a REF constant!
            return VNWithExc(VNForVoid(), VNExcSetSingleton(VNForFunc(TYP_REF, VNF_NullPtrExc, VNForNull())));
        default:
            unreached();
    }
}

bool ValueNumStore::SelectIsBeingEvaluatedRecursively(ValueNum map, ValueNum ind)
{
    for (unsigned i = 0; i < m_fixedPointMapSels.Size(); i++)
    {
        VNDefFunc2Arg& elem = m_fixedPointMapSels.GetRef(i);
        assert(elem.m_func == VNF_MapSelect);
        if (elem.m_arg0 == map && elem.m_arg1 == ind)
        {
            return true;
        }
    }
    return false;
}

#ifdef DEBUG
bool ValueNumStore::FixedPointMapSelsTopHasValue(ValueNum map, ValueNum index)
{
    if (m_fixedPointMapSels.Size() == 0)
    {
        return false;
    }
    VNDefFunc2Arg& top = m_fixedPointMapSels.TopRef();
    return top.m_func == VNF_MapSelect && top.m_arg0 == map && top.m_arg1 == index;
}
#endif

// Given an integer constant value number return its value as an int.
//
int ValueNumStore::GetConstantInt32(ValueNum argVN)
{
    assert(IsVNConstant(argVN));
    var_types argVNtyp = TypeOfVN(argVN);

    int result = 0;

    switch (argVNtyp)
    {
        case TYP_INT:
            result = ConstantValue<int>(argVN);
            break;
#ifndef _TARGET_64BIT_
        case TYP_REF:
        case TYP_BYREF:
            result = (int)ConstantValue<size_t>(argVN);
            break;
#endif
        default:
            unreached();
    }
    return result;
}

// Given an integer constant value number return its value as an INT64.
//
INT64 ValueNumStore::GetConstantInt64(ValueNum argVN)
{
    assert(IsVNConstant(argVN));
    var_types argVNtyp = TypeOfVN(argVN);

    INT64 result = 0;

    switch (argVNtyp)
    {
        case TYP_INT:
            result = (INT64)ConstantValue<int>(argVN);
            break;
        case TYP_LONG:
            result = ConstantValue<INT64>(argVN);
            break;
        case TYP_REF:
        case TYP_BYREF:
            result = (INT64)ConstantValue<size_t>(argVN);
            break;
        default:
            unreached();
    }
    return result;
}

// Given a float or a double constant value number return its value as a double.
//
double ValueNumStore::GetConstantDouble(ValueNum argVN)
{
    assert(IsVNConstant(argVN));
    var_types argVNtyp = TypeOfVN(argVN);

    double result = 0;

    switch (argVNtyp)
    {
        case TYP_FLOAT:
            result = (double)ConstantValue<float>(argVN);
            break;
        case TYP_DOUBLE:
            result = ConstantValue<double>(argVN);
            break;
        default:
            unreached();
    }
    return result;
}

// Compute the proper value number when the VNFunc has all constant arguments
// This essentially performs constant folding at value numbering time
//
ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, ValueNum arg0VN, ValueNum arg1VN)
{
    assert(CanEvalForConstantArgs(func));
    assert(IsVNConstant(arg0VN) && IsVNConstant(arg1VN));
    assert(!VNHasExc(arg0VN) && !VNHasExc(arg1VN)); // Otherwise, would not be constant.

    // if our func is the VNF_Cast operation we handle it first
    if (func == VNF_Cast)
    {
        return EvalCastForConstantArgs(typ, func, arg0VN, arg1VN);
    }

    if (typ == TYP_BYREF)
    {
        // We don't want to fold expressions that produce TYP_BYREF
        return false;
    }

    var_types arg0VNtyp = TypeOfVN(arg0VN);
    var_types arg1VNtyp = TypeOfVN(arg1VN);

    // When both arguments are floating point types
    // We defer to the EvalFuncForConstantFPArgs()
    if (varTypeIsFloating(arg0VNtyp) && varTypeIsFloating(arg1VNtyp))
    {
        return EvalFuncForConstantFPArgs(typ, func, arg0VN, arg1VN);
    }

    // after this we shouldn't have to deal with floating point types for arg0VN or arg1VN
    assert(!varTypeIsFloating(arg0VNtyp));
    assert(!varTypeIsFloating(arg1VNtyp));

    // Stack-normalize the result type.
    if (varTypeIsSmall(typ))
    {
        typ = TYP_INT;
    }

    ValueNum result; // left uninitialized, we are required to initialize it on all paths below.
    ValueNum excSet = VNForEmptyExcSet();

    // Are both args of the same type?
    if (arg0VNtyp == arg1VNtyp)
    {
        if (arg0VNtyp == TYP_INT)
        {
            int arg0Val = ConstantValue<int>(arg0VN);
            int arg1Val = ConstantValue<int>(arg1VN);

            assert(typ == TYP_INT);
            int resultVal = EvalOp(func, arg0Val, arg1Val, &excSet);
            // Bin op on a handle results in a handle.
            ValueNum handleVN = IsVNHandle(arg0VN) ? arg0VN : IsVNHandle(arg1VN) ? arg1VN : NoVN;
            ValueNum resultVN = (handleVN != NoVN)
                                    ? VNForHandle(ssize_t(resultVal), GetHandleFlags(handleVN)) // Use VN for Handle
                                    : VNForIntCon(resultVal);
            result = VNWithExc(resultVN, excSet);
        }
        else if (arg0VNtyp == TYP_LONG)
        {
            INT64 arg0Val = ConstantValue<INT64>(arg0VN);
            INT64 arg1Val = ConstantValue<INT64>(arg1VN);

            if (VNFuncIsComparison(func))
            {
                assert(typ == TYP_INT);
                result = VNForIntCon(EvalComparison(func, arg0Val, arg1Val));
            }
            else
            {
                assert(typ == TYP_LONG);
                INT64    resultVal = EvalOp(func, arg0Val, arg1Val, &excSet);
                ValueNum handleVN  = IsVNHandle(arg0VN) ? arg0VN : IsVNHandle(arg1VN) ? arg1VN : NoVN;
                ValueNum resultVN  = (handleVN != NoVN)
                                        ? VNForHandle(ssize_t(resultVal), GetHandleFlags(handleVN)) // Use VN for Handle
                                        : VNForLongCon(resultVal);
                result = VNWithExc(resultVN, excSet);
            }
        }
        else // both args are TYP_REF or both args are TYP_BYREF
        {
            INT64 arg0Val = ConstantValue<size_t>(arg0VN); // We represent ref/byref constants as size_t's.
            INT64 arg1Val = ConstantValue<size_t>(arg1VN); // Also we consider null to be zero.

            if (VNFuncIsComparison(func))
            {
                assert(typ == TYP_INT);
                result = VNForIntCon(EvalComparison(func, arg0Val, arg1Val));
            }
            else if (typ == TYP_INT) // We could see GT_OR of a constant ByRef and Null
            {
                int resultVal = (int)EvalOp(func, arg0Val, arg1Val, &excSet);
                result        = VNWithExc(VNForIntCon(resultVal), excSet);
            }
            else // We could see GT_OR of a constant ByRef and Null
            {
                assert((typ == TYP_BYREF) || (typ == TYP_LONG));
                INT64 resultVal = EvalOp(func, arg0Val, arg1Val, &excSet);
                result          = VNWithExc(VNForByrefCon(resultVal), excSet);
            }
        }
    }
    else // We have args of different types
    {
        // We represent ref/byref constants as size_t's.
        // Also we consider null to be zero.
        //
        INT64 arg0Val = GetConstantInt64(arg0VN);
        INT64 arg1Val = GetConstantInt64(arg1VN);

        if (VNFuncIsComparison(func))
        {
            assert(typ == TYP_INT);
            result = VNForIntCon(EvalComparison(func, arg0Val, arg1Val));
        }
        else if (typ == TYP_INT) // We could see GT_OR of an int and constant ByRef or Null
        {
            int resultVal = (int)EvalOp(func, arg0Val, arg1Val, &excSet);
            result        = VNWithExc(VNForIntCon(resultVal), excSet);
        }
        else
        {
            assert(typ != TYP_INT);
            ValueNum resultValx = VNForEmptyExcSet();
            INT64    resultVal  = EvalOp(func, arg0Val, arg1Val, &resultValx);

            // check for the Exception case
            if (resultValx != VNForEmptyExcSet())
            {
                result = VNWithExc(VNForVoid(), resultValx);
            }
            else
            {
                switch (typ)
                {
                    case TYP_BYREF:
                        result = VNForByrefCon(resultVal);
                        break;
                    case TYP_LONG:
                        result = VNForLongCon(resultVal);
                        break;
                    case TYP_REF:
                        assert(resultVal == 0); // Only valid REF constant
                        result = VNForNull();
                        break;
                    default:
                        unreached();
                }
            }
        }
    }

    return result;
}

// Compute the proper value number when the VNFunc has all constant floating-point arguments
// This essentially must perform constant folding at value numbering time
//
ValueNum ValueNumStore::EvalFuncForConstantFPArgs(var_types typ, VNFunc func, ValueNum arg0VN, ValueNum arg1VN)
{
    assert(CanEvalForConstantArgs(func));
    assert(IsVNConstant(arg0VN) && IsVNConstant(arg1VN));

    // We expect both argument types to be floating point types
    var_types arg0VNtyp = TypeOfVN(arg0VN);
    var_types arg1VNtyp = TypeOfVN(arg1VN);

    assert(varTypeIsFloating(arg0VNtyp));
    assert(varTypeIsFloating(arg1VNtyp));

    double arg0Val = GetConstantDouble(arg0VN);
    double arg1Val = GetConstantDouble(arg1VN);

    ValueNum result; // left uninitialized, we are required to initialize it on all paths below.

    if (VNFuncIsComparison(func))
    {
        assert(genActualType(typ) == TYP_INT);
        result = VNForIntCon(EvalComparison(func, arg0Val, arg1Val));
    }
    else
    {
        assert(varTypeIsFloating(typ)); // We must be computing a floating point result

        // We always compute the result using a double
        ValueNum exception       = VNForEmptyExcSet();
        double   doubleResultVal = EvalOp(func, arg0Val, arg1Val, &exception);
        assert(exception == VNForEmptyExcSet()); // Floating point ops don't throw.

        if (typ == TYP_FLOAT)
        {
            float floatResultVal = float(doubleResultVal);
            result               = VNForFloatCon(floatResultVal);
        }
        else
        {
            assert(typ == TYP_DOUBLE);
            result = VNForDoubleCon(doubleResultVal);
        }
    }

    return result;
}

// Compute the proper value number for a VNF_Cast with constant arguments
// This essentially must perform constant folding at value numbering time
//
ValueNum ValueNumStore::EvalCastForConstantArgs(var_types typ, VNFunc func, ValueNum arg0VN, ValueNum arg1VN)
{
    assert(func == VNF_Cast);
    assert(IsVNConstant(arg0VN) && IsVNConstant(arg1VN));

    // Stack-normalize the result type.
    if (varTypeIsSmall(typ))
    {
        typ = TYP_INT;
    }

    var_types arg0VNtyp = TypeOfVN(arg0VN);
    var_types arg1VNtyp = TypeOfVN(arg1VN);

    // arg1VN is really the gtCastType that we are casting to
    assert(arg1VNtyp == TYP_INT);
    int arg1Val = ConstantValue<int>(arg1VN);
    assert(arg1Val >= 0);

    if (IsVNHandle(arg0VN))
    {
        // We don't allow handles to be cast to random var_types.
        assert(typ == TYP_I_IMPL);
    }

    // We previously encoded the castToType operation using vnForCastOper()
    //
    bool      srcIsUnsigned = ((arg1Val & INT32(VCA_UnsignedSrc)) != 0);
    var_types castToType    = var_types(arg1Val >> INT32(VCA_BitCount));

    var_types castFromType = arg0VNtyp;

    switch (castFromType) // GT_CAST source type
    {
#ifndef _TARGET_64BIT_
        case TYP_REF:
#endif
        case TYP_INT:
        {
            int arg0Val = GetConstantInt32(arg0VN);

            switch (castToType)
            {
                case TYP_BYTE:
                    assert(typ == TYP_INT);
                    return VNForIntCon(INT8(arg0Val));
                case TYP_BOOL:
                case TYP_UBYTE:
                    assert(typ == TYP_INT);
                    return VNForIntCon(UINT8(arg0Val));
                case TYP_SHORT:
                    assert(typ == TYP_INT);
                    return VNForIntCon(INT16(arg0Val));
                case TYP_CHAR:
                case TYP_USHORT:
                    assert(typ == TYP_INT);
                    return VNForIntCon(UINT16(arg0Val));
                case TYP_INT:
                case TYP_UINT:
                    assert(typ == TYP_INT);
                    return arg0VN;
                case TYP_LONG:
                case TYP_ULONG:
                    assert(!IsVNHandle(arg0VN));
#ifdef _TARGET_64BIT_
                    if (typ == TYP_LONG)
                    {
                        if (srcIsUnsigned)
                        {
                            return VNForLongCon(INT64(unsigned(arg0Val)));
                        }
                        else
                        {
                            return VNForLongCon(INT64(arg0Val));
                        }
                    }
                    else
                    {
                        assert(typ == TYP_BYREF);
                        if (srcIsUnsigned)
                        {
                            return VNForByrefCon(INT64(unsigned(arg0Val)));
                        }
                        else
                        {
                            return VNForByrefCon(INT64(arg0Val));
                        }
                    }
#else // TARGET_32BIT
                    if (srcIsUnsigned)
                        return VNForLongCon(INT64(unsigned(arg0Val)));
                    else
                        return VNForLongCon(INT64(arg0Val));
#endif
                case TYP_FLOAT:
                    assert(typ == TYP_FLOAT);
                    if (srcIsUnsigned)
                    {
                        return VNForFloatCon(float(unsigned(arg0Val)));
                    }
                    else
                    {
                        return VNForFloatCon(float(arg0Val));
                    }
                case TYP_DOUBLE:
                    assert(typ == TYP_DOUBLE);
                    if (srcIsUnsigned)
                    {
                        return VNForDoubleCon(double(unsigned(arg0Val)));
                    }
                    else
                    {
                        return VNForDoubleCon(double(arg0Val));
                    }
                default:
                    unreached();
            }
            break;
        }
            {
#ifdef _TARGET_64BIT_
                case TYP_REF:
#endif
                case TYP_LONG:
                    INT64 arg0Val = GetConstantInt64(arg0VN);

                    switch (castToType)
                    {
                        case TYP_BYTE:
                            assert(typ == TYP_INT);
                            return VNForIntCon(INT8(arg0Val));
                        case TYP_BOOL:
                        case TYP_UBYTE:
                            assert(typ == TYP_INT);
                            return VNForIntCon(UINT8(arg0Val));
                        case TYP_SHORT:
                            assert(typ == TYP_INT);
                            return VNForIntCon(INT16(arg0Val));
                        case TYP_CHAR:
                        case TYP_USHORT:
                            assert(typ == TYP_INT);
                            return VNForIntCon(UINT16(arg0Val));
                        case TYP_INT:
                            assert(typ == TYP_INT);
                            return VNForIntCon(INT32(arg0Val));
                        case TYP_UINT:
                            assert(typ == TYP_INT);
                            return VNForIntCon(UINT32(arg0Val));
                        case TYP_LONG:
                        case TYP_ULONG:
                            assert(typ == TYP_LONG);
                            return arg0VN;
                        case TYP_FLOAT:
                            assert(typ == TYP_FLOAT);
                            if (srcIsUnsigned)
                            {
                                return VNForFloatCon(FloatingPointUtils::convertUInt64ToFloat(UINT64(arg0Val)));
                            }
                            else
                            {
                                return VNForFloatCon(float(arg0Val));
                            }
                        case TYP_DOUBLE:
                            assert(typ == TYP_DOUBLE);
                            if (srcIsUnsigned)
                            {
                                return VNForDoubleCon(FloatingPointUtils::convertUInt64ToDouble(UINT64(arg0Val)));
                            }
                            else
                            {
                                return VNForDoubleCon(double(arg0Val));
                            }
                        default:
                            unreached();
                    }
            }
        case TYP_FLOAT:
        case TYP_DOUBLE:
        {
            double arg0Val = GetConstantDouble(arg0VN);

            switch (castToType)
            {
                case TYP_BYTE:
                    assert(typ == TYP_INT);
                    return VNForIntCon(INT8(arg0Val));
                case TYP_BOOL:
                case TYP_UBYTE:
                    assert(typ == TYP_INT);
                    return VNForIntCon(UINT8(arg0Val));
                case TYP_SHORT:
                    assert(typ == TYP_INT);
                    return VNForIntCon(INT16(arg0Val));
                case TYP_CHAR:
                case TYP_USHORT:
                    assert(typ == TYP_INT);
                    return VNForIntCon(UINT16(arg0Val));
                case TYP_INT:
                    assert(typ == TYP_INT);
                    return VNForIntCon(INT32(arg0Val));
                case TYP_UINT:
                    assert(typ == TYP_INT);
                    return VNForIntCon(UINT32(arg0Val));
                case TYP_LONG:
                    assert(typ == TYP_LONG);
                    return VNForLongCon(INT64(arg0Val));
                case TYP_ULONG:
                    assert(typ == TYP_LONG);
                    return VNForLongCon(UINT64(arg0Val));
                case TYP_FLOAT:
                    assert(typ == TYP_FLOAT);
                    return VNForFloatCon(float(arg0Val));
                case TYP_DOUBLE:
                    assert(typ == TYP_DOUBLE);
                    return VNForDoubleCon(arg0Val);
                default:
                    unreached();
            }
        }
        default:
            unreached();
    }
}

bool ValueNumStore::CanEvalForConstantArgs(VNFunc vnf)
{
    if (vnf < VNF_Boundary)
    {
        // We'll refine this as we get counterexamples.  But to
        // a first approximation, VNFuncs that are genTreeOps should
        // be things we can evaluate.
        genTreeOps oper = genTreeOps(vnf);
        // Some exceptions...
        switch (oper)
        {
            case GT_MKREFANY: // We can't evaluate these.
            case GT_RETFILT:
            case GT_LIST:
            case GT_FIELD_LIST:
            case GT_ARR_LENGTH:
                return false;
            case GT_MULHI:
                // should be rare, not worth the complexity and risk of getting it wrong
                return false;
            default:
                return true;
        }
    }
    else
    {
        // some VNF_ that we can evaluate
        switch (vnf)
        {
            case VNF_Cast: // We can evaluate these.
                return true;
            case VNF_ObjGetType:
                return false;
            default:
                return false;
        }
    }
}

unsigned ValueNumStore::VNFuncArity(VNFunc vnf)
{
    // Read the bit field out of the table...
    return (s_vnfOpAttribs[vnf] & VNFOA_ArityMask) >> VNFOA_ArityShift;
}

template <>
bool ValueNumStore::IsOverflowIntDiv(int v0, int v1)
{
    return (v1 == -1) && (v0 == INT32_MIN);
}
template <>
bool ValueNumStore::IsOverflowIntDiv(INT64 v0, INT64 v1)
{
    return (v1 == -1) && (v0 == INT64_MIN);
}
template <typename T>
bool ValueNumStore::IsOverflowIntDiv(T v0, T v1)
{
    return false;
}

template <>
bool ValueNumStore::IsIntZero(int v)
{
    return v == 0;
}
template <>
bool ValueNumStore::IsIntZero(unsigned v)
{
    return v == 0;
}
template <>
bool ValueNumStore::IsIntZero(INT64 v)
{
    return v == 0;
}
template <>
bool ValueNumStore::IsIntZero(UINT64 v)
{
    return v == 0;
}
template <typename T>
bool ValueNumStore::IsIntZero(T v)
{
    return false;
}

template <>
float ValueNumStore::EvalOpIntegral<float>(VNFunc vnf, float v0)
{
    assert(!"EvalOpIntegral<float>");
    return 0.0f;
}

template <>
double ValueNumStore::EvalOpIntegral<double>(VNFunc vnf, double v0)
{
    assert(!"EvalOpIntegral<double>");
    return 0.0;
}

template <>
float ValueNumStore::EvalOpIntegral<float>(VNFunc vnf, float v0, float v1, ValueNum* pExcSet)
{
    genTreeOps oper = genTreeOps(vnf);
    switch (oper)
    {
        case GT_MOD:
            return fmodf(v0, v1);
        default:
            // For any other values of 'oper', we will assert and return 0.0f
            break;
    }
    assert(!"EvalOpIntegral<float> with pExcSet");
    return 0.0f;
}

template <>
double ValueNumStore::EvalOpIntegral<double>(VNFunc vnf, double v0, double v1, ValueNum* pExcSet)
{
    genTreeOps oper = genTreeOps(vnf);
    switch (oper)
    {
        case GT_MOD:
            return fmod(v0, v1);
        default:
            // For any other value of 'oper', we will assert and return 0.0
            break;
    }
    assert(!"EvalOpIntegral<double> with pExcSet");
    return 0.0;
}

ValueNum ValueNumStore::VNForFunc(var_types typ, VNFunc func, ValueNum arg0VN, ValueNum arg1VN, ValueNum arg2VN)
{
    assert(arg0VN != NoVN);
    assert(arg1VN != NoVN);
    assert(arg2VN != NoVN);
    assert(VNFuncArity(func) == 3);

    // Function arguments carry no exceptions.
    CLANG_FORMAT_COMMENT_ANCHOR;

#ifdef DEBUG
    if (func != VNF_PhiDef)
    {
        // For a phi definition first and second argument are "plain" local/ssa numbers.
        // (I don't know if having such non-VN arguments to a VN function is a good idea -- if we wanted to declare
        // ValueNum to be "short" it would be a problem, for example.  But we'll leave it for now, with these explicit
        // exceptions.)
        assert(arg0VN == VNNormVal(arg0VN));
        assert(arg1VN == VNNormVal(arg1VN));
    }
    assert(arg2VN == VNNormVal(arg2VN));

#endif
    assert(VNFuncArity(func) == 3);

    ValueNum      res;
    VNDefFunc3Arg fstruct(func, arg0VN, arg1VN, arg2VN);
    if (GetVNFunc3Map()->Lookup(fstruct, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                                     = GetAllocChunk(typ, CEA_Func3);
        unsigned offsetWithinChunk                                     = c->AllocVN();
        res                                                            = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<VNDefFunc3Arg*>(c->m_defs)[offsetWithinChunk] = fstruct;
        GetVNFunc3Map()->Set(fstruct, res);
        return res;
    }
}

ValueNum ValueNumStore::VNForFunc(
    var_types typ, VNFunc func, ValueNum arg0VN, ValueNum arg1VN, ValueNum arg2VN, ValueNum arg3VN)
{
    assert(arg0VN != NoVN && arg1VN != NoVN && arg2VN != NoVN && arg3VN != NoVN);
    // Function arguments carry no exceptions.
    assert(arg0VN == VNNormVal(arg0VN));
    assert(arg1VN == VNNormVal(arg1VN));
    assert(arg2VN == VNNormVal(arg2VN));
    assert(arg3VN == VNNormVal(arg3VN));
    assert(VNFuncArity(func) == 4);

    ValueNum      res;
    VNDefFunc4Arg fstruct(func, arg0VN, arg1VN, arg2VN, arg3VN);
    if (GetVNFunc4Map()->Lookup(fstruct, &res))
    {
        return res;
    }
    else
    {
        Chunk*   c                                                     = GetAllocChunk(typ, CEA_Func4);
        unsigned offsetWithinChunk                                     = c->AllocVN();
        res                                                            = c->m_baseVN + offsetWithinChunk;
        reinterpret_cast<VNDefFunc4Arg*>(c->m_defs)[offsetWithinChunk] = fstruct;
        GetVNFunc4Map()->Set(fstruct, res);
        return res;
    }
}

//------------------------------------------------------------------------
// VNForExpr: Opaque value number that is equivalent to itself but unique
//    from all other value numbers.
//
// Arguments:
//    block - BasicBlock where the expression that produces this value occurs.
//            May be nullptr to force conservative "could be anywhere" interpretation.
//     typ - Type of the expression in the IR
//
// Return Value:
//    A new value number distinct from any previously generated, that compares as equal
//    to itself, but not any other value number, and is annotated with the given
//    type and block.

ValueNum ValueNumStore::VNForExpr(BasicBlock* block, var_types typ)
{
    BasicBlock::loopNumber loopNum;
    if (block == nullptr)
    {
        loopNum = MAX_LOOP_NUM;
    }
    else
    {
        loopNum = block->bbNatLoopNum;
    }

    // We always allocate a new, unique VN in this call.
    // The 'typ' is used to partition the allocation of VNs into different chunks.
    Chunk*   c                 = GetAllocChunk(typ, CEA_None, loopNum);
    unsigned offsetWithinChunk = c->AllocVN();
    ValueNum result            = c->m_baseVN + offsetWithinChunk;
    return result;
}

ValueNum ValueNumStore::VNApplySelectors(ValueNumKind  vnk,
                                         ValueNum      map,
                                         FieldSeqNode* fieldSeq,
                                         size_t*       wbFinalStructSize)
{
    if (fieldSeq == nullptr)
    {
        return map;
    }
    else
    {
        assert(fieldSeq != FieldSeqStore::NotAField());

        // Skip any "FirstElem" pseudo-fields or any "ConstantIndex" pseudo-fields
        if (fieldSeq->IsPseudoField())
        {
            return VNApplySelectors(vnk, map, fieldSeq->m_next, wbFinalStructSize);
        }

        // Otherwise, is a real field handle.
        CORINFO_FIELD_HANDLE fldHnd    = fieldSeq->m_fieldHnd;
        CORINFO_CLASS_HANDLE structHnd = NO_CLASS_HANDLE;
        ValueNum             fldHndVN  = VNForHandle(ssize_t(fldHnd), GTF_ICON_FIELD_HDL);
        noway_assert(fldHnd != nullptr);
        CorInfoType fieldCit  = m_pComp->info.compCompHnd->getFieldType(fldHnd, &structHnd);
        var_types   fieldType = JITtype2varType(fieldCit);

        size_t structSize = 0;
        if (varTypeIsStruct(fieldType))
        {
            structSize = m_pComp->info.compCompHnd->getClassSize(structHnd);
            // We do not normalize the type field accesses during importation unless they
            // are used in a call, return or assignment.
            if ((fieldType == TYP_STRUCT) && (structSize <= m_pComp->largestEnregisterableStructSize()))
            {
                fieldType = m_pComp->impNormStructType(structHnd);
            }
        }
        if (wbFinalStructSize != nullptr)
        {
            *wbFinalStructSize = structSize;
        }

#ifdef DEBUG
        if (m_pComp->verbose)
        {
            printf("  VNApplySelectors:\n");
            const char* modName;
            const char* fldName = m_pComp->eeGetFieldName(fldHnd, &modName);
            printf("    VNForHandle(Fseq[%s]) is " STR_VN "%x, fieldType is %s", fldName, fldHndVN,
                   varTypeName(fieldType));
            if (varTypeIsStruct(fieldType))
            {
                printf(", size = %d", structSize);
            }
            printf("\n");
        }
#endif

        if (fieldSeq->m_next != nullptr)
        {
            ValueNum newMap = VNForMapSelect(vnk, fieldType, map, fldHndVN);
            return VNApplySelectors(vnk, newMap, fieldSeq->m_next, wbFinalStructSize);
        }
        else // end of fieldSeq
        {
            return VNForMapSelect(vnk, fieldType, map, fldHndVN);
        }
    }
}

ValueNum ValueNumStore::VNApplySelectorsTypeCheck(ValueNum elem, var_types indType, size_t elemStructSize)
{
    var_types elemTyp = TypeOfVN(elem);

    // Check if the elemTyp is matching/compatible

    if (indType != elemTyp)
    {
        bool isConstant = IsVNConstant(elem);
        if (isConstant && (elemTyp == genActualType(indType)))
        {
            // (i.e. We recorded a constant of TYP_INT for a TYP_BYTE field)
        }
        else
        {
            // We are trying to read from an 'elem' of type 'elemType' using 'indType' read

            size_t elemTypSize = (elemTyp == TYP_STRUCT) ? elemStructSize : genTypeSize(elemTyp);
            size_t indTypeSize = genTypeSize(indType);

            if ((indType == TYP_REF) && (varTypeIsStruct(elemTyp)))
            {
                // indType is TYP_REF and elemTyp is TYP_STRUCT
                //
                // We have a pointer to a static that is a Boxed Struct
                //
                return elem;
            }
            else if (indTypeSize > elemTypSize)
            {
                // Reading beyong the end of 'elem'

                // return a new unique value number
                elem = VNForExpr(nullptr, indType);
                JITDUMP("    *** Mismatched types in VNApplySelectorsTypeCheck (reading beyond the end)\n");
            }
            else if (varTypeIsStruct(indType))
            {
                // indType is TYP_STRUCT

                // return a new unique value number
                elem = VNForExpr(nullptr, indType);
                JITDUMP("    *** Mismatched types in VNApplySelectorsTypeCheck (indType is TYP_STRUCT)\n");
            }
            else
            {
                // We are trying to read an 'elem' of type 'elemType' using 'indType' read

                // insert a cast of elem to 'indType'
                elem = VNForCast(elem, indType, elemTyp);
            }
        }
    }
    return elem;
}

ValueNum ValueNumStore::VNApplySelectorsAssignTypeCoerce(ValueNum elem, var_types indType, BasicBlock* block)
{
    var_types elemTyp = TypeOfVN(elem);

    // Check if the elemTyp is matching/compatible

    if (indType != elemTyp)
    {
        bool isConstant = IsVNConstant(elem);
        if (isConstant && (elemTyp == genActualType(indType)))
        {
            // (i.e. We recorded a constant of TYP_INT for a TYP_BYTE field)
        }
        else
        {
            // We are trying to write an 'elem' of type 'elemType' using 'indType' store

            if (varTypeIsStruct(indType))
            {
                // return a new unique value number
                elem = VNForExpr(block, indType);
                JITDUMP("    *** Mismatched types in VNApplySelectorsAssignTypeCoerce (indType is TYP_STRUCT)\n");
            }
            else
            {
                // We are trying to write an 'elem' of type 'elemType' using 'indType' store

                // insert a cast of elem to 'indType'
                elem = VNForCast(elem, indType, elemTyp);
            }
        }
    }
    return elem;
}

//------------------------------------------------------------------------
// VNApplySelectorsAssign: Compute the value number corresponding to "map" but with
//    the element at "fieldSeq" updated to have type "elem"; this is the new heap
//    value for an assignment of value "elem" into the heap at location "fieldSeq"
//    that occurs in block "block" and has type "indType".
//
// Arguments:
//    vnk - Identifies whether to recurse to Conservative or Liberal value numbers
//          when recursing through phis
//    map - Value number for the field map before the assignment
//    elem - Value number for the value being stored (to the given field)
//    indType - Type of the indirection storing the value to the field
//    block - Block where the assignment occurs
//
// Return Value:
//    The value number corresopnding to the heap after the assignment.

ValueNum ValueNumStore::VNApplySelectorsAssign(
    ValueNumKind vnk, ValueNum map, FieldSeqNode* fieldSeq, ValueNum elem, var_types indType, BasicBlock* block)
{
    if (fieldSeq == nullptr)
    {
        return VNApplySelectorsAssignTypeCoerce(elem, indType, block);
    }
    else
    {
        assert(fieldSeq != FieldSeqStore::NotAField());

        // Skip any "FirstElem" pseudo-fields or any "ConstantIndex" pseudo-fields
        // These will occur, at least, in struct static expressions, for method table offsets.
        if (fieldSeq->IsPseudoField())
        {
            return VNApplySelectorsAssign(vnk, map, fieldSeq->m_next, elem, indType, block);
        }

        // Otherwise, fldHnd is a real field handle.
        CORINFO_FIELD_HANDLE fldHnd     = fieldSeq->m_fieldHnd;
        CORINFO_CLASS_HANDLE structType = nullptr;
        noway_assert(fldHnd != nullptr);
        CorInfoType fieldCit  = m_pComp->info.compCompHnd->getFieldType(fldHnd, &structType);
        var_types   fieldType = JITtype2varType(fieldCit);

        ValueNum fieldHndVN = VNForHandle(ssize_t(fldHnd), GTF_ICON_FIELD_HDL);

#ifdef DEBUG
        if (m_pComp->verbose)
        {
            printf("  fieldHnd " STR_VN "%x is ", fieldHndVN);
            vnDump(m_pComp, fieldHndVN);
            printf("\n");

            ValueNum seqNextVN  = VNForFieldSeq(fieldSeq->m_next);
            ValueNum fieldSeqVN = VNForFunc(TYP_REF, VNF_FieldSeq, fieldHndVN, seqNextVN);

            printf("  fieldSeq " STR_VN "%x is ", fieldSeqVN);
            vnDump(m_pComp, fieldSeqVN);
            printf("\n");
        }
#endif

        ValueNum elemAfter;
        if (fieldSeq->m_next)
        {
            ValueNum fseqMap = VNForMapSelect(vnk, fieldType, map, fieldHndVN);
            elemAfter        = VNApplySelectorsAssign(vnk, fseqMap, fieldSeq->m_next, elem, indType, block);
        }
        else
        {
            elemAfter = VNApplySelectorsAssignTypeCoerce(elem, indType, block);
        }

        ValueNum newMap = VNForMapStore(fieldType, map, fieldHndVN, elemAfter);
        return newMap;
    }
}

ValueNumPair ValueNumStore::VNPairApplySelectors(ValueNumPair map, FieldSeqNode* fieldSeq, var_types indType)
{
    size_t   structSize = 0;
    ValueNum liberalVN  = VNApplySelectors(VNK_Liberal, map.GetLiberal(), fieldSeq, &structSize);
    liberalVN           = VNApplySelectorsTypeCheck(liberalVN, indType, structSize);

    structSize         = 0;
    ValueNum conservVN = VNApplySelectors(VNK_Conservative, map.GetConservative(), fieldSeq, &structSize);
    conservVN          = VNApplySelectorsTypeCheck(conservVN, indType, structSize);

    return ValueNumPair(liberalVN, conservVN);
}

ValueNum ValueNumStore::VNForFieldSeq(FieldSeqNode* fieldSeq)
{
    if (fieldSeq == nullptr)
    {
        return VNForNull();
    }
    else if (fieldSeq == FieldSeqStore::NotAField())
    {
        return VNForNotAField();
    }
    else
    {
        ssize_t  fieldHndVal = ssize_t(fieldSeq->m_fieldHnd);
        ValueNum fieldHndVN  = VNForHandle(fieldHndVal, GTF_ICON_FIELD_HDL);
        ValueNum seqNextVN   = VNForFieldSeq(fieldSeq->m_next);
        ValueNum fieldSeqVN  = VNForFunc(TYP_REF, VNF_FieldSeq, fieldHndVN, seqNextVN);

#ifdef DEBUG
        if (m_pComp->verbose)
        {
            printf("  fieldHnd " STR_VN "%x is ", fieldHndVN);
            vnDump(m_pComp, fieldHndVN);
            printf("\n");

            printf("  fieldSeq " STR_VN "%x is ", fieldSeqVN);
            vnDump(m_pComp, fieldSeqVN);
            printf("\n");
        }
#endif

        return fieldSeqVN;
    }
}

FieldSeqNode* ValueNumStore::FieldSeqVNToFieldSeq(ValueNum vn)
{
    if (vn == VNForNull())
    {
        return nullptr;
    }
    else if (vn == VNForNotAField())
    {
        return FieldSeqStore::NotAField();
    }
    else
    {
        assert(IsVNFunc(vn));
        VNFuncApp funcApp;
        GetVNFunc(vn, &funcApp);
        assert(funcApp.m_func == VNF_FieldSeq);
        ssize_t       fieldHndVal = ConstantValue<ssize_t>(funcApp.m_args[0]);
        FieldSeqNode* head =
            m_pComp->GetFieldSeqStore()->CreateSingleton(reinterpret_cast<CORINFO_FIELD_HANDLE>(fieldHndVal));
        FieldSeqNode* tail = FieldSeqVNToFieldSeq(funcApp.m_args[1]);
        return m_pComp->GetFieldSeqStore()->Append(head, tail);
    }
}

ValueNum ValueNumStore::FieldSeqVNAppend(ValueNum fsVN1, ValueNum fsVN2)
{
    if (fsVN1 == VNForNull())
    {
        return fsVN2;
    }
    else if (fsVN1 == VNForNotAField() || fsVN2 == VNForNotAField())
    {
        return VNForNotAField();
    }
    else
    {
        assert(IsVNFunc(fsVN1));
        VNFuncApp funcApp1;
        GetVNFunc(fsVN1, &funcApp1);
        assert(funcApp1.m_func == VNF_FieldSeq);
        ValueNum tailRes    = FieldSeqVNAppend(funcApp1.m_args[1], fsVN2);
        ValueNum fieldSeqVN = VNForFunc(TYP_REF, VNF_FieldSeq, funcApp1.m_args[0], tailRes);

#ifdef DEBUG
        if (m_pComp->verbose)
        {
            printf("  fieldSeq " STR_VN "%x is ", fieldSeqVN);
            vnDump(m_pComp, fieldSeqVN);
            printf("\n");
        }
#endif

        return fieldSeqVN;
    }
}

ValueNum ValueNumStore::VNForPtrToLoc(var_types typ, ValueNum lclVarVN, ValueNum fieldSeqVN)
{
    if (fieldSeqVN == VNForNotAField())
    {
        // To distinguish two different not a fields, append a unique value.
        return VNForFunc(typ, VNF_PtrToLoc, lclVarVN, fieldSeqVN, VNForIntCon(++m_uPtrToLocNotAFieldCount));
    }
    return VNForFunc(typ, VNF_PtrToLoc, lclVarVN, fieldSeqVN, VNForIntCon(0));
}

ValueNum ValueNumStore::ExtendPtrVN(GenTreePtr opA, GenTreePtr opB)
{
    if (opB->OperGet() == GT_CNS_INT)
    {
        FieldSeqNode* fldSeq = opB->gtIntCon.gtFieldSeq;
        if ((fldSeq != nullptr) && (fldSeq != FieldSeqStore::NotAField()))
        {
            return ExtendPtrVN(opA, opB->gtIntCon.gtFieldSeq);
        }
    }
    return NoVN;
}

ValueNum ValueNumStore::ExtendPtrVN(GenTreePtr opA, FieldSeqNode* fldSeq)
{
    ValueNum res = NoVN;
    assert(fldSeq != FieldSeqStore::NotAField());

    ValueNum opAvnWx = opA->gtVNPair.GetLiberal();
    assert(VNIsValid(opAvnWx));
    ValueNum opAvn;
    ValueNum opAvnx = VNForEmptyExcSet();
    VNUnpackExc(opAvnWx, &opAvn, &opAvnx);
    assert(VNIsValid(opAvn) && VNIsValid(opAvnx));

    VNFuncApp funcApp;
    if (!GetVNFunc(opAvn, &funcApp))
    {
        return res;
    }

    if (funcApp.m_func == VNF_PtrToLoc)
    {
#ifdef DEBUG
        // For PtrToLoc, lib == cons.
        VNFuncApp consFuncApp;
        assert(GetVNFunc(VNNormVal(opA->GetVN(VNK_Conservative)), &consFuncApp) && consFuncApp.Equals(funcApp));
#endif
        ValueNum fldSeqVN = VNForFieldSeq(fldSeq);
        res               = VNForPtrToLoc(TYP_BYREF, funcApp.m_args[0], FieldSeqVNAppend(funcApp.m_args[1], fldSeqVN));
    }
    else if (funcApp.m_func == VNF_PtrToStatic)
    {
        ValueNum fldSeqVN = VNForFieldSeq(fldSeq);
        res               = VNForFunc(TYP_BYREF, VNF_PtrToStatic, FieldSeqVNAppend(funcApp.m_args[0], fldSeqVN));
    }
    else if (funcApp.m_func == VNF_PtrToArrElem)
    {
        ValueNum fldSeqVN = VNForFieldSeq(fldSeq);
        res = VNForFunc(TYP_BYREF, VNF_PtrToArrElem, funcApp.m_args[0], funcApp.m_args[1], funcApp.m_args[2],
                        FieldSeqVNAppend(funcApp.m_args[3], fldSeqVN));
    }
    if (res != NoVN)
    {
        res = VNWithExc(res, opAvnx);
    }
    return res;
}

void Compiler::fgValueNumberArrIndexAssign(CORINFO_CLASS_HANDLE elemTypeEq,
                                           ValueNum             arrVN,
                                           ValueNum             inxVN,
                                           FieldSeqNode*        fldSeq,
                                           ValueNum             rhsVN,
                                           var_types            indType)
{
    bool      invalidateArray      = false;
    ValueNum  elemTypeEqVN         = vnStore->VNForHandle(ssize_t(elemTypeEq), GTF_ICON_CLASS_HDL);
    var_types arrElemType          = DecodeElemType(elemTypeEq);
    ValueNum  hAtArrType           = vnStore->VNForMapSelect(VNK_Liberal, TYP_REF, fgCurHeapVN, elemTypeEqVN);
    ValueNum  hAtArrTypeAtArr      = vnStore->VNForMapSelect(VNK_Liberal, TYP_REF, hAtArrType, arrVN);
    ValueNum  hAtArrTypeAtArrAtInx = vnStore->VNForMapSelect(VNK_Liberal, arrElemType, hAtArrTypeAtArr, inxVN);

    ValueNum newValAtInx     = ValueNumStore::NoVN;
    ValueNum newValAtArr     = ValueNumStore::NoVN;
    ValueNum newValAtArrType = ValueNumStore::NoVN;

    if (fldSeq == FieldSeqStore::NotAField())
    {
        // This doesn't represent a proper array access
        JITDUMP("    *** NotAField sequence encountered in fgValueNumberArrIndexAssign\n");

        // Store a new unique value for newValAtArrType
        newValAtArrType = vnStore->VNForExpr(compCurBB, TYP_REF);
        invalidateArray = true;
    }
    else
    {
        // Note that this does the right thing if "fldSeq" is null -- returns last "rhs" argument.
        // This is the value that should be stored at "arr[inx]".
        newValAtInx =
            vnStore->VNApplySelectorsAssign(VNK_Liberal, hAtArrTypeAtArrAtInx, fldSeq, rhsVN, indType, compCurBB);

        var_types arrElemFldType = arrElemType; // Uses arrElemType unless we has a non-null fldSeq
        if (vnStore->IsVNFunc(newValAtInx))
        {
            VNFuncApp funcApp;
            vnStore->GetVNFunc(newValAtInx, &funcApp);
            if (funcApp.m_func == VNF_MapStore)
            {
                arrElemFldType = vnStore->TypeOfVN(newValAtInx);
            }
        }

        if (indType != arrElemFldType)
        {
            // Mismatched types: Store between different types (indType into array of arrElemFldType)
            //

            JITDUMP("    *** Mismatched types in fgValueNumberArrIndexAssign\n");

            // Store a new unique value for newValAtArrType
            newValAtArrType = vnStore->VNForExpr(compCurBB, TYP_REF);
            invalidateArray = true;
        }
    }

    if (!invalidateArray)
    {
        newValAtArr     = vnStore->VNForMapStore(indType, hAtArrTypeAtArr, inxVN, newValAtInx);
        newValAtArrType = vnStore->VNForMapStore(TYP_REF, hAtArrType, arrVN, newValAtArr);
    }

#ifdef DEBUG
    if (verbose)
    {
        printf("  hAtArrType " STR_VN "%x is MapSelect(curHeap(" STR_VN "%x), ", hAtArrType, fgCurHeapVN);

        if (arrElemType == TYP_STRUCT)
        {
            printf("%s[]).\n", eeGetClassName(elemTypeEq));
        }
        else
        {
            printf("%s[]).\n", varTypeName(arrElemType));
        }
        printf("  hAtArrTypeAtArr " STR_VN "%x is MapSelect(hAtArrType(" STR_VN "%x), arr=" STR_VN "%x)\n",
               hAtArrTypeAtArr, hAtArrType, arrVN);
        printf("  hAtArrTypeAtArrAtInx " STR_VN "%x is MapSelect(hAtArrTypeAtArr(" STR_VN "%x), inx=" STR_VN "%x):%s\n",
               hAtArrTypeAtArrAtInx, hAtArrTypeAtArr, inxVN, varTypeName(arrElemType));

        if (!invalidateArray)
        {
            printf("  newValAtInd " STR_VN "%x is ", newValAtInx);
            vnStore->vnDump(this, newValAtInx);
            printf("\n");

            printf("  newValAtArr " STR_VN "%x is ", newValAtArr);
            vnStore->vnDump(this, newValAtArr);
            printf("\n");
        }

        printf("  newValAtArrType " STR_VN "%x is ", newValAtArrType);
        vnStore->vnDump(this, newValAtArrType);
        printf("\n");

        printf("  fgCurHeapVN assigned:\n");
    }
#endif // DEBUG

    // bbHeapDef must be set to true for any block that Mutates the global Heap
    assert(compCurBB->bbHeapDef);

    fgCurHeapVN = vnStore->VNForMapStore(TYP_REF, fgCurHeapVN, elemTypeEqVN, newValAtArrType);
}

ValueNum Compiler::fgValueNumberArrIndexVal(GenTreePtr tree, VNFuncApp* pFuncApp, ValueNum addrXvn)
{
    assert(vnStore->IsVNHandle(pFuncApp->m_args[0]));
    CORINFO_CLASS_HANDLE arrElemTypeEQ = CORINFO_CLASS_HANDLE(vnStore->ConstantValue<ssize_t>(pFuncApp->m_args[0]));
    ValueNum             arrVN         = pFuncApp->m_args[1];
    ValueNum             inxVN         = pFuncApp->m_args[2];
    FieldSeqNode*        fldSeq        = vnStore->FieldSeqVNToFieldSeq(pFuncApp->m_args[3]);
    return fgValueNumberArrIndexVal(tree, arrElemTypeEQ, arrVN, inxVN, addrXvn, fldSeq);
}

ValueNum Compiler::fgValueNumberArrIndexVal(GenTreePtr           tree,
                                            CORINFO_CLASS_HANDLE elemTypeEq,
                                            ValueNum             arrVN,
                                            ValueNum             inxVN,
                                            ValueNum             excVN,
                                            FieldSeqNode*        fldSeq)
{
    assert(tree == nullptr || tree->OperIsIndir());

    // The VN inputs are required to be non-exceptional values.
    assert(arrVN == vnStore->VNNormVal(arrVN));
    assert(inxVN == vnStore->VNNormVal(inxVN));

    var_types elemTyp = DecodeElemType(elemTypeEq);
    var_types indType = (tree == nullptr) ? elemTyp : tree->TypeGet();
    ValueNum  selectedElem;

    if (fldSeq == FieldSeqStore::NotAField())
    {
        // This doesn't represent a proper array access
        JITDUMP("    *** NotAField sequence encountered in fgValueNumberArrIndexVal\n");

        // a new unique value number
        selectedElem = vnStore->VNForExpr(compCurBB, elemTyp);

#ifdef DEBUG
        if (verbose)
        {
            printf("  IND of PtrToArrElem is unique VN " STR_VN "%x.\n", selectedElem);
        }
#endif // DEBUG

        if (tree != nullptr)
        {
            tree->gtVNPair.SetBoth(selectedElem);
        }
    }
    else
    {
        ValueNum elemTypeEqVN    = vnStore->VNForHandle(ssize_t(elemTypeEq), GTF_ICON_CLASS_HDL);
        ValueNum hAtArrType      = vnStore->VNForMapSelect(VNK_Liberal, TYP_REF, fgCurHeapVN, elemTypeEqVN);
        ValueNum hAtArrTypeAtArr = vnStore->VNForMapSelect(VNK_Liberal, TYP_REF, hAtArrType, arrVN);
        ValueNum wholeElem       = vnStore->VNForMapSelect(VNK_Liberal, elemTyp, hAtArrTypeAtArr, inxVN);

#ifdef DEBUG
        if (verbose)
        {
            printf("  hAtArrType " STR_VN "%x is MapSelect(curHeap(" STR_VN "%x), ", hAtArrType, fgCurHeapVN);
            if (elemTyp == TYP_STRUCT)
            {
                printf("%s[]).\n", eeGetClassName(elemTypeEq));
            }
            else
            {
                printf("%s[]).\n", varTypeName(elemTyp));
            }

            printf("  hAtArrTypeAtArr " STR_VN "%x is MapSelect(hAtArrType(" STR_VN "%x), arr=" STR_VN "%x).\n",
                   hAtArrTypeAtArr, hAtArrType, arrVN);

            printf("  wholeElem " STR_VN "%x is MapSelect(hAtArrTypeAtArr(" STR_VN "%x), ind=" STR_VN "%x).\n",
                   wholeElem, hAtArrTypeAtArr, inxVN);
        }
#endif // DEBUG

        selectedElem          = wholeElem;
        size_t elemStructSize = 0;
        if (fldSeq)
        {
            selectedElem = vnStore->VNApplySelectors(VNK_Liberal, wholeElem, fldSeq, &elemStructSize);
            elemTyp      = vnStore->TypeOfVN(selectedElem);
        }
        selectedElem = vnStore->VNApplySelectorsTypeCheck(selectedElem, indType, elemStructSize);
        selectedElem = vnStore->VNWithExc(selectedElem, excVN);

#ifdef DEBUG
        if (verbose && (selectedElem != wholeElem))
        {
            printf("  selectedElem is " STR_VN "%x after applying selectors.\n", selectedElem);
        }
#endif // DEBUG

        if (tree != nullptr)
        {
            tree->gtVNPair.SetLiberal(selectedElem);
            // TODO-CQ: what to do here about exceptions?  We don't have the array and ind conservative
            // values, so we don't have their exceptions.  Maybe we should.
            tree->gtVNPair.SetConservative(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
        }
    }

    return selectedElem;
}

var_types ValueNumStore::TypeOfVN(ValueNum vn)
{
    if (vn == NoVN)
    {
        return TYP_UNDEF;
    }

    Chunk* c = m_chunks.GetNoExpand(GetChunkNum(vn));
    return c->m_typ;
}

//------------------------------------------------------------------------
// LoopOfVN: If the given value number is an opaque one associated with a particular
//    expression in the IR, give the loop number where the expression occurs; otherwise,
//    returns MAX_LOOP_NUM.
//
// Arguments:
//    vn - Value number to query
//
// Return Value:
//    The correspondingblock's bbNatLoopNum, which may be BasicBlock::NOT_IN_LOOP.
//    Returns MAX_LOOP_NUM if this VN is not an opaque value number associated with
//    a particular expression/location in the IR.

BasicBlock::loopNumber ValueNumStore::LoopOfVN(ValueNum vn)
{
    if (vn == NoVN)
    {
        return MAX_LOOP_NUM;
    }

    Chunk* c = m_chunks.GetNoExpand(GetChunkNum(vn));
    return c->m_loopNum;
}

bool ValueNumStore::IsVNConstant(ValueNum vn)
{
    if (vn == NoVN)
    {
        return false;
    }
    Chunk* c = m_chunks.GetNoExpand(GetChunkNum(vn));
    if (c->m_attribs == CEA_Const)
    {
        return vn != VNForVoid(); // Void is not a "real" constant -- in the sense that it represents no value.
    }
    else
    {
        return c->m_attribs == CEA_Handle;
    }
}

bool ValueNumStore::IsVNInt32Constant(ValueNum vn)
{
    if (!IsVNConstant(vn))
    {
        return false;
    }

    return TypeOfVN(vn) == TYP_INT;
}

unsigned ValueNumStore::GetHandleFlags(ValueNum vn)
{
    assert(IsVNHandle(vn));
    Chunk*    c      = m_chunks.GetNoExpand(GetChunkNum(vn));
    unsigned  offset = ChunkOffset(vn);
    VNHandle* handle = &reinterpret_cast<VNHandle*>(c->m_defs)[offset];
    return handle->m_flags;
}

bool ValueNumStore::IsVNHandle(ValueNum vn)
{
    if (vn == NoVN)
    {
        return false;
    }

    Chunk* c = m_chunks.GetNoExpand(GetChunkNum(vn));
    return c->m_attribs == CEA_Handle;
}

bool ValueNumStore::IsVNConstantBound(ValueNum vn)
{
    // Do we have "var < 100"?
    if (vn == NoVN)
    {
        return false;
    }

    VNFuncApp funcAttr;
    if (!GetVNFunc(vn, &funcAttr))
    {
        return false;
    }
    if (funcAttr.m_func != (VNFunc)GT_LE && funcAttr.m_func != (VNFunc)GT_GE && funcAttr.m_func != (VNFunc)GT_LT &&
        funcAttr.m_func != (VNFunc)GT_GT)
    {
        return false;
    }

    return IsVNInt32Constant(funcAttr.m_args[0]) != IsVNInt32Constant(funcAttr.m_args[1]);
}

void ValueNumStore::GetConstantBoundInfo(ValueNum vn, ConstantBoundInfo* info)
{
    assert(IsVNConstantBound(vn));
    assert(info);

    // Do we have var < 100?
    VNFuncApp funcAttr;
    GetVNFunc(vn, &funcAttr);

    bool isOp1Const = IsVNInt32Constant(funcAttr.m_args[1]);

    if (isOp1Const)
    {
        info->cmpOper  = funcAttr.m_func;
        info->cmpOpVN  = funcAttr.m_args[0];
        info->constVal = GetConstantInt32(funcAttr.m_args[1]);
    }
    else
    {
        info->cmpOper  = GenTree::SwapRelop((genTreeOps)funcAttr.m_func);
        info->cmpOpVN  = funcAttr.m_args[1];
        info->constVal = GetConstantInt32(funcAttr.m_args[0]);
    }
}

bool ValueNumStore::IsVNArrLenBound(ValueNum vn)
{
    // Do we have "var < a.len"?
    if (vn == NoVN)
    {
        return false;
    }

    VNFuncApp funcAttr;
    if (!GetVNFunc(vn, &funcAttr))
    {
        return false;
    }
    if (funcAttr.m_func != (VNFunc)GT_LE && funcAttr.m_func != (VNFunc)GT_GE && funcAttr.m_func != (VNFunc)GT_LT &&
        funcAttr.m_func != (VNFunc)GT_GT)
    {
        return false;
    }
    if (!IsVNArrLen(funcAttr.m_args[0]) && !IsVNArrLen(funcAttr.m_args[1]))
    {
        return false;
    }

    return true;
}

void ValueNumStore::GetArrLenBoundInfo(ValueNum vn, ArrLenArithBoundInfo* info)
{
    assert(IsVNArrLenBound(vn));

    // Do we have var < a.len?
    VNFuncApp funcAttr;
    GetVNFunc(vn, &funcAttr);

    bool isOp1ArrLen = IsVNArrLen(funcAttr.m_args[1]);
    if (isOp1ArrLen)
    {
        info->cmpOper = funcAttr.m_func;
        info->cmpOp   = funcAttr.m_args[0];
        info->vnArray = GetArrForLenVn(funcAttr.m_args[1]);
    }
    else
    {
        info->cmpOper = GenTree::SwapRelop((genTreeOps)funcAttr.m_func);
        info->cmpOp   = funcAttr.m_args[1];
        info->vnArray = GetArrForLenVn(funcAttr.m_args[0]);
    }
}

bool ValueNumStore::IsVNArrLenArith(ValueNum vn)
{
    // Do we have "a.len +or- var"
    if (vn == NoVN)
    {
        return false;
    }

    VNFuncApp funcAttr;

    return GetVNFunc(vn, &funcAttr) &&                                                 // vn is a func.
           (funcAttr.m_func == (VNFunc)GT_ADD || funcAttr.m_func == (VNFunc)GT_SUB) && // the func is +/-
           (IsVNArrLen(funcAttr.m_args[0]) || IsVNArrLen(funcAttr.m_args[1]));         // either op1 or op2 is a.len
}

void ValueNumStore::GetArrLenArithInfo(ValueNum vn, ArrLenArithBoundInfo* info)
{
    // Do we have a.len +/- var?
    assert(IsVNArrLenArith(vn));
    VNFuncApp funcArith;
    GetVNFunc(vn, &funcArith);

    bool isOp1ArrLen = IsVNArrLen(funcArith.m_args[1]);
    if (isOp1ArrLen)
    {
        info->arrOper = funcArith.m_func;
        info->arrOp   = funcArith.m_args[0];
        info->vnArray = GetArrForLenVn(funcArith.m_args[1]);
    }
    else
    {
        info->arrOper = funcArith.m_func;
        info->arrOp   = funcArith.m_args[1];
        info->vnArray = GetArrForLenVn(funcArith.m_args[0]);
    }
}

bool ValueNumStore::IsVNArrLenArithBound(ValueNum vn)
{
    // Do we have: "var < a.len - var"
    if (vn == NoVN)
    {
        return false;
    }

    VNFuncApp funcAttr;
    if (!GetVNFunc(vn, &funcAttr))
    {
        return false;
    }

    // Suitable comparator.
    if (funcAttr.m_func != (VNFunc)GT_LE && funcAttr.m_func != (VNFunc)GT_GE && funcAttr.m_func != (VNFunc)GT_LT &&
        funcAttr.m_func != (VNFunc)GT_GT)
    {
        return false;
    }

    // Either the op0 or op1 is arr len arithmetic.
    if (!IsVNArrLenArith(funcAttr.m_args[0]) && !IsVNArrLenArith(funcAttr.m_args[1]))
    {
        return false;
    }

    return true;
}

void ValueNumStore::GetArrLenArithBoundInfo(ValueNum vn, ArrLenArithBoundInfo* info)
{
    assert(IsVNArrLenArithBound(vn));

    VNFuncApp funcAttr;
    GetVNFunc(vn, &funcAttr);

    // Check whether op0 or op1 ia arr len arithmetic.
    bool isOp1ArrLenArith = IsVNArrLenArith(funcAttr.m_args[1]);
    if (isOp1ArrLenArith)
    {
        info->cmpOper = funcAttr.m_func;
        info->cmpOp   = funcAttr.m_args[0];
        GetArrLenArithInfo(funcAttr.m_args[1], info);
    }
    else
    {
        info->cmpOper = GenTree::SwapRelop((genTreeOps)funcAttr.m_func);
        info->cmpOp   = funcAttr.m_args[1];
        GetArrLenArithInfo(funcAttr.m_args[0], info);
    }
}

ValueNum ValueNumStore::GetArrForLenVn(ValueNum vn)
{
    if (vn == NoVN)
    {
        return NoVN;
    }

    VNFuncApp funcAttr;
    if (GetVNFunc(vn, &funcAttr) && funcAttr.m_func == (VNFunc)GT_ARR_LENGTH)
    {
        return funcAttr.m_args[0];
    }
    return NoVN;
}

bool ValueNumStore::IsVNNewArr(ValueNum vn, VNFuncApp* funcApp)
{
    if (vn == NoVN)
    {
        return false;
    }
    bool result = false;
    if (GetVNFunc(vn, funcApp))
    {
        result = (funcApp->m_func == VNF_JitNewArr) || (funcApp->m_func == VNF_JitReadyToRunNewArr);
    }
    return result;
}

int ValueNumStore::GetNewArrSize(ValueNum vn)
{
    VNFuncApp funcApp;
    if (IsVNNewArr(vn, &funcApp))
    {
        ValueNum arg1VN = funcApp.m_args[1];
        if (IsVNConstant(arg1VN) && TypeOfVN(arg1VN) == TYP_INT)
        {
            return ConstantValue<int>(arg1VN);
        }
    }
    return 0;
}

bool ValueNumStore::IsVNArrLen(ValueNum vn)
{
    if (vn == NoVN)
    {
        return false;
    }
    VNFuncApp funcAttr;
    return (GetVNFunc(vn, &funcAttr) && funcAttr.m_func == (VNFunc)GT_ARR_LENGTH);
}

ValueNum ValueNumStore::EvalMathFuncUnary(var_types typ, CorInfoIntrinsics gtMathFN, ValueNum arg0VN)
{
    assert(arg0VN == VNNormVal(arg0VN));
    if (IsVNConstant(arg0VN) && Compiler::IsTargetIntrinsic(gtMathFN))
    {
        // If the math intrinsic is not implemented by target-specific instructions, such as implemented
        // by user calls, then don't do constant folding on it. This minimizes precision loss.
        // I *may* need separate tracks for the double/float -- if the intrinsic funcs have overloads for these.
        double arg0Val = GetConstantDouble(arg0VN);

        double res = 0.0;
        switch (gtMathFN)
        {
            case CORINFO_INTRINSIC_Sin:
                res = sin(arg0Val);
                break;
            case CORINFO_INTRINSIC_Cos:
                res = cos(arg0Val);
                break;
            case CORINFO_INTRINSIC_Sqrt:
                res = sqrt(arg0Val);
                break;
            case CORINFO_INTRINSIC_Abs:
                res = fabs(arg0Val); // The result and params are doubles.
                break;
            case CORINFO_INTRINSIC_Round:
                res = FloatingPointUtils::round(arg0Val);
                break;
            default:
                unreached(); // the above are the only math intrinsics at the time of this writing.
        }
        if (typ == TYP_DOUBLE)
        {
            return VNForDoubleCon(res);
        }
        else if (typ == TYP_FLOAT)
        {
            return VNForFloatCon(float(res));
        }
        else
        {
            assert(typ == TYP_INT);
            assert(gtMathFN == CORINFO_INTRINSIC_Round);

            return VNForIntCon(int(res));
        }
    }
    else
    {
        assert(typ == TYP_DOUBLE || typ == TYP_FLOAT || (typ == TYP_INT && gtMathFN == CORINFO_INTRINSIC_Round));

        VNFunc vnf = VNF_Boundary;
        switch (gtMathFN)
        {
            case CORINFO_INTRINSIC_Sin:
                vnf = VNF_Sin;
                break;
            case CORINFO_INTRINSIC_Cos:
                vnf = VNF_Cos;
                break;
            case CORINFO_INTRINSIC_Sqrt:
                vnf = VNF_Sqrt;
                break;
            case CORINFO_INTRINSIC_Abs:
                vnf = VNF_Abs;
                break;
            case CORINFO_INTRINSIC_Round:
                if (typ == TYP_DOUBLE)
                {
                    vnf = VNF_RoundDouble;
                }
                else if (typ == TYP_FLOAT)
                {
                    vnf = VNF_RoundFloat;
                }
                else if (typ == TYP_INT)
                {
                    vnf = VNF_RoundInt;
                }
                else
                {
                    noway_assert(!"Invalid INTRINSIC_Round");
                }
                break;
            case CORINFO_INTRINSIC_Cosh:
                vnf = VNF_Cosh;
                break;
            case CORINFO_INTRINSIC_Sinh:
                vnf = VNF_Sinh;
                break;
            case CORINFO_INTRINSIC_Tan:
                vnf = VNF_Tan;
                break;
            case CORINFO_INTRINSIC_Tanh:
                vnf = VNF_Tanh;
                break;
            case CORINFO_INTRINSIC_Asin:
                vnf = VNF_Asin;
                break;
            case CORINFO_INTRINSIC_Acos:
                vnf = VNF_Acos;
                break;
            case CORINFO_INTRINSIC_Atan:
                vnf = VNF_Atan;
                break;
            case CORINFO_INTRINSIC_Log10:
                vnf = VNF_Log10;
                break;
            case CORINFO_INTRINSIC_Exp:
                vnf = VNF_Exp;
                break;
            case CORINFO_INTRINSIC_Ceiling:
                vnf = VNF_Ceiling;
                break;
            case CORINFO_INTRINSIC_Floor:
                vnf = VNF_Floor;
                break;
            default:
                unreached(); // the above are the only math intrinsics at the time of this writing.
        }

        return VNForFunc(typ, vnf, arg0VN);
    }
}

ValueNum ValueNumStore::EvalMathFuncBinary(var_types typ, CorInfoIntrinsics gtMathFN, ValueNum arg0VN, ValueNum arg1VN)
{
    assert(varTypeIsFloating(typ));
    assert(arg0VN == VNNormVal(arg0VN));
    assert(arg1VN == VNNormVal(arg1VN));

    VNFunc vnf = VNF_Boundary;

    // Currently, none of the binary math intrinsic are implemented by target-specific instructions.
    // To minimize precision loss, do not do constant folding on them.

    switch (gtMathFN)
    {
        case CORINFO_INTRINSIC_Atan2:
            vnf = VNF_Atan2;
            break;

        case CORINFO_INTRINSIC_Pow:
            vnf = VNF_Pow;
            break;

        default:
            unreached(); // the above are the only binary math intrinsics at the time of this writing.
    }

    return VNForFunc(typ, vnf, arg0VN, arg1VN);
}

bool ValueNumStore::IsVNFunc(ValueNum vn)
{
    if (vn == NoVN)
    {
        return false;
    }
    Chunk* c = m_chunks.GetNoExpand(GetChunkNum(vn));
    switch (c->m_attribs)
    {
        case CEA_Func0:
        case CEA_Func1:
        case CEA_Func2:
        case CEA_Func3:
        case CEA_Func4:
            return true;
        default:
            return false;
    }
}

bool ValueNumStore::GetVNFunc(ValueNum vn, VNFuncApp* funcApp)
{
    if (vn == NoVN)
    {
        return false;
    }

    Chunk*   c      = m_chunks.GetNoExpand(GetChunkNum(vn));
    unsigned offset = ChunkOffset(vn);
    assert(offset < c->m_numUsed);
    switch (c->m_attribs)
    {
        case CEA_Func4:
        {
            VNDefFunc4Arg* farg4 = &reinterpret_cast<VNDefFunc4Arg*>(c->m_defs)[offset];
            funcApp->m_func      = farg4->m_func;
            funcApp->m_arity     = 4;
            funcApp->m_args[0]   = farg4->m_arg0;
            funcApp->m_args[1]   = farg4->m_arg1;
            funcApp->m_args[2]   = farg4->m_arg2;
            funcApp->m_args[3]   = farg4->m_arg3;
        }
            return true;
        case CEA_Func3:
        {
            VNDefFunc3Arg* farg3 = &reinterpret_cast<VNDefFunc3Arg*>(c->m_defs)[offset];
            funcApp->m_func      = farg3->m_func;
            funcApp->m_arity     = 3;
            funcApp->m_args[0]   = farg3->m_arg0;
            funcApp->m_args[1]   = farg3->m_arg1;
            funcApp->m_args[2]   = farg3->m_arg2;
        }
            return true;
        case CEA_Func2:
        {
            VNDefFunc2Arg* farg2 = &reinterpret_cast<VNDefFunc2Arg*>(c->m_defs)[offset];
            funcApp->m_func      = farg2->m_func;
            funcApp->m_arity     = 2;
            funcApp->m_args[0]   = farg2->m_arg0;
            funcApp->m_args[1]   = farg2->m_arg1;
        }
            return true;
        case CEA_Func1:
        {
            VNDefFunc1Arg* farg1 = &reinterpret_cast<VNDefFunc1Arg*>(c->m_defs)[offset];
            funcApp->m_func      = farg1->m_func;
            funcApp->m_arity     = 1;
            funcApp->m_args[0]   = farg1->m_arg0;
        }
            return true;
        case CEA_Func0:
        {
            VNDefFunc0Arg* farg0 = &reinterpret_cast<VNDefFunc0Arg*>(c->m_defs)[offset];
            funcApp->m_func      = farg0->m_func;
            funcApp->m_arity     = 0;
        }
            return true;
        default:
            return false;
    }
}

ValueNum ValueNumStore::VNForRefInAddr(ValueNum vn)
{
    var_types vnType = TypeOfVN(vn);
    if (vnType == TYP_REF)
    {
        return vn;
    }
    // Otherwise...
    assert(vnType == TYP_BYREF);
    VNFuncApp funcApp;
    if (GetVNFunc(vn, &funcApp))
    {
        assert(funcApp.m_arity == 2 && (funcApp.m_func == VNFunc(GT_ADD) || funcApp.m_func == VNFunc(GT_SUB)));
        var_types vnArg0Type = TypeOfVN(funcApp.m_args[0]);
        if (vnArg0Type == TYP_REF || vnArg0Type == TYP_BYREF)
        {
            return VNForRefInAddr(funcApp.m_args[0]);
        }
        else
        {
            assert(funcApp.m_func == VNFunc(GT_ADD) &&
                   (TypeOfVN(funcApp.m_args[1]) == TYP_REF || TypeOfVN(funcApp.m_args[1]) == TYP_BYREF));
            return VNForRefInAddr(funcApp.m_args[1]);
        }
    }
    else
    {
        assert(IsVNConstant(vn));
        return vn;
    }
}

bool ValueNumStore::VNIsValid(ValueNum vn)
{
    ChunkNum cn = GetChunkNum(vn);
    if (cn >= m_chunks.Size())
    {
        return false;
    }
    // Otherwise...
    Chunk* c = m_chunks.GetNoExpand(cn);
    return ChunkOffset(vn) < c->m_numUsed;
}

#ifdef DEBUG

void ValueNumStore::vnDump(Compiler* comp, ValueNum vn, bool isPtr)
{
    printf(" {");
    if (vn == NoVN)
    {
        printf("NoVN");
    }
    else if (IsVNHandle(vn))
    {
        ssize_t val = ConstantValue<ssize_t>(vn);
        printf("Hnd const: 0x%p", dspPtr(val));
    }
    else if (IsVNConstant(vn))
    {
        var_types vnt = TypeOfVN(vn);
        switch (vnt)
        {
            case TYP_BOOL:
            case TYP_BYTE:
            case TYP_UBYTE:
            case TYP_CHAR:
            case TYP_SHORT:
            case TYP_USHORT:
            case TYP_INT:
            case TYP_UINT:
            {
                int val = ConstantValue<int>(vn);
                if (isPtr)
                {
                    printf("PtrCns[%p]", dspPtr(val));
                }
                else
                {
                    printf("IntCns");
                    if ((val > -1000) && (val < 1000))
                    {
                        printf(" %ld", val);
                    }
                    else
                    {
                        printf(" 0x%X", val);
                    }
                }
            }
            break;
            case TYP_LONG:
            case TYP_ULONG:
            {
                INT64 val = ConstantValue<INT64>(vn);
                if (isPtr)
                {
                    printf("LngPtrCns: 0x%p", dspPtr(val));
                }
                else
                {
                    printf("LngCns: ");
                    if ((val > -1000) && (val < 1000))
                    {
                        printf(" %ld", val);
                    }
                    else if ((val & 0xFFFFFFFF00000000LL) == 0)
                    {
                        printf(" 0x%X", val);
                    }
                    else
                    {
                        printf(" 0x%llx", val);
                    }
                }
            }
            break;
            case TYP_FLOAT:
                printf("FltCns[%f]", ConstantValue<float>(vn));
                break;
            case TYP_DOUBLE:
                printf("DblCns[%f]", ConstantValue<double>(vn));
                break;
            case TYP_REF:
            case TYP_ARRAY:
                if (vn == VNForNull())
                {
                    printf("null");
                }
                else if (vn == VNForVoid())
                {
                    printf("void");
                }
                else
                {
                    assert(vn == VNForZeroMap());
                    printf("zeroMap");
                }
                break;
            case TYP_BYREF:
                printf("byrefVal");
                break;
            case TYP_STRUCT:
#ifdef FEATURE_SIMD
            case TYP_SIMD8:
            case TYP_SIMD12:
            case TYP_SIMD16:
            case TYP_SIMD32:
#endif // FEATURE_SIMD
                printf("structVal");
                break;

            // These should be unreached.
            default:
                unreached();
        }
    }
    else if (IsVNArrLenBound(vn))
    {
        ArrLenArithBoundInfo info;
        GetArrLenBoundInfo(vn, &info);
        info.dump(this);
    }
    else if (IsVNArrLenArithBound(vn))
    {
        ArrLenArithBoundInfo info;
        GetArrLenArithBoundInfo(vn, &info);
        info.dump(this);
    }
    else if (IsVNFunc(vn))
    {
        VNFuncApp funcApp;
        GetVNFunc(vn, &funcApp);
        // A few special cases...
        switch (funcApp.m_func)
        {
            case VNF_FieldSeq:
                vnDumpFieldSeq(comp, &funcApp, true);
                break;
            case VNF_MapSelect:
                vnDumpMapSelect(comp, &funcApp);
                break;
            case VNF_MapStore:
                vnDumpMapStore(comp, &funcApp);
                break;
            default:
                printf("%s(", VNFuncName(funcApp.m_func));
                for (unsigned i = 0; i < funcApp.m_arity; i++)
                {
                    if (i > 0)
                    {
                        printf(", ");
                    }

                    printf(STR_VN "%x", funcApp.m_args[i]);

#if FEATURE_VN_DUMP_FUNC_ARGS
                    printf("=");
                    vnDump(comp, funcApp.m_args[i]);
#endif
                }
                printf(")");
        }
    }
    else
    {
        // Otherwise, just a VN with no structure; print just the VN.
        printf("%x", vn);
    }
    printf("}");
}

void ValueNumStore::vnDumpFieldSeq(Compiler* comp, VNFuncApp* fieldSeq, bool isHead)
{
    assert(fieldSeq->m_func == VNF_FieldSeq); // Precondition.
    // First arg is the field handle VN.
    assert(IsVNConstant(fieldSeq->m_args[0]) && TypeOfVN(fieldSeq->m_args[0]) == TYP_I_IMPL);
    ssize_t fieldHndVal = ConstantValue<ssize_t>(fieldSeq->m_args[0]);
    bool    hasTail     = (fieldSeq->m_args[1] != VNForNull());

    if (isHead && hasTail)
    {
        printf("(");
    }

    CORINFO_FIELD_HANDLE fldHnd = CORINFO_FIELD_HANDLE(fieldHndVal);
    if (fldHnd == FieldSeqStore::FirstElemPseudoField)
    {
        printf("#FirstElem");
    }
    else if (fldHnd == FieldSeqStore::ConstantIndexPseudoField)
    {
        printf("#ConstantIndex");
    }
    else
    {
        const char* modName;
        const char* fldName = m_pComp->eeGetFieldName(fldHnd, &modName);
        printf("%s", fldName);
    }

    if (hasTail)
    {
        printf(", ");
        assert(IsVNFunc(fieldSeq->m_args[1]));
        VNFuncApp tail;
        GetVNFunc(fieldSeq->m_args[1], &tail);
        vnDumpFieldSeq(comp, &tail, false);
    }

    if (isHead && hasTail)
    {
        printf(")");
    }
}

void ValueNumStore::vnDumpMapSelect(Compiler* comp, VNFuncApp* mapSelect)
{
    assert(mapSelect->m_func == VNF_MapSelect); // Precondition.

    ValueNum mapVN   = mapSelect->m_args[0]; // First arg is the map id
    ValueNum indexVN = mapSelect->m_args[1]; // Second arg is the index

    comp->vnPrint(mapVN, 0);
    printf("[");
    comp->vnPrint(indexVN, 0);
    printf("]");
}

void ValueNumStore::vnDumpMapStore(Compiler* comp, VNFuncApp* mapStore)
{
    assert(mapStore->m_func == VNF_MapStore); // Precondition.

    ValueNum mapVN    = mapStore->m_args[0]; // First arg is the map id
    ValueNum indexVN  = mapStore->m_args[1]; // Second arg is the index
    ValueNum newValVN = mapStore->m_args[2]; // Third arg is the new value

    comp->vnPrint(mapVN, 0);
    printf("[");
    comp->vnPrint(indexVN, 0);
    printf(" := ");
    comp->vnPrint(newValVN, 0);
    printf("]");
}
#endif // DEBUG

// Static fields, methods.
static UINT8      vnfOpAttribs[VNF_COUNT];
static genTreeOps genTreeOpsIllegalAsVNFunc[] = {GT_IND, // When we do heap memory.
                                                 GT_NULLCHECK, GT_QMARK, GT_COLON, GT_LOCKADD, GT_XADD, GT_XCHG,
                                                 GT_CMPXCHG, GT_LCLHEAP, GT_BOX,

                                                 // These need special semantics:
                                                 GT_COMMA, // == second argument (but with exception(s) from first).
                                                 GT_ADDR, GT_ARR_BOUNDS_CHECK,
                                                 GT_OBJ,      // May reference heap memory.
                                                 GT_BLK,      // May reference heap memory.
                                                 GT_INIT_VAL, // Not strictly a pass-through.

                                                 // These control-flow operations need no values.
                                                 GT_JTRUE, GT_RETURN, GT_SWITCH, GT_RETFILT, GT_CKFINITE};

UINT8* ValueNumStore::s_vnfOpAttribs = nullptr;

void ValueNumStore::InitValueNumStoreStatics()
{
    // Make sure we've gotten constants right...
    assert(unsigned(VNFOA_Arity) == (1 << VNFOA_ArityShift));
    assert(unsigned(VNFOA_AfterArity) == (unsigned(VNFOA_Arity) << VNFOA_ArityBits));

    s_vnfOpAttribs = &vnfOpAttribs[0];
    for (unsigned i = 0; i < GT_COUNT; i++)
    {
        genTreeOps gtOper = static_cast<genTreeOps>(i);
        unsigned   arity  = 0;
        if (GenTree::OperIsUnary(gtOper))
        {
            arity = 1;
        }
        else if (GenTree::OperIsBinary(gtOper))
        {
            arity = 2;
        }
        // Since GT_ARR_BOUNDS_CHECK is not currently GTK_BINOP
        else if (gtOper == GT_ARR_BOUNDS_CHECK)
        {
            arity = 2;
        }
        vnfOpAttribs[i] |= (arity << VNFOA_ArityShift);

        if (GenTree::OperIsCommutative(gtOper))
        {
            vnfOpAttribs[i] |= VNFOA_Commutative;
        }
    }

    // I so wish this wasn't the best way to do this...

    int vnfNum = VNF_Boundary + 1; // The macro definition below will update this after using it.

#define ValueNumFuncDef(vnf, arity, commute, knownNonNull, sharedStatic)                                               \
    if (commute)                                                                                                       \
        vnfOpAttribs[vnfNum] |= VNFOA_Commutative;                                                                     \
    if (knownNonNull)                                                                                                  \
        vnfOpAttribs[vnfNum] |= VNFOA_KnownNonNull;                                                                    \
    if (sharedStatic)                                                                                                  \
        vnfOpAttribs[vnfNum] |= VNFOA_SharedStatic;                                                                    \
    vnfOpAttribs[vnfNum] |= (arity << VNFOA_ArityShift);                                                               \
    vnfNum++;

#include "valuenumfuncs.h"
#undef ValueNumFuncDef

    unsigned n = sizeof(genTreeOpsIllegalAsVNFunc) / sizeof(genTreeOps);
    for (unsigned i = 0; i < n; i++)
    {
        vnfOpAttribs[genTreeOpsIllegalAsVNFunc[i]] |= VNFOA_IllegalGenTreeOp;
    }
}

#ifdef DEBUG
// Define the name array.
#define ValueNumFuncDef(vnf, arity, commute, knownNonNull, sharedStatic) #vnf,

const char* ValueNumStore::VNFuncNameArr[] = {
#include "valuenumfuncs.h"
#undef ValueNumFuncDef
};

// static
const char* ValueNumStore::VNFuncName(VNFunc vnf)
{
    if (vnf < VNF_Boundary)
    {
        return GenTree::NodeName(genTreeOps(vnf));
    }
    else
    {
        return VNFuncNameArr[vnf - (VNF_Boundary + 1)];
    }
}

static const char* s_reservedNameArr[] = {
    "$VN.Recursive",    // -2  RecursiveVN
    "$VN.No",           // -1  NoVN
    "$VN.Null",         //  0  VNForNull()
    "$VN.ZeroMap",      //  1  VNForZeroMap()
    "$VN.NotAField",    //  2  VNForNotAField()
    "$VN.ReadOnlyHeap", //  3  VNForROH()
    "$VN.Void",         //  4  VNForVoid()
    "$VN.EmptyExcSet"   //  5  VNForEmptyExcSet()
};

// Returns the string name of "vn" when it is a reserved value number, nullptr otherwise
// static
const char* ValueNumStore::reservedName(ValueNum vn)
{
    int val = vn - ValueNumStore::RecursiveVN; // Add two, making 'RecursiveVN' equal to zero
    int max = ValueNumStore::SRC_NumSpecialRefConsts - ValueNumStore::RecursiveVN;

    if ((val >= 0) && (val < max))
    {
        return s_reservedNameArr[val];
    }
    return nullptr;
}

#endif // DEBUG

// Returns true if "vn" is a reserved value number

// static
bool ValueNumStore::isReservedVN(ValueNum vn)
{
    int val = vn - ValueNumStore::RecursiveVN; // Adding two, making 'RecursiveVN' equal to zero
    int max = ValueNumStore::SRC_NumSpecialRefConsts - ValueNumStore::RecursiveVN;

    if ((val >= 0) && (val < max))
    {
        return true;
    }
    return false;
}

#ifdef DEBUG
void ValueNumStore::RunTests(Compiler* comp)
{
    VNFunc VNF_Add = GenTreeOpToVNFunc(GT_ADD);

    ValueNumStore* vns    = new (comp->getAllocatorDebugOnly()) ValueNumStore(comp, comp->getAllocatorDebugOnly());
    ValueNum       vnNull = VNForNull();
    assert(vnNull == VNForNull());

    ValueNum vnFor1 = vns->VNForIntCon(1);
    assert(vnFor1 == vns->VNForIntCon(1));
    assert(vns->TypeOfVN(vnFor1) == TYP_INT);
    assert(vns->IsVNConstant(vnFor1));
    assert(vns->ConstantValue<int>(vnFor1) == 1);

    ValueNum vnFor100 = vns->VNForIntCon(100);
    assert(vnFor100 == vns->VNForIntCon(100));
    assert(vnFor100 != vnFor1);
    assert(vns->TypeOfVN(vnFor100) == TYP_INT);
    assert(vns->IsVNConstant(vnFor100));
    assert(vns->ConstantValue<int>(vnFor100) == 100);

    ValueNum vnFor1F = vns->VNForFloatCon(1.0f);
    assert(vnFor1F == vns->VNForFloatCon(1.0f));
    assert(vnFor1F != vnFor1 && vnFor1F != vnFor100);
    assert(vns->TypeOfVN(vnFor1F) == TYP_FLOAT);
    assert(vns->IsVNConstant(vnFor1F));
    assert(vns->ConstantValue<float>(vnFor1F) == 1.0f);

    ValueNum vnFor1D = vns->VNForDoubleCon(1.0);
    assert(vnFor1D == vns->VNForDoubleCon(1.0));
    assert(vnFor1D != vnFor1F && vnFor1D != vnFor1 && vnFor1D != vnFor100);
    assert(vns->TypeOfVN(vnFor1D) == TYP_DOUBLE);
    assert(vns->IsVNConstant(vnFor1D));
    assert(vns->ConstantValue<double>(vnFor1D) == 1.0);

    ValueNum vnRandom1   = vns->VNForExpr(nullptr, TYP_INT);
    ValueNum vnForFunc2a = vns->VNForFunc(TYP_INT, VNF_Add, vnFor1, vnRandom1);
    assert(vnForFunc2a == vns->VNForFunc(TYP_INT, VNF_Add, vnFor1, vnRandom1));
    assert(vnForFunc2a != vnFor1D && vnForFunc2a != vnFor1F && vnForFunc2a != vnFor1 && vnForFunc2a != vnRandom1);
    assert(vns->TypeOfVN(vnForFunc2a) == TYP_INT);
    assert(!vns->IsVNConstant(vnForFunc2a));
    assert(vns->IsVNFunc(vnForFunc2a));
    VNFuncApp fa2a;
    bool      b = vns->GetVNFunc(vnForFunc2a, &fa2a);
    assert(b);
    assert(fa2a.m_func == VNF_Add && fa2a.m_arity == 2 && fa2a.m_args[0] == vnFor1 && fa2a.m_args[1] == vnRandom1);

    ValueNum vnForFunc2b = vns->VNForFunc(TYP_INT, VNF_Add, vnFor1, vnFor100);
    assert(vnForFunc2b == vns->VNForFunc(TYP_INT, VNF_Add, vnFor1, vnFor100));
    assert(vnForFunc2b != vnFor1D && vnForFunc2b != vnFor1F && vnForFunc2b != vnFor1 && vnForFunc2b != vnFor100);
    assert(vns->TypeOfVN(vnForFunc2b) == TYP_INT);
    assert(vns->IsVNConstant(vnForFunc2b));
    assert(vns->ConstantValue<int>(vnForFunc2b) == 101);

    // printf("Did ValueNumStore::RunTests.\n");
}
#endif // DEBUG

typedef ExpandArrayStack<BasicBlock*> BlockStack;

// This represents the "to do" state of the value number computation.
struct ValueNumberState
{
    // These two stacks collectively represent the set of blocks that are candidates for
    // processing, because at least one predecessor has been processed.  Blocks on "m_toDoAllPredsDone"
    // have had *all* predecessors processed, and thus are candidates for some extra optimizations.
    // Blocks on "m_toDoNotAllPredsDone" have at least one predecessor that has not been processed.
    // Blocks are initially on "m_toDoNotAllPredsDone" may be moved to "m_toDoAllPredsDone" when their last
    // unprocessed predecessor is processed, thus maintaining the invariants.
    BlockStack m_toDoAllPredsDone;
    BlockStack m_toDoNotAllPredsDone;

    Compiler* m_comp;

    // TBD: This should really be a bitset...
    // For now:
    // first bit indicates completed,
    // second bit indicates that it's been pushed on all-done stack,
    // third bit indicates that it's been pushed on not-all-done stack.
    BYTE* m_visited;

    enum BlockVisitBits
    {
        BVB_complete     = 0x1,
        BVB_onAllDone    = 0x2,
        BVB_onNotAllDone = 0x4,
    };

    bool GetVisitBit(unsigned bbNum, BlockVisitBits bvb)
    {
        return (m_visited[bbNum] & bvb) != 0;
    }
    void SetVisitBit(unsigned bbNum, BlockVisitBits bvb)
    {
        m_visited[bbNum] |= bvb;
    }

    ValueNumberState(Compiler* comp)
        : m_toDoAllPredsDone(comp->getAllocator(), /*minSize*/ 4)
        , m_toDoNotAllPredsDone(comp->getAllocator(), /*minSize*/ 4)
        , m_comp(comp)
        , m_visited(new (comp, CMK_ValueNumber) BYTE[comp->fgBBNumMax + 1]())
    {
    }

    BasicBlock* ChooseFromNotAllPredsDone()
    {
        assert(m_toDoAllPredsDone.Size() == 0);
        // If we have no blocks with all preds done, then (ideally, if all cycles have been captured by loops)
        // we must have at least one block within a loop.  We want to do the loops first.  Doing a loop entry block
        // should break the cycle, making the rest of the body of the loop (unless there's a nested loop) doable by the
        // all-preds-done rule.  If several loop entry blocks are available, at least one should have all non-loop preds
        // done -- we choose that.
        for (unsigned i = 0; i < m_toDoNotAllPredsDone.Size(); i++)
        {
            BasicBlock* cand = m_toDoNotAllPredsDone.Get(i);

            // Skip any already-completed blocks (a block may have all its preds finished, get added to the
            // all-preds-done todo set, and get processed there).  Do this by moving the last one down, to
            // keep the array compact.
            while (GetVisitBit(cand->bbNum, BVB_complete))
            {
                if (i + 1 < m_toDoNotAllPredsDone.Size())
                {
                    cand = m_toDoNotAllPredsDone.Pop();
                    m_toDoNotAllPredsDone.Set(i, cand);
                }
                else
                {
                    // "cand" is the last element; delete it.
                    (void)m_toDoNotAllPredsDone.Pop();
                    break;
                }
            }
            // We may have run out of non-complete candidates above.  If so, we're done.
            if (i == m_toDoNotAllPredsDone.Size())
            {
                break;
            }

            // See if "cand" is a loop entry.
            unsigned lnum;
            if (m_comp->optBlockIsLoopEntry(cand, &lnum))
            {
                // "lnum" is the innermost loop of which "cand" is the entry; find the outermost.
                unsigned lnumPar = m_comp->optLoopTable[lnum].lpParent;
                while (lnumPar != BasicBlock::NOT_IN_LOOP)
                {
                    if (m_comp->optLoopTable[lnumPar].lpEntry == cand)
                    {
                        lnum = lnumPar;
                    }
                    else
                    {
                        break;
                    }
                    lnumPar = m_comp->optLoopTable[lnumPar].lpParent;
                }

                bool allNonLoopPredsDone = true;
                for (flowList* pred = m_comp->BlockPredsWithEH(cand); pred != nullptr; pred = pred->flNext)
                {
                    BasicBlock* predBlock = pred->flBlock;
                    if (!m_comp->optLoopTable[lnum].lpContains(predBlock))
                    {
                        if (!GetVisitBit(predBlock->bbNum, BVB_complete))
                        {
                            allNonLoopPredsDone = false;
                        }
                    }
                }
                if (allNonLoopPredsDone)
                {
                    return cand;
                }
            }
        }

        // If we didn't find a loop entry block with all non-loop preds done above, then return a random member (if
        // there is one).
        if (m_toDoNotAllPredsDone.Size() == 0)
        {
            return nullptr;
        }
        else
        {
            return m_toDoNotAllPredsDone.Pop();
        }
    }

// Debugging output that is too detailed for a normal JIT dump...
#define DEBUG_VN_VISIT 0

    // Record that "blk" has been visited, and add any unvisited successors of "blk" to the appropriate todo set.
    void FinishVisit(BasicBlock* blk)
    {
#ifdef DEBUG_VN_VISIT
        JITDUMP("finish(BB%02u).\n", blk->bbNum);
#endif // DEBUG_VN_VISIT

        SetVisitBit(blk->bbNum, BVB_complete);

        AllSuccessorIter succsEnd = blk->GetAllSuccs(m_comp).end();
        for (AllSuccessorIter succs = blk->GetAllSuccs(m_comp).begin(); succs != succsEnd; ++succs)
        {
            BasicBlock* succ = (*succs);
#ifdef DEBUG_VN_VISIT
            JITDUMP("   Succ(BB%02u).\n", succ->bbNum);
#endif // DEBUG_VN_VISIT

            if (GetVisitBit(succ->bbNum, BVB_complete))
            {
                continue;
            }
#ifdef DEBUG_VN_VISIT
            JITDUMP("     Not yet completed.\n");
#endif // DEBUG_VN_VISIT

            bool allPredsVisited = true;
            for (flowList* pred = m_comp->BlockPredsWithEH(succ); pred != nullptr; pred = pred->flNext)
            {
                BasicBlock* predBlock = pred->flBlock;
                if (!GetVisitBit(predBlock->bbNum, BVB_complete))
                {
                    allPredsVisited = false;
                    break;
                }
            }

            if (allPredsVisited)
            {
#ifdef DEBUG_VN_VISIT
                JITDUMP("     All preds complete, adding to allDone.\n");
#endif // DEBUG_VN_VISIT

                assert(!GetVisitBit(succ->bbNum, BVB_onAllDone)); // Only last completion of last succ should add to
                                                                  // this.
                m_toDoAllPredsDone.Push(succ);
                SetVisitBit(succ->bbNum, BVB_onAllDone);
            }
            else
            {
#ifdef DEBUG_VN_VISIT
                JITDUMP("     Not all preds complete  Adding to notallDone, if necessary...\n");
#endif // DEBUG_VN_VISIT

                if (!GetVisitBit(succ->bbNum, BVB_onNotAllDone))
                {
#ifdef DEBUG_VN_VISIT
                    JITDUMP("       Was necessary.\n");
#endif // DEBUG_VN_VISIT
                    m_toDoNotAllPredsDone.Push(succ);
                    SetVisitBit(succ->bbNum, BVB_onNotAllDone);
                }
            }
        }
    }

    bool ToDoExists()
    {
        return m_toDoAllPredsDone.Size() > 0 || m_toDoNotAllPredsDone.Size() > 0;
    }
};

void Compiler::fgValueNumber()
{
#ifdef DEBUG
    // This could be a JITDUMP, but some people find it convenient to set a breakpoint on the printf.
    if (verbose)
    {
        printf("\n*************** In fgValueNumber()\n");
    }
#endif

    // If we skipped SSA, skip VN as well.
    if (fgSsaPassesCompleted == 0)
    {
        return;
    }

    // Allocate the value number store.
    assert(fgVNPassesCompleted > 0 || vnStore == nullptr);
    if (fgVNPassesCompleted == 0)
    {
        CompAllocator* allocator = new (this, CMK_ValueNumber) CompAllocator(this, CMK_ValueNumber);
        vnStore                  = new (this, CMK_ValueNumber) ValueNumStore(this, allocator);
    }
    else
    {
        ValueNumPair noVnp;
        // Make sure the heap SSA names have no value numbers.
        for (unsigned i = 0; i < lvHeapNumSsaNames; i++)
        {
            lvHeapPerSsaData.GetRef(i).m_vnPair = noVnp;
        }
        for (BasicBlock* blk = fgFirstBB; blk != nullptr; blk = blk->bbNext)
        {
            // Now iterate over the block's statements, and their trees.
            for (GenTreePtr stmts = blk->FirstNonPhiDef(); stmts != nullptr; stmts = stmts->gtNext)
            {
                assert(stmts->IsStatement());
                for (GenTreePtr tree = stmts->gtStmt.gtStmtList; tree; tree = tree->gtNext)
                {
                    tree->gtVNPair.SetBoth(ValueNumStore::NoVN);
                }
            }
        }
    }

    // Compute the side effects of loops.
    optComputeLoopSideEffects();

    // At the block level, we will use a modified worklist algorithm.  We will have two
    // "todo" sets of unvisited blocks.  Blocks (other than the entry block) are put in a
    // todo set only when some predecessor has been visited, so all blocks have at least one
    // predecessor visited.  The distinction between the two sets is whether *all* predecessors have
    // already been visited.  We visit such blocks preferentially if they exist, since phi definitions
    // in such blocks will have all arguments defined, enabling a simplification in the case that all
    // arguments to the phi have the same VN.  If no such blocks exist, we pick a block with at least
    // one unvisited predecessor.  In this case, we assign a new VN for phi definitions.

    // Start by giving incoming arguments value numbers.
    // Also give must-init vars a zero of their type.
    for (unsigned i = 0; i < lvaCount; i++)
    {
        LclVarDsc* varDsc = &lvaTable[i];
        if (varDsc->lvIsParam)
        {
            // We assume that code equivalent to this variable initialization loop
            // has been performed when doing SSA naming, so that all the variables we give
            // initial VNs to here have been given initial SSA definitions there.
            // SSA numbers always start from FIRST_SSA_NUM, and we give the value number to SSA name FIRST_SSA_NUM.
            // We use the VNF_InitVal(i) from here so we know that this value is loop-invariant
            // in all loops.
            ValueNum      initVal = vnStore->VNForFunc(varDsc->TypeGet(), VNF_InitVal, vnStore->VNForIntCon(i));
            LclSsaVarDsc* ssaDef  = varDsc->GetPerSsaData(SsaConfig::FIRST_SSA_NUM);
            ssaDef->m_vnPair.SetBoth(initVal);
            ssaDef->m_defLoc.m_blk = fgFirstBB;
        }
        else if (info.compInitMem || varDsc->lvMustInit ||
                 (varDsc->lvTracked && VarSetOps::IsMember(this, fgFirstBB->bbLiveIn, varDsc->lvVarIndex)))
        {
            // The last clause covers the use-before-def variables (the ones that are live-in to the the first block),
            // these are variables that are read before being initialized (at least on some control flow paths)
            // if they are not must-init, then they get VNF_InitVal(i), as with the param case.)

            bool      isZeroed = (info.compInitMem || varDsc->lvMustInit);
            ValueNum  initVal  = ValueNumStore::NoVN; // We must assign a new value to initVal
            var_types typ      = varDsc->TypeGet();

            switch (typ)
            {
                case TYP_LCLBLK: // The outgoing args area for arm and x64
                case TYP_BLK:    // A blob of memory
                    // TYP_BLK is used for the EHSlots LclVar on x86 (aka shadowSPslotsVar)
                    // and for the lvaInlinedPInvokeFrameVar on x64, arm and x86
                    // The stack associated with these LclVars are not zero initialized
                    // thus we set 'initVN' to a new, unique VN.
                    //
                    initVal = vnStore->VNForExpr(fgFirstBB);
                    break;

                case TYP_BYREF:
                    if (isZeroed)
                    {
                        // LclVars of TYP_BYREF can be zero-inited.
                        initVal = vnStore->VNForByrefCon(0);
                    }
                    else
                    {
                        // Here we have uninitialized TYP_BYREF
                        initVal = vnStore->VNForFunc(typ, VNF_InitVal, vnStore->VNForIntCon(i));
                    }
                    break;

                default:
                    if (isZeroed)
                    {
                        // By default we will zero init these LclVars
                        initVal = vnStore->VNZeroForType(typ);
                    }
                    else
                    {
                        initVal = vnStore->VNForFunc(typ, VNF_InitVal, vnStore->VNForIntCon(i));
                    }
                    break;
            }
#ifdef _TARGET_X86_
            bool isVarargParam = (i == lvaVarargsBaseOfStkArgs || i == lvaVarargsHandleArg);
            if (isVarargParam)
                initVal = vnStore->VNForExpr(fgFirstBB); // a new, unique VN.
#endif
            assert(initVal != ValueNumStore::NoVN);

            LclSsaVarDsc* ssaDef = varDsc->GetPerSsaData(SsaConfig::FIRST_SSA_NUM);
            ssaDef->m_vnPair.SetBoth(initVal);
            ssaDef->m_defLoc.m_blk = fgFirstBB;
        }
    }
    // Give "Heap" an initial value number (about which we know nothing).
    ValueNum heapInitVal = vnStore->VNForFunc(TYP_REF, VNF_InitVal, vnStore->VNForIntCon(-1)); // Use -1 for the heap.
    GetHeapPerSsaData(SsaConfig::FIRST_SSA_NUM)->m_vnPair.SetBoth(heapInitVal);
#ifdef DEBUG
    if (verbose)
    {
        printf("Heap Initial Value in BB01 is: " STR_VN "%x\n", heapInitVal);
    }
#endif // DEBUG

    ValueNumberState vs(this);

    // Push the first block.  This has no preds.
    vs.m_toDoAllPredsDone.Push(fgFirstBB);

    while (vs.ToDoExists())
    {
        while (vs.m_toDoAllPredsDone.Size() > 0)
        {
            BasicBlock* toDo = vs.m_toDoAllPredsDone.Pop();
            fgValueNumberBlock(toDo, /*newVNsForPhis*/ false);
            // Record that we've visited "toDo", and add successors to the right sets.
            vs.FinishVisit(toDo);
        }
        // OK, we've run out of blocks whose predecessors are done.  Pick one whose predecessors are not all done,
        // process that.  This may make more "all-done" blocks, so we'll go around the outer loop again --
        // note that this is an "if", not a "while" loop.
        if (vs.m_toDoNotAllPredsDone.Size() > 0)
        {
            BasicBlock* toDo = vs.ChooseFromNotAllPredsDone();
            if (toDo == nullptr)
            {
                continue; // We may have run out, because of completed blocks on the not-all-preds done list.
            }

            fgValueNumberBlock(toDo, /*newVNsForPhis*/ true);
            // Record that we've visited "toDo", and add successors to the right sest.
            vs.FinishVisit(toDo);
        }
    }

#ifdef DEBUG
    JitTestCheckVN();
#endif // DEBUG

    fgVNPassesCompleted++;
}

void Compiler::fgValueNumberBlock(BasicBlock* blk, bool newVNsForPhis)
{
    compCurBB = blk;

#ifdef DEBUG
    compCurStmtNum = blk->bbStmtNum - 1; // Set compCurStmtNum
#endif

    unsigned outerLoopNum = BasicBlock::NOT_IN_LOOP;

    // First: visit phi's.  If "newVNForPhis", give them new VN's.  If not,
    // first check to see if all phi args have the same value.
    GenTreePtr firstNonPhi = blk->FirstNonPhiDef();
    for (GenTreePtr phiDefs = blk->bbTreeList; phiDefs != firstNonPhi; phiDefs = phiDefs->gtNext)
    {
        // TODO-Cleanup: It has been proposed that we should have an IsPhiDef predicate.  We would use it
        // in Block::FirstNonPhiDef as well.
        GenTreePtr phiDef = phiDefs->gtStmt.gtStmtExpr;
        assert(phiDef->OperGet() == GT_ASG);
        GenTreeLclVarCommon* newSsaVar = phiDef->gtOp.gtOp1->AsLclVarCommon();

        ValueNumPair phiAppVNP;
        ValueNumPair sameVNPair;

        GenTreePtr phiFunc = phiDef->gtOp.gtOp2;

        // At this point a GT_PHI node should never have a nullptr for gtOp1
        // and the gtOp1 should always be a GT_LIST node.
        GenTreePtr phiOp1 = phiFunc->gtOp.gtOp1;
        noway_assert(phiOp1 != nullptr);
        noway_assert(phiOp1->OperGet() == GT_LIST);

        GenTreeArgList* phiArgs = phiFunc->gtOp.gtOp1->AsArgList();

        // A GT_PHI node should have more than one argument.
        noway_assert(phiArgs->Rest() != nullptr);

        GenTreeLclVarCommon* phiArg = phiArgs->Current()->AsLclVarCommon();
        phiArgs                     = phiArgs->Rest();

        phiAppVNP.SetBoth(vnStore->VNForIntCon(phiArg->gtSsaNum));
        bool allSameLib  = true;
        bool allSameCons = true;
        sameVNPair       = lvaTable[phiArg->gtLclNum].GetPerSsaData(phiArg->gtSsaNum)->m_vnPair;
        if (!sameVNPair.BothDefined())
        {
            allSameLib  = false;
            allSameCons = false;
        }
        while (phiArgs != nullptr)
        {
            phiArg = phiArgs->Current()->AsLclVarCommon();
            // Set the VN of the phi arg.
            phiArg->gtVNPair = lvaTable[phiArg->gtLclNum].GetPerSsaData(phiArg->gtSsaNum)->m_vnPair;
            if (phiArg->gtVNPair.BothDefined())
            {
                if (phiArg->gtVNPair.GetLiberal() != sameVNPair.GetLiberal())
                {
                    allSameLib = false;
                }
                if (phiArg->gtVNPair.GetConservative() != sameVNPair.GetConservative())
                {
                    allSameCons = false;
                }
            }
            else
            {
                allSameLib  = false;
                allSameCons = false;
            }
            ValueNumPair phiArgSsaVNP;
            phiArgSsaVNP.SetBoth(vnStore->VNForIntCon(phiArg->gtSsaNum));
            phiAppVNP = vnStore->VNPairForFunc(newSsaVar->TypeGet(), VNF_Phi, phiArgSsaVNP, phiAppVNP);
            phiArgs   = phiArgs->Rest();
        }

        ValueNumPair newVNPair;
        if (allSameLib)
        {
            newVNPair.SetLiberal(sameVNPair.GetLiberal());
        }
        else
        {
            newVNPair.SetLiberal(phiAppVNP.GetLiberal());
        }
        if (allSameCons)
        {
            newVNPair.SetConservative(sameVNPair.GetConservative());
        }
        else
        {
            newVNPair.SetConservative(phiAppVNP.GetConservative());
        }

        LclSsaVarDsc* newSsaVarDsc = lvaTable[newSsaVar->gtLclNum].GetPerSsaData(newSsaVar->GetSsaNum());
        // If all the args of the phi had the same value(s, liberal and conservative), then there wasn't really
        // a reason to have the phi -- just pass on that value.
        if (allSameLib && allSameCons)
        {
            newSsaVarDsc->m_vnPair = newVNPair;
#ifdef DEBUG
            if (verbose)
            {
                printf("In SSA definition, incoming phi args all same, set VN of local %d/%d to ",
                       newSsaVar->GetLclNum(), newSsaVar->GetSsaNum());
                vnpPrint(newVNPair, 1);
                printf(".\n");
            }
#endif // DEBUG
        }
        else
        {
            // They were not the same; we need to create a phi definition.
            ValueNumPair lclNumVNP;
            lclNumVNP.SetBoth(ValueNum(newSsaVar->GetLclNum()));
            ValueNumPair ssaNumVNP;
            ssaNumVNP.SetBoth(ValueNum(newSsaVar->GetSsaNum()));
            ValueNumPair vnPhiDef =
                vnStore->VNPairForFunc(newSsaVar->TypeGet(), VNF_PhiDef, lclNumVNP, ssaNumVNP, phiAppVNP);
            newSsaVarDsc->m_vnPair = vnPhiDef;
#ifdef DEBUG
            if (verbose)
            {
                printf("SSA definition: set VN of local %d/%d to ", newSsaVar->GetLclNum(), newSsaVar->GetSsaNum());
                vnpPrint(vnPhiDef, 1);
                printf(".\n");
            }
#endif // DEBUG
        }
    }

    // Now do the same for "Heap".
    // Is there a phi for this block?
    if (blk->bbHeapSsaPhiFunc == nullptr)
    {
        fgCurHeapVN = GetHeapPerSsaData(blk->bbHeapSsaNumIn)->m_vnPair.GetLiberal();
        assert(fgCurHeapVN != ValueNumStore::NoVN);
    }
    else
    {
        unsigned loopNum;
        ValueNum newHeapVN;
        if (optBlockIsLoopEntry(blk, &loopNum))
        {
            newHeapVN = fgHeapVNForLoopSideEffects(blk, loopNum);
        }
        else
        {
            // Are all the VN's the same?
            BasicBlock::HeapPhiArg* phiArgs = blk->bbHeapSsaPhiFunc;
            assert(phiArgs != BasicBlock::EmptyHeapPhiDef);
            // There should be > 1 args to a phi.
            assert(phiArgs->m_nextArg != nullptr);
            ValueNum phiAppVN = vnStore->VNForIntCon(phiArgs->GetSsaNum());
            JITDUMP("  Building phi application: $%x = SSA# %d.\n", phiAppVN, phiArgs->GetSsaNum());
            bool     allSame = true;
            ValueNum sameVN  = GetHeapPerSsaData(phiArgs->GetSsaNum())->m_vnPair.GetLiberal();
            if (sameVN == ValueNumStore::NoVN)
            {
                allSame = false;
            }
            phiArgs = phiArgs->m_nextArg;
            while (phiArgs != nullptr)
            {
                ValueNum phiArgVN = GetHeapPerSsaData(phiArgs->GetSsaNum())->m_vnPair.GetLiberal();
                if (phiArgVN == ValueNumStore::NoVN || phiArgVN != sameVN)
                {
                    allSame = false;
                }
#ifdef DEBUG
                ValueNum oldPhiAppVN = phiAppVN;
#endif
                unsigned phiArgSSANum   = phiArgs->GetSsaNum();
                ValueNum phiArgSSANumVN = vnStore->VNForIntCon(phiArgSSANum);
                JITDUMP("  Building phi application: $%x = SSA# %d.\n", phiArgSSANumVN, phiArgSSANum);
                phiAppVN = vnStore->VNForFunc(TYP_REF, VNF_Phi, phiArgSSANumVN, phiAppVN);
                JITDUMP("  Building phi application: $%x = phi($%x, $%x).\n", phiAppVN, phiArgSSANumVN, oldPhiAppVN);
                phiArgs = phiArgs->m_nextArg;
            }
            if (allSame)
            {
                newHeapVN = sameVN;
            }
            else
            {
                newHeapVN =
                    vnStore->VNForFunc(TYP_REF, VNF_PhiHeapDef, vnStore->VNForHandle(ssize_t(blk), 0), phiAppVN);
            }
        }
        GetHeapPerSsaData(blk->bbHeapSsaNumIn)->m_vnPair.SetLiberal(newHeapVN);
        fgCurHeapVN = newHeapVN;
    }
#ifdef DEBUG
    if (verbose)
    {
        printf("The SSA definition for heap (#%d) at start of BB%02u is ", blk->bbHeapSsaNumIn, blk->bbNum);
        vnPrint(fgCurHeapVN, 1);
        printf("\n");
    }
#endif // DEBUG

    // Now iterate over the remaining statements, and their trees.
    for (GenTreePtr stmt = firstNonPhi; stmt != nullptr; stmt = stmt->gtNext)
    {
        assert(stmt->IsStatement());

#ifdef DEBUG
        compCurStmtNum++;
        if (verbose)
        {
            printf("\n***** BB%02u, stmt %d (before)\n", blk->bbNum, compCurStmtNum);
            gtDispTree(stmt->gtStmt.gtStmtExpr);
            printf("\n");
        }
#endif

        for (GenTreePtr tree = stmt->gtStmt.gtStmtList; tree; tree = tree->gtNext)
        {
            fgValueNumberTree(tree);
        }

#ifdef DEBUG
        if (verbose)
        {
            printf("\n***** BB%02u, stmt %d (after)\n", blk->bbNum, compCurStmtNum);
            gtDispTree(stmt->gtStmt.gtStmtExpr);
            printf("\n");
            if (stmt->gtNext)
            {
                printf("---------\n");
            }
        }
#endif
    }

    if (blk->bbHeapSsaNumOut != blk->bbHeapSsaNumIn)
    {
        GetHeapPerSsaData(blk->bbHeapSsaNumOut)->m_vnPair.SetLiberal(fgCurHeapVN);
    }

    compCurBB = nullptr;
}

ValueNum Compiler::fgHeapVNForLoopSideEffects(BasicBlock* entryBlock, unsigned innermostLoopNum)
{
    // "loopNum" is the innermost loop for which "blk" is the entry; find the outermost one.
    assert(innermostLoopNum != BasicBlock::NOT_IN_LOOP);
    unsigned loopsInNest = innermostLoopNum;
    unsigned loopNum     = innermostLoopNum;
    while (loopsInNest != BasicBlock::NOT_IN_LOOP)
    {
        if (optLoopTable[loopsInNest].lpEntry != entryBlock)
        {
            break;
        }
        loopNum     = loopsInNest;
        loopsInNest = optLoopTable[loopsInNest].lpParent;
    }

#ifdef DEBUG
    if (verbose)
    {
        printf("Computing heap state for block BB%02u, entry block for loops %d to %d:\n", entryBlock->bbNum,
               innermostLoopNum, loopNum);
    }
#endif // DEBUG

    // If this loop has heap havoc effects, just use a new, unique VN.
    if (optLoopTable[loopNum].lpLoopHasHeapHavoc)
    {
        ValueNum res = vnStore->VNForExpr(entryBlock, TYP_REF);
#ifdef DEBUG
        if (verbose)
        {
            printf("  Loop %d has heap havoc effect; heap state is new fresh $%x.\n", loopNum, res);
        }
#endif // DEBUG
        return res;
    }

    // Otherwise, find the predecessors of the entry block that are not in the loop.
    // If there is only one such, use its heap value as the "base."  If more than one,
    // use a new unique heap VN.
    BasicBlock* nonLoopPred          = nullptr;
    bool        multipleNonLoopPreds = false;
    for (flowList* pred = BlockPredsWithEH(entryBlock); pred != nullptr; pred = pred->flNext)
    {
        BasicBlock* predBlock = pred->flBlock;
        if (!optLoopTable[loopNum].lpContains(predBlock))
        {
            if (nonLoopPred == nullptr)
            {
                nonLoopPred = predBlock;
            }
            else
            {
#ifdef DEBUG
                if (verbose)
                {
                    printf("  Entry block has >1 non-loop preds: (at least) BB%02u and BB%02u.\n", nonLoopPred->bbNum,
                           predBlock->bbNum);
                }
#endif // DEBUG
                multipleNonLoopPreds = true;
                break;
            }
        }
    }
    if (multipleNonLoopPreds)
    {
        ValueNum res = vnStore->VNForExpr(entryBlock, TYP_REF);
#ifdef DEBUG
        if (verbose)
        {
            printf("  Therefore, heap state is new, fresh $%x.\n", res);
        }
#endif // DEBUG
        return res;
    }
    // Otherwise, there is a single non-loop pred.
    assert(nonLoopPred != nullptr);
    // What is it's heap post-state?
    ValueNum newHeapVN = GetHeapPerSsaData(nonLoopPred->bbHeapSsaNumOut)->m_vnPair.GetLiberal();
    assert(newHeapVN !=
           ValueNumStore::NoVN); // We must have processed the single non-loop pred before reaching the loop entry.

#ifdef DEBUG
    if (verbose)
    {
        printf("  Init heap state is $%x, with new, fresh VN at:\n", newHeapVN);
    }
#endif // DEBUG
    // Modify "base" by setting all the modified fields/field maps/array maps to unknown values.
    // First the fields/field maps.

    Compiler::LoopDsc::FieldHandleSet* fieldsMod = optLoopTable[loopNum].lpFieldsModified;
    if (fieldsMod != nullptr)
    {
        for (Compiler::LoopDsc::FieldHandleSet::KeyIterator ki = fieldsMod->Begin(); !ki.Equal(fieldsMod->End()); ++ki)
        {
            CORINFO_FIELD_HANDLE fldHnd   = ki.Get();
            ValueNum             fldHndVN = vnStore->VNForHandle(ssize_t(fldHnd), GTF_ICON_FIELD_HDL);

#ifdef DEBUG
            if (verbose)
            {
                const char* modName;
                const char* fldName = eeGetFieldName(fldHnd, &modName);
                printf("     VNForHandle(Fseq[%s]) is " STR_VN "%x\n", fldName, fldHndVN);

                printf("  fgCurHeapVN assigned:\n");
            }
#endif // DEBUG

            newHeapVN = vnStore->VNForMapStore(TYP_REF, newHeapVN, fldHndVN, vnStore->VNForExpr(entryBlock, TYP_REF));
        }
    }
    // Now do the array maps.
    Compiler::LoopDsc::ClassHandleSet* elemTypesMod = optLoopTable[loopNum].lpArrayElemTypesModified;
    if (elemTypesMod != nullptr)
    {
        for (Compiler::LoopDsc::ClassHandleSet::KeyIterator ki = elemTypesMod->Begin(); !ki.Equal(elemTypesMod->End());
             ++ki)
        {
            CORINFO_CLASS_HANDLE elemClsHnd = ki.Get();

#ifdef DEBUG
            if (verbose)
            {
                var_types elemTyp = DecodeElemType(elemClsHnd);
                if (varTypeIsStruct(elemTyp))
                {
                    printf("     Array map %s[]\n", eeGetClassName(elemClsHnd));
                }
                else
                {
                    printf("     Array map %s[]\n", varTypeName(elemTyp));
                }
                printf("  fgCurHeapVN assigned:\n");
            }
#endif // DEBUG

            ValueNum elemTypeVN = vnStore->VNForHandle(ssize_t(elemClsHnd), GTF_ICON_CLASS_HDL);
            ValueNum uniqueVN   = vnStore->VNForExpr(entryBlock, TYP_REF);
            newHeapVN           = vnStore->VNForMapStore(TYP_REF, newHeapVN, elemTypeVN, uniqueVN);
        }
    }

#ifdef DEBUG
    if (verbose)
    {
        printf("  Final heap state is $%x.\n", newHeapVN);
    }
#endif // DEBUG
    return newHeapVN;
}

void Compiler::fgMutateHeap(GenTreePtr tree DEBUGARG(const char* msg))
{
    // bbHeapDef must be set to true for any block that Mutates the global Heap
    assert(compCurBB->bbHeapDef);

    fgCurHeapVN = vnStore->VNForExpr(compCurBB, TYP_REF);

    // If we're tracking the heap SSA # caused by this node, record it.
    fgValueNumberRecordHeapSsa(tree);

#ifdef DEBUG
    if (verbose)
    {
        printf("  fgCurHeapVN assigned by %s at ", msg);
        Compiler::printTreeID(tree);
        printf(" to new unique VN: " STR_VN "%x.\n", fgCurHeapVN);
    }
#endif // DEBUG
}

void Compiler::fgValueNumberRecordHeapSsa(GenTreePtr tree)
{
    unsigned ssaNum;
    if (GetHeapSsaMap()->Lookup(tree, &ssaNum))
    {
        GetHeapPerSsaData(ssaNum)->m_vnPair.SetLiberal(fgCurHeapVN);
#ifdef DEBUG
        if (verbose)
        {
            printf("Node ");
            Compiler::printTreeID(tree);
            printf(" sets heap SSA # %d to VN $%x: ", ssaNum, fgCurHeapVN);
            vnStore->vnDump(this, fgCurHeapVN);
            printf("\n");
        }
#endif // DEBUG
    }
}

// The input 'tree' is a leaf node that is a constant
// Assign the proper value number to the tree
void Compiler::fgValueNumberTreeConst(GenTreePtr tree)
{
    genTreeOps oper = tree->OperGet();
    var_types  typ  = tree->TypeGet();
    assert(GenTree::OperIsConst(oper));

    switch (typ)
    {
        case TYP_LONG:
        case TYP_ULONG:
        case TYP_INT:
        case TYP_UINT:
        case TYP_CHAR:
        case TYP_SHORT:
        case TYP_BYTE:
        case TYP_UBYTE:
        case TYP_BOOL:
            if (tree->IsCnsIntOrI() && tree->IsIconHandle())
            {
                tree->gtVNPair.SetBoth(
                    vnStore->VNForHandle(ssize_t(tree->gtIntConCommon.IconValue()), tree->GetIconHandleFlag()));
            }
            else if ((typ == TYP_LONG) || (typ == TYP_ULONG))
            {
                tree->gtVNPair.SetBoth(vnStore->VNForLongCon(INT64(tree->gtIntConCommon.LngValue())));
            }
            else
            {
                tree->gtVNPair.SetBoth(vnStore->VNForIntCon(int(tree->gtIntConCommon.IconValue())));
            }
            break;

        case TYP_FLOAT:
            tree->gtVNPair.SetBoth(vnStore->VNForFloatCon((float)tree->gtDblCon.gtDconVal));
            break;
        case TYP_DOUBLE:
            tree->gtVNPair.SetBoth(vnStore->VNForDoubleCon(tree->gtDblCon.gtDconVal));
            break;
        case TYP_REF:
            if (tree->gtIntConCommon.IconValue() == 0)
            {
                tree->gtVNPair.SetBoth(ValueNumStore::VNForNull());
            }
            else
            {
                assert(tree->gtFlags == GTF_ICON_STR_HDL); // Constant object can be only frozen string.
                tree->gtVNPair.SetBoth(
                    vnStore->VNForHandle(ssize_t(tree->gtIntConCommon.IconValue()), tree->GetIconHandleFlag()));
            }
            break;

        case TYP_BYREF:
            if (tree->gtIntConCommon.IconValue() == 0)
            {
                tree->gtVNPair.SetBoth(ValueNumStore::VNForNull());
            }
            else
            {
                assert(tree->IsCnsIntOrI());

                if (tree->IsIconHandle())
                {
                    tree->gtVNPair.SetBoth(
                        vnStore->VNForHandle(ssize_t(tree->gtIntConCommon.IconValue()), tree->GetIconHandleFlag()));
                }
                else
                {
                    tree->gtVNPair.SetBoth(vnStore->VNForByrefCon(tree->gtIntConCommon.IconValue()));
                }
            }
            break;

        default:
            unreached();
    }
}

//------------------------------------------------------------------------
// fgValueNumberBlockAssignment: Perform value numbering for block assignments.
//
// Arguments:
//    tree          - the block assignment to be value numbered.
//    evalAsgLhsInd - true iff we should value number the LHS of the assignment.
//
// Return Value:
//    None.
//
// Assumptions:
//    'tree' must be a block assignment (GT_INITBLK, GT_COPYBLK, GT_COPYOBJ).

void Compiler::fgValueNumberBlockAssignment(GenTreePtr tree, bool evalAsgLhsInd)
{
    GenTree* lhs = tree->gtGetOp1();
    GenTree* rhs = tree->gtGetOp2();
#ifdef DEBUG
    // Sometimes we query the heap ssa map, and need a dummy location for the ignored result.
    unsigned heapSsaNum;
#endif

    if (tree->OperIsInitBlkOp())
    {
        GenTreeLclVarCommon* lclVarTree;
        bool                 isEntire;

        if (tree->DefinesLocal(this, &lclVarTree, &isEntire))
        {
            assert(lclVarTree->gtFlags & GTF_VAR_DEF);
            // Should not have been recorded as updating the heap.
            assert(!GetHeapSsaMap()->Lookup(tree, &heapSsaNum));

            unsigned lclNum = lclVarTree->GetLclNum();

            // Ignore vars that we excluded from SSA (for example, because they're address-exposed). They don't have
            // SSA names in which to store VN's on defs.  We'll yield unique VN's when we read from them.
            if (!fgExcludeFromSsa(lclNum))
            {
                unsigned lclDefSsaNum = GetSsaNumForLocalVarDef(lclVarTree);

                ValueNum   initBlkVN = ValueNumStore::NoVN;
                GenTreePtr initConst = rhs;
                if (isEntire && initConst->OperGet() == GT_CNS_INT)
                {
                    unsigned initVal = 0xFF & (unsigned)initConst->AsIntConCommon()->IconValue();
                    if (initVal == 0)
                    {
                        initBlkVN = vnStore->VNZeroForType(lclVarTree->TypeGet());
                    }
                }
                ValueNum lclVarVN = (initBlkVN != ValueNumStore::NoVN)
                                        ? initBlkVN
                                        : vnStore->VNForExpr(compCurBB, var_types(lvaTable[lclNum].lvType));

                lvaTable[lclNum].GetPerSsaData(lclDefSsaNum)->m_vnPair.SetBoth(lclVarVN);
#ifdef DEBUG
                if (verbose)
                {
                    printf("N%03u ", tree->gtSeqNum);
                    Compiler::printTreeID(tree);
                    printf(" ");
                    gtDispNodeName(tree);
                    printf(" V%02u/%d => ", lclNum, lclDefSsaNum);
                    vnPrint(lclVarVN, 1);
                    printf("\n");
                }
#endif // DEBUG
            }
        }
        else
        {
            // For now, arbitrary side effect on Heap.
            // TODO-CQ: Why not be complete, and get this case right?
            fgMutateHeap(tree DEBUGARG("INITBLK - non local"));
        }
        // Initblock's are of type void.  Give them the void "value" -- they may occur in argument lists, which we
        // want to be able to give VN's to.
        tree->gtVNPair.SetBoth(ValueNumStore::VNForVoid());
    }
    else
    {
        assert(tree->OperIsCopyBlkOp());
        // TODO-Cleanup: We should factor things so that we uniformly rely on "PtrTo" VN's, and
        // the heap cases can be shared with assignments.
        GenTreeLclVarCommon* lclVarTree = nullptr;
        bool                 isEntire   = false;
        // Note that we don't care about exceptions here, since we're only using the values
        // to perform an assignment (which happens after any exceptions are raised...)

        if (tree->DefinesLocal(this, &lclVarTree, &isEntire))
        {
            // Should not have been recorded as updating the heap.
            assert(!GetHeapSsaMap()->Lookup(tree, &heapSsaNum));

            unsigned      lhsLclNum = lclVarTree->GetLclNum();
            FieldSeqNode* lhsFldSeq = nullptr;
            // If it's excluded from SSA, don't need to do anything.
            if (!fgExcludeFromSsa(lhsLclNum))
            {
                unsigned lclDefSsaNum = GetSsaNumForLocalVarDef(lclVarTree);

                if (lhs->IsLocalExpr(this, &lclVarTree, &lhsFldSeq) ||
                    (lhs->OperIsBlk() && (lhs->AsBlk()->gtBlkSize == lvaLclSize(lhsLclNum))))
                {
                    noway_assert(lclVarTree->gtLclNum == lhsLclNum);
                }
                else
                {
                    GenTree* lhsAddr;
                    if (lhs->OperIsBlk())
                    {
                        lhsAddr = lhs->AsBlk()->Addr();
                    }
                    else
                    {
                        assert(lhs->OperGet() == GT_IND);
                        lhsAddr = lhs->gtOp.gtOp1;
                    }
                    // For addr-of-local expressions, lib/cons shouldn't matter.
                    assert(lhsAddr->gtVNPair.BothEqual());
                    ValueNum lhsAddrVN = lhsAddr->GetVN(VNK_Liberal);

                    // Unpack the PtrToLoc value number of the address.
                    assert(vnStore->IsVNFunc(lhsAddrVN));
                    VNFuncApp lhsAddrFuncApp;
                    vnStore->GetVNFunc(lhsAddrVN, &lhsAddrFuncApp);
                    assert(lhsAddrFuncApp.m_func == VNF_PtrToLoc);
                    assert(vnStore->IsVNConstant(lhsAddrFuncApp.m_args[0]) &&
                           vnStore->ConstantValue<unsigned>(lhsAddrFuncApp.m_args[0]) == lhsLclNum);
                    lhsFldSeq = vnStore->FieldSeqVNToFieldSeq(lhsAddrFuncApp.m_args[1]);
                }

                // Now we need to get the proper RHS.
                GenTreeLclVarCommon* rhsLclVarTree = nullptr;
                LclVarDsc*           rhsVarDsc     = nullptr;
                FieldSeqNode*        rhsFldSeq     = nullptr;
                ValueNumPair         rhsVNPair;
                bool                 isNewUniq = false;
                if (!rhs->OperIsIndir())
                {
                    if (rhs->IsLocalExpr(this, &rhsLclVarTree, &rhsFldSeq))
                    {
                        unsigned rhsLclNum = rhsLclVarTree->GetLclNum();
                        rhsVarDsc          = &lvaTable[rhsLclNum];
                        if (fgExcludeFromSsa(rhsLclNum) || rhsFldSeq == FieldSeqStore::NotAField())
                        {
                            rhsVNPair.SetBoth(vnStore->VNForExpr(compCurBB, rhsLclVarTree->TypeGet()));
                            isNewUniq = true;
                        }
                        else
                        {
                            rhsVNPair = lvaTable[rhsLclVarTree->GetLclNum()]
                                            .GetPerSsaData(rhsLclVarTree->GetSsaNum())
                                            ->m_vnPair;
                            var_types indType = rhsLclVarTree->TypeGet();

                            rhsVNPair = vnStore->VNPairApplySelectors(rhsVNPair, rhsFldSeq, indType);
                        }
                    }
                    else
                    {
                        rhsVNPair.SetBoth(vnStore->VNForExpr(compCurBB, rhs->TypeGet()));
                        isNewUniq = true;
                    }
                }
                else
                {
                    GenTreePtr srcAddr = rhs->AsIndir()->Addr();
                    VNFuncApp  srcAddrFuncApp;
                    if (srcAddr->IsLocalAddrExpr(this, &rhsLclVarTree, &rhsFldSeq))
                    {
                        unsigned rhsLclNum = rhsLclVarTree->GetLclNum();
                        rhsVarDsc          = &lvaTable[rhsLclNum];
                        if (fgExcludeFromSsa(rhsLclNum) || rhsFldSeq == FieldSeqStore::NotAField())
                        {
                            isNewUniq = true;
                        }
                        else
                        {
                            rhsVNPair = lvaTable[rhsLclVarTree->GetLclNum()]
                                            .GetPerSsaData(rhsLclVarTree->GetSsaNum())
                                            ->m_vnPair;
                            var_types indType = rhsLclVarTree->TypeGet();

                            rhsVNPair = vnStore->VNPairApplySelectors(rhsVNPair, rhsFldSeq, indType);
                        }
                    }
                    else if (vnStore->GetVNFunc(vnStore->VNNormVal(srcAddr->gtVNPair.GetLiberal()), &srcAddrFuncApp))
                    {
                        if (srcAddrFuncApp.m_func == VNF_PtrToStatic)
                        {
                            var_types indType    = lclVarTree->TypeGet();
                            ValueNum  fieldSeqVN = srcAddrFuncApp.m_args[0];

                            FieldSeqNode* zeroOffsetFldSeq = nullptr;
                            if (GetZeroOffsetFieldMap()->Lookup(srcAddr, &zeroOffsetFldSeq))
                            {
                                fieldSeqVN =
                                    vnStore->FieldSeqVNAppend(fieldSeqVN, vnStore->VNForFieldSeq(zeroOffsetFldSeq));
                            }

                            FieldSeqNode* fldSeqForStaticVar = vnStore->FieldSeqVNToFieldSeq(fieldSeqVN);

                            if (fldSeqForStaticVar != FieldSeqStore::NotAField())
                            {
                                // We model statics as indices into the heap variable.
                                ValueNum selectedStaticVar;
                                size_t   structSize = 0;
                                selectedStaticVar   = vnStore->VNApplySelectors(VNK_Liberal, fgCurHeapVN,
                                                                              fldSeqForStaticVar, &structSize);
                                selectedStaticVar =
                                    vnStore->VNApplySelectorsTypeCheck(selectedStaticVar, indType, structSize);

                                rhsVNPair.SetLiberal(selectedStaticVar);
                                rhsVNPair.SetConservative(vnStore->VNForExpr(compCurBB, indType));
                            }
                            else
                            {
                                JITDUMP("    *** Missing field sequence info for Src/RHS of COPYBLK\n");
                                rhsVNPair.SetBoth(vnStore->VNForExpr(compCurBB, indType)); //  a new unique value number
                            }
                        }
                        else if (srcAddrFuncApp.m_func == VNF_PtrToArrElem)
                        {
                            ValueNum elemLib =
                                fgValueNumberArrIndexVal(nullptr, &srcAddrFuncApp, vnStore->VNForEmptyExcSet());
                            rhsVNPair.SetLiberal(elemLib);
                            rhsVNPair.SetConservative(vnStore->VNForExpr(compCurBB, lclVarTree->TypeGet()));
                        }
                        else
                        {
                            isNewUniq = true;
                        }
                    }
                    else
                    {
                        isNewUniq = true;
                    }
                }

                if (lhsFldSeq == FieldSeqStore::NotAField())
                {
                    // We don't have proper field sequence information for the lhs
                    //
                    JITDUMP("    *** Missing field sequence info for Dst/LHS of COPYBLK\n");
                    isNewUniq = true;
                }
                else if (lhsFldSeq != nullptr && isEntire)
                {
                    // This can occur in for structs with one field, itself of a struct type.
                    // We won't promote these.
                    // TODO-Cleanup: decide what exactly to do about this.
                    // Always treat them as maps, making them use/def, or reconstitute the
                    // map view here?
                    isNewUniq = true;
                }
                else if (!isNewUniq)
                {
                    ValueNumPair oldLhsVNPair = lvaTable[lhsLclNum].GetPerSsaData(lclVarTree->GetSsaNum())->m_vnPair;
                    rhsVNPair                 = vnStore->VNPairApplySelectorsAssign(oldLhsVNPair, lhsFldSeq, rhsVNPair,
                                                                    lclVarTree->TypeGet(), compCurBB);
                }

                if (isNewUniq)
                {
                    rhsVNPair.SetBoth(vnStore->VNForExpr(compCurBB, lclVarTree->TypeGet()));
                }

                lvaTable[lhsLclNum].GetPerSsaData(lclDefSsaNum)->m_vnPair = vnStore->VNPNormVal(rhsVNPair);

#ifdef DEBUG
                if (verbose)
                {
                    printf("Tree ");
                    Compiler::printTreeID(tree);
                    printf(" assigned VN to local var V%02u/%d: ", lhsLclNum, lclDefSsaNum);
                    if (isNewUniq)
                    {
                        printf("new uniq ");
                    }
                    vnpPrint(rhsVNPair, 1);
                    printf("\n");
                }
#endif // DEBUG
            }
        }
        else
        {
            // For now, arbitrary side effect on Heap.
            // TODO-CQ: Why not be complete, and get this case right?
            fgMutateHeap(tree DEBUGARG("COPYBLK - non local"));
        }
        // Copyblock's are of type void.  Give them the void "value" -- they may occur in argument lists, which we want
        // to be able to give VN's to.
        tree->gtVNPair.SetBoth(ValueNumStore::VNForVoid());
    }
}

void Compiler::fgValueNumberTree(GenTreePtr tree, bool evalAsgLhsInd)
{
    genTreeOps oper = tree->OperGet();

#ifdef FEATURE_SIMD
    // TODO-CQ: For now TYP_SIMD values are not handled by value numbering to be amenable for CSE'ing.
    if (oper == GT_SIMD)
    {
        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, TYP_UNKNOWN));
        return;
    }
#endif

    var_types typ = tree->TypeGet();
    if (GenTree::OperIsConst(oper))
    {
        // If this is a struct assignment, with a constant rhs, it is an initBlk, and it is not
        // really useful to value number the constant.
        if (!varTypeIsStruct(tree))
        {
            fgValueNumberTreeConst(tree);
        }
    }
    else if (GenTree::OperIsLeaf(oper))
    {
        switch (oper)
        {
            case GT_LCL_VAR:
            case GT_REG_VAR:
            {
                GenTreeLclVarCommon* lcl    = tree->AsLclVarCommon();
                unsigned             lclNum = lcl->gtLclNum;

                if ((lcl->gtFlags & GTF_VAR_DEF) == 0 ||
                    (lcl->gtFlags & GTF_VAR_USEASG)) // If it is a "pure" def, will handled as part of the assignment.
                {
                    LclVarDsc* varDsc = &lvaTable[lcl->gtLclNum];
                    if (varDsc->lvPromoted && varDsc->lvFieldCnt == 1)
                    {
                        // If the promoted var has only one field var, treat like a use of the field var.
                        lclNum = varDsc->lvFieldLclStart;
                    }

                    // Initialize to the undefined value, so we know whether we hit any of the cases here.
                    lcl->gtVNPair = ValueNumPair();

                    if (lcl->gtSsaNum == SsaConfig::RESERVED_SSA_NUM)
                    {
                        // Not an SSA variable.  Assign each occurrence a new, unique, VN.
                        lcl->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, lcl->TypeGet()));
                    }
                    else
                    {
                        var_types    varType        = varDsc->TypeGet();
                        ValueNumPair wholeLclVarVNP = varDsc->GetPerSsaData(lcl->gtSsaNum)->m_vnPair;

                        // Check for mismatched LclVar size
                        //
                        unsigned typSize = genTypeSize(genActualType(typ));
                        unsigned varSize = genTypeSize(genActualType(varType));

                        if (typSize == varSize)
                        {
                            lcl->gtVNPair = wholeLclVarVNP;
                        }
                        else // mismatched LclVar definition and LclVar use size
                        {
                            if (typSize < varSize)
                            {
                                // the indirection is reading less that the whole LclVar
                                // create a new VN that represent the partial value
                                //
                                ValueNumPair partialLclVarVNP = vnStore->VNPairForCast(wholeLclVarVNP, typ, varType);
                                lcl->gtVNPair                 = partialLclVarVNP;
                            }
                            else
                            {
                                assert(typSize > varSize);
                                // the indirection is reading beyond the end of the field
                                //
                                lcl->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, typ)); // return a new unique value
                                                                                           // number
                            }
                        }
                    }
                    // Temporary, to make progress.
                    // TODO-CQ: This should become an assert again...
                    if (lcl->gtVNPair.GetLiberal() == ValueNumStore::NoVN)
                    {
                        assert(lcl->gtVNPair.GetConservative() == ValueNumStore::NoVN);

                        // We don't want to fabricate arbitrary value numbers to things we can't reason about.
                        // So far, we know about two of these cases:
                        // Case 1) We have a local var who has never been defined but it's seen as a use.
                        //         This is the case of storeIndir(addr(lclvar)) = expr.  In this case since we only
                        //         take the address of the variable, this doesn't mean it's a use nor we have to
                        //         initialize it, so in this very rare case, we fabricate a value number.
                        // Case 2) Local variables that represent structs which are assigned using CpBlk.
                        GenTree* nextNode = lcl->gtNext;
                        assert((nextNode->gtOper == GT_ADDR && nextNode->gtOp.gtOp1 == lcl) ||
                               varTypeIsStruct(lcl->TypeGet()));
                        lcl->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, lcl->TypeGet()));
                    }
                    assert(lcl->gtVNPair.BothDefined());
                }

                // TODO-Review: For the short term, we have a workaround for copyblk/initblk.  Those that use
                // addrSpillTemp will have a statement like "addrSpillTemp = addr(local)."  If we previously decided
                // that this block operation defines the local, we will have labeled the "local" node as a DEF
                // (or USEDEF).  This flag propogates to the "local" on the RHS.  So we'll assume that this is correct,
                // and treat it as a def (to a new, unique VN).
                else if ((lcl->gtFlags & GTF_VAR_DEF) != 0)
                {
                    LclVarDsc* varDsc = &lvaTable[lcl->gtLclNum];
                    if (lcl->gtSsaNum != SsaConfig::RESERVED_SSA_NUM)
                    {
                        lvaTable[lclNum]
                            .GetPerSsaData(lcl->gtSsaNum)
                            ->m_vnPair.SetBoth(vnStore->VNForExpr(compCurBB, lcl->TypeGet()));
                    }
                    lcl->gtVNPair = ValueNumPair(); // Avoid confusion -- we don't set the VN of a lcl being defined.
                }
            }
            break;

            case GT_FTN_ADDR:
                // Use the value of the function pointer (actually, a method handle.)
                tree->gtVNPair.SetBoth(
                    vnStore->VNForHandle(ssize_t(tree->gtFptrVal.gtFptrMethod), GTF_ICON_METHOD_HDL));
                break;

            // This group passes through a value from a child node.
            case GT_RET_EXPR:
                tree->SetVNsFromNode(tree->gtRetExpr.gtInlineCandidate);
                break;

            case GT_LCL_FLD:
            {
                GenTreeLclFld* lclFld = tree->AsLclFld();
                assert(fgExcludeFromSsa(lclFld->GetLclNum()) || lclFld->gtFieldSeq != nullptr);
                // If this is a (full) def, then the variable will be labeled with the new SSA number,
                // which will not have a value.  We skip; it will be handled by one of the assignment-like
                // forms (assignment, or initBlk or copyBlk).
                if (((lclFld->gtFlags & GTF_VAR_DEF) == 0) || (lclFld->gtFlags & GTF_VAR_USEASG))
                {
                    unsigned   lclNum = lclFld->GetLclNum();
                    unsigned   ssaNum = lclFld->GetSsaNum();
                    LclVarDsc* varDsc = &lvaTable[lclNum];

                    if (ssaNum == SsaConfig::UNINIT_SSA_NUM)
                    {
                        if (varDsc->GetPerSsaData(ssaNum)->m_vnPair.GetLiberal() == ValueNumStore::NoVN)
                        {
                            ValueNum vnForLcl                       = vnStore->VNForExpr(compCurBB, lclFld->TypeGet());
                            varDsc->GetPerSsaData(ssaNum)->m_vnPair = ValueNumPair(vnForLcl, vnForLcl);
                        }
                    }

                    var_types indType = tree->TypeGet();
                    if (lclFld->gtFieldSeq == FieldSeqStore::NotAField() || fgExcludeFromSsa(lclFld->GetLclNum()))
                    {
                        // This doesn't represent a proper field access or it's a struct
                        // with overlapping fields that is hard to reason about; return a new unique VN.
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, indType));
                    }
                    else
                    {
                        ValueNumPair lclVNPair = varDsc->GetPerSsaData(ssaNum)->m_vnPair;
                        tree->gtVNPair         = vnStore->VNPairApplySelectors(lclVNPair, lclFld->gtFieldSeq, indType);
                    }
                }
            }
            break;

            // The ones below here all get a new unique VN -- but for various reasons, explained after each.
            case GT_CATCH_ARG:
                // We know nothing about the value of a caught expression.
                tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                break;

            case GT_CLS_VAR:
                // Skip GT_CLS_VAR nodes that are the LHS of an assignment.  (We labeled these earlier.)
                // We will "evaluate" this as part of the assignment.  (Unless we're explicitly told by
                // the caller to evaluate anyway -- perhaps the assignment is an "op=" assignment.)
                //
                if (((tree->gtFlags & GTF_CLS_VAR_ASG_LHS) == 0) || evalAsgLhsInd)
                {
                    bool isVolatile = (tree->gtFlags & GTF_FLD_VOLATILE) != 0;

                    if (isVolatile)
                    {
                        // For Volatile indirection, first mutate the global heap
                        fgMutateHeap(tree DEBUGARG("GTF_FLD_VOLATILE - read"));
                    }

                    // We just mutate the heap if isVolatile is true, and then do the read as normal.
                    //
                    // This allows:
                    //   1: read s;
                    //   2: volatile read s;
                    //   3: read s;
                    //
                    // We should never assume that the values read by 1 and 2 are the same (because the heap was mutated
                    // in between them)... but we *should* be able to prove that the values read in 2 and 3 are the
                    // same.
                    //

                    ValueNumPair clsVarVNPair;

                    // If the static field handle is for a struct type field, then the value of the static
                    // is a "ref" to the boxed struct -- treat it as the address of the static (we assume that a
                    // first element offset will be added to get to the actual struct...)
                    GenTreeClsVar* clsVar = tree->AsClsVar();
                    FieldSeqNode*  fldSeq = clsVar->gtFieldSeq;
                    assert(fldSeq != nullptr); // We need to have one.
                    ValueNum selectedStaticVar = ValueNumStore::NoVN;
                    if (gtIsStaticFieldPtrToBoxedStruct(clsVar->TypeGet(), fldSeq->m_fieldHnd))
                    {
                        clsVarVNPair.SetBoth(
                            vnStore->VNForFunc(TYP_BYREF, VNF_PtrToStatic, vnStore->VNForFieldSeq(fldSeq)));
                    }
                    else
                    {
                        // This is a reference to heap memory.
                        // We model statics as indices into the heap variable.

                        FieldSeqNode* fldSeqForStaticVar =
                            GetFieldSeqStore()->CreateSingleton(tree->gtClsVar.gtClsVarHnd);
                        size_t structSize = 0;
                        selectedStaticVar =
                            vnStore->VNApplySelectors(VNK_Liberal, fgCurHeapVN, fldSeqForStaticVar, &structSize);
                        selectedStaticVar =
                            vnStore->VNApplySelectorsTypeCheck(selectedStaticVar, tree->TypeGet(), structSize);

                        clsVarVNPair.SetLiberal(selectedStaticVar);
                        // The conservative interpretation always gets a new, unique VN.
                        clsVarVNPair.SetConservative(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    }

                    // The ValueNum returned must represent the full-sized IL-Stack value
                    // If we need to widen this value then we need to introduce a VNF_Cast here to represent
                    // the widened value.    This is necessary since the CSE package can replace all occurances
                    // of a given ValueNum with a LclVar that is a full-sized IL-Stack value
                    //
                    if (varTypeIsSmall(tree->TypeGet()))
                    {
                        var_types castToType = tree->TypeGet();
                        clsVarVNPair         = vnStore->VNPairForCast(clsVarVNPair, castToType, castToType);
                    }
                    tree->gtVNPair = clsVarVNPair;
                }
                break;

            case GT_MEMORYBARRIER: // Leaf
                // For MEMORYBARRIER add an arbitrary side effect on Heap.
                fgMutateHeap(tree DEBUGARG("MEMORYBARRIER"));
                break;

            // These do not represent values.
            case GT_NO_OP:
            case GT_JMP:   // Control flow
            case GT_LABEL: // Control flow
#if !FEATURE_EH_FUNCLETS
            case GT_END_LFIN: // Control flow
#endif
            case GT_ARGPLACE:
                // This node is a standin for an argument whose value will be computed later.  (Perhaps it's
                // a register argument, and we don't want to preclude use of the register in arg evaluation yet.)
                // We give this a "fake" value number now; if the call in which it occurs cares about the
                // value (e.g., it's a helper call whose result is a function of argument values) we'll reset
                // this later, when the later args have been assigned VNs.
                tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                break;

            case GT_PHI_ARG:
                // This one is special because we should never process it in this method: it should
                // always be taken care of, when needed, during pre-processing of a blocks phi definitions.
                assert(false);
                break;

            default:
                unreached();
        }
    }
    else if (GenTree::OperIsSimple(oper))
    {
#ifdef DEBUG
        // Sometimes we query the heap ssa map, and need a dummy location for the ignored result.
        unsigned heapSsaNum;
#endif

        if (GenTree::OperIsAssignment(oper) && !varTypeIsStruct(tree))
        {

            GenTreePtr lhs = tree->gtOp.gtOp1;
            GenTreePtr rhs = tree->gtOp.gtOp2;

            ValueNumPair rhsVNPair;
            if (oper == GT_ASG)
            {
                rhsVNPair = rhs->gtVNPair;
            }
            else // Must be an "op="
            {
                // If the LHS is an IND, we didn't evaluate it when we visited it previously.
                // But we didn't know that the parent was an op=.  We do now, so go back and evaluate it.
                // (We actually check if the effective val is the IND.  We will have evaluated any non-last
                // args of an LHS comma already -- including their heap effects.)
                GenTreePtr lhsVal = lhs->gtEffectiveVal(/*commaOnly*/ true);
                if (lhsVal->OperIsIndir() || (lhsVal->OperGet() == GT_CLS_VAR))
                {
                    fgValueNumberTree(lhsVal, /*evalAsgLhsInd*/ true);
                }
                // Now we can make this assertion:
                assert(lhsVal->gtVNPair.BothDefined());
                genTreeOps op = GenTree::OpAsgToOper(oper);
                if (GenTree::OperIsBinary(op))
                {
                    ValueNumPair lhsNormVNP;
                    ValueNumPair lhsExcVNP;
                    lhsExcVNP.SetBoth(ValueNumStore::VNForEmptyExcSet());
                    vnStore->VNPUnpackExc(lhsVal->gtVNPair, &lhsNormVNP, &lhsExcVNP);
                    assert(rhs->gtVNPair.BothDefined());
                    ValueNumPair rhsNormVNP;
                    ValueNumPair rhsExcVNP;
                    rhsExcVNP.SetBoth(ValueNumStore::VNForEmptyExcSet());
                    vnStore->VNPUnpackExc(rhs->gtVNPair, &rhsNormVNP, &rhsExcVNP);
                    rhsVNPair = vnStore->VNPWithExc(vnStore->VNPairForFunc(tree->TypeGet(),
                                                                           GetVNFuncForOper(op, (tree->gtFlags &
                                                                                                 GTF_UNSIGNED) != 0),
                                                                           lhsNormVNP, rhsNormVNP),
                                                    vnStore->VNPExcSetUnion(lhsExcVNP, rhsExcVNP));
                }
                else
                {
                    // As of now, GT_CHS ==> GT_NEG is the only pattern fitting this.
                    assert(GenTree::OperIsUnary(op));
                    ValueNumPair lhsNormVNP;
                    ValueNumPair lhsExcVNP;
                    lhsExcVNP.SetBoth(ValueNumStore::VNForEmptyExcSet());
                    vnStore->VNPUnpackExc(lhsVal->gtVNPair, &lhsNormVNP, &lhsExcVNP);
                    rhsVNPair = vnStore->VNPWithExc(vnStore->VNPairForFunc(tree->TypeGet(),
                                                                           GetVNFuncForOper(op, (tree->gtFlags &
                                                                                                 GTF_UNSIGNED) != 0),
                                                                           lhsNormVNP),
                                                    lhsExcVNP);
                }
            }
            if (tree->TypeGet() != TYP_VOID)
            {
                // Assignment operators, as expressions, return the value of the RHS.
                tree->gtVNPair = rhsVNPair;
            }

            // Now that we've labeled the assignment as a whole, we don't care about exceptions.
            rhsVNPair = vnStore->VNPNormVal(rhsVNPair);

            // If the types of the rhs and lhs are different then we
            //  may want to change the ValueNumber assigned to the lhs.
            //
            if (rhs->TypeGet() != lhs->TypeGet())
            {
                if (rhs->TypeGet() == TYP_REF)
                {
                    // If we have an unsafe IL assignment of a TYP_REF to a non-ref (typically a TYP_BYREF)
                    // then don't propagate this ValueNumber to the lhs, instead create a new unique VN
                    //
                    rhsVNPair.SetBoth(vnStore->VNForExpr(compCurBB, lhs->TypeGet()));
                }
            }

            // We have to handle the case where the LHS is a comma.  In that case, we don't evaluate the comma,
            // so we give it VNForVoid, and we're really interested in the effective value.
            GenTreePtr lhsCommaIter = lhs;
            while (lhsCommaIter->OperGet() == GT_COMMA)
            {
                lhsCommaIter->gtVNPair.SetBoth(vnStore->VNForVoid());
                lhsCommaIter = lhsCommaIter->gtOp.gtOp2;
            }
            lhs = lhs->gtEffectiveVal();

            // Now, record the new VN for an assignment (performing the indicated "state update").
            // It's safe to use gtEffectiveVal here, because the non-last elements of a comma list on the
            // LHS will come before the assignment in evaluation order.
            switch (lhs->OperGet())
            {
                case GT_LCL_VAR:
                case GT_REG_VAR:
                {
                    GenTreeLclVarCommon* lcl          = lhs->AsLclVarCommon();
                    unsigned             lclDefSsaNum = GetSsaNumForLocalVarDef(lcl);

                    // Should not have been recorded as updating the heap.
                    assert(!GetHeapSsaMap()->Lookup(tree, &heapSsaNum));

                    if (lclDefSsaNum != SsaConfig::RESERVED_SSA_NUM)
                    {
                        assert(rhsVNPair.GetLiberal() != ValueNumStore::NoVN);

                        lhs->gtVNPair                                                 = rhsVNPair;
                        lvaTable[lcl->gtLclNum].GetPerSsaData(lclDefSsaNum)->m_vnPair = rhsVNPair;

#ifdef DEBUG
                        if (verbose)
                        {
                            printf("N%03u ", lhs->gtSeqNum);
                            Compiler::printTreeID(lhs);
                            printf(" ");
                            gtDispNodeName(lhs);
                            gtDispLeaf(lhs, nullptr);
                            printf(" => ");
                            vnpPrint(lhs->gtVNPair, 1);
                            printf("\n");
                        }
#endif // DEBUG
                    }
#ifdef DEBUG
                    else
                    {
                        if (verbose)
                        {
                            JITDUMP("Tree ");
                            Compiler::printTreeID(tree);
                            printf(" assigns to local var V%02u; excluded from SSA, so value not tracked.\n",
                                   lcl->GetLclNum());
                        }
                    }
#endif // DEBUG
                }
                break;
                case GT_LCL_FLD:
                {
                    GenTreeLclFld* lclFld       = lhs->AsLclFld();
                    unsigned       lclDefSsaNum = GetSsaNumForLocalVarDef(lclFld);

                    // Should not have been recorded as updating the heap.
                    assert(!GetHeapSsaMap()->Lookup(tree, &heapSsaNum));

                    if (lclDefSsaNum != SsaConfig::RESERVED_SSA_NUM)
                    {
                        ValueNumPair newLhsVNPair;
                        // Is this a full definition?
                        if ((lclFld->gtFlags & GTF_VAR_USEASG) == 0)
                        {
                            assert(!lclFld->IsPartialLclFld(this));
                            assert(rhsVNPair.GetLiberal() != ValueNumStore::NoVN);
                            newLhsVNPair = rhsVNPair;
                        }
                        else
                        {
                            // We should never have a null field sequence here.
                            assert(lclFld->gtFieldSeq != nullptr);
                            if (lclFld->gtFieldSeq == FieldSeqStore::NotAField())
                            {
                                // We don't know what field this represents.  Assign a new VN to the whole variable
                                // (since we may be writing to an unknown portion of it.)
                                newLhsVNPair.SetBoth(vnStore->VNForExpr(compCurBB, lvaGetActualType(lclFld->gtLclNum)));
                            }
                            else
                            {
                                // We do know the field sequence.
                                // The "lclFld" node will be labeled with the SSA number of its "use" identity
                                // (we looked in a side table above for its "def" identity).  Look up that value.
                                ValueNumPair oldLhsVNPair =
                                    lvaTable[lclFld->GetLclNum()].GetPerSsaData(lclFld->GetSsaNum())->m_vnPair;
                                newLhsVNPair =
                                    vnStore->VNPairApplySelectorsAssign(oldLhsVNPair, lclFld->gtFieldSeq,
                                                                        rhsVNPair, // Pre-value.
                                                                        lvaGetActualType(lclFld->gtLclNum), compCurBB);
                            }
                        }
                        lvaTable[lclFld->GetLclNum()].GetPerSsaData(lclDefSsaNum)->m_vnPair = newLhsVNPair;
                        lhs->gtVNPair                                                       = newLhsVNPair;
#ifdef DEBUG
                        if (verbose)
                        {
                            if (lhs->gtVNPair.GetLiberal() != ValueNumStore::NoVN)
                            {
                                printf("N%03u ", lhs->gtSeqNum);
                                Compiler::printTreeID(lhs);
                                printf(" ");
                                gtDispNodeName(lhs);
                                gtDispLeaf(lhs, nullptr);
                                printf(" => ");
                                vnpPrint(lhs->gtVNPair, 1);
                                printf("\n");
                            }
                        }
#endif // DEBUG
                    }
                }
                break;

                case GT_PHI_ARG:
                    assert(false); // Phi arg cannot be LHS.

                case GT_BLK:
                case GT_OBJ:
                case GT_IND:
                {
                    bool isVolatile = (lhs->gtFlags & GTF_IND_VOLATILE) != 0;

                    if (isVolatile)
                    {
                        // For Volatile store indirection, first mutate the global heap
                        fgMutateHeap(lhs DEBUGARG("GTF_IND_VOLATILE - store"));
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, lhs->TypeGet()));
                    }

                    GenTreePtr arg = lhs->gtOp.gtOp1;

                    // Indicates whether the argument of the IND is the address of a local.
                    bool wasLocal = false;

                    lhs->gtVNPair = rhsVNPair;

                    VNFuncApp funcApp;
                    ValueNum  argVN = arg->gtVNPair.GetLiberal();

                    bool argIsVNFunc = vnStore->GetVNFunc(vnStore->VNNormVal(argVN), &funcApp);

                    // Is this an assignment to a (field of, perhaps) a local?
                    // If it is a PtrToLoc, lib and cons VNs will be the same.
                    if (argIsVNFunc)
                    {
                        IndirectAssignmentAnnotation* pIndirAnnot =
                            nullptr; // This will be used if "tree" is an "indirect assignment",
                                     // explained below.
                        if (funcApp.m_func == VNF_PtrToLoc)
                        {
                            assert(arg->gtVNPair.BothEqual()); // If it's a PtrToLoc, lib/cons shouldn't differ.
                            assert(vnStore->IsVNConstant(funcApp.m_args[0]));
                            unsigned lclNum = vnStore->ConstantValue<unsigned>(funcApp.m_args[0]);

                            wasLocal = true;

                            if (!fgExcludeFromSsa(lclNum))
                            {
                                FieldSeqNode* fieldSeq = vnStore->FieldSeqVNToFieldSeq(funcApp.m_args[1]);

                                // Either "arg" is the address of (part of) a local itself, or the assignment is an
                                // "indirect assignment", where an outer comma expression assigned the address of a
                                // local to a temp, and that temp is our lhs, and we recorded this in a table when we
                                // made the indirect assignment...or else we have a "rogue" PtrToLoc, one that should
                                // have made the local in question address-exposed.  Assert on that.
                                GenTreeLclVarCommon* lclVarTree   = nullptr;
                                bool                 isEntire     = false;
                                unsigned             lclDefSsaNum = SsaConfig::RESERVED_SSA_NUM;
                                ValueNumPair         newLhsVNPair;

                                if (arg->DefinesLocalAddr(this, genTypeSize(lhs->TypeGet()), &lclVarTree, &isEntire))
                                {
                                    // The local #'s should agree.
                                    assert(lclNum == lclVarTree->GetLclNum());

                                    if (fieldSeq == FieldSeqStore::NotAField())
                                    {
                                        // We don't know where we're storing, so give the local a new, unique VN.
                                        // Do this by considering it an "entire" assignment, with an unknown RHS.
                                        isEntire = true;
                                        rhsVNPair.SetBoth(vnStore->VNForExpr(compCurBB, lclVarTree->TypeGet()));
                                    }

                                    if (isEntire)
                                    {
                                        newLhsVNPair = rhsVNPair;
                                        lclDefSsaNum = lclVarTree->GetSsaNum();
                                    }
                                    else
                                    {
                                        // Don't use the lclVarTree's VN: if it's a local field, it will
                                        // already be dereferenced by it's field sequence.
                                        ValueNumPair oldLhsVNPair = lvaTable[lclVarTree->GetLclNum()]
                                                                        .GetPerSsaData(lclVarTree->GetSsaNum())
                                                                        ->m_vnPair;
                                        lclDefSsaNum = GetSsaNumForLocalVarDef(lclVarTree);
                                        newLhsVNPair =
                                            vnStore->VNPairApplySelectorsAssign(oldLhsVNPair, fieldSeq, rhsVNPair,
                                                                                lhs->TypeGet(), compCurBB);
                                    }
                                    lvaTable[lclNum].GetPerSsaData(lclDefSsaNum)->m_vnPair = newLhsVNPair;
                                }
                                else if (m_indirAssignMap != nullptr && GetIndirAssignMap()->Lookup(tree, &pIndirAnnot))
                                {
                                    // The local #'s should agree.
                                    assert(lclNum == pIndirAnnot->m_lclNum);
                                    assert(pIndirAnnot->m_defSsaNum != SsaConfig::RESERVED_SSA_NUM);
                                    lclDefSsaNum = pIndirAnnot->m_defSsaNum;
                                    // Does this assignment write the entire width of the local?
                                    if (genTypeSize(lhs->TypeGet()) == genTypeSize(var_types(lvaTable[lclNum].lvType)))
                                    {
                                        assert(pIndirAnnot->m_useSsaNum == SsaConfig::RESERVED_SSA_NUM);
                                        assert(pIndirAnnot->m_isEntire);
                                        newLhsVNPair = rhsVNPair;
                                    }
                                    else
                                    {
                                        assert(pIndirAnnot->m_useSsaNum != SsaConfig::RESERVED_SSA_NUM);
                                        assert(!pIndirAnnot->m_isEntire);
                                        assert(pIndirAnnot->m_fieldSeq == fieldSeq);
                                        ValueNumPair oldLhsVNPair =
                                            lvaTable[lclNum].GetPerSsaData(pIndirAnnot->m_useSsaNum)->m_vnPair;
                                        newLhsVNPair =
                                            vnStore->VNPairApplySelectorsAssign(oldLhsVNPair, fieldSeq, rhsVNPair,
                                                                                lhs->TypeGet(), compCurBB);
                                    }
                                    lvaTable[lclNum].GetPerSsaData(lclDefSsaNum)->m_vnPair = newLhsVNPair;
                                }
                                else
                                {
                                    unreached(); // "Rogue" PtrToLoc, as discussed above.
                                }
#ifdef DEBUG
                                if (verbose)
                                {
                                    printf("Tree ");
                                    Compiler::printTreeID(tree);
                                    printf(" assigned VN to local var V%02u/%d: VN ", lclNum, lclDefSsaNum);
                                    vnpPrint(newLhsVNPair, 1);
                                    printf("\n");
                                }
#endif // DEBUG
                            }
                        }
                    }

                    // Was the argument of the GT_IND the address of a local, handled above?
                    if (!wasLocal)
                    {
                        GenTreePtr    obj          = nullptr;
                        GenTreePtr    staticOffset = nullptr;
                        FieldSeqNode* fldSeq       = nullptr;

                        // Is the LHS an array index expression?
                        if (argIsVNFunc && funcApp.m_func == VNF_PtrToArrElem)
                        {
                            CORINFO_CLASS_HANDLE elemTypeEq =
                                CORINFO_CLASS_HANDLE(vnStore->ConstantValue<ssize_t>(funcApp.m_args[0]));
                            ValueNum      arrVN  = funcApp.m_args[1];
                            ValueNum      inxVN  = funcApp.m_args[2];
                            FieldSeqNode* fldSeq = vnStore->FieldSeqVNToFieldSeq(funcApp.m_args[3]);

                            // Does the child of the GT_IND 'arg' have an associated zero-offset field sequence?
                            FieldSeqNode* addrFieldSeq = nullptr;
                            if (GetZeroOffsetFieldMap()->Lookup(arg, &addrFieldSeq))
                            {
                                fldSeq = GetFieldSeqStore()->Append(addrFieldSeq, fldSeq);
                            }

#ifdef DEBUG
                            if (verbose)
                            {
                                printf("Tree ");
                                Compiler::printTreeID(tree);
                                printf(" assigns to an array element:\n");
                            }
#endif // DEBUG

                            fgValueNumberArrIndexAssign(elemTypeEq, arrVN, inxVN, fldSeq, rhsVNPair.GetLiberal(),
                                                        lhs->TypeGet());
                            fgValueNumberRecordHeapSsa(tree);
                        }
                        // It may be that we haven't parsed it yet.  Try.
                        else if (lhs->gtFlags & GTF_IND_ARR_INDEX)
                        {
                            ArrayInfo arrInfo;
                            bool      b = GetArrayInfoMap()->Lookup(lhs, &arrInfo);
                            assert(b);
                            ValueNum      arrVN  = ValueNumStore::NoVN;
                            ValueNum      inxVN  = ValueNumStore::NoVN;
                            FieldSeqNode* fldSeq = nullptr;

                            // Try to parse it.
                            GenTreePtr arr = nullptr;
                            arg->ParseArrayAddress(this, &arrInfo, &arr, &inxVN, &fldSeq);
                            if (arr == nullptr)
                            {
                                fgMutateHeap(tree DEBUGARG("assignment to unparseable array expression"));
                                return;
                            }
                            // Otherwise, parsing succeeded.

                            // Need to form H[arrType][arr][ind][fldSeq] = rhsVNPair.GetLiberal()

                            // Get the element type equivalence class representative.
                            CORINFO_CLASS_HANDLE elemTypeEq =
                                EncodeElemType(arrInfo.m_elemType, arrInfo.m_elemStructType);
                            arrVN = arr->gtVNPair.GetLiberal();

                            FieldSeqNode* zeroOffsetFldSeq = nullptr;
                            if (GetZeroOffsetFieldMap()->Lookup(arg, &zeroOffsetFldSeq))
                            {
                                fldSeq = GetFieldSeqStore()->Append(fldSeq, zeroOffsetFldSeq);
                            }

                            fgValueNumberArrIndexAssign(elemTypeEq, arrVN, inxVN, fldSeq, rhsVNPair.GetLiberal(),
                                                        lhs->TypeGet());
                            fgValueNumberRecordHeapSsa(tree);
                        }
                        else if (arg->IsFieldAddr(this, &obj, &staticOffset, &fldSeq))
                        {
                            if (fldSeq == FieldSeqStore::NotAField())
                            {
                                fgMutateHeap(tree DEBUGARG("NotAField"));
                            }
                            else
                            {
                                assert(fldSeq != nullptr);
#ifdef DEBUG
                                CORINFO_CLASS_HANDLE fldCls = info.compCompHnd->getFieldClass(fldSeq->m_fieldHnd);
                                if (obj != nullptr)
                                {
                                    // Make sure that the class containing it is not a value class (as we are expecting
                                    // an instance field)
                                    assert((info.compCompHnd->getClassAttribs(fldCls) & CORINFO_FLG_VALUECLASS) == 0);
                                    assert(staticOffset == nullptr);
                                }
#endif // DEBUG
                                // Get the first (instance or static) field from field seq.  Heap[field] will yield the
                                // "field map".
                                if (fldSeq->IsFirstElemFieldSeq())
                                {
                                    fldSeq = fldSeq->m_next;
                                    assert(fldSeq != nullptr);
                                }

                                // Get a field sequence for just the first field in the sequence
                                //
                                FieldSeqNode* firstFieldOnly = GetFieldSeqStore()->CreateSingleton(fldSeq->m_fieldHnd);

                                // The final field in the sequence will need to match the 'indType'
                                var_types indType = lhs->TypeGet();
                                ValueNum fldMapVN = vnStore->VNApplySelectors(VNK_Liberal, fgCurHeapVN, firstFieldOnly);

                                // The type of the field is "struct" if there are more fields in the sequence,
                                // otherwise it is the type returned from VNApplySelectors above.
                                var_types firstFieldType = vnStore->TypeOfVN(fldMapVN);

                                ValueNum storeVal =
                                    rhsVNPair.GetLiberal(); // The value number from the rhs of the assignment
                                ValueNum newFldMapVN = ValueNumStore::NoVN;

                                // when (obj != nullptr) we have an instance field, otherwise a static field
                                // when (staticOffset != nullptr) it represents a offset into a static or the call to
                                // Shared Static Base
                                if ((obj != nullptr) || (staticOffset != nullptr))
                                {
                                    ValueNum valAtAddr = fldMapVN;
                                    ValueNum normVal   = ValueNumStore::NoVN;

                                    if (obj != nullptr)
                                    {
                                        // construct the ValueNumber for 'fldMap at obj'
                                        normVal = vnStore->VNNormVal(obj->GetVN(VNK_Liberal));
                                        valAtAddr =
                                            vnStore->VNForMapSelect(VNK_Liberal, firstFieldType, fldMapVN, normVal);
                                    }
                                    else // (staticOffset != nullptr)
                                    {
                                        // construct the ValueNumber for 'fldMap at staticOffset'
                                        normVal = vnStore->VNNormVal(staticOffset->GetVN(VNK_Liberal));
                                        valAtAddr =
                                            vnStore->VNForMapSelect(VNK_Liberal, firstFieldType, fldMapVN, normVal);
                                    }
                                    // Now get rid of any remaining struct field dereferences. (if they exist)
                                    if (fldSeq->m_next)
                                    {
                                        storeVal =
                                            vnStore->VNApplySelectorsAssign(VNK_Liberal, valAtAddr, fldSeq->m_next,
                                                                            storeVal, indType, compCurBB);
                                    }

                                    // From which we can construct the new ValueNumber for 'fldMap at normVal'
                                    newFldMapVN = vnStore->VNForMapStore(vnStore->TypeOfVN(fldMapVN), fldMapVN, normVal,
                                                                         storeVal);
                                }
                                else
                                {
                                    // plain static field

                                    // Now get rid of any remaining struct field dereferences. (if they exist)
                                    if (fldSeq->m_next)
                                    {
                                        storeVal =
                                            vnStore->VNApplySelectorsAssign(VNK_Liberal, fldMapVN, fldSeq->m_next,
                                                                            storeVal, indType, compCurBB);
                                    }

                                    newFldMapVN = vnStore->VNApplySelectorsAssign(VNK_Liberal, fgCurHeapVN, fldSeq,
                                                                                  storeVal, indType, compCurBB);
                                }

                                // It is not strictly necessary to set the lhs value number,
                                // but the dumps read better with it set to the 'storeVal' that we just computed
                                lhs->gtVNPair.SetBoth(storeVal);

#ifdef DEBUG
                                if (verbose)
                                {
                                    printf("  fgCurHeapVN assigned:\n");
                                }
#endif // DEBUG
                                // bbHeapDef must be set to true for any block that Mutates the global Heap
                                assert(compCurBB->bbHeapDef);

                                // Update the field map for firstField in Heap to this new value.
                                fgCurHeapVN = vnStore->VNApplySelectorsAssign(VNK_Liberal, fgCurHeapVN, firstFieldOnly,
                                                                              newFldMapVN, indType, compCurBB);

                                fgValueNumberRecordHeapSsa(tree);
                            }
                        }
                        else
                        {
                            GenTreeLclVarCommon* dummyLclVarTree = nullptr;
                            if (!tree->DefinesLocal(this, &dummyLclVarTree))
                            {
                                // If it doesn't define a local, then it might update the heap.
                                fgMutateHeap(tree DEBUGARG("assign-of-IND"));
                            }
                        }
                    }

                    // We don't actually evaluate an IND on the LHS, so give it the Void value.
                    tree->gtVNPair.SetBoth(vnStore->VNForVoid());
                }
                break;

                case GT_CLS_VAR:
                {
                    bool isVolatile = (lhs->gtFlags & GTF_FLD_VOLATILE) != 0;

                    if (isVolatile)
                    {
                        // For Volatile store indirection, first mutate the global heap
                        fgMutateHeap(lhs DEBUGARG("GTF_CLS_VAR - store")); // always change fgCurHeapVN
                    }

                    // We model statics as indices into the heap variable.
                    FieldSeqNode* fldSeqForStaticVar = GetFieldSeqStore()->CreateSingleton(lhs->gtClsVar.gtClsVarHnd);
                    assert(fldSeqForStaticVar != FieldSeqStore::NotAField());

                    ValueNum storeVal = rhsVNPair.GetLiberal(); // The value number from the rhs of the assignment
                    storeVal = vnStore->VNApplySelectorsAssign(VNK_Liberal, fgCurHeapVN, fldSeqForStaticVar, storeVal,
                                                               lhs->TypeGet(), compCurBB);

                    // It is not strictly necessary to set the lhs value number,
                    // but the dumps read better with it set to the 'storeVal' that we just computed
                    lhs->gtVNPair.SetBoth(storeVal);
#ifdef DEBUG
                    if (verbose)
                    {
                        printf("  fgCurHeapVN assigned:\n");
                    }
#endif // DEBUG
                    // bbHeapDef must be set to true for any block that Mutates the global Heap
                    assert(compCurBB->bbHeapDef);

                    // Update the field map for the fgCurHeapVN
                    fgCurHeapVN = storeVal;
                    fgValueNumberRecordHeapSsa(tree);
                }
                break;

                default:
                    assert(!"Unknown node for lhs of assignment!");

                    // For Unknown stores, mutate the global heap
                    fgMutateHeap(lhs DEBUGARG("Unkwown Assignment - store")); // always change fgCurHeapVN
                    break;
            }
        }
        // Other kinds of assignment: initblk and copyblk.
        else if (oper == GT_ASG && varTypeIsStruct(tree))
        {
            fgValueNumberBlockAssignment(tree, evalAsgLhsInd);
        }
        else if (oper == GT_ADDR)
        {
            // We have special representations for byrefs to lvalues.
            GenTreePtr arg = tree->gtOp.gtOp1;
            if (arg->OperIsLocal())
            {
                FieldSeqNode* fieldSeq = nullptr;
                ValueNum      newVN    = ValueNumStore::NoVN;
                if (fgExcludeFromSsa(arg->gtLclVarCommon.GetLclNum()))
                {
                    newVN = vnStore->VNForExpr(compCurBB, TYP_BYREF);
                }
                else if (arg->OperGet() == GT_LCL_FLD)
                {
                    fieldSeq = arg->AsLclFld()->gtFieldSeq;
                    if (fieldSeq == nullptr)
                    {
                        // Local field with unknown field seq -- not a precise pointer.
                        newVN = vnStore->VNForExpr(compCurBB, TYP_BYREF);
                    }
                }
                if (newVN == ValueNumStore::NoVN)
                {
                    assert(arg->gtLclVarCommon.GetSsaNum() != ValueNumStore::NoVN);
                    newVN = vnStore->VNForPtrToLoc(TYP_BYREF, vnStore->VNForIntCon(arg->gtLclVarCommon.GetLclNum()),
                                                   vnStore->VNForFieldSeq(fieldSeq));
                }
                tree->gtVNPair.SetBoth(newVN);
            }
            else if ((arg->gtOper == GT_IND) || arg->OperIsBlk())
            {
                // Usually the ADDR and IND just cancel out...
                // except when this GT_ADDR has a valid zero-offset field sequence
                //
                FieldSeqNode* zeroOffsetFieldSeq = nullptr;
                if (GetZeroOffsetFieldMap()->Lookup(tree, &zeroOffsetFieldSeq) &&
                    (zeroOffsetFieldSeq != FieldSeqStore::NotAField()))
                {
                    ValueNum addrExtended = vnStore->ExtendPtrVN(arg->gtOp.gtOp1, zeroOffsetFieldSeq);
                    if (addrExtended != ValueNumStore::NoVN)
                    {
                        tree->gtVNPair.SetBoth(addrExtended); // We don't care about lib/cons differences for addresses.
                    }
                    else
                    {
                        // ExtendPtrVN returned a failure result
                        // So give this address a new unique value
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, TYP_BYREF));
                    }
                }
                else
                {
                    // They just cancel, so fetch the ValueNumber from the op1 of the GT_IND node.
                    //
                    GenTree* addr  = arg->AsIndir()->Addr();
                    tree->gtVNPair = addr->gtVNPair;

                    // For the CSE phase mark the address as GTF_DONT_CSE
                    // because it will end up with the same value number as tree (the GT_ADDR).
                    addr->gtFlags |= GTF_DONT_CSE;
                }
            }
            else
            {
                // May be more cases to do here!  But we'll punt for now.
                tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, TYP_BYREF));
            }
        }
        else if ((oper == GT_IND) || GenTree::OperIsBlk(oper))
        {
            // So far, we handle cases in which the address is a ptr-to-local, or if it's
            // a pointer to an object field.
            GenTreePtr           addr         = tree->AsIndir()->Addr();
            GenTreeLclVarCommon* lclVarTree   = nullptr;
            FieldSeqNode*        fldSeq1      = nullptr;
            FieldSeqNode*        fldSeq2      = nullptr;
            GenTreePtr           obj          = nullptr;
            GenTreePtr           staticOffset = nullptr;
            bool                 isVolatile   = (tree->gtFlags & GTF_IND_VOLATILE) != 0;

            // See if the addr has any exceptional part.
            ValueNumPair addrNvnp;
            ValueNumPair addrXvnp = ValueNumPair(ValueNumStore::VNForEmptyExcSet(), ValueNumStore::VNForEmptyExcSet());
            vnStore->VNPUnpackExc(addr->gtVNPair, &addrNvnp, &addrXvnp);

            // Is the dereference immutable?  If so, model it as referencing the read-only heap.
            if (tree->gtFlags & GTF_IND_INVARIANT)
            {
                assert(!isVolatile); // We don't expect both volatile and invariant
                tree->gtVNPair =
                    ValueNumPair(vnStore->VNForMapSelect(VNK_Liberal, TYP_REF, ValueNumStore::VNForROH(),
                                                         addrNvnp.GetLiberal()),
                                 vnStore->VNForMapSelect(VNK_Conservative, TYP_REF, ValueNumStore::VNForROH(),
                                                         addrNvnp.GetConservative()));
                tree->gtVNPair = vnStore->VNPWithExc(tree->gtVNPair, addrXvnp);
            }
            else if (isVolatile)
            {
                // For Volatile indirection, mutate the global heap
                fgMutateHeap(tree DEBUGARG("GTF_IND_VOLATILE - read"));

                // The value read by the GT_IND can immediately change
                ValueNum newUniq = vnStore->VNForExpr(compCurBB, tree->TypeGet());
                tree->gtVNPair   = vnStore->VNPWithExc(ValueNumPair(newUniq, newUniq), addrXvnp);
            }
            // We always want to evaluate the LHS when the GT_IND node is marked with GTF_IND_ARR_INDEX
            // as this will relabel the GT_IND child correctly using the VNF_PtrToArrElem
            else if ((tree->gtFlags & GTF_IND_ARR_INDEX) != 0)
            {
                ArrayInfo arrInfo;
                bool      b = GetArrayInfoMap()->Lookup(tree, &arrInfo);
                assert(b);

                ValueNum      inxVN  = ValueNumStore::NoVN;
                FieldSeqNode* fldSeq = nullptr;

                // GenTreePtr addr = tree->gtOp.gtOp1;
                ValueNum addrVN = addrNvnp.GetLiberal();

                // Try to parse it.
                GenTreePtr arr = nullptr;
                addr->ParseArrayAddress(this, &arrInfo, &arr, &inxVN, &fldSeq);
                if (arr == nullptr)
                {
                    tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    return;
                }
                assert(fldSeq != FieldSeqStore::NotAField());

                // Otherwise...
                // Need to form H[arrType][arr][ind][fldSeq]
                // Get the array element type equivalence class rep.
                CORINFO_CLASS_HANDLE elemTypeEq   = EncodeElemType(arrInfo.m_elemType, arrInfo.m_elemStructType);
                ValueNum             elemTypeEqVN = vnStore->VNForHandle(ssize_t(elemTypeEq), GTF_ICON_CLASS_HDL);

                // We take the "VNNormVal"s here, because if either has exceptional outcomes, they will be captured
                // as part of the value of the composite "addr" operation...
                ValueNum arrVN = vnStore->VNNormVal(arr->gtVNPair.GetLiberal());
                inxVN          = vnStore->VNNormVal(inxVN);

                // Additionally, relabel the address with a PtrToArrElem value number.
                ValueNum fldSeqVN = vnStore->VNForFieldSeq(fldSeq);
                ValueNum elemAddr =
                    vnStore->VNForFunc(TYP_BYREF, VNF_PtrToArrElem, elemTypeEqVN, arrVN, inxVN, fldSeqVN);

                // The aggregate "addr" VN should have had all the exceptions bubble up...
                elemAddr = vnStore->VNWithExc(elemAddr, addrXvnp.GetLiberal());
                addr->gtVNPair.SetBoth(elemAddr);
#ifdef DEBUG
                if (verbose)
                {
                    printf("  Relabeled IND_ARR_INDEX address node ");
                    Compiler::printTreeID(addr);
                    printf(" with l:" STR_VN "%x: ", elemAddr);
                    vnStore->vnDump(this, elemAddr);
                    printf("\n");
                    if (vnStore->VNNormVal(elemAddr) != elemAddr)
                    {
                        printf("      [" STR_VN "%x is: ", vnStore->VNNormVal(elemAddr));
                        vnStore->vnDump(this, vnStore->VNNormVal(elemAddr));
                        printf("]\n");
                    }
                }
#endif // DEBUG
                // We now need to retrieve the value number for the array element value
                // and give this value number to the GT_IND node 'tree'
                // We do this whenever we have an rvalue, or for the LHS when we have an "op=",
                // but we don't do it for a normal LHS assignment into an array element.
                //
                if (evalAsgLhsInd || ((tree->gtFlags & GTF_IND_ASG_LHS) == 0))
                {
                    fgValueNumberArrIndexVal(tree, elemTypeEq, arrVN, inxVN, addrXvnp.GetLiberal(), fldSeq);
                }
            }

            // In general we skip GT_IND nodes on that are the LHS of an assignment.  (We labeled these earlier.)
            // We will "evaluate" this as part of the assignment.  (Unless we're explicitly told by
            // the caller to evaluate anyway -- perhaps the assignment is an "op=" assignment.)
            else if (((tree->gtFlags & GTF_IND_ASG_LHS) == 0) || evalAsgLhsInd)
            {
                FieldSeqNode* localFldSeq = nullptr;
                VNFuncApp     funcApp;

                // Is it a local or a heap address?
                if (addr->IsLocalAddrExpr(this, &lclVarTree, &localFldSeq) &&
                    !fgExcludeFromSsa(lclVarTree->GetLclNum()))
                {
                    unsigned   lclNum = lclVarTree->GetLclNum();
                    unsigned   ssaNum = lclVarTree->GetSsaNum();
                    LclVarDsc* varDsc = &lvaTable[lclNum];

                    if ((localFldSeq == FieldSeqStore::NotAField()) || (localFldSeq == nullptr))
                    {
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    }
                    else
                    {
                        var_types    indType   = tree->TypeGet();
                        ValueNumPair lclVNPair = varDsc->GetPerSsaData(ssaNum)->m_vnPair;
                        tree->gtVNPair         = vnStore->VNPairApplySelectors(lclVNPair, localFldSeq, indType);
                        ;
                    }
                    tree->gtVNPair = vnStore->VNPWithExc(tree->gtVNPair, addrXvnp);
                }
                else if (vnStore->GetVNFunc(addrNvnp.GetLiberal(), &funcApp) && funcApp.m_func == VNF_PtrToStatic)
                {
                    var_types indType    = tree->TypeGet();
                    ValueNum  fieldSeqVN = funcApp.m_args[0];

                    FieldSeqNode* fldSeqForStaticVar = vnStore->FieldSeqVNToFieldSeq(fieldSeqVN);

                    if (fldSeqForStaticVar != FieldSeqStore::NotAField())
                    {
                        ValueNum selectedStaticVar;
                        // We model statics as indices into the heap variable.
                        size_t structSize = 0;
                        selectedStaticVar =
                            vnStore->VNApplySelectors(VNK_Liberal, fgCurHeapVN, fldSeqForStaticVar, &structSize);
                        selectedStaticVar = vnStore->VNApplySelectorsTypeCheck(selectedStaticVar, indType, structSize);

                        tree->gtVNPair.SetLiberal(selectedStaticVar);
                        tree->gtVNPair.SetConservative(vnStore->VNForExpr(compCurBB, indType));
                    }
                    else
                    {
                        JITDUMP("    *** Missing field sequence info for VNF_PtrToStatic value GT_IND\n");
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, indType)); //  a new unique value number
                    }
                    tree->gtVNPair = vnStore->VNPWithExc(tree->gtVNPair, addrXvnp);
                }
                else if (vnStore->GetVNFunc(addrNvnp.GetLiberal(), &funcApp) && (funcApp.m_func == VNF_PtrToArrElem))
                {
                    fgValueNumberArrIndexVal(tree, &funcApp, addrXvnp.GetLiberal());
                }
                else if (addr->IsFieldAddr(this, &obj, &staticOffset, &fldSeq2))
                {
                    if (fldSeq2 == FieldSeqStore::NotAField())
                    {
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    }
                    else if (fldSeq2 != nullptr)
                    {
                        // Get the first (instance or static) field from field seq.  Heap[field] will yield the "field
                        // map".
                        CLANG_FORMAT_COMMENT_ANCHOR;

#ifdef DEBUG
                        CORINFO_CLASS_HANDLE fldCls = info.compCompHnd->getFieldClass(fldSeq2->m_fieldHnd);
                        if (obj != nullptr)
                        {
                            // Make sure that the class containing it is not a value class (as we are expecting an
                            // instance field)
                            assert((info.compCompHnd->getClassAttribs(fldCls) & CORINFO_FLG_VALUECLASS) == 0);
                            assert(staticOffset == nullptr);
                        }
#endif // DEBUG
                        // Get a field sequence for just the first field in the sequence
                        //
                        FieldSeqNode* firstFieldOnly = GetFieldSeqStore()->CreateSingleton(fldSeq2->m_fieldHnd);
                        size_t        structSize     = 0;
                        ValueNum      fldMapVN =
                            vnStore->VNApplySelectors(VNK_Liberal, fgCurHeapVN, firstFieldOnly, &structSize);

                        // The final field in the sequence will need to match the 'indType'
                        var_types indType = tree->TypeGet();

                        // The type of the field is "struct" if there are more fields in the sequence,
                        // otherwise it is the type returned from VNApplySelectors above.
                        var_types firstFieldType = vnStore->TypeOfVN(fldMapVN);

                        ValueNum valAtAddr = fldMapVN;
                        if (obj != nullptr)
                        {
                            // construct the ValueNumber for 'fldMap at obj'
                            ValueNum objNormVal = vnStore->VNNormVal(obj->GetVN(VNK_Liberal));
                            valAtAddr = vnStore->VNForMapSelect(VNK_Liberal, firstFieldType, fldMapVN, objNormVal);
                        }
                        else if (staticOffset != nullptr)
                        {
                            // construct the ValueNumber for 'fldMap at staticOffset'
                            ValueNum offsetNormVal = vnStore->VNNormVal(staticOffset->GetVN(VNK_Liberal));
                            valAtAddr = vnStore->VNForMapSelect(VNK_Liberal, firstFieldType, fldMapVN, offsetNormVal);
                        }

                        // Now get rid of any remaining struct field dereferences.
                        if (fldSeq2->m_next)
                        {
                            valAtAddr = vnStore->VNApplySelectors(VNK_Liberal, valAtAddr, fldSeq2->m_next, &structSize);
                        }
                        valAtAddr = vnStore->VNApplySelectorsTypeCheck(valAtAddr, indType, structSize);

                        tree->gtVNPair.SetLiberal(valAtAddr);

                        // The conservative value is a new, unique VN.
                        tree->gtVNPair.SetConservative(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                        tree->gtVNPair = vnStore->VNPWithExc(tree->gtVNPair, addrXvnp);
                    }
                    else
                    {
                        // Occasionally we do an explicit null test on a REF, so we just dereference it with no
                        // field sequence.  The result is probably unused.
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                        tree->gtVNPair = vnStore->VNPWithExc(tree->gtVNPair, addrXvnp);
                    }
                }
                else // We don't know where the address points.
                {
                    tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    tree->gtVNPair = vnStore->VNPWithExc(tree->gtVNPair, addrXvnp);
                }
            }
        }
        else if (tree->OperGet() == GT_CAST)
        {
            fgValueNumberCastTree(tree);
        }
        else if (tree->OperGet() == GT_INTRINSIC)
        {
            fgValueNumberIntrinsic(tree);
        }
        else if (ValueNumStore::VNFuncIsLegal(GetVNFuncForOper(oper, (tree->gtFlags & GTF_UNSIGNED) != 0)))
        {
            if (GenTree::OperIsUnary(oper))
            {
                if (tree->gtOp.gtOp1 != nullptr)
                {
                    if (tree->OperGet() == GT_NOP)
                    {
                        // Pass through arg vn.
                        tree->gtVNPair = tree->gtOp.gtOp1->gtVNPair;
                    }
                    else
                    {
                        ValueNumPair op1VNP;
                        ValueNumPair op1VNPx = ValueNumStore::VNPForEmptyExcSet();
                        vnStore->VNPUnpackExc(tree->gtOp.gtOp1->gtVNPair, &op1VNP, &op1VNPx);
                        tree->gtVNPair =
                            vnStore->VNPWithExc(vnStore->VNPairForFunc(tree->TypeGet(),
                                                                       GetVNFuncForOper(oper, (tree->gtFlags &
                                                                                               GTF_UNSIGNED) != 0),
                                                                       op1VNP),
                                                op1VNPx);
                    }
                }
                else // Is actually nullary.
                {
                    // Mostly we'll leave these without a value number, assuming we'll detect these as VN failures
                    // if they actually need to have values.  With the exception of NOPs, which can sometimes have
                    // meaning.
                    if (tree->OperGet() == GT_NOP)
                    {
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    }
                }
            }
            else
            {
                assert(!GenTree::OperIsAssignment(oper)); // We handled assignments earlier.
                assert(GenTree::OperIsBinary(oper));
                // Standard binary operator.
                ValueNumPair op2VNPair;
                if (tree->gtOp.gtOp2 == nullptr)
                {
                    op2VNPair.SetBoth(ValueNumStore::VNForNull());
                }
                else
                {
                    op2VNPair = tree->gtOp.gtOp2->gtVNPair;
                }
                // A few special case: if we add a field offset constant to a PtrToXXX, we get back a new PtrToXXX.
                ValueNum newVN = ValueNumStore::NoVN;

                ValueNumPair op1vnp;
                ValueNumPair op1Xvnp = ValueNumStore::VNPForEmptyExcSet();
                vnStore->VNPUnpackExc(tree->gtOp.gtOp1->gtVNPair, &op1vnp, &op1Xvnp);
                ValueNumPair op2vnp;
                ValueNumPair op2Xvnp = ValueNumStore::VNPForEmptyExcSet();
                vnStore->VNPUnpackExc(op2VNPair, &op2vnp, &op2Xvnp);
                ValueNumPair excSet = vnStore->VNPExcSetUnion(op1Xvnp, op2Xvnp);

                if (oper == GT_ADD)
                {
                    newVN = vnStore->ExtendPtrVN(tree->gtOp.gtOp1, tree->gtOp.gtOp2);
                    if (newVN == ValueNumStore::NoVN)
                    {
                        newVN = vnStore->ExtendPtrVN(tree->gtOp.gtOp2, tree->gtOp.gtOp1);
                    }
                }
                if (newVN != ValueNumStore::NoVN)
                {
                    newVN = vnStore->VNWithExc(newVN, excSet.GetLiberal());
                    // We don't care about differences between liberal and conservative for pointer values.
                    tree->gtVNPair.SetBoth(newVN);
                }
                else
                {

                    ValueNumPair normalRes =
                        vnStore->VNPairForFunc(tree->TypeGet(),
                                               GetVNFuncForOper(oper, (tree->gtFlags & GTF_UNSIGNED) != 0), op1vnp,
                                               op2vnp);
                    // Overflow-checking operations add an overflow exception
                    if (tree->gtOverflowEx())
                    {
                        ValueNum overflowExcSet =
                            vnStore->VNExcSetSingleton(vnStore->VNForFunc(TYP_REF, VNF_OverflowExc));
                        excSet = vnStore->VNPExcSetUnion(excSet, ValueNumPair(overflowExcSet, overflowExcSet));
                    }
                    tree->gtVNPair = vnStore->VNPWithExc(normalRes, excSet);
                }
            }
        }
        else // ValueNumStore::VNFuncIsLegal returns false
        {
            // Some of the genTreeOps that aren't legal VNFuncs so they get special handling.
            switch (oper)
            {
                case GT_COMMA:
                {
                    ValueNumPair op1vnp;
                    ValueNumPair op1Xvnp = ValueNumStore::VNPForEmptyExcSet();
                    vnStore->VNPUnpackExc(tree->gtOp.gtOp1->gtVNPair, &op1vnp, &op1Xvnp);
                    ValueNumPair op2vnp;
                    ValueNumPair op2Xvnp = ValueNumStore::VNPForEmptyExcSet();

                    GenTree* op2 = tree->gtGetOp2();
                    if (op2->OperIsIndir() && ((op2->gtFlags & GTF_IND_ASG_LHS) != 0))
                    {
                        // If op2 represents the lhs of an assignment then we give a VNForVoid for the lhs
                        op2vnp = ValueNumPair(ValueNumStore::VNForVoid(), ValueNumStore::VNForVoid());
                    }
                    else if ((op2->OperGet() == GT_CLS_VAR) && (op2->gtFlags & GTF_CLS_VAR_ASG_LHS))
                    {
                        // If op2 represents the lhs of an assignment then we give a VNForVoid for the lhs
                        op2vnp = ValueNumPair(ValueNumStore::VNForVoid(), ValueNumStore::VNForVoid());
                    }
                    else
                    {
                        vnStore->VNPUnpackExc(op2->gtVNPair, &op2vnp, &op2Xvnp);
                    }

                    tree->gtVNPair = vnStore->VNPWithExc(op2vnp, vnStore->VNPExcSetUnion(op1Xvnp, op2Xvnp));
                }
                break;

                case GT_NULLCHECK:
                    // Explicit null check.
                    tree->gtVNPair =
                        vnStore->VNPWithExc(ValueNumPair(ValueNumStore::VNForVoid(), ValueNumStore::VNForVoid()),
                                            vnStore->VNPExcSetSingleton(
                                                vnStore->VNPairForFunc(TYP_REF, VNF_NullPtrExc,
                                                                       tree->gtOp.gtOp1->gtVNPair)));
                    break;

                case GT_BLK:
                case GT_OBJ:
                case GT_IND:
                    if (tree->gtFlags & GTF_IND_ARR_LEN)
                    {
                        // It's an array length.  The argument is the sum of an array ref with some integer values...
                        ValueNum arrRefLib  = vnStore->VNForRefInAddr(tree->gtOp.gtOp1->gtVNPair.GetLiberal());
                        ValueNum arrRefCons = vnStore->VNForRefInAddr(tree->gtOp.gtOp1->gtVNPair.GetConservative());

                        assert(vnStore->TypeOfVN(arrRefLib) == TYP_REF || vnStore->TypeOfVN(arrRefLib) == TYP_BYREF);
                        if (vnStore->IsVNConstant(arrRefLib))
                        {
                            // (or in weird cases, a REF or BYREF constant, in which case the result is an exception).
                            tree->gtVNPair.SetLiberal(
                                vnStore->VNWithExc(ValueNumStore::VNForVoid(),
                                                   vnStore->VNExcSetSingleton(
                                                       vnStore->VNForFunc(TYP_REF, VNF_NullPtrExc, arrRefLib))));
                        }
                        else
                        {
                            tree->gtVNPair.SetLiberal(vnStore->VNForFunc(TYP_INT, VNFunc(GT_ARR_LENGTH), arrRefLib));
                        }
                        assert(vnStore->TypeOfVN(arrRefCons) == TYP_REF || vnStore->TypeOfVN(arrRefCons) == TYP_BYREF);
                        if (vnStore->IsVNConstant(arrRefCons))
                        {
                            // (or in weird cases, a REF or BYREF constant, in which case the result is an exception).
                            tree->gtVNPair.SetConservative(
                                vnStore->VNWithExc(ValueNumStore::VNForVoid(),
                                                   vnStore->VNExcSetSingleton(
                                                       vnStore->VNForFunc(TYP_REF, VNF_NullPtrExc, arrRefCons))));
                        }
                        else
                        {
                            tree->gtVNPair.SetConservative(
                                vnStore->VNForFunc(TYP_INT, VNFunc(GT_ARR_LENGTH), arrRefCons));
                        }
                    }
                    else
                    {
                        tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    }
                    break;

                case GT_LOCKADD: // Binop
                case GT_XADD:    // Binop
                case GT_XCHG:    // Binop
                    // For CMPXCHG and other intrinsics add an arbitrary side effect on Heap.
                    fgMutateHeap(tree DEBUGARG("Interlocked intrinsic"));
                    tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    break;

                case GT_JTRUE:
                case GT_LIST:
#ifndef LEGACY_BACKEND
                case GT_FIELD_LIST:
#endif // !LEGACY_BACKEND
                    // These nodes never need to have a ValueNumber
                    tree->gtVNPair.SetBoth(ValueNumStore::NoVN);
                    break;

                default:
                    // The default action is to give the node a new, unique VN.
                    tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                    break;
            }
        }
    }
    else
    {
        assert(GenTree::OperIsSpecial(oper));

        // TBD: We must handle these individually.  For now:
        switch (oper)
        {
            case GT_CALL:
                fgValueNumberCall(tree->AsCall());
                break;

            case GT_ARR_BOUNDS_CHECK:
#ifdef FEATURE_SIMD
            case GT_SIMD_CHK:
#endif // FEATURE_SIMD
            {
                // A bounds check node has no value, but may throw exceptions.
                ValueNumPair excSet = vnStore->VNPExcSetSingleton(
                    vnStore->VNPairForFunc(TYP_REF, VNF_IndexOutOfRangeExc,
                                           vnStore->VNPNormVal(tree->AsBoundsChk()->gtArrLen->gtVNPair),
                                           vnStore->VNPNormVal(tree->AsBoundsChk()->gtIndex->gtVNPair)));
                excSet = vnStore->VNPExcSetUnion(excSet, vnStore->VNPExcVal(tree->AsBoundsChk()->gtArrLen->gtVNPair));
                excSet = vnStore->VNPExcSetUnion(excSet, vnStore->VNPExcVal(tree->AsBoundsChk()->gtIndex->gtVNPair));

                tree->gtVNPair = vnStore->VNPWithExc(vnStore->VNPForVoid(), excSet);
            }
            break;

            case GT_CMPXCHG: // Specialop
                // For CMPXCHG and other intrinsics add an arbitrary side effect on Heap.
                fgMutateHeap(tree DEBUGARG("Interlocked intrinsic"));
                tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
                break;

            default:
                tree->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, tree->TypeGet()));
        }
    }
#ifdef DEBUG
    if (verbose)
    {
        if (tree->gtVNPair.GetLiberal() != ValueNumStore::NoVN)
        {
            printf("N%03u ", tree->gtSeqNum);
            printTreeID(tree);
            printf(" ");
            gtDispNodeName(tree);
            if (tree->OperIsLeaf() || tree->OperIsLocalStore()) // local stores used to be leaves
            {
                gtDispLeaf(tree, nullptr);
            }
            printf(" => ");
            vnpPrint(tree->gtVNPair, 1);
            printf("\n");
        }
    }
#endif // DEBUG
}

void Compiler::fgValueNumberIntrinsic(GenTreePtr tree)
{
    assert(tree->OperGet() == GT_INTRINSIC);
    GenTreeIntrinsic* intrinsic = tree->AsIntrinsic();
    ValueNumPair      arg0VNP, arg1VNP;
    ValueNumPair      arg0VNPx = ValueNumStore::VNPForEmptyExcSet();
    ValueNumPair      arg1VNPx = ValueNumStore::VNPForEmptyExcSet();

    vnStore->VNPUnpackExc(intrinsic->gtOp.gtOp1->gtVNPair, &arg0VNP, &arg0VNPx);

    if (intrinsic->gtOp.gtOp2 != nullptr)
    {
        vnStore->VNPUnpackExc(intrinsic->gtOp.gtOp2->gtVNPair, &arg1VNP, &arg1VNPx);
    }

    switch (intrinsic->gtIntrinsicId)
    {
        case CORINFO_INTRINSIC_Sin:
        case CORINFO_INTRINSIC_Sqrt:
        case CORINFO_INTRINSIC_Abs:
        case CORINFO_INTRINSIC_Cos:
        case CORINFO_INTRINSIC_Round:
        case CORINFO_INTRINSIC_Cosh:
        case CORINFO_INTRINSIC_Sinh:
        case CORINFO_INTRINSIC_Tan:
        case CORINFO_INTRINSIC_Tanh:
        case CORINFO_INTRINSIC_Asin:
        case CORINFO_INTRINSIC_Acos:
        case CORINFO_INTRINSIC_Atan:
        case CORINFO_INTRINSIC_Atan2:
        case CORINFO_INTRINSIC_Log10:
        case CORINFO_INTRINSIC_Pow:
        case CORINFO_INTRINSIC_Exp:
        case CORINFO_INTRINSIC_Ceiling:
        case CORINFO_INTRINSIC_Floor:

            // GT_INTRINSIC is a currently a subtype of binary operators. But most of
            // the math intrinsics are actually unary operations.

            if (intrinsic->gtOp.gtOp2 == nullptr)
            {
                intrinsic->gtVNPair =
                    vnStore->VNPWithExc(vnStore->EvalMathFuncUnary(tree->TypeGet(), intrinsic->gtIntrinsicId, arg0VNP),
                                        arg0VNPx);
            }
            else
            {
                ValueNumPair newVNP =
                    vnStore->EvalMathFuncBinary(tree->TypeGet(), intrinsic->gtIntrinsicId, arg0VNP, arg1VNP);
                ValueNumPair excSet = vnStore->VNPExcSetUnion(arg0VNPx, arg1VNPx);
                intrinsic->gtVNPair = vnStore->VNPWithExc(newVNP, excSet);
            }

            break;

        case CORINFO_INTRINSIC_Object_GetType:
            intrinsic->gtVNPair =
                vnStore->VNPWithExc(vnStore->VNPairForFunc(intrinsic->TypeGet(), VNF_ObjGetType, arg0VNP), arg0VNPx);
            break;

        default:
            unreached();
    }
}

void Compiler::fgValueNumberCastTree(GenTreePtr tree)
{
    assert(tree->OperGet() == GT_CAST);

    ValueNumPair srcVNPair        = tree->gtOp.gtOp1->gtVNPair;
    var_types    castToType       = tree->CastToType();
    var_types    castFromType     = tree->CastFromType();
    bool         srcIsUnsigned    = ((tree->gtFlags & GTF_UNSIGNED) != 0);
    bool         hasOverflowCheck = tree->gtOverflowEx();

    assert(genActualType(castToType) == genActualType(tree->TypeGet())); // Insure that the resultType is correct

    tree->gtVNPair = vnStore->VNPairForCast(srcVNPair, castToType, castFromType, srcIsUnsigned, hasOverflowCheck);
}

// Compute the normal ValueNumber for a cast operation with no exceptions
ValueNum ValueNumStore::VNForCast(ValueNum  srcVN,
                                  var_types castToType,
                                  var_types castFromType,
                                  bool      srcIsUnsigned /* = false */)
{
    // The resulting type after performingthe cast is always widened to a supported IL stack size
    var_types resultType = genActualType(castToType);

    // When we're considering actual value returned by a non-checking cast whether or not the source is
    // unsigned does *not* matter for non-widening casts.  That is, if we cast an int or a uint to short,
    // we just extract the first two bytes from the source bit pattern, not worrying about the interpretation.
    // The same is true in casting between signed/unsigned types of the same width.  Only when we're doing
    // a widening cast do we care about whether the source was unsigned,so we know whether to sign or zero extend it.
    //
    bool srcIsUnsignedNorm = srcIsUnsigned;
    if (genTypeSize(castToType) <= genTypeSize(castFromType))
    {
        srcIsUnsignedNorm = false;
    }

    ValueNum castTypeVN = VNForCastOper(castToType, srcIsUnsigned);
    ValueNum resultVN   = VNForFunc(resultType, VNF_Cast, srcVN, castTypeVN);

#ifdef DEBUG
    if (m_pComp->verbose)
    {
        printf("    VNForCast(" STR_VN "%x, " STR_VN "%x) returns ", srcVN, castTypeVN);
        m_pComp->vnPrint(resultVN, 1);
        printf("\n");
    }
#endif

    return resultVN;
}

// Compute the ValueNumberPair for a cast operation
ValueNumPair ValueNumStore::VNPairForCast(ValueNumPair srcVNPair,
                                          var_types    castToType,
                                          var_types    castFromType,
                                          bool         srcIsUnsigned,    /* = false */
                                          bool         hasOverflowCheck) /* = false */
{
    // The resulting type after performingthe cast is always widened to a supported IL stack size
    var_types resultType = genActualType(castToType);

    ValueNumPair castArgVNP;
    ValueNumPair castArgxVNP = ValueNumStore::VNPForEmptyExcSet();
    VNPUnpackExc(srcVNPair, &castArgVNP, &castArgxVNP);

    // When we're considering actual value returned by a non-checking cast (or a checking cast that succeeds),
    // whether or not the source is unsigned does *not* matter for non-widening casts.
    // That is, if we cast an int or a uint to short, we just extract the first two bytes from the source
    // bit pattern, not worrying about the interpretation.  The same is true in casting between signed/unsigned
    // types of the same width.  Only when we're doing a widening cast do we care about whether the source
    // was unsigned, so we know whether to sign or zero extend it.
    //
    // Important: Casts to floating point cannot be optimized in this fashion. (bug 946768)
    //
    bool srcIsUnsignedNorm = srcIsUnsigned;
    if (genTypeSize(castToType) <= genTypeSize(castFromType) && !varTypeIsFloating(castToType))
    {
        srcIsUnsignedNorm = false;
    }

    ValueNum     castTypeVN = VNForCastOper(castToType, srcIsUnsignedNorm);
    ValueNumPair castTypeVNPair(castTypeVN, castTypeVN);
    ValueNumPair castNormRes = VNPairForFunc(resultType, VNF_Cast, castArgVNP, castTypeVNPair);

    ValueNumPair resultVNP = VNPWithExc(castNormRes, castArgxVNP);

    // If we have a check for overflow, add the exception information.
    if (hasOverflowCheck)
    {
        // For overflow checking, we always need to know whether the source is unsigned.
        castTypeVNPair.SetBoth(VNForCastOper(castToType, srcIsUnsigned));
        ValueNumPair excSet =
            VNPExcSetSingleton(VNPairForFunc(TYP_REF, VNF_ConvOverflowExc, castArgVNP, castTypeVNPair));
        excSet    = VNPExcSetUnion(excSet, castArgxVNP);
        resultVNP = VNPWithExc(castNormRes, excSet);
    }

    return resultVNP;
}

void Compiler::fgValueNumberHelperCallFunc(GenTreeCall* call, VNFunc vnf, ValueNumPair vnpExc)
{
    unsigned nArgs = ValueNumStore::VNFuncArity(vnf);
    assert(vnf != VNF_Boundary);
    GenTreeArgList* args                    = call->gtCallArgs;
    bool            generateUniqueVN        = false;
    bool            useEntryPointAddrAsArg0 = false;

    switch (vnf)
    {
        case VNF_JitNew:
        {
            generateUniqueVN = true;
            vnpExc           = ValueNumStore::VNPForEmptyExcSet();
        }
        break;

        case VNF_JitNewArr:
        {
            generateUniqueVN  = true;
            ValueNumPair vnp1 = vnStore->VNPNormVal(args->Rest()->Current()->gtVNPair);

            // The New Array helper may throw an overflow exception
            vnpExc = vnStore->VNPExcSetSingleton(vnStore->VNPairForFunc(TYP_REF, VNF_NewArrOverflowExc, vnp1));
        }
        break;

        case VNF_BoxNullable:
        {
            // Generate unique VN so, VNForFunc generates a uniq value number for box nullable.
            // Alternatively instead of using vnpUniq below in VNPairForFunc(...),
            // we could use the value number of what the byref arg0 points to.
            //
            // But retrieving the value number of what the byref arg0 points to is quite a bit more work
            // and doing so only very rarely allows for an additional optimization.
            generateUniqueVN = true;
        }
        break;

        case VNF_JitReadyToRunNew:
        {
            generateUniqueVN        = true;
            vnpExc                  = ValueNumStore::VNPForEmptyExcSet();
            useEntryPointAddrAsArg0 = true;
        }
        break;

        case VNF_JitReadyToRunNewArr:
        {
            generateUniqueVN  = true;
            ValueNumPair vnp1 = vnStore->VNPNormVal(args->Current()->gtVNPair);

            // The New Array helper may throw an overflow exception
            vnpExc = vnStore->VNPExcSetSingleton(vnStore->VNPairForFunc(TYP_REF, VNF_NewArrOverflowExc, vnp1));
            useEntryPointAddrAsArg0 = true;
        }
        break;

        case VNF_ReadyToRunStaticBase:
        case VNF_ReadyToRunGenericStaticBase:
        case VNF_ReadyToRunIsInstanceOf:
        case VNF_ReadyToRunCastClass:
        {
            useEntryPointAddrAsArg0 = true;
        }
        break;

        default:
        {
            assert(s_helperCallProperties.IsPure(eeGetHelperNum(call->gtCallMethHnd)));
        }
        break;
    }

    if (generateUniqueVN)
    {
        nArgs--;
    }

    ValueNumPair vnpUniq;
    if (generateUniqueVN)
    {
        // Generate unique VN so, VNForFunc generates a unique value number.
        vnpUniq.SetBoth(vnStore->VNForExpr(compCurBB, call->TypeGet()));
    }

    if (nArgs == 0)
    {
        if (generateUniqueVN)
        {
            call->gtVNPair = vnStore->VNPairForFunc(call->TypeGet(), vnf, vnpUniq);
        }
        else
        {
            call->gtVNPair.SetBoth(vnStore->VNForFunc(call->TypeGet(), vnf));
        }
    }
    else
    {
        // Has at least one argument.
        ValueNumPair vnp0;
        ValueNumPair vnp0x = ValueNumStore::VNPForEmptyExcSet();
#ifdef FEATURE_READYTORUN_COMPILER
        if (useEntryPointAddrAsArg0)
        {
            ValueNum callAddrVN = vnStore->VNForPtrSizeIntCon((ssize_t)call->gtCall.gtEntryPoint.addr);
            vnp0                = ValueNumPair(callAddrVN, callAddrVN);
        }
        else
#endif
        {
            assert(!useEntryPointAddrAsArg0);
            ValueNumPair vnp0wx = args->Current()->gtVNPair;
            vnStore->VNPUnpackExc(vnp0wx, &vnp0, &vnp0x);

            // Also include in the argument exception sets
            vnpExc = vnStore->VNPExcSetUnion(vnpExc, vnp0x);

            args = args->Rest();
        }
        if (nArgs == 1)
        {
            if (generateUniqueVN)
            {
                call->gtVNPair = vnStore->VNPairForFunc(call->TypeGet(), vnf, vnp0, vnpUniq);
            }
            else
            {
                call->gtVNPair = vnStore->VNPairForFunc(call->TypeGet(), vnf, vnp0);
            }
        }
        else
        {
            // Has at least two arguments.
            ValueNumPair vnp1wx = args->Current()->gtVNPair;
            ValueNumPair vnp1;
            ValueNumPair vnp1x = ValueNumStore::VNPForEmptyExcSet();
            vnStore->VNPUnpackExc(vnp1wx, &vnp1, &vnp1x);
            vnpExc = vnStore->VNPExcSetUnion(vnpExc, vnp1x);

            args = args->Rest();
            if (nArgs == 2)
            {
                if (generateUniqueVN)
                {
                    call->gtVNPair = vnStore->VNPairForFunc(call->TypeGet(), vnf, vnp0, vnp1, vnpUniq);
                }
                else
                {
                    call->gtVNPair = vnStore->VNPairForFunc(call->TypeGet(), vnf, vnp0, vnp1);
                }
            }
            else
            {
                ValueNumPair vnp2wx = args->Current()->gtVNPair;
                ValueNumPair vnp2;
                ValueNumPair vnp2x = ValueNumStore::VNPForEmptyExcSet();
                vnStore->VNPUnpackExc(vnp2wx, &vnp2, &vnp2x);
                vnpExc = vnStore->VNPExcSetUnion(vnpExc, vnp2x);

                args = args->Rest();
                assert(nArgs == 3); // Our current maximum.
                assert(args == nullptr);
                if (generateUniqueVN)
                {
                    call->gtVNPair = vnStore->VNPairForFunc(call->TypeGet(), vnf, vnp0, vnp1, vnp2, vnpUniq);
                }
                else
                {
                    call->gtVNPair = vnStore->VNPairForFunc(call->TypeGet(), vnf, vnp0, vnp1, vnp2);
                }
            }
        }
        // Add the accumulated exceptions.
        call->gtVNPair = vnStore->VNPWithExc(call->gtVNPair, vnpExc);
    }
}

void Compiler::fgValueNumberCall(GenTreeCall* call)
{
    // First: do value numbering of any argument placeholder nodes in the argument list
    // (by transferring from the VN of the late arg that they are standing in for...)
    unsigned        i               = 0;
    GenTreeArgList* args            = call->gtCallArgs;
    bool            updatedArgPlace = false;
    while (args != nullptr)
    {
        GenTreePtr arg = args->Current();
        if (arg->OperGet() == GT_ARGPLACE)
        {
            // Find the corresponding late arg.
            GenTreePtr lateArg = nullptr;
            for (unsigned j = 0; j < call->fgArgInfo->ArgCount(); j++)
            {
                if (call->fgArgInfo->ArgTable()[j]->argNum == i)
                {
                    lateArg = call->fgArgInfo->ArgTable()[j]->node;
                    break;
                }
            }
            assert(lateArg != nullptr);
            assert(lateArg->gtVNPair.BothDefined());
            arg->gtVNPair   = lateArg->gtVNPair;
            updatedArgPlace = true;
#ifdef DEBUG
            if (verbose)
            {
                printf("VN of ARGPLACE tree ");
                Compiler::printTreeID(arg);
                printf(" updated to ");
                vnpPrint(arg->gtVNPair, 1);
                printf("\n");
            }
#endif
        }
        i++;
        args = args->Rest();
    }
    if (updatedArgPlace)
    {
        // Now we have to update the VN's of the argument list nodes, since that will be used in determining
        // loop-invariance.
        fgUpdateArgListVNs(call->gtCallArgs);
    }

    if (call->gtCallType == CT_HELPER)
    {
        bool modHeap = fgValueNumberHelperCall(call);

        if (modHeap)
        {
            // For now, arbitrary side effect on Heap.
            fgMutateHeap(call DEBUGARG("HELPER - modifies heap"));
        }
    }
    else
    {
        if (call->TypeGet() == TYP_VOID)
        {
            call->gtVNPair.SetBoth(ValueNumStore::VNForVoid());
        }
        else
        {
            call->gtVNPair.SetBoth(vnStore->VNForExpr(compCurBB, call->TypeGet()));
        }

        // For now, arbitrary side effect on Heap.
        fgMutateHeap(call DEBUGARG("CALL"));
    }
}

void Compiler::fgUpdateArgListVNs(GenTreeArgList* args)
{
    if (args == nullptr)
    {
        return;
    }
    // Otherwise...
    fgUpdateArgListVNs(args->Rest());
    fgValueNumberTree(args);
}

VNFunc Compiler::fgValueNumberHelperMethVNFunc(CorInfoHelpFunc helpFunc)
{
    assert(s_helperCallProperties.IsPure(helpFunc) || s_helperCallProperties.IsAllocator(helpFunc));

    VNFunc vnf = VNF_Boundary; // An illegal value...
    switch (helpFunc)
    {
        // These translate to other function symbols:
        case CORINFO_HELP_DIV:
            vnf = VNFunc(GT_DIV);
            break;
        case CORINFO_HELP_MOD:
            vnf = VNFunc(GT_MOD);
            break;
        case CORINFO_HELP_UDIV:
            vnf = VNFunc(GT_UDIV);
            break;
        case CORINFO_HELP_UMOD:
            vnf = VNFunc(GT_UMOD);
            break;
        case CORINFO_HELP_LLSH:
            vnf = VNFunc(GT_LSH);
            break;
        case CORINFO_HELP_LRSH:
            vnf = VNFunc(GT_RSH);
            break;
        case CORINFO_HELP_LRSZ:
            vnf = VNFunc(GT_RSZ);
            break;
        case CORINFO_HELP_LMUL:
        case CORINFO_HELP_LMUL_OVF:
            vnf = VNFunc(GT_MUL);
            break;
        case CORINFO_HELP_ULMUL_OVF:
            vnf = VNFunc(GT_MUL);
            break; // Is this the right thing?
        case CORINFO_HELP_LDIV:
            vnf = VNFunc(GT_DIV);
            break;
        case CORINFO_HELP_LMOD:
            vnf = VNFunc(GT_MOD);
            break;
        case CORINFO_HELP_ULDIV:
            vnf = VNFunc(GT_UDIV);
            break;
        case CORINFO_HELP_ULMOD:
            vnf = VNFunc(GT_UMOD);
            break;

        case CORINFO_HELP_LNG2DBL:
            vnf = VNF_Lng2Dbl;
            break;
        case CORINFO_HELP_ULNG2DBL:
            vnf = VNF_ULng2Dbl;
            break;
        case CORINFO_HELP_DBL2INT:
            vnf = VNF_Dbl2Int;
            break;
        case CORINFO_HELP_DBL2INT_OVF:
            vnf = VNF_Dbl2Int;
            break;
        case CORINFO_HELP_DBL2LNG:
            vnf = VNF_Dbl2Lng;
            break;
        case CORINFO_HELP_DBL2LNG_OVF:
            vnf = VNF_Dbl2Lng;
            break;
        case CORINFO_HELP_DBL2UINT:
            vnf = VNF_Dbl2UInt;
            break;
        case CORINFO_HELP_DBL2UINT_OVF:
            vnf = VNF_Dbl2UInt;
            break;
        case CORINFO_HELP_DBL2ULNG:
            vnf = VNF_Dbl2ULng;
            break;
        case CORINFO_HELP_DBL2ULNG_OVF:
            vnf = VNF_Dbl2ULng;
            break;
        case CORINFO_HELP_FLTREM:
            vnf = VNFunc(GT_MOD);
            break;
        case CORINFO_HELP_DBLREM:
            vnf = VNFunc(GT_MOD);
            break;
        case CORINFO_HELP_FLTROUND:
            vnf = VNF_FltRound;
            break; // Is this the right thing?
        case CORINFO_HELP_DBLROUND:
            vnf = VNF_DblRound;
            break; // Is this the right thing?

        // These allocation operations probably require some augmentation -- perhaps allocSiteId,
        // something about array length...
        case CORINFO_HELP_NEW_CROSSCONTEXT:
        case CORINFO_HELP_NEWFAST:
        case CORINFO_HELP_NEWSFAST:
        case CORINFO_HELP_NEWSFAST_ALIGN8:
            vnf = VNF_JitNew;
            break;

        case CORINFO_HELP_READYTORUN_NEW:
            vnf = VNF_JitReadyToRunNew;
            break;

        case CORINFO_HELP_NEWARR_1_DIRECT:
        case CORINFO_HELP_NEWARR_1_OBJ:
        case CORINFO_HELP_NEWARR_1_VC:
        case CORINFO_HELP_NEWARR_1_ALIGN8:
            vnf = VNF_JitNewArr;
            break;

        case CORINFO_HELP_READYTORUN_NEWARR_1:
            vnf = VNF_JitReadyToRunNewArr;
            break;

        case CORINFO_HELP_GETGENERICS_GCSTATIC_BASE:
            vnf = VNF_GetgenericsGcstaticBase;
            break;
        case CORINFO_HELP_GETGENERICS_NONGCSTATIC_BASE:
            vnf = VNF_GetgenericsNongcstaticBase;
            break;
        case CORINFO_HELP_GETSHARED_GCSTATIC_BASE:
            vnf = VNF_GetsharedGcstaticBase;
            break;
        case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE:
            vnf = VNF_GetsharedNongcstaticBase;
            break;
        case CORINFO_HELP_GETSHARED_GCSTATIC_BASE_NOCTOR:
            vnf = VNF_GetsharedGcstaticBaseNoctor;
            break;
        case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE_NOCTOR:
            vnf = VNF_GetsharedNongcstaticBaseNoctor;
            break;
        case CORINFO_HELP_READYTORUN_STATIC_BASE:
            vnf = VNF_ReadyToRunStaticBase;
            break;
#if COR_JIT_EE_VERSION > 460
        case CORINFO_HELP_READYTORUN_GENERIC_STATIC_BASE:
            vnf = VNF_ReadyToRunGenericStaticBase;
            break;
#endif // COR_JIT_EE_VERSION > 460
        case CORINFO_HELP_GETSHARED_GCSTATIC_BASE_DYNAMICCLASS:
            vnf = VNF_GetsharedGcstaticBaseDynamicclass;
            break;
        case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE_DYNAMICCLASS:
            vnf = VNF_GetsharedNongcstaticBaseDynamicclass;
            break;
        case CORINFO_HELP_CLASSINIT_SHARED_DYNAMICCLASS:
            vnf = VNF_ClassinitSharedDynamicclass;
            break;
        case CORINFO_HELP_GETGENERICS_GCTHREADSTATIC_BASE:
            vnf = VNF_GetgenericsGcthreadstaticBase;
            break;
        case CORINFO_HELP_GETGENERICS_NONGCTHREADSTATIC_BASE:
            vnf = VNF_GetgenericsNongcthreadstaticBase;
            break;
        case CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE:
            vnf = VNF_GetsharedGcthreadstaticBase;
            break;
        case CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE:
            vnf = VNF_GetsharedNongcthreadstaticBase;
            break;
        case CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_NOCTOR:
            vnf = VNF_GetsharedGcthreadstaticBaseNoctor;
            break;
        case CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_NOCTOR:
            vnf = VNF_GetsharedNongcthreadstaticBaseNoctor;
            break;
        case CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_DYNAMICCLASS:
            vnf = VNF_GetsharedGcthreadstaticBaseDynamicclass;
            break;
        case CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_DYNAMICCLASS:
            vnf = VNF_GetsharedNongcthreadstaticBaseDynamicclass;
            break;
        case CORINFO_HELP_GETSTATICFIELDADDR_CONTEXT:
            vnf = VNF_GetStaticAddrContext;
            break;
        case CORINFO_HELP_GETSTATICFIELDADDR_TLS:
            vnf = VNF_GetStaticAddrTLS;
            break;

        case CORINFO_HELP_RUNTIMEHANDLE_METHOD:
        case CORINFO_HELP_RUNTIMEHANDLE_METHOD_LOG:
            vnf = VNF_RuntimeHandleMethod;
            break;

        case CORINFO_HELP_RUNTIMEHANDLE_CLASS:
        case CORINFO_HELP_RUNTIMEHANDLE_CLASS_LOG:
            vnf = VNF_RuntimeHandleClass;
            break;

        case CORINFO_HELP_STRCNS:
            vnf = VNF_StrCns;
            break;

        case CORINFO_HELP_CHKCASTCLASS:
        case CORINFO_HELP_CHKCASTCLASS_SPECIAL:
        case CORINFO_HELP_CHKCASTARRAY:
        case CORINFO_HELP_CHKCASTINTERFACE:
        case CORINFO_HELP_CHKCASTANY:
            vnf = VNF_CastClass;
            break;

        case CORINFO_HELP_READYTORUN_CHKCAST:
            vnf = VNF_ReadyToRunCastClass;
            break;

        case CORINFO_HELP_ISINSTANCEOFCLASS:
        case CORINFO_HELP_ISINSTANCEOFINTERFACE:
        case CORINFO_HELP_ISINSTANCEOFARRAY:
        case CORINFO_HELP_ISINSTANCEOFANY:
            vnf = VNF_IsInstanceOf;
            break;

        case CORINFO_HELP_READYTORUN_ISINSTANCEOF:
            vnf = VNF_ReadyToRunIsInstanceOf;
            break;

        case CORINFO_HELP_LDELEMA_REF:
            vnf = VNF_LdElemA;
            break;

        case CORINFO_HELP_UNBOX:
            vnf = VNF_Unbox;
            break;

        // A constant within any method.
        case CORINFO_HELP_GETCURRENTMANAGEDTHREADID:
            vnf = VNF_ManagedThreadId;
            break;

        case CORINFO_HELP_GETREFANY:
            // TODO-CQ: This should really be interpreted as just a struct field reference, in terms of values.
            vnf = VNF_GetRefanyVal;
            break;

        case CORINFO_HELP_GETCLASSFROMMETHODPARAM:
            vnf = VNF_GetClassFromMethodParam;
            break;

        case CORINFO_HELP_GETSYNCFROMCLASSHANDLE:
            vnf = VNF_GetSyncFromClassHandle;
            break;

        case CORINFO_HELP_LOOP_CLONE_CHOICE_ADDR:
            vnf = VNF_LoopCloneChoiceAddr;
            break;

        case CORINFO_HELP_BOX_NULLABLE:
            vnf = VNF_BoxNullable;
            break;

        default:
            unreached();
    }

    assert(vnf != VNF_Boundary);
    return vnf;
}

bool Compiler::fgValueNumberHelperCall(GenTreeCall* call)
{
    CorInfoHelpFunc helpFunc    = eeGetHelperNum(call->gtCallMethHnd);
    bool            pure        = s_helperCallProperties.IsPure(helpFunc);
    bool            isAlloc     = s_helperCallProperties.IsAllocator(helpFunc);
    bool            modHeap     = s_helperCallProperties.MutatesHeap(helpFunc);
    bool            mayRunCctor = s_helperCallProperties.MayRunCctor(helpFunc);
    bool            noThrow     = s_helperCallProperties.NoThrow(helpFunc);

    ValueNumPair vnpExc = ValueNumStore::VNPForEmptyExcSet();

    // If the JIT helper can throw an exception make sure that we fill in
    // vnpExc with a Value Number that represents the exception(s) that can be thrown.
    if (!noThrow)
    {
        // If the helper is known to only throw only one particular exception
        // we can set vnpExc to that exception, otherwise we conservatively
        // model the JIT helper as possibly throwing multiple different exceptions
        //
        switch (helpFunc)
        {
            case CORINFO_HELP_OVERFLOW:
                // This helper always throws the VNF_OverflowExc exception
                vnpExc = vnStore->VNPExcSetSingleton(vnStore->VNPairForFunc(TYP_REF, VNF_OverflowExc));
                break;

            default:
                // Setup vnpExc with the information that multiple different exceptions
                // could be generated by this helper
                vnpExc = vnStore->VNPExcSetSingleton(vnStore->VNPairForFunc(TYP_REF, VNF_HelperMultipleExc));
        }
    }

    ValueNumPair vnpNorm;

    if (call->TypeGet() == TYP_VOID)
    {
        vnpNorm = ValueNumStore::VNPForVoid();
    }
    else
    {
        // TODO-CQ: this is a list of helpers we're going to treat as non-pure,
        // because they raise complications.  Eventually, we need to handle those complications...
        bool needsFurtherWork = false;
        switch (helpFunc)
        {
            case CORINFO_HELP_NEW_MDARR:
                // This is a varargs helper.  We need to represent the array shape in the VN world somehow.
                needsFurtherWork = true;
                break;
            default:
                break;
        }

        if (!needsFurtherWork && (pure || isAlloc))
        {
            VNFunc vnf = fgValueNumberHelperMethVNFunc(helpFunc);

            if (mayRunCctor)
            {
                if ((call->gtFlags & GTF_CALL_HOISTABLE) == 0)
                {
                    modHeap = true;
                }
            }

            fgValueNumberHelperCallFunc(call, vnf, vnpExc);
            return modHeap;
        }
        else
        {
            vnpNorm.SetBoth(vnStore->VNForExpr(compCurBB, call->TypeGet()));
        }
    }

    call->gtVNPair = vnStore->VNPWithExc(vnpNorm, vnpExc);
    return modHeap;
}

#ifdef DEBUG
// This method asserts that SSA name constraints specified are satisfied.
// Until we figure out otherwise, all VN's are assumed to be liberal.
// TODO-Cleanup: new JitTestLabels for lib vs cons vs both VN classes?
void Compiler::JitTestCheckVN()
{
    typedef SimplerHashTable<ssize_t, SmallPrimitiveKeyFuncs<ssize_t>, ValueNum, JitSimplerHashBehavior>  LabelToVNMap;
    typedef SimplerHashTable<ValueNum, SmallPrimitiveKeyFuncs<ValueNum>, ssize_t, JitSimplerHashBehavior> VNToLabelMap;

    // If we have no test data, early out.
    if (m_nodeTestData == nullptr)
    {
        return;
    }

    NodeToTestDataMap* testData = GetNodeTestData();

    // First we have to know which nodes in the tree are reachable.
    typedef SimplerHashTable<GenTreePtr, PtrKeyFuncs<GenTree>, int, JitSimplerHashBehavior> NodeToIntMap;
    NodeToIntMap* reachable = FindReachableNodesInNodeTestData();

    LabelToVNMap* labelToVN = new (getAllocatorDebugOnly()) LabelToVNMap(getAllocatorDebugOnly());
    VNToLabelMap* vnToLabel = new (getAllocatorDebugOnly()) VNToLabelMap(getAllocatorDebugOnly());

    if (verbose)
    {
        printf("\nJit Testing: Value numbering.\n");
    }
    for (NodeToTestDataMap::KeyIterator ki = testData->Begin(); !ki.Equal(testData->End()); ++ki)
    {
        TestLabelAndNum tlAndN;
        GenTreePtr      node   = ki.Get();
        ValueNum        nodeVN = node->GetVN(VNK_Liberal);

        bool b = testData->Lookup(node, &tlAndN);
        assert(b);
        if (tlAndN.m_tl == TL_VN || tlAndN.m_tl == TL_VNNorm)
        {
            int dummy;
            if (!reachable->Lookup(node, &dummy))
            {
                printf("Node ");
                Compiler::printTreeID(node);
                printf(" had a test constraint declared, but has become unreachable at the time the constraint is "
                       "tested.\n"
                       "(This is probably as a result of some optimization -- \n"
                       "you may need to modify the test case to defeat this opt.)\n");
                assert(false);
            }

            if (verbose)
            {
                printf("  Node ");
                Compiler::printTreeID(node);
                printf(" -- VN class %d.\n", tlAndN.m_num);
            }

            if (tlAndN.m_tl == TL_VNNorm)
            {
                nodeVN = vnStore->VNNormVal(nodeVN);
            }

            ValueNum vn;
            if (labelToVN->Lookup(tlAndN.m_num, &vn))
            {
                if (verbose)
                {
                    printf("      Already in hash tables.\n");
                }
                // The mapping(s) must be one-to-one: if the label has a mapping, then the ssaNm must, as well.
                ssize_t num2;
                bool    b = vnToLabel->Lookup(vn, &num2);
                // And the mappings must be the same.
                if (tlAndN.m_num != num2)
                {
                    printf("Node: ");
                    Compiler::printTreeID(node);
                    printf(", with value number " STR_VN "%x, was declared in VN class %d,\n", nodeVN, tlAndN.m_num);
                    printf("but this value number " STR_VN
                           "%x has already been associated with a different SSA name class: %d.\n",
                           vn, num2);
                    assert(false);
                }
                // And the current node must be of the specified SSA family.
                if (nodeVN != vn)
                {
                    printf("Node: ");
                    Compiler::printTreeID(node);
                    printf(", " STR_VN "%x was declared in SSA name class %d,\n", nodeVN, tlAndN.m_num);
                    printf("but that name class was previously bound to a different value number: " STR_VN "%x.\n", vn);
                    assert(false);
                }
            }
            else
            {
                ssize_t num;
                // The mapping(s) must be one-to-one: if the label has no mapping, then the ssaNm may not, either.
                if (vnToLabel->Lookup(nodeVN, &num))
                {
                    printf("Node: ");
                    Compiler::printTreeID(node);
                    printf(", " STR_VN "%x was declared in value number class %d,\n", nodeVN, tlAndN.m_num);
                    printf(
                        "but this value number has already been associated with a different value number class: %d.\n",
                        num);
                    assert(false);
                }
                // Add to both mappings.
                labelToVN->Set(tlAndN.m_num, nodeVN);
                vnToLabel->Set(nodeVN, tlAndN.m_num);
                if (verbose)
                {
                    printf("      added to hash tables.\n");
                }
            }
        }
    }
}

void Compiler::vnpPrint(ValueNumPair vnp, unsigned level)
{
    if (vnp.BothEqual())
    {
        vnPrint(vnp.GetLiberal(), level);
    }
    else
    {
        printf("<l:");
        vnPrint(vnp.GetLiberal(), level);
        printf(", c:");
        vnPrint(vnp.GetConservative(), level);
        printf(">");
    }
}

void Compiler::vnPrint(ValueNum vn, unsigned level)
{

    if (ValueNumStore::isReservedVN(vn))
    {
        printf(ValueNumStore::reservedName(vn));
    }
    else
    {
        printf(STR_VN "%x", vn);
        if (level > 0)
        {
            vnStore->vnDump(this, vn);
        }
    }
}

#endif // DEBUG

// Methods of ValueNumPair.
ValueNumPair::ValueNumPair() : m_liberal(ValueNumStore::NoVN), m_conservative(ValueNumStore::NoVN)
{
}

bool ValueNumPair::BothDefined() const
{
    return (m_liberal != ValueNumStore::NoVN) && (m_conservative != ValueNumStore::NoVN);
}