1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "intrinsics_x86_64.h"
#include <limits>
#include "arch/x86_64/instruction_set_features_x86_64.h"
#include "art_method.h"
#include "base/bit_utils.h"
#include "code_generator_x86_64.h"
#include "entrypoints/quick/quick_entrypoints.h"
#include "heap_poisoning.h"
#include "intrinsics.h"
#include "intrinsics_utils.h"
#include "lock_word.h"
#include "mirror/array-inl.h"
#include "mirror/object_array-inl.h"
#include "mirror/reference.h"
#include "mirror/string.h"
#include "scoped_thread_state_change-inl.h"
#include "thread-current-inl.h"
#include "utils/x86_64/assembler_x86_64.h"
#include "utils/x86_64/constants_x86_64.h"
namespace art HIDDEN {
namespace x86_64 {
IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
: allocator_(codegen->GetGraph()->GetAllocator()), codegen_(codegen) {
}
X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
return down_cast<X86_64Assembler*>(codegen_->GetAssembler());
}
ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
return codegen_->GetGraph()->GetAllocator();
}
bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
Dispatch(invoke);
LocationSummary* res = invoke->GetLocations();
if (res == nullptr) {
return false;
}
return res->Intrinsified();
}
using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
class ReadBarrierSystemArrayCopySlowPathX86_64 : public SlowPathCode {
public:
explicit ReadBarrierSystemArrayCopySlowPathX86_64(HInstruction* instruction)
: SlowPathCode(instruction) {
DCHECK(gUseReadBarrier);
DCHECK(kUseBakerReadBarrier);
}
void EmitNativeCode(CodeGenerator* codegen) override {
CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
LocationSummary* locations = instruction_->GetLocations();
DCHECK(locations->CanCall());
DCHECK(instruction_->IsInvokeStaticOrDirect())
<< "Unexpected instruction in read barrier arraycopy slow path: "
<< instruction_->DebugName();
DCHECK(instruction_->GetLocations()->Intrinsified());
DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
int32_t element_size = DataType::Size(DataType::Type::kReference);
CpuRegister src_curr_addr = locations->GetTemp(0).AsRegister<CpuRegister>();
CpuRegister dst_curr_addr = locations->GetTemp(1).AsRegister<CpuRegister>();
CpuRegister src_stop_addr = locations->GetTemp(2).AsRegister<CpuRegister>();
__ Bind(GetEntryLabel());
NearLabel loop;
__ Bind(&loop);
__ movl(CpuRegister(TMP), Address(src_curr_addr, 0));
__ MaybeUnpoisonHeapReference(CpuRegister(TMP));
// TODO: Inline the mark bit check before calling the runtime?
// TMP = ReadBarrier::Mark(TMP);
// No need to save live registers; it's taken care of by the
// entrypoint. Also, there is no need to update the stack mask,
// as this runtime call will not trigger a garbage collection.
int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(TMP);
// This runtime call does not require a stack map.
x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
__ MaybePoisonHeapReference(CpuRegister(TMP));
__ movl(Address(dst_curr_addr, 0), CpuRegister(TMP));
__ addl(src_curr_addr, Immediate(element_size));
__ addl(dst_curr_addr, Immediate(element_size));
__ cmpl(src_curr_addr, src_stop_addr);
__ j(kNotEqual, &loop);
__ jmp(GetExitLabel());
}
const char* GetDescription() const override { return "ReadBarrierSystemArrayCopySlowPathX86_64"; }
private:
DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86_64);
};
#undef __
#define __ assembler->
static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresFpuRegister());
locations->SetOut(Location::RequiresRegister());
}
static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
locations->SetOut(Location::RequiresFpuRegister());
}
static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
Location input = locations->InAt(0);
Location output = locations->Out();
__ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
}
static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
Location input = locations->InAt(0);
Location output = locations->Out();
__ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
}
void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
CreateFPToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
CreateIntToFPLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
MoveFPToInt(invoke->GetLocations(), /* is64bit= */ true, GetAssembler());
}
void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
MoveIntToFP(invoke->GetLocations(), /* is64bit= */ true, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
CreateFPToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
CreateIntToFPLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
MoveFPToInt(invoke->GetLocations(), /* is64bit= */ false, GetAssembler());
}
void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
MoveIntToFP(invoke->GetLocations(), /* is64bit= */ false, GetAssembler());
}
static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
locations->SetOut(Location::SameAsFirstInput());
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
CreateIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
codegen_->GetInstructionCodegen()->Bswap(invoke->GetLocations()->Out(), DataType::Type::kInt32);
}
void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
CreateIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
codegen_->GetInstructionCodegen()->Bswap(invoke->GetLocations()->Out(), DataType::Type::kInt64);
}
void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
CreateIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
codegen_->GetInstructionCodegen()->Bswap(invoke->GetLocations()->Out(), DataType::Type::kInt16);
}
static void GenIsInfinite(LocationSummary* locations,
bool is64bit,
CodeGeneratorX86_64* codegen) {
X86_64Assembler* assembler = codegen->GetAssembler();
XmmRegister input = locations->InAt(0).AsFpuRegister<XmmRegister>();
CpuRegister output = locations->Out().AsRegister<CpuRegister>();
NearLabel done1, done2;
if (is64bit) {
double kPositiveInfinity = std::numeric_limits<double>::infinity();
double kNegativeInfinity = -1 * kPositiveInfinity;
__ xorq(output, output);
__ comisd(input, codegen->LiteralDoubleAddress(kPositiveInfinity));
__ j(kNotEqual, &done1);
__ j(kParityEven, &done2);
__ movq(output, Immediate(1));
__ jmp(&done2);
__ Bind(&done1);
__ comisd(input, codegen->LiteralDoubleAddress(kNegativeInfinity));
__ j(kNotEqual, &done2);
__ j(kParityEven, &done2);
__ movq(output, Immediate(1));
__ Bind(&done2);
} else {
float kPositiveInfinity = std::numeric_limits<float>::infinity();
float kNegativeInfinity = -1 * kPositiveInfinity;
__ xorl(output, output);
__ comiss(input, codegen->LiteralFloatAddress(kPositiveInfinity));
__ j(kNotEqual, &done1);
__ j(kParityEven, &done2);
__ movl(output, Immediate(1));
__ jmp(&done2);
__ Bind(&done1);
__ comiss(input, codegen->LiteralFloatAddress(kNegativeInfinity));
__ j(kNotEqual, &done2);
__ j(kParityEven, &done2);
__ movl(output, Immediate(1));
__ Bind(&done2);
}
}
void IntrinsicLocationsBuilderX86_64::VisitFloatIsInfinite(HInvoke* invoke) {
CreateFPToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitFloatIsInfinite(HInvoke* invoke) {
GenIsInfinite(invoke->GetLocations(), /* is64bit=*/ false, codegen_);
}
void IntrinsicLocationsBuilderX86_64::VisitDoubleIsInfinite(HInvoke* invoke) {
CreateFPToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitDoubleIsInfinite(HInvoke* invoke) {
GenIsInfinite(invoke->GetLocations(), /* is64bit=*/ true, codegen_);
}
static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresFpuRegister());
locations->SetOut(Location::RequiresFpuRegister());
}
void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
CreateFPToFPLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
LocationSummary* locations = invoke->GetLocations();
XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
GetAssembler()->sqrtsd(out, in);
}
static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator,
HInvoke* invoke,
CodeGeneratorX86_64* codegen) {
// Do we have instruction support?
if (!codegen->GetInstructionSetFeatures().HasSSE4_1()) {
return;
}
CreateFPToFPLocations(allocator, invoke);
}
static void GenSSE41FPToFPIntrinsic(HInvoke* invoke, X86_64Assembler* assembler, int round_mode) {
LocationSummary* locations = invoke->GetLocations();
DCHECK(!locations->WillCall());
XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
__ roundsd(out, in, Immediate(round_mode));
}
void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
GenSSE41FPToFPIntrinsic(invoke, GetAssembler(), 2);
}
void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
GenSSE41FPToFPIntrinsic(invoke, GetAssembler(), 1);
}
void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
GenSSE41FPToFPIntrinsic(invoke, GetAssembler(), 0);
}
static void CreateSSE41FPToIntLocations(ArenaAllocator* allocator,
HInvoke* invoke,
CodeGeneratorX86_64* codegen) {
// Do we have instruction support?
if (!codegen->GetInstructionSetFeatures().HasSSE4_1()) {
return;
}
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresFpuRegister());
locations->SetOut(Location::RequiresRegister());
locations->AddTemp(Location::RequiresFpuRegister());
locations->AddTemp(Location::RequiresFpuRegister());
}
void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
CreateSSE41FPToIntLocations(allocator_, invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
LocationSummary* locations = invoke->GetLocations();
DCHECK(!locations->WillCall());
XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
NearLabel skip_incr, done;
X86_64Assembler* assembler = GetAssembler();
// Since no direct x86 rounding instruction matches the required semantics,
// this intrinsic is implemented as follows:
// result = floor(in);
// if (in - result >= 0.5f)
// result = result + 1.0f;
__ movss(t2, in);
__ roundss(t1, in, Immediate(1));
__ subss(t2, t1);
__ comiss(t2, codegen_->LiteralFloatAddress(0.5f));
__ j(kBelow, &skip_incr);
__ addss(t1, codegen_->LiteralFloatAddress(1.0f));
__ Bind(&skip_incr);
// Final conversion to an integer. Unfortunately this also does not have a
// direct x86 instruction, since NaN should map to 0 and large positive
// values need to be clipped to the extreme value.
codegen_->Load32BitValue(out, kPrimIntMax);
__ cvtsi2ss(t2, out);
__ comiss(t1, t2);
__ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
__ movl(out, Immediate(0)); // does not change flags
__ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
__ cvttss2si(out, t1);
__ Bind(&done);
}
void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
CreateSSE41FPToIntLocations(allocator_, invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
LocationSummary* locations = invoke->GetLocations();
DCHECK(!locations->WillCall());
XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
NearLabel skip_incr, done;
X86_64Assembler* assembler = GetAssembler();
// Since no direct x86 rounding instruction matches the required semantics,
// this intrinsic is implemented as follows:
// result = floor(in);
// if (in - result >= 0.5)
// result = result + 1.0f;
__ movsd(t2, in);
__ roundsd(t1, in, Immediate(1));
__ subsd(t2, t1);
__ comisd(t2, codegen_->LiteralDoubleAddress(0.5));
__ j(kBelow, &skip_incr);
__ addsd(t1, codegen_->LiteralDoubleAddress(1.0f));
__ Bind(&skip_incr);
// Final conversion to an integer. Unfortunately this also does not have a
// direct x86 instruction, since NaN should map to 0 and large positive
// values need to be clipped to the extreme value.
codegen_->Load64BitValue(out, kPrimLongMax);
__ cvtsi2sd(t2, out, /* is64bit= */ true);
__ comisd(t1, t2);
__ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
__ movl(out, Immediate(0)); // does not change flags, implicit zero extension to 64-bit
__ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
__ cvttsd2si(out, t1, /* is64bit= */ true);
__ Bind(&done);
}
static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
InvokeRuntimeCallingConvention calling_convention;
locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
locations->SetOut(Location::FpuRegisterLocation(XMM0));
CodeGeneratorX86_64::BlockNonVolatileXmmRegisters(locations);
}
static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
QuickEntrypointEnum entry) {
LocationSummary* locations = invoke->GetLocations();
DCHECK(locations->WillCall());
DCHECK(invoke->IsInvokeStaticOrDirect());
codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
}
void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickCos);
}
void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickSin);
}
void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickAcos);
}
void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickAsin);
}
void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickAtan);
}
void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickCbrt);
}
void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickCosh);
}
void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickExp);
}
void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickExpm1);
}
void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickLog);
}
void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickLog10);
}
void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickSinh);
}
void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickTan);
}
void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
CreateFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickTanh);
}
static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
InvokeRuntimeCallingConvention calling_convention;
locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
locations->SetOut(Location::FpuRegisterLocation(XMM0));
CodeGeneratorX86_64::BlockNonVolatileXmmRegisters(locations);
}
static void CreateFPFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
DCHECK_EQ(invoke->GetNumberOfArguments(), 3U);
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
InvokeRuntimeCallingConvention calling_convention;
locations->SetInAt(0, Location::RequiresFpuRegister());
locations->SetInAt(1, Location::RequiresFpuRegister());
locations->SetInAt(2, Location::RequiresFpuRegister());
locations->SetOut(Location::SameAsFirstInput());
}
void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
CreateFPFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickAtan2);
}
void IntrinsicLocationsBuilderX86_64::VisitMathPow(HInvoke* invoke) {
CreateFPFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathPow(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickPow);
}
void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
CreateFPFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickHypot);
}
void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
CreateFPFPToFPCallLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
}
static void CreateSystemArrayCopyLocations(HInvoke* invoke) {
// Check to see if we have known failures that will cause us to have to bail out
// to the runtime, and just generate the runtime call directly.
HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
// The positions must be non-negative.
if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
(dest_pos != nullptr && dest_pos->GetValue() < 0)) {
// We will have to fail anyways.
return;
}
// The length must be > 0.
HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
if (length != nullptr) {
int32_t len = length->GetValue();
if (len < 0) {
// Just call as normal.
return;
}
}
LocationSummary* locations =
new (invoke->GetBlock()->GetGraph()->GetAllocator()) LocationSummary
(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
// arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
locations->SetInAt(0, Location::RequiresRegister());
locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
locations->SetInAt(2, Location::RequiresRegister());
locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
// And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
locations->AddTemp(Location::RegisterLocation(RSI));
locations->AddTemp(Location::RegisterLocation(RDI));
locations->AddTemp(Location::RegisterLocation(RCX));
}
static void CheckPosition(X86_64Assembler* assembler,
Location pos,
CpuRegister input,
Location length,
SlowPathCode* slow_path,
CpuRegister temp,
bool length_is_input_length = false) {
// Where is the length in the Array?
const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
if (pos.IsConstant()) {
int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
if (pos_const == 0) {
if (!length_is_input_length) {
// Check that length(input) >= length.
if (length.IsConstant()) {
__ cmpl(Address(input, length_offset),
Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
} else {
__ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
}
__ j(kLess, slow_path->GetEntryLabel());
}
} else {
// Check that length(input) >= pos.
__ movl(temp, Address(input, length_offset));
__ subl(temp, Immediate(pos_const));
__ j(kLess, slow_path->GetEntryLabel());
// Check that (length(input) - pos) >= length.
if (length.IsConstant()) {
__ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
} else {
__ cmpl(temp, length.AsRegister<CpuRegister>());
}
__ j(kLess, slow_path->GetEntryLabel());
}
} else if (length_is_input_length) {
// The only way the copy can succeed is if pos is zero.
CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
__ testl(pos_reg, pos_reg);
__ j(kNotEqual, slow_path->GetEntryLabel());
} else {
// Check that pos >= 0.
CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
__ testl(pos_reg, pos_reg);
__ j(kLess, slow_path->GetEntryLabel());
// Check that pos <= length(input).
__ cmpl(Address(input, length_offset), pos_reg);
__ j(kLess, slow_path->GetEntryLabel());
// Check that (length(input) - pos) >= length.
__ movl(temp, Address(input, length_offset));
__ subl(temp, pos_reg);
if (length.IsConstant()) {
__ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
} else {
__ cmpl(temp, length.AsRegister<CpuRegister>());
}
__ j(kLess, slow_path->GetEntryLabel());
}
}
static void SystemArrayCopyPrimitive(HInvoke* invoke,
X86_64Assembler* assembler,
CodeGeneratorX86_64* codegen,
DataType::Type type) {
LocationSummary* locations = invoke->GetLocations();
CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Location src_pos = locations->InAt(1);
CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Location dest_pos = locations->InAt(3);
Location length = locations->InAt(4);
// Temporaries that we need for MOVSB/W/L.
CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
DCHECK_EQ(src_base.AsRegister(), RSI);
CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
DCHECK_EQ(dest_base.AsRegister(), RDI);
CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
DCHECK_EQ(count.AsRegister(), RCX);
SlowPathCode* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen->AddSlowPath(slow_path);
// Bail out if the source and destination are the same.
__ cmpl(src, dest);
__ j(kEqual, slow_path->GetEntryLabel());
// Bail out if the source is null.
__ testl(src, src);
__ j(kEqual, slow_path->GetEntryLabel());
// Bail out if the destination is null.
__ testl(dest, dest);
__ j(kEqual, slow_path->GetEntryLabel());
// If the length is negative, bail out.
// We have already checked in the LocationsBuilder for the constant case.
if (!length.IsConstant()) {
__ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
__ j(kLess, slow_path->GetEntryLabel());
}
// Validity checks: source. Use src_base as a temporary register.
CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
// Validity checks: dest. Use src_base as a temporary register.
CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
// We need the count in RCX.
if (length.IsConstant()) {
__ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
} else {
__ movl(count, length.AsRegister<CpuRegister>());
}
// Okay, everything checks out. Finally time to do the copy.
// Check assumption that sizeof(Char) is 2 (used in scaling below).
const size_t data_size = DataType::Size(type);
const ScaleFactor scale_factor = CodeGenerator::ScaleFactorForType(type);
const uint32_t data_offset = mirror::Array::DataOffset(data_size).Uint32Value();
if (src_pos.IsConstant()) {
int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
__ leal(src_base, Address(src, data_size * src_pos_const + data_offset));
} else {
__ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
}
if (dest_pos.IsConstant()) {
int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
__ leal(dest_base, Address(dest, data_size * dest_pos_const + data_offset));
} else {
__ leal(dest_base,
Address(dest, dest_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
}
// Do the move.
switch (type) {
case DataType::Type::kInt8:
__ rep_movsb();
break;
case DataType::Type::kUint16:
__ rep_movsw();
break;
case DataType::Type::kInt32:
__ rep_movsl();
break;
default:
LOG(FATAL) << "Unexpected data type for intrinsic";
}
__ Bind(slow_path->GetExitLabel());
}
void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
CreateSystemArrayCopyLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
SystemArrayCopyPrimitive(invoke, assembler, codegen_, DataType::Type::kUint16);
}
void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyByte(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
SystemArrayCopyPrimitive(invoke, assembler, codegen_, DataType::Type::kInt8);
}
void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyByte(HInvoke* invoke) {
CreateSystemArrayCopyLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyInt(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
SystemArrayCopyPrimitive(invoke, assembler, codegen_, DataType::Type::kInt32);
}
void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyInt(HInvoke* invoke) {
CreateSystemArrayCopyLocations(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
// The only read barrier implementation supporting the
// SystemArrayCopy intrinsic is the Baker-style read barriers.
if (gUseReadBarrier && !kUseBakerReadBarrier) {
return;
}
CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
}
// Compute base source address, base destination address, and end
// source address for the System.arraycopy intrinsic in `src_base`,
// `dst_base` and `src_end` respectively.
static void GenSystemArrayCopyAddresses(X86_64Assembler* assembler,
DataType::Type type,
const CpuRegister& src,
const Location& src_pos,
const CpuRegister& dst,
const Location& dst_pos,
const Location& copy_length,
const CpuRegister& src_base,
const CpuRegister& dst_base,
const CpuRegister& src_end) {
// This routine is only used by the SystemArrayCopy intrinsic.
DCHECK_EQ(type, DataType::Type::kReference);
const int32_t element_size = DataType::Size(type);
const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
if (src_pos.IsConstant()) {
int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
__ leal(src_base, Address(src, element_size * constant + data_offset));
} else {
__ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
}
if (dst_pos.IsConstant()) {
int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
__ leal(dst_base, Address(dst, element_size * constant + data_offset));
} else {
__ leal(dst_base, Address(dst, dst_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
}
if (copy_length.IsConstant()) {
int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
__ leal(src_end, Address(src_base, element_size * constant));
} else {
__ leal(src_end, Address(src_base, copy_length.AsRegister<CpuRegister>(), scale_factor, 0));
}
}
void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
// The only read barrier implementation supporting the
// SystemArrayCopy intrinsic is the Baker-style read barriers.
DCHECK_IMPLIES(gUseReadBarrier, kUseBakerReadBarrier);
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Location src_pos = locations->InAt(1);
CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Location dest_pos = locations->InAt(3);
Location length = locations->InAt(4);
Location temp1_loc = locations->GetTemp(0);
CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
Location temp2_loc = locations->GetTemp(1);
CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
Location temp3_loc = locations->GetTemp(2);
CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
Location TMP_loc = Location::RegisterLocation(TMP);
SlowPathCode* intrinsic_slow_path =
new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen_->AddSlowPath(intrinsic_slow_path);
NearLabel conditions_on_positions_validated;
SystemArrayCopyOptimizations optimizations(invoke);
// If source and destination are the same, we go to slow path if we need to do
// forward copying.
if (src_pos.IsConstant()) {
int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
if (dest_pos.IsConstant()) {
int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
if (optimizations.GetDestinationIsSource()) {
// Checked when building locations.
DCHECK_GE(src_pos_constant, dest_pos_constant);
} else if (src_pos_constant < dest_pos_constant) {
__ cmpl(src, dest);
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
}
} else {
if (!optimizations.GetDestinationIsSource()) {
__ cmpl(src, dest);
__ j(kNotEqual, &conditions_on_positions_validated);
}
__ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
__ j(kGreater, intrinsic_slow_path->GetEntryLabel());
}
} else {
if (!optimizations.GetDestinationIsSource()) {
__ cmpl(src, dest);
__ j(kNotEqual, &conditions_on_positions_validated);
}
if (dest_pos.IsConstant()) {
int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
__ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
__ j(kLess, intrinsic_slow_path->GetEntryLabel());
} else {
__ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
__ j(kLess, intrinsic_slow_path->GetEntryLabel());
}
}
__ Bind(&conditions_on_positions_validated);
if (!optimizations.GetSourceIsNotNull()) {
// Bail out if the source is null.
__ testl(src, src);
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
}
if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
// Bail out if the destination is null.
__ testl(dest, dest);
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
}
// If the length is negative, bail out.
// We have already checked in the LocationsBuilder for the constant case.
if (!length.IsConstant() &&
!optimizations.GetCountIsSourceLength() &&
!optimizations.GetCountIsDestinationLength()) {
__ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
__ j(kLess, intrinsic_slow_path->GetEntryLabel());
}
// Validity checks: source.
CheckPosition(assembler,
src_pos,
src,
length,
intrinsic_slow_path,
temp1,
optimizations.GetCountIsSourceLength());
// Validity checks: dest.
CheckPosition(assembler,
dest_pos,
dest,
length,
intrinsic_slow_path,
temp1,
optimizations.GetCountIsDestinationLength());
if (!optimizations.GetDoesNotNeedTypeCheck()) {
// Check whether all elements of the source array are assignable to the component
// type of the destination array. We do two checks: the classes are the same,
// or the destination is Object[]. If none of these checks succeed, we go to the
// slow path.
bool did_unpoison = false;
if (gUseReadBarrier && kUseBakerReadBarrier) {
// /* HeapReference<Class> */ temp1 = dest->klass_
codegen_->GenerateFieldLoadWithBakerReadBarrier(
invoke, temp1_loc, dest, class_offset, /* needs_null_check= */ false);
// Register `temp1` is not trashed by the read barrier emitted
// by GenerateFieldLoadWithBakerReadBarrier below, as that
// method produces a call to a ReadBarrierMarkRegX entry point,
// which saves all potentially live registers, including
// temporaries such a `temp1`.
// /* HeapReference<Class> */ temp2 = src->klass_
codegen_->GenerateFieldLoadWithBakerReadBarrier(
invoke, temp2_loc, src, class_offset, /* needs_null_check= */ false);
// If heap poisoning is enabled, `temp1` and `temp2` have been
// unpoisoned by the the previous calls to
// GenerateFieldLoadWithBakerReadBarrier.
} else {
// /* HeapReference<Class> */ temp1 = dest->klass_
__ movl(temp1, Address(dest, class_offset));
// /* HeapReference<Class> */ temp2 = src->klass_
__ movl(temp2, Address(src, class_offset));
if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
!optimizations.GetSourceIsNonPrimitiveArray()) {
// One or two of the references need to be unpoisoned. Unpoison them
// both to make the identity check valid.
__ MaybeUnpoisonHeapReference(temp1);
__ MaybeUnpoisonHeapReference(temp2);
did_unpoison = true;
}
}
if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
// Bail out if the destination is not a non primitive array.
if (gUseReadBarrier && kUseBakerReadBarrier) {
// /* HeapReference<Class> */ TMP = temp1->component_type_
codegen_->GenerateFieldLoadWithBakerReadBarrier(
invoke, TMP_loc, temp1, component_offset, /* needs_null_check= */ false);
__ testl(CpuRegister(TMP), CpuRegister(TMP));
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
// If heap poisoning is enabled, `TMP` has been unpoisoned by
// the the previous call to GenerateFieldLoadWithBakerReadBarrier.
} else {
// /* HeapReference<Class> */ TMP = temp1->component_type_
__ movl(CpuRegister(TMP), Address(temp1, component_offset));
__ testl(CpuRegister(TMP), CpuRegister(TMP));
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
__ MaybeUnpoisonHeapReference(CpuRegister(TMP));
}
__ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
__ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
}
if (!optimizations.GetSourceIsNonPrimitiveArray()) {
// Bail out if the source is not a non primitive array.
if (gUseReadBarrier && kUseBakerReadBarrier) {
// For the same reason given earlier, `temp1` is not trashed by the
// read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
// /* HeapReference<Class> */ TMP = temp2->component_type_
codegen_->GenerateFieldLoadWithBakerReadBarrier(
invoke, TMP_loc, temp2, component_offset, /* needs_null_check= */ false);
__ testl(CpuRegister(TMP), CpuRegister(TMP));
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
// If heap poisoning is enabled, `TMP` has been unpoisoned by
// the the previous call to GenerateFieldLoadWithBakerReadBarrier.
} else {
// /* HeapReference<Class> */ TMP = temp2->component_type_
__ movl(CpuRegister(TMP), Address(temp2, component_offset));
__ testl(CpuRegister(TMP), CpuRegister(TMP));
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
__ MaybeUnpoisonHeapReference(CpuRegister(TMP));
}
__ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
__ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
}
__ cmpl(temp1, temp2);
if (optimizations.GetDestinationIsTypedObjectArray()) {
NearLabel do_copy;
__ j(kEqual, &do_copy);
if (gUseReadBarrier && kUseBakerReadBarrier) {
// /* HeapReference<Class> */ temp1 = temp1->component_type_
codegen_->GenerateFieldLoadWithBakerReadBarrier(
invoke, temp1_loc, temp1, component_offset, /* needs_null_check= */ false);
// We do not need to emit a read barrier for the following
// heap reference load, as `temp1` is only used in a
// comparison with null below, and this reference is not
// kept afterwards.
__ cmpl(Address(temp1, super_offset), Immediate(0));
} else {
if (!did_unpoison) {
__ MaybeUnpoisonHeapReference(temp1);
}
// /* HeapReference<Class> */ temp1 = temp1->component_type_
__ movl(temp1, Address(temp1, component_offset));
__ MaybeUnpoisonHeapReference(temp1);
// No need to unpoison the following heap reference load, as
// we're comparing against null.
__ cmpl(Address(temp1, super_offset), Immediate(0));
}
__ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
__ Bind(&do_copy);
} else {
__ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
}
} else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
// Bail out if the source is not a non primitive array.
if (gUseReadBarrier && kUseBakerReadBarrier) {
// /* HeapReference<Class> */ temp1 = src->klass_
codegen_->GenerateFieldLoadWithBakerReadBarrier(
invoke, temp1_loc, src, class_offset, /* needs_null_check= */ false);
// /* HeapReference<Class> */ TMP = temp1->component_type_
codegen_->GenerateFieldLoadWithBakerReadBarrier(
invoke, TMP_loc, temp1, component_offset, /* needs_null_check= */ false);
__ testl(CpuRegister(TMP), CpuRegister(TMP));
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
} else {
// /* HeapReference<Class> */ temp1 = src->klass_
__ movl(temp1, Address(src, class_offset));
__ MaybeUnpoisonHeapReference(temp1);
// /* HeapReference<Class> */ TMP = temp1->component_type_
__ movl(CpuRegister(TMP), Address(temp1, component_offset));
// No need to unpoison `TMP` now, as we're comparing against null.
__ testl(CpuRegister(TMP), CpuRegister(TMP));
__ j(kEqual, intrinsic_slow_path->GetEntryLabel());
__ MaybeUnpoisonHeapReference(CpuRegister(TMP));
}
__ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
__ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
}
const DataType::Type type = DataType::Type::kReference;
const int32_t element_size = DataType::Size(type);
// Compute base source address, base destination address, and end
// source address in `temp1`, `temp2` and `temp3` respectively.
GenSystemArrayCopyAddresses(
GetAssembler(), type, src, src_pos, dest, dest_pos, length, temp1, temp2, temp3);
if (gUseReadBarrier && kUseBakerReadBarrier) {
// SystemArrayCopy implementation for Baker read barriers (see
// also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
//
// if (src_ptr != end_ptr) {
// uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
// lfence; // Load fence or artificial data dependency to prevent load-load reordering
// bool is_gray = (rb_state == ReadBarrier::GrayState());
// if (is_gray) {
// // Slow-path copy.
// do {
// *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
// } while (src_ptr != end_ptr)
// } else {
// // Fast-path copy.
// do {
// *dest_ptr++ = *src_ptr++;
// } while (src_ptr != end_ptr)
// }
// }
NearLabel loop, done;
// Don't enter copy loop if `length == 0`.
__ cmpl(temp1, temp3);
__ j(kEqual, &done);
// Given the numeric representation, it's enough to check the low bit of the rb_state.
static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
// if (rb_state == ReadBarrier::GrayState())
// goto slow_path;
// At this point, just do the "if" and make sure that flags are preserved until the branch.
__ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
// Load fence to prevent load-load reordering.
// Note that this is a no-op, thanks to the x86-64 memory model.
codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
// Slow path used to copy array when `src` is gray.
SlowPathCode* read_barrier_slow_path =
new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
codegen_->AddSlowPath(read_barrier_slow_path);
// We have done the "if" of the gray bit check above, now branch based on the flags.
__ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
// Fast-path copy.
// Iterate over the arrays and do a raw copy of the objects. We don't need to
// poison/unpoison.
__ Bind(&loop);
__ movl(CpuRegister(TMP), Address(temp1, 0));
__ movl(Address(temp2, 0), CpuRegister(TMP));
__ addl(temp1, Immediate(element_size));
__ addl(temp2, Immediate(element_size));
__ cmpl(temp1, temp3);
__ j(kNotEqual, &loop);
__ Bind(read_barrier_slow_path->GetExitLabel());
__ Bind(&done);
} else {
// Non read barrier code.
// Iterate over the arrays and do a raw copy of the objects. We don't need to
// poison/unpoison.
NearLabel loop, done;
__ cmpl(temp1, temp3);
__ j(kEqual, &done);
__ Bind(&loop);
__ movl(CpuRegister(TMP), Address(temp1, 0));
__ movl(Address(temp2, 0), CpuRegister(TMP));
__ addl(temp1, Immediate(element_size));
__ addl(temp2, Immediate(element_size));
__ cmpl(temp1, temp3);
__ j(kNotEqual, &loop);
__ Bind(&done);
}
// We only need one card marking on the destination array.
codegen_->MarkGCCard(temp1, temp2, dest, CpuRegister(kNoRegister), /* emit_null_check= */ false);
__ Bind(intrinsic_slow_path->GetExitLabel());
}
void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
LocationSummary* locations = new (allocator_) LocationSummary(
invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
InvokeRuntimeCallingConvention calling_convention;
locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
locations->SetOut(Location::RegisterLocation(RAX));
}
void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
// Note that the null check must have been done earlier.
DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
__ testl(argument, argument);
SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen_->AddSlowPath(slow_path);
__ j(kEqual, slow_path->GetEntryLabel());
codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
__ Bind(slow_path->GetExitLabel());
}
void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
locations->SetInAt(1, Location::RequiresRegister());
// Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
locations->AddTemp(Location::RegisterLocation(RCX));
locations->AddTemp(Location::RegisterLocation(RDI));
// Set output, RSI needed for repe_cmpsq instruction anyways.
locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
}
void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
NearLabel end, return_true, return_false;
// Get offsets of count, value, and class fields within a string object.
const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
// Note that the null check must have been done earlier.
DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
StringEqualsOptimizations optimizations(invoke);
if (!optimizations.GetArgumentNotNull()) {
// Check if input is null, return false if it is.
__ testl(arg, arg);
__ j(kEqual, &return_false);
}
if (!optimizations.GetArgumentIsString()) {
// Instanceof check for the argument by comparing class fields.
// All string objects must have the same type since String cannot be subclassed.
// Receiver must be a string object, so its class field is equal to all strings' class fields.
// If the argument is a string object, its class field must be equal to receiver's class field.
//
// As the String class is expected to be non-movable, we can read the class
// field from String.equals' arguments without read barriers.
AssertNonMovableStringClass();
// Also, because we use the loaded class references only to compare them, we
// don't need to unpoison them.
// /* HeapReference<Class> */ rcx = str->klass_
__ movl(rcx, Address(str, class_offset));
// if (rcx != /* HeapReference<Class> */ arg->klass_) return false
__ cmpl(rcx, Address(arg, class_offset));
__ j(kNotEqual, &return_false);
}
// Reference equality check, return true if same reference.
__ cmpl(str, arg);
__ j(kEqual, &return_true);
// Load length and compression flag of receiver string.
__ movl(rcx, Address(str, count_offset));
// Check if lengths and compressiond flags are equal, return false if they're not.
// Two identical strings will always have same compression style since
// compression style is decided on alloc.
__ cmpl(rcx, Address(arg, count_offset));
__ j(kNotEqual, &return_false);
// Return true if both strings are empty. Even with string compression `count == 0` means empty.
static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
"Expecting 0=compressed, 1=uncompressed");
__ jrcxz(&return_true);
if (mirror::kUseStringCompression) {
NearLabel string_uncompressed;
// Extract length and differentiate between both compressed or both uncompressed.
// Different compression style is cut above.
__ shrl(rcx, Immediate(1));
__ j(kCarrySet, &string_uncompressed);
// Divide string length by 2, rounding up, and continue as if uncompressed.
// Merge clearing the compression flag with +1 for rounding.
__ addl(rcx, Immediate(1));
__ shrl(rcx, Immediate(1));
__ Bind(&string_uncompressed);
}
// Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
__ leal(rsi, Address(str, value_offset));
__ leal(rdi, Address(arg, value_offset));
// Divide string length by 4 and adjust for lengths not divisible by 4.
__ addl(rcx, Immediate(3));
__ shrl(rcx, Immediate(2));
// Assertions that must hold in order to compare strings 4 characters (uncompressed)
// or 8 characters (compressed) at a time.
DCHECK_ALIGNED(value_offset, 8);
static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
// Loop to compare strings four characters at a time starting at the beginning of the string.
__ repe_cmpsq();
// If strings are not equal, zero flag will be cleared.
__ j(kNotEqual, &return_false);
// Return true and exit the function.
// If loop does not result in returning false, we return true.
__ Bind(&return_true);
__ movl(rsi, Immediate(1));
__ jmp(&end);
// Return false and exit the function.
__ Bind(&return_false);
__ xorl(rsi, rsi);
__ Bind(&end);
}
static void CreateStringIndexOfLocations(HInvoke* invoke,
ArenaAllocator* allocator,
bool start_at_zero) {
LocationSummary* locations = new (allocator) LocationSummary(invoke,
LocationSummary::kCallOnSlowPath,
kIntrinsified);
// The data needs to be in RDI for scasw. So request that the string is there, anyways.
locations->SetInAt(0, Location::RegisterLocation(RDI));
// If we look for a constant char, we'll still have to copy it into RAX. So just request the
// allocator to do that, anyways. We can still do the constant check by checking the parameter
// of the instruction explicitly.
// Note: This works as we don't clobber RAX anywhere.
locations->SetInAt(1, Location::RegisterLocation(RAX));
if (!start_at_zero) {
locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
}
// As we clobber RDI during execution anyways, also use it as the output.
locations->SetOut(Location::SameAsFirstInput());
// repne scasw uses RCX as the counter.
locations->AddTemp(Location::RegisterLocation(RCX));
// Need another temporary to be able to compute the result.
locations->AddTemp(Location::RequiresRegister());
}
static void GenerateStringIndexOf(HInvoke* invoke,
X86_64Assembler* assembler,
CodeGeneratorX86_64* codegen,
bool start_at_zero) {
LocationSummary* locations = invoke->GetLocations();
// Note that the null check must have been done earlier.
DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
// Check our assumptions for registers.
DCHECK_EQ(string_obj.AsRegister(), RDI);
DCHECK_EQ(search_value.AsRegister(), RAX);
DCHECK_EQ(counter.AsRegister(), RCX);
DCHECK_EQ(out.AsRegister(), RDI);
// Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
// or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
SlowPathCode* slow_path = nullptr;
HInstruction* code_point = invoke->InputAt(1);
if (code_point->IsIntConstant()) {
if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
std::numeric_limits<uint16_t>::max()) {
// Always needs the slow-path. We could directly dispatch to it, but this case should be
// rare, so for simplicity just put the full slow-path down and branch unconditionally.
slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen->AddSlowPath(slow_path);
__ jmp(slow_path->GetEntryLabel());
__ Bind(slow_path->GetExitLabel());
return;
}
} else if (code_point->GetType() != DataType::Type::kUint16) {
__ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen->AddSlowPath(slow_path);
__ j(kAbove, slow_path->GetEntryLabel());
}
// From here down, we know that we are looking for a char that fits in
// 16 bits (uncompressed) or 8 bits (compressed).
// Location of reference to data array within the String object.
int32_t value_offset = mirror::String::ValueOffset().Int32Value();
// Location of count within the String object.
int32_t count_offset = mirror::String::CountOffset().Int32Value();
// Load the count field of the string containing the length and compression flag.
__ movl(string_length, Address(string_obj, count_offset));
// Do a zero-length check. Even with string compression `count == 0` means empty.
// TODO: Support jecxz.
NearLabel not_found_label;
__ testl(string_length, string_length);
__ j(kEqual, ¬_found_label);
if (mirror::kUseStringCompression) {
// Use TMP to keep string_length_flagged.
__ movl(CpuRegister(TMP), string_length);
// Mask out first bit used as compression flag.
__ shrl(string_length, Immediate(1));
}
if (start_at_zero) {
// Number of chars to scan is the same as the string length.
__ movl(counter, string_length);
// Move to the start of the string.
__ addq(string_obj, Immediate(value_offset));
} else {
CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
// Do a start_index check.
__ cmpl(start_index, string_length);
__ j(kGreaterEqual, ¬_found_label);
// Ensure we have a start index >= 0;
__ xorl(counter, counter);
__ cmpl(start_index, Immediate(0));
__ cmov(kGreater, counter, start_index, /* is64bit= */ false); // 32-bit copy is enough.
if (mirror::kUseStringCompression) {
NearLabel modify_counter, offset_uncompressed_label;
__ testl(CpuRegister(TMP), Immediate(1));
__ j(kNotZero, &offset_uncompressed_label);
__ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
__ jmp(&modify_counter);
// Move to the start of the string: string_obj + value_offset + 2 * start_index.
__ Bind(&offset_uncompressed_label);
__ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
__ Bind(&modify_counter);
} else {
__ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
}
// Now update ecx, the work counter: it's gonna be string.length - start_index.
__ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
__ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
}
if (mirror::kUseStringCompression) {
NearLabel uncompressed_string_comparison;
NearLabel comparison_done;
__ testl(CpuRegister(TMP), Immediate(1));
__ j(kNotZero, &uncompressed_string_comparison);
// Check if RAX (search_value) is ASCII.
__ cmpl(search_value, Immediate(127));
__ j(kGreater, ¬_found_label);
// Comparing byte-per-byte.
__ repne_scasb();
__ jmp(&comparison_done);
// Everything is set up for repne scasw:
// * Comparison address in RDI.
// * Counter in ECX.
__ Bind(&uncompressed_string_comparison);
__ repne_scasw();
__ Bind(&comparison_done);
} else {
__ repne_scasw();
}
// Did we find a match?
__ j(kNotEqual, ¬_found_label);
// Yes, we matched. Compute the index of the result.
__ subl(string_length, counter);
__ leal(out, Address(string_length, -1));
NearLabel done;
__ jmp(&done);
// Failed to match; return -1.
__ Bind(¬_found_label);
__ movl(out, Immediate(-1));
// And join up at the end.
__ Bind(&done);
if (slow_path != nullptr) {
__ Bind(slow_path->GetExitLabel());
}
}
void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero= */ true);
}
void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero= */ true);
}
void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero= */ false);
}
void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero= */ false);
}
void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
LocationSummary* locations = new (allocator_) LocationSummary(
invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
InvokeRuntimeCallingConvention calling_convention;
locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
locations->SetOut(Location::RegisterLocation(RAX));
}
void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
__ testl(byte_array, byte_array);
SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen_->AddSlowPath(slow_path);
__ j(kEqual, slow_path->GetEntryLabel());
codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
__ Bind(slow_path->GetExitLabel());
}
void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
InvokeRuntimeCallingConvention calling_convention;
locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
locations->SetOut(Location::RegisterLocation(RAX));
}
void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
// No need to emit code checking whether `locations->InAt(2)` is a null
// pointer, as callers of the native method
//
// java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
//
// all include a null check on `data` before calling that method.
codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
}
void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
LocationSummary* locations = new (allocator_) LocationSummary(
invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
InvokeRuntimeCallingConvention calling_convention;
locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
locations->SetOut(Location::RegisterLocation(RAX));
}
void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
__ testl(string_to_copy, string_to_copy);
SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen_->AddSlowPath(slow_path);
__ j(kEqual, slow_path->GetEntryLabel());
codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
__ Bind(slow_path->GetExitLabel());
}
void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
// public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
locations->SetInAt(2, Location::RequiresRegister());
locations->SetInAt(3, Location::RequiresRegister());
locations->SetInAt(4, Location::RequiresRegister());
// And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
locations->AddTemp(Location::RegisterLocation(RSI));
locations->AddTemp(Location::RegisterLocation(RDI));
locations->AddTemp(Location::RegisterLocation(RCX));
}
void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
size_t char_component_size = DataType::Size(DataType::Type::kUint16);
// Location of data in char array buffer.
const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
// Location of char array data in string.
const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
// public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Location srcBegin = locations->InAt(1);
int srcBegin_value =
srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
// Check assumption that sizeof(Char) is 2 (used in scaling below).
const size_t char_size = DataType::Size(DataType::Type::kUint16);
DCHECK_EQ(char_size, 2u);
NearLabel done;
// Compute the number of chars (words) to move.
__ movl(CpuRegister(RCX), srcEnd);
if (srcBegin.IsConstant()) {
__ subl(CpuRegister(RCX), Immediate(srcBegin_value));
} else {
DCHECK(srcBegin.IsRegister());
__ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
}
if (mirror::kUseStringCompression) {
NearLabel copy_uncompressed, copy_loop;
const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
DCHECK_EQ(c_char_size, 1u);
// Location of count in string.
const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
__ testl(Address(obj, count_offset), Immediate(1));
static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
"Expecting 0=compressed, 1=uncompressed");
__ j(kNotZero, ©_uncompressed);
// Compute the address of the source string by adding the number of chars from
// the source beginning to the value offset of a string.
__ leaq(CpuRegister(RSI),
CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
// Start the loop to copy String's value to Array of Char.
__ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
__ Bind(©_loop);
__ jrcxz(&done);
// Use TMP as temporary (convert byte from RSI to word).
// TODO: Selecting RAX as the temporary and using LODSB/STOSW.
__ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
__ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
__ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
__ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
// TODO: Add support for LOOP to X86_64Assembler.
__ subl(CpuRegister(RCX), Immediate(1));
__ jmp(©_loop);
__ Bind(©_uncompressed);
}
__ leaq(CpuRegister(RSI),
CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
// Compute the address of the destination buffer.
__ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
// Do the move.
__ rep_movsw();
__ Bind(&done);
}
static void GenPeek(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
// x86 allows unaligned access. We do not have to check the input or use specific instructions
// to avoid a SIGBUS.
switch (size) {
case DataType::Type::kInt8:
__ movsxb(out, Address(address, 0));
break;
case DataType::Type::kInt16:
__ movsxw(out, Address(address, 0));
break;
case DataType::Type::kInt32:
__ movl(out, Address(address, 0));
break;
case DataType::Type::kInt64:
__ movq(out, Address(address, 0));
break;
default:
LOG(FATAL) << "Type not recognized for peek: " << size;
UNREACHABLE();
}
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
CreateIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
CreateIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
CreateIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
CreateIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
}
static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
}
static void GenPoke(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Location value = locations->InAt(1);
// x86 allows unaligned access. We do not have to check the input or use specific instructions
// to avoid a SIGBUS.
switch (size) {
case DataType::Type::kInt8:
if (value.IsConstant()) {
__ movb(Address(address, 0),
Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
} else {
__ movb(Address(address, 0), value.AsRegister<CpuRegister>());
}
break;
case DataType::Type::kInt16:
if (value.IsConstant()) {
__ movw(Address(address, 0),
Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
} else {
__ movw(Address(address, 0), value.AsRegister<CpuRegister>());
}
break;
case DataType::Type::kInt32:
if (value.IsConstant()) {
__ movl(Address(address, 0),
Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
} else {
__ movl(Address(address, 0), value.AsRegister<CpuRegister>());
}
break;
case DataType::Type::kInt64:
if (value.IsConstant()) {
int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
DCHECK(IsInt<32>(v));
int32_t v_32 = v;
__ movq(Address(address, 0), Immediate(v_32));
} else {
__ movq(Address(address, 0), value.AsRegister<CpuRegister>());
}
break;
default:
LOG(FATAL) << "Type not recognized for poke: " << size;
UNREACHABLE();
}
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
CreateIntIntToVoidLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
CreateIntIntToVoidLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
CreateIntIntToVoidLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
CreateIntIntToVoidLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
}
void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetOut(Location::RequiresRegister());
}
void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
/* no_rip= */ true));
}
static void GenUnsafeGet(HInvoke* invoke,
DataType::Type type,
bool is_volatile ATTRIBUTE_UNUSED,
CodeGeneratorX86_64* codegen) {
X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
LocationSummary* locations = invoke->GetLocations();
Location base_loc = locations->InAt(1);
CpuRegister base = base_loc.AsRegister<CpuRegister>();
Location offset_loc = locations->InAt(2);
CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
Location output_loc = locations->Out();
CpuRegister output = output_loc.AsRegister<CpuRegister>();
switch (type) {
case DataType::Type::kInt32:
__ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
break;
case DataType::Type::kReference: {
if (gUseReadBarrier) {
if (kUseBakerReadBarrier) {
Address src(base, offset, ScaleFactor::TIMES_1, 0);
codegen->GenerateReferenceLoadWithBakerReadBarrier(
invoke, output_loc, base, src, /* needs_null_check= */ false);
} else {
__ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
codegen->GenerateReadBarrierSlow(
invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
}
} else {
__ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
__ MaybeUnpoisonHeapReference(output);
}
break;
}
case DataType::Type::kInt64:
__ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
break;
default:
LOG(FATAL) << "Unsupported op size " << type;
UNREACHABLE();
}
}
static bool UnsafeGetIntrinsicOnCallList(Intrinsics intrinsic) {
switch (intrinsic) {
case Intrinsics::kUnsafeGetObject:
case Intrinsics::kUnsafeGetObjectVolatile:
case Intrinsics::kJdkUnsafeGetObject:
case Intrinsics::kJdkUnsafeGetObjectVolatile:
case Intrinsics::kJdkUnsafeGetObjectAcquire:
return true;
default:
break;
}
return false;
}
static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
bool can_call = gUseReadBarrier && UnsafeGetIntrinsicOnCallList(invoke->GetIntrinsic());
LocationSummary* locations =
new (allocator) LocationSummary(invoke,
can_call
? LocationSummary::kCallOnSlowPath
: LocationSummary::kNoCall,
kIntrinsified);
if (can_call && kUseBakerReadBarrier) {
locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
}
locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
locations->SetInAt(1, Location::RequiresRegister());
locations->SetInAt(2, Location::RequiresRegister());
locations->SetOut(Location::RequiresRegister(),
(can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
VisitJdkUnsafeGet(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
VisitJdkUnsafeGetVolatile(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
VisitJdkUnsafeGetLong(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
VisitJdkUnsafeGetLongVolatile(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
VisitJdkUnsafeGetObject(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
VisitJdkUnsafeGetObjectVolatile(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGet(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetVolatile(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetAcquire(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetLong(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetLongVolatile(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetLongAcquire(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetObject(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetObjectVolatile(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeGetObjectAcquire(HInvoke* invoke) {
CreateIntIntIntToIntLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
VisitJdkUnsafeGet(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
VisitJdkUnsafeGetVolatile(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
VisitJdkUnsafeGetLong(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
VisitJdkUnsafeGetLongVolatile(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
VisitJdkUnsafeGetObject(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
VisitJdkUnsafeGetObjectVolatile(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGet(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kInt32, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetVolatile(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kInt32, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetAcquire(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kInt32, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetLong(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kInt64, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetLongVolatile(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kInt64, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetLongAcquire(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kInt64, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetObject(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kReference, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetObjectVolatile(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kReference, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeGetObjectAcquire(HInvoke* invoke) {
GenUnsafeGet(invoke, DataType::Type::kReference, /*is_volatile=*/ true, codegen_);
}
static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
DataType::Type type,
HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
locations->SetInAt(1, Location::RequiresRegister());
locations->SetInAt(2, Location::RequiresRegister());
locations->SetInAt(3, Location::RequiresRegister());
if (type == DataType::Type::kReference) {
// Need temp registers for card-marking.
locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
locations->AddTemp(Location::RequiresRegister());
}
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
VisitJdkUnsafePut(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
VisitJdkUnsafePutOrdered(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
VisitJdkUnsafePutVolatile(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
VisitJdkUnsafePutObject(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
VisitJdkUnsafePutObjectOrdered(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
VisitJdkUnsafePutObjectVolatile(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
VisitJdkUnsafePutLong(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
VisitJdkUnsafePutLongOrdered(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
VisitJdkUnsafePutLongVolatile(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePut(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutOrdered(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutVolatile(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutRelease(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutObject(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutObjectOrdered(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutObjectVolatile(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutObjectRelease(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutLong(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutLongOrdered(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutLongVolatile(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafePutLongRelease(HInvoke* invoke) {
CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
}
// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
// memory model.
static void GenUnsafePut(LocationSummary* locations, DataType::Type type, bool is_volatile,
CodeGeneratorX86_64* codegen) {
X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
if (type == DataType::Type::kInt64) {
__ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
} else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
__ movl(temp, value);
__ PoisonHeapReference(temp);
__ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
} else {
__ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
}
if (is_volatile) {
codegen->MemoryFence();
}
if (type == DataType::Type::kReference) {
bool value_can_be_null = true; // TODO: Worth finding out this information?
codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
locations->GetTemp(1).AsRegister<CpuRegister>(),
base,
value,
value_can_be_null);
}
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
VisitJdkUnsafePut(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
VisitJdkUnsafePutOrdered(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
VisitJdkUnsafePutVolatile(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
VisitJdkUnsafePutObject(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
VisitJdkUnsafePutObjectOrdered(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
VisitJdkUnsafePutObjectVolatile(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
VisitJdkUnsafePutLong(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
VisitJdkUnsafePutLongOrdered(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
VisitJdkUnsafePutLongVolatile(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePut(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutOrdered(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutVolatile(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutRelease(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile= */ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutObject(HInvoke* invoke) {
GenUnsafePut(
invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutObjectOrdered(HInvoke* invoke) {
GenUnsafePut(
invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutObjectVolatile(HInvoke* invoke) {
GenUnsafePut(
invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutObjectRelease(HInvoke* invoke) {
GenUnsafePut(
invoke->GetLocations(), DataType::Type::kReference, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutLong(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutLongOrdered(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ false, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutLongVolatile(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ true, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafePutLongRelease(HInvoke* invoke) {
GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /*is_volatile=*/ true, codegen_);
}
static void CreateUnsafeCASLocations(ArenaAllocator* allocator,
DataType::Type type,
HInvoke* invoke) {
const bool can_call = gUseReadBarrier &&
kUseBakerReadBarrier &&
IsUnsafeCASObject(invoke);
LocationSummary* locations =
new (allocator) LocationSummary(invoke,
can_call
? LocationSummary::kCallOnSlowPath
: LocationSummary::kNoCall,
kIntrinsified);
locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
locations->SetInAt(1, Location::RequiresRegister());
locations->SetInAt(2, Location::RequiresRegister());
// expected value must be in EAX/RAX.
locations->SetInAt(3, Location::RegisterLocation(RAX));
locations->SetInAt(4, Location::RequiresRegister());
// RAX is clobbered in CMPXCHG, but we set it as out so no need to add it as temporary.
locations->SetOut(Location::RegisterLocation(RAX));
if (type == DataType::Type::kReference) {
// Need two temporaries for MarkGCCard.
locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
locations->AddTemp(Location::RequiresRegister());
if (gUseReadBarrier) {
// Need three temporaries for GenerateReferenceLoadWithBakerReadBarrier.
DCHECK(kUseBakerReadBarrier);
locations->AddTemp(Location::RequiresRegister());
}
}
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
VisitJdkUnsafeCASInt(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
VisitJdkUnsafeCASLong(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
VisitJdkUnsafeCASObject(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeCASInt(HInvoke* invoke) {
// `jdk.internal.misc.Unsafe.compareAndSwapInt` has compare-and-set semantics (see javadoc).
VisitJdkUnsafeCompareAndSetInt(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeCASLong(HInvoke* invoke) {
// `jdk.internal.misc.Unsafe.compareAndSwapLong` has compare-and-set semantics (see javadoc).
VisitJdkUnsafeCompareAndSetLong(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeCASObject(HInvoke* invoke) {
// `jdk.internal.misc.Unsafe.compareAndSwapObject` has compare-and-set semantics (see javadoc).
VisitJdkUnsafeCompareAndSetObject(invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeCompareAndSetInt(HInvoke* invoke) {
CreateUnsafeCASLocations(allocator_, DataType::Type::kInt32, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeCompareAndSetLong(HInvoke* invoke) {
CreateUnsafeCASLocations(allocator_, DataType::Type::kInt64, invoke);
}
void IntrinsicLocationsBuilderX86_64::VisitJdkUnsafeCompareAndSetObject(HInvoke* invoke) {
// The only supported read barrier implementation is the Baker-style read barriers.
if (gUseReadBarrier && !kUseBakerReadBarrier) {
return;
}
CreateUnsafeCASLocations(allocator_, DataType::Type::kReference, invoke);
}
// Convert ZF into the Boolean result.
static inline void GenZFlagToResult(X86_64Assembler* assembler, CpuRegister out) {
__ setcc(kZero, out);
__ movzxb(out, out);
}
// This function assumes that expected value for CMPXCHG and output are in RAX.
static void GenCompareAndSetOrExchangeInt(CodeGeneratorX86_64* codegen,
DataType::Type type,
Address field_addr,
Location value,
bool is_cmpxchg,
bool byte_swap) {
X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
InstructionCodeGeneratorX86_64* instr_codegen = codegen->GetInstructionCodegen();
if (byte_swap) {
instr_codegen->Bswap(Location::RegisterLocation(RAX), type);
instr_codegen->Bswap(value, type);
}
switch (type) {
case DataType::Type::kBool:
case DataType::Type::kInt8:
__ LockCmpxchgb(field_addr, value.AsRegister<CpuRegister>());
break;
case DataType::Type::kInt16:
case DataType::Type::kUint16:
__ LockCmpxchgw(field_addr, value.AsRegister<CpuRegister>());
break;
case DataType::Type::kInt32:
case DataType::Type::kUint32:
__ LockCmpxchgl(field_addr, value.AsRegister<CpuRegister>());
break;
case DataType::Type::kInt64:
case DataType::Type::kUint64:
__ LockCmpxchgq(field_addr, value.AsRegister<CpuRegister>());
break;
default:
LOG(FATAL) << "Unexpected non-integral CAS type " << type;
}
// LOCK CMPXCHG has full barrier semantics, so we don't need barriers here.
if (byte_swap) {
// Restore byte order for value.
instr_codegen->Bswap(value, type);
}
CpuRegister rax(RAX);
if (is_cmpxchg) {
if (byte_swap) {
instr_codegen->Bswap(Location::RegisterLocation(RAX), type);
}
// Sign-extend or zero-extend the result as necessary.
switch (type) {
case DataType::Type::kBool:
__ movzxb(rax, rax);
break;
case DataType::Type::kInt8:
__ movsxb(rax, rax);
break;
case DataType::Type::kInt16:
__ movsxw(rax, rax);
break;
case DataType::Type::kUint16:
__ movzxw(rax, rax);
break;
default:
break; // No need to do anything.
}
} else {
GenZFlagToResult(assembler, rax);
}
}
static void GenCompareAndSetOrExchangeFP(CodeGeneratorX86_64* codegen,
Address field_addr,
CpuRegister temp,
Location value,
Location expected,
Location out,
bool is64bit,
bool is_cmpxchg,
bool byte_swap) {
X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
InstructionCodeGeneratorX86_64* instr_codegen = codegen->GetInstructionCodegen();
Location rax_loc = Location::RegisterLocation(RAX);
Location temp_loc = Location::RegisterLocation(temp.AsRegister());
DataType::Type type = is64bit ? DataType::Type::kUint64 : DataType::Type::kUint32;
// Copy `expected` to RAX (required by the CMPXCHG instruction).
codegen->Move(rax_loc, expected);
// Copy value to some other register (ensure it's not RAX).
DCHECK_NE(temp.AsRegister(), RAX);
codegen->Move(temp_loc, value);
if (byte_swap) {
instr_codegen->Bswap(rax_loc, type);
instr_codegen->Bswap(temp_loc, type);
}
if (is64bit) {
__ LockCmpxchgq(field_addr, temp);
} else {
__ LockCmpxchgl(field_addr, temp);
}
// LOCK CMPXCHG has full barrier semantics, so we don't need barriers here.
// No need to restore byte order for temporary register.
if (is_cmpxchg) {
if (byte_swap) {
instr_codegen->Bswap(rax_loc, type);
}
__ movd(out.AsFpuRegister<XmmRegister>(), CpuRegister(RAX), is64bit);
} else {
GenZFlagToResult(assembler, out.AsRegister<CpuRegister>());
}
}
// This function assumes that expected value for CMPXCHG and output are in RAX.
static void GenCompareAndSetOrExchangeRef(CodeGeneratorX86_64* codegen,
HInvoke* invoke,
CpuRegister base,
CpuRegister offset,
CpuRegister value,
CpuRegister temp1,
CpuRegister temp2,
CpuRegister temp3,
bool is_cmpxchg) {
// The only supported read barrier implementation is the Baker-style read barriers.
DCHECK_IMPLIES(gUseReadBarrier, kUseBakerReadBarrier);
X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
// Mark card for object assuming new value is stored.
bool value_can_be_null = true; // TODO: Worth finding out this information?
codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
Address field_addr(base, offset, TIMES_1, 0);
if (gUseReadBarrier && kUseBakerReadBarrier) {
// Need to make sure the reference stored in the field is a to-space
// one before attempting the CAS or the CAS could fail incorrectly.
codegen->GenerateReferenceLoadWithBakerReadBarrier(
invoke,
Location::RegisterLocation(temp3.AsRegister()),
base,
field_addr,
/* needs_null_check= */ false,
/* always_update_field= */ true,
&temp1,
&temp2);
} else {
// Nothing to do, the value will be loaded into the out register by CMPXCHG.
}
bool base_equals_value = (base.AsRegister() == value.AsRegister());
Register value_reg = value.AsRegister();
if (kPoisonHeapReferences) {
if (base_equals_value) {
// If `base` and `value` are the same register location, move `value_reg` to a temporary
// register. This way, poisoning `value_reg` won't invalidate `base`.
value_reg = temp1.AsRegister();
__ movl(CpuRegister(value_reg), base);
}
// Check that the register allocator did not assign the location of expected value (RAX) to
// `value` nor to `base`, so that heap poisoning (when enabled) works as intended below.
// - If `value` were equal to RAX, both references would be poisoned twice, meaning they would
// not be poisoned at all, as heap poisoning uses address negation.
// - If `base` were equal to RAX, poisoning RAX would invalidate `base`.
DCHECK_NE(RAX, value_reg);
DCHECK_NE(RAX, base.AsRegister());
__ PoisonHeapReference(CpuRegister(RAX));
__ PoisonHeapReference(CpuRegister(value_reg));
}
__ LockCmpxchgl(field_addr, CpuRegister(value_reg));
// LOCK CMPXCHG has full barrier semantics, so we don't need barriers.
if (is_cmpxchg) {
// Output is in RAX, so we can rely on CMPXCHG and do nothing.
__ MaybeUnpoisonHeapReference(CpuRegister(RAX));
} else {
GenZFlagToResult(assembler, CpuRegister(RAX));
}
// If heap poisoning is enabled, we need to unpoison the values that were poisoned earlier.
if (kPoisonHeapReferences) {
if (base_equals_value) {
// `value_reg` has been moved to a temporary register, no need to unpoison it.
} else {
// Ensure `value` is not RAX, so that unpoisoning the former does not invalidate the latter.
DCHECK_NE(RAX, value_reg);
__ UnpoisonHeapReference(CpuRegister(value_reg));
}
}
}
// In debug mode, return true if all registers are pairwise different. In release mode, do nothing
// and always return true.
static bool RegsAreAllDifferent(const std::vector<CpuRegister>& regs) {
if (kIsDebugBuild) {
for (size_t i = 0; i < regs.size(); ++i) {
for (size_t j = 0; j < i; ++j) {
if (regs[i].AsRegister() == regs[j].AsRegister()) {
return false;
}
}
}
}
return true;
}
// GenCompareAndSetOrExchange handles all value types and therefore accepts generic locations and
// temporary indices that may not correspond to real registers for code paths that do not use them.
static void GenCompareAndSetOrExchange(CodeGeneratorX86_64* codegen,
HInvoke* invoke,
DataType::Type type,
CpuRegister base,
CpuRegister offset,
uint32_t temp1_index,
uint32_t temp2_index,
uint32_t temp3_index,
Location new_value,
Location expected,
Location out,
bool is_cmpxchg,
bool byte_swap) {
LocationSummary* locations = invoke->GetLocations();
Address field_address(base, offset, TIMES_1, 0);
if (DataType::IsFloatingPointType(type)) {
bool is64bit = (type == DataType::Type::kFloat64);
CpuRegister temp = locations->GetTemp(temp1_index).AsRegister<CpuRegister>();
DCHECK(RegsAreAllDifferent({base, offset, temp, CpuRegister(RAX)}));
GenCompareAndSetOrExchangeFP(
codegen, field_address, temp, new_value, expected, out, is64bit, is_cmpxchg, byte_swap);
} else {
// Both the expected value for CMPXCHG and the output are in RAX.
DCHECK_EQ(RAX, expected.AsRegister<Register>());
DCHECK_EQ(RAX, out.AsRegister<Register>());
if (type == DataType::Type::kReference) {
CpuRegister new_value_reg = new_value.AsRegister<CpuRegister>();
CpuRegister temp1 = locations->GetTemp(temp1_index).AsRegister<CpuRegister>();
CpuRegister temp2 = locations->GetTemp(temp2_index).AsRegister<CpuRegister>();
CpuRegister temp3 = gUseReadBarrier
? locations->GetTemp(temp3_index).AsRegister<CpuRegister>()
: CpuRegister(kNoRegister);
DCHECK(RegsAreAllDifferent({base, offset, temp1, temp2, temp3}));
DCHECK(!byte_swap);
GenCompareAndSetOrExchangeRef(
codegen, invoke, base, offset, new_value_reg, temp1, temp2, temp3, is_cmpxchg);
} else {
GenCompareAndSetOrExchangeInt(codegen, type, field_address, new_value, is_cmpxchg, byte_swap);
}
}
}
static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
LocationSummary* locations = invoke->GetLocations();
GenCompareAndSetOrExchange(codegen,
invoke,
type,
/*base=*/ locations->InAt(1).AsRegister<CpuRegister>(),
/*offset=*/ locations->InAt(2).AsRegister<CpuRegister>(),
/*temp1_index=*/ 0,
/*temp2_index=*/ 1,
/*temp3_index=*/ 2,
/*new_value=*/ locations->InAt(4),
/*expected=*/ locations->InAt(3),
locations->Out(),
/*is_cmpxchg=*/ false,
/*byte_swap=*/ false);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
VisitJdkUnsafeCASInt(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
VisitJdkUnsafeCASLong(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
VisitJdkUnsafeCASObject(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeCASInt(HInvoke* invoke) {
// `jdk.internal.misc.Unsafe.compareAndSwapInt` has compare-and-set semantics (see javadoc).
VisitJdkUnsafeCompareAndSetInt(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeCASLong(HInvoke* invoke) {
// `jdk.internal.misc.Unsafe.compareAndSwapLong` has compare-and-set semantics (see javadoc).
VisitJdkUnsafeCompareAndSetLong(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeCASObject(HInvoke* invoke) {
// `jdk.internal.misc.Unsafe.compareAndSwapObject` has compare-and-set semantics (see javadoc).
VisitJdkUnsafeCompareAndSetObject(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeCompareAndSetInt(HInvoke* invoke) {
GenCAS(DataType::Type::kInt32, invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeCompareAndSetLong(HInvoke* invoke) {
GenCAS(DataType::Type::kInt64, invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitJdkUnsafeCompareAndSetObject(HInvoke* invoke) {
// The only supported read barrier implementation is the Baker-style read barriers.
DCHECK_IMPLIES(gUseReadBarrier, kUseBakerReadBarrier);
GenCAS(DataType::Type::kReference, invoke, codegen_);
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
locations->SetOut(Location::SameAsFirstInput());
locations->AddTemp(Location::RequiresRegister());
}
static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
X86_64Assembler* assembler) {
Immediate imm_shift(shift);
Immediate imm_mask(mask);
__ movl(temp, reg);
__ shrl(reg, imm_shift);
__ andl(temp, imm_mask);
__ andl(reg, imm_mask);
__ shll(temp, imm_shift);
__ orl(reg, temp);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
/*
* Use one bswap instruction to reverse byte order first and then use 3 rounds of
* swapping bits to reverse bits in a number x. Using bswap to save instructions
* compared to generic luni implementation which has 5 rounds of swapping bits.
* x = bswap x
* x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
* x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
* x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
*/
__ bswapl(reg);
SwapBits(reg, temp, 1, 0x55555555, assembler);
SwapBits(reg, temp, 2, 0x33333333, assembler);
SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
}
void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
locations->SetOut(Location::SameAsFirstInput());
locations->AddTemp(Location::RequiresRegister());
locations->AddTemp(Location::RequiresRegister());
}
static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
int32_t shift, int64_t mask, X86_64Assembler* assembler) {
Immediate imm_shift(shift);
__ movq(temp_mask, Immediate(mask));
__ movq(temp, reg);
__ shrq(reg, imm_shift);
__ andq(temp, temp_mask);
__ andq(reg, temp_mask);
__ shlq(temp, imm_shift);
__ orq(reg, temp);
}
void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
/*
* Use one bswap instruction to reverse byte order first and then use 3 rounds of
* swapping bits to reverse bits in a long number x. Using bswap to save instructions
* compared to generic luni implementation which has 5 rounds of swapping bits.
* x = bswap x
* x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
* x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
* x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
*/
__ bswapq(reg);
SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
}
static void CreateBitCountLocations(
ArenaAllocator* allocator, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
// Do nothing if there is no popcnt support. This results in generating
// a call for the intrinsic rather than direct code.
return;
}
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::Any());
locations->SetOut(Location::RequiresRegister());
}
static void GenBitCount(X86_64Assembler* assembler,
CodeGeneratorX86_64* codegen,
HInvoke* invoke,
bool is_long) {
LocationSummary* locations = invoke->GetLocations();
Location src = locations->InAt(0);
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
if (invoke->InputAt(0)->IsConstant()) {
// Evaluate this at compile time.
int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
int32_t result = is_long
? POPCOUNT(static_cast<uint64_t>(value))
: POPCOUNT(static_cast<uint32_t>(value));
codegen->Load32BitValue(out, result);
return;
}
if (src.IsRegister()) {
if (is_long) {
__ popcntq(out, src.AsRegister<CpuRegister>());
} else {
__ popcntl(out, src.AsRegister<CpuRegister>());
}
} else if (is_long) {
DCHECK(src.IsDoubleStackSlot());
__ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
} else {
DCHECK(src.IsStackSlot());
__ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
}
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
CreateBitCountLocations(allocator_, codegen_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
GenBitCount(GetAssembler(), codegen_, invoke, /* is_long= */ false);
}
void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
CreateBitCountLocations(allocator_, codegen_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
GenBitCount(GetAssembler(), codegen_, invoke, /* is_long= */ true);
}
static void CreateOneBitLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_high) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::Any());
locations->SetOut(Location::RequiresRegister());
locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
: Location::RequiresRegister()); // any will do
}
static void GenOneBit(X86_64Assembler* assembler,
CodeGeneratorX86_64* codegen,
HInvoke* invoke,
bool is_high, bool is_long) {
LocationSummary* locations = invoke->GetLocations();
Location src = locations->InAt(0);
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
if (invoke->InputAt(0)->IsConstant()) {
// Evaluate this at compile time.
int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
if (value == 0) {
__ xorl(out, out); // Clears upper bits too.
return;
}
// Nonzero value.
if (is_high) {
value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
: 31 - CLZ(static_cast<uint32_t>(value));
} else {
value = is_long ? CTZ(static_cast<uint64_t>(value))
: CTZ(static_cast<uint32_t>(value));
}
if (is_long) {
codegen->Load64BitValue(out, 1ULL << value);
} else {
codegen->Load32BitValue(out, 1 << value);
}
return;
}
// Handle the non-constant cases.
if (!is_high && codegen->GetInstructionSetFeatures().HasAVX2() &&
src.IsRegister()) {
__ blsi(out, src.AsRegister<CpuRegister>());
} else {
CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
if (is_high) {
// Use architectural support: basically 1 << bsr.
if (src.IsRegister()) {
if (is_long) {
__ bsrq(tmp, src.AsRegister<CpuRegister>());
} else {
__ bsrl(tmp, src.AsRegister<CpuRegister>());
}
} else if (is_long) {
DCHECK(src.IsDoubleStackSlot());
__ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
} else {
DCHECK(src.IsStackSlot());
__ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
}
// BSR sets ZF if the input was zero.
NearLabel is_zero, done;
__ j(kEqual, &is_zero);
__ movl(out, Immediate(1)); // Clears upper bits too.
if (is_long) {
__ shlq(out, tmp);
} else {
__ shll(out, tmp);
}
__ jmp(&done);
__ Bind(&is_zero);
__ xorl(out, out); // Clears upper bits too.
__ Bind(&done);
} else {
// Copy input into temporary.
if (src.IsRegister()) {
if (is_long) {
__ movq(tmp, src.AsRegister<CpuRegister>());
} else {
__ movl(tmp, src.AsRegister<CpuRegister>());
}
} else if (is_long) {
DCHECK(src.IsDoubleStackSlot());
__ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
} else {
DCHECK(src.IsStackSlot());
__ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
}
// Do the bit twiddling: basically tmp & -tmp;
if (is_long) {
__ movq(out, tmp);
__ negq(tmp);
__ andq(out, tmp);
} else {
__ movl(out, tmp);
__ negl(tmp);
__ andl(out, tmp);
}
}
}
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
CreateOneBitLocations(allocator_, invoke, /* is_high= */ true);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ true, /* is_long= */ false);
}
void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
CreateOneBitLocations(allocator_, invoke, /* is_high= */ true);
}
void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ true, /* is_long= */ true);
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
CreateOneBitLocations(allocator_, invoke, /* is_high= */ false);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ false, /* is_long= */ false);
}
void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
CreateOneBitLocations(allocator_, invoke, /* is_high= */ false);
}
void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ false, /* is_long= */ true);
}
static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::Any());
locations->SetOut(Location::RequiresRegister());
}
static void GenLeadingZeros(X86_64Assembler* assembler,
CodeGeneratorX86_64* codegen,
HInvoke* invoke, bool is_long) {
LocationSummary* locations = invoke->GetLocations();
Location src = locations->InAt(0);
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
int zero_value_result = is_long ? 64 : 32;
if (invoke->InputAt(0)->IsConstant()) {
// Evaluate this at compile time.
int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
if (value == 0) {
value = zero_value_result;
} else {
value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
}
codegen->Load32BitValue(out, value);
return;
}
// Handle the non-constant cases.
if (src.IsRegister()) {
if (is_long) {
__ bsrq(out, src.AsRegister<CpuRegister>());
} else {
__ bsrl(out, src.AsRegister<CpuRegister>());
}
} else if (is_long) {
DCHECK(src.IsDoubleStackSlot());
__ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
} else {
DCHECK(src.IsStackSlot());
__ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
}
// BSR sets ZF if the input was zero, and the output is undefined.
NearLabel is_zero, done;
__ j(kEqual, &is_zero);
// Correct the result from BSR to get the CLZ result.
__ xorl(out, Immediate(zero_value_result - 1));
__ jmp(&done);
// Fix the zero case with the expected result.
__ Bind(&is_zero);
__ movl(out, Immediate(zero_value_result));
__ Bind(&done);
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
CreateLeadingZeroLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ false);
}
void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
CreateLeadingZeroLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ true);
}
static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::Any());
locations->SetOut(Location::RequiresRegister());
}
static void GenTrailingZeros(X86_64Assembler* assembler,
CodeGeneratorX86_64* codegen,
HInvoke* invoke, bool is_long) {
LocationSummary* locations = invoke->GetLocations();
Location src = locations->InAt(0);
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
int zero_value_result = is_long ? 64 : 32;
if (invoke->InputAt(0)->IsConstant()) {
// Evaluate this at compile time.
int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
if (value == 0) {
value = zero_value_result;
} else {
value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
}
codegen->Load32BitValue(out, value);
return;
}
// Handle the non-constant cases.
if (src.IsRegister()) {
if (is_long) {
__ bsfq(out, src.AsRegister<CpuRegister>());
} else {
__ bsfl(out, src.AsRegister<CpuRegister>());
}
} else if (is_long) {
DCHECK(src.IsDoubleStackSlot());
__ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
} else {
DCHECK(src.IsStackSlot());
__ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
}
// BSF sets ZF if the input was zero, and the output is undefined.
NearLabel done;
__ j(kNotEqual, &done);
// Fix the zero case with the expected result.
__ movl(out, Immediate(zero_value_result));
__ Bind(&done);
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
CreateTrailingZeroLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ false);
}
void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
CreateTrailingZeroLocations(allocator_, invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ true);
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerValueOf(HInvoke* invoke) {
InvokeRuntimeCallingConvention calling_convention;
IntrinsicVisitor::ComputeIntegerValueOfLocations(
invoke,
codegen_,
Location::RegisterLocation(RAX),
Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerValueOf(HInvoke* invoke) {
IntrinsicVisitor::IntegerValueOfInfo info =
IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
LocationSummary* locations = invoke->GetLocations();
X86_64Assembler* assembler = GetAssembler();
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
InvokeRuntimeCallingConvention calling_convention;
CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
auto allocate_instance = [&]() {
codegen_->LoadIntrinsicDeclaringClass(argument, invoke);
codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
};
if (invoke->InputAt(0)->IsIntConstant()) {
int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
if (static_cast<uint32_t>(value - info.low) < info.length) {
// Just embed the j.l.Integer in the code.
DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
codegen_->LoadBootImageAddress(out, info.value_boot_image_reference);
} else {
DCHECK(locations->CanCall());
// Allocate and initialize a new j.l.Integer.
// TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
// JIT object table.
allocate_instance();
__ movl(Address(out, info.value_offset), Immediate(value));
}
} else {
DCHECK(locations->CanCall());
CpuRegister in = locations->InAt(0).AsRegister<CpuRegister>();
// Check bounds of our cache.
__ leal(out, Address(in, -info.low));
__ cmpl(out, Immediate(info.length));
NearLabel allocate, done;
__ j(kAboveEqual, &allocate);
// If the value is within the bounds, load the j.l.Integer directly from the array.
DCHECK_NE(out.AsRegister(), argument.AsRegister());
codegen_->LoadBootImageAddress(argument, info.array_data_boot_image_reference);
static_assert((1u << TIMES_4) == sizeof(mirror::HeapReference<mirror::Object>),
"Check heap reference size.");
__ movl(out, Address(argument, out, TIMES_4, 0));
__ MaybeUnpoisonHeapReference(out);
__ jmp(&done);
__ Bind(&allocate);
// Otherwise allocate and initialize a new j.l.Integer.
allocate_instance();
__ movl(Address(out, info.value_offset), in);
__ Bind(&done);
}
}
void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
IntrinsicVisitor::CreateReferenceGetReferentLocations(invoke, codegen_);
}
void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
Location obj = locations->InAt(0);
Location out = locations->Out();
SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen_->AddSlowPath(slow_path);
if (gUseReadBarrier) {
// Check self->GetWeakRefAccessEnabled().
ThreadOffset64 offset = Thread::WeakRefAccessEnabledOffset<kX86_64PointerSize>();
__ gs()->cmpl(Address::Absolute(offset, /* no_rip= */ true),
Immediate(enum_cast<int32_t>(WeakRefAccessState::kVisiblyEnabled)));
__ j(kNotEqual, slow_path->GetEntryLabel());
}
// Load the java.lang.ref.Reference class, use the output register as a temporary.
codegen_->LoadIntrinsicDeclaringClass(out.AsRegister<CpuRegister>(), invoke);
// Check static fields java.lang.ref.Reference.{disableIntrinsic,slowPathEnabled} together.
MemberOffset disable_intrinsic_offset = IntrinsicVisitor::GetReferenceDisableIntrinsicOffset();
DCHECK_ALIGNED(disable_intrinsic_offset.Uint32Value(), 2u);
DCHECK_EQ(disable_intrinsic_offset.Uint32Value() + 1u,
IntrinsicVisitor::GetReferenceSlowPathEnabledOffset().Uint32Value());
__ cmpw(Address(out.AsRegister<CpuRegister>(), disable_intrinsic_offset.Uint32Value()),
Immediate(0));
__ j(kNotEqual, slow_path->GetEntryLabel());
// Load the value from the field.
uint32_t referent_offset = mirror::Reference::ReferentOffset().Uint32Value();
if (gUseReadBarrier && kUseBakerReadBarrier) {
codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
out,
obj.AsRegister<CpuRegister>(),
referent_offset,
/*needs_null_check=*/ true);
// Note that the fence is a no-op, thanks to the x86-64 memory model.
codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); // `referent` is volatile.
} else {
__ movl(out.AsRegister<CpuRegister>(), Address(obj.AsRegister<CpuRegister>(), referent_offset));
codegen_->MaybeRecordImplicitNullCheck(invoke);
// Note that the fence is a no-op, thanks to the x86-64 memory model.
codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); // `referent` is volatile.
codegen_->MaybeGenerateReadBarrierSlow(invoke, out, out, obj, referent_offset);
}
__ Bind(slow_path->GetExitLabel());
}
void IntrinsicLocationsBuilderX86_64::VisitReferenceRefersTo(HInvoke* invoke) {
IntrinsicVisitor::CreateReferenceRefersToLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitReferenceRefersTo(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister other = locations->InAt(1).AsRegister<CpuRegister>();
CpuRegister out = locations->Out().AsRegister<CpuRegister>();
uint32_t referent_offset = mirror::Reference::ReferentOffset().Uint32Value();
uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
__ movl(out, Address(obj, referent_offset));
codegen_->MaybeRecordImplicitNullCheck(invoke);
__ MaybeUnpoisonHeapReference(out);
// Note that the fence is a no-op, thanks to the x86-64 memory model.
codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny); // `referent` is volatile.
__ cmpl(out, other);
if (gUseReadBarrier) {
DCHECK(kUseBakerReadBarrier);
NearLabel calculate_result;
__ j(kEqual, &calculate_result); // ZF set if taken.
// Check if the loaded reference is null in a way that leaves ZF clear for null.
__ cmpl(out, Immediate(1));
__ j(kBelow, &calculate_result); // ZF clear if taken.
// For correct memory visibility, we need a barrier before loading the lock word
// but we already have the barrier emitted for volatile load above which is sufficient.
// Load the lockword and check if it is a forwarding address.
static_assert(LockWord::kStateShift == 30u);
static_assert(LockWord::kStateForwardingAddress == 3u);
__ movl(out, Address(out, monitor_offset));
__ cmpl(out, Immediate(static_cast<int32_t>(0xc0000000)));
__ j(kBelow, &calculate_result); // ZF clear if taken.
// Extract the forwarding address and compare with `other`.
__ shll(out, Immediate(LockWord::kForwardingAddressShift));
__ cmpl(out, other);
__ Bind(&calculate_result);
}
// Convert ZF into the Boolean result.
__ setcc(kEqual, out);
__ movzxb(out, out);
}
void IntrinsicLocationsBuilderX86_64::VisitThreadInterrupted(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetOut(Location::RequiresRegister());
}
void IntrinsicCodeGeneratorX86_64::VisitThreadInterrupted(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Address address = Address::Absolute
(Thread::InterruptedOffset<kX86_64PointerSize>().Int32Value(), /* no_rip= */ true);
NearLabel done;
__ gs()->movl(out, address);
__ testl(out, out);
__ j(kEqual, &done);
__ gs()->movl(address, Immediate(0));
codegen_->MemoryFence();
__ Bind(&done);
}
void IntrinsicLocationsBuilderX86_64::VisitReachabilityFence(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::Any());
}
void IntrinsicCodeGeneratorX86_64::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
static void CreateDivideUnsignedLocations(HInvoke* invoke, ArenaAllocator* allocator) {
LocationSummary* locations =
new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
locations->SetInAt(0, Location::RegisterLocation(RAX));
locations->SetInAt(1, Location::RequiresRegister());
locations->SetOut(Location::SameAsFirstInput());
// Intel uses edx:eax as the dividend.
locations->AddTemp(Location::RegisterLocation(RDX));
}
static void GenerateDivideUnsigned(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
DataType::Type data_type) {
LocationSummary* locations = invoke->GetLocations();
Location out = locations->Out();
Location first = locations->InAt(0);
Location second = locations->InAt(1);
CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
CpuRegister second_reg = second.AsRegister<CpuRegister>();
DCHECK_EQ(RAX, first.AsRegister<Register>());
DCHECK_EQ(RAX, out.AsRegister<Register>());
DCHECK_EQ(RDX, rdx.AsRegister());
// We check if the divisor is zero and bail to the slow path to handle if so.
auto* slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
codegen->AddSlowPath(slow_path);
X86_64Assembler* assembler = codegen->GetAssembler();
if (data_type == DataType::Type::kInt32) {
__ testl(second_reg, second_reg);
__ j(kEqual, slow_path->GetEntryLabel());
__ xorl(rdx, rdx);
__ divl(second_reg);
} else {
DCHECK(data_type == DataType::Type::kInt64);
__ testq(second_reg, second_reg);
__ j(kEqual, slow_path->GetEntryLabel());
__ xorq(rdx, rdx);
__ divq(second_reg);
}
__ Bind(slow_path->GetExitLabel());
}
void IntrinsicLocationsBuilderX86_64::VisitIntegerDivideUnsigned(HInvoke* invoke) {
CreateDivideUnsignedLocations(invoke, allocator_);
}
void IntrinsicCodeGeneratorX86_64::VisitIntegerDivideUnsigned(HInvoke* invoke) {
GenerateDivideUnsigned(invoke, codegen_, DataType::Type::kInt32);
}
void IntrinsicLocationsBuilderX86_64::VisitLongDivideUnsigned(HInvoke* invoke) {
CreateDivideUnsignedLocations(invoke, allocator_);
}
void IntrinsicCodeGeneratorX86_64::VisitLongDivideUnsigned(HInvoke* invoke) {
GenerateDivideUnsigned(invoke, codegen_, DataType::Type::kInt64);
}
void IntrinsicLocationsBuilderX86_64::VisitMathMultiplyHigh(HInvoke* invoke) {
LocationSummary* locations =
new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
locations->SetInAt(0, Location::RegisterLocation(RAX));
locations->SetInAt(1, Location::RequiresRegister());
locations->SetOut(Location::RegisterLocation(RDX));
locations->AddTemp(Location::RegisterLocation(RAX));
}
void IntrinsicCodeGeneratorX86_64::VisitMathMultiplyHigh(HInvoke* invoke) {
X86_64Assembler* assembler = GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister y = locations->InAt(1).AsRegister<CpuRegister>();
DCHECK_EQ(locations->InAt(0).AsRegister<Register>(), RAX);
DCHECK_EQ(locations->Out().AsRegister<Register>(), RDX);
__ imulq(y);
}
enum class GetAndUpdateOp {
kSet,
kAdd,
kBitwiseAnd,
kBitwiseOr,
kBitwiseXor
};
class VarHandleSlowPathX86_64 : public IntrinsicSlowPathX86_64 {
public:
explicit VarHandleSlowPathX86_64(HInvoke* invoke)
: IntrinsicSlowPathX86_64(invoke) {
}
void SetVolatile(bool is_volatile) {
is_volatile_ = is_volatile;
}
void SetAtomic(bool is_atomic) {
is_atomic_ = is_atomic;
}
void SetNeedAnyStoreBarrier(bool need_any_store_barrier) {
need_any_store_barrier_ = need_any_store_barrier;
}
void SetNeedAnyAnyBarrier(bool need_any_any_barrier) {
need_any_any_barrier_ = need_any_any_barrier;
}
void SetGetAndUpdateOp(GetAndUpdateOp get_and_update_op) {
get_and_update_op_ = get_and_update_op;
}
Label* GetByteArrayViewCheckLabel() {
return &byte_array_view_check_label_;
}
Label* GetNativeByteOrderLabel() {
return &native_byte_order_label_;
}
void EmitNativeCode(CodeGenerator* codegen) override {
if (GetByteArrayViewCheckLabel()->IsLinked()) {
EmitByteArrayViewCode(down_cast<CodeGeneratorX86_64*>(codegen));
}
IntrinsicSlowPathX86_64::EmitNativeCode(codegen);
}
private:
HInvoke* GetInvoke() const {
return GetInstruction()->AsInvoke();
}
mirror::VarHandle::AccessModeTemplate GetAccessModeTemplate() const {
return mirror::VarHandle::GetAccessModeTemplateByIntrinsic(GetInvoke()->GetIntrinsic());
}
void EmitByteArrayViewCode(CodeGeneratorX86_64* codegen);
Label byte_array_view_check_label_;
Label native_byte_order_label_;
// Arguments forwarded to specific methods.
bool is_volatile_;
bool is_atomic_;
bool need_any_store_barrier_;
bool need_any_any_barrier_;
GetAndUpdateOp get_and_update_op_;
};
static void GenerateMathFma(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
DCHECK(DataType::IsFloatingPointType(invoke->GetType()));
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
DCHECK(locations->InAt(0).Equals(locations->Out()));
XmmRegister left = locations->InAt(0).AsFpuRegister<XmmRegister>();
XmmRegister right = locations->InAt(1).AsFpuRegister<XmmRegister>();
XmmRegister accumulator = locations->InAt(2).AsFpuRegister<XmmRegister>();
if (invoke->GetType() == DataType::Type::kFloat32) {
__ vfmadd213ss(left, right, accumulator);
} else {
DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64);
__ vfmadd213sd(left, right, accumulator);
}
}
void IntrinsicCodeGeneratorX86_64::VisitMathFmaDouble(HInvoke* invoke) {
DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
GenerateMathFma(invoke, codegen_);
}
void IntrinsicLocationsBuilderX86_64::VisitMathFmaDouble(HInvoke* invoke) {
if (codegen_->GetInstructionSetFeatures().HasAVX2()) {
CreateFPFPFPToFPCallLocations(allocator_, invoke);
}
}
void IntrinsicCodeGeneratorX86_64::VisitMathFmaFloat(HInvoke* invoke) {
DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
GenerateMathFma(invoke, codegen_);
}
void IntrinsicLocationsBuilderX86_64::VisitMathFmaFloat(HInvoke* invoke) {
if (codegen_->GetInstructionSetFeatures().HasAVX2()) {
CreateFPFPFPToFPCallLocations(allocator_, invoke);
}
}
// Generate subtype check without read barriers.
static void GenerateSubTypeObjectCheckNoReadBarrier(CodeGeneratorX86_64* codegen,
VarHandleSlowPathX86_64* slow_path,
CpuRegister object,
CpuRegister temp,
Address type_address,
bool object_can_be_null = true) {
X86_64Assembler* assembler = codegen->GetAssembler();
const MemberOffset class_offset = mirror::Object::ClassOffset();
const MemberOffset super_class_offset = mirror::Class::SuperClassOffset();
NearLabel check_type_compatibility, type_matched;
// If the object is null, there is no need to check the type
if (object_can_be_null) {
__ testl(object, object);
__ j(kZero, &type_matched);
}
// Do not unpoison for in-memory comparison.
// We deliberately avoid the read barrier, letting the slow path handle the false negatives.
__ movl(temp, Address(object, class_offset));
__ Bind(&check_type_compatibility);
__ cmpl(temp, type_address);
__ j(kEqual, &type_matched);
// Load the super class.
__ MaybeUnpoisonHeapReference(temp);
__ movl(temp, Address(temp, super_class_offset));
// If the super class is null, we reached the root of the hierarchy without a match.
// We let the slow path handle uncovered cases (e.g. interfaces).
__ testl(temp, temp);
__ j(kEqual, slow_path->GetEntryLabel());
__ jmp(&check_type_compatibility);
__ Bind(&type_matched);
}
// Check access mode and the primitive type from VarHandle.varType.
// Check reference arguments against the VarHandle.varType; for references this is a subclass
// check without read barrier, so it can have false negatives which we handle in the slow path.
static void GenerateVarHandleAccessModeAndVarTypeChecks(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
VarHandleSlowPathX86_64* slow_path,
DataType::Type type) {
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister varhandle = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
mirror::VarHandle::AccessMode access_mode =
mirror::VarHandle::GetAccessModeByIntrinsic(invoke->GetIntrinsic());
Primitive::Type primitive_type = DataTypeToPrimitive(type);
const MemberOffset var_type_offset = mirror::VarHandle::VarTypeOffset();
const MemberOffset access_mode_bit_mask_offset = mirror::VarHandle::AccessModesBitMaskOffset();
const MemberOffset primitive_type_offset = mirror::Class::PrimitiveTypeOffset();
// Check that the operation is permitted.
__ testl(Address(varhandle, access_mode_bit_mask_offset),
Immediate(1u << static_cast<uint32_t>(access_mode)));
__ j(kZero, slow_path->GetEntryLabel());
// For primitive types, we do not need a read barrier when loading a reference only for loading
// constant field through the reference. For reference types, we deliberately avoid the read
// barrier, letting the slow path handle the false negatives.
__ movl(temp, Address(varhandle, var_type_offset));
__ MaybeUnpoisonHeapReference(temp);
// Check check the varType.primitiveType field against the type we're trying to retrieve.
__ cmpw(Address(temp, primitive_type_offset), Immediate(static_cast<uint16_t>(primitive_type)));
__ j(kNotEqual, slow_path->GetEntryLabel());
if (type == DataType::Type::kReference) {
// Check reference arguments against the varType.
// False negatives due to varType being an interface or array type
// or due to the missing read barrier are handled by the slow path.
size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
uint32_t arguments_start = /* VarHandle object */ 1u + expected_coordinates_count;
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
for (size_t arg_index = arguments_start; arg_index != number_of_arguments; ++arg_index) {
HInstruction* arg = invoke->InputAt(arg_index);
DCHECK_EQ(arg->GetType(), DataType::Type::kReference);
if (!arg->IsNullConstant()) {
CpuRegister arg_reg = invoke->GetLocations()->InAt(arg_index).AsRegister<CpuRegister>();
Address type_addr(varhandle, var_type_offset);
GenerateSubTypeObjectCheckNoReadBarrier(codegen, slow_path, arg_reg, temp, type_addr);
}
}
}
}
static void GenerateVarHandleStaticFieldCheck(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
VarHandleSlowPathX86_64* slow_path) {
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister varhandle = locations->InAt(0).AsRegister<CpuRegister>();
const MemberOffset coordinate_type0_offset = mirror::VarHandle::CoordinateType0Offset();
// Check that the VarHandle references a static field by checking that coordinateType0 == null.
// Do not emit read barrier (or unpoison the reference) for comparing to null.
__ cmpl(Address(varhandle, coordinate_type0_offset), Immediate(0));
__ j(kNotEqual, slow_path->GetEntryLabel());
}
static void GenerateVarHandleInstanceFieldChecks(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
VarHandleSlowPathX86_64* slow_path) {
VarHandleOptimizations optimizations(invoke);
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister varhandle = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister object = locations->InAt(1).AsRegister<CpuRegister>();
CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
const MemberOffset coordinate_type0_offset = mirror::VarHandle::CoordinateType0Offset();
const MemberOffset coordinate_type1_offset = mirror::VarHandle::CoordinateType1Offset();
// Null-check the object.
if (!optimizations.GetSkipObjectNullCheck()) {
__ testl(object, object);
__ j(kZero, slow_path->GetEntryLabel());
}
if (!optimizations.GetUseKnownBootImageVarHandle()) {
// Check that the VarHandle references an instance field by checking that
// coordinateType1 == null. coordinateType0 should be not null, but this is handled by the
// type compatibility check with the source object's type, which will fail for null.
__ cmpl(Address(varhandle, coordinate_type1_offset), Immediate(0));
__ j(kNotEqual, slow_path->GetEntryLabel());
// Check that the object has the correct type.
// We deliberately avoid the read barrier, letting the slow path handle the false negatives.
GenerateSubTypeObjectCheckNoReadBarrier(codegen,
slow_path,
object,
temp,
Address(varhandle, coordinate_type0_offset),
/*object_can_be_null=*/ false);
}
}
static void GenerateVarHandleArrayChecks(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
VarHandleSlowPathX86_64* slow_path) {
VarHandleOptimizations optimizations(invoke);
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
CpuRegister varhandle = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister object = locations->InAt(1).AsRegister<CpuRegister>();
CpuRegister index = locations->InAt(2).AsRegister<CpuRegister>();
DataType::Type value_type =
GetVarHandleExpectedValueType(invoke, /*expected_coordinates_count=*/ 2u);
Primitive::Type primitive_type = DataTypeToPrimitive(value_type);
const MemberOffset coordinate_type0_offset = mirror::VarHandle::CoordinateType0Offset();
const MemberOffset coordinate_type1_offset = mirror::VarHandle::CoordinateType1Offset();
const MemberOffset component_type_offset = mirror::Class::ComponentTypeOffset();
const MemberOffset primitive_type_offset = mirror::Class::PrimitiveTypeOffset();
const MemberOffset class_offset = mirror::Object::ClassOffset();
const MemberOffset array_length_offset = mirror::Array::LengthOffset();
// Null-check the object.
if (!optimizations.GetSkipObjectNullCheck()) {
__ testl(object, object);
__ j(kZero, slow_path->GetEntryLabel());
}
CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
// Check that the VarHandle references an array, byte array view or ByteBuffer by checking
// that coordinateType1 != null. If that's true, coordinateType1 shall be int.class and
// coordinateType0 shall not be null but we do not explicitly verify that.
// No need for read barrier or unpoisoning of coordinateType1 for comparison with null.
__ cmpl(Address(varhandle, coordinate_type1_offset.Int32Value()), Immediate(0));
__ j(kEqual, slow_path->GetEntryLabel());
// Check object class against componentType0.
//
// This is an exact check and we defer other cases to the runtime. This includes
// conversion to array of superclass references, which is valid but subsequently
// requires all update operations to check that the value can indeed be stored.
// We do not want to perform such extra checks in the intrinsified code.
//
// We do this check without read barrier, so there can be false negatives which we
// defer to the slow path. There shall be no false negatives for array classes in the
// boot image (including Object[] and primitive arrays) because they are non-movable.
__ movl(temp, Address(object, class_offset.Int32Value()));
__ cmpl(temp, Address(varhandle, coordinate_type0_offset.Int32Value()));
__ j(kNotEqual, slow_path->GetEntryLabel());
// Check that the coordinateType0 is an array type. We do not need a read barrier
// for loading constant reference fields (or chains of them) for comparison with null,
// nor for finally loading a constant primitive field (primitive type) below.
codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
__ movl(temp, Address(temp, component_type_offset.Int32Value()));
codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
__ testl(temp, temp);
__ j(kZero, slow_path->GetEntryLabel());
// Check that the array component type matches the primitive type.
Label* slow_path_label;
if (primitive_type == Primitive::kPrimNot) {
slow_path_label = slow_path->GetEntryLabel();
} else {
// With the exception of `kPrimNot` (handled above), `kPrimByte` and `kPrimBoolean`,
// we shall check for a byte array view in the slow path.
// The check requires the ByteArrayViewVarHandle.class to be in the boot image,
// so we cannot emit that if we're JITting without boot image.
bool boot_image_available =
codegen->GetCompilerOptions().IsBootImage() ||
!Runtime::Current()->GetHeap()->GetBootImageSpaces().empty();
bool can_be_view = (DataType::Size(value_type) != 1u) && boot_image_available;
slow_path_label =
can_be_view ? slow_path->GetByteArrayViewCheckLabel() : slow_path->GetEntryLabel();
}
__ cmpw(Address(temp, primitive_type_offset), Immediate(static_cast<uint16_t>(primitive_type)));
__ j(kNotEqual, slow_path_label);
// Check for array index out of bounds.
__ cmpl(index, Address(object, array_length_offset.Int32Value()));
__ j(kAboveEqual, slow_path->GetEntryLabel());
}
static void GenerateVarHandleCoordinateChecks(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
VarHandleSlowPathX86_64* slow_path) {
size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
if (expected_coordinates_count == 0u) {
GenerateVarHandleStaticFieldCheck(invoke, codegen, slow_path);
} else if (expected_coordinates_count == 1u) {
GenerateVarHandleInstanceFieldChecks(invoke, codegen, slow_path);
} else {
DCHECK_EQ(expected_coordinates_count, 2u);
GenerateVarHandleArrayChecks(invoke, codegen, slow_path);
}
}
static VarHandleSlowPathX86_64* GenerateVarHandleChecks(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
DataType::Type type) {
size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
VarHandleOptimizations optimizations(invoke);
if (optimizations.GetUseKnownBootImageVarHandle()) {
DCHECK_NE(expected_coordinates_count, 2u);
if (expected_coordinates_count == 0u || optimizations.GetSkipObjectNullCheck()) {
return nullptr;
}
}
VarHandleSlowPathX86_64* slow_path =
new (codegen->GetScopedAllocator()) VarHandleSlowPathX86_64(invoke);
codegen->AddSlowPath(slow_path);
if (!optimizations.GetUseKnownBootImageVarHandle()) {
GenerateVarHandleAccessModeAndVarTypeChecks(invoke, codegen, slow_path, type);
}
GenerateVarHandleCoordinateChecks(invoke, codegen, slow_path);
return slow_path;
}
struct VarHandleTarget {
Register object; // The object holding the value to operate on.
Register offset; // The offset of the value to operate on.
};
static VarHandleTarget GetVarHandleTarget(HInvoke* invoke) {
size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
LocationSummary* locations = invoke->GetLocations();
VarHandleTarget target;
// The temporary allocated for loading the offset.
target.offset = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
// The reference to the object that holds the value to operate on.
target.object = (expected_coordinates_count == 0u)
? locations->GetTemp(1).AsRegister<CpuRegister>().AsRegister()
: locations->InAt(1).AsRegister<CpuRegister>().AsRegister();
return target;
}
static void GenerateVarHandleTarget(HInvoke* invoke,
const VarHandleTarget& target,
CodeGeneratorX86_64* codegen) {
LocationSummary* locations = invoke->GetLocations();
X86_64Assembler* assembler = codegen->GetAssembler();
size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
CpuRegister varhandle = locations->InAt(0).AsRegister<CpuRegister>();
if (expected_coordinates_count <= 1u) {
if (VarHandleOptimizations(invoke).GetUseKnownBootImageVarHandle()) {
ScopedObjectAccess soa(Thread::Current());
ArtField* target_field = GetBootImageVarHandleField(invoke);
if (expected_coordinates_count == 0u) {
ObjPtr<mirror::Class> declaring_class = target_field->GetDeclaringClass();
__ movl(CpuRegister(target.object),
Address::Absolute(CodeGeneratorX86_64::kPlaceholder32BitOffset, /*no_rip=*/ false));
if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(declaring_class)) {
codegen->RecordBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(declaring_class));
} else {
codegen->RecordBootImageTypePatch(declaring_class->GetDexFile(),
declaring_class->GetDexTypeIndex());
}
}
__ movl(CpuRegister(target.offset), Immediate(target_field->GetOffset().Uint32Value()));
} else {
// For static fields, we need to fill the `target.object` with the declaring class,
// so we can use `target.object` as temporary for the `ArtMethod*`. For instance fields,
// we do not need the declaring class, so we can forget the `ArtMethod*` when
// we load the `target.offset`, so use the `target.offset` to hold the `ArtMethod*`.
CpuRegister method((expected_coordinates_count == 0) ? target.object : target.offset);
const MemberOffset art_field_offset = mirror::FieldVarHandle::ArtFieldOffset();
const MemberOffset offset_offset = ArtField::OffsetOffset();
// Load the ArtField, the offset and, if needed, declaring class.
__ movq(method, Address(varhandle, art_field_offset));
__ movl(CpuRegister(target.offset), Address(method, offset_offset));
if (expected_coordinates_count == 0u) {
InstructionCodeGeneratorX86_64* instr_codegen = codegen->GetInstructionCodegen();
instr_codegen->GenerateGcRootFieldLoad(invoke,
Location::RegisterLocation(target.object),
Address(method, ArtField::DeclaringClassOffset()),
/*fixup_label=*/ nullptr,
gCompilerReadBarrierOption);
}
}
} else {
DCHECK_EQ(expected_coordinates_count, 2u);
DataType::Type value_type =
GetVarHandleExpectedValueType(invoke, /*expected_coordinates_count=*/ 2u);
ScaleFactor scale = CodeGenerator::ScaleFactorForType(value_type);
MemberOffset data_offset = mirror::Array::DataOffset(DataType::Size(value_type));
CpuRegister index = locations->InAt(2).AsRegister<CpuRegister>();
// The effect of LEA is `target.offset = index * scale + data_offset`.
__ leal(CpuRegister(target.offset), Address(index, scale, data_offset.Int32Value()));
}
}
static bool HasVarHandleIntrinsicImplementation(HInvoke* invoke) {
// The only supported read barrier implementation is the Baker-style read barriers.
if (gUseReadBarrier && !kUseBakerReadBarrier) {
return false;
}
VarHandleOptimizations optimizations(invoke);
if (optimizations.GetDoNotIntrinsify()) {
return false;
}
size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
DCHECK_LE(expected_coordinates_count, 2u); // Filtered by the `DoNotIntrinsify` flag above.
return true;
}
static LocationSummary* CreateVarHandleCommonLocations(HInvoke* invoke) {
size_t expected_coordinates_count = GetExpectedVarHandleCoordinatesCount(invoke);
ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
LocationSummary* locations = new (allocator) LocationSummary(
invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
locations->SetInAt(0, Location::RequiresRegister());
// Require coordinates in registers. These are the object holding the value
// to operate on (except for static fields) and index (for arrays and views).
for (size_t i = 0; i != expected_coordinates_count; ++i) {
locations->SetInAt(/* VarHandle object */ 1u + i, Location::RequiresRegister());
}
uint32_t arguments_start = /* VarHandle object */ 1u + expected_coordinates_count;
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
for (size_t arg_index = arguments_start; arg_index != number_of_arguments; ++arg_index) {
HInstruction* arg = invoke->InputAt(arg_index);
if (DataType::IsFloatingPointType(arg->GetType())) {
locations->SetInAt(arg_index, Location::FpuRegisterOrConstant(arg));
} else {
locations->SetInAt(arg_index, Location::RegisterOrConstant(arg));
}
}
// Add a temporary for offset.
locations->AddTemp(Location::RequiresRegister());
if (expected_coordinates_count == 0u) {
// Add a temporary to hold the declaring class.
locations->AddTemp(Location::RequiresRegister());
}
return locations;
}
static void CreateVarHandleGetLocations(HInvoke* invoke) {
if (!HasVarHandleIntrinsicImplementation(invoke)) {
return;
}
LocationSummary* locations = CreateVarHandleCommonLocations(invoke);
if (DataType::IsFloatingPointType(invoke->GetType())) {
locations->SetOut(Location::RequiresFpuRegister());
} else {
locations->SetOut(Location::RequiresRegister());
}
}
static void GenerateVarHandleGet(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
bool byte_swap = false) {
DataType::Type type = invoke->GetType();
DCHECK_NE(type, DataType::Type::kVoid);
LocationSummary* locations = invoke->GetLocations();
X86_64Assembler* assembler = codegen->GetAssembler();
VarHandleTarget target = GetVarHandleTarget(invoke);
VarHandleSlowPathX86_64* slow_path = nullptr;
if (!byte_swap) {
slow_path = GenerateVarHandleChecks(invoke, codegen, type);
GenerateVarHandleTarget(invoke, target, codegen);
if (slow_path != nullptr) {
__ Bind(slow_path->GetNativeByteOrderLabel());
}
}
// Load the value from the field
Address src(CpuRegister(target.object), CpuRegister(target.offset), TIMES_1, 0);
Location out = locations->Out();
if (type == DataType::Type::kReference) {
if (gUseReadBarrier) {
DCHECK(kUseBakerReadBarrier);
codegen->GenerateReferenceLoadWithBakerReadBarrier(
invoke, out, CpuRegister(target.object), src, /* needs_null_check= */ false);
} else {
__ movl(out.AsRegister<CpuRegister>(), src);
__ MaybeUnpoisonHeapReference(out.AsRegister<CpuRegister>());
}
DCHECK(!byte_swap);
} else {
codegen->LoadFromMemoryNoReference(type, out, src);
if (byte_swap) {
CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
codegen->GetInstructionCodegen()->Bswap(out, type, &temp);
}
}
if (slow_path != nullptr) {
DCHECK(!byte_swap);
__ Bind(slow_path->GetExitLabel());
}
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGet(HInvoke* invoke) {
CreateVarHandleGetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGet(HInvoke* invoke) {
GenerateVarHandleGet(invoke, codegen_);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAcquire(HInvoke* invoke) {
CreateVarHandleGetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAcquire(HInvoke* invoke) {
// VarHandleGetAcquire is the same as VarHandleGet on x86-64 due to the x86 memory model.
GenerateVarHandleGet(invoke, codegen_);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetOpaque(HInvoke* invoke) {
CreateVarHandleGetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetOpaque(HInvoke* invoke) {
// VarHandleGetOpaque is the same as VarHandleGet on x86-64 due to the x86 memory model.
GenerateVarHandleGet(invoke, codegen_);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetVolatile(HInvoke* invoke) {
CreateVarHandleGetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetVolatile(HInvoke* invoke) {
// VarHandleGetVolatile is the same as VarHandleGet on x86-64 due to the x86 memory model.
GenerateVarHandleGet(invoke, codegen_);
}
static void CreateVarHandleSetLocations(HInvoke* invoke) {
if (!HasVarHandleIntrinsicImplementation(invoke)) {
return;
}
LocationSummary* locations = CreateVarHandleCommonLocations(invoke);
// Extra temporary is used for card in MarkGCCard and to move 64-bit constants to memory.
locations->AddTemp(Location::RequiresRegister());
}
static void GenerateVarHandleSet(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
bool is_volatile,
bool is_atomic,
bool byte_swap = false) {
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
const uint32_t last_temp_index = locations->GetTempCount() - 1;
uint32_t value_index = invoke->GetNumberOfArguments() - 1;
DataType::Type value_type = GetDataTypeFromShorty(invoke, value_index);
VarHandleTarget target = GetVarHandleTarget(invoke);
VarHandleSlowPathX86_64* slow_path = nullptr;
if (!byte_swap) {
slow_path = GenerateVarHandleChecks(invoke, codegen, value_type);
GenerateVarHandleTarget(invoke, target, codegen);
if (slow_path != nullptr) {
slow_path->SetVolatile(is_volatile);
slow_path->SetAtomic(is_atomic);
__ Bind(slow_path->GetNativeByteOrderLabel());
}
}
switch (invoke->GetIntrinsic()) {
case Intrinsics::kVarHandleSetRelease:
codegen->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
break;
case Intrinsics::kVarHandleSetVolatile:
// setVolatile needs kAnyStore barrier, but HandleFieldSet takes care of that.
break;
default:
// Other intrinsics don't need a barrier.
break;
}
Address dst(CpuRegister(target.object), CpuRegister(target.offset), TIMES_1, 0);
// Store the value to the field.
codegen->GetInstructionCodegen()->HandleFieldSet(
invoke,
value_index,
last_temp_index,
value_type,
dst,
CpuRegister(target.object),
is_volatile,
is_atomic,
/*value_can_be_null=*/true,
byte_swap,
// Value can be null, and this write barrier is not being relied on for other sets.
WriteBarrierKind::kEmitWithNullCheck);
// setVolatile needs kAnyAny barrier, but HandleFieldSet takes care of that.
if (slow_path != nullptr) {
DCHECK(!byte_swap);
__ Bind(slow_path->GetExitLabel());
}
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleSet(HInvoke* invoke) {
CreateVarHandleSetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleSet(HInvoke* invoke) {
GenerateVarHandleSet(invoke, codegen_, /*is_volatile=*/ false, /*is_atomic=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleSetOpaque(HInvoke* invoke) {
CreateVarHandleSetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleSetOpaque(HInvoke* invoke) {
GenerateVarHandleSet(invoke, codegen_, /*is_volatile=*/ false, /*is_atomic=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleSetRelease(HInvoke* invoke) {
CreateVarHandleSetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleSetRelease(HInvoke* invoke) {
GenerateVarHandleSet(invoke, codegen_, /*is_volatile=*/ false, /*is_atomic=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleSetVolatile(HInvoke* invoke) {
CreateVarHandleSetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleSetVolatile(HInvoke* invoke) {
GenerateVarHandleSet(invoke, codegen_, /*is_volatile=*/ true, /*is_atomic=*/ true);
}
static void CreateVarHandleCompareAndSetOrExchangeLocations(HInvoke* invoke) {
if (!HasVarHandleIntrinsicImplementation(invoke)) {
return;
}
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
uint32_t expected_value_index = number_of_arguments - 2;
uint32_t new_value_index = number_of_arguments - 1;
DataType::Type return_type = invoke->GetType();
DataType::Type expected_type = GetDataTypeFromShorty(invoke, expected_value_index);
DCHECK_EQ(expected_type, GetDataTypeFromShorty(invoke, new_value_index));
LocationSummary* locations = CreateVarHandleCommonLocations(invoke);
if (DataType::IsFloatingPointType(return_type)) {
locations->SetOut(Location::RequiresFpuRegister());
} else {
// Take advantage of the fact that CMPXCHG writes result to RAX.
locations->SetOut(Location::RegisterLocation(RAX));
}
if (DataType::IsFloatingPointType(expected_type)) {
// RAX is needed to load the expected floating-point value into a register for CMPXCHG.
locations->AddTemp(Location::RegisterLocation(RAX));
// Another temporary is needed to load the new floating-point value into a register for CMPXCHG.
locations->AddTemp(Location::RequiresRegister());
} else {
// Ensure that expected value is in RAX, as required by CMPXCHG.
locations->SetInAt(expected_value_index, Location::RegisterLocation(RAX));
locations->SetInAt(new_value_index, Location::RequiresRegister());
if (expected_type == DataType::Type::kReference) {
// Need two temporaries for MarkGCCard.
locations->AddTemp(Location::RequiresRegister());
locations->AddTemp(Location::RequiresRegister());
if (gUseReadBarrier) {
// Need three temporaries for GenerateReferenceLoadWithBakerReadBarrier.
DCHECK(kUseBakerReadBarrier);
locations->AddTemp(Location::RequiresRegister());
}
}
// RAX is clobbered in CMPXCHG, but no need to mark it as temporary as it's the output register.
DCHECK_EQ(RAX, locations->Out().AsRegister<Register>());
}
}
static void GenerateVarHandleCompareAndSetOrExchange(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
bool is_cmpxchg,
bool byte_swap = false) {
DCHECK_IMPLIES(gUseReadBarrier, kUseBakerReadBarrier);
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
uint32_t expected_value_index = number_of_arguments - 2;
uint32_t new_value_index = number_of_arguments - 1;
DataType::Type type = GetDataTypeFromShorty(invoke, expected_value_index);
VarHandleSlowPathX86_64* slow_path = nullptr;
VarHandleTarget target = GetVarHandleTarget(invoke);
if (!byte_swap) {
slow_path = GenerateVarHandleChecks(invoke, codegen, type);
GenerateVarHandleTarget(invoke, target, codegen);
if (slow_path != nullptr) {
__ Bind(slow_path->GetNativeByteOrderLabel());
}
}
uint32_t temp_count = locations->GetTempCount();
GenCompareAndSetOrExchange(codegen,
invoke,
type,
CpuRegister(target.object),
CpuRegister(target.offset),
/*temp1_index=*/ temp_count - 1,
/*temp2_index=*/ temp_count - 2,
/*temp3_index=*/ temp_count - 3,
locations->InAt(new_value_index),
locations->InAt(expected_value_index),
locations->Out(),
is_cmpxchg,
byte_swap);
// We are using LOCK CMPXCHG in all cases because there is no CAS equivalent that has weak
// failure semantics. LOCK CMPXCHG has full barrier semantics, so we don't need barriers.
if (slow_path != nullptr) {
DCHECK(!byte_swap);
__ Bind(slow_path->GetExitLabel());
}
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleCompareAndSet(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleCompareAndSet(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleWeakCompareAndSet(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleWeakCompareAndSet(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleWeakCompareAndSetPlain(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleWeakCompareAndSetPlain(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleWeakCompareAndSetAcquire(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleWeakCompareAndSetAcquire(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleWeakCompareAndSetRelease(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleWeakCompareAndSetRelease(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleCompareAndExchange(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleCompareAndExchange(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleCompareAndExchangeAcquire(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleCompareAndExchangeAcquire(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleCompareAndExchangeRelease(HInvoke* invoke) {
CreateVarHandleCompareAndSetOrExchangeLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleCompareAndExchangeRelease(HInvoke* invoke) {
GenerateVarHandleCompareAndSetOrExchange(invoke, codegen_, /*is_cmpxchg=*/ true);
}
static void CreateVarHandleGetAndSetLocations(HInvoke* invoke) {
if (!HasVarHandleIntrinsicImplementation(invoke)) {
return;
}
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
uint32_t new_value_index = number_of_arguments - 1;
DataType::Type type = invoke->GetType();
DCHECK_EQ(type, GetDataTypeFromShorty(invoke, new_value_index));
LocationSummary* locations = CreateVarHandleCommonLocations(invoke);
if (DataType::IsFloatingPointType(type)) {
locations->SetOut(Location::RequiresFpuRegister());
// A temporary is needed to load the new floating-point value into a register for XCHG.
locations->AddTemp(Location::RequiresRegister());
} else {
// Use the same register for both the new value and output to take advantage of XCHG.
// It doesn't have to be RAX, but we need to choose some to make sure it's the same.
locations->SetOut(Location::RegisterLocation(RAX));
locations->SetInAt(new_value_index, Location::RegisterLocation(RAX));
if (type == DataType::Type::kReference) {
// Need two temporaries for MarkGCCard.
locations->AddTemp(Location::RequiresRegister());
locations->AddTemp(Location::RequiresRegister());
if (gUseReadBarrier) {
// Need a third temporary for GenerateReferenceLoadWithBakerReadBarrier.
DCHECK(kUseBakerReadBarrier);
locations->AddTemp(Location::RequiresRegister());
}
}
}
}
static void GenerateVarHandleGetAndSet(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
Location value,
DataType::Type type,
Address field_addr,
CpuRegister ref,
bool byte_swap) {
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
Location out = locations->Out();
uint32_t temp_count = locations->GetTempCount();
if (DataType::IsFloatingPointType(type)) {
// `getAndSet` for floating-point types: move the new FP value into a register, atomically
// exchange it with the field, and move the old value into the output FP register.
Location temp = locations->GetTemp(temp_count - 1);
codegen->Move(temp, value);
bool is64bit = (type == DataType::Type::kFloat64);
DataType::Type bswap_type = is64bit ? DataType::Type::kUint64 : DataType::Type::kUint32;
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(temp, bswap_type);
}
if (is64bit) {
__ xchgq(temp.AsRegister<CpuRegister>(), field_addr);
} else {
__ xchgl(temp.AsRegister<CpuRegister>(), field_addr);
}
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(temp, bswap_type);
}
__ movd(out.AsFpuRegister<XmmRegister>(), temp.AsRegister<CpuRegister>(), is64bit);
} else if (type == DataType::Type::kReference) {
// `getAndSet` for references: load reference and atomically exchange it with the field.
// Output register is the same as the one holding new value, so no need to move the result.
DCHECK(!byte_swap);
CpuRegister temp1 = locations->GetTemp(temp_count - 1).AsRegister<CpuRegister>();
CpuRegister temp2 = locations->GetTemp(temp_count - 2).AsRegister<CpuRegister>();
CpuRegister valreg = value.AsRegister<CpuRegister>();
if (gUseReadBarrier && kUseBakerReadBarrier) {
codegen->GenerateReferenceLoadWithBakerReadBarrier(
invoke,
locations->GetTemp(temp_count - 3),
ref,
field_addr,
/*needs_null_check=*/ false,
/*always_update_field=*/ true,
&temp1,
&temp2);
}
codegen->MarkGCCard(temp1, temp2, ref, valreg, /* emit_null_check= */ false);
DCHECK_EQ(valreg, out.AsRegister<CpuRegister>());
if (kPoisonHeapReferences) {
// Use a temp to avoid poisoning base of the field address, which might happen if `valreg` is
// the same as `target.object` (for code like `vh.getAndSet(obj, obj)`).
__ movl(temp1, valreg);
__ PoisonHeapReference(temp1);
__ xchgl(temp1, field_addr);
__ UnpoisonHeapReference(temp1);
__ movl(valreg, temp1);
} else {
__ xchgl(valreg, field_addr);
}
} else {
// `getAndSet` for integral types: atomically exchange the new value with the field. Output
// register is the same as the one holding new value. Do sign extend / zero extend as needed.
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(value, type);
}
CpuRegister valreg = value.AsRegister<CpuRegister>();
DCHECK_EQ(valreg, out.AsRegister<CpuRegister>());
switch (type) {
case DataType::Type::kBool:
case DataType::Type::kUint8:
__ xchgb(valreg, field_addr);
__ movzxb(valreg, valreg);
break;
case DataType::Type::kInt8:
__ xchgb(valreg, field_addr);
__ movsxb(valreg, valreg);
break;
case DataType::Type::kUint16:
__ xchgw(valreg, field_addr);
__ movzxw(valreg, valreg);
break;
case DataType::Type::kInt16:
__ xchgw(valreg, field_addr);
__ movsxw(valreg, valreg);
break;
case DataType::Type::kInt32:
case DataType::Type::kUint32:
__ xchgl(valreg, field_addr);
break;
case DataType::Type::kInt64:
case DataType::Type::kUint64:
__ xchgq(valreg, field_addr);
break;
default:
DCHECK(false) << "unexpected type in getAndSet intrinsic";
UNREACHABLE();
}
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(value, type);
}
}
}
static void CreateVarHandleGetAndBitwiseOpLocations(HInvoke* invoke) {
if (!HasVarHandleIntrinsicImplementation(invoke)) {
return;
}
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
uint32_t new_value_index = number_of_arguments - 1;
DataType::Type type = invoke->GetType();
DCHECK_EQ(type, GetDataTypeFromShorty(invoke, new_value_index));
LocationSummary* locations = CreateVarHandleCommonLocations(invoke);
DCHECK_NE(DataType::Type::kReference, type);
DCHECK(!DataType::IsFloatingPointType(type));
// A temporary to compute the bitwise operation on the old and the new values.
locations->AddTemp(Location::RequiresRegister());
// We need value to be either in a register, or a 32-bit constant (as there are no arithmetic
// instructions that accept 64-bit immediate on x86_64).
locations->SetInAt(new_value_index, DataType::Is64BitType(type)
? Location::RequiresRegister()
: Location::RegisterOrConstant(invoke->InputAt(new_value_index)));
// Output is in RAX to accommodate CMPXCHG. It is also used as a temporary.
locations->SetOut(Location::RegisterLocation(RAX));
}
static void GenerateVarHandleGetAndOp(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
Location value,
DataType::Type type,
Address field_addr,
GetAndUpdateOp get_and_update_op,
bool byte_swap) {
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
Location temp_loc = locations->GetTemp(locations->GetTempCount() - 1);
Location rax_loc = locations->Out();
CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
CpuRegister rax = rax_loc.AsRegister<CpuRegister>();
DCHECK_EQ(rax.AsRegister(), RAX);
bool is64Bit = DataType::Is64BitType(type);
NearLabel retry;
__ Bind(&retry);
// Load field value into RAX and copy it into a temporary register for the operation.
codegen->LoadFromMemoryNoReference(type, Location::RegisterLocation(RAX), field_addr);
codegen->Move(temp_loc, rax_loc);
if (byte_swap) {
// Byte swap the temporary, since we need to perform operation in native endianness.
codegen->GetInstructionCodegen()->Bswap(temp_loc, type);
}
DCHECK_IMPLIES(value.IsConstant(), !is64Bit);
int32_t const_value = value.IsConstant()
? CodeGenerator::GetInt32ValueOf(value.GetConstant())
: 0;
// Use 32-bit registers for 8/16/32-bit types to save on the REX prefix.
switch (get_and_update_op) {
case GetAndUpdateOp::kAdd:
DCHECK(byte_swap); // The non-byte-swapping path should use a faster XADD instruction.
if (is64Bit) {
__ addq(temp, value.AsRegister<CpuRegister>());
} else if (value.IsConstant()) {
__ addl(temp, Immediate(const_value));
} else {
__ addl(temp, value.AsRegister<CpuRegister>());
}
break;
case GetAndUpdateOp::kBitwiseAnd:
if (is64Bit) {
__ andq(temp, value.AsRegister<CpuRegister>());
} else if (value.IsConstant()) {
__ andl(temp, Immediate(const_value));
} else {
__ andl(temp, value.AsRegister<CpuRegister>());
}
break;
case GetAndUpdateOp::kBitwiseOr:
if (is64Bit) {
__ orq(temp, value.AsRegister<CpuRegister>());
} else if (value.IsConstant()) {
__ orl(temp, Immediate(const_value));
} else {
__ orl(temp, value.AsRegister<CpuRegister>());
}
break;
case GetAndUpdateOp::kBitwiseXor:
if (is64Bit) {
__ xorq(temp, value.AsRegister<CpuRegister>());
} else if (value.IsConstant()) {
__ xorl(temp, Immediate(const_value));
} else {
__ xorl(temp, value.AsRegister<CpuRegister>());
}
break;
default:
DCHECK(false) << "unexpected operation";
UNREACHABLE();
}
if (byte_swap) {
// RAX still contains the original value, but we need to byte swap the temporary back.
codegen->GetInstructionCodegen()->Bswap(temp_loc, type);
}
switch (type) {
case DataType::Type::kBool:
case DataType::Type::kUint8:
case DataType::Type::kInt8:
__ LockCmpxchgb(field_addr, temp);
break;
case DataType::Type::kUint16:
case DataType::Type::kInt16:
__ LockCmpxchgw(field_addr, temp);
break;
case DataType::Type::kInt32:
case DataType::Type::kUint32:
__ LockCmpxchgl(field_addr, temp);
break;
case DataType::Type::kInt64:
case DataType::Type::kUint64:
__ LockCmpxchgq(field_addr, temp);
break;
default:
DCHECK(false) << "unexpected type in getAndBitwiseOp intrinsic";
UNREACHABLE();
}
__ j(kNotZero, &retry);
// The result is in RAX after CMPXCHG. Byte swap if necessary, but do not sign/zero extend,
// as it has already been done by `LoadFromMemoryNoReference` above (and not altered by CMPXCHG).
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(rax_loc, type);
}
}
static void CreateVarHandleGetAndAddLocations(HInvoke* invoke) {
if (!HasVarHandleIntrinsicImplementation(invoke)) {
return;
}
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
uint32_t new_value_index = number_of_arguments - 1;
DataType::Type type = invoke->GetType();
DCHECK_EQ(type, GetDataTypeFromShorty(invoke, new_value_index));
LocationSummary* locations = CreateVarHandleCommonLocations(invoke);
if (DataType::IsFloatingPointType(type)) {
locations->SetOut(Location::RequiresFpuRegister());
// Require that the new FP value is in a register (and not a constant) for ADDSS/ADDSD.
locations->SetInAt(new_value_index, Location::RequiresFpuRegister());
// CMPXCHG clobbers RAX.
locations->AddTemp(Location::RegisterLocation(RAX));
// An FP temporary to load the old value from the field and perform FP addition.
locations->AddTemp(Location::RequiresFpuRegister());
// A temporary to hold the new value for CMPXCHG.
locations->AddTemp(Location::RequiresRegister());
} else {
DCHECK_NE(type, DataType::Type::kReference);
// Use the same register for both the new value and output to take advantage of XADD.
// It should be RAX, because the byte-swapping path of GenerateVarHandleGetAndAdd falls
// back to GenerateVarHandleGetAndOp that expects out in RAX.
locations->SetOut(Location::RegisterLocation(RAX));
locations->SetInAt(new_value_index, Location::RegisterLocation(RAX));
if (GetExpectedVarHandleCoordinatesCount(invoke) == 2) {
// For byte array views with non-native endianness we need extra BSWAP operations, so we
// cannot use XADD and have to fallback to a generic implementation based on CMPXCH. In that
// case we need two temporary registers: one to hold value instead of RAX (which may get
// clobbered by repeated CMPXCHG) and one for performing the operation. At compile time we
// cannot distinguish this case from arrays or native-endian byte array views.
locations->AddTemp(Location::RequiresRegister());
locations->AddTemp(Location::RequiresRegister());
}
}
}
static void GenerateVarHandleGetAndAdd(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
Location value,
DataType::Type type,
Address field_addr,
bool byte_swap) {
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
Location out = locations->Out();
uint32_t temp_count = locations->GetTempCount();
if (DataType::IsFloatingPointType(type)) {
if (byte_swap) {
// This code should never be executed: it is the case of a byte array view (since it requires
// a byte swap), and varhandles for byte array views support numeric atomic update access mode
// only for int and long, but not for floating-point types (see javadoc comments for
// java.lang.invoke.MethodHandles.byteArrayViewVarHandle()). But ART varhandle implementation
// for byte array views treats floating-point types them as numeric types in
// ByteArrayViewVarHandle::Access(). Terefore we do generate intrinsic code, but it always
// fails access mode check at runtime prior to reaching this point. Illegal instruction UD2
// ensures that if control flow gets here by mistake, we will notice.
__ ud2();
}
// `getAndAdd` for floating-point types: load the old FP value into a temporary FP register and
// in RAX for CMPXCHG, add the new FP value to the old one, move it to a non-FP temporary for
// CMPXCHG and loop until CMPXCHG succeeds. Move the result from RAX to the output FP register.
bool is64bit = (type == DataType::Type::kFloat64);
DataType::Type bswap_type = is64bit ? DataType::Type::kUint64 : DataType::Type::kUint32;
XmmRegister fptemp = locations->GetTemp(temp_count - 2).AsFpuRegister<XmmRegister>();
Location rax_loc = Location::RegisterLocation(RAX);
Location temp_loc = locations->GetTemp(temp_count - 1);
CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
NearLabel retry;
__ Bind(&retry);
// Read value from memory into an FP register and copy in into RAX.
if (is64bit) {
__ movsd(fptemp, field_addr);
} else {
__ movss(fptemp, field_addr);
}
__ movd(CpuRegister(RAX), fptemp, is64bit);
// If necessary, byte swap RAX and update the value in FP register to also be byte-swapped.
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(rax_loc, bswap_type);
__ movd(fptemp, CpuRegister(RAX), is64bit);
}
// Perform the FP addition and move it to a temporary register to prepare for CMPXCHG.
if (is64bit) {
__ addsd(fptemp, value.AsFpuRegister<XmmRegister>());
} else {
__ addss(fptemp, value.AsFpuRegister<XmmRegister>());
}
__ movd(temp, fptemp, is64bit);
// If necessary, byte swap RAX before CMPXCHG and the temporary before copying to FP register.
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(temp_loc, bswap_type);
codegen->GetInstructionCodegen()->Bswap(rax_loc, bswap_type);
}
if (is64bit) {
__ LockCmpxchgq(field_addr, temp);
} else {
__ LockCmpxchgl(field_addr, temp);
}
__ j(kNotZero, &retry);
// The old value is in RAX, byte swap if necessary.
if (byte_swap) {
codegen->GetInstructionCodegen()->Bswap(rax_loc, bswap_type);
}
__ movd(out.AsFpuRegister<XmmRegister>(), CpuRegister(RAX), is64bit);
} else {
if (byte_swap) {
// We cannot use XADD since we need to byte-swap the old value when reading it from memory,
// and then byte-swap the sum before writing it to memory. So fallback to the slower generic
// implementation that is also used for bitwise operations.
// Move value from RAX to a temporary register, as RAX may get clobbered by repeated CMPXCHG.
DCHECK_EQ(GetExpectedVarHandleCoordinatesCount(invoke), 2u);
Location temp = locations->GetTemp(temp_count - 2);
codegen->Move(temp, value);
GenerateVarHandleGetAndOp(
invoke, codegen, temp, type, field_addr, GetAndUpdateOp::kAdd, byte_swap);
} else {
// `getAndAdd` for integral types: atomically exchange the new value with the field and add
// the old value to the field. Output register is the same as the one holding new value. Do
// sign extend / zero extend as needed.
CpuRegister valreg = value.AsRegister<CpuRegister>();
DCHECK_EQ(valreg, out.AsRegister<CpuRegister>());
switch (type) {
case DataType::Type::kBool:
case DataType::Type::kUint8:
__ LockXaddb(field_addr, valreg);
__ movzxb(valreg, valreg);
break;
case DataType::Type::kInt8:
__ LockXaddb(field_addr, valreg);
__ movsxb(valreg, valreg);
break;
case DataType::Type::kUint16:
__ LockXaddw(field_addr, valreg);
__ movzxw(valreg, valreg);
break;
case DataType::Type::kInt16:
__ LockXaddw(field_addr, valreg);
__ movsxw(valreg, valreg);
break;
case DataType::Type::kInt32:
case DataType::Type::kUint32:
__ LockXaddl(field_addr, valreg);
break;
case DataType::Type::kInt64:
case DataType::Type::kUint64:
__ LockXaddq(field_addr, valreg);
break;
default:
DCHECK(false) << "unexpected type in getAndAdd intrinsic";
UNREACHABLE();
}
}
}
}
static void GenerateVarHandleGetAndUpdate(HInvoke* invoke,
CodeGeneratorX86_64* codegen,
GetAndUpdateOp get_and_update_op,
bool need_any_store_barrier,
bool need_any_any_barrier,
bool byte_swap = false) {
DCHECK_IMPLIES(gUseReadBarrier, kUseBakerReadBarrier);
X86_64Assembler* assembler = codegen->GetAssembler();
LocationSummary* locations = invoke->GetLocations();
uint32_t number_of_arguments = invoke->GetNumberOfArguments();
Location value = locations->InAt(number_of_arguments - 1);
DataType::Type type = invoke->GetType();
VarHandleSlowPathX86_64* slow_path = nullptr;
VarHandleTarget target = GetVarHandleTarget(invoke);
if (!byte_swap) {
slow_path = GenerateVarHandleChecks(invoke, codegen, type);
GenerateVarHandleTarget(invoke, target, codegen);
if (slow_path != nullptr) {
slow_path->SetGetAndUpdateOp(get_and_update_op);
slow_path->SetNeedAnyStoreBarrier(need_any_store_barrier);
slow_path->SetNeedAnyAnyBarrier(need_any_any_barrier);
__ Bind(slow_path->GetNativeByteOrderLabel());
}
}
CpuRegister ref(target.object);
Address field_addr(ref, CpuRegister(target.offset), TIMES_1, 0);
if (need_any_store_barrier) {
codegen->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
}
switch (get_and_update_op) {
case GetAndUpdateOp::kSet:
GenerateVarHandleGetAndSet(invoke, codegen, value, type, field_addr, ref, byte_swap);
break;
case GetAndUpdateOp::kAdd:
GenerateVarHandleGetAndAdd(invoke, codegen, value, type, field_addr, byte_swap);
break;
case GetAndUpdateOp::kBitwiseAnd:
case GetAndUpdateOp::kBitwiseOr:
case GetAndUpdateOp::kBitwiseXor:
GenerateVarHandleGetAndOp(
invoke, codegen, value, type, field_addr, get_and_update_op, byte_swap);
break;
}
if (need_any_any_barrier) {
codegen->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
}
if (slow_path != nullptr) {
DCHECK(!byte_swap);
__ Bind(slow_path->GetExitLabel());
}
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndSet(HInvoke* invoke) {
CreateVarHandleGetAndSetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndSet(HInvoke* invoke) {
// `getAndSet` has `getVolatile` + `setVolatile` semantics, so it needs both barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kSet,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndSetAcquire(HInvoke* invoke) {
CreateVarHandleGetAndSetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndSetAcquire(HInvoke* invoke) {
// `getAndSetAcquire` has `getAcquire` + `set` semantics, so it doesn't need any barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kSet,
/*need_any_store_barrier=*/ false,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndSetRelease(HInvoke* invoke) {
CreateVarHandleGetAndSetLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndSetRelease(HInvoke* invoke) {
// `getAndSetRelease` has `get` + `setRelease` semantics, so it needs `kAnyStore` barrier.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kSet,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndAdd(HInvoke* invoke) {
CreateVarHandleGetAndAddLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndAdd(HInvoke* invoke) {
// `getAndAdd` has `getVolatile` + `setVolatile` semantics, so it needs both barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kAdd,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndAddAcquire(HInvoke* invoke) {
CreateVarHandleGetAndAddLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndAddAcquire(HInvoke* invoke) {
// `getAndAddAcquire` has `getAcquire` + `set` semantics, so it doesn't need any barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kAdd,
/*need_any_store_barrier=*/ false,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndAddRelease(HInvoke* invoke) {
CreateVarHandleGetAndAddLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndAddRelease(HInvoke* invoke) {
// `getAndAddRelease` has `get` + `setRelease` semantics, so it needs `kAnyStore` barrier.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kAdd,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseAnd(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseAnd(HInvoke* invoke) {
// `getAndBitwiseAnd` has `getVolatile` + `setVolatile` semantics, so it needs both barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseAnd,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseAndAcquire(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseAndAcquire(HInvoke* invoke) {
// `getAndBitwiseAndAcquire` has `getAcquire` + `set` semantics, so it doesn't need any barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseAnd,
/*need_any_store_barrier=*/ false,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseAndRelease(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseAndRelease(HInvoke* invoke) {
// `getAndBitwiseAndRelease` has `get` + `setRelease` semantics, so it needs `kAnyStore` barrier.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseAnd,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseOr(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseOr(HInvoke* invoke) {
// `getAndBitwiseOr` has `getVolatile` + `setVolatile` semantics, so it needs both barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseOr,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseOrAcquire(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseOrAcquire(HInvoke* invoke) {
// `getAndBitwiseOrAcquire` has `getAcquire` + `set` semantics, so it doesn't need any barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseOr,
/*need_any_store_barrier=*/ false,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseOrRelease(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseOrRelease(HInvoke* invoke) {
// `getAndBitwiseOrRelease` has `get` + `setRelease` semantics, so it needs `kAnyStore` barrier.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseOr,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseXor(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseXor(HInvoke* invoke) {
// `getAndBitwiseXor` has `getVolatile` + `setVolatile` semantics, so it needs both barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseXor,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ true);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseXorAcquire(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseXorAcquire(HInvoke* invoke) {
// `getAndBitwiseXorAcquire` has `getAcquire` + `set` semantics, so it doesn't need any barriers.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseXor,
/*need_any_store_barrier=*/ false,
/*need_any_any_barrier=*/ false);
}
void IntrinsicLocationsBuilderX86_64::VisitVarHandleGetAndBitwiseXorRelease(HInvoke* invoke) {
CreateVarHandleGetAndBitwiseOpLocations(invoke);
}
void IntrinsicCodeGeneratorX86_64::VisitVarHandleGetAndBitwiseXorRelease(HInvoke* invoke) {
// `getAndBitwiseXorRelease` has `get` + `setRelease` semantics, so it needs `kAnyStore` barrier.
GenerateVarHandleGetAndUpdate(invoke,
codegen_,
GetAndUpdateOp::kBitwiseXor,
/*need_any_store_barrier=*/ true,
/*need_any_any_barrier=*/ false);
}
void VarHandleSlowPathX86_64::EmitByteArrayViewCode(CodeGeneratorX86_64* codegen) {
DCHECK(GetByteArrayViewCheckLabel()->IsLinked());
X86_64Assembler* assembler = codegen->GetAssembler();
HInvoke* invoke = GetInvoke();
LocationSummary* locations = invoke->GetLocations();
mirror::VarHandle::AccessModeTemplate access_mode_template = GetAccessModeTemplate();
DataType::Type value_type =
GetVarHandleExpectedValueType(invoke, /*expected_coordinates_count=*/ 2u);
DCHECK_NE(value_type, DataType::Type::kReference);
size_t size = DataType::Size(value_type);
DCHECK_GT(size, 1u);
CpuRegister varhandle = locations->InAt(0).AsRegister<CpuRegister>();
CpuRegister object = locations->InAt(1).AsRegister<CpuRegister>();
CpuRegister index = locations->InAt(2).AsRegister<CpuRegister>();
CpuRegister temp = locations->GetTemp(locations->GetTempCount() - 1).AsRegister<CpuRegister>();
MemberOffset class_offset = mirror::Object::ClassOffset();
MemberOffset array_length_offset = mirror::Array::LengthOffset();
MemberOffset data_offset = mirror::Array::DataOffset(Primitive::kPrimByte);
MemberOffset native_byte_order_offset = mirror::ByteArrayViewVarHandle::NativeByteOrderOffset();
VarHandleTarget target = GetVarHandleTarget(invoke);
__ Bind(GetByteArrayViewCheckLabel());
// The main path checked that the coordinateType0 is an array class that matches
// the class of the actual coordinate argument but it does not match the value type.
// Check if the `varhandle` references a ByteArrayViewVarHandle instance.
codegen->LoadClassRootForIntrinsic(temp, ClassRoot::kJavaLangInvokeByteArrayViewVarHandle);
assembler->MaybePoisonHeapReference(temp);
__ cmpl(temp, Address(varhandle, class_offset.Int32Value()));
__ j(kNotEqual, GetEntryLabel());
// Check for array index out of bounds.
__ movl(temp, Address(object, array_length_offset.Int32Value()));
// SUB sets flags in the same way as CMP.
__ subl(temp, index);
__ j(kBelowEqual, GetEntryLabel());
// The difference between index and array length must be enough for the `value_type` size.
__ cmpl(temp, Immediate(size));
__ j(kBelow, GetEntryLabel());
// Construct the target.
__ leal(CpuRegister(target.offset), Address(index, TIMES_1, data_offset.Int32Value()));
// Alignment check. For unaligned access, go to the runtime.
DCHECK(IsPowerOfTwo(size));
__ testl(CpuRegister(target.offset), Immediate(size - 1u));
__ j(kNotZero, GetEntryLabel());
// Byte order check. For native byte order return to the main path.
if (access_mode_template == mirror::VarHandle::AccessModeTemplate::kSet &&
IsZeroBitPattern(invoke->InputAt(invoke->GetNumberOfArguments() - 1u))) {
// There is no reason to differentiate between native byte order and byte-swap
// for setting a zero bit pattern. Just return to the main path.
__ jmp(GetNativeByteOrderLabel());
return;
}
__ cmpl(Address(varhandle, native_byte_order_offset.Int32Value()), Immediate(0));
__ j(kNotEqual, GetNativeByteOrderLabel());
switch (access_mode_template) {
case mirror::VarHandle::AccessModeTemplate::kGet:
GenerateVarHandleGet(invoke, codegen, /*byte_swap=*/ true);
break;
case mirror::VarHandle::AccessModeTemplate::kSet:
GenerateVarHandleSet(invoke, codegen, is_volatile_, is_atomic_, /*byte_swap=*/ true);
break;
case mirror::VarHandle::AccessModeTemplate::kCompareAndSet:
GenerateVarHandleCompareAndSetOrExchange(
invoke, codegen, /*is_cmpxchg=*/ false, /*byte_swap=*/ true);
break;
case mirror::VarHandle::AccessModeTemplate::kCompareAndExchange:
GenerateVarHandleCompareAndSetOrExchange(
invoke, codegen, /*is_cmpxchg=*/ true, /*byte_swap=*/ true);
break;
case mirror::VarHandle::AccessModeTemplate::kGetAndUpdate:
GenerateVarHandleGetAndUpdate(invoke,
codegen,
get_and_update_op_,
need_any_store_barrier_,
need_any_any_barrier_,
/*byte_swap=*/ true);
break;
}
__ jmp(GetExitLabel());
}
#define MARK_UNIMPLEMENTED(Name) UNIMPLEMENTED_INTRINSIC(X86_64, Name)
UNIMPLEMENTED_INTRINSIC_LIST_X86_64(MARK_UNIMPLEMENTED);
#undef MARK_UNIMPLEMENTED
UNREACHABLE_INTRINSICS(X86_64)
#undef __
} // namespace x86_64
} // namespace art
|