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
|
//===--- MemAccessUtils.cpp - Utilities for SIL memory access. ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-access-utils"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/Basic/GraphNodeWorklist.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/DynamicCasts.h"
#include "swift/SIL/NodeDatastructures.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/SILBridging.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/Test.h"
#include "llvm/Support/Debug.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// MARK: FindAccessVisitor
//===----------------------------------------------------------------------===//
namespace {
enum StorageCastTy { StopAtStorageCast, IgnoreStorageCast };
// Handle a single phi-web within an access use-def chain.
//
// Recursively calls the useDefVisitor on any operations that aren't recognized
// as storage casts or projections. If the useDefVisitor finds a consistent
// result for all operands, then it's result will remain valid. If the
// useDefVisitor has an invalid result after processing the phi web, then it's
// original result is restored, then the phi reported to the useDefVisitor as a
// NonAccess.
//
// Phi-web's are only allowed to contain casts and projections that do not
// affect the access path. If AccessPhiVisitor reaches an unhandled projection,
// it remembers that as the commonDefinition. If after processing the entire
// web, the commonDefinition is unique, then it calls the original useDefVisitor
// to update its result. Note that visitAccessProjection and setDefinition are
// only used by visitors that process access projections; once the accessed
// address is reached, they are no longer relevant.
template <typename UseDefVisitor>
class AccessPhiVisitor
: public AccessUseDefChainVisitor<AccessPhiVisitor<UseDefVisitor>> {
UseDefVisitor &useDefVisitor;
StorageCastTy storageCastTy;
std::optional<SILValue> commonDefinition;
SmallVector<SILValue, 8> pointerWorklist;
SmallPtrSet<SILPhiArgument *, 4> nestedPhis;
public:
AccessPhiVisitor(UseDefVisitor &useDefVisitor, StorageCastTy storageCastTy)
: useDefVisitor(useDefVisitor), storageCastTy(storageCastTy) {}
// Main entry point.
void findPhiAccess(SILPhiArgument *phiArg) && {
auto savedResult = useDefVisitor.saveResult();
visitPhi(phiArg);
while (!pointerWorklist.empty()) {
this->visit(pointerWorklist.pop_back_val());
}
// If a common path component was found, recursively look for the result.
if (commonDefinition) {
if (commonDefinition.value()) {
useDefVisitor.reenterUseDef(commonDefinition.value());
} else {
// Divergent paths were found; invalidate any previously discovered
// storage.
useDefVisitor.invalidateResult();
}
}
// If the result is now invalid, reset it and process the current phi as an
// unrecognized access instead.
if (!useDefVisitor.isResultValid()) {
useDefVisitor.restoreResult(savedResult);
visitNonAccess(phiArg);
}
}
// Visitor helper.
void setDefinition(SILValue def) {
if (!commonDefinition) {
commonDefinition = def;
return;
}
if (commonDefinition.value() != def)
commonDefinition = SILValue();
}
void checkVisitorResult(SILValue result) {
assert(!result && "must override any visitor that returns a result");
}
// MARK: Visitor implementation.
// Recursively call the original storageVisitor for each base. We can't simply
// look for a common definition on all phi inputs, because the base may be
// cloned on each path. For example, two global_addr instructions may refer to
// the same global storage. Those global_addr instructions may each be
// converted to a RawPointer before being passed into the non-address phi.
void visitBase(SILValue base, AccessStorage::Kind kind) {
checkVisitorResult(useDefVisitor.visitBase(base, kind));
}
void visitNonAccess(SILValue value) {
checkVisitorResult(useDefVisitor.visitNonAccess(value));
}
void visitNestedAccess(BeginAccessInst *access) {
checkVisitorResult(useDefVisitor.visitNestedAccess(access));
}
void visitPhi(SILPhiArgument *phiArg) {
if (nestedPhis.insert(phiArg).second)
phiArg->getIncomingPhiValues(pointerWorklist);
}
void visitStorageCast(SingleValueInstruction *cast, Operand *sourceOper,
AccessStorageCast) {
// Allow conversions to/from pointers and addresses on disjoint phi paths
// only if the underlying useDefVisitor allows it.
if (storageCastTy == IgnoreStorageCast)
pointerWorklist.push_back(sourceOper->get());
else
visitNonAccess(cast);
}
void visitAccessProjection(SingleValueInstruction *projectedAddr,
Operand *sourceOper) {
// An offset index on a phi path is always conservatively considered an
// unknown offset.
if (isa<IndexAddrInst>(projectedAddr) || isa<TailAddrInst>(projectedAddr)) {
useDefVisitor.addUnknownOffset();
pointerWorklist.push_back(sourceOper->get());
return;
}
// No other access projections are expected to occur on disjoint phi
// paths. Stop searching at this projection.
setDefinition(projectedAddr);
}
};
// Find the origin of an access while skipping projections and casts and
// handling phis.
template <typename Impl>
class FindAccessVisitorImpl : public AccessUseDefChainVisitor<Impl, SILValue> {
using SuperTy = AccessUseDefChainVisitor<Impl, SILValue>;
protected:
NestedAccessType nestedAccessTy;
StorageCastTy storageCastTy;
SmallPtrSet<SILPhiArgument *, 4> visitedPhis;
bool hasUnknownOffset = false;
public:
FindAccessVisitorImpl(NestedAccessType nestedAccessTy,
StorageCastTy storageCastTy)
: nestedAccessTy(nestedAccessTy), storageCastTy(storageCastTy) {}
// MARK: AccessPhiVisitor::UseDefVisitor implementation.
//
// Subclasses must implement:
// isResultValid()
// invalidateResult()
// saveResult()
// restoreResult(Result)
// addUnknownOffset()
void reenterUseDef(SILValue sourceAddr) {
SILValue nextAddr = this->visit(sourceAddr);
while (nextAddr) {
checkNextAddressType(nextAddr, sourceAddr);
nextAddr = this->visit(nextAddr);
}
}
// MARK: visitor implementation.
// Override AccessUseDefChainVisitor to ignore access markers and find the
// outer access base.
SILValue visitNestedAccessImpl(BeginAccessInst *access) {
if (nestedAccessTy == NestedAccessType::IgnoreAccessBegin)
return access->getSource();
return SuperTy::visitNestedAccess(access);
}
SILValue visitNestedAccess(BeginAccessInst *access) {
auto value = visitNestedAccessImpl(access);
if (value) {
reenterUseDef(value);
}
return SILValue();
}
SILValue visitPhi(SILPhiArgument *phiArg) {
// Cycles involving phis are only handled within AccessPhiVisitor.
// Path components are not allowed in phi cycles.
if (visitedPhis.insert(phiArg).second) {
AccessPhiVisitor<Impl>(this->asImpl(), storageCastTy)
.findPhiAccess(phiArg);
// Each phi operand was now reentrantly processed. Stop visiting.
return SILValue();
}
// Cannot treat unresolved phis as "unidentified" because they may alias
// with global or class access.
return this->asImpl().visitNonAccess(phiArg);
}
SILValue visitStorageCast(SingleValueInstruction *, Operand *sourceAddr,
AccessStorageCast cast) {
assert(storageCastTy == IgnoreStorageCast);
return sourceAddr->get();
}
SILValue visitAccessProjection(SingleValueInstruction *projectedAddr,
Operand *sourceAddr) {
if (auto *indexAddr = dyn_cast<IndexAddrInst>(projectedAddr)) {
if (!Projection(indexAddr).isValid())
this->asImpl().addUnknownOffset();
} else if (isa<TailAddrInst>(projectedAddr)) {
this->asImpl().addUnknownOffset();
}
return sourceAddr->get();
}
protected:
// Helper for reenterUseDef
void checkNextAddressType(SILValue nextAddr, SILValue sourceAddr) {
#ifdef NDEBUG
return;
#endif
SILType type = nextAddr->getType();
// FIXME: This relatively expensive pointer getAnyPointerElementType check
// is only needed because keypath generation incorrectly produces
// pointer_to_address directly from stdlib Pointer types without a
// struct_extract (as is correctly done in emitAddressorAccessor), and
// the PointerToAddressInst operand type is never verified.
if (type.getASTType()->getAnyPointerElementType())
return;
if (type.isAddress() || isa<SILBoxType>(type.getASTType())
|| isa<BuiltinRawPointerType>(type.getASTType())) {
return;
}
llvm::errs() << "Visiting ";
sourceAddr->print(llvm::errs());
llvm::errs() << " not an address ";
nextAddr->print(llvm::errs());
nextAddr->getFunction()->print(llvm::errs());
assert(false);
}
};
// Implement getAccessAddress, getAccessBegin, and getAccessBase.
class FindAccessBaseVisitor
: public FindAccessVisitorImpl<FindAccessBaseVisitor> {
using SuperTy = FindAccessVisitorImpl<FindAccessBaseVisitor>;
protected:
// If the optional baseVal is set, then a result was found. If the SILValue
// within the optional is invalid, then there are multiple inconsistent base
// addresses (this may currently happen with RawPointer phis).
std::optional<SILValue> baseVal;
// If the kind optional is set, then 'baseVal' is a valid
// AccessBase. 'baseVal' may be a valid SILValue while kind optional has no
// value if an invalid address producer was detected, via a call to
// visitNonAccess.
std::optional<AccessBase::Kind> kindVal;
public:
FindAccessBaseVisitor(NestedAccessType nestedAccessTy,
StorageCastTy storageCastTy)
: FindAccessVisitorImpl(nestedAccessTy, storageCastTy) {}
// Returns the accessed address or an invalid SILValue.
SILValue findPossibleBaseAddress(SILValue sourceAddr) && {
reenterUseDef(sourceAddr);
return baseVal.value_or(SILValue());
}
AccessBase findBase(SILValue sourceAddr) && {
reenterUseDef(sourceAddr);
if (!baseVal || !kindVal)
return AccessBase();
return AccessBase(baseVal.value(), kindVal.value());
}
void setResult(SILValue foundBase) {
if (!baseVal)
baseVal = foundBase;
else if (baseVal.value() != foundBase)
baseVal = SILValue();
}
// MARK: AccessPhiVisitor::UseDefVisitor implementation.
// Keep going as long as baseVal is valid regardless of kindVal.
bool isResultValid() const { return baseVal && bool(baseVal.value()); }
void invalidateResult() {
baseVal = SILValue();
kindVal = std::nullopt;
}
std::optional<SILValue> saveResult() const { return baseVal; }
void restoreResult(std::optional<SILValue> result) { baseVal = result; }
void addUnknownOffset() { return; }
// MARK: visitor implementation.
SILValue visitBase(SILValue base, AccessStorage::Kind kind) {
setResult(base);
if (!baseVal.value()) {
kindVal = std::nullopt;
} else {
assert(!kindVal || kindVal.value() == kind);
kindVal = kind;
}
return SILValue();
}
SILValue visitNonAccess(SILValue value) {
setResult(value);
kindVal = std::nullopt;
return SILValue();
}
// Override visitStorageCast to avoid seeing through arbitrary address casts.
SILValue visitStorageCast(SingleValueInstruction *svi, Operand *sourceAddr,
AccessStorageCast cast) {
if (storageCastTy == StopAtStorageCast)
return visitNonAccess(svi);
return SuperTy::visitStorageCast(svi, sourceAddr, cast);
}
};
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// MARK: Standalone API
//===----------------------------------------------------------------------===//
SILValue swift::getTypedAccessAddress(SILValue address) {
assert(address->getType().isAddress());
SILValue accessAddress =
FindAccessBaseVisitor(NestedAccessType::StopAtAccessBegin,
StopAtStorageCast)
.findPossibleBaseAddress(address);
assert(accessAddress->getType().isAddress());
return accessAddress;
}
namespace swift::test {
static FunctionTest
GetTypedAccessAddress("get_typed_access_address",
[](auto &function, auto &arguments, auto &test) {
auto address = arguments.takeValue();
function.print(llvm::outs());
llvm::outs() << "Address: " << address;
auto access = getTypedAccessAddress(address);
llvm::outs() << "Access: " << access;
});
} // end namespace swift::test
// TODO: When the optimizer stops stripping begin_access markers and SILGen
// protects all memory operations with at least an "unsafe" access scope, then
// we should be able to assert that this returns a BeginAccessInst.
SILValue swift::getAccessScope(SILValue address) {
assert(address->getType().isAddress());
return FindAccessBaseVisitor(NestedAccessType::StopAtAccessBegin,
IgnoreStorageCast)
.findPossibleBaseAddress(address);
}
// This is allowed to be called on a non-address pointer type.
SILValue swift::getAccessBase(SILValue address) {
return FindAccessBaseVisitor(NestedAccessType::IgnoreAccessBegin,
IgnoreStorageCast)
.findPossibleBaseAddress(address);
}
namespace swift::test {
static FunctionTest GetAccessBaseTest("get_access_base",
[](auto &function, auto &arguments,
auto &test) {
auto address = arguments.takeValue();
function.print(llvm::outs());
llvm::outs() << "Address: " << address;
auto base = getAccessBase(address);
llvm::outs() << "Base: " << base;
});
} // end namespace swift::test
static bool isLetForBase(SILValue base) {
// Is this an address of a "let" class member?
if (auto *rea = dyn_cast<RefElementAddrInst>(base))
return rea->getField()->isLet();
// Is this an address of a global "let"?
if (auto *gai = dyn_cast<GlobalAddrInst>(base)) {
auto *globalDecl = gai->getReferencedGlobal()->getDecl();
return globalDecl && globalDecl->isLet();
}
return false;
}
bool swift::isLetAddress(SILValue address) {
SILValue base = getAccessBase(address);
if (!base)
return false;
return isLetForBase(base);
}
//===----------------------------------------------------------------------===//
// MARK: Deinitialization barriers.
//===----------------------------------------------------------------------===//
bool swift::mayAccessPointer(SILInstruction *instruction) {
assert(!FullApplySite::isa(instruction) && !isa<EndApplyInst>(instruction) &&
!isa<AbortApplyInst>(instruction));
if (!instruction->mayReadOrWriteMemory())
return false;
if (isa<BuiltinInst>(instruction)) {
// Consider all builtins that read/write memory to access pointers.
return true;
}
bool retval = false;
visitAccessedAddress(instruction, [&retval](Operand *operand) {
auto accessStorage = AccessStorage::compute(operand->get());
auto kind = accessStorage.getKind();
if (kind == AccessRepresentation::Kind::Unidentified ||
kind == AccessRepresentation::Kind::Global)
retval = true;
});
return retval;
}
bool swift::mayLoadWeakOrUnowned(SILInstruction *instruction) {
assert(!FullApplySite::isa(instruction) && !isa<EndApplyInst>(instruction) &&
!isa<AbortApplyInst>(instruction));
if (isa<BuiltinInst>(instruction)) {
return instruction->mayReadOrWriteMemory();
}
return isa<LoadWeakInst>(instruction)
|| isa<LoadUnownedInst>(instruction)
|| isa<StrongCopyUnownedValueInst>(instruction)
|| isa<StrongCopyUnmanagedValueInst>(instruction);
}
/// Conservatively, whether this instruction could involve a synchronization
/// point like a memory barrier, lock or syscall.
bool swift::maySynchronize(SILInstruction *instruction) {
assert(!FullApplySite::isa(instruction) && !isa<EndApplyInst>(instruction) &&
!isa<AbortApplyInst>(instruction));
if (isa<BuiltinInst>(instruction)) {
return instruction->mayReadOrWriteMemory();
}
return isa<HopToExecutorInst>(instruction);
}
bool swift::mayBeDeinitBarrierNotConsideringSideEffects(SILInstruction *instruction) {
if (FullApplySite::isa(instruction) || isa<EndApplyInst>(instruction) ||
isa<AbortApplyInst>(instruction)) {
return true;
}
bool retval = mayAccessPointer(instruction)
|| mayLoadWeakOrUnowned(instruction)
|| maySynchronize(instruction);
assert(!retval || !isa<BranchInst>(instruction) && "br as deinit barrier!?");
return retval;
}
//===----------------------------------------------------------------------===//
// MARK: AccessRepresentation
//===----------------------------------------------------------------------===//
constexpr unsigned AccessRepresentation::TailIndex;
const char *AccessRepresentation::getKindName(AccessStorage::Kind k) {
switch (k) {
case Box:
return "Box";
case Stack:
return "Stack";
case Nested:
return "Nested";
case Unidentified:
return "Unidentified";
case Argument:
return "Argument";
case Yield:
return "Yield";
case Global:
return "Global";
case Class:
return "Class";
case Tail:
return "Tail";
}
llvm_unreachable("unhandled kind");
}
// 'value' remains invalid. AccessBase or AccessStorage must initialize it
// accordingly.
AccessRepresentation::AccessRepresentation(SILValue base, Kind kind) : value() {
Bits.opaqueBits = 0;
// For kind==Unidentified, base may be an invalid, empty, or tombstone value.
initKind(kind, InvalidElementIndex);
switch (kind) {
case Box:
assert(isa<ProjectBoxInst>(base));
break;
case Stack:
assert(isa<AllocStackInst>(base));
break;
case Nested:
assert(isa<BeginAccessInst>(base));
break;
case Yield:
assert(isa<BeginApplyInst>(
cast<MultipleValueInstructionResult>(base)->getParent()));
break;
case Unidentified:
break;
case Global:
break;
case Tail:
assert(isa<RefTailAddrInst>(base));
setElementIndex(TailIndex);
break;
case Argument:
setElementIndex(cast<SILFunctionArgument>(base)->getIndex());
break;
case Class: {
setElementIndex(cast<RefElementAddrInst>(base)->getFieldIndex());
break;
}
}
}
bool AccessRepresentation::
isDistinctFrom(const AccessRepresentation &other) const {
if (isUniquelyIdentified()) {
if (other.isUniquelyIdentified() && !hasIdenticalAccessInfo(other))
return true;
if (other.isObjectAccess())
return true;
// We currently assume that Unidentified storage may overlap with
// Box/Stack storage.
return false;
}
if (other.isUniquelyIdentified())
return other.isDistinctFrom(*this);
// Neither storage is uniquely identified.
if (isObjectAccess()) {
if (other.isObjectAccess()) {
// Property access cannot overlap with Tail access.
if (getKind() != other.getKind())
return true;
// We could also check if the object types are distinct, but that only
// helps if we know the relationships between class types.
return getKind() == Class
&& getPropertyIndex() != other.getPropertyIndex();
}
// Any type of nested/argument address may be within the same object.
//
// We also currently assume Unidentified access may be within an object
// purely to handle KeyPath accesses. The derivation of the KeyPath
// address must separately appear to be a Class access so that all Class
// accesses are accounted for.
return false;
}
if (other.isObjectAccess())
return other.isDistinctFrom(*this);
// Neither storage is from a class or tail.
//
// Unidentified values may alias with each other or with any kind of
// nested/argument access.
return false;
}
// The subclass prints Class and Global values.
void AccessRepresentation::print(raw_ostream &os) const {
if (!*this) {
os << "INVALID\n";
return;
}
os << getKindName(getKind()) << " ";
switch (getKind()) {
case Box:
case Stack:
case Nested:
case Yield:
case Unidentified:
case Tail:
os << value;
break;
case Argument:
os << value;
break;
case Global:
case Class:
break;
}
}
//===----------------------------------------------------------------------===//
// MARK: AccessBase
//===----------------------------------------------------------------------===//
AccessBase AccessBase::compute(SILValue sourceAddress) {
return FindAccessBaseVisitor(NestedAccessType::IgnoreAccessBegin,
IgnoreStorageCast)
.findBase(sourceAddress);
}
AccessBase::AccessBase(SILValue base, Kind kind)
: AccessRepresentation(base, kind)
{
assert(base && "invalid storage base");
value = base;
setLetAccess(isLetForBase(base));
}
static SILValue
getReferenceFromBase(SILValue base, AccessRepresentation::Kind kind) {
switch (kind) {
case AccessBase::Stack:
case AccessBase::Nested:
case AccessBase::Yield:
case AccessBase::Unidentified:
case AccessBase::Argument:
case AccessBase::Global:
llvm_unreachable("Not object storage");
break;
case AccessBase::Box:
return cast<ProjectBoxInst>(base)->getOperand();
case AccessBase::Tail:
return cast<RefTailAddrInst>(base)->getOperand();
case AccessBase::Class:
return cast<RefElementAddrInst>(base)->getOperand();
}
}
SILValue AccessBase::getReference() const {
return getReferenceFromBase(value, getKind());
}
static SILGlobalVariable *getReferencedGlobal(SILInstruction *inst) {
if (auto *gai = dyn_cast<GlobalAddrInst>(inst)) {
return gai->getReferencedGlobal();
}
if (auto apply = FullApplySite::isa(inst)) {
if (auto *funcRef = apply.getReferencedFunctionOrNull()) {
return getVariableOfGlobalInit(funcRef);
}
}
return nullptr;
}
SILGlobalVariable *AccessBase::getGlobal() const {
assert(getKind() == Global);
return getReferencedGlobal(cast<SingleValueInstruction>(value));
}
static const ValueDecl *
getNonRefNonGlobalDecl(SILValue base, AccessRepresentation::Kind kind) {
switch (kind) {
case AccessBase::Box:
case AccessBase::Class:
case AccessBase::Tail:
llvm_unreachable("Cannot handle reference access");
case AccessBase::Global:
llvm_unreachable("Cannot handle global access");
case AccessBase::Stack:
return cast<AllocStackInst>(base)->getDecl();
case AccessBase::Argument:
return cast<SILFunctionArgument>(base)->getDecl();
case AccessBase::Yield:
return nullptr;
case AccessBase::Nested:
return nullptr;
case AccessBase::Unidentified:
return nullptr;
}
llvm_unreachable("unhandled kind");
}
const ValueDecl *AccessBase::getDecl() const {
switch (getKind()) {
case Box:
if (auto *allocBox = dyn_cast<AllocBoxInst>(
findReferenceRoot(getReference()))) {
return allocBox->getLoc().getAsASTNode<VarDecl>();
}
return nullptr;
case Class: {
auto *classDecl = cast<RefElementAddrInst>(value)->getClassDecl();
return getIndexedField(classDecl, getPropertyIndex());
}
case Tail:
return nullptr;
case Global:
return getGlobal()->getDecl();
default:
return getNonRefNonGlobalDecl(value, getKind());
}
}
bool AccessBase::hasLocalOwnershipLifetime() const {
switch (getKind()) {
case AccessBase::Argument:
case AccessBase::Stack:
case AccessBase::Global:
return false;
case AccessBase::Unidentified:
// Unidentified storage may be nested within object access, but this is an
// "escaped pointer", so it is not restricted to the object's borrow scope.
return false;
case AccessBase::Yield:
// Yielded values have a local apply scope, but they never have the same
// storage as yielded values from a different scope, so there is no need to
// consider their local scope during substitution.
return false;
case AccessBase::Box:
case AccessBase::Class:
case AccessBase::Tail:
return getReference()->getOwnershipKind() != OwnershipKind::None;
case AccessBase::Nested:
llvm_unreachable("unexpected storage");
};
}
void AccessBase::print(raw_ostream &os) const {
AccessRepresentation::print(os);
switch (getKind()) {
case Global:
os << *getGlobal();
break;
case Class:
os << getReference();
if (auto *decl = getDecl()) {
os << " Field: ";
decl->print(os);
}
os << " Index: " << getPropertyIndex() << "\n";
break;
default:
break;
}
}
LLVM_ATTRIBUTE_USED void AccessBase::dump() const { print(llvm::dbgs()); }
//===----------------------------------------------------------------------===//
// MARK: FindReferenceRoot
//===----------------------------------------------------------------------===//
bool swift::isIdentityPreservingRefCast(SingleValueInstruction *svi) {
// Ignore both copies and other identity and ownership preserving casts
return isa<CopyValueInst>(svi) || isa<BeginBorrowInst>(svi) ||
isa<EndInitLetRefInst>(svi) || isa<BeginDeallocRefInst>(svi) ||
isa<EndCOWMutationInst>(svi) ||
isIdentityAndOwnershipPreservingRefCast(svi);
}
// On some platforms, casting from a metatype to a reference type dynamically
// allocates a ref-counted box for the metatype. Naturally that is the place
// where RC-identity begins. Considering the source of such a casts to be
// RC-identical would confuse ARC optimization, which might eliminate a retain
// of such an object completely.
//
// The SILVerifier checks that none of these operations cast a trivial value to
// a reference except unconditional_checked_cast[_value], which is checked By
// SILDynamicCastInst::isRCIdentityPreserving().
bool swift::isIdentityAndOwnershipPreservingRefCast(
SingleValueInstruction *svi) {
switch (svi->getKind()) {
default:
return false;
// Ignore class type casts
case SILInstructionKind::UpcastInst:
case SILInstructionKind::UncheckedRefCastInst:
case SILInstructionKind::RefToBridgeObjectInst:
case SILInstructionKind::BridgeObjectToRefInst:
return true;
case SILInstructionKind::UnconditionalCheckedCastInst:
return SILDynamicCastInst(svi).isRCIdentityPreserving();
// Ignore markers
case SILInstructionKind::MarkUninitializedInst:
case SILInstructionKind::MarkDependenceInst:
case SILInstructionKind::MarkUnresolvedReferenceBindingInst:
return true;
}
}
namespace {
// Essentially RC identity where the starting point is already a reference.
class FindReferenceRoot {
SmallPtrSet<SILPhiArgument *, 4> visitedPhis;
public:
SILValue findRoot(SILValue ref) && {
SILValue root = recursiveFindRoot(ref);
assert(root && "all phi inputs must be reachable");
return root;
}
protected:
// Return an invalid value for a phi with no resolved inputs.
SILValue recursiveFindRoot(SILValue ref) {
while (auto *svi = dyn_cast<SingleValueInstruction>(ref)) {
// If preserveOwnership is true, stop at the first owned root
if (!isIdentityPreservingRefCast(svi)) {
break;
}
ref = svi->getOperand(0);
};
auto *phi = dyn_cast<SILPhiArgument>(ref);
if (!phi || !phi->isPhi()) {
return ref;
}
// Handle phis...
if (!visitedPhis.insert(phi).second) {
return SILValue();
}
SILValue commonInput;
phi->visitIncomingPhiOperands([&](Operand *operand) {
SILValue input = recursiveFindRoot(operand->get());
// Ignore "back/cross edges" to previously visited phis.
if (!input)
return true;
if (!commonInput) {
commonInput = input;
return true;
}
if (commonInput == input)
return true;
commonInput = phi;
return false;
});
return commonInput;
}
};
} // end anonymous namespace
SILValue swift::findReferenceRoot(SILValue ref) {
return FindReferenceRoot().findRoot(ref);
}
// This does not handle phis because a phis is either a consume or a
// reborrow. In either case, the phi argument's ownership is independent from
// the phi itself. The client assumes that the returned root is in the same
// lifetime or borrow scope of the access.
SILValue swift::findOwnershipReferenceRoot(SILValue ref) {
while (auto *svi = dyn_cast<SingleValueInstruction>(ref)) {
if (isIdentityAndOwnershipPreservingRefCast(svi)) {
ref = svi->getOperand(0);
continue;
}
break;
}
return ref;
}
void swift::findGuaranteedReferenceRoots(SILValue referenceValue,
bool lookThroughNestedBorrows,
SmallVectorImpl<SILValue> &roots) {
ValueWorklist worklist(referenceValue->getFunction());
worklist.pushIfNotVisited(referenceValue);
while (auto value = worklist.pop()) {
// Instructions may forwarded None ownership to guaranteed.
if (value->getOwnershipKind() != OwnershipKind::Guaranteed)
continue;
if (SILArgument::asPhi(value)) {
roots.push_back(value);
continue;
}
if (visitForwardedGuaranteedOperands(value, [&](Operand *operand) {
worklist.pushIfNotVisited(operand->get());
})) {
// This instruction is not a root if any operands were forwarded,
// regardless of whether they were already visited.
continue;
}
// Found a potential root.
if (lookThroughNestedBorrows) {
if (auto *bbi = dyn_cast<BeginBorrowInst>(value)) {
auto borrowee = bbi->getOperand();
if (borrowee->getOwnershipKind() == OwnershipKind::Guaranteed) {
// A nested borrow, the root guaranteed earlier in the use-def chain.
worklist.pushIfNotVisited(borrowee);
continue;
}
// The borrowee isn't guaranteed or we aren't looking through nested
// borrows. Fall through to add the begin_borrow to roots.
}
}
roots.push_back(value);
}
}
/// Find the first owned aggregate containing the reference, or simply the
/// reference root if no aggregate is found.
///
/// TODO: Add a component path to a ReferenceRoot abstraction and handle
/// that within FindReferenceRoot.
SILValue swift::findOwnershipReferenceAggregate(SILValue ref) {
SILValue root = ref;
while(true) {
root = findOwnershipReferenceRoot(root);
if (!root)
return root;
if (auto *inst = root->getDefiningInstruction()) {
if (auto fwdOp = ForwardingOperation(inst)) {
if (auto *singleForwardingOp = fwdOp.getSingleForwardingOperand()) {
root = singleForwardingOp->get();
continue;
}
}
}
if (auto *termResult = SILArgument::isTerminatorResult(root)) {
if (auto *oper = termResult->forwardedTerminatorResultOperand()) {
root = oper->get();
continue;
}
}
break;
}
return root;
}
//===----------------------------------------------------------------------===//
// MARK: AccessStorage
//===----------------------------------------------------------------------===//
AccessStorage::AccessStorage(SILValue base, Kind kind)
: AccessRepresentation(base, kind)
{
if (isReference()) {
value = findReferenceRoot(getReferenceFromBase(base, kind));
// Class access is a "let" if it's base points to a stored property.
if (getKind() == AccessBase::Class) {
setLetAccess(isLetForBase(base));
}
// Box access is a "let" if it's root is from a "let" VarDecl.
if (getKind() == AccessBase::Box) {
if (auto *decl = dyn_cast_or_null<VarDecl>(getDecl())) {
setLetAccess(decl->isLet());
}
}
return;
}
if (getKind() == AccessBase::Global) {
global = getReferencedGlobal(cast<SingleValueInstruction>(base));
// It's unclear whether a global will ever be missing it's varDecl, but
// technically we only preserve it for debug info. So if we don't have a
// decl, check the flag on SILGlobalVariable, which is guaranteed valid.
setLetAccess(global->isLet());
return;
}
value = base;
if (auto *decl = dyn_cast_or_null<VarDecl>(getDecl())) {
setLetAccess(decl->isLet());
}
}
void AccessStorage::visitRoots(
SILFunction *function,
llvm::function_ref<bool(SILValue)> visitor) const {
if (SILValue root = getRoot()) {
visitor(root);
return;
}
assert(getKind() == Global && function);
SILGlobalVariable *global = getGlobal();
for (auto &block : *function) {
for (auto &instruction : block) {
if (global == getReferencedGlobal(&instruction)) {
visitor(cast<SingleValueInstruction>(&instruction));
}
}
}
}
const ValueDecl *AccessStorage::getDecl() const {
switch (getKind()) {
case Box:
if (auto *allocBox = dyn_cast<AllocBoxInst>(getRoot())) {
return allocBox->getLoc().getAsASTNode<VarDecl>();
}
return nullptr;
case Class: {
// The property index is relative to the VarDecl in ref_element_addr, and
// can only be reliably determined when the base is available. Without the
// base, we can only make a best effort to extract it from the object type,
// which might not even be a class in the case of bridge objects.
if (ClassDecl *classDecl =
getObject()->getType().getClassOrBoundGenericClass()) {
return getIndexedField(classDecl, getPropertyIndex());
}
return nullptr;
}
case Tail:
return nullptr;
case Global:
return global->getDecl();
default:
return getNonRefNonGlobalDecl(value, getKind());
}
}
const ValueDecl *AccessStorageWithBase::getDecl() const {
if (storage.getKind() == AccessBase::Class)
return getAccessBase().getDecl();
return storage.getDecl();
}
void AccessStorage::print(raw_ostream &os) const {
AccessRepresentation::print(os);
switch (getKind()) {
case Global:
os << *global;
break;
case Class:
os << getObject();
if (auto *decl = getDecl()) {
os << " Field: ";
decl->print(os);
}
os << " Index: " << getPropertyIndex() << "\n";
break;
default:
break;
}
}
LLVM_ATTRIBUTE_USED void AccessStorage::dump() const { print(llvm::dbgs()); }
void AccessStorageWithBase::print(raw_ostream &os) const {
if (base)
os << "Base: " << base;
else
os << "Base: unidentified\n";
storage.print(os);
}
LLVM_ATTRIBUTE_USED void AccessStorageWithBase::dump() const {
print(llvm::dbgs());
}
namespace {
// Implementation of AccessUseDefChainVisitor that looks for a single common
// AccessStorage object for all projection paths.
class FindAccessStorageVisitor
: public FindAccessVisitorImpl<FindAccessStorageVisitor> {
using SuperTy = FindAccessVisitorImpl<FindAccessStorageVisitor>;
public:
struct Result {
std::optional<AccessStorage> storage;
SILValue base;
std::optional<AccessStorageCast> seenCast;
};
private:
Result result;
void setResult(AccessStorage foundStorage, SILValue foundBase) {
if (!result.storage) {
result.storage = foundStorage;
assert(!result.base);
result.base = foundBase;
} else {
// `storage` may still be invalid. If both `storage` and `foundStorage`
// are invalid, this check passes, but we return an invalid storage
// below.
if (!result.storage->hasIdenticalStorage(foundStorage))
result.storage = AccessStorage();
if (result.base != foundBase)
result.base = SILValue();
}
}
public:
FindAccessStorageVisitor(NestedAccessType nestedAccessTy)
: FindAccessVisitorImpl(nestedAccessTy, IgnoreStorageCast) {}
// Main entry point
void findStorage(SILValue sourceAddr) { this->reenterUseDef(sourceAddr); }
AccessStorage getStorage() const {
return result.storage.value_or(AccessStorage());
}
// getBase may return an invalid value for valid Global storage because there
// may be multiple global_addr bases for identical storage.
SILValue getBase() const { return result.base; }
std::optional<AccessStorageCast> getCast() const { return result.seenCast; }
// MARK: AccessPhiVisitor::UseDefVisitor implementation.
// A valid result requires valid storage, but not a valid base.
bool isResultValid() const {
return result.storage && bool(result.storage.value());
}
void invalidateResult() { setResult(AccessStorage(), SILValue()); }
Result saveResult() const { return result; }
void restoreResult(Result savedResult) { result = savedResult; }
void addUnknownOffset() { return; }
// MARK: visitor implementation.
SILValue visitBase(SILValue base, AccessStorage::Kind kind) {
setResult(AccessStorage(base, kind), base);
return SILValue();
}
SILValue visitNonAccess(SILValue value) {
invalidateResult();
return SILValue();
}
SILValue visitStorageCast(SingleValueInstruction *svi, Operand *sourceOper,
AccessStorageCast cast) {
result.seenCast = result.seenCast ? std::max(*result.seenCast, cast) : cast;
return SuperTy::visitStorageCast(svi, sourceOper, cast);
}
};
} // end anonymous namespace
RelativeAccessStorageWithBase
RelativeAccessStorageWithBase::compute(SILValue address) {
FindAccessStorageVisitor visitor(NestedAccessType::IgnoreAccessBegin);
visitor.findStorage(address);
return {
address, {visitor.getStorage(), visitor.getBase()}, visitor.getCast()};
}
RelativeAccessStorageWithBase
RelativeAccessStorageWithBase::computeInScope(SILValue address) {
FindAccessStorageVisitor visitor(NestedAccessType::StopAtAccessBegin);
visitor.findStorage(address);
return {
address, {visitor.getStorage(), visitor.getBase()}, visitor.getCast()};
}
AccessStorageWithBase
AccessStorageWithBase::compute(SILValue sourceAddress) {
return RelativeAccessStorageWithBase::compute(sourceAddress).storageWithBase;
}
AccessStorageWithBase
AccessStorageWithBase::computeInScope(SILValue sourceAddress) {
return RelativeAccessStorageWithBase::computeInScope(sourceAddress)
.storageWithBase;
}
AccessStorage AccessStorage::compute(SILValue sourceAddress) {
return AccessStorageWithBase::compute(sourceAddress).storage;
}
namespace swift::test {
static FunctionTest ComputeAccessStorage("compute_access_storage",
[](auto &function, auto &arguments,
auto &test) {
auto address = arguments.takeValue();
function.print(llvm::outs());
llvm::outs() << "Address: " << address;
auto accessStorage = AccessStorage::compute(address);
accessStorage.print(llvm::outs());
});
} // end namespace swift::test
AccessStorage AccessStorage::computeInScope(SILValue sourceAddress) {
return AccessStorageWithBase::computeInScope(sourceAddress).storage;
}
AccessStorage AccessStorage::forObjectTail(SILValue object) {
AccessStorage storage;
storage.initKind(Tail, TailIndex);
storage.value = findReferenceRoot(object);
return storage;
}
//===----------------------------------------------------------------------===//
// MARK: AccessPath
//===----------------------------------------------------------------------===//
AccessPath AccessPath::forTailStorage(SILValue rootReference) {
return AccessPath(AccessStorage::forObjectTail(rootReference),
PathNode(rootReference->getModule()->getIndexTrieRoot()),
/*offset*/ 0);
}
bool AccessPath::contains(AccessPath subPath) const {
if (!isValid() || !subPath.isValid()) {
return false;
}
if (!storage.hasIdenticalStorage(subPath.storage)) {
return false;
}
// Does the offset index match?
if (offset != subPath.offset || offset == UnknownOffset) {
return false;
}
return pathNode.node->isPrefixOf(subPath.pathNode.node);
}
bool AccessPath::mayOverlap(AccessPath otherPath) const {
if (!isValid() || !otherPath.isValid())
return true;
if (storage.isDistinctFrom(otherPath.storage)) {
return false;
}
// If subpaths are disjoint, they do not overlap regardless of offset.
if (!pathNode.node->isPrefixOf(otherPath.pathNode.node)
&& !otherPath.pathNode.node->isPrefixOf(pathNode.node)) {
return true;
}
return offset == otherPath.offset || offset == UnknownOffset
|| otherPath.offset == UnknownOffset;
}
namespace {
// Implementation of AccessUseDefChainVisitor that builds an AccessPath.
class AccessPathVisitor : public FindAccessVisitorImpl<AccessPathVisitor> {
using SuperTy = FindAccessVisitorImpl<AccessPathVisitor>;
SILModule *module;
// This nested visitor holds the AccessStorage and base results.
FindAccessStorageVisitor storageVisitor;
// Save just enough information for to checkpoint before processing phis. Phis
// can add path components and add an unknown offset.
struct Result {
FindAccessStorageVisitor::Result storageResult;
int savedOffset;
unsigned pathLength;
Result(FindAccessStorageVisitor::Result storageResult, int offset,
unsigned pathLength)
: storageResult(storageResult), savedOffset(offset),
pathLength(pathLength) {}
};
// Only access projections affect this path. Since they are not allowed
// beyond phis, this path is not part of AccessPathVisitor::Result.
llvm::SmallVector<AccessPath::Index, 8> reversePath;
// Holds a non-zero value if an index_addr has been processed without yet
// creating a path index for it.
int pendingOffset = 0;
public:
AccessPathVisitor(SILModule *module, NestedAccessType nestedAccessTy)
: FindAccessVisitorImpl(nestedAccessTy, IgnoreStorageCast),
module(module), storageVisitor(NestedAccessType::IgnoreAccessBegin) {}
// Main entry point.
AccessPathWithBase findAccessPath(SILValue sourceAddr) && {
this->reenterUseDef(sourceAddr);
if (auto storage = storageVisitor.getStorage()) {
return AccessPathWithBase(
AccessPath(storage, computeForwardPath(), pendingOffset),
storageVisitor.getBase());
}
return AccessPathWithBase(AccessPath(), SILValue());
}
protected:
void addPathOffset(int offset) {
if (pendingOffset == AccessPath::UnknownOffset)
return;
if (offset == AccessPath::UnknownOffset) {
pendingOffset = offset;
return;
}
// Accumulate static offsets
pendingOffset = pendingOffset + offset;
}
// Return the trie node corresponding to the current state of reversePath.
AccessPath::PathNode computeForwardPath() {
IndexTrieNode *forwardPath = module->getIndexTrieRoot();
for (AccessPath::Index nextIndex : llvm::reverse(reversePath)) {
forwardPath = forwardPath->getChild(nextIndex.getEncoding());
}
return AccessPath::PathNode(forwardPath);
}
public:
// MARK: AccessPhiVisitor::UseDefVisitor implementation.
bool isResultValid() const { return storageVisitor.isResultValid(); }
void invalidateResult() {
storageVisitor.invalidateResult();
// Don't clear reversePath. We my call restoreResult later.
pendingOffset = 0;
}
Result saveResult() const {
return Result(storageVisitor.saveResult(), pendingOffset,
reversePath.size());
}
void restoreResult(Result result) {
storageVisitor.restoreResult(result.storageResult);
pendingOffset = result.savedOffset;
assert(result.pathLength <= reversePath.size()
&& "a phi should only add to the path");
reversePath.erase(reversePath.begin() + result.pathLength,
reversePath.end());
}
void addUnknownOffset() { pendingOffset = AccessPath::UnknownOffset; }
// MARK: visitor implementation. Return the address source as the next use-def
// value to process. An invalid SILValue stops def-use traversal.
SILValue visitBase(SILValue base, AccessStorage::Kind kind) {
return storageVisitor.visitBase(base, kind);
}
SILValue visitNonAccess(SILValue value) {
invalidateResult();
return SILValue();
}
// Override FindAccessVisitorImpl to record path components.
SILValue visitAccessProjection(SingleValueInstruction *projectedAddr,
Operand *sourceAddr) {
auto projIdx = ProjectionIndex(projectedAddr);
if (auto *indexAddr = dyn_cast<IndexAddrInst>(projectedAddr)) {
addPathOffset(projIdx.isValid() ? projIdx.Index
: AccessPath::UnknownOffset);
} else if (isa<TailAddrInst>(projectedAddr)) {
addPathOffset(AccessPath::UnknownOffset);
} else if (projIdx.isValid()) {
if (pendingOffset) {
LLVM_DEBUG(llvm::dbgs() << "Subobject projection with offset index: "
<< *projectedAddr);
// Return an invalid result even though findAccessStorage() may be
// able to find valid storage, because an offset from a subobject is an
// invalid access path.
return visitNonAccess(projectedAddr);
}
reversePath.push_back(
AccessPath::Index::forSubObjectProjection(projIdx.Index));
} else {
// Ignore everything in getAccessProjectionOperand that is an access
// projection with no affect on the access path.
assert(isa<OpenExistentialAddrInst>(projectedAddr) ||
isa<InitEnumDataAddrInst>(projectedAddr) ||
isa<UncheckedTakeEnumDataAddrInst>(projectedAddr)
// project_box is not normally an access projection but we treat it
// as such when it operates on unchecked_take_enum_data_addr.
|| isa<ProjectBoxInst>(projectedAddr)
// Ignore mark_unresolved_non_copyable_value, we just look through
// it when we see it.
|| isa<MarkUnresolvedNonCopyableValueInst>(projectedAddr)
// Ignore moveonlywrapper_to_copyable_addr and
// copyable_to_moveonlywrapper_addr, we just look through it when
// we see it
|| isa<MoveOnlyWrapperToCopyableAddrInst>(projectedAddr) ||
isa<CopyableToMoveOnlyWrapperAddrInst>(projectedAddr));
}
return sourceAddr->get();
}
};
} // end anonymous namespace
AccessPathWithBase AccessPathWithBase::compute(SILValue address) {
return AccessPathVisitor(address->getModule(),
NestedAccessType::IgnoreAccessBegin)
.findAccessPath(address);
}
AccessPathWithBase AccessPathWithBase::computeInScope(SILValue address) {
return AccessPathVisitor(address->getModule(),
NestedAccessType::StopAtAccessBegin)
.findAccessPath(address);
}
bool swift::visitProductLeafAccessPathNodes(
SILValue address, TypeExpansionContext tec, SILModule &module,
std::function<void(AccessPath::PathNode, SILType)> visitor) {
auto rootPath = AccessPath::compute(address);
if (!rootPath.isValid()) {
return false;
}
SmallVector<std::pair<SILType, IndexTrieNode *>, 32> worklist;
auto *node = rootPath.getPathNode().node;
worklist.push_back({address->getType(), node});
while (!worklist.empty()) {
auto pair = worklist.pop_back_val();
auto silType = pair.first;
auto *node = pair.second;
if (auto tupleType = silType.getAs<TupleType>()) {
for (unsigned index : indices(tupleType->getElements())) {
auto *elementNode = node->getChild(index);
worklist.push_back({silType.getTupleElementType(index), elementNode});
}
} else if (auto *decl = silType.getStructOrBoundGenericStruct()) {
if (decl->isResilient(tec.getContext()->getParentModule(),
tec.getResilienceExpansion())) {
visitor(AccessPath::PathNode(node), silType);
continue;
}
if (decl->isCxxNonTrivial()) {
visitor(AccessPath::PathNode(node), silType);
continue;
}
unsigned index = 0;
for (auto *field : decl->getStoredProperties()) {
auto *fieldNode = node->getChild(index);
worklist.push_back(
{silType.getFieldType(field, module, tec), fieldNode});
++index;
}
} else {
visitor(AccessPath::PathNode(node), silType);
}
}
return true;
}
void AccessPath::Index::print(raw_ostream &os) const {
if (isSubObjectProjection())
os << '#' << getSubObjectIndex();
else {
os << '@';
if (isUnknownOffset())
os << "Unknown";
else
os << getOffset();
}
}
LLVM_ATTRIBUTE_USED void AccessPath::Index::dump() const {
print(llvm::dbgs());
}
static void recursivelyPrintPath(AccessPath::PathNode node, raw_ostream &os) {
AccessPath::PathNode parent = node.getParent();
if (!parent.isRoot()) {
recursivelyPrintPath(parent, os);
os << ",";
}
node.getIndex().print(os);
}
void AccessPath::printPath(raw_ostream &os) const {
os << "Path: ";
if (!isValid()) {
os << "INVALID\n";
return;
}
os << "(";
PathNode node = getPathNode();
if (offset != 0) {
Index::forOffset(offset).print(os);
if (!node.isRoot())
os << ",";
}
if (!node.isRoot())
recursivelyPrintPath(node, os);
os << ")\n";
}
void AccessPath::print(raw_ostream &os) const {
if (!isValid()) {
os << "INVALID\n";
return;
}
os << "Storage: ";
getStorage().print(os);
printPath(os);
}
LLVM_ATTRIBUTE_USED void AccessPath::dump() const { print(llvm::dbgs()); }
void AccessPathWithBase::print(raw_ostream &os) const {
if (base)
os << "Base: " << base;
else
os << "Base: unidentified\n";
accessPath.print(os);
}
LLVM_ATTRIBUTE_USED void AccessPathWithBase::dump() const {
print(llvm::dbgs());
}
//===----------------------------------------------------------------------===//
// MARK: AccessPathDefUseTraversal
//===----------------------------------------------------------------------===//
namespace {
// Perform def-use DFS traversal along a given AccessPath. DFS terminates at
// each discovered use.
//
// For useTy == Exact, the collected uses all have the same AccessPath.
// Subobject projections within that access path and their transitive uses are
// not included.
//
// For useTy == Inner, the collected uses to subobjects contained by the
// current access path.
//
// For useTy == Overlapping, the collected uses also include uses that
// access an object that contains the given AccessPath as well as uses at
// an unknown offset relative to the current path.
//
// Example, where AccessPath == (#2):
// %base = ... // access base
// load %base // containing use
// %elt1 = struct_element_addr %base, #1 // non-use (ignored)
// load %elt1 // non-use (unseen)
// %elt2 = struct_element_addr %base, #2 // outer projection (followed)
// load %elt2 // exact use
// %sub = struct_element_addr %elt2, %i // inner projection (followed)
// load %sub // inner use
//
// A use may be a BranchInst if the corresponding phi does not have common
// AccessStorage.
//
// For class storage, the def-use traversal starts at the reference
// root. Eventually, traversal reach the base address of the formal access:
//
// %ref = ... // reference root
// %base = ref_element_addr %refRoot // formal access address
// load %base // use
class AccessPathDefUseTraversal {
AccessUseVisitor &visitor;
// The origin of the def-use traversal.
AccessStorage storage;
// The base of the formal access. For class storage, it is the
// ref_element_addr. For global storage it is the global_addr or initializer
// apply. For other storage, it is the same as accessPath.getRoot().
//
// 'base' is typically invalid, maning that all uses of 'storage' for the
// access path will be visited. When 'base' is set, the visitor is
// restricted to a specific access base, such as a particular
// ref_element_addr.
SILValue base;
// Indices of the path to match from inner to outer component.
// A cursor is used to represent the most recently visited def.
// During def-use traversal, the cursor starts at the end of pathIndices and
// decrements with each projection.
// The first index represents an exact match.
// Index < 0 represents some subobject of the requested path.
SmallVector<AccessPath::Index, 4> pathIndices;
// A point in the def-use traversal. isRef() is true only for object access
// prior to reaching the base address.
struct DFSEntry {
// Next potential use to visit and flag indicating whether traversal has
// reached the access base yet.
llvm::PointerIntPair<Operand *, 1, bool> useAndIsRef;
int pathCursor; // position within pathIndices
int offset; // index_addr offsets seen prior to this use
DFSEntry(Operand *use, bool isRef, int pathCursor, int offset)
: useAndIsRef(use, isRef), pathCursor(pathCursor), offset(offset) {}
Operand *getUse() const { return useAndIsRef.getPointer(); }
// Is this pointer a reference?
bool isRef() const { return useAndIsRef.getInt(); }
};
SmallVector<DFSEntry, 16> dfsStack;
SmallPtrSet<const SILPhiArgument *, 4> visitedPhis;
// Transient traversal data should not be copied.
AccessPathDefUseTraversal(const AccessPathDefUseTraversal &) = delete;
AccessPathDefUseTraversal &
operator=(const AccessPathDefUseTraversal &) = delete;
public:
AccessPathDefUseTraversal(AccessUseVisitor &visitor, AccessPath accessPath,
SILFunction *function)
: visitor(visitor), storage(accessPath.getStorage()) {
assert(accessPath.isValid());
initializePathIndices(accessPath);
storage.visitRoots(function, [this](SILValue root) {
initializeDFS(root);
return true;
});
}
AccessPathDefUseTraversal(AccessUseVisitor &visitor,
AccessPathWithBase accessPathWithBase,
SILFunction *function)
: visitor(visitor), storage(accessPathWithBase.accessPath.getStorage()),
base(accessPathWithBase.getAccessBase().getBaseAddress()) {
auto accessPath = accessPathWithBase.accessPath;
assert(accessPath.isValid());
initializePathIndices(accessPath);
initializeDFS(base);
}
// Return true is all uses have been visited.
bool visitUses() {
// Return false if initialization failed.
if (!storage) {
return false;
}
while (!dfsStack.empty()) {
if (!visitUser(dfsStack.pop_back_val()))
return false;
}
return true;
}
protected:
void initializeDFS(SILValue root) {
// If root is a phi, record it so that its uses aren't visited twice.
if (auto *phi = dyn_cast<SILPhiArgument>(root)) {
if (phi->isPhi())
visitedPhis.insert(phi);
}
bool isRef = !base && storage.isReference();
pushUsers(root, DFSEntry(nullptr, isRef, pathIndices.size(), 0));
}
void pushUsers(SILValue def, const DFSEntry &dfs) {
for (auto *use : def->getUses())
pushUser(DFSEntry(use, dfs.isRef(), dfs.pathCursor, dfs.offset));
}
void pushUser(DFSEntry dfs) {
Operand *use = dfs.getUse();
if (auto *bi = dyn_cast<BranchInst>(use->getUser())) {
if (pushPhiUses(bi->getArgForOperand(use), dfs))
return;
}
// If we didn't find and process a phi, continue DFS.
dfsStack.emplace_back(dfs);
}
bool pushPhiUses(const SILPhiArgument *phi, DFSEntry dfs);
void initializePathIndices(AccessPath accessPath);
// Return the offset at the current DFS path cursor, or zero.
int getPathOffset(const DFSEntry &dfs) const;
// Return true if the accumulated offset matches the current path index.
// Update the DFSEntry and pathCursor to skip remaining offsets.
bool checkAndUpdateOffset(DFSEntry &dfs);
// Handle non-index_addr projections.
void followProjection(SingleValueInstruction *svi, DFSEntry dfs);
enum UseKind { LeafUse, IgnoredUse };
UseKind visitSingleValueUser(SingleValueInstruction *svi, DFSEntry dfs);
// Returns true as long as the visitor returns true.
bool visitUser(DFSEntry dfs);
};
} // end anonymous namespace
// Initialize the array of remaining path indices.
void AccessPathDefUseTraversal::initializePathIndices(AccessPath accessPath) {
for (AccessPath::PathNode currentNode = accessPath.getPathNode();
!currentNode.isRoot(); currentNode = currentNode.getParent()) {
assert(currentNode.getIndex().isSubObjectProjection()
&& "a valid AccessPath does not contain any intermediate offsets");
pathIndices.push_back(currentNode.getIndex());
}
if (int offset = accessPath.getOffset()) {
pathIndices.push_back(AccessPath::Index::forOffset(offset));
}
// If traversal starts at the base address, then class storage is irrelevant.
if (base)
return;
// The search will start from the object root, not the formal access base,
// so add the class index to the front.
if (storage.getKind() == AccessStorage::Class) {
pathIndices.push_back(
AccessPath::Index::forSubObjectProjection(storage.getPropertyIndex()));
}
if (storage.getKind() == AccessStorage::Tail) {
pathIndices.push_back(
AccessPath::Index::forSubObjectProjection(ProjectionIndex::TailIndex));
}
// If the expected path has an unknown offset, then none of the uses are
// exact.
if (!visitor.findOverlappingUses() && !pathIndices.empty()
&& pathIndices.back().isUnknownOffset()) {
return;
}
}
// Return true if this phi has been processed and does not need to be
// considered as a separate use.
bool AccessPathDefUseTraversal::pushPhiUses(const SILPhiArgument *phi,
DFSEntry dfs) {
if (!visitedPhis.insert(phi).second)
return true;
// If this phi has a common base, continue to follow the access path. This
// check is different for reference types vs pointer types.
if (dfs.isRef()) {
assert(!dfs.offset && "index_addr not allowed on reference roots");
// When isRef is true, the address access hasn't been seen yet and
// we're still following the reference root's users. Check if all phi
// inputs have the same reference root before looking through it.
if (findReferenceRoot(phi) == storage.getObject()) {
pushUsers(phi, dfs);
return true;
}
// The branch will be pushed onto the normal user list.
return false;
}
// Check if all phi inputs have the same accessed storage before
// looking through it. If the phi input differ the its storage is invalid.
auto phiPath = AccessPath::compute(phi);
if (phiPath.isValid()) {
assert(phiPath.getStorage().hasIdenticalStorage(storage)
&& "inconsistent phi storage");
// If the phi paths have different offsets, its path has unknown offset.
if (phiPath.getOffset() == AccessPath::UnknownOffset) {
if (!visitor.findOverlappingUses())
return true;
dfs.offset = AccessPath::UnknownOffset;
}
pushUsers(phi, dfs);
return true;
}
// The branch will be pushed onto the normal user list.
return false;
}
// Return the offset at the current DFS path cursor, or zero.
int AccessPathDefUseTraversal::getPathOffset(const DFSEntry &dfs) const {
if (dfs.pathCursor <= 0
|| pathIndices[dfs.pathCursor - 1].isSubObjectProjection()) {
return 0;
}
return pathIndices[dfs.pathCursor - 1].getOffset();
}
// Return true if the accumulated offset matches the current path index.
// Update the DFSEntry and pathCursor to skip remaining offsets.
bool AccessPathDefUseTraversal::checkAndUpdateOffset(DFSEntry &dfs) {
int pathOffset = getPathOffset(dfs);
if (dfs.offset == AccessPath::UnknownOffset) {
if (pathOffset != 0) {
// Pop the offset from the expected path; there should only be
// one. Continue matching subobject indices even after seeing an unknown
// offset. A subsequent mismatching subobject index is still considered
// non-overlapping. This is valid for aliasing since an offset from a
// subobject is considered an invalid access path.
--dfs.pathCursor;
assert(getPathOffset(dfs) == 0 && "only one offset index allowed");
}
// Continue searching only if we need to find overlapping uses. Preserve the
// unknown dfs offset so we don't consider any dependent operations to be
// exact or inner uses.
return visitor.findOverlappingUses();
}
if (pathOffset == 0) {
return dfs.offset == 0;
}
// pop the offset from the expected path; there should only be one.
--dfs.pathCursor;
assert(getPathOffset(dfs) == 0 && "only one offset index allowed");
// Ignore all uses on this path unless we're collecting containing uses.
// UnknownOffset appears to overlap with all offsets and subobject uses.
if (pathOffset == AccessPath::UnknownOffset) {
// Set the dfs offset to unknown to avoid considering any dependent
// operations as exact or inner uses.
dfs.offset = AccessPath::UnknownOffset;
return visitor.findOverlappingUses();
}
int useOffset = dfs.offset;
dfs.offset = 0;
// A known offset must match regardless of findOverlappingUses.
return pathOffset == useOffset;
}
// Handle non-index_addr projections.
void AccessPathDefUseTraversal::followProjection(SingleValueInstruction *svi,
DFSEntry dfs) {
if (!checkAndUpdateOffset(dfs)) {
return;
}
if (dfs.pathCursor <= 0) {
if (visitor.useTy == AccessUseType::Exact) {
assert(dfs.pathCursor == 0);
return;
}
--dfs.pathCursor;
pushUsers(svi, dfs);
return;
}
AccessPath::Index pathIndex = pathIndices[dfs.pathCursor - 1];
auto projIdx = ProjectionIndex(svi);
assert(projIdx.isValid());
// Only subobjects indices are expected because offsets are handled above.
if (projIdx.Index == pathIndex.getSubObjectIndex()) {
--dfs.pathCursor;
pushUsers(svi, dfs);
}
return;
}
// During the def-use traversal, visit a single-value instruction in which the
// used address is at operand zero.
//
// This must handle the def-use side of all operations that
// AccessUseDefChainVisitor::visit can handle.
//
// Return IgnoredUse if the def-use traversal either continues past \p
// svi or ignores this use.
//
// FIXME: Reuse getAccessProjectionOperand() instead of using special cases once
// the unchecked_take_enum_data_addr -> load -> project_box pattern is fixed.
AccessPathDefUseTraversal::UseKind
AccessPathDefUseTraversal::visitSingleValueUser(SingleValueInstruction *svi,
DFSEntry dfs) {
if (dfs.isRef()) {
if (isIdentityPreservingRefCast(svi)) {
pushUsers(svi, dfs);
return IgnoredUse;
}
// 'svi' will be processed below as either RefElementAddrInst,
// RefTailAddrInst, or some unknown LeafUse.
} else if (isAccessStorageCast(svi)) {
pushUsers(svi, dfs);
return IgnoredUse;
}
switch (svi->getKind()) {
default:
return LeafUse;
case SILInstructionKind::BeginAccessInst:
if (visitor.nestedAccessTy == NestedAccessType::StopAtAccessBegin) {
return LeafUse;
}
pushUsers(svi, dfs);
return IgnoredUse;
// Handle ref_element_addr, ref_tail_addr, and project_box since we start at
// the object root instead of the access base.
case SILInstructionKind::RefElementAddrInst:
assert(dfs.isRef());
assert(dfs.pathCursor > 0 && "ref_element_addr cannot occur within access");
dfs.useAndIsRef.setInt(false);
followProjection(svi, dfs);
return IgnoredUse;
case SILInstructionKind::RefTailAddrInst: {
assert(dfs.isRef());
assert(dfs.pathCursor > 0 && "ref_tail_addr cannot occur within an access");
dfs.useAndIsRef.setInt(false);
--dfs.pathCursor;
AccessPath::Index pathIndex = pathIndices[dfs.pathCursor];
assert(pathIndex.isSubObjectProjection());
if (pathIndex.getSubObjectIndex() == AccessStorage::TailIndex)
pushUsers(svi, dfs);
return IgnoredUse;
}
case SILInstructionKind::ProjectBoxInst: {
assert(dfs.isRef());
dfs.useAndIsRef.setInt(false);
pushUsers(svi, dfs);
return IgnoredUse;
}
case SILInstructionKind::DropDeinitInst:
case SILInstructionKind::MarkUnresolvedNonCopyableValueInst: {
// Mark must check goes on the project_box, so it isn't a ref.
assert(!dfs.isRef());
pushUsers(svi, dfs);
return IgnoredUse;
}
// Look through both of these.
case SILInstructionKind::MoveOnlyWrapperToCopyableAddrInst:
case SILInstructionKind::CopyableToMoveOnlyWrapperAddrInst:
pushUsers(svi, dfs);
return IgnoredUse;
// MARK: Access projections
case SILInstructionKind::StructElementAddrInst:
case SILInstructionKind::TupleElementAddrInst:
followProjection(svi, dfs);
return IgnoredUse;
case SILInstructionKind::IndexAddrInst:
case SILInstructionKind::TailAddrInst: {
auto projIdx = ProjectionIndex(svi);
if (projIdx.isValid()) {
if (dfs.offset != AccessPath::UnknownOffset)
dfs.offset += projIdx.Index;
else
assert(visitor.findOverlappingUses());
} else if (visitor.findOverlappingUses()) {
dfs.offset = AccessPath::UnknownOffset;
} else {
return IgnoredUse;
}
pushUsers(svi, dfs);
return IgnoredUse;
}
case SILInstructionKind::InitEnumDataAddrInst:
pushUsers(svi, dfs);
return IgnoredUse;
// open_existential_addr and unchecked_take_enum_data_addr are classified as
// access projections, but they also modify memory. Both see through them and
// also report them as uses.
case SILInstructionKind::OpenExistentialAddrInst:
case SILInstructionKind::UncheckedTakeEnumDataAddrInst:
pushUsers(svi, dfs);
return LeafUse;
case SILInstructionKind::StructExtractInst:
// Handle nested access to a KeyPath projection. The projection itself
// uses a Builtin. However, the returned UnsafeMutablePointer may be
// converted to an address and accessed via an inout argument.
if (isUnsafePointerExtraction(cast<StructExtractInst>(svi))) {
pushUsers(svi, dfs);
return IgnoredUse;
}
return LeafUse;
case SILInstructionKind::LoadInst:
// Load a box from an indirect payload of an opaque enum. See comments
// in AccessUseDefChainVisitor::visit. Record this load as a leaf-use even
// when we look through its project_box because anyone inspecting the load
// itself will see the same AccessPath.
// FIXME: if this doesn't go away with opaque values, add a new instruction
// for load+project_box.
if (svi->getType().is<SILBoxType>()) {
Operand *addrOper = &cast<LoadInst>(svi)->getOperandRef();
assert(isa<UncheckedTakeEnumDataAddrInst>(addrOper->get()));
// Push the project_box uses
for (auto *use : svi->getUses()) {
if (auto *projectBox = dyn_cast<ProjectBoxInst>(use->getUser())) {
assert(!dfs.isRef() && "originates from an enum address");
pushUsers(projectBox, dfs);
}
}
}
return LeafUse;
}
}
bool AccessPathDefUseTraversal::visitUser(DFSEntry dfs) {
Operand *use = dfs.getUse();
assert(!(dfs.isRef() && use->get()->getType().isAddress()));
if (auto *svi = dyn_cast<SingleValueInstruction>(use->getUser())) {
if (use->getOperandNumber() == 0
&& visitSingleValueUser(svi, dfs) == IgnoredUse) {
return true;
}
}
if (auto *sbi = dyn_cast<StoreBorrowInst>(use->getUser())) {
if (use->get() == sbi->getDest()) {
pushUsers(sbi, dfs);
}
}
if (isa<EndBorrowInst>(use->getUser())) {
return true;
}
// We weren't able to "see through" any more address conversions; so
// record this as a use.
// Do the path offsets match?
if (!checkAndUpdateOffset(dfs))
return true;
// Is this a partial path match?
if (dfs.pathCursor > 0 || dfs.offset == AccessPath::UnknownOffset) {
return visitor.visitOverlappingUse(use);
}
if (dfs.pathCursor < 0) {
return visitor.visitInnerUse(use);
}
return visitor.visitExactUse(use);
}
bool swift::visitAccessPathUses(AccessUseVisitor &visitor,
AccessPath accessPath, SILFunction *function) {
return AccessPathDefUseTraversal(visitor, accessPath, function).visitUses();
}
bool swift::visitAccessPathBaseUses(AccessUseVisitor &visitor,
AccessPathWithBase accessPathWithBase,
SILFunction *function) {
return AccessPathDefUseTraversal(visitor, accessPathWithBase, function)
.visitUses();
}
namespace swift::test {
struct AccessUseTestVisitor : public AccessUseVisitor {
AccessUseTestVisitor()
: AccessUseVisitor(AccessUseType::Overlapping,
NestedAccessType::IgnoreAccessBegin) {}
bool visitUse(Operand *op, AccessUseType useTy) override {
switch (useTy) {
case AccessUseType::Exact:
llvm::errs() << "Exact Use: ";
break;
case AccessUseType::Inner:
llvm::errs() << "Inner Use: ";
break;
case AccessUseType::Overlapping:
llvm::errs() << "Overlapping Use ";
break;
}
llvm::errs() << *op->getUser();
return true;
}
};
static FunctionTest AccessPathBaseTest("accesspath-base", [](auto &function,
auto &arguments,
auto &test) {
auto value = arguments.takeValue();
function.print(llvm::outs());
llvm::outs() << "Access path base: " << value;
auto accessPathWithBase = AccessPathWithBase::compute(value);
AccessUseTestVisitor visitor;
visitAccessPathBaseUses(visitor, accessPathWithBase, &function);
});
} // end namespace swift::test
bool swift::visitAccessStorageUses(AccessUseVisitor &visitor,
AccessStorage storage,
SILFunction *function) {
IndexTrieNode *emptyPath = function->getModule().getIndexTrieRoot();
return visitAccessPathUses(visitor, AccessPath(storage, emptyPath, 0),
function);
}
class CollectAccessPathUses : public AccessUseVisitor {
// Result: Exact uses, projection uses, and containing uses.
SmallVectorImpl<Operand *> &uses;
unsigned useLimit;
public:
CollectAccessPathUses(SmallVectorImpl<Operand *> &uses, AccessUseType useTy,
unsigned useLimit)
: AccessUseVisitor(useTy, NestedAccessType::IgnoreAccessBegin), uses(uses),
useLimit(useLimit) {}
bool visitUse(Operand *use, AccessUseType useTy) override {
if (uses.size() == useLimit) {
return false;
}
uses.push_back(use);
return true;
}
};
bool AccessPath::collectUses(SmallVectorImpl<Operand *> &uses,
AccessUseType useTy, SILFunction *function,
unsigned useLimit) const {
CollectAccessPathUses collector(uses, useTy, useLimit);
return visitAccessPathUses(collector, *this, function);
}
//===----------------------------------------------------------------------===//
// MARK: UniqueStorageUseVisitor
//===----------------------------------------------------------------------===//
struct GatherUniqueStorageUses : public AccessUseVisitor {
UniqueStorageUseVisitor &visitor;
GatherUniqueStorageUses(UniqueStorageUseVisitor &visitor)
: AccessUseVisitor(AccessUseType::Overlapping,
NestedAccessType::IgnoreAccessBegin),
visitor(visitor) {}
bool visitUse(Operand *use, AccessUseType useTy) override;
};
bool UniqueStorageUseVisitor::findUses(UniqueStorageUseVisitor &visitor) {
assert(visitor.storage.isUniquelyIdentified() ||
visitor.storage.getKind() == AccessStorage::Kind::Nested);
GatherUniqueStorageUses gather(visitor);
return visitAccessStorageUses(gather, visitor.storage, visitor.function);
}
static bool
visitApplyOperand(Operand *use, UniqueStorageUseVisitor &visitor,
bool (UniqueStorageUseVisitor::*visit)(Operand *)) {
auto *user = use->getUser();
if (auto *bai = dyn_cast<BeginApplyInst>(user)) {
if (!(visitor.*visit)(use))
return false;
SmallVector<Operand *, 2> endApplyUses;
SmallVector<Operand *, 2> abortApplyUses;
bai->getCoroutineEndPoints(endApplyUses, abortApplyUses);
for (auto *endApplyUse : endApplyUses) {
if (!(visitor.*visit)(endApplyUse))
return false;
}
for (auto *abortApplyUse : abortApplyUses) {
if (!(visitor.*visit)(abortApplyUse))
return false;
}
return true;
}
return (visitor.*visit)(use);
}
bool GatherUniqueStorageUses::visitUse(Operand *use, AccessUseType useTy) {
unsigned operIdx = use->getOperandNumber();
auto *user = use->getUser();
assert(!user->isTypeDependentOperand(*use));
// TODO: handle non-escaping partial-applies just like a full apply. The
// address uses are the points where the partial apply is invoked.
if (FullApplySite apply = FullApplySite::isa(user)) {
switch (apply.getArgumentConvention(*use)) {
case SILArgumentConvention::Indirect_Inout:
case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Indirect_Out:
case SILArgumentConvention::Pack_Inout:
case SILArgumentConvention::Pack_Out:
return visitApplyOperand(use, visitor,
&UniqueStorageUseVisitor::visitStore);
case SILArgumentConvention::Indirect_In_Guaranteed:
case SILArgumentConvention::Indirect_In:
case SILArgumentConvention::Pack_Owned:
case SILArgumentConvention::Pack_Guaranteed:
return visitApplyOperand(use, visitor,
&UniqueStorageUseVisitor::visitLoad);
case SILArgumentConvention::Direct_Unowned:
case SILArgumentConvention::Direct_Owned:
case SILArgumentConvention::Direct_Guaranteed:
// most likely an escape of a box
return visitApplyOperand(use, visitor,
&UniqueStorageUseVisitor::visitUnknownUse);
}
}
switch (user->getKind()) {
case SILInstructionKind::BeginAccessInst:
return visitor.visitBeginAccess(use);
case SILInstructionKind::DestroyAddrInst:
case SILInstructionKind::DestroyValueInst:
if (useTy == AccessUseType::Exact) {
return visitor.visitDestroy(use);
}
return visitor.visitUnknownUse(use);
case SILInstructionKind::DebugValueInst:
return visitor.visitDebugUse(use);
case SILInstructionKind::EndAccessInst:
return true;
case SILInstructionKind::LoadInst:
case SILInstructionKind::LoadWeakInst:
case SILInstructionKind::LoadUnownedInst:
case SILInstructionKind::ExistentialMetatypeInst:
return visitor.visitLoad(use);
case SILInstructionKind::StoreInst:
case SILInstructionKind::StoreWeakInst:
case SILInstructionKind::StoreUnownedInst:
if (operIdx == CopyLikeInstruction::Dest) {
return visitor.visitStore(use);
}
break;
case SILInstructionKind::InjectEnumAddrInst:
return visitor.visitStore(use);
case SILInstructionKind::ExplicitCopyAddrInst:
case SILInstructionKind::CopyAddrInst:
if (operIdx == CopyLikeInstruction::Dest) {
return visitor.visitStore(use);
}
assert(operIdx == CopyLikeInstruction::Src);
return visitor.visitLoad(use);
case SILInstructionKind::DeallocStackInst:
return visitor.visitDealloc(use);
default:
break;
}
return visitor.visitUnknownUse(use);
}
//===----------------------------------------------------------------------===//
// MARK: Helper API for specific formal access patterns
//===----------------------------------------------------------------------===//
static bool isScratchBuffer(SILValue value) {
// Special case unsafe value buffer access.
return value->getType().is<BuiltinUnsafeValueBufferType>();
}
bool swift::memInstMustInitialize(Operand *memOper) {
SILValue address = memOper->get();
SILInstruction *memInst = memOper->getUser();
switch (memInst->getKind()) {
default:
return false;
case SILInstructionKind::CopyAddrInst: {
auto *CAI = cast<CopyAddrInst>(memInst);
return CAI->getDest() == address && CAI->isInitializationOfDest();
}
case SILInstructionKind::ExplicitCopyAddrInst: {
auto *CAI = cast<ExplicitCopyAddrInst>(memInst);
return CAI->getDest() == address && CAI->isInitializationOfDest();
}
case SILInstructionKind::MarkUnresolvedMoveAddrInst: {
return cast<MarkUnresolvedMoveAddrInst>(memInst)->getDest() == address;
}
case SILInstructionKind::InitExistentialAddrInst:
case SILInstructionKind::InitEnumDataAddrInst:
case SILInstructionKind::InjectEnumAddrInst:
return true;
case SILInstructionKind::BeginApplyInst:
case SILInstructionKind::TryApplyInst:
case SILInstructionKind::ApplyInst: {
FullApplySite applySite(memInst);
return applySite.isIndirectResultOperand(*memOper);
}
case SILInstructionKind::StoreInst:
return cast<StoreInst>(memInst)->getOwnershipQualifier()
== StoreOwnershipQualifier::Init;
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst: \
return cast<Store##Name##Inst>(memInst)->isInitializationOfDest();
#include "swift/AST/ReferenceStorage.def"
}
}
Operand *
swift::getSingleInitAllocStackUse(AllocStackInst *asi,
SmallVectorImpl<Operand *> *destroyingUses) {
// For now, we just look through projections and rely on memInstMustInitialize
// to classify all other uses as init or not.
SmallVector<Operand *, 32> worklist(asi->getUses());
Operand *singleInit = nullptr;
while (!worklist.empty()) {
auto *use = worklist.pop_back_val();
auto *user = use->getUser();
if (Projection::isAddressProjection(user)
|| isa<OpenExistentialAddrInst>(user)) {
// Look through address projections.
for (SILValue r : user->getResults()) {
llvm::copy(r->getUses(), std::back_inserter(worklist));
}
continue;
}
if (auto *li = dyn_cast<LoadInst>(user)) {
// If we are not taking,
if (li->getOwnershipQualifier() != LoadOwnershipQualifier::Take) {
continue;
}
// Treat load [take] as a write.
return nullptr;
}
switch (user->getKind()) {
default:
break;
case SILInstructionKind::UnconditionalCheckedCastAddrInst: {
auto *uccai = cast<UnconditionalCheckedCastAddrInst>(user);
// Only handle the case where we are doing a take of our alloc_stack as a
// source value. If we are the dest, then something else is happening!
// Break!
if (use->get() == uccai->getDest())
break;
// Ok, we are the Src and are performing a take. Treat it as a destroy!
if (destroyingUses)
destroyingUses->push_back(use);
continue;
}
case SILInstructionKind::CheckedCastAddrBranchInst: {
auto *ccabi = cast<CheckedCastAddrBranchInst>(user);
// We only handle the case where we are doing a take of our alloc_stack as
// a source.
//
// TODO: Can we expand this?
if (use->get() == ccabi->getDest())
break;
if (ccabi->getConsumptionKind() != CastConsumptionKind::TakeAlways)
break;
// Ok, we are the Src and are performing a take. Treat it as a destroy!
if (destroyingUses)
destroyingUses->push_back(use);
continue;
}
case SILInstructionKind::DestroyAddrInst:
if (destroyingUses)
destroyingUses->push_back(use);
continue;
case SILInstructionKind::DebugValueInst:
if (cast<DebugValueInst>(user)->hasAddrVal())
continue;
break;
case SILInstructionKind::DeallocStackInst:
case SILInstructionKind::LoadBorrowInst:
continue;
}
// See if we have an initializer and that such initializer is in the same
// block.
if (memInstMustInitialize(use)) {
if (user->getParent() != asi->getParent() || singleInit) {
return nullptr;
}
singleInit = use;
continue;
}
// Otherwise, if we have found something not in our allowlist, return false.
return nullptr;
}
// We did not find any users that we did not understand. So we can
// conservatively return the single initializing write that we found.
return singleInit;
}
/// Return true if the given address value is produced by a special address
/// producer that is only used for local initialization, not formal access.
bool swift::isAddressForLocalInitOnly(SILValue sourceAddr) {
switch (sourceAddr->getKind()) {
default:
return false;
// Value to address conversions: the operand is the non-address source
// value. These allow local mutation of the value but should never be used
// for formal access of an lvalue.
case ValueKind::OpenExistentialBoxInst:
case ValueKind::ProjectExistentialBoxInst:
return true;
// Self-evident local initialization.
case ValueKind::InitEnumDataAddrInst:
case ValueKind::InitExistentialAddrInst:
case ValueKind::AllocExistentialBoxInst:
return true;
}
}
// Return true if the given apply invokes a global addressor defined in another
// module.
bool swift::isExternalGlobalAddressor(ApplyInst *AI) {
FullApplySite apply(AI);
auto *funcRef = apply.getReferencedFunctionOrNull();
if (!funcRef)
return false;
return funcRef->isGlobalInit() && funcRef->isExternalDeclaration();
}
// Return true if the given StructExtractInst extracts the RawPointer from
// Unsafe[Mutable]Pointer.
bool swift::isUnsafePointerExtraction(StructExtractInst *SEI) {
if (!isa<BuiltinRawPointerType>(SEI->getType().getASTType()))
return false;
auto &C = SEI->getModule().getASTContext();
auto *decl = SEI->getStructDecl();
return decl == C.getUnsafeMutablePointerDecl() ||
decl == C.getUnsafePointerDecl();
}
// Given a block argument address base, check if it is actually a box projected
// from a switch_enum. This is a valid pattern at any SIL stage resulting in a
// block-type phi. In later SIL stages, the optimizer may form address-type
// phis, causing this assert if called on those cases.
void swift::checkSwitchEnumBlockArg(SILPhiArgument *arg) {
assert(!arg->getType().isAddress());
SILBasicBlock *Pred = arg->getParent()->getSinglePredecessorBlock();
if (!Pred || !isa<SwitchEnumInst>(Pred->getTerminator())) {
arg->dump();
llvm_unreachable("unexpected box source.");
}
}
bool swift::isPossibleFormalAccessStorage(const AccessStorage &storage,
SILFunction *F) {
switch (storage.getKind()) {
case AccessStorage::Nested:
assert(false && "don't pass nested storage to this helper");
return false;
case AccessStorage::Box:
case AccessStorage::Stack:
if (isScratchBuffer(storage.getValue()))
return false;
break;
case AccessStorage::Global:
break;
case AccessStorage::Class:
break;
case AccessStorage::Tail:
return false;
case AccessStorage::Yield:
// Yields are accessed by the caller.
return false;
case AccessStorage::Argument:
// Function arguments are accessed by the caller.
return false;
case AccessStorage::Unidentified:
if (isAddressForLocalInitOnly(storage.getValue()))
return false;
if (isa<SILPhiArgument>(storage.getValue())) {
checkSwitchEnumBlockArg(cast<SILPhiArgument>(storage.getValue()));
return false;
}
// Pointer-to-address exclusivity cannot be enforced. `baseAddress` may be
// pointing anywhere within an object.
if (isa<PointerToAddressInst>(storage.getValue()))
return false;
if (isa<SILUndef>(storage.getValue()))
return false;
if (isScratchBuffer(storage.getValue()))
return false;
break;
}
// Additional checks that apply to anything that may fall through.
// Immutable values are only accessed for initialization.
if (storage.isLetAccess())
return false;
return true;
}
SILBasicBlock::iterator swift::removeBeginAccess(BeginAccessInst *beginAccess) {
while (!beginAccess->use_empty()) {
Operand *op = *beginAccess->use_begin();
// Delete any associated end_access instructions.
if (auto endAccess = dyn_cast<EndAccessInst>(op->getUser())) {
endAccess->eraseFromParent();
// Forward all other uses to the original address.
} else {
op->set(beginAccess->getSource());
}
}
auto nextIter = std::next(beginAccess->getIterator());
beginAccess->getParent()->erase(beginAccess);
return nextIter;
}
//===----------------------------------------------------------------------===//
// MARK: Verification
//===----------------------------------------------------------------------===//
// Helper for visitApplyAccesses that visits address-type call arguments,
// including arguments to @noescape functions that are passed as closures to
// the current call.
static void visitApplyAccesses(ApplySite apply,
llvm::function_ref<void(Operand *)> visitor) {
for (Operand &oper : apply.getArgumentOperands()) {
// Consider any address-type operand an access. Whether it is read or modify
// depends on the argument convention.
if (oper.get()->getType().isAddress()) {
visitor(&oper);
continue;
}
auto fnType = oper.get()->getType().getAs<SILFunctionType>();
if (!fnType || !fnType->isNoEscape())
continue;
// When @noescape function closures are passed as arguments, their
// arguments are considered accessed at the call site.
TinyPtrVector<PartialApplyInst *> partialApplies;
findClosuresForFunctionValue(oper.get(), partialApplies);
// Recursively visit @noescape function closure arguments.
for (auto *PAI : partialApplies)
visitApplyAccesses(PAI, visitor);
}
}
static void visitBuiltinAddress(BuiltinInst *builtin,
llvm::function_ref<void(Operand *)> visitor) {
if (auto kind = builtin->getBuiltinKind()) {
switch (kind.value()) {
default:
builtin->dump();
llvm_unreachable("unexpected builtin memory access.");
// Handle builtin "generic_add"<V>($*V, $*V, $*V) and the like.
#define BUILTIN(Id, Name, Attrs)
#define BUILTIN_BINARY_OPERATION_POLYMORPHIC(Id, Name) \
case BuiltinValueKind::Id:
#include "swift/AST/Builtins.def"
visitor(&builtin->getAllOperands()[1]);
visitor(&builtin->getAllOperands()[2]);
return;
// Writes back to the first operand.
case BuiltinValueKind::TaskRunInline:
visitor(&builtin->getAllOperands()[0]);
return;
// These effect both operands.
case BuiltinValueKind::Copy:
visitor(&builtin->getAllOperands()[1]);
return;
// These consume values out of their second operand.
case BuiltinValueKind::ResumeNonThrowingContinuationReturning:
case BuiltinValueKind::ResumeThrowingContinuationReturning:
case BuiltinValueKind::ResumeThrowingContinuationThrowing:
visitor(&builtin->getAllOperands()[1]);
return;
// WillThrow exists for the debugger, does nothing.
case BuiltinValueKind::WillThrow:
return;
// Buitins that affect memory but can't be formal accesses.
case BuiltinValueKind::AssumeTrue:
case BuiltinValueKind::UnexpectedError:
case BuiltinValueKind::ErrorInMain:
case BuiltinValueKind::IsOptionalType:
case BuiltinValueKind::CondFailMessage:
case BuiltinValueKind::AllocRaw:
case BuiltinValueKind::DeallocRaw:
case BuiltinValueKind::StackAlloc:
case BuiltinValueKind::UnprotectedStackAlloc:
case BuiltinValueKind::AllocVector:
case BuiltinValueKind::StackDealloc:
case BuiltinValueKind::Fence:
case BuiltinValueKind::StaticReport:
case BuiltinValueKind::Once:
case BuiltinValueKind::OnceWithContext:
case BuiltinValueKind::Unreachable:
case BuiltinValueKind::CondUnreachable:
case BuiltinValueKind::DestroyArray:
case BuiltinValueKind::PoundAssert:
case BuiltinValueKind::TSanInoutAccess:
case BuiltinValueKind::CancelAsyncTask:
case BuiltinValueKind::CreateAsyncTask:
case BuiltinValueKind::AutoDiffCreateLinearMapContextWithType:
case BuiltinValueKind::AutoDiffAllocateSubcontextWithType:
case BuiltinValueKind::InitializeDefaultActor:
case BuiltinValueKind::InitializeDistributedRemoteActor:
case BuiltinValueKind::InitializeNonDefaultDistributedActor:
case BuiltinValueKind::DestroyDefaultActor:
case BuiltinValueKind::GetCurrentExecutor:
case BuiltinValueKind::StartAsyncLet:
case BuiltinValueKind::StartAsyncLetWithLocalBuffer:
case BuiltinValueKind::EndAsyncLet:
case BuiltinValueKind::EndAsyncLetLifetime:
case BuiltinValueKind::CreateTaskGroup:
case BuiltinValueKind::CreateTaskGroupWithFlags:
case BuiltinValueKind::DestroyTaskGroup:
return;
// General memory access to a pointer in first operand position.
case BuiltinValueKind::CmpXChg:
case BuiltinValueKind::AtomicLoad:
case BuiltinValueKind::AtomicStore:
case BuiltinValueKind::AtomicRMW:
// Currently ignored because the access is on a RawPointer, not a
// SIL address.
// visitor(&builtin->getAllOperands()[0]);
return;
// zeroInitializer with an address operand zeroes the address.
case BuiltinValueKind::ZeroInitializer:
if (builtin->getAllOperands().size() > 0) {
visitor(&builtin->getAllOperands()[0]);
}
return;
// These builtins take a generic 'T' as their operand.
case BuiltinValueKind::GetEnumTag:
case BuiltinValueKind::InjectEnumTag:
case BuiltinValueKind::AddressOfRawLayout:
visitor(&builtin->getAllOperands()[0]);
return;
// Arrays: (T.Type, Builtin.RawPointer, Builtin.RawPointer,
// Builtin.Word)
case BuiltinValueKind::CopyArray:
case BuiltinValueKind::TakeArrayNoAlias:
case BuiltinValueKind::TakeArrayFrontToBack:
case BuiltinValueKind::TakeArrayBackToFront:
case BuiltinValueKind::AssignCopyArrayNoAlias:
case BuiltinValueKind::AssignCopyArrayFrontToBack:
case BuiltinValueKind::AssignCopyArrayBackToFront:
case BuiltinValueKind::AssignTakeArray:
// Currently ignored because the access is on a RawPointer.
// visitor(&builtin->getAllOperands()[1]);
// visitor(&builtin->getAllOperands()[2]);
return;
}
}
if (auto ID = builtin->getIntrinsicID()) {
switch (ID.value()) {
// Exhaustively verifying all LLVM intrinsics that access memory is
// impractical. Instead, we call out the few common cases and return in
// the default case.
default:
return;
case llvm::Intrinsic::memcpy:
case llvm::Intrinsic::memmove:
// Currently ignored because the access is on a RawPointer.
// visitor(&builtin->getAllOperands()[0]);
// visitor(&builtin->getAllOperands()[1]);
return;
case llvm::Intrinsic::memset:
// Currently ignored because the access is on a RawPointer.
// visitor(&builtin->getAllOperands()[0]);
return;
}
}
llvm_unreachable("Must be either a builtin or intrinsic.");
}
void swift::visitAccessedAddress(SILInstruction *I,
llvm::function_ref<void(Operand *)> visitor) {
assert(I->mayReadOrWriteMemory());
// Reference counting instructions do not access user visible memory.
if (isa<RefCountingInst>(I))
return;
if (isa<DeallocationInst>(I))
return;
if (auto apply = FullApplySite::isa(I)) {
visitApplyAccesses(apply, visitor);
return;
}
if (auto builtin = dyn_cast<BuiltinInst>(I)) {
visitBuiltinAddress(builtin, visitor);
return;
}
switch (I->getKind()) {
default:
I->dump();
llvm_unreachable("unexpected memory access.");
case SILInstructionKind::AssignInst:
case SILInstructionKind::AssignByWrapperInst:
case SILInstructionKind::AssignOrInitInst:
visitor(&I->getAllOperands()[AssignInst::Dest]);
return;
case SILInstructionKind::CheckedCastAddrBranchInst:
visitor(&I->getAllOperands()[CheckedCastAddrBranchInst::Src]);
visitor(&I->getAllOperands()[CheckedCastAddrBranchInst::Dest]);
return;
case SILInstructionKind::CopyAddrInst:
visitor(&I->getAllOperands()[CopyAddrInst::Src]);
visitor(&I->getAllOperands()[CopyAddrInst::Dest]);
return;
case SILInstructionKind::ExplicitCopyAddrInst:
visitor(&I->getAllOperands()[ExplicitCopyAddrInst::Src]);
visitor(&I->getAllOperands()[ExplicitCopyAddrInst::Dest]);
return;
case SILInstructionKind::MarkUnresolvedMoveAddrInst:
visitor(&I->getAllOperands()[MarkUnresolvedMoveAddrInst::Src]);
visitor(&I->getAllOperands()[MarkUnresolvedMoveAddrInst::Dest]);
return;
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::StoreInst:
case SILInstructionKind::StoreBorrowInst:
case SILInstructionKind::PackElementSetInst:
visitor(&I->getAllOperands()[StoreInst::Dest]);
return;
case SILInstructionKind::SelectEnumAddrInst:
visitor(&I->getAllOperands()[0]);
return;
case SILInstructionKind::InitExistentialAddrInst:
case SILInstructionKind::InjectEnumAddrInst:
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Load##Name##Inst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::LoadInst:
case SILInstructionKind::LoadBorrowInst:
case SILInstructionKind::OpenExistentialAddrInst:
case SILInstructionKind::SwitchEnumAddrInst:
case SILInstructionKind::UncheckedTakeEnumDataAddrInst:
case SILInstructionKind::UnconditionalCheckedCastInst:
case SILInstructionKind::PackElementGetInst: {
// Assuming all the above have only a single address operand.
assert(I->getNumOperands() - I->getNumTypeDependentOperands() == 1);
Operand *singleOperand = &I->getAllOperands()[0];
// Check the operand type because UnconditionalCheckedCastInst may operate
// on a non-address.
if (singleOperand->get()->getType().isAddress())
visitor(singleOperand);
return;
}
// Non-access cases: these are marked with memory side effects, but, by
// themselves, do not access formal memory.
#define UNCHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::StrongCopy##Name##ValueInst:
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::StrongCopy##Name##ValueInst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::AbortApplyInst:
case SILInstructionKind::AllocBoxInst:
case SILInstructionKind::AllocExistentialBoxInst:
case SILInstructionKind::AllocGlobalInst:
case SILInstructionKind::BeginAccessInst:
case SILInstructionKind::BeginApplyInst:
case SILInstructionKind::BeginBorrowInst:
case SILInstructionKind::BeginCOWMutationInst:
case SILInstructionKind::EndCOWMutationInst:
case SILInstructionKind::BeginUnpairedAccessInst:
case SILInstructionKind::BindMemoryInst:
case SILInstructionKind::RebindMemoryInst:
case SILInstructionKind::CondFailInst:
case SILInstructionKind::CopyBlockInst:
case SILInstructionKind::CopyBlockWithoutEscapingInst:
case SILInstructionKind::CopyValueInst:
case SILInstructionKind::DebugStepInst:
case SILInstructionKind::DeinitExistentialAddrInst:
case SILInstructionKind::DeinitExistentialValueInst:
case SILInstructionKind::DestroyAddrInst:
case SILInstructionKind::DestroyValueInst:
case SILInstructionKind::DropDeinitInst:
case SILInstructionKind::EndAccessInst:
case SILInstructionKind::EndApplyInst:
case SILInstructionKind::EndBorrowInst:
case SILInstructionKind::EndUnpairedAccessInst:
case SILInstructionKind::EndLifetimeInst:
case SILInstructionKind::ExistentialMetatypeInst:
case SILInstructionKind::FixLifetimeInst:
case SILInstructionKind::GlobalAddrInst:
case SILInstructionKind::HasSymbolInst:
case SILInstructionKind::HopToExecutorInst:
case SILInstructionKind::ExtractExecutorInst:
case SILInstructionKind::InitExistentialValueInst:
case SILInstructionKind::IsUniqueInst:
case SILInstructionKind::IsEscapingClosureInst:
case SILInstructionKind::KeyPathInst:
case SILInstructionKind::OpenExistentialBoxInst:
case SILInstructionKind::OpenExistentialBoxValueInst:
case SILInstructionKind::OpenExistentialValueInst:
case SILInstructionKind::PartialApplyInst:
case SILInstructionKind::YieldInst:
case SILInstructionKind::UnwindInst:
case SILInstructionKind::IncrementProfilerCounterInst:
case SILInstructionKind::UncheckedOwnershipConversionInst:
case SILInstructionKind::UncheckedRefCastAddrInst:
case SILInstructionKind::UnconditionalCheckedCastAddrInst:
case SILInstructionKind::ValueMetatypeInst:
// TODO: Is this correct?
case SILInstructionKind::GetAsyncContinuationInst:
case SILInstructionKind::GetAsyncContinuationAddrInst:
case SILInstructionKind::AwaitAsyncContinuationInst:
return;
}
}
|