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
|
#include "Python.h"
#include "opcode.h"
#include "pycore_c_array.h" // _Py_CArray_EnsureCapacity
#include "pycore_flowgraph.h"
#include "pycore_compile.h"
#include "pycore_intrinsics.h"
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
#include "pycore_long.h" // _PY_IS_SMALL_INT()
#include "pycore_opcode_utils.h"
#include "pycore_opcode_metadata.h" // OPCODE_HAS_ARG, etc
#include <stdbool.h>
#undef SUCCESS
#undef ERROR
#define SUCCESS 0
#define ERROR -1
#define RETURN_IF_ERROR(X) \
if ((X) == -1) { \
return ERROR; \
}
#define DEFAULT_BLOCK_SIZE 16
typedef _Py_SourceLocation location;
typedef _PyJumpTargetLabel jump_target_label;
typedef struct _PyCfgInstruction {
int i_opcode;
int i_oparg;
_Py_SourceLocation i_loc;
struct _PyCfgBasicblock *i_target; /* target block (if jump instruction) */
struct _PyCfgBasicblock *i_except; /* target block when exception is raised */
} cfg_instr;
typedef struct _PyCfgBasicblock {
/* Each basicblock in a compilation unit is linked via b_list in the
reverse order that the block are allocated. b_list points to the next
block in this list, not to be confused with b_next, which is next by
control flow. */
struct _PyCfgBasicblock *b_list;
/* The label of this block if it is a jump target, -1 otherwise */
_PyJumpTargetLabel b_label;
/* Exception stack at start of block, used by assembler to create the exception handling table */
struct _PyCfgExceptStack *b_exceptstack;
/* pointer to an array of instructions, initially NULL */
cfg_instr *b_instr;
/* If b_next is non-NULL, it is a pointer to the next
block reached by normal control flow. */
struct _PyCfgBasicblock *b_next;
/* number of instructions used */
int b_iused;
/* length of instruction array (b_instr) */
int b_ialloc;
/* Used by add_checks_for_loads_of_unknown_variables */
uint64_t b_unsafe_locals_mask;
/* Number of predecessors that a block has. */
int b_predecessors;
/* depth of stack upon entry of block, computed by stackdepth() */
int b_startdepth;
/* Basic block is an exception handler that preserves lasti */
unsigned b_preserve_lasti : 1;
/* Used by compiler passes to mark whether they have visited a basic block. */
unsigned b_visited : 1;
/* b_except_handler is used by the cold-detection algorithm to mark exception targets */
unsigned b_except_handler : 1;
/* b_cold is true if this block is not perf critical (like an exception handler) */
unsigned b_cold : 1;
/* b_warm is used by the cold-detection algorithm to mark blocks which are definitely not cold */
unsigned b_warm : 1;
} basicblock;
struct _PyCfgBuilder {
/* The entryblock, at which control flow begins. All blocks of the
CFG are reachable through the b_next links */
struct _PyCfgBasicblock *g_entryblock;
/* Pointer to the most recently allocated block. By following
b_list links, you can reach all allocated blocks. */
struct _PyCfgBasicblock *g_block_list;
/* pointer to the block currently being constructed */
struct _PyCfgBasicblock *g_curblock;
/* label for the next instruction to be placed */
_PyJumpTargetLabel g_current_label;
};
typedef struct _PyCfgBuilder cfg_builder;
#define SAME_LABEL(L1, L2) ((L1).id == (L2).id)
#define IS_LABEL(L) (!SAME_LABEL((L), (NO_LABEL)))
#define LOCATION(LNO, END_LNO, COL, END_COL) \
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
static inline int
is_block_push(cfg_instr *i)
{
assert(OPCODE_HAS_ARG(i->i_opcode) || !IS_BLOCK_PUSH_OPCODE(i->i_opcode));
return IS_BLOCK_PUSH_OPCODE(i->i_opcode);
}
static inline int
is_jump(cfg_instr *i)
{
return OPCODE_HAS_JUMP(i->i_opcode);
}
/* One arg*/
#define INSTR_SET_OP1(I, OP, ARG) \
do { \
assert(OPCODE_HAS_ARG(OP)); \
cfg_instr *_instr__ptr_ = (I); \
_instr__ptr_->i_opcode = (OP); \
_instr__ptr_->i_oparg = (ARG); \
} while (0);
/* No args*/
#define INSTR_SET_OP0(I, OP) \
do { \
assert(!OPCODE_HAS_ARG(OP)); \
cfg_instr *_instr__ptr_ = (I); \
_instr__ptr_->i_opcode = (OP); \
_instr__ptr_->i_oparg = 0; \
} while (0);
#define INSTR_SET_LOC(I, LOC) \
do { \
cfg_instr *_instr__ptr_ = (I); \
_instr__ptr_->i_loc = (LOC); \
} while (0);
/***** Blocks *****/
/* Returns the offset of the next instruction in the current block's
b_instr array. Resizes the b_instr as necessary.
Returns -1 on failure.
*/
static int
basicblock_next_instr(basicblock *b)
{
assert(b != NULL);
_Py_c_array_t array = {
.array = (void*)b->b_instr,
.allocated_entries = b->b_ialloc,
.item_size = sizeof(cfg_instr),
.initial_num_entries = DEFAULT_BLOCK_SIZE,
};
RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, b->b_iused + 1));
b->b_instr = array.array;
b->b_ialloc = array.allocated_entries;
return b->b_iused++;
}
static cfg_instr *
basicblock_last_instr(const basicblock *b) {
assert(b->b_iused >= 0);
if (b->b_iused > 0) {
assert(b->b_instr != NULL);
return &b->b_instr[b->b_iused - 1];
}
return NULL;
}
/* Allocate a new block and return a pointer to it.
Returns NULL on error.
*/
static basicblock *
cfg_builder_new_block(cfg_builder *g)
{
basicblock *b = (basicblock *)PyMem_Calloc(1, sizeof(basicblock));
if (b == NULL) {
PyErr_NoMemory();
return NULL;
}
/* Extend the singly linked list of blocks with new block. */
b->b_list = g->g_block_list;
g->g_block_list = b;
b->b_label = NO_LABEL;
return b;
}
static int
basicblock_addop(basicblock *b, int opcode, int oparg, location loc)
{
assert(IS_WITHIN_OPCODE_RANGE(opcode));
assert(!IS_ASSEMBLER_OPCODE(opcode));
assert(OPCODE_HAS_ARG(opcode) || HAS_TARGET(opcode) || oparg == 0);
assert(0 <= oparg && oparg < (1 << 30));
int off = basicblock_next_instr(b);
if (off < 0) {
return ERROR;
}
cfg_instr *i = &b->b_instr[off];
i->i_opcode = opcode;
i->i_oparg = oparg;
i->i_target = NULL;
i->i_loc = loc;
return SUCCESS;
}
static int
basicblock_add_jump(basicblock *b, int opcode, basicblock *target, location loc)
{
cfg_instr *last = basicblock_last_instr(b);
if (last && is_jump(last)) {
return ERROR;
}
RETURN_IF_ERROR(
basicblock_addop(b, opcode, target->b_label.id, loc));
last = basicblock_last_instr(b);
assert(last && last->i_opcode == opcode);
last->i_target = target;
return SUCCESS;
}
static inline int
basicblock_append_instructions(basicblock *to, basicblock *from)
{
for (int i = 0; i < from->b_iused; i++) {
int n = basicblock_next_instr(to);
if (n < 0) {
return ERROR;
}
to->b_instr[n] = from->b_instr[i];
}
return SUCCESS;
}
static inline int
basicblock_nofallthrough(const basicblock *b) {
cfg_instr *last = basicblock_last_instr(b);
return (last &&
(IS_SCOPE_EXIT_OPCODE(last->i_opcode) ||
IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)));
}
#define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
#define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))
static basicblock *
copy_basicblock(cfg_builder *g, basicblock *block)
{
/* Cannot copy a block if it has a fallthrough, since
* a block can only have one fallthrough predecessor.
*/
assert(BB_NO_FALLTHROUGH(block));
basicblock *result = cfg_builder_new_block(g);
if (result == NULL) {
return NULL;
}
if (basicblock_append_instructions(result, block) < 0) {
return NULL;
}
return result;
}
static int
basicblock_insert_instruction(basicblock *block, int pos, cfg_instr *instr) {
RETURN_IF_ERROR(basicblock_next_instr(block));
for (int i = block->b_iused - 1; i > pos; i--) {
block->b_instr[i] = block->b_instr[i-1];
}
block->b_instr[pos] = *instr;
return SUCCESS;
}
/* For debugging purposes only */
#if 0
static void
dump_instr(cfg_instr *i)
{
const char *jump = is_jump(i) ? "jump " : "";
char arg[128];
*arg = '\0';
if (OPCODE_HAS_ARG(i->i_opcode)) {
sprintf(arg, "arg: %d ", i->i_oparg);
}
if (HAS_TARGET(i->i_opcode)) {
sprintf(arg, "target: %p [%d] ", i->i_target, i->i_oparg);
}
fprintf(stderr, "line: %d, %s (%d) %s%s\n",
i->i_loc.lineno, _PyOpcode_OpName[i->i_opcode], i->i_opcode, arg, jump);
}
static inline int
basicblock_returns(const basicblock *b) {
cfg_instr *last = basicblock_last_instr(b);
return last && IS_RETURN_OPCODE(last->i_opcode);
}
static void
dump_basicblock(const basicblock *b)
{
const char *b_return = basicblock_returns(b) ? "return " : "";
fprintf(stderr, "%d: [EH=%d CLD=%d WRM=%d NO_FT=%d %p] used: %d, depth: %d, preds: %d %s\n",
b->b_label.id, b->b_except_handler, b->b_cold, b->b_warm, BB_NO_FALLTHROUGH(b), b, b->b_iused,
b->b_startdepth, b->b_predecessors, b_return);
if (b->b_instr) {
int i;
for (i = 0; i < b->b_iused; i++) {
fprintf(stderr, " [%02d] ", i);
dump_instr(b->b_instr + i);
}
}
}
void
_PyCfgBuilder_DumpGraph(const basicblock *entryblock)
{
for (const basicblock *b = entryblock; b != NULL; b = b->b_next) {
dump_basicblock(b);
}
}
#endif
/***** CFG construction and modification *****/
static basicblock *
cfg_builder_use_next_block(cfg_builder *g, basicblock *block)
{
assert(block != NULL);
g->g_curblock->b_next = block;
g->g_curblock = block;
return block;
}
static inline int
basicblock_exits_scope(const basicblock *b) {
cfg_instr *last = basicblock_last_instr(b);
return last && IS_SCOPE_EXIT_OPCODE(last->i_opcode);
}
static inline int
basicblock_has_eval_break(const basicblock *b) {
for (int i = 0; i < b->b_iused; i++) {
if (OPCODE_HAS_EVAL_BREAK(b->b_instr[i].i_opcode)) {
return true;
}
}
return false;
}
static bool
cfg_builder_current_block_is_terminated(cfg_builder *g)
{
cfg_instr *last = basicblock_last_instr(g->g_curblock);
if (last && IS_TERMINATOR_OPCODE(last->i_opcode)) {
return true;
}
if (IS_LABEL(g->g_current_label)) {
if (last || IS_LABEL(g->g_curblock->b_label)) {
return true;
}
else {
/* current block is empty, label it */
g->g_curblock->b_label = g->g_current_label;
g->g_current_label = NO_LABEL;
}
}
return false;
}
static int
cfg_builder_maybe_start_new_block(cfg_builder *g)
{
if (cfg_builder_current_block_is_terminated(g)) {
basicblock *b = cfg_builder_new_block(g);
if (b == NULL) {
return ERROR;
}
b->b_label = g->g_current_label;
g->g_current_label = NO_LABEL;
cfg_builder_use_next_block(g, b);
}
return SUCCESS;
}
#ifndef NDEBUG
static bool
cfg_builder_check(cfg_builder *g)
{
assert(g->g_entryblock->b_iused > 0);
for (basicblock *block = g->g_block_list; block != NULL; block = block->b_list) {
assert(!_PyMem_IsPtrFreed(block));
if (block->b_instr != NULL) {
assert(block->b_ialloc > 0);
assert(block->b_iused >= 0);
assert(block->b_ialloc >= block->b_iused);
}
else {
assert (block->b_iused == 0);
assert (block->b_ialloc == 0);
}
}
return true;
}
#endif
static int
init_cfg_builder(cfg_builder *g)
{
g->g_block_list = NULL;
basicblock *block = cfg_builder_new_block(g);
if (block == NULL) {
return ERROR;
}
g->g_curblock = g->g_entryblock = block;
g->g_current_label = NO_LABEL;
return SUCCESS;
}
cfg_builder *
_PyCfgBuilder_New(void)
{
cfg_builder *g = PyMem_Malloc(sizeof(cfg_builder));
if (g == NULL) {
PyErr_NoMemory();
return NULL;
}
memset(g, 0, sizeof(cfg_builder));
if (init_cfg_builder(g) < 0) {
PyMem_Free(g);
return NULL;
}
return g;
}
void
_PyCfgBuilder_Free(cfg_builder *g)
{
if (g == NULL) {
return;
}
assert(cfg_builder_check(g));
basicblock *b = g->g_block_list;
while (b != NULL) {
if (b->b_instr) {
PyMem_Free((void *)b->b_instr);
}
basicblock *next = b->b_list;
PyMem_Free((void *)b);
b = next;
}
PyMem_Free(g);
}
int
_PyCfgBuilder_CheckSize(cfg_builder *g)
{
int nblocks = 0;
for (basicblock *b = g->g_block_list; b != NULL; b = b->b_list) {
nblocks++;
}
if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
PyErr_NoMemory();
return ERROR;
}
return SUCCESS;
}
int
_PyCfgBuilder_UseLabel(cfg_builder *g, jump_target_label lbl)
{
g->g_current_label = lbl;
return cfg_builder_maybe_start_new_block(g);
}
int
_PyCfgBuilder_Addop(cfg_builder *g, int opcode, int oparg, location loc)
{
RETURN_IF_ERROR(cfg_builder_maybe_start_new_block(g));
return basicblock_addop(g->g_curblock, opcode, oparg, loc);
}
static basicblock *
next_nonempty_block(basicblock *b)
{
while (b && b->b_iused == 0) {
b = b->b_next;
}
return b;
}
/***** debugging helpers *****/
#ifndef NDEBUG
static int remove_redundant_nops(cfg_builder *g);
static bool
no_redundant_nops(cfg_builder *g) {
if (remove_redundant_nops(g) != 0) {
return false;
}
return true;
}
static bool
no_redundant_jumps(cfg_builder *g) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
cfg_instr *last = basicblock_last_instr(b);
if (last != NULL) {
if (IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
basicblock *next = next_nonempty_block(b->b_next);
basicblock *jump_target = next_nonempty_block(last->i_target);
if (jump_target == next) {
assert(next);
if (last->i_loc.lineno == next->b_instr[0].i_loc.lineno) {
assert(0);
return false;
}
}
}
}
}
return true;
}
#endif
/***** CFG preprocessing (jump targets and exceptions) *****/
static int
normalize_jumps_in_block(cfg_builder *g, basicblock *b) {
cfg_instr *last = basicblock_last_instr(b);
if (last == NULL || !IS_CONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
return SUCCESS;
}
assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
bool is_forward = last->i_target->b_visited == 0;
if (is_forward) {
RETURN_IF_ERROR(
basicblock_addop(b, NOT_TAKEN, 0, last->i_loc));
return SUCCESS;
}
int reversed_opcode = 0;
switch(last->i_opcode) {
case POP_JUMP_IF_NOT_NONE:
reversed_opcode = POP_JUMP_IF_NONE;
break;
case POP_JUMP_IF_NONE:
reversed_opcode = POP_JUMP_IF_NOT_NONE;
break;
case POP_JUMP_IF_FALSE:
reversed_opcode = POP_JUMP_IF_TRUE;
break;
case POP_JUMP_IF_TRUE:
reversed_opcode = POP_JUMP_IF_FALSE;
break;
}
/* transform 'conditional jump T' to
* 'reversed_jump b_next' followed by 'jump_backwards T'
*/
basicblock *target = last->i_target;
basicblock *backwards_jump = cfg_builder_new_block(g);
if (backwards_jump == NULL) {
return ERROR;
}
RETURN_IF_ERROR(
basicblock_addop(backwards_jump, NOT_TAKEN, 0, last->i_loc));
RETURN_IF_ERROR(
basicblock_add_jump(backwards_jump, JUMP, target, last->i_loc));
backwards_jump->b_startdepth = target->b_startdepth;
last->i_opcode = reversed_opcode;
last->i_target = b->b_next;
backwards_jump->b_cold = b->b_cold;
backwards_jump->b_next = b->b_next;
b->b_next = backwards_jump;
return SUCCESS;
}
static int
normalize_jumps(cfg_builder *g)
{
basicblock *entryblock = g->g_entryblock;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
b->b_visited = 0;
}
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
b->b_visited = 1;
RETURN_IF_ERROR(normalize_jumps_in_block(g, b));
}
return SUCCESS;
}
static int
check_cfg(cfg_builder *g) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
/* Raise SystemError if jump or exit is not last instruction in the block. */
for (int i = 0; i < b->b_iused; i++) {
int opcode = b->b_instr[i].i_opcode;
assert(!IS_ASSEMBLER_OPCODE(opcode));
if (IS_TERMINATOR_OPCODE(opcode)) {
if (i != b->b_iused - 1) {
PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
return ERROR;
}
}
}
}
return SUCCESS;
}
static int
get_max_label(basicblock *entryblock)
{
int lbl = -1;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
if (b->b_label.id > lbl) {
lbl = b->b_label.id;
}
}
return lbl;
}
/* Calculate the actual jump target from the target_label */
static int
translate_jump_labels_to_targets(basicblock *entryblock)
{
int max_label = get_max_label(entryblock);
size_t mapsize = sizeof(basicblock *) * (max_label + 1);
basicblock **label2block = (basicblock **)PyMem_Malloc(mapsize);
if (!label2block) {
PyErr_NoMemory();
return ERROR;
}
memset(label2block, 0, mapsize);
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
if (b->b_label.id >= 0) {
label2block[b->b_label.id] = b;
}
}
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
assert(instr->i_target == NULL);
if (HAS_TARGET(instr->i_opcode)) {
int lbl = instr->i_oparg;
assert(lbl >= 0 && lbl <= max_label);
instr->i_target = label2block[lbl];
assert(instr->i_target != NULL);
assert(instr->i_target->b_label.id == lbl);
}
}
}
PyMem_Free(label2block);
return SUCCESS;
}
static int
mark_except_handlers(basicblock *entryblock) {
#ifndef NDEBUG
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
assert(!b->b_except_handler);
}
#endif
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i=0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (is_block_push(instr)) {
instr->i_target->b_except_handler = 1;
}
}
}
return SUCCESS;
}
struct _PyCfgExceptStack {
basicblock *handlers[CO_MAXBLOCKS+2];
int depth;
};
static basicblock *
push_except_block(struct _PyCfgExceptStack *stack, cfg_instr *setup) {
assert(is_block_push(setup));
int opcode = setup->i_opcode;
basicblock * target = setup->i_target;
if (opcode == SETUP_WITH || opcode == SETUP_CLEANUP) {
target->b_preserve_lasti = 1;
}
assert(stack->depth <= CO_MAXBLOCKS);
stack->handlers[++stack->depth] = target;
return target;
}
static basicblock *
pop_except_block(struct _PyCfgExceptStack *stack) {
assert(stack->depth > 0);
return stack->handlers[--stack->depth];
}
static basicblock *
except_stack_top(struct _PyCfgExceptStack *stack) {
return stack->handlers[stack->depth];
}
static struct _PyCfgExceptStack *
make_except_stack(void) {
struct _PyCfgExceptStack *new = PyMem_Malloc(sizeof(struct _PyCfgExceptStack));
if (new == NULL) {
PyErr_NoMemory();
return NULL;
}
new->depth = 0;
new->handlers[0] = NULL;
return new;
}
static struct _PyCfgExceptStack *
copy_except_stack(struct _PyCfgExceptStack *stack) {
struct _PyCfgExceptStack *copy = PyMem_Malloc(sizeof(struct _PyCfgExceptStack));
if (copy == NULL) {
PyErr_NoMemory();
return NULL;
}
memcpy(copy, stack, sizeof(struct _PyCfgExceptStack));
return copy;
}
static basicblock**
make_cfg_traversal_stack(basicblock *entryblock) {
int nblocks = 0;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
b->b_visited = 0;
nblocks++;
}
basicblock **stack = (basicblock **)PyMem_Malloc(sizeof(basicblock *) * nblocks);
if (!stack) {
PyErr_NoMemory();
}
return stack;
}
/* Compute the stack effects of opcode with argument oparg.
Some opcodes have different stack effect when jump to the target and
when not jump. The 'jump' parameter specifies the case:
* 0 -- when not jump
* 1 -- when jump
* -1 -- maximal
*/
typedef struct {
/* The stack effect of the instruction. */
int net;
} stack_effects;
Py_LOCAL(int)
get_stack_effects(int opcode, int oparg, int jump, stack_effects *effects)
{
if (opcode < 0) {
return -1;
}
if ((opcode <= MAX_REAL_OPCODE) && (_PyOpcode_Deopt[opcode] != opcode)) {
// Specialized instructions are not supported.
return -1;
}
int popped = _PyOpcode_num_popped(opcode, oparg);
int pushed = _PyOpcode_num_pushed(opcode, oparg);
if (popped < 0 || pushed < 0) {
return -1;
}
if (IS_BLOCK_PUSH_OPCODE(opcode) && !jump) {
effects->net = 0;
return 0;
}
effects->net = pushed - popped;
return 0;
}
Py_LOCAL_INLINE(int)
stackdepth_push(basicblock ***sp, basicblock *b, int depth)
{
if (!(b->b_startdepth < 0 || b->b_startdepth == depth)) {
PyErr_Format(PyExc_ValueError, "Invalid CFG, inconsistent stackdepth");
return ERROR;
}
if (b->b_startdepth < depth && b->b_startdepth < 100) {
assert(b->b_startdepth < 0);
b->b_startdepth = depth;
*(*sp)++ = b;
}
return SUCCESS;
}
/* Find the flow path that needs the largest stack. We assume that
* cycles in the flow graph have no net effect on the stack depth.
*/
static int
calculate_stackdepth(cfg_builder *g)
{
basicblock *entryblock = g->g_entryblock;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
b->b_startdepth = INT_MIN;
}
basicblock **stack = make_cfg_traversal_stack(entryblock);
if (!stack) {
return ERROR;
}
int stackdepth = -1;
int maxdepth = 0;
basicblock **sp = stack;
if (stackdepth_push(&sp, entryblock, 0) < 0) {
goto error;
}
while (sp != stack) {
basicblock *b = *--sp;
int depth = b->b_startdepth;
assert(depth >= 0);
basicblock *next = b->b_next;
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
stack_effects effects;
if (get_stack_effects(instr->i_opcode, instr->i_oparg, 0, &effects) < 0) {
PyErr_Format(PyExc_SystemError,
"Invalid stack effect for opcode=%d, arg=%i",
instr->i_opcode, instr->i_oparg);
goto error;
}
int new_depth = depth + effects.net;
if (new_depth < 0) {
PyErr_Format(PyExc_ValueError,
"Invalid CFG, stack underflow");
goto error;
}
maxdepth = Py_MAX(maxdepth, depth);
if (HAS_TARGET(instr->i_opcode) && instr->i_opcode != END_ASYNC_FOR) {
if (get_stack_effects(instr->i_opcode, instr->i_oparg, 1, &effects) < 0) {
PyErr_Format(PyExc_SystemError,
"Invalid stack effect for opcode=%d, arg=%i",
instr->i_opcode, instr->i_oparg);
goto error;
}
int target_depth = depth + effects.net;
assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
maxdepth = Py_MAX(maxdepth, depth);
if (stackdepth_push(&sp, instr->i_target, target_depth) < 0) {
goto error;
}
}
depth = new_depth;
assert(!IS_ASSEMBLER_OPCODE(instr->i_opcode));
if (IS_UNCONDITIONAL_JUMP_OPCODE(instr->i_opcode) ||
IS_SCOPE_EXIT_OPCODE(instr->i_opcode))
{
/* remaining code is dead */
next = NULL;
break;
}
}
if (next != NULL) {
assert(BB_HAS_FALLTHROUGH(b));
if (stackdepth_push(&sp, next, depth) < 0) {
goto error;
}
}
}
stackdepth = maxdepth;
error:
PyMem_Free(stack);
return stackdepth;
}
static int
label_exception_targets(basicblock *entryblock) {
basicblock **todo_stack = make_cfg_traversal_stack(entryblock);
if (todo_stack == NULL) {
return ERROR;
}
struct _PyCfgExceptStack *except_stack = make_except_stack();
if (except_stack == NULL) {
PyMem_Free(todo_stack);
PyErr_NoMemory();
return ERROR;
}
except_stack->depth = 0;
todo_stack[0] = entryblock;
entryblock->b_visited = 1;
entryblock->b_exceptstack = except_stack;
basicblock **todo = &todo_stack[1];
basicblock *handler = NULL;
while (todo > todo_stack) {
todo--;
basicblock *b = todo[0];
assert(b->b_visited == 1);
except_stack = b->b_exceptstack;
assert(except_stack != NULL);
b->b_exceptstack = NULL;
handler = except_stack_top(except_stack);
int last_yield_except_depth = -1;
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (is_block_push(instr)) {
if (!instr->i_target->b_visited) {
struct _PyCfgExceptStack *copy = copy_except_stack(except_stack);
if (copy == NULL) {
goto error;
}
instr->i_target->b_exceptstack = copy;
todo[0] = instr->i_target;
instr->i_target->b_visited = 1;
todo++;
}
handler = push_except_block(except_stack, instr);
}
else if (instr->i_opcode == POP_BLOCK) {
handler = pop_except_block(except_stack);
INSTR_SET_OP0(instr, NOP);
}
else if (is_jump(instr)) {
instr->i_except = handler;
assert(i == b->b_iused -1);
if (!instr->i_target->b_visited) {
if (BB_HAS_FALLTHROUGH(b)) {
struct _PyCfgExceptStack *copy = copy_except_stack(except_stack);
if (copy == NULL) {
goto error;
}
instr->i_target->b_exceptstack = copy;
}
else {
instr->i_target->b_exceptstack = except_stack;
except_stack = NULL;
}
todo[0] = instr->i_target;
instr->i_target->b_visited = 1;
todo++;
}
}
else if (instr->i_opcode == YIELD_VALUE) {
instr->i_except = handler;
last_yield_except_depth = except_stack->depth;
}
else if (instr->i_opcode == RESUME) {
instr->i_except = handler;
if (instr->i_oparg != RESUME_AT_FUNC_START) {
assert(last_yield_except_depth >= 0);
if (last_yield_except_depth == 1) {
instr->i_oparg |= RESUME_OPARG_DEPTH1_MASK;
}
last_yield_except_depth = -1;
}
}
else {
instr->i_except = handler;
}
}
if (BB_HAS_FALLTHROUGH(b) && !b->b_next->b_visited) {
assert(except_stack != NULL);
b->b_next->b_exceptstack = except_stack;
todo[0] = b->b_next;
b->b_next->b_visited = 1;
todo++;
}
else if (except_stack != NULL) {
PyMem_Free(except_stack);
}
}
#ifdef Py_DEBUG
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
assert(b->b_exceptstack == NULL);
}
#endif
PyMem_Free(todo_stack);
return SUCCESS;
error:
PyMem_Free(todo_stack);
PyMem_Free(except_stack);
return ERROR;
}
/***** CFG optimizations *****/
static int
remove_unreachable(basicblock *entryblock) {
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
b->b_predecessors = 0;
}
basicblock **stack = make_cfg_traversal_stack(entryblock);
if (stack == NULL) {
return ERROR;
}
basicblock **sp = stack;
entryblock->b_predecessors = 1;
*sp++ = entryblock;
entryblock->b_visited = 1;
while (sp > stack) {
basicblock *b = *(--sp);
if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
if (!b->b_next->b_visited) {
assert(b->b_next->b_predecessors == 0);
*sp++ = b->b_next;
b->b_next->b_visited = 1;
}
b->b_next->b_predecessors++;
}
for (int i = 0; i < b->b_iused; i++) {
basicblock *target;
cfg_instr *instr = &b->b_instr[i];
if (is_jump(instr) || is_block_push(instr)) {
target = instr->i_target;
if (!target->b_visited) {
*sp++ = target;
target->b_visited = 1;
}
target->b_predecessors++;
}
}
}
PyMem_Free(stack);
/* Delete unreachable instructions */
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
if (b->b_predecessors == 0) {
b->b_iused = 0;
b->b_except_handler = 0;
}
}
return SUCCESS;
}
static int
basicblock_remove_redundant_nops(basicblock *bb) {
/* Remove NOPs when legal to do so. */
int dest = 0;
int prev_lineno = -1;
for (int src = 0; src < bb->b_iused; src++) {
int lineno = bb->b_instr[src].i_loc.lineno;
if (bb->b_instr[src].i_opcode == NOP) {
/* Eliminate no-op if it doesn't have a line number */
if (lineno < 0) {
continue;
}
/* or, if the previous instruction had the same line number. */
if (prev_lineno == lineno) {
continue;
}
/* or, if the next instruction has same line number or no line number */
if (src < bb->b_iused - 1) {
int next_lineno = bb->b_instr[src+1].i_loc.lineno;
if (next_lineno == lineno) {
continue;
}
if (next_lineno < 0) {
bb->b_instr[src+1].i_loc = bb->b_instr[src].i_loc;
continue;
}
}
else {
basicblock *next = next_nonempty_block(bb->b_next);
/* or if last instruction in BB and next BB has same line number */
if (next) {
location next_loc = NO_LOCATION;
for (int next_i=0; next_i < next->b_iused; next_i++) {
cfg_instr *instr = &next->b_instr[next_i];
if (instr->i_opcode == NOP && instr->i_loc.lineno < 0) {
/* Skip over NOPs without a location, they will be removed */
continue;
}
next_loc = instr->i_loc;
break;
}
if (lineno == next_loc.lineno) {
continue;
}
}
}
}
if (dest != src) {
bb->b_instr[dest] = bb->b_instr[src];
}
dest++;
prev_lineno = lineno;
}
assert(dest <= bb->b_iused);
int num_removed = bb->b_iused - dest;
bb->b_iused = dest;
return num_removed;
}
static int
remove_redundant_nops(cfg_builder *g) {
int changes = 0;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
int change = basicblock_remove_redundant_nops(b);
RETURN_IF_ERROR(change);
changes += change;
}
return changes;
}
static int
remove_redundant_nops_and_pairs(basicblock *entryblock)
{
bool done = false;
while (! done) {
done = true;
cfg_instr *prev_instr = NULL;
cfg_instr *instr = NULL;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
RETURN_IF_ERROR(basicblock_remove_redundant_nops(b));
if (IS_LABEL(b->b_label)) {
/* this block is a jump target, forget instr */
instr = NULL;
}
for (int i = 0; i < b->b_iused; i++) {
prev_instr = instr;
instr = &b->b_instr[i];
int prev_opcode = prev_instr ? prev_instr->i_opcode : 0;
int prev_oparg = prev_instr ? prev_instr->i_oparg : 0;
int opcode = instr->i_opcode;
bool is_redundant_pair = false;
if (opcode == POP_TOP) {
if (prev_opcode == LOAD_CONST || prev_opcode == LOAD_SMALL_INT) {
is_redundant_pair = true;
}
else if (prev_opcode == COPY && prev_oparg == 1) {
is_redundant_pair = true;
}
}
if (is_redundant_pair) {
INSTR_SET_OP0(prev_instr, NOP);
INSTR_SET_OP0(instr, NOP);
done = false;
}
}
if ((instr && is_jump(instr)) || !BB_HAS_FALLTHROUGH(b)) {
instr = NULL;
}
}
}
return SUCCESS;
}
static int
remove_redundant_jumps(cfg_builder *g) {
/* If a non-empty block ends with a jump instruction, check if the next
* non-empty block reached through normal flow control is the target
* of that jump. If it is, then the jump instruction is redundant and
* can be deleted.
*
* Return the number of changes applied, or -1 on error.
*/
int changes = 0;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
cfg_instr *last = basicblock_last_instr(b);
if (last == NULL) {
continue;
}
assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
if (IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
basicblock* jump_target = next_nonempty_block(last->i_target);
if (jump_target == NULL) {
PyErr_SetString(PyExc_SystemError, "jump with NULL target");
return ERROR;
}
basicblock *next = next_nonempty_block(b->b_next);
if (jump_target == next) {
changes++;
INSTR_SET_OP0(last, NOP);
}
}
}
return changes;
}
static inline bool
basicblock_has_no_lineno(basicblock *b) {
for (int i = 0; i < b->b_iused; i++) {
if (b->b_instr[i].i_loc.lineno >= 0) {
return false;
}
}
return true;
}
/* Maximum size of basic block that should be copied in optimizer */
#define MAX_COPY_SIZE 4
/* If this block ends with an unconditional jump to a small exit block or
* a block that has no line numbers (and no fallthrough), then
* remove the jump and extend this block with the target.
* Returns 1 if extended, 0 if no change, and -1 on error.
*/
static int
basicblock_inline_small_or_no_lineno_blocks(basicblock *bb) {
cfg_instr *last = basicblock_last_instr(bb);
if (last == NULL) {
return 0;
}
if (!IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
return 0;
}
basicblock *target = last->i_target;
bool small_exit_block = (basicblock_exits_scope(target) &&
target->b_iused <= MAX_COPY_SIZE);
bool no_lineno_no_fallthrough = (basicblock_has_no_lineno(target) &&
!BB_HAS_FALLTHROUGH(target));
if (small_exit_block || no_lineno_no_fallthrough) {
assert(is_jump(last));
int removed_jump_opcode = last->i_opcode;
INSTR_SET_OP0(last, NOP);
RETURN_IF_ERROR(basicblock_append_instructions(bb, target));
if (no_lineno_no_fallthrough) {
last = basicblock_last_instr(bb);
if (IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode) &&
removed_jump_opcode == JUMP)
{
/* Make sure we don't lose eval breaker checks */
last->i_opcode = JUMP;
}
}
target->b_predecessors--;
return 1;
}
return 0;
}
static int
inline_small_or_no_lineno_blocks(basicblock *entryblock) {
bool changes;
do {
changes = false;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
int res = basicblock_inline_small_or_no_lineno_blocks(b);
RETURN_IF_ERROR(res);
if (res) {
changes = true;
}
}
} while(changes); /* every change removes a jump, ensuring convergence */
return changes;
}
// Attempt to eliminate jumps to jumps by updating inst to jump to
// target->i_target using the provided opcode. Return whether or not the
// optimization was successful.
static bool
jump_thread(basicblock *bb, cfg_instr *inst, cfg_instr *target, int opcode)
{
assert(is_jump(inst));
assert(is_jump(target));
assert(inst == basicblock_last_instr(bb));
// bpo-45773: If inst->i_target == target->i_target, then nothing actually
// changes (and we fall into an infinite loop):
if (inst->i_target != target->i_target) {
/* Change inst to NOP and append a jump to target->i_target. The
* NOP will be removed later if it's not needed for the lineno.
*/
INSTR_SET_OP0(inst, NOP);
RETURN_IF_ERROR(
basicblock_add_jump(
bb, opcode, target->i_target, target->i_loc));
return true;
}
return false;
}
static int
loads_const(int opcode)
{
return OPCODE_HAS_CONST(opcode) || opcode == LOAD_SMALL_INT;
}
/* Returns new reference */
static PyObject*
get_const_value(int opcode, int oparg, PyObject *co_consts)
{
PyObject *constant = NULL;
assert(loads_const(opcode));
if (opcode == LOAD_CONST) {
constant = PyList_GET_ITEM(co_consts, oparg);
}
if (opcode == LOAD_SMALL_INT) {
return PyLong_FromLong(oparg);
}
if (constant == NULL) {
PyErr_SetString(PyExc_SystemError,
"Internal error: failed to get value of a constant");
return NULL;
}
return Py_NewRef(constant);
}
// Steals a reference to newconst.
static int
add_const(PyObject *newconst, PyObject *consts, PyObject *const_cache)
{
if (_PyCompile_ConstCacheMergeOne(const_cache, &newconst) < 0) {
Py_DECREF(newconst);
return -1;
}
Py_ssize_t index;
for (index = 0; index < PyList_GET_SIZE(consts); index++) {
if (PyList_GET_ITEM(consts, index) == newconst) {
break;
}
}
if (index == PyList_GET_SIZE(consts)) {
if ((size_t)index >= (size_t)INT_MAX - 1) {
PyErr_SetString(PyExc_OverflowError, "too many constants");
Py_DECREF(newconst);
return -1;
}
if (PyList_Append(consts, newconst)) {
Py_DECREF(newconst);
return -1;
}
}
Py_DECREF(newconst);
return (int)index;
}
/*
Traverse the instructions of the basic block backwards from index "start", skipping over NOPs.
Try to collect "size" number of consecutive instructions that load constants into the array "instrs".
Caller must make sure that length of "instrs" is sufficient to fit in at least "size" instructions.
Return boolean indicating whether "size" such instructions were found.
*/
static bool
get_const_loading_instrs(basicblock *bb, int start, cfg_instr **instrs, int size)
{
assert(start < bb->b_iused);
assert(size >= 0);
assert(size <= _PY_STACK_USE_GUIDELINE);
for (; start >= 0 && size > 0; start--) {
cfg_instr *instr = &bb->b_instr[start];
if (instr->i_opcode == NOP) {
continue;
}
if (!loads_const(instr->i_opcode)) {
return false;
}
instrs[--size] = instr;
}
return size == 0;
}
/*
Change every instruction in "instrs" NOP and set its location to NO_LOCATION.
Caller must make sure "instrs" has at least "size" elements.
*/
static void
nop_out(cfg_instr **instrs, int size)
{
for (int i = 0; i < size; i++) {
cfg_instr *instr = instrs[i];
assert(instr->i_opcode != NOP);
INSTR_SET_OP0(instr, NOP);
INSTR_SET_LOC(instr, NO_LOCATION);
}
}
/* Does not steal reference to "newconst".
Return 1 if changed instruction to LOAD_SMALL_INT.
Return 0 if could not change instruction to LOAD_SMALL_INT.
Return -1 on error.
*/
static int
maybe_instr_make_load_smallint(cfg_instr *instr, PyObject *newconst,
PyObject *consts, PyObject *const_cache)
{
if (PyLong_CheckExact(newconst)) {
int overflow;
long val = PyLong_AsLongAndOverflow(newconst, &overflow);
if (val == -1 && PyErr_Occurred()) {
return -1;
}
if (!overflow && _PY_IS_SMALL_INT(val)) {
assert(_Py_IsImmortal(newconst));
INSTR_SET_OP1(instr, LOAD_SMALL_INT, (int)val);
return 1;
}
}
return 0;
}
/* Steals reference to "newconst" */
static int
instr_make_load_const(cfg_instr *instr, PyObject *newconst,
PyObject *consts, PyObject *const_cache)
{
int res = maybe_instr_make_load_smallint(instr, newconst, consts, const_cache);
if (res < 0) {
Py_DECREF(newconst);
return ERROR;
}
if (res > 0) {
return SUCCESS;
}
int oparg = add_const(newconst, consts, const_cache);
RETURN_IF_ERROR(oparg);
INSTR_SET_OP1(instr, LOAD_CONST, oparg);
return SUCCESS;
}
/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
with LOAD_CONST (c1, c2, ... cn).
The consts table must still be in list form so that the
new constant (c1, c2, ... cn) can be appended.
Called with codestr pointing to the first LOAD_CONST.
*/
static int
fold_tuple_of_constants(basicblock *bb, int i, PyObject *consts, PyObject *const_cache)
{
/* Pre-conditions */
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
cfg_instr *instr = &bb->b_instr[i];
assert(instr->i_opcode == BUILD_TUPLE);
int seq_size = instr->i_oparg;
if (seq_size > _PY_STACK_USE_GUIDELINE) {
return SUCCESS;
}
cfg_instr *const_instrs[_PY_STACK_USE_GUIDELINE];
if (!get_const_loading_instrs(bb, i-1, const_instrs, seq_size)) {
/* not a const sequence */
return SUCCESS;
}
PyObject *const_tuple = PyTuple_New((Py_ssize_t)seq_size);
if (const_tuple == NULL) {
return ERROR;
}
for (int i = 0; i < seq_size; i++) {
cfg_instr *inst = const_instrs[i];
assert(loads_const(inst->i_opcode));
PyObject *element = get_const_value(inst->i_opcode, inst->i_oparg, consts);
if (element == NULL) {
Py_DECREF(const_tuple);
return ERROR;
}
PyTuple_SET_ITEM(const_tuple, i, element);
}
nop_out(const_instrs, seq_size);
return instr_make_load_const(instr, const_tuple, consts, const_cache);
}
/* Replace:
BUILD_LIST 0
LOAD_CONST c1
LIST_APPEND 1
LOAD_CONST c2
LIST_APPEND 1
...
LOAD_CONST cN
LIST_APPEND 1
CALL_INTRINSIC_1 INTRINSIC_LIST_TO_TUPLE
with:
LOAD_CONST (c1, c2, ... cN)
*/
static int
fold_constant_intrinsic_list_to_tuple(basicblock *bb, int i,
PyObject *consts, PyObject *const_cache)
{
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
assert(i >= 0);
assert(i < bb->b_iused);
cfg_instr *intrinsic = &bb->b_instr[i];
assert(intrinsic->i_opcode == CALL_INTRINSIC_1);
assert(intrinsic->i_oparg == INTRINSIC_LIST_TO_TUPLE);
int consts_found = 0;
bool expect_append = true;
for (int pos = i - 1; pos >= 0; pos--) {
cfg_instr *instr = &bb->b_instr[pos];
int opcode = instr->i_opcode;
int oparg = instr->i_oparg;
if (opcode == NOP) {
continue;
}
if (opcode == BUILD_LIST && oparg == 0) {
if (!expect_append) {
/* Not a sequence start. */
return SUCCESS;
}
/* Sequence start, we are done. */
PyObject *newconst = PyTuple_New((Py_ssize_t)consts_found);
if (newconst == NULL) {
return ERROR;
}
for (int newpos = i - 1; newpos >= pos; newpos--) {
instr = &bb->b_instr[newpos];
if (instr->i_opcode == NOP) {
continue;
}
if (loads_const(instr->i_opcode)) {
PyObject *constant = get_const_value(instr->i_opcode, instr->i_oparg, consts);
if (constant == NULL) {
Py_DECREF(newconst);
return ERROR;
}
assert(consts_found > 0);
PyTuple_SET_ITEM(newconst, --consts_found, constant);
}
nop_out(&instr, 1);
}
assert(consts_found == 0);
return instr_make_load_const(intrinsic, newconst, consts, const_cache);
}
if (expect_append) {
if (opcode != LIST_APPEND || oparg != 1) {
return SUCCESS;
}
}
else {
if (!loads_const(opcode)) {
return SUCCESS;
}
consts_found++;
}
expect_append = !expect_append;
}
/* Did not find sequence start. */
return SUCCESS;
}
#define MIN_CONST_SEQUENCE_SIZE 3
/*
Optimize lists and sets for:
1. "for" loop, comprehension or "in"/"not in" tests:
Change literal list or set of constants into constant
tuple or frozenset respectively. Change list of
non-constants into tuple.
2. Constant literal lists/set with length >= MIN_CONST_SEQUENCE_SIZE:
Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cN, BUILD_LIST N
with BUILD_LIST 0, LOAD_CONST (c1, c2, ... cN), LIST_EXTEND 1,
or BUILD_SET & SET_UPDATE respectively.
*/
static int
optimize_lists_and_sets(basicblock *bb, int i, int nextop,
PyObject *consts, PyObject *const_cache)
{
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
cfg_instr *instr = &bb->b_instr[i];
assert(instr->i_opcode == BUILD_LIST || instr->i_opcode == BUILD_SET);
bool contains_or_iter = nextop == GET_ITER || nextop == CONTAINS_OP;
int seq_size = instr->i_oparg;
if (seq_size > _PY_STACK_USE_GUIDELINE ||
(seq_size < MIN_CONST_SEQUENCE_SIZE && !contains_or_iter))
{
return SUCCESS;
}
cfg_instr *const_instrs[_PY_STACK_USE_GUIDELINE];
if (!get_const_loading_instrs(bb, i-1, const_instrs, seq_size)) { /* not a const sequence */
if (contains_or_iter && instr->i_opcode == BUILD_LIST) {
/* iterate over a tuple instead of list */
INSTR_SET_OP1(instr, BUILD_TUPLE, instr->i_oparg);
}
return SUCCESS;
}
PyObject *const_result = PyTuple_New((Py_ssize_t)seq_size);
if (const_result == NULL) {
return ERROR;
}
for (int i = 0; i < seq_size; i++) {
cfg_instr *inst = const_instrs[i];
assert(loads_const(inst->i_opcode));
PyObject *element = get_const_value(inst->i_opcode, inst->i_oparg, consts);
if (element == NULL) {
Py_DECREF(const_result);
return ERROR;
}
PyTuple_SET_ITEM(const_result, i, element);
}
if (instr->i_opcode == BUILD_SET) {
PyObject *frozenset = PyFrozenSet_New(const_result);
if (frozenset == NULL) {
Py_DECREF(const_result);
return ERROR;
}
Py_SETREF(const_result, frozenset);
}
int index = add_const(const_result, consts, const_cache);
RETURN_IF_ERROR(index);
nop_out(const_instrs, seq_size);
if (contains_or_iter) {
INSTR_SET_OP1(instr, LOAD_CONST, index);
}
else {
assert(i >= 2);
assert(instr->i_opcode == BUILD_LIST || instr->i_opcode == BUILD_SET);
INSTR_SET_LOC(&bb->b_instr[i-2], instr->i_loc);
INSTR_SET_OP1(&bb->b_instr[i-2], instr->i_opcode, 0);
INSTR_SET_OP1(&bb->b_instr[i-1], LOAD_CONST, index);
INSTR_SET_OP1(&bb->b_instr[i], instr->i_opcode == BUILD_LIST ? LIST_EXTEND : SET_UPDATE, 1);
}
return SUCCESS;
}
/* Check whether the total number of items in the (possibly nested) collection obj exceeds
* limit. Return a negative number if it does, and a non-negative number otherwise.
* Used to avoid creating constants which are slow to hash.
*/
static Py_ssize_t
const_folding_check_complexity(PyObject *obj, Py_ssize_t limit)
{
if (PyTuple_Check(obj)) {
Py_ssize_t i;
limit -= PyTuple_GET_SIZE(obj);
for (i = 0; limit >= 0 && i < PyTuple_GET_SIZE(obj); i++) {
limit = const_folding_check_complexity(PyTuple_GET_ITEM(obj, i), limit);
if (limit < 0) {
return limit;
}
}
}
return limit;
}
#define MAX_INT_SIZE 128 /* bits */
#define MAX_COLLECTION_SIZE 256 /* items */
#define MAX_STR_SIZE 4096 /* characters */
#define MAX_TOTAL_ITEMS 1024 /* including nested collections */
static PyObject *
const_folding_safe_multiply(PyObject *v, PyObject *w)
{
if (PyLong_Check(v) && PyLong_Check(w) &&
!_PyLong_IsZero((PyLongObject *)v) && !_PyLong_IsZero((PyLongObject *)w)
) {
int64_t vbits = _PyLong_NumBits(v);
int64_t wbits = _PyLong_NumBits(w);
assert(vbits >= 0);
assert(wbits >= 0);
if (vbits + wbits > MAX_INT_SIZE) {
return NULL;
}
}
else if (PyLong_Check(v) && PyTuple_Check(w)) {
Py_ssize_t size = PyTuple_GET_SIZE(w);
if (size) {
long n = PyLong_AsLong(v);
if (n < 0 || n > MAX_COLLECTION_SIZE / size) {
return NULL;
}
if (n && const_folding_check_complexity(w, MAX_TOTAL_ITEMS / n) < 0) {
return NULL;
}
}
}
else if (PyLong_Check(v) && (PyUnicode_Check(w) || PyBytes_Check(w))) {
Py_ssize_t size = PyUnicode_Check(w) ? PyUnicode_GET_LENGTH(w) :
PyBytes_GET_SIZE(w);
if (size) {
long n = PyLong_AsLong(v);
if (n < 0 || n > MAX_STR_SIZE / size) {
return NULL;
}
}
}
else if (PyLong_Check(w) &&
(PyTuple_Check(v) || PyUnicode_Check(v) || PyBytes_Check(v)))
{
return const_folding_safe_multiply(w, v);
}
return PyNumber_Multiply(v, w);
}
static PyObject *
const_folding_safe_power(PyObject *v, PyObject *w)
{
if (PyLong_Check(v) && PyLong_Check(w) &&
!_PyLong_IsZero((PyLongObject *)v) && _PyLong_IsPositive((PyLongObject *)w)
) {
int64_t vbits = _PyLong_NumBits(v);
size_t wbits = PyLong_AsSize_t(w);
assert(vbits >= 0);
if (wbits == (size_t)-1) {
return NULL;
}
if ((uint64_t)vbits > MAX_INT_SIZE / wbits) {
return NULL;
}
}
return PyNumber_Power(v, w, Py_None);
}
static PyObject *
const_folding_safe_lshift(PyObject *v, PyObject *w)
{
if (PyLong_Check(v) && PyLong_Check(w) &&
!_PyLong_IsZero((PyLongObject *)v) && !_PyLong_IsZero((PyLongObject *)w)
) {
int64_t vbits = _PyLong_NumBits(v);
size_t wbits = PyLong_AsSize_t(w);
assert(vbits >= 0);
if (wbits == (size_t)-1) {
return NULL;
}
if (wbits > MAX_INT_SIZE || (uint64_t)vbits > MAX_INT_SIZE - wbits) {
return NULL;
}
}
return PyNumber_Lshift(v, w);
}
static PyObject *
const_folding_safe_mod(PyObject *v, PyObject *w)
{
if (PyUnicode_Check(v) || PyBytes_Check(v)) {
return NULL;
}
return PyNumber_Remainder(v, w);
}
static PyObject *
eval_const_binop(PyObject *left, int op, PyObject *right)
{
assert(left != NULL && right != NULL);
assert(op >= 0 && op <= NB_OPARG_LAST);
PyObject *result = NULL;
switch (op) {
case NB_ADD:
result = PyNumber_Add(left, right);
break;
case NB_SUBTRACT:
result = PyNumber_Subtract(left, right);
break;
case NB_MULTIPLY:
result = const_folding_safe_multiply(left, right);
break;
case NB_TRUE_DIVIDE:
result = PyNumber_TrueDivide(left, right);
break;
case NB_FLOOR_DIVIDE:
result = PyNumber_FloorDivide(left, right);
break;
case NB_REMAINDER:
result = const_folding_safe_mod(left, right);
break;
case NB_POWER:
result = const_folding_safe_power(left, right);
break;
case NB_LSHIFT:
result = const_folding_safe_lshift(left, right);
break;
case NB_RSHIFT:
result = PyNumber_Rshift(left, right);
break;
case NB_OR:
result = PyNumber_Or(left, right);
break;
case NB_XOR:
result = PyNumber_Xor(left, right);
break;
case NB_AND:
result = PyNumber_And(left, right);
break;
case NB_SUBSCR:
result = PyObject_GetItem(left, right);
break;
case NB_MATRIX_MULTIPLY:
// No builtin constants implement matrix multiplication
break;
default:
Py_UNREACHABLE();
}
return result;
}
static int
fold_const_binop(basicblock *bb, int i, PyObject *consts, PyObject *const_cache)
{
#define BINOP_OPERAND_COUNT 2
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
cfg_instr *binop = &bb->b_instr[i];
assert(binop->i_opcode == BINARY_OP);
cfg_instr *operands_instrs[BINOP_OPERAND_COUNT];
if (!get_const_loading_instrs(bb, i-1, operands_instrs, BINOP_OPERAND_COUNT)) {
/* not a const sequence */
return SUCCESS;
}
cfg_instr *lhs_instr = operands_instrs[0];
assert(loads_const(lhs_instr->i_opcode));
PyObject *lhs = get_const_value(lhs_instr->i_opcode, lhs_instr->i_oparg, consts);
if (lhs == NULL) {
return ERROR;
}
cfg_instr *rhs_instr = operands_instrs[1];
assert(loads_const(rhs_instr->i_opcode));
PyObject *rhs = get_const_value(rhs_instr->i_opcode, rhs_instr->i_oparg, consts);
if (rhs == NULL) {
Py_DECREF(lhs);
return ERROR;
}
PyObject *newconst = eval_const_binop(lhs, binop->i_oparg, rhs);
Py_DECREF(lhs);
Py_DECREF(rhs);
if (newconst == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
return ERROR;
}
PyErr_Clear();
return SUCCESS;
}
nop_out(operands_instrs, BINOP_OPERAND_COUNT);
return instr_make_load_const(binop, newconst, consts, const_cache);
}
static PyObject *
eval_const_unaryop(PyObject *operand, int opcode, int oparg)
{
assert(operand != NULL);
assert(
opcode == UNARY_NEGATIVE ||
opcode == UNARY_INVERT ||
opcode == UNARY_NOT ||
(opcode == CALL_INTRINSIC_1 && oparg == INTRINSIC_UNARY_POSITIVE)
);
PyObject *result;
switch (opcode) {
case UNARY_NEGATIVE:
result = PyNumber_Negative(operand);
break;
case UNARY_INVERT:
// XXX: This should be removed once the ~bool depreciation expires.
if (PyBool_Check(operand)) {
return NULL;
}
result = PyNumber_Invert(operand);
break;
case UNARY_NOT: {
int r = PyObject_IsTrue(operand);
if (r < 0) {
return NULL;
}
result = PyBool_FromLong(!r);
break;
}
case CALL_INTRINSIC_1:
if (oparg != INTRINSIC_UNARY_POSITIVE) {
Py_UNREACHABLE();
}
result = PyNumber_Positive(operand);
break;
default:
Py_UNREACHABLE();
}
return result;
}
static int
fold_const_unaryop(basicblock *bb, int i, PyObject *consts, PyObject *const_cache)
{
#define UNARYOP_OPERAND_COUNT 1
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
cfg_instr *unaryop = &bb->b_instr[i];
cfg_instr *operand_instr;
if (!get_const_loading_instrs(bb, i-1, &operand_instr, UNARYOP_OPERAND_COUNT)) {
/* not a const */
return SUCCESS;
}
assert(loads_const(operand_instr->i_opcode));
PyObject *operand = get_const_value(
operand_instr->i_opcode,
operand_instr->i_oparg,
consts
);
if (operand == NULL) {
return ERROR;
}
PyObject *newconst = eval_const_unaryop(operand, unaryop->i_opcode, unaryop->i_oparg);
Py_DECREF(operand);
if (newconst == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
return ERROR;
}
PyErr_Clear();
return SUCCESS;
}
if (unaryop->i_opcode == UNARY_NOT) {
assert(PyBool_Check(newconst));
}
nop_out(&operand_instr, UNARYOP_OPERAND_COUNT);
return instr_make_load_const(unaryop, newconst, consts, const_cache);
}
#define VISITED (-1)
// Replace an arbitrary run of SWAPs and NOPs with an optimal one that has the
// same effect.
static int
swaptimize(basicblock *block, int *ix)
{
// NOTE: "./python -m test test_patma" serves as a good, quick stress test
// for this function. Make sure to blow away cached *.pyc files first!
assert(*ix < block->b_iused);
cfg_instr *instructions = &block->b_instr[*ix];
// Find the length of the current sequence of SWAPs and NOPs, and record the
// maximum depth of the stack manipulations:
assert(instructions[0].i_opcode == SWAP);
int depth = instructions[0].i_oparg;
int len = 0;
int more = false;
int limit = block->b_iused - *ix;
while (++len < limit) {
int opcode = instructions[len].i_opcode;
if (opcode == SWAP) {
depth = Py_MAX(depth, instructions[len].i_oparg);
more = true;
}
else if (opcode != NOP) {
break;
}
}
// It's already optimal if there's only one SWAP:
if (!more) {
return SUCCESS;
}
// Create an array with elements {0, 1, 2, ..., depth - 1}:
int *stack = PyMem_Malloc(depth * sizeof(int));
if (stack == NULL) {
PyErr_NoMemory();
return ERROR;
}
for (int i = 0; i < depth; i++) {
stack[i] = i;
}
// Simulate the combined effect of these instructions by "running" them on
// our "stack":
for (int i = 0; i < len; i++) {
if (instructions[i].i_opcode == SWAP) {
int oparg = instructions[i].i_oparg;
int top = stack[0];
// SWAPs are 1-indexed:
stack[0] = stack[oparg - 1];
stack[oparg - 1] = top;
}
}
// Now we can begin! Our approach here is based on a solution to a closely
// related problem (https://cs.stackexchange.com/a/13938). It's easiest to
// think of this algorithm as determining the steps needed to efficiently
// "un-shuffle" our stack. By performing the moves in *reverse* order,
// though, we can efficiently *shuffle* it! For this reason, we will be
// replacing instructions starting from the *end* of the run. Since the
// solution is optimal, we don't need to worry about running out of space:
int current = len - 1;
for (int i = 0; i < depth; i++) {
// Skip items that have already been visited, or just happen to be in
// the correct location:
if (stack[i] == VISITED || stack[i] == i) {
continue;
}
// Okay, we've found an item that hasn't been visited. It forms a cycle
// with other items; traversing the cycle and swapping each item with
// the next will put them all in the correct place. The weird
// loop-and-a-half is necessary to insert 0 into every cycle, since we
// can only swap from that position:
int j = i;
while (true) {
// Skip the actual swap if our item is zero, since swapping the top
// item with itself is pointless:
if (j) {
assert(0 <= current);
// SWAPs are 1-indexed:
instructions[current].i_opcode = SWAP;
instructions[current--].i_oparg = j + 1;
}
if (stack[j] == VISITED) {
// Completed the cycle:
assert(j == i);
break;
}
int next_j = stack[j];
stack[j] = VISITED;
j = next_j;
}
}
// NOP out any unused instructions:
while (0 <= current) {
INSTR_SET_OP0(&instructions[current--], NOP);
}
PyMem_Free(stack);
*ix += len - 1;
return SUCCESS;
}
// This list is pretty small, since it's only okay to reorder opcodes that:
// - can't affect control flow (like jumping or raising exceptions)
// - can't invoke arbitrary code (besides finalizers)
// - only touch the TOS (and pop it when finished)
#define SWAPPABLE(opcode) \
((opcode) == STORE_FAST || \
(opcode) == STORE_FAST_MAYBE_NULL || \
(opcode) == POP_TOP)
#define STORES_TO(instr) \
(((instr).i_opcode == STORE_FAST || \
(instr).i_opcode == STORE_FAST_MAYBE_NULL) \
? (instr).i_oparg : -1)
static int
next_swappable_instruction(basicblock *block, int i, int lineno)
{
while (++i < block->b_iused) {
cfg_instr *instruction = &block->b_instr[i];
if (0 <= lineno && instruction->i_loc.lineno != lineno) {
// Optimizing across this instruction could cause user-visible
// changes in the names bound between line tracing events!
return -1;
}
if (instruction->i_opcode == NOP) {
continue;
}
if (SWAPPABLE(instruction->i_opcode)) {
return i;
}
return -1;
}
return -1;
}
// Attempt to apply SWAPs statically by swapping *instructions* rather than
// stack items. For example, we can replace SWAP(2), POP_TOP, STORE_FAST(42)
// with the more efficient NOP, STORE_FAST(42), POP_TOP.
static void
apply_static_swaps(basicblock *block, int i)
{
// SWAPs are to our left, and potential swaperands are to our right:
for (; 0 <= i; i--) {
assert(i < block->b_iused);
cfg_instr *swap = &block->b_instr[i];
if (swap->i_opcode != SWAP) {
if (swap->i_opcode == NOP || SWAPPABLE(swap->i_opcode)) {
// Nope, but we know how to handle these. Keep looking:
continue;
}
// We can't reason about what this instruction does. Bail:
return;
}
int j = next_swappable_instruction(block, i, -1);
if (j < 0) {
return;
}
int k = j;
int lineno = block->b_instr[j].i_loc.lineno;
for (int count = swap->i_oparg - 1; 0 < count; count--) {
k = next_swappable_instruction(block, k, lineno);
if (k < 0) {
return;
}
}
// The reordering is not safe if the two instructions to be swapped
// store to the same location, or if any intervening instruction stores
// to the same location as either of them.
int store_j = STORES_TO(block->b_instr[j]);
int store_k = STORES_TO(block->b_instr[k]);
if (store_j >= 0 || store_k >= 0) {
if (store_j == store_k) {
return;
}
for (int idx = j + 1; idx < k; idx++) {
int store_idx = STORES_TO(block->b_instr[idx]);
if (store_idx >= 0 && (store_idx == store_j || store_idx == store_k)) {
return;
}
}
}
// Success!
INSTR_SET_OP0(swap, NOP);
cfg_instr temp = block->b_instr[j];
block->b_instr[j] = block->b_instr[k];
block->b_instr[k] = temp;
}
}
static int
basicblock_optimize_load_const(PyObject *const_cache, basicblock *bb, PyObject *consts)
{
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
int opcode = 0;
int oparg = 0;
for (int i = 0; i < bb->b_iused; i++) {
cfg_instr *inst = &bb->b_instr[i];
if (inst->i_opcode == LOAD_CONST) {
PyObject *constant = get_const_value(inst->i_opcode, inst->i_oparg, consts);
int res = maybe_instr_make_load_smallint(inst, constant, consts, const_cache);
Py_DECREF(constant);
if (res < 0) {
return ERROR;
}
}
bool is_copy_of_load_const = (opcode == LOAD_CONST &&
inst->i_opcode == COPY &&
inst->i_oparg == 1);
if (! is_copy_of_load_const) {
opcode = inst->i_opcode;
oparg = inst->i_oparg;
}
assert(!IS_ASSEMBLER_OPCODE(opcode));
if (opcode != LOAD_CONST && opcode != LOAD_SMALL_INT) {
continue;
}
int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
switch(nextop) {
case POP_JUMP_IF_FALSE:
case POP_JUMP_IF_TRUE:
case JUMP_IF_FALSE:
case JUMP_IF_TRUE:
{
/* Remove LOAD_CONST const; conditional jump */
PyObject* cnt = get_const_value(opcode, oparg, consts);
if (cnt == NULL) {
return ERROR;
}
int is_true = PyObject_IsTrue(cnt);
Py_DECREF(cnt);
if (is_true == -1) {
return ERROR;
}
if (PyCompile_OpcodeStackEffect(nextop, 0) == -1) {
/* POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE */
INSTR_SET_OP0(inst, NOP);
}
int jump_if_true = (nextop == POP_JUMP_IF_TRUE || nextop == JUMP_IF_TRUE);
if (is_true == jump_if_true) {
bb->b_instr[i+1].i_opcode = JUMP;
}
else {
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
}
break;
}
case IS_OP:
{
// Fold to POP_JUMP_IF_NONE:
// - LOAD_CONST(None) IS_OP(0) POP_JUMP_IF_TRUE
// - LOAD_CONST(None) IS_OP(1) POP_JUMP_IF_FALSE
// - LOAD_CONST(None) IS_OP(0) TO_BOOL POP_JUMP_IF_TRUE
// - LOAD_CONST(None) IS_OP(1) TO_BOOL POP_JUMP_IF_FALSE
// Fold to POP_JUMP_IF_NOT_NONE:
// - LOAD_CONST(None) IS_OP(0) POP_JUMP_IF_FALSE
// - LOAD_CONST(None) IS_OP(1) POP_JUMP_IF_TRUE
// - LOAD_CONST(None) IS_OP(0) TO_BOOL POP_JUMP_IF_FALSE
// - LOAD_CONST(None) IS_OP(1) TO_BOOL POP_JUMP_IF_TRUE
PyObject *cnt = get_const_value(opcode, oparg, consts);
if (cnt == NULL) {
return ERROR;
}
if (!Py_IsNone(cnt)) {
Py_DECREF(cnt);
break;
}
if (bb->b_iused <= i + 2) {
break;
}
cfg_instr *is_instr = &bb->b_instr[i + 1];
cfg_instr *jump_instr = &bb->b_instr[i + 2];
// Get rid of TO_BOOL regardless:
if (jump_instr->i_opcode == TO_BOOL) {
INSTR_SET_OP0(jump_instr, NOP);
if (bb->b_iused <= i + 3) {
break;
}
jump_instr = &bb->b_instr[i + 3];
}
bool invert = is_instr->i_oparg;
if (jump_instr->i_opcode == POP_JUMP_IF_FALSE) {
invert = !invert;
}
else if (jump_instr->i_opcode != POP_JUMP_IF_TRUE) {
break;
}
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP0(is_instr, NOP);
jump_instr->i_opcode = invert ? POP_JUMP_IF_NOT_NONE
: POP_JUMP_IF_NONE;
break;
}
case TO_BOOL:
{
PyObject *cnt = get_const_value(opcode, oparg, consts);
if (cnt == NULL) {
return ERROR;
}
int is_true = PyObject_IsTrue(cnt);
Py_DECREF(cnt);
if (is_true == -1) {
return ERROR;
}
cnt = PyBool_FromLong(is_true);
int index = add_const(cnt, consts, const_cache);
if (index < 0) {
return ERROR;
}
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP1(&bb->b_instr[i + 1], LOAD_CONST, index);
break;
}
}
}
return SUCCESS;
}
static int
optimize_load_const(PyObject *const_cache, cfg_builder *g, PyObject *consts) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
RETURN_IF_ERROR(basicblock_optimize_load_const(const_cache, b, consts));
}
return SUCCESS;
}
static int
optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
{
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
cfg_instr nop;
INSTR_SET_OP0(&nop, NOP);
for (int i = 0; i < bb->b_iused; i++) {
cfg_instr *inst = &bb->b_instr[i];
cfg_instr *target;
int opcode = inst->i_opcode;
int oparg = inst->i_oparg;
if (HAS_TARGET(opcode)) {
assert(inst->i_target->b_iused > 0);
target = &inst->i_target->b_instr[0];
assert(!IS_ASSEMBLER_OPCODE(target->i_opcode));
}
else {
target = &nop;
}
int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
assert(!IS_ASSEMBLER_OPCODE(opcode));
switch (opcode) {
/* Try to fold tuples of constants.
Skip over BUILD_TUPLE(1) UNPACK_SEQUENCE(1).
Replace BUILD_TUPLE(2) UNPACK_SEQUENCE(2) with SWAP(2).
Replace BUILD_TUPLE(3) UNPACK_SEQUENCE(3) with SWAP(3). */
case BUILD_TUPLE:
if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
switch(oparg) {
case 1:
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
continue;
case 2:
case 3:
INSTR_SET_OP0(inst, NOP);
bb->b_instr[i+1].i_opcode = SWAP;
continue;
}
}
RETURN_IF_ERROR(fold_tuple_of_constants(bb, i, consts, const_cache));
break;
case BUILD_LIST:
case BUILD_SET:
RETURN_IF_ERROR(optimize_lists_and_sets(bb, i, nextop, consts, const_cache));
break;
case POP_JUMP_IF_NOT_NONE:
case POP_JUMP_IF_NONE:
switch (target->i_opcode) {
case JUMP:
i -= jump_thread(bb, inst, target, inst->i_opcode);
}
break;
case POP_JUMP_IF_FALSE:
switch (target->i_opcode) {
case JUMP:
i -= jump_thread(bb, inst, target, POP_JUMP_IF_FALSE);
}
break;
case POP_JUMP_IF_TRUE:
switch (target->i_opcode) {
case JUMP:
i -= jump_thread(bb, inst, target, POP_JUMP_IF_TRUE);
}
break;
case JUMP_IF_FALSE:
switch (target->i_opcode) {
case JUMP:
case JUMP_IF_FALSE:
i -= jump_thread(bb, inst, target, JUMP_IF_FALSE);
continue;
case JUMP_IF_TRUE:
// No need to check for loops here, a block's b_next
// cannot point to itself.
assert(inst->i_target != inst->i_target->b_next);
inst->i_target = inst->i_target->b_next;
i--;
continue;
}
break;
case JUMP_IF_TRUE:
switch (target->i_opcode) {
case JUMP:
case JUMP_IF_TRUE:
i -= jump_thread(bb, inst, target, JUMP_IF_TRUE);
continue;
case JUMP_IF_FALSE:
// No need to check for loops here, a block's b_next
// cannot point to itself.
assert(inst->i_target != inst->i_target->b_next);
inst->i_target = inst->i_target->b_next;
i--;
continue;
}
break;
case JUMP:
case JUMP_NO_INTERRUPT:
switch (target->i_opcode) {
case JUMP:
i -= jump_thread(bb, inst, target, JUMP);
continue;
case JUMP_NO_INTERRUPT:
i -= jump_thread(bb, inst, target, opcode);
continue;
}
break;
case FOR_ITER:
if (target->i_opcode == JUMP) {
/* This will not work now because the jump (at target) could
* be forward or backward and FOR_ITER only jumps forward. We
* can re-enable this if ever we implement a backward version
* of FOR_ITER.
*/
/*
i -= jump_thread(bb, inst, target, FOR_ITER);
*/
}
break;
case STORE_FAST:
if (opcode == nextop &&
oparg == bb->b_instr[i+1].i_oparg &&
bb->b_instr[i].i_loc.lineno == bb->b_instr[i+1].i_loc.lineno) {
bb->b_instr[i].i_opcode = POP_TOP;
bb->b_instr[i].i_oparg = 0;
}
break;
case SWAP:
if (oparg == 1) {
INSTR_SET_OP0(inst, NOP);
}
break;
case LOAD_GLOBAL:
if (nextop == PUSH_NULL && (oparg & 1) == 0) {
INSTR_SET_OP1(inst, LOAD_GLOBAL, oparg | 1);
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
}
break;
case COMPARE_OP:
if (nextop == TO_BOOL) {
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP1(&bb->b_instr[i + 1], COMPARE_OP, oparg | 16);
continue;
}
break;
case CONTAINS_OP:
case IS_OP:
if (nextop == TO_BOOL) {
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP1(&bb->b_instr[i + 1], opcode, oparg);
continue;
}
if (nextop == UNARY_NOT) {
INSTR_SET_OP0(inst, NOP);
int inverted = oparg ^ 1;
assert(inverted == 0 || inverted == 1);
INSTR_SET_OP1(&bb->b_instr[i + 1], opcode, inverted);
continue;
}
break;
case TO_BOOL:
if (nextop == TO_BOOL) {
INSTR_SET_OP0(inst, NOP);
continue;
}
break;
case UNARY_NOT:
if (nextop == TO_BOOL) {
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP0(&bb->b_instr[i + 1], UNARY_NOT);
continue;
}
if (nextop == UNARY_NOT) {
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
continue;
}
_Py_FALLTHROUGH;
case UNARY_INVERT:
case UNARY_NEGATIVE:
RETURN_IF_ERROR(fold_const_unaryop(bb, i, consts, const_cache));
break;
case CALL_INTRINSIC_1:
if (oparg == INTRINSIC_LIST_TO_TUPLE) {
if (nextop == GET_ITER) {
INSTR_SET_OP0(inst, NOP);
}
else {
RETURN_IF_ERROR(fold_constant_intrinsic_list_to_tuple(bb, i, consts, const_cache));
}
}
else if (oparg == INTRINSIC_UNARY_POSITIVE) {
RETURN_IF_ERROR(fold_const_unaryop(bb, i, consts, const_cache));
}
break;
case BINARY_OP:
RETURN_IF_ERROR(fold_const_binop(bb, i, consts, const_cache));
break;
}
}
for (int i = 0; i < bb->b_iused; i++) {
cfg_instr *inst = &bb->b_instr[i];
if (inst->i_opcode == SWAP) {
if (swaptimize(bb, &i) < 0) {
goto error;
}
apply_static_swaps(bb, i);
}
}
return SUCCESS;
error:
return ERROR;
}
static int resolve_line_numbers(cfg_builder *g, int firstlineno);
static int
remove_redundant_nops_and_jumps(cfg_builder *g)
{
int removed_nops, removed_jumps;
do {
/* Convergence is guaranteed because the number of
* redundant jumps and nops only decreases.
*/
removed_nops = remove_redundant_nops(g);
RETURN_IF_ERROR(removed_nops);
removed_jumps = remove_redundant_jumps(g);
RETURN_IF_ERROR(removed_jumps);
} while(removed_nops + removed_jumps > 0);
return SUCCESS;
}
/* Perform optimizations on a control flow graph.
The consts object should still be in list form to allow new constants
to be appended.
Code trasnformations that reduce code size initially fill the gaps with
NOPs. Later those NOPs are removed.
*/
static int
optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache, int firstlineno)
{
assert(PyDict_CheckExact(const_cache));
RETURN_IF_ERROR(check_cfg(g));
RETURN_IF_ERROR(inline_small_or_no_lineno_blocks(g->g_entryblock));
RETURN_IF_ERROR(remove_unreachable(g->g_entryblock));
RETURN_IF_ERROR(resolve_line_numbers(g, firstlineno));
RETURN_IF_ERROR(optimize_load_const(const_cache, g, consts));
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
RETURN_IF_ERROR(optimize_basic_block(const_cache, b, consts));
}
RETURN_IF_ERROR(remove_redundant_nops_and_pairs(g->g_entryblock));
RETURN_IF_ERROR(remove_unreachable(g->g_entryblock));
RETURN_IF_ERROR(remove_redundant_nops_and_jumps(g));
assert(no_redundant_jumps(g));
return SUCCESS;
}
static void
make_super_instruction(cfg_instr *inst1, cfg_instr *inst2, int super_op)
{
int32_t line1 = inst1->i_loc.lineno;
int32_t line2 = inst2->i_loc.lineno;
/* Skip if instructions are on different lines */
if (line1 >= 0 && line2 >= 0 && line1 != line2) {
return;
}
if (inst1->i_oparg >= 16 || inst2->i_oparg >= 16) {
return;
}
INSTR_SET_OP1(inst1, super_op, (inst1->i_oparg << 4) | inst2->i_oparg);
INSTR_SET_OP0(inst2, NOP);
}
static int
insert_superinstructions(cfg_builder *g)
{
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *inst = &b->b_instr[i];
int nextop = i+1 < b->b_iused ? b->b_instr[i+1].i_opcode : 0;
switch(inst->i_opcode) {
case LOAD_FAST:
if (nextop == LOAD_FAST) {
make_super_instruction(inst, &b->b_instr[i + 1], LOAD_FAST_LOAD_FAST);
}
break;
case STORE_FAST:
switch (nextop) {
case LOAD_FAST:
make_super_instruction(inst, &b->b_instr[i + 1], STORE_FAST_LOAD_FAST);
break;
case STORE_FAST:
make_super_instruction(inst, &b->b_instr[i + 1], STORE_FAST_STORE_FAST);
break;
}
break;
}
}
}
int res = remove_redundant_nops(g);
assert(no_redundant_nops(g));
return res;
}
#define NOT_LOCAL -1
#define DUMMY_INSTR -1
typedef struct {
// Index of instruction that produced the reference or DUMMY_INSTR.
int instr;
// The local to which the reference refers or NOT_LOCAL.
int local;
} ref;
typedef struct {
ref *refs;
Py_ssize_t size;
Py_ssize_t capacity;
} ref_stack;
static int
ref_stack_push(ref_stack *stack, ref r)
{
if (stack->size == stack->capacity) {
Py_ssize_t new_cap = Py_MAX(32, stack->capacity * 2);
ref *refs = PyMem_Realloc(stack->refs, sizeof(*stack->refs) * new_cap);
if (refs == NULL) {
PyErr_NoMemory();
return -1;
}
stack->refs = refs;
stack->capacity = new_cap;
}
stack->refs[stack->size] = r;
stack->size++;
return 0;
}
static ref
ref_stack_pop(ref_stack *stack)
{
assert(stack->size > 0);
stack->size--;
ref r = stack->refs[stack->size];
return r;
}
static void
ref_stack_swap_top(ref_stack *stack, Py_ssize_t off)
{
Py_ssize_t idx = stack->size - off;
assert(idx >= 0 && idx < stack->size);
ref tmp = stack->refs[idx];
stack->refs[idx] = stack->refs[stack->size - 1];
stack->refs[stack->size - 1] = tmp;
}
static ref
ref_stack_at(ref_stack *stack, Py_ssize_t idx)
{
assert(idx >= 0 && idx < stack->size);
return stack->refs[idx];
}
static void
ref_stack_clear(ref_stack *stack)
{
stack->size = 0;
}
static void
ref_stack_fini(ref_stack *stack)
{
if (stack->refs != NULL) {
PyMem_Free(stack->refs);
}
stack->refs = NULL;
stack->capacity = 0;
stack->size = 0;
}
typedef enum {
// The loaded reference is still on the stack when the local is killed
SUPPORT_KILLED = 1,
// The loaded reference is stored into a local
STORED_AS_LOCAL = 2,
// The loaded reference is still on the stack at the end of the basic block
REF_UNCONSUMED = 4,
} LoadFastInstrFlag;
static void
kill_local(uint8_t *instr_flags, ref_stack *refs, int local)
{
for (Py_ssize_t i = 0; i < refs->size; i++) {
ref r = ref_stack_at(refs, i);
if (r.local == local) {
assert(r.instr >= 0);
instr_flags[r.instr] |= SUPPORT_KILLED;
}
}
}
static void
store_local(uint8_t *instr_flags, ref_stack *refs, int local, ref r)
{
kill_local(instr_flags, refs, local);
if (r.instr != DUMMY_INSTR) {
instr_flags[r.instr] |= STORED_AS_LOCAL;
}
}
static void
load_fast_push_block(basicblock ***sp, basicblock *target,
Py_ssize_t start_depth)
{
assert(target->b_startdepth >= 0 && target->b_startdepth == start_depth);
if (!target->b_visited) {
target->b_visited = 1;
*(*sp)++ = target;
}
}
/*
* Strength reduce LOAD_FAST{_LOAD_FAST} instructions into faster variants that
* load borrowed references onto the operand stack.
*
* This is only safe when we can prove that the reference in the frame outlives
* the borrowed reference produced by the instruction. We make this tractable
* by enforcing the following lifetimes:
*
* 1. Borrowed references loaded onto the operand stack live until the end of
* the instruction that consumes them from the stack. Any borrowed
* references that would escape into the heap (e.g. into frame objects or
* generators) are converted into new, strong references.
*
* 2. Locals live until they are either killed by an instruction
* (e.g. STORE_FAST) or the frame is unwound. Any local that is overwritten
* via `f_locals` is added to a tuple owned by the frame object.
*
* To simplify the problem of detecting which supporting references in the
* frame are killed by instructions that overwrite locals, we only allow
* borrowed references to be stored as a local in the frame if they were passed
* as an argument. {RETURN,YIELD}_VALUE convert borrowed references into new,
* strong references.
*
* Using the above, we can optimize any LOAD_FAST{_LOAD_FAST} instructions
* that meet the following criteria:
*
* 1. The produced reference must be consumed from the stack before the
* supporting reference in the frame is killed.
*
* 2. The produced reference cannot be stored as a local.
*
* We use abstract interpretation to identify instructions that meet these
* criteria. For each basic block, we simulate the effect the bytecode has on a
* stack of abstract references and note any instructions that violate the
* criteria above. Once we've processed all the instructions in a block, any
* non-violating LOAD_FAST{_LOAD_FAST} can be optimized.
*/
static int
optimize_load_fast(cfg_builder *g)
{
int status;
ref_stack refs = {0};
int max_instrs = 0;
basicblock *entryblock = g->g_entryblock;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
max_instrs = Py_MAX(max_instrs, b->b_iused);
}
size_t instr_flags_size = max_instrs * sizeof(uint8_t);
uint8_t *instr_flags = PyMem_Malloc(instr_flags_size);
if (instr_flags == NULL) {
PyErr_NoMemory();
return ERROR;
}
basicblock **blocks = make_cfg_traversal_stack(entryblock);
if (blocks == NULL) {
status = ERROR;
goto done;
}
basicblock **sp = blocks;
*sp = entryblock;
sp++;
entryblock->b_startdepth = 0;
entryblock->b_visited = 1;
#define PUSH_REF(instr, local) \
do { \
if (ref_stack_push(&refs, (ref){(instr), (local)}) < 0) { \
status = ERROR; \
goto done; \
} \
} while(0)
while (sp != blocks) {
basicblock *block = *--sp;
assert(block->b_startdepth > -1);
// Reset per-block state.
memset(instr_flags, 0, block->b_iused * sizeof(*instr_flags));
// Reset the stack of refs. We don't track references on the stack
// across basic blocks, but the bytecode will expect their
// presence. Add dummy references as necessary.
ref_stack_clear(&refs);
for (int i = 0; i < block->b_startdepth; i++) {
PUSH_REF(DUMMY_INSTR, NOT_LOCAL);
}
for (int i = 0; i < block->b_iused; i++) {
cfg_instr *instr = &block->b_instr[i];
int opcode = instr->i_opcode;
int oparg = instr->i_oparg;
assert(opcode != EXTENDED_ARG);
switch (opcode) {
// Opcodes that load and store locals
case DELETE_FAST: {
kill_local(instr_flags, &refs, oparg);
break;
}
case LOAD_FAST: {
PUSH_REF(i, oparg);
break;
}
case LOAD_FAST_AND_CLEAR: {
kill_local(instr_flags, &refs, oparg);
PUSH_REF(i, oparg);
break;
}
case LOAD_FAST_LOAD_FAST: {
PUSH_REF(i, oparg >> 4);
PUSH_REF(i, oparg & 15);
break;
}
case STORE_FAST: {
ref r = ref_stack_pop(&refs);
store_local(instr_flags, &refs, oparg, r);
break;
}
case STORE_FAST_LOAD_FAST: {
// STORE_FAST
ref r = ref_stack_pop(&refs);
store_local(instr_flags, &refs, oparg >> 4, r);
// LOAD_FAST
PUSH_REF(i, oparg & 15);
break;
}
case STORE_FAST_STORE_FAST: {
// STORE_FAST
ref r = ref_stack_pop(&refs);
store_local(instr_flags, &refs, oparg >> 4, r);
// STORE_FAST
r = ref_stack_pop(&refs);
store_local(instr_flags, &refs, oparg & 15, r);
break;
}
// Opcodes that shuffle values on the stack
case COPY: {
assert(oparg > 0);
Py_ssize_t idx = refs.size - oparg;
ref r = ref_stack_at(&refs, idx);
PUSH_REF(r.instr, r.local);
break;
}
case SWAP: {
assert(oparg >= 2);
ref_stack_swap_top(&refs, oparg);
break;
}
// We treat opcodes that do not consume all of their inputs on
// a case by case basis, as we have no generic way of knowing
// how many inputs should be left on the stack.
// Opcodes that consume no inputs
case FORMAT_SIMPLE:
case GET_ANEXT:
case GET_LEN:
case GET_YIELD_FROM_ITER:
case IMPORT_FROM:
case MATCH_KEYS:
case MATCH_MAPPING:
case MATCH_SEQUENCE:
case WITH_EXCEPT_START: {
int num_popped = _PyOpcode_num_popped(opcode, oparg);
int num_pushed = _PyOpcode_num_pushed(opcode, oparg);
int net_pushed = num_pushed - num_popped;
assert(net_pushed >= 0);
for (int i = 0; i < net_pushed; i++) {
PUSH_REF(i, NOT_LOCAL);
}
break;
}
// Opcodes that consume some inputs and push no new values
case DICT_MERGE:
case DICT_UPDATE:
case LIST_APPEND:
case LIST_EXTEND:
case MAP_ADD:
case RERAISE:
case SET_ADD:
case SET_UPDATE: {
int num_popped = _PyOpcode_num_popped(opcode, oparg);
int num_pushed = _PyOpcode_num_pushed(opcode, oparg);
int net_popped = num_popped - num_pushed;
assert(net_popped > 0);
for (int i = 0; i < net_popped; i++) {
ref_stack_pop(&refs);
}
break;
}
case END_SEND:
case SET_FUNCTION_ATTRIBUTE: {
assert(_PyOpcode_num_popped(opcode, oparg) == 2);
assert(_PyOpcode_num_pushed(opcode, oparg) == 1);
ref tos = ref_stack_pop(&refs);
ref_stack_pop(&refs);
PUSH_REF(tos.instr, tos.local);
break;
}
// Opcodes that consume some inputs and push new values
case CHECK_EXC_MATCH: {
ref_stack_pop(&refs);
PUSH_REF(i, NOT_LOCAL);
break;
}
case FOR_ITER: {
load_fast_push_block(&sp, instr->i_target, refs.size + 1);
PUSH_REF(i, NOT_LOCAL);
break;
}
case LOAD_ATTR:
case LOAD_SUPER_ATTR: {
ref self = ref_stack_pop(&refs);
if (opcode == LOAD_SUPER_ATTR) {
ref_stack_pop(&refs);
ref_stack_pop(&refs);
}
PUSH_REF(i, NOT_LOCAL);
if (oparg & 1) {
// A method call; conservatively assume that self is pushed
// back onto the stack
PUSH_REF(self.instr, self.local);
}
break;
}
case LOAD_SPECIAL:
case PUSH_EXC_INFO: {
ref tos = ref_stack_pop(&refs);
PUSH_REF(i, NOT_LOCAL);
PUSH_REF(tos.instr, tos.local);
break;
}
case SEND: {
load_fast_push_block(&sp, instr->i_target, refs.size);
ref_stack_pop(&refs);
PUSH_REF(i, NOT_LOCAL);
break;
}
// Opcodes that consume all of their inputs
default: {
int num_popped = _PyOpcode_num_popped(opcode, oparg);
int num_pushed = _PyOpcode_num_pushed(opcode, oparg);
if (HAS_TARGET(instr->i_opcode)) {
load_fast_push_block(&sp, instr->i_target, refs.size - num_popped + num_pushed);
}
if (!IS_BLOCK_PUSH_OPCODE(instr->i_opcode)) {
// Block push opcodes only affect the stack when jumping
// to the target.
for (int j = 0; j < num_popped; j++) {
ref_stack_pop(&refs);
}
for (int j = 0; j < num_pushed; j++) {
PUSH_REF(i, NOT_LOCAL);
}
}
break;
}
}
}
// Push fallthrough block
cfg_instr *term = basicblock_last_instr(block);
if (term != NULL && block->b_next != NULL &&
!(IS_UNCONDITIONAL_JUMP_OPCODE(term->i_opcode) ||
IS_SCOPE_EXIT_OPCODE(term->i_opcode))) {
assert(BB_HAS_FALLTHROUGH(block));
load_fast_push_block(&sp, block->b_next, refs.size);
}
// Mark instructions that produce values that are on the stack at the
// end of the basic block
for (Py_ssize_t i = 0; i < refs.size; i++) {
ref r = ref_stack_at(&refs, i);
if (r.instr != -1) {
instr_flags[r.instr] |= REF_UNCONSUMED;
}
}
// Optimize instructions
for (int i = 0; i < block->b_iused; i++) {
if (!instr_flags[i]) {
cfg_instr *instr = &block->b_instr[i];
switch (instr->i_opcode) {
case LOAD_FAST:
instr->i_opcode = LOAD_FAST_BORROW;
break;
case LOAD_FAST_LOAD_FAST:
instr->i_opcode = LOAD_FAST_BORROW_LOAD_FAST_BORROW;
break;
default:
break;
}
}
}
}
#undef PUSH_REF
status = SUCCESS;
done:
ref_stack_fini(&refs);
PyMem_Free(instr_flags);
PyMem_Free(blocks);
return status;
}
// helper functions for add_checks_for_loads_of_unknown_variables
static inline void
maybe_push(basicblock *b, uint64_t unsafe_mask, basicblock ***sp)
{
// Push b if the unsafe mask is giving us any new information.
// To avoid overflowing the stack, only allow each block once.
// Use b->b_visited=1 to mean that b is currently on the stack.
uint64_t both = b->b_unsafe_locals_mask | unsafe_mask;
if (b->b_unsafe_locals_mask != both) {
b->b_unsafe_locals_mask = both;
// More work left to do.
if (!b->b_visited) {
// not on the stack, so push it.
*(*sp)++ = b;
b->b_visited = 1;
}
}
}
static void
scan_block_for_locals(basicblock *b, basicblock ***sp)
{
// bit i is set if local i is potentially uninitialized
uint64_t unsafe_mask = b->b_unsafe_locals_mask;
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
assert(instr->i_opcode != EXTENDED_ARG);
if (instr->i_except != NULL) {
maybe_push(instr->i_except, unsafe_mask, sp);
}
if (instr->i_oparg >= 64) {
continue;
}
assert(instr->i_oparg >= 0);
uint64_t bit = (uint64_t)1 << instr->i_oparg;
switch (instr->i_opcode) {
case DELETE_FAST:
case LOAD_FAST_AND_CLEAR:
case STORE_FAST_MAYBE_NULL:
unsafe_mask |= bit;
break;
case STORE_FAST:
unsafe_mask &= ~bit;
break;
case LOAD_FAST_CHECK:
// If this doesn't raise, then the local is defined.
unsafe_mask &= ~bit;
break;
case LOAD_FAST:
if (unsafe_mask & bit) {
instr->i_opcode = LOAD_FAST_CHECK;
}
unsafe_mask &= ~bit;
break;
}
}
if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
maybe_push(b->b_next, unsafe_mask, sp);
}
cfg_instr *last = basicblock_last_instr(b);
if (last && is_jump(last)) {
assert(last->i_target != NULL);
maybe_push(last->i_target, unsafe_mask, sp);
}
}
static int
fast_scan_many_locals(basicblock *entryblock, int nlocals)
{
assert(nlocals > 64);
Py_ssize_t *states = PyMem_Calloc(nlocals - 64, sizeof(Py_ssize_t));
if (states == NULL) {
PyErr_NoMemory();
return ERROR;
}
Py_ssize_t blocknum = 0;
// state[i - 64] == blocknum if local i is guaranteed to
// be initialized, i.e., if it has had a previous LOAD_FAST or
// STORE_FAST within that basicblock (not followed by
// DELETE_FAST/LOAD_FAST_AND_CLEAR/STORE_FAST_MAYBE_NULL).
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
blocknum++;
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
assert(instr->i_opcode != EXTENDED_ARG);
int arg = instr->i_oparg;
if (arg < 64) {
continue;
}
assert(arg >= 0);
switch (instr->i_opcode) {
case DELETE_FAST:
case LOAD_FAST_AND_CLEAR:
case STORE_FAST_MAYBE_NULL:
states[arg - 64] = blocknum - 1;
break;
case STORE_FAST:
states[arg - 64] = blocknum;
break;
case LOAD_FAST:
if (states[arg - 64] != blocknum) {
instr->i_opcode = LOAD_FAST_CHECK;
}
states[arg - 64] = blocknum;
break;
Py_UNREACHABLE();
}
}
}
PyMem_Free(states);
return SUCCESS;
}
static int
remove_unused_consts(basicblock *entryblock, PyObject *consts)
{
assert(PyList_CheckExact(consts));
Py_ssize_t nconsts = PyList_GET_SIZE(consts);
if (nconsts == 0) {
return SUCCESS; /* nothing to do */
}
Py_ssize_t *index_map = NULL;
Py_ssize_t *reverse_index_map = NULL;
int err = ERROR;
index_map = PyMem_Malloc(nconsts * sizeof(Py_ssize_t));
if (index_map == NULL) {
goto end;
}
for (Py_ssize_t i = 1; i < nconsts; i++) {
index_map[i] = -1;
}
// The first constant may be docstring; keep it always.
index_map[0] = 0;
/* mark used consts */
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
int opcode = b->b_instr[i].i_opcode;
if (OPCODE_HAS_CONST(opcode)) {
int index = b->b_instr[i].i_oparg;
index_map[index] = index;
}
}
}
/* now index_map[i] == i if consts[i] is used, -1 otherwise */
/* condense consts */
Py_ssize_t n_used_consts = 0;
for (Py_ssize_t i = 0; i < nconsts; i++) {
if (index_map[i] != -1) {
assert(index_map[i] == i);
index_map[n_used_consts++] = index_map[i];
}
}
if (n_used_consts == nconsts) {
/* nothing to do */
err = SUCCESS;
goto end;
}
/* move all used consts to the beginning of the consts list */
assert(n_used_consts < nconsts);
for (Py_ssize_t i = 0; i < n_used_consts; i++) {
Py_ssize_t old_index = index_map[i];
assert(i <= old_index && old_index < nconsts);
if (i != old_index) {
PyObject *value = PyList_GET_ITEM(consts, index_map[i]);
assert(value != NULL);
PyList_SetItem(consts, i, Py_NewRef(value));
}
}
/* truncate the consts list at its new size */
if (PyList_SetSlice(consts, n_used_consts, nconsts, NULL) < 0) {
goto end;
}
/* adjust const indices in the bytecode */
reverse_index_map = PyMem_Malloc(nconsts * sizeof(Py_ssize_t));
if (reverse_index_map == NULL) {
goto end;
}
for (Py_ssize_t i = 0; i < nconsts; i++) {
reverse_index_map[i] = -1;
}
for (Py_ssize_t i = 0; i < n_used_consts; i++) {
assert(index_map[i] != -1);
assert(reverse_index_map[index_map[i]] == -1);
reverse_index_map[index_map[i]] = i;
}
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
int opcode = b->b_instr[i].i_opcode;
if (OPCODE_HAS_CONST(opcode)) {
int index = b->b_instr[i].i_oparg;
assert(reverse_index_map[index] >= 0);
assert(reverse_index_map[index] < n_used_consts);
b->b_instr[i].i_oparg = (int)reverse_index_map[index];
}
}
}
err = SUCCESS;
end:
PyMem_Free(index_map);
PyMem_Free(reverse_index_map);
return err;
}
static int
add_checks_for_loads_of_uninitialized_variables(basicblock *entryblock,
int nlocals,
int nparams)
{
if (nlocals == 0) {
return SUCCESS;
}
if (nlocals > 64) {
// To avoid O(nlocals**2) compilation, locals beyond the first
// 64 are only analyzed one basicblock at a time: initialization
// info is not passed between basicblocks.
if (fast_scan_many_locals(entryblock, nlocals) < 0) {
return ERROR;
}
nlocals = 64;
}
basicblock **stack = make_cfg_traversal_stack(entryblock);
if (stack == NULL) {
return ERROR;
}
basicblock **sp = stack;
// First origin of being uninitialized:
// The non-parameter locals in the entry block.
uint64_t start_mask = 0;
for (int i = nparams; i < nlocals; i++) {
start_mask |= (uint64_t)1 << i;
}
maybe_push(entryblock, start_mask, &sp);
// Second origin of being uninitialized:
// There could be DELETE_FAST somewhere, so
// be sure to scan each basicblock at least once.
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
scan_block_for_locals(b, &sp);
}
// Now propagate the uncertainty from the origins we found: Use
// LOAD_FAST_CHECK for any LOAD_FAST where the local could be undefined.
while (sp > stack) {
basicblock *b = *--sp;
// mark as no longer on stack
b->b_visited = 0;
scan_block_for_locals(b, &sp);
}
PyMem_Free(stack);
return SUCCESS;
}
static int
mark_warm(basicblock *entryblock) {
basicblock **stack = make_cfg_traversal_stack(entryblock);
if (stack == NULL) {
return ERROR;
}
basicblock **sp = stack;
*sp++ = entryblock;
entryblock->b_visited = 1;
while (sp > stack) {
basicblock *b = *(--sp);
assert(!b->b_except_handler);
b->b_warm = 1;
basicblock *next = b->b_next;
if (next && BB_HAS_FALLTHROUGH(b) && !next->b_visited) {
*sp++ = next;
next->b_visited = 1;
}
for (int i=0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (is_jump(instr) && !instr->i_target->b_visited) {
*sp++ = instr->i_target;
instr->i_target->b_visited = 1;
}
}
}
PyMem_Free(stack);
return SUCCESS;
}
static int
mark_cold(basicblock *entryblock) {
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
assert(!b->b_cold && !b->b_warm);
}
if (mark_warm(entryblock) < 0) {
return ERROR;
}
basicblock **stack = make_cfg_traversal_stack(entryblock);
if (stack == NULL) {
return ERROR;
}
basicblock **sp = stack;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
if (b->b_except_handler) {
assert(!b->b_warm);
*sp++ = b;
b->b_visited = 1;
}
}
while (sp > stack) {
basicblock *b = *(--sp);
b->b_cold = 1;
basicblock *next = b->b_next;
if (next && BB_HAS_FALLTHROUGH(b)) {
if (!next->b_warm && !next->b_visited) {
*sp++ = next;
next->b_visited = 1;
}
}
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (is_jump(instr)) {
assert(i == b->b_iused - 1);
basicblock *target = b->b_instr[i].i_target;
if (!target->b_warm && !target->b_visited) {
*sp++ = target;
target->b_visited = 1;
}
}
}
}
PyMem_Free(stack);
return SUCCESS;
}
static int
push_cold_blocks_to_end(cfg_builder *g) {
basicblock *entryblock = g->g_entryblock;
if (entryblock->b_next == NULL) {
/* single basicblock, no need to reorder */
return SUCCESS;
}
RETURN_IF_ERROR(mark_cold(entryblock));
int next_lbl = get_max_label(g->g_entryblock) + 1;
/* If we have a cold block with fallthrough to a warm block, add */
/* an explicit jump instead of fallthrough */
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
if (b->b_cold && BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_next->b_warm) {
basicblock *explicit_jump = cfg_builder_new_block(g);
if (explicit_jump == NULL) {
return ERROR;
}
if (!IS_LABEL(b->b_next->b_label)) {
b->b_next->b_label.id = next_lbl++;
}
basicblock_addop(explicit_jump, JUMP_NO_INTERRUPT, b->b_next->b_label.id,
NO_LOCATION);
explicit_jump->b_cold = 1;
explicit_jump->b_next = b->b_next;
explicit_jump->b_predecessors = 1;
b->b_next = explicit_jump;
/* set target */
cfg_instr *last = basicblock_last_instr(explicit_jump);
last->i_target = explicit_jump->b_next;
}
}
assert(!entryblock->b_cold); /* First block can't be cold */
basicblock *cold_blocks = NULL;
basicblock *cold_blocks_tail = NULL;
basicblock *b = entryblock;
while(b->b_next) {
assert(!b->b_cold);
while (b->b_next && !b->b_next->b_cold) {
b = b->b_next;
}
if (b->b_next == NULL) {
/* no more cold blocks */
break;
}
/* b->b_next is the beginning of a cold streak */
assert(!b->b_cold && b->b_next->b_cold);
basicblock *b_end = b->b_next;
while (b_end->b_next && b_end->b_next->b_cold) {
b_end = b_end->b_next;
}
/* b_end is the end of the cold streak */
assert(b_end && b_end->b_cold);
assert(b_end->b_next == NULL || !b_end->b_next->b_cold);
if (cold_blocks == NULL) {
cold_blocks = b->b_next;
}
else {
cold_blocks_tail->b_next = b->b_next;
}
cold_blocks_tail = b_end;
b->b_next = b_end->b_next;
b_end->b_next = NULL;
}
assert(b != NULL && b->b_next == NULL);
b->b_next = cold_blocks;
if (cold_blocks != NULL) {
RETURN_IF_ERROR(remove_redundant_nops_and_jumps(g));
}
return SUCCESS;
}
static int
convert_pseudo_conditional_jumps(cfg_builder *g)
{
basicblock *entryblock = g->g_entryblock;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (instr->i_opcode == JUMP_IF_FALSE || instr->i_opcode == JUMP_IF_TRUE) {
assert(i == b->b_iused - 1);
instr->i_opcode = instr->i_opcode == JUMP_IF_FALSE ?
POP_JUMP_IF_FALSE : POP_JUMP_IF_TRUE;
location loc = instr->i_loc;
cfg_instr copy = {
.i_opcode = COPY,
.i_oparg = 1,
.i_loc = loc,
.i_target = NULL,
};
RETURN_IF_ERROR(basicblock_insert_instruction(b, i++, ©));
cfg_instr to_bool = {
.i_opcode = TO_BOOL,
.i_oparg = 0,
.i_loc = loc,
.i_target = NULL,
};
RETURN_IF_ERROR(basicblock_insert_instruction(b, i++, &to_bool));
}
}
}
return SUCCESS;
}
static int
convert_pseudo_ops(cfg_builder *g)
{
basicblock *entryblock = g->g_entryblock;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (is_block_push(instr)) {
INSTR_SET_OP0(instr, NOP);
}
else if (instr->i_opcode == LOAD_CLOSURE) {
assert(is_pseudo_target(LOAD_CLOSURE, LOAD_FAST));
instr->i_opcode = LOAD_FAST;
}
else if (instr->i_opcode == STORE_FAST_MAYBE_NULL) {
assert(is_pseudo_target(STORE_FAST_MAYBE_NULL, STORE_FAST));
instr->i_opcode = STORE_FAST;
}
}
}
return remove_redundant_nops_and_jumps(g);
}
static inline bool
is_exit_or_eval_check_without_lineno(basicblock *b) {
if (basicblock_exits_scope(b) || basicblock_has_eval_break(b)) {
return basicblock_has_no_lineno(b);
}
else {
return false;
}
}
/* PEP 626 mandates that the f_lineno of a frame is correct
* after a frame terminates. It would be prohibitively expensive
* to continuously update the f_lineno field at runtime,
* so we make sure that all exiting instruction (raises and returns)
* have a valid line number, allowing us to compute f_lineno lazily.
* We can do this by duplicating the exit blocks without line number
* so that none have more than one predecessor. We can then safely
* copy the line number from the sole predecessor block.
*/
static int
duplicate_exits_without_lineno(cfg_builder *g)
{
int next_lbl = get_max_label(g->g_entryblock) + 1;
/* Copy all exit blocks without line number that are targets of a jump.
*/
basicblock *entryblock = g->g_entryblock;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
cfg_instr *last = basicblock_last_instr(b);
if (last == NULL) {
continue;
}
if (is_jump(last)) {
basicblock *target = next_nonempty_block(last->i_target);
if (is_exit_or_eval_check_without_lineno(target) && target->b_predecessors > 1) {
basicblock *new_target = copy_basicblock(g, target);
if (new_target == NULL) {
return ERROR;
}
new_target->b_instr[0].i_loc = last->i_loc;
last->i_target = new_target;
target->b_predecessors--;
new_target->b_predecessors = 1;
new_target->b_next = target->b_next;
new_target->b_label.id = next_lbl++;
target->b_next = new_target;
}
}
}
/* Any remaining reachable exit blocks without line number can only be reached by
* fall through, and thus can only have a single predecessor */
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
if (BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_iused > 0) {
if (is_exit_or_eval_check_without_lineno(b->b_next)) {
cfg_instr *last = basicblock_last_instr(b);
assert(last != NULL);
b->b_next->b_instr[0].i_loc = last->i_loc;
}
}
}
return SUCCESS;
}
/* If an instruction has no line number, but it's predecessor in the BB does,
* then copy the line number. If a successor block has no line number, and only
* one predecessor, then inherit the line number.
* This ensures that all exit blocks (with one predecessor) receive a line number.
* Also reduces the size of the line number table,
* but has no impact on the generated line number events.
*/
static void
propagate_line_numbers(basicblock *entryblock) {
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
cfg_instr *last = basicblock_last_instr(b);
if (last == NULL) {
continue;
}
location prev_location = NO_LOCATION;
for (int i = 0; i < b->b_iused; i++) {
if (b->b_instr[i].i_loc.lineno == NO_LOCATION.lineno) {
b->b_instr[i].i_loc = prev_location;
}
else {
prev_location = b->b_instr[i].i_loc;
}
}
if (BB_HAS_FALLTHROUGH(b) && b->b_next->b_predecessors == 1) {
if (b->b_next->b_iused > 0) {
if (b->b_next->b_instr[0].i_loc.lineno == NO_LOCATION.lineno) {
b->b_next->b_instr[0].i_loc = prev_location;
}
}
}
if (is_jump(last)) {
basicblock *target = last->i_target;
if (target->b_predecessors == 1) {
if (target->b_instr[0].i_loc.lineno == NO_LOCATION.lineno) {
target->b_instr[0].i_loc = prev_location;
}
}
}
}
}
static int
resolve_line_numbers(cfg_builder *g, int firstlineno)
{
RETURN_IF_ERROR(duplicate_exits_without_lineno(g));
propagate_line_numbers(g->g_entryblock);
return SUCCESS;
}
int
_PyCfg_OptimizeCodeUnit(cfg_builder *g, PyObject *consts, PyObject *const_cache,
int nlocals, int nparams, int firstlineno)
{
assert(cfg_builder_check(g));
/** Preprocessing **/
/* Map labels to targets and mark exception handlers */
RETURN_IF_ERROR(translate_jump_labels_to_targets(g->g_entryblock));
RETURN_IF_ERROR(mark_except_handlers(g->g_entryblock));
RETURN_IF_ERROR(label_exception_targets(g->g_entryblock));
/** Optimization **/
RETURN_IF_ERROR(optimize_cfg(g, consts, const_cache, firstlineno));
RETURN_IF_ERROR(remove_unused_consts(g->g_entryblock, consts));
RETURN_IF_ERROR(
add_checks_for_loads_of_uninitialized_variables(
g->g_entryblock, nlocals, nparams));
RETURN_IF_ERROR(insert_superinstructions(g));
RETURN_IF_ERROR(push_cold_blocks_to_end(g));
RETURN_IF_ERROR(resolve_line_numbers(g, firstlineno));
// temporarily remove assert. See https://github.com/python/cpython/issues/125845
// assert(all_exits_have_lineno(g->g_entryblock));
return SUCCESS;
}
static int *
build_cellfixedoffsets(_PyCompile_CodeUnitMetadata *umd)
{
int nlocals = (int)PyDict_GET_SIZE(umd->u_varnames);
int ncellvars = (int)PyDict_GET_SIZE(umd->u_cellvars);
int nfreevars = (int)PyDict_GET_SIZE(umd->u_freevars);
int noffsets = ncellvars + nfreevars;
int *fixed = PyMem_New(int, noffsets);
if (fixed == NULL) {
PyErr_NoMemory();
return NULL;
}
for (int i = 0; i < noffsets; i++) {
fixed[i] = nlocals + i;
}
PyObject *varname, *cellindex;
Py_ssize_t pos = 0;
while (PyDict_Next(umd->u_cellvars, &pos, &varname, &cellindex)) {
PyObject *varindex;
if (PyDict_GetItemRef(umd->u_varnames, varname, &varindex) < 0) {
goto error;
}
if (varindex == NULL) {
continue;
}
int argoffset = PyLong_AsInt(varindex);
Py_DECREF(varindex);
if (argoffset == -1 && PyErr_Occurred()) {
goto error;
}
int oldindex = PyLong_AsInt(cellindex);
if (oldindex == -1 && PyErr_Occurred()) {
goto error;
}
fixed[oldindex] = argoffset;
}
return fixed;
error:
PyMem_Free(fixed);
return NULL;
}
#define IS_GENERATOR(CF) \
((CF) & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR))
static int
insert_prefix_instructions(_PyCompile_CodeUnitMetadata *umd, basicblock *entryblock,
int *fixed, int nfreevars, int code_flags)
{
assert(umd->u_firstlineno > 0);
/* Add the generator prefix instructions. */
if (IS_GENERATOR(code_flags)) {
/* Note that RETURN_GENERATOR + POP_TOP have a net stack effect
* of 0. This is because RETURN_GENERATOR pushes an element
* with _PyFrame_StackPush before switching stacks.
*/
location loc = LOCATION(umd->u_firstlineno, umd->u_firstlineno, -1, -1);
cfg_instr make_gen = {
.i_opcode = RETURN_GENERATOR,
.i_oparg = 0,
.i_loc = loc,
.i_target = NULL,
};
RETURN_IF_ERROR(basicblock_insert_instruction(entryblock, 0, &make_gen));
cfg_instr pop_top = {
.i_opcode = POP_TOP,
.i_oparg = 0,
.i_loc = loc,
.i_target = NULL,
};
RETURN_IF_ERROR(basicblock_insert_instruction(entryblock, 1, &pop_top));
}
/* Set up cells for any variable that escapes, to be put in a closure. */
const int ncellvars = (int)PyDict_GET_SIZE(umd->u_cellvars);
if (ncellvars) {
// umd->u_cellvars has the cells out of order so we sort them
// before adding the MAKE_CELL instructions. Note that we
// adjust for arg cells, which come first.
const int nvars = ncellvars + (int)PyDict_GET_SIZE(umd->u_varnames);
int *sorted = PyMem_RawCalloc(nvars, sizeof(int));
if (sorted == NULL) {
PyErr_NoMemory();
return ERROR;
}
for (int i = 0; i < ncellvars; i++) {
sorted[fixed[i]] = i + 1;
}
for (int i = 0, ncellsused = 0; ncellsused < ncellvars; i++) {
int oldindex = sorted[i] - 1;
if (oldindex == -1) {
continue;
}
cfg_instr make_cell = {
.i_opcode = MAKE_CELL,
// This will get fixed in offset_derefs().
.i_oparg = oldindex,
.i_loc = NO_LOCATION,
.i_target = NULL,
};
if (basicblock_insert_instruction(entryblock, ncellsused, &make_cell) < 0) {
PyMem_RawFree(sorted);
return ERROR;
}
ncellsused += 1;
}
PyMem_RawFree(sorted);
}
if (nfreevars) {
cfg_instr copy_frees = {
.i_opcode = COPY_FREE_VARS,
.i_oparg = nfreevars,
.i_loc = NO_LOCATION,
.i_target = NULL,
};
RETURN_IF_ERROR(basicblock_insert_instruction(entryblock, 0, ©_frees));
}
return SUCCESS;
}
static int
fix_cell_offsets(_PyCompile_CodeUnitMetadata *umd, basicblock *entryblock, int *fixedmap)
{
int nlocals = (int)PyDict_GET_SIZE(umd->u_varnames);
int ncellvars = (int)PyDict_GET_SIZE(umd->u_cellvars);
int nfreevars = (int)PyDict_GET_SIZE(umd->u_freevars);
int noffsets = ncellvars + nfreevars;
// First deal with duplicates (arg cells).
int numdropped = 0;
for (int i = 0; i < noffsets ; i++) {
if (fixedmap[i] == i + nlocals) {
fixedmap[i] -= numdropped;
}
else {
// It was a duplicate (cell/arg).
numdropped += 1;
}
}
// Then update offsets, either relative to locals or by cell2arg.
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *inst = &b->b_instr[i];
// This is called before extended args are generated.
assert(inst->i_opcode != EXTENDED_ARG);
int oldoffset = inst->i_oparg;
switch(inst->i_opcode) {
case MAKE_CELL:
case LOAD_CLOSURE:
case LOAD_DEREF:
case STORE_DEREF:
case DELETE_DEREF:
case LOAD_FROM_DICT_OR_DEREF:
assert(oldoffset >= 0);
assert(oldoffset < noffsets);
assert(fixedmap[oldoffset] >= 0);
inst->i_oparg = fixedmap[oldoffset];
}
}
}
return numdropped;
}
static int
prepare_localsplus(_PyCompile_CodeUnitMetadata *umd, cfg_builder *g, int code_flags)
{
assert(PyDict_GET_SIZE(umd->u_varnames) < INT_MAX);
assert(PyDict_GET_SIZE(umd->u_cellvars) < INT_MAX);
assert(PyDict_GET_SIZE(umd->u_freevars) < INT_MAX);
int nlocals = (int)PyDict_GET_SIZE(umd->u_varnames);
int ncellvars = (int)PyDict_GET_SIZE(umd->u_cellvars);
int nfreevars = (int)PyDict_GET_SIZE(umd->u_freevars);
assert(INT_MAX - nlocals - ncellvars > 0);
assert(INT_MAX - nlocals - ncellvars - nfreevars > 0);
int nlocalsplus = nlocals + ncellvars + nfreevars;
int* cellfixedoffsets = build_cellfixedoffsets(umd);
if (cellfixedoffsets == NULL) {
return ERROR;
}
// This must be called before fix_cell_offsets().
if (insert_prefix_instructions(umd, g->g_entryblock, cellfixedoffsets, nfreevars, code_flags)) {
PyMem_Free(cellfixedoffsets);
return ERROR;
}
int numdropped = fix_cell_offsets(umd, g->g_entryblock, cellfixedoffsets);
PyMem_Free(cellfixedoffsets); // At this point we're done with it.
cellfixedoffsets = NULL;
if (numdropped < 0) {
return ERROR;
}
nlocalsplus -= numdropped;
return nlocalsplus;
}
cfg_builder *
_PyCfg_FromInstructionSequence(_PyInstructionSequence *seq)
{
if (_PyInstructionSequence_ApplyLabelMap(seq) < 0) {
return NULL;
}
cfg_builder *g = _PyCfgBuilder_New();
if (g == NULL) {
return NULL;
}
for (int i = 0; i < seq->s_used; i++) {
seq->s_instrs[i].i_target = 0;
}
for (int i = 0; i < seq->s_used; i++) {
_PyInstruction *instr = &seq->s_instrs[i];
if (HAS_TARGET(instr->i_opcode)) {
assert(instr->i_oparg >= 0 && instr->i_oparg < seq->s_used);
seq->s_instrs[instr->i_oparg].i_target = 1;
}
}
int offset = 0;
for (int i = 0; i < seq->s_used; i++) {
_PyInstruction *instr = &seq->s_instrs[i];
if (instr->i_opcode == ANNOTATIONS_PLACEHOLDER) {
if (seq->s_annotations_code != NULL) {
assert(seq->s_annotations_code->s_labelmap_size == 0
&& seq->s_annotations_code->s_nested == NULL);
for (int j = 0; j < seq->s_annotations_code->s_used; j++) {
_PyInstruction *ann_instr = &seq->s_annotations_code->s_instrs[j];
assert(!HAS_TARGET(ann_instr->i_opcode));
if (_PyCfgBuilder_Addop(g, ann_instr->i_opcode, ann_instr->i_oparg, ann_instr->i_loc) < 0) {
goto error;
}
}
offset += seq->s_annotations_code->s_used - 1;
}
else {
offset -= 1;
}
continue;
}
if (instr->i_target) {
jump_target_label lbl_ = {i + offset};
if (_PyCfgBuilder_UseLabel(g, lbl_) < 0) {
goto error;
}
}
int opcode = instr->i_opcode;
int oparg = instr->i_oparg;
if (HAS_TARGET(opcode)) {
oparg += offset;
}
if (_PyCfgBuilder_Addop(g, opcode, oparg, instr->i_loc) < 0) {
goto error;
}
}
if (_PyCfgBuilder_CheckSize(g) < 0) {
goto error;
}
return g;
error:
_PyCfgBuilder_Free(g);
return NULL;
}
int
_PyCfg_ToInstructionSequence(cfg_builder *g, _PyInstructionSequence *seq)
{
int lbl = 0;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
b->b_label = (jump_target_label){lbl};
lbl += 1;
}
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
RETURN_IF_ERROR(_PyInstructionSequence_UseLabel(seq, b->b_label.id));
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (HAS_TARGET(instr->i_opcode)) {
/* Set oparg to the label id (it will later be mapped to an offset) */
instr->i_oparg = instr->i_target->b_label.id;
}
RETURN_IF_ERROR(
_PyInstructionSequence_Addop(
seq, instr->i_opcode, instr->i_oparg, instr->i_loc));
_PyExceptHandlerInfo *hi = &seq->s_instrs[seq->s_used-1].i_except_handler_info;
if (instr->i_except != NULL) {
hi->h_label = instr->i_except->b_label.id;
hi->h_startdepth = instr->i_except->b_startdepth;
hi->h_preserve_lasti = instr->i_except->b_preserve_lasti;
}
else {
hi->h_label = -1;
}
}
}
if (_PyInstructionSequence_ApplyLabelMap(seq) < 0) {
return ERROR;
}
return SUCCESS;
}
int
_PyCfg_OptimizedCfgToInstructionSequence(cfg_builder *g,
_PyCompile_CodeUnitMetadata *umd, int code_flags,
int *stackdepth, int *nlocalsplus,
_PyInstructionSequence *seq)
{
RETURN_IF_ERROR(convert_pseudo_conditional_jumps(g));
*stackdepth = calculate_stackdepth(g);
if (*stackdepth < 0) {
return ERROR;
}
/* prepare_localsplus adds instructions for generators that push
* and pop an item on the stack. This assertion makes sure there
* is space on the stack for that.
* It should always be true, because a generator must have at
* least one expression or call to INTRINSIC_STOPITERATION_ERROR,
* which requires stackspace.
*/
assert(!(IS_GENERATOR(code_flags) && *stackdepth == 0));
*nlocalsplus = prepare_localsplus(umd, g, code_flags);
if (*nlocalsplus < 0) {
return ERROR;
}
RETURN_IF_ERROR(convert_pseudo_ops(g));
/* Order of basic blocks must have been determined by now */
RETURN_IF_ERROR(normalize_jumps(g));
assert(no_redundant_jumps(g));
/* Can't modify the bytecode after inserting instructions that produce
* borrowed references.
*/
RETURN_IF_ERROR(optimize_load_fast(g));
/* Can't modify the bytecode after computing jump offsets. */
if (_PyCfg_ToInstructionSequence(g, seq) < 0) {
return ERROR;
}
return SUCCESS;
}
/* This is used by _PyCompile_Assemble to fill in the jump and exception
* targets in a synthetic CFG (which is not the output of the builtin compiler).
*/
int
_PyCfg_JumpLabelsToTargets(cfg_builder *g)
{
RETURN_IF_ERROR(translate_jump_labels_to_targets(g->g_entryblock));
RETURN_IF_ERROR(label_exception_targets(g->g_entryblock));
return SUCCESS;
}
/* Exported API functions */
int
PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
{
stack_effects effs;
if (get_stack_effects(opcode, oparg, jump, &effs) < 0) {
return PY_INVALID_STACK_EFFECT;
}
return effs.net;
}
int
PyCompile_OpcodeStackEffect(int opcode, int oparg)
{
stack_effects effs;
if (get_stack_effects(opcode, oparg, -1, &effs) < 0) {
return PY_INVALID_STACK_EFFECT;
}
return effs.net;
}
/* Access to compiler optimizations for unit tests.
* _PyCompile_OptimizeCfg takes an instruction list, constructs
* a CFG, optimizes it and converts back to an instruction list.
*/
static PyObject *
cfg_to_instruction_sequence(cfg_builder *g)
{
_PyInstructionSequence *seq = (_PyInstructionSequence *)_PyInstructionSequence_New();
if (seq == NULL) {
return NULL;
}
if (_PyCfg_ToInstructionSequence(g, seq) < 0) {
PyInstructionSequence_Fini(seq);
return NULL;
}
return (PyObject*)seq;
}
PyObject *
_PyCompile_OptimizeCfg(PyObject *seq, PyObject *consts, int nlocals)
{
if (!_PyInstructionSequence_Check(seq)) {
PyErr_SetString(PyExc_ValueError, "expected an instruction sequence");
return NULL;
}
PyObject *const_cache = PyDict_New();
if (const_cache == NULL) {
return NULL;
}
PyObject *res = NULL;
cfg_builder *g = _PyCfg_FromInstructionSequence((_PyInstructionSequence*)seq);
if (g == NULL) {
goto error;
}
int nparams = 0, firstlineno = 1;
if (_PyCfg_OptimizeCodeUnit(g, consts, const_cache, nlocals,
nparams, firstlineno) < 0) {
goto error;
}
if (calculate_stackdepth(g) == ERROR) {
goto error;
}
if (optimize_load_fast(g) != SUCCESS) {
goto error;
}
res = cfg_to_instruction_sequence(g);
error:
Py_DECREF(const_cache);
_PyCfgBuilder_Free(g);
return res;
}
|