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
|
//===--- SILCombinerMiscVisitors.cpp --------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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-combine"
#include "SILCombiner.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/Basic/STLExtras.h"
#include "swift/SIL/BasicBlockBits.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/DynamicCasts.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/NodeBits.h"
#include "swift/SIL/PatternMatch.h"
#include "swift/SIL/Projection.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SILOptimizer/Analysis/ARCAnalysis.h"
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
#include "swift/SILOptimizer/Analysis/ValueTracking.h"
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/Devirtualize.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/CommandLine.h"
using namespace swift;
using namespace swift::PatternMatch;
/// This flag is used to disable alloc stack optimizations to ease testing of
/// other SILCombine optimizations.
static llvm::cl::opt<bool>
DisableAllocStackOpts("sil-combine-disable-alloc-stack-opts",
llvm::cl::init(false));
SILInstruction*
SILCombiner::visitAllocExistentialBoxInst(AllocExistentialBoxInst *AEBI) {
// Optimize away the pattern below that happens when exceptions are created
// and in some cases, due to inlining, are not needed.
//
// %6 = alloc_existential_box $Error, $ColorError
// %6a = project_existential_box %6
// %7 = enum $VendingMachineError, #ColorError.Red
// store %7 to %6a : $*ColorError
// debug_value %6 : $Error
// strong_release %6 : $Error
//
// %6 = alloc_existential_box $Error, $ColorError
// %6a = project_existential_box %6
// %7 = enum $VendingMachineError, #ColorError.Red
// store %7 to [init] %6a : $*ColorError
// debug_value %6 : $Error
// destroy_value %6 : $Error
SILValue boxedValue =
getConcreteValueOfExistentialBox(AEBI, /*ignoreUser*/ nullptr);
if (!boxedValue)
return nullptr;
// Check if the box is destroyed at a single place. That's the end of its
// lifetime.
SILInstruction *singleDestroy = nullptr;
if (hasOwnership()) {
if (auto *use = AEBI->getSingleConsumingUse()) {
singleDestroy = dyn_cast<DestroyValueInst>(use->getUser());
}
} else {
for (Operand *use : AEBI->getUses()) {
auto *user = use->getUser();
if (isa<StrongReleaseInst>(user) || isa<ReleaseValueInst>(user)) {
if (singleDestroy)
return nullptr;
singleDestroy = user;
}
}
}
if (!singleDestroy)
return nullptr;
// Release the value that was stored into the existential box. The box
// is going away so we need to release the stored value.
// NOTE: It's important that the release is inserted at the single
// release of the box and not at the store, because a balancing retain could
// be _after_ the store, e.g:
// %box = alloc_existential_box
// %addr = project_existential_box %box
// store %value to %addr
// retain_value %value // must insert the release after this retain
// strong_release %box
Builder.setInsertionPoint(singleDestroy);
Builder.emitDestroyValueOperation(AEBI->getLoc(), boxedValue);
eraseInstIncludingUsers(AEBI);
return nullptr;
}
/// Return the enum case injected by an inject_enum_addr if it is the only
/// instruction which writes to \p Addr.
static EnumElementDecl *getInjectEnumCaseTo(SILValue Addr) {
while (true) {
// For everything else than an alloc_stack we cannot easily prove that we
// see all writes.
if (!isa<AllocStackInst>(Addr))
return nullptr;
SILInstruction *WritingInst = nullptr;
int NumWrites = 0;
for (auto *Use : getNonDebugUses(Addr)) {
SILInstruction *User = Use->getUser();
switch (User->getKind()) {
// Handle a very narrow set of known not harmful instructions.
case swift::SILInstructionKind::DestroyAddrInst:
case swift::SILInstructionKind::DeallocStackInst:
case swift::SILInstructionKind::SwitchEnumAddrInst:
break;
case swift::SILInstructionKind::ApplyInst:
case swift::SILInstructionKind::TryApplyInst: {
// Check if the addr is only passed to in_guaranteed arguments.
FullApplySite AI(User);
for (Operand &Op : AI.getArgumentOperands()) {
if (Op.get() == Addr &&
AI.getArgumentConvention(Op) !=
SILArgumentConvention::Indirect_In_Guaranteed)
return nullptr;
}
break;
}
case swift::SILInstructionKind::InjectEnumAddrInst:
WritingInst = User;
++NumWrites;
break;
case swift::SILInstructionKind::CopyAddrInst:
if (Addr == cast<CopyAddrInst>(User)->getDest()) {
WritingInst = User;
++NumWrites;
}
break;
default:
return nullptr;
}
}
if (NumWrites != 1)
return nullptr;
if (auto *IEA = dyn_cast<InjectEnumAddrInst>(WritingInst))
return IEA->getElement();
// In case of a copy_addr continue with the source of the copy.
Addr = dyn_cast<CopyAddrInst>(WritingInst)->getSrc();
}
}
SILInstruction *SILCombiner::visitSwitchEnumAddrInst(SwitchEnumAddrInst *SEAI) {
SILValue Addr = SEAI->getOperand();
// Convert switch_enum_addr -> br
//
// If the only thing which writes to the address is an inject_enum_addr. We
// only perform these optimizations when we are not in OSSA since this
// eliminates an edge from the CFG and we want SILCombine in OSSA to never do
// that, so in the future we can invalidate less.
if (!SEAI->getFunction()->hasOwnership()) {
if (EnumElementDecl *EnumCase = getInjectEnumCaseTo(Addr)) {
SILBasicBlock *Dest = SEAI->getCaseDestination(EnumCase);
// If the only instruction which writes to Addr is an inject_enum_addr we
// know that there cannot be an enum payload.
assert(Dest->getNumArguments() == 0 &&
"didn't expect a payload argument");
Builder.createBranch(SEAI->getLoc(), Dest);
return eraseInstFromFunction(*SEAI);
}
}
SILType Ty = Addr->getType();
if (!Ty.isLoadable(*SEAI->getFunction()))
return nullptr;
// Promote switch_enum_addr to switch_enum if the enum is loadable.
// switch_enum_addr %ptr : $*Optional<SomeClass>, case ...
// ->
// %value = load %ptr
// switch_enum %value
//
// If we are using ownership, we perform a load_borrow right before the new
// switch_enum and end the borrow scope right afterwards.
Builder.setCurrentDebugScope(SEAI->getDebugScope());
SmallVector<std::pair<EnumElementDecl *, SILBasicBlock *>, 8> Cases;
for (int i : range(SEAI->getNumCases())) {
Cases.push_back(SEAI->getCase(i));
}
SILBasicBlock *Default = SEAI->hasDefault() ? SEAI->getDefaultBB() : nullptr;
SILValue EnumVal = Builder.emitLoadBorrowOperation(SEAI->getLoc(), Addr);
auto *sei = Builder.createSwitchEnum(SEAI->getLoc(), EnumVal, Default, Cases);
if (Builder.hasOwnership()) {
for (int i : range(sei->getNumCases())) {
auto c = sei->getCase(i);
if (c.first->hasAssociatedValues()) {
auto eltType = Addr->getType().getEnumElementType(
c.first, Builder.getModule(), Builder.getTypeExpansionContext());
eltType = eltType.getObjectType();
sei->createResult(c.second, eltType);
}
Builder.setInsertionPoint(c.second->front().getIterator());
Builder.emitEndBorrowOperation(sei->getLoc(), EnumVal);
}
sei->createDefaultResult();
if (auto defaultBlock = sei->getDefaultBBOrNull()) {
Builder.setInsertionPoint(defaultBlock.get()->front().getIterator());
Builder.emitEndBorrowOperation(sei->getLoc(), EnumVal);
}
}
return eraseInstFromFunction(*SEAI);
}
SILInstruction *SILCombiner::visitSelectEnumAddrInst(SelectEnumAddrInst *seai) {
// Canonicalize a select_enum_addr: if the default refers to exactly one case,
// then replace the default with that case.
Builder.setCurrentDebugScope(seai->getDebugScope());
if (seai->hasDefault()) {
NullablePtr<EnumElementDecl> elementDecl = seai->getUniqueCaseForDefault();
if (elementDecl.isNonNull()) {
// Construct a new instruction by copying all the case entries.
SmallVector<std::pair<EnumElementDecl *, SILValue>, 4> caseValues;
for (int idx = 0, numIdcs = seai->getNumCases(); idx < numIdcs; ++idx) {
caseValues.push_back(seai->getCase(idx));
}
// Add the default-entry of the original instruction as case-entry.
caseValues.push_back(
std::make_pair(elementDecl.get(), seai->getDefaultResult()));
return Builder.createSelectEnumAddr(
seai->getLoc(), seai->getEnumOperand(), seai->getType(), SILValue(),
caseValues);
}
}
// Promote select_enum_addr to select_enum if the enum is loadable.
// = select_enum_addr %ptr : $*Optional<SomeClass>, case ...
// ->
// %value = load %ptr
// = select_enum %value
SILType ty = seai->getEnumOperand()->getType();
if (!ty.isLoadable(*seai->getFunction()))
return nullptr;
SmallVector<std::pair<EnumElementDecl *, SILValue>, 8> cases;
for (int i = 0, e = seai->getNumCases(); i < e; ++i)
cases.push_back(seai->getCase(i));
SILValue defaultCase =
seai->hasDefault() ? seai->getDefaultResult() : SILValue();
auto enumVal =
Builder.emitLoadBorrowOperation(seai->getLoc(), seai->getEnumOperand());
auto *result = Builder.createSelectEnum(seai->getLoc(), enumVal,
seai->getType(), defaultCase, cases);
Builder.emitEndBorrowOperation(seai->getLoc(), enumVal);
replaceInstUsesWith(*seai, result);
return eraseInstFromFunction(*seai);
}
SILInstruction *SILCombiner::visitSwitchValueInst(SwitchValueInst *svi) {
SILValue cond = svi->getOperand();
BuiltinIntegerType *condTy = cond->getType().getAs<BuiltinIntegerType>();
if (!condTy || !condTy->isFixedWidth(1))
return nullptr;
SILBasicBlock *falseBB = nullptr;
SILBasicBlock *trueBB = nullptr;
for (unsigned idx : range(svi->getNumCases())) {
auto switchCase = svi->getCase(idx);
auto *caseVal = dyn_cast<IntegerLiteralInst>(switchCase.first);
if (!caseVal)
return nullptr;
SILBasicBlock *destBB = switchCase.second;
assert(destBB->args_empty() &&
"switch_value case destination cannot take arguments");
if (caseVal->getValue() == 0) {
assert(!falseBB && "double case value 0 in switch_value");
falseBB = destBB;
} else {
assert(!trueBB && "double case value 1 in switch_value");
trueBB = destBB;
}
}
if (svi->hasDefault()) {
assert(svi->getDefaultBB()->args_empty() &&
"switch_value default destination cannot take arguments");
if (!falseBB) {
falseBB = svi->getDefaultBB();
} else if (!trueBB) {
trueBB = svi->getDefaultBB();
}
}
if (!falseBB || !trueBB)
return nullptr;
Builder.setCurrentDebugScope(svi->getDebugScope());
return Builder.createCondBranch(svi->getLoc(), cond, trueBB, falseBB);
}
namespace {
/// A SILInstruction visitor that analyzes alloc stack values for dead live
/// range and promotion opportunities.
///
/// init_existential_addr instructions behave like memory allocation within the
/// allocated object. We can promote the init_existential_addr allocation into a
/// dedicated allocation.
///
/// We detect this pattern
/// %0 = alloc_stack $LogicValue
/// %1 = init_existential_addr %0 : $*LogicValue, $*Bool
/// ...
/// use of %1
/// ...
/// destroy_addr %0 : $*LogicValue
/// dealloc_stack %0 : $*LogicValue
///
/// At the same we time also look for dead alloc_stack live ranges that are only
/// copied into.
///
/// %0 = alloc_stack
/// copy_addr %src, %0
/// destroy_addr %0 : $*LogicValue
/// dealloc_stack %0 : $*LogicValue
struct AllocStackAnalyzer : SILInstructionVisitor<AllocStackAnalyzer> {
/// The alloc_stack that we are analyzing.
AllocStackInst *ASI;
/// Do all of the users of the alloc stack allow us to perform optimizations.
bool LegalUsers = true;
/// If we saw an init_existential_addr in the use list of the alloc_stack,
/// this is the init_existential_addr. We are conservative in the face of
/// having multiple init_existential_addr. In such a case, we say that the use
/// list of the alloc_stack does not allow for optimizations to occur.
InitExistentialAddrInst *IEI = nullptr;
/// If we saw an open_existential_addr in the use list of the alloc_stack,
/// this is the open_existential_addr. We are conservative in the case of
/// multiple open_existential_addr. In such a case, we say that the use list
/// of the alloc_stack does not allow for optimizations to occur.
OpenExistentialAddrInst *OEI = nullptr;
/// Did we see any copies into the alloc stack.
bool HaveSeenCopyInto = false;
public:
AllocStackAnalyzer(AllocStackInst *ASI) : ASI(ASI) {}
/// Analyze the alloc_stack instruction's uses.
void analyze() {
// Scan all of the uses of the AllocStack and check if it is not used for
// anything other than the init_existential_addr/open_existential_addr
// container.
// There is no interesting scenario where a non-copyable type should have
// its allocation eliminated. A destroy_addr cannot be removed because it
// may run the struct-deinit, and the lifetime cannot be shortened. A
// copy_addr [take] [init] cannot be replaced by a destroy_addr because the
// destination may hold a 'discard'ed value, which is never destroyed. This
// analysis assumes memory is deinitialized on all paths, which is not the
// case for discarded values. Eventually copyable types may also be
// discarded; to support that, we will leave a drop_deinit_addr in place.
if (ASI->getType().isMoveOnly(/*orWrapped=*/false)) {
LegalUsers = false;
return;
}
for (auto *Op : getNonDebugUses(ASI)) {
visit(Op->getUser());
// If we found a non-legal user, bail early.
if (!LegalUsers)
break;
}
}
/// Given an unhandled case, we have an illegal use for our optimization
/// purposes. Set LegalUsers to false and return.
void visitSILInstruction(SILInstruction *I) { LegalUsers = false; }
// Destroy and dealloc are both fine.
void visitDestroyAddrInst(DestroyAddrInst *I) {}
void visitDeinitExistentialAddrInst(DeinitExistentialAddrInst *I) {}
void visitDeallocStackInst(DeallocStackInst *I) {}
void visitInitExistentialAddrInst(InitExistentialAddrInst *I) {
// If we have already seen an init_existential_addr, we cannot
// optimize. This is because we only handle the single init_existential_addr
// case.
if (IEI || HaveSeenCopyInto) {
LegalUsers = false;
return;
}
IEI = I;
}
void visitOpenExistentialAddrInst(OpenExistentialAddrInst *I) {
// If we have already seen an open_existential_addr, we cannot
// optimize. This is because we only handle the single open_existential_addr
// case.
if (OEI) {
LegalUsers = false;
return;
}
// Make sure that the open_existential does not have any uses except
// destroy_addr.
for (auto *Use : getNonDebugUses(I)) {
if (!isa<DestroyAddrInst>(Use->getUser())) {
LegalUsers = false;
return;
}
}
OEI = I;
}
void visitCopyAddrInst(CopyAddrInst *I) {
if (IEI) {
LegalUsers = false;
return;
}
// Copies into the alloc_stack live range are safe.
if (I->getDest() == ASI) {
HaveSeenCopyInto = true;
return;
}
LegalUsers = false;
}
};
} // end anonymous namespace
/// Returns true if there is a retain instruction between \p from and the
/// destroy or deallocation of \p alloc.
static bool somethingIsRetained(SILInstruction *from, AllocStackInst *alloc) {
llvm::SmallVector<SILInstruction *, 8> workList;
BasicBlockSet handled(from->getFunction());
workList.push_back(from);
while (!workList.empty()) {
SILInstruction *start = workList.pop_back_val();
for (auto iter = start->getIterator(), end = start->getParent()->end();
iter != end;
++iter) {
SILInstruction *inst = &*iter;
if (isa<RetainValueInst>(inst) || isa<StrongRetainInst>(inst)) {
return true;
}
if ((isa<DeallocStackInst>(inst) || isa<DestroyAddrInst>(inst)) &&
inst->getOperand(0) == alloc) {
break;
}
if (isa<TermInst>(inst)) {
for (SILBasicBlock *succ : start->getParent()->getSuccessors()) {
if (handled.insert(succ))
workList.push_back(&*succ->begin());
}
}
}
}
return false;
}
/// Replaces an alloc_stack of an enum by an alloc_stack of the payload if only
/// one enum case (with payload) is stored to that location.
///
/// For example:
///
/// %loc = alloc_stack $Optional<T>
/// %payload = init_enum_data_addr %loc
/// store %value to %payload
/// ...
/// %take_addr = unchecked_take_enum_data_addr %loc
/// %l = load %take_addr
///
/// is transformed to
///
/// %loc = alloc_stack $T
/// store %value to %loc
/// ...
/// %l = load %loc
bool SILCombiner::optimizeStackAllocatedEnum(AllocStackInst *AS) {
EnumDecl *enumDecl = AS->getType().getEnumOrBoundGenericEnum();
if (!enumDecl)
return false;
EnumElementDecl *element = nullptr;
unsigned numInits =0;
unsigned numTakes = 0;
SILBasicBlock *initBlock = nullptr;
SILBasicBlock *takeBlock = nullptr;
SILType payloadType;
// First step: check if the stack location is only used to hold one specific
// enum case with payload.
for (auto *use : AS->getUses()) {
SILInstruction *user = use->getUser();
switch (user->getKind()) {
case SILInstructionKind::DestroyAddrInst:
case SILInstructionKind::DeallocStackInst:
case SILInstructionKind::InjectEnumAddrInst:
// We'll check init_enum_addr below.
break;
case SILInstructionKind::DebugValueInst:
if (DebugValueInst::hasAddrVal(user))
break;
return false;
case SILInstructionKind::InitEnumDataAddrInst: {
auto *ieda = cast<InitEnumDataAddrInst>(user);
auto *el = ieda->getElement();
if (element && el != element)
return false;
element = el;
assert(!payloadType || payloadType == ieda->getType());
payloadType = ieda->getType();
numInits++;
initBlock = user->getParent();
break;
}
case SILInstructionKind::UncheckedTakeEnumDataAddrInst: {
auto *el = cast<UncheckedTakeEnumDataAddrInst>(user)->getElement();
if (element && el != element)
return false;
element = el;
numTakes++;
takeBlock = user->getParent();
break;
}
default:
return false;
}
}
if (!element || !payloadType)
return false;
// If the enum has a single init-take pair in a single block, we know that
// the enum cannot contain any valid payload outside that init-take pair.
//
// This also means that we can ignore any inject_enum_addr of another enum
// case, because this can only inject a case without a payload.
bool singleInitTakePair =
(numInits == 1 && numTakes == 1 && initBlock == takeBlock);
if (!singleInitTakePair) {
// No single init-take pair: We cannot ignore inject_enum_addrs with a
// mismatching case.
for (auto *use : AS->getUses()) {
if (auto *inject = dyn_cast<InjectEnumAddrInst>(use->getUser())) {
if (inject->getElement() != element)
return false;
}
}
}
// Second step: replace the enum alloc_stack with a payload alloc_stack.
Builder.setCurrentDebugScope(AS->getDebugScope());
auto *newAlloc = Builder.createAllocStack(
AS->getLoc(), payloadType, {}, AS->hasDynamicLifetime(), IsNotLexical,
IsNotFromVarDecl, DoesNotUseMoveableValueDebugInfo, true);
if (auto varInfo = AS->getVarInfo()) {
// TODO: Add support for op_enum_fragment
// For now, we can't represent this variable correctly, so we drop it.
Builder.createDebugValue(AS->getLoc(), SILUndef::get(AS), *varInfo);
}
while (!AS->use_empty()) {
Operand *use = *AS->use_begin();
SILInstruction *user = use->getUser();
switch (user->getKind()) {
case SILInstructionKind::InjectEnumAddrInst:
eraseInstFromFunction(*user);
break;
case SILInstructionKind::DestroyAddrInst:
if (singleInitTakePair) {
// It's not possible that the enum has a payload at the destroy_addr,
// because it must have already been taken by the take of the
// single init-take pair.
// We _have_ to remove the destroy_addr, because we also remove all
// inject_enum_addrs which might inject a payload-less case before
// the destroy_addr.
eraseInstFromFunction(*user);
} else {
// The enum payload can still be valid at the destroy_addr, so we have
// to keep the destroy_addr. Just replace the enum with the payload
// (and because it's not a singleInitTakePair, we can be sure that the
// enum cannot have any other case than the payload case).
use->set(newAlloc);
}
break;
case SILInstructionKind::DeallocStackInst:
use->set(newAlloc);
break;
case SILInstructionKind::InitEnumDataAddrInst:
case SILInstructionKind::UncheckedTakeEnumDataAddrInst: {
auto *svi = cast<SingleValueInstruction>(user);
svi->replaceAllUsesWith(newAlloc);
eraseInstFromFunction(*svi);
break;
}
case SILInstructionKind::DebugValueInst:
// TODO: Add support for op_enum_fragment
use->set(SILUndef::get(AS));
break;
default:
llvm_unreachable("unexpected alloc_stack user");
}
}
return true;
}
SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
if (AS->getFunction()->hasOwnership())
return nullptr;
if (optimizeStackAllocatedEnum(AS))
return nullptr;
// If we are testing SILCombine and we are asked not to eliminate
// alloc_stacks, just return.
if (DisableAllocStackOpts)
return nullptr;
AllocStackAnalyzer Analyzer(AS);
Analyzer.analyze();
// If when analyzing, we found a user that makes our optimization, illegal,
// bail early.
if (!Analyzer.LegalUsers)
return nullptr;
InitExistentialAddrInst *IEI = Analyzer.IEI;
OpenExistentialAddrInst *OEI = Analyzer.OEI;
// If the only users of the alloc_stack are alloc, destroy and
// init_existential_addr then we can promote the allocation of the init
// existential.
// Be careful with open archetypes, because they cannot be moved before
// their definitions.
if (IEI && !OEI &&
!IEI->getLoweredConcreteType().hasOpenedExistential()) {
assert(!IEI->getLoweredConcreteType().isOpenedExistential());
Builder.setCurrentDebugScope(AS->getDebugScope());
auto varInfo = AS->getVarInfo();
if (varInfo) {
if (varInfo->Type == AS->getElementType()) {
varInfo->Type = {}; // Lower the variable's type too.
} else {
// Cannot salvage the variable, its type has changed and its expression
// cannot be rewritten.
Builder.createDebugValue(AS->getLoc(), SILUndef::get(AS), *varInfo);
varInfo = {};
}
}
auto *ConcAlloc = Builder.createAllocStack(
AS->getLoc(), IEI->getLoweredConcreteType(), varInfo);
IEI->replaceAllUsesWith(ConcAlloc);
eraseInstFromFunction(*IEI);
for (auto UI = AS->use_begin(), UE = AS->use_end(); UI != UE;) {
auto *Op = *UI;
++UI;
if (auto *DA = dyn_cast<DestroyAddrInst>(Op->getUser())) {
Builder.setInsertionPoint(DA);
Builder.createDestroyAddr(DA->getLoc(), ConcAlloc);
eraseInstFromFunction(*DA);
continue;
}
if (isa<DeinitExistentialAddrInst>(Op->getUser())) {
eraseInstFromFunction(*Op->getUser());
continue;
}
if (!isa<DeallocStackInst>(Op->getUser()))
continue;
auto *DS = cast<DeallocStackInst>(Op->getUser());
Builder.setInsertionPoint(DS);
Builder.createDeallocStack(DS->getLoc(), ConcAlloc);
eraseInstFromFunction(*DS);
}
return eraseInstFromFunction(*AS);
}
// If we have a live 'live range' or a live range that we have not sen a copy
// into, bail.
if (!Analyzer.HaveSeenCopyInto || IEI)
return nullptr;
// Otherwise remove the dead live range that is only copied into.
//
// TODO: Do we not remove purely dead live ranges here? Seems like we should.
SmallPtrSet<SILInstruction *, 16> ToDelete;
SmallVector<CopyAddrInst *, 4> takingCopies;
for (auto *Op : AS->getUses()) {
// Replace a copy_addr [take] %src ... by a destroy_addr %src if %src is
// no the alloc_stack.
// Otherwise, just delete the copy_addr.
if (auto *CopyAddr = dyn_cast<CopyAddrInst>(Op->getUser())) {
if (CopyAddr->isTakeOfSrc() && CopyAddr->getSrc() != AS) {
takingCopies.push_back(CopyAddr);
}
}
if (auto *OEAI = dyn_cast<OpenExistentialAddrInst>(Op->getUser())) {
for (auto *Op : OEAI->getUses()) {
assert(isa<DestroyAddrInst>(Op->getUser()) ||
Op->getUser()->isDebugInstruction() && "Unexpected instruction");
ToDelete.insert(Op->getUser());
}
}
assert(isa<CopyAddrInst>(Op->getUser()) ||
isa<OpenExistentialAddrInst>(Op->getUser()) ||
isa<DestroyAddrInst>(Op->getUser()) ||
isa<DeallocStackInst>(Op->getUser()) ||
isa<DeinitExistentialAddrInst>(Op->getUser()) ||
Op->getUser()->isDebugInstruction() && "Unexpected instruction");
ToDelete.insert(Op->getUser());
}
// Check if a retain is moved after the copy_addr. If the retained object
// happens to be the source of the copy_addr it might be only kept alive by
// the stack location. This cannot happen with OSSA.
// TODO: remove this check once we have OSSA.
for (CopyAddrInst *copy : takingCopies) {
if (somethingIsRetained(copy, AS))
return nullptr;
}
for (CopyAddrInst *copy : takingCopies) {
SILBuilderWithScope destroyBuilder(copy, Builder.getBuilderContext());
destroyBuilder.createDestroyAddr(copy->getLoc(), copy->getSrc());
}
// Erase the 'live-range'
for (auto *Inst : ToDelete) {
Inst->replaceAllUsesOfAllResultsWithUndef();
eraseInstFromFunction(*Inst);
}
return eraseInstFromFunction(*AS);
}
/// Returns the base address if \p val is an index_addr with constant index.
static SILValue isConstIndexAddr(SILValue val, unsigned &index) {
auto *IA = dyn_cast<IndexAddrInst>(val);
if (!IA)
return nullptr;
auto *Index = dyn_cast<IntegerLiteralInst>(IA->getIndex());
// Limiting to 32 bits is more than enough. The reason why not limiting to 64
// bits is to leave room for overflow when we add two indices.
if (!Index || Index->getValue().getActiveBits() > 32)
return nullptr;
index = Index->getValue().getZExtValue();
return IA->getBase();
}
SILInstruction *SILCombiner::visitLoadBorrowInst(LoadBorrowInst *lbi) {
// If we have a load_borrow that only has non_debug end_borrow uses, delete
// it.
if (llvm::all_of(getNonDebugUses(lbi), [](Operand *use) {
return isa<EndBorrowInst>(use->getUser());
})) {
eraseInstIncludingUsers(lbi);
return nullptr;
}
return nullptr;
}
/// Optimize nested index_addr instructions:
/// Example in SIL pseudo code:
/// %1 = index_addr %ptr, x
/// %2 = index_addr %1, y
/// ->
/// %2 = index_addr %ptr, x+y
SILInstruction *SILCombiner::visitIndexAddrInst(IndexAddrInst *IA) {
unsigned index = 0;
SILValue base = isConstIndexAddr(IA, index);
if (!base)
return nullptr;
unsigned index2 = 0;
SILValue base2 = isConstIndexAddr(base, index2);
if (!base2)
return nullptr;
auto *newIndex = Builder.createIntegerLiteral(IA->getLoc(),
IA->getIndex()->getType(), index + index2);
return Builder.createIndexAddr(IA->getLoc(), base2, newIndex,
IA->needsStackProtection() || cast<IndexAddrInst>(base)->needsStackProtection());
}
SILInstruction *SILCombiner::visitCondFailInst(CondFailInst *CFI) {
// Remove runtime asserts such as overflow checks and bounds checks.
if (RemoveCondFails)
return eraseInstFromFunction(*CFI);
auto *I = dyn_cast<IntegerLiteralInst>(CFI->getOperand());
if (!I)
return nullptr;
// Erase. (cond_fail 0)
if (!I->getValue().getBoolValue())
return eraseInstFromFunction(*CFI);
// Remove non-lifetime-ending code that follows a (cond_fail 1) and set the
// block's terminator to unreachable.
// Are there instructions after this point to delete?
// First check if the next instruction is unreachable.
if (isa<UnreachableInst>(std::next(SILBasicBlock::iterator(CFI))))
return nullptr;
// Otherwise, check if the only instructions are unreachables and destroys of
// lexical values.
// Collect all instructions and, in OSSA, the values they define.
llvm::SmallVector<SILInstruction *, 32> ToRemove;
ValueSet DefinedValues(CFI->getFunction());
for (auto Iter = std::next(CFI->getIterator());
Iter != CFI->getParent()->end(); ++Iter) {
if (isBeginScopeMarker(&*Iter)) {
for (auto *scopeUse : cast<SingleValueInstruction>(&*Iter)->getUses()) {
auto *scopeEnd = scopeUse->getUser();
if (isEndOfScopeMarker(scopeEnd)) {
ToRemove.push_back(scopeEnd);
}
}
}
if (!CFI->getFunction()->hasOwnership()) {
ToRemove.push_back(&*Iter);
continue;
}
for (auto result : Iter->getResults()) {
DefinedValues.insert(result);
}
// Look for destroys of lexical values whose def isn't after the cond_fail.
if (auto *dvi = dyn_cast<DestroyValueInst>(&*Iter)) {
auto value = dvi->getOperand();
if (!DefinedValues.contains(value) && value->isLexical())
continue;
}
ToRemove.push_back(&*Iter);
}
unsigned instructionsToDelete = ToRemove.size();
// If the last instruction is an unreachable already, it needn't be deleted.
if (isa<UnreachableInst>(ToRemove.back())) {
--instructionsToDelete;
}
if (instructionsToDelete == 0)
return nullptr;
for (auto *Inst : ToRemove) {
if (Inst->isDeleted())
continue;
// Replace any still-remaining uses with undef and erase.
Inst->replaceAllUsesOfAllResultsWithUndef();
eraseInstFromFunction(*Inst);
}
// Add an `unreachable` to be the new terminator for this block
Builder.setInsertionPoint(CFI->getParent());
Builder.createUnreachable(ArtificialUnreachableLocation());
return nullptr;
}
/// Create a value from stores to an address.
///
/// If there are only stores to \p addr, return the stored value. Also, if there
/// are address projections, create aggregate instructions for it.
/// If builder is null, it's just a dry-run to check if it's possible.
static SILValue createValueFromAddr(SILValue addr, SILBuilder *builder,
SILLocation loc) {
SmallVector<SILValue, 4> elems;
enum Kind {
none, store, tuple
} kind = none;
for (Operand *use : addr->getUses()) {
SILInstruction *user = use->getUser();
if (user->isDebugInstruction())
continue;
auto *st = dyn_cast<StoreInst>(user);
if (st && kind == none && st->getDest() == addr) {
elems.push_back(st->getSrc());
kind = store;
// We cannot just return st->getSrc() here because we also have to check
// if the store destination is the only use of addr.
continue;
}
if (auto *telem = dyn_cast<TupleElementAddrInst>(user)) {
if (kind == none) {
elems.resize(addr->getType().castTo<TupleType>()->getNumElements());
kind = tuple;
}
if (kind == tuple) {
if (elems[telem->getFieldIndex()])
return SILValue();
elems[telem->getFieldIndex()] = createValueFromAddr(telem, builder, loc);
continue;
}
}
// TODO: handle StructElementAddrInst to create structs.
return SILValue();
}
switch (kind) {
case none:
return SILValue();
case store:
assert(elems.size() == 1);
return elems[0];
case tuple:
if (std::any_of(elems.begin(), elems.end(),
[](SILValue v){ return !(bool)v; }))
return SILValue();
if (builder) {
return builder->createTuple(loc, addr->getType().getObjectType(), elems);
}
// Just return anything not null for the dry-run.
return elems[0];
}
llvm_unreachable("invalid kind");
}
/// Simplify the following two frontend patterns:
///
/// %payload_addr = init_enum_data_addr %payload_allocation
/// store %payload to %payload_addr
/// inject_enum_addr %payload_allocation, $EnumType.case
///
/// inject_enum_addr %nopayload_allocation, $EnumType.case
///
/// for a concrete enum type $EnumType.case to:
///
/// %1 = enum $EnumType, $EnumType.case, %payload
/// store %1 to %payload_addr
///
/// %1 = enum $EnumType, $EnumType.case
/// store %1 to %nopayload_addr
///
/// We leave the cleaning up to mem2reg.
SILInstruction *
SILCombiner::visitInjectEnumAddrInst(InjectEnumAddrInst *IEAI) {
if (IEAI->getFunction()->hasOwnership())
return nullptr;
// Disable this for empty typle type because empty tuple stack locations maybe
// uninitialized. And converting to value form loses tag information.
if (IEAI->getElement()->hasAssociatedValues()) {
SILType elemType = IEAI->getOperand()->getType().getEnumElementType(
IEAI->getElement(), IEAI->getFunction());
if (elemType.isEmpty(*IEAI->getFunction())) {
return nullptr;
}
}
// Given an inject_enum_addr of a concrete type without payload, promote it to
// a store of an enum. Mem2reg/load forwarding will clean things up for us. We
// can't handle the payload case here due to the flow problems caused by the
// dependency in between the enum and its data.
assert(IEAI->getOperand()->getType().isAddress() && "Must be an address");
Builder.setCurrentDebugScope(IEAI->getDebugScope());
if (IEAI->getOperand()->getType().isAddressOnly(*IEAI->getFunction())) {
// Check for the following pattern inside the current basic block:
// inject_enum_addr %payload_allocation, $EnumType.case1
// ... no insns storing anything into %payload_allocation
// select_enum_addr %payload_allocation,
// case $EnumType.case1: %Result1,
// case case $EnumType.case2: %bResult2
// ...
//
// Replace the select_enum_addr by %Result1
auto *Term = IEAI->getParent()->getTerminator();
if (isa<CondBranchInst>(Term) || isa<SwitchValueInst>(Term)) {
auto BeforeTerm = std::prev(std::prev(IEAI->getParent()->end()));
auto *SEAI = dyn_cast<SelectEnumAddrInst>(BeforeTerm);
if (!SEAI)
return nullptr;
if (SEAI->getOperand() != IEAI->getOperand())
return nullptr;
SILBasicBlock::iterator II = IEAI->getIterator();
StoreInst *SI = nullptr;
for (;;) {
SILInstruction *CI = &*II;
if (CI == SEAI)
break;
++II;
SI = dyn_cast<StoreInst>(CI);
if (SI) {
if (SI->getDest() == IEAI->getOperand())
return nullptr;
}
// Allow all instructions in between, which don't have any dependency to
// the store.
if (AA->mayWriteToMemory(&*II, IEAI->getOperand()))
return nullptr;
}
auto *InjectedEnumElement = IEAI->getElement();
auto Result = SEAI->getCaseResult(InjectedEnumElement);
// Replace select_enum_addr by the result
replaceInstUsesWith(*SEAI, Result);
return nullptr;
}
// Check for the following pattern inside the current basic block:
// inject_enum_addr %payload_allocation, $EnumType.case1
// ... no insns storing anything into %payload_allocation
// switch_enum_addr %payload_allocation,
// case $EnumType.case1: %bbX,
// case case $EnumType.case2: %bbY
// ...
//
// Replace the switch_enum_addr by select_enum_addr, switch_value.
if (auto *SEI = dyn_cast<SwitchEnumAddrInst>(Term)) {
if (SEI->getOperand() != IEAI->getOperand())
return nullptr;
SILBasicBlock::iterator II = IEAI->getIterator();
StoreInst *SI = nullptr;
for (;;) {
SILInstruction *CI = &*II;
if (CI == SEI)
break;
++II;
SI = dyn_cast<StoreInst>(CI);
if (SI) {
if (SI->getDest() == IEAI->getOperand())
return nullptr;
}
// Allow all instructions in between, which don't have any dependency to
// the store.
if (AA->mayWriteToMemory(&*II, IEAI->getOperand()))
return nullptr;
}
// Replace switch_enum_addr by a branch instruction.
SILBuilderWithScope B(SEI);
SmallVector<std::pair<EnumElementDecl *, SILValue>, 8> CaseValues;
SmallVector<std::pair<SILValue, SILBasicBlock *>, 8> CaseBBs;
auto IntTy = SILType::getBuiltinIntegerType(32, B.getASTContext());
for (int i = 0, e = SEI->getNumCases(); i < e; ++i) {
auto Pair = SEI->getCase(i);
auto *IL = B.createIntegerLiteral(SEI->getLoc(), IntTy, APInt(32, i, false));
SILValue ILValue = SILValue(IL);
CaseValues.push_back(std::make_pair(Pair.first, ILValue));
CaseBBs.push_back(std::make_pair(ILValue, Pair.second));
}
SILValue DefaultValue;
SILBasicBlock *DefaultBB = nullptr;
if (SEI->hasDefault()) {
auto *IL = B.createIntegerLiteral(
SEI->getLoc(), IntTy,
APInt(32, static_cast<uint64_t>(SEI->getNumCases()), false));
DefaultValue = SILValue(IL);
DefaultBB = SEI->getDefaultBB();
}
auto *SEAI = B.createSelectEnumAddr(SEI->getLoc(), SEI->getOperand(), IntTy, DefaultValue, CaseValues);
B.createSwitchValue(SEI->getLoc(), SILValue(SEAI), DefaultBB, CaseBBs);
return eraseInstFromFunction(*SEI);
}
return nullptr;
}
// If the enum does not have a payload create the enum/store since we don't
// need to worry about payloads.
if (!IEAI->getElement()->hasAssociatedValues()) {
EnumInst *E =
Builder.createEnum(IEAI->getLoc(), SILValue(), IEAI->getElement(),
IEAI->getOperand()->getType().getObjectType());
Builder.createStore(IEAI->getLoc(), E, IEAI->getOperand(),
StoreOwnershipQualifier::Unqualified);
return eraseInstFromFunction(*IEAI);
}
// Ok, we have a payload enum, make sure that we have a store previous to
// us...
SILValue ASO = IEAI->getOperand();
if (!isa<AllocStackInst>(ASO)) {
return nullptr;
}
InitEnumDataAddrInst *DataAddrInst = nullptr;
InjectEnumAddrInst *EnumAddrIns = nullptr;
InstructionSetWithSize WriteSet(IEAI->getFunction());
for (auto UsersIt : ASO->getUses()) {
SILInstruction *CurrUser = UsersIt->getUser();
if (CurrUser->isDeallocatingStack()) {
// we don't care about the dealloc stack instructions
continue;
}
if (CurrUser->isDebugInstruction() || isa<LoadInst>(CurrUser)) {
// These Instructions are a non-risky use we can ignore
continue;
}
if (auto *CurrInst = dyn_cast<InitEnumDataAddrInst>(CurrUser)) {
if (DataAddrInst) {
return nullptr;
}
DataAddrInst = CurrInst;
continue;
}
if (auto *CurrInst = dyn_cast<InjectEnumAddrInst>(CurrUser)) {
if (EnumAddrIns) {
return nullptr;
}
EnumAddrIns = CurrInst;
continue;
}
if (isa<StoreInst>(CurrUser)) {
// The only MayWrite Instruction we can safely handle
WriteSet.insert(CurrUser);
continue;
}
// It is too risky to continue if it is any other instruction.
return nullptr;
}
if (!DataAddrInst || !EnumAddrIns) {
return nullptr;
}
assert((EnumAddrIns == IEAI) &&
"Found InitEnumDataAddrInst differs from IEAI");
// Make sure the enum pattern instructions are the only ones which write to
// this location
if (!WriteSet.empty()) {
// Analyze the instructions (implicit dominator analysis)
// If we find any of MayWriteSet, return nullptr
SILBasicBlock *InitEnumBB = DataAddrInst->getParent();
assert(InitEnumBB && "DataAddrInst is not in a valid Basic Block");
llvm::SmallVector<SILInstruction *, 64> Worklist;
Worklist.push_back(IEAI);
BasicBlockSet Preds(InitEnumBB->getParent());
Preds.insert(IEAI->getParent());
while (!Worklist.empty()) {
SILInstruction *CurrIns = Worklist.pop_back_val();
SILBasicBlock *CurrBB = CurrIns->getParent();
if (CurrBB->isEntry() && CurrBB != InitEnumBB) {
// reached prologue without encountering the init bb
return nullptr;
}
for (auto InsIt = ++CurrIns->getIterator().getReverse();
InsIt != CurrBB->rend(); ++InsIt) {
SILInstruction *Ins = &*InsIt;
if (Ins == DataAddrInst) {
// don't care about what comes before init enum in the basic block
break;
}
if (WriteSet.contains(Ins) != 0) {
return nullptr;
}
}
if (CurrBB == InitEnumBB) {
continue;
}
// Go to predecessors and do all that again
for (SILBasicBlock *Pred : CurrBB->getPredecessorBlocks()) {
// If it's already in the set, then we've already queued and/or
// processed the predecessors.
if (Preds.insert(Pred)) {
Worklist.push_back(&*Pred->rbegin());
}
}
}
}
// Check if we can replace all stores to the enum data with an enum of the
// stored value. We can also handle tuples as payloads, e.g.
//
// %payload_addr = init_enum_data_addr %enum_addr
// %elem0_addr = tuple_element_addr %payload_addr, 0
// %elem1_addr = tuple_element_addr %payload_addr, 1
// store %payload0 to %elem0_addr
// store %payload1 to %elem1_addr
// inject_enum_addr %enum_addr, $EnumType.case
//
if (createValueFromAddr(DataAddrInst, nullptr, DataAddrInst->getLoc())) {
SILValue en =
createValueFromAddr(DataAddrInst, &Builder, DataAddrInst->getLoc());
assert(en);
// In that case, create the payload enum/store.
EnumInst *E = Builder.createEnum(
DataAddrInst->getLoc(), en, DataAddrInst->getElement(),
DataAddrInst->getOperand()->getType().getObjectType());
Builder.createStore(DataAddrInst->getLoc(), E, DataAddrInst->getOperand(),
StoreOwnershipQualifier::Unqualified);
// Cleanup.
getInstModCallbacks().notifyWillBeDeleted(DataAddrInst);
deleter.forceDeleteWithUsers(DataAddrInst);
deleter.forceDelete(IEAI);
deleter.cleanupDeadInstructions();
return nullptr;
}
// Check whether we have an apply initializing the enum.
// %iedai = init_enum_data_addr %enum_addr
// = apply(%iedai,...)
// inject_enum_addr %enum_addr
//
// We can localize the store to an alloc_stack.
// Allowing us to perform the same optimization as for the store.
//
// %alloca = alloc_stack
// apply(%alloca,...)
// %load = load %alloca
// %1 = enum $EnumType, $EnumType.case, %load
// store %1 to %nopayload_addr
//
auto *AI = dyn_cast_or_null<ApplyInst>(getSingleNonDebugUser(DataAddrInst));
if (!AI)
return nullptr;
unsigned ArgIdx = 0;
Operand *EnumInitOperand = nullptr;
for (auto &Opd : AI->getArgumentOperands()) {
// Found an apply that initializes the enum. We can optimize this by
// localizing the initialization to an alloc_stack and loading from it.
DataAddrInst = dyn_cast<InitEnumDataAddrInst>(Opd.get());
if (DataAddrInst && DataAddrInst->getOperand() == IEAI->getOperand()
&& ArgIdx < AI->getSubstCalleeConv().getNumIndirectSILResults()) {
EnumInitOperand = &Opd;
break;
}
++ArgIdx;
}
if (!EnumInitOperand) {
return nullptr;
}
SILType elemType = IEAI->getOperand()->getType().getEnumElementType(
IEAI->getElement(), IEAI->getFunction());
auto *structDecl = elemType.getStructOrBoundGenericStruct();
// We cannot create a struct when it has unreferenceable storage.
if (elemType.isEmpty(*IEAI->getFunction()) && structDecl &&
findUnreferenceableStorage(structDecl, elemType, IEAI->getFunction())) {
return nullptr;
}
// Localize the address access.
Builder.setInsertionPoint(AI);
auto *AllocStack = Builder.createAllocStack(DataAddrInst->getLoc(),
EnumInitOperand->get()->getType());
EnumInitOperand->set(AllocStack);
Builder.setInsertionPoint(std::next(SILBasicBlock::iterator(AI)));
SILValue enumValue;
// If it is an empty type, apply may not initialize it.
// Create an empty value of the empty type and store it to a new local.
if (elemType.isEmpty(*IEAI->getFunction())) {
enumValue = createEmptyAndUndefValue(
elemType.getObjectType(), &*Builder.getInsertionPoint(),
Builder.getBuilderContext(), /*noUndef*/ true);
} else {
enumValue = Builder.createLoad(DataAddrInst->getLoc(), AllocStack,
LoadOwnershipQualifier::Unqualified);
}
EnumInst *E = Builder.createEnum(
DataAddrInst->getLoc(), enumValue, DataAddrInst->getElement(),
DataAddrInst->getOperand()->getType().getObjectType());
Builder.createStore(DataAddrInst->getLoc(), E, DataAddrInst->getOperand(),
StoreOwnershipQualifier::Unqualified);
Builder.createDeallocStack(DataAddrInst->getLoc(), AllocStack);
eraseInstFromFunction(*DataAddrInst);
return eraseInstFromFunction(*IEAI);
}
SILInstruction *
SILCombiner::
visitUnreachableInst(UnreachableInst *UI) {
// Make sure that this unreachable instruction
// is the last instruction in the basic block.
if (UI->getParent()->getTerminator() == UI)
return nullptr;
// Collect together all the instructions after this point
llvm::SmallVector<SILInstruction *, 32> ToRemove;
for (auto Inst = UI->getParent()->rbegin(); &*Inst != UI; ++Inst)
ToRemove.push_back(&*Inst);
for (auto *Inst : ToRemove) {
// Replace any still-remaining uses with undef values and erase.
Inst->replaceAllUsesOfAllResultsWithUndef();
eraseInstFromFunction(*Inst);
}
return nullptr;
}
/// We really want to eliminate unchecked_take_enum_data_addr. Thus if we find
/// one go through all of its uses and see if they are all loads and address
/// projections (in many common situations this is true). If so, perform:
///
/// (load (unchecked_take_enum_data_addr x)) -> (unchecked_enum_data (load x))
///
/// FIXME: Implement this for address projections.
///
/// Also remove dead unchecked_take_enum_data_addr:
/// (destroy_addr (unchecked_take_enum_data_addr x)) -> (destroy_addr x)
SILInstruction *SILCombiner::visitUncheckedTakeEnumDataAddrInst(
UncheckedTakeEnumDataAddrInst *tedai) {
// If our TEDAI has no users, there is nothing to do.
if (tedai->use_empty())
return nullptr;
bool onlyLoads = true;
bool onlyDestroys = true;
for (auto U : getNonDebugUses(tedai)) {
// Check if it is load. If it is not a load, bail...
if (!isa<LoadInst>(U->getUser()) && !isa<LoadBorrowInst>(U->getUser()))
onlyLoads = false;
// If we have a load_borrow, perform an additional check that we do not have
// any reborrow uses. We do not handle reborrows in this optimization.
if (auto *lbi = dyn_cast<LoadBorrowInst>(U->getUser())) {
// false if any consuming use is not an end_borrow.
for (auto *use : lbi->getConsumingUses()) {
if (!isa<EndBorrowInst>(use->getUser())) {
onlyLoads = false;
break;
}
}
}
if (!isa<DestroyAddrInst>(U->getUser()))
onlyDestroys = false;
}
if (onlyDestroys) {
// The unchecked_take_enum_data_addr is dead: remove it and replace all
// destroys with a destroy of its operand.
while (!tedai->use_empty()) {
Operand *use = *tedai->use_begin();
SILInstruction *user = use->getUser();
if (auto *dai = dyn_cast<DestroyAddrInst>(user)) {
dai->setOperand(tedai->getOperand());
} else {
assert(user->isDebugInstruction());
eraseInstFromFunction(*user);
}
}
return eraseInstFromFunction(*tedai);
}
if (!onlyLoads)
return nullptr;
// If our enum type is address only, we cannot do anything here. The key
// thing to remember is that an enum is address only if any of its cases are
// address only. So we *could* have a loadable payload resulting from the
// TEDAI without the TEDAI being loadable itself.
if (tedai->getOperand()->getType().isAddressOnly(*tedai->getFunction()))
return nullptr;
// Grab the EnumAddr.
SILLocation loc = tedai->getLoc();
Builder.setCurrentDebugScope(tedai->getDebugScope());
SILValue enumAddr = tedai->getOperand();
EnumElementDecl *enumElt = tedai->getElement();
SILType payloadType = tedai->getType().getObjectType();
// Go back through a second time now that we know all of our users are
// loads. Perform the transformation on each load at the load's use site. The
// reason that we have to do this is that otherwise we would be hoisting the
// loads causing us to need to consider additional ARC issues.
while (!tedai->use_empty()) {
auto *use = *tedai->use_begin();
auto *user = use->getUser();
// Delete debug insts.
if (user->isDebugInstruction()) {
eraseInstFromFunction(*user);
continue;
}
// Insert a new Load of the enum and extract the data from that.
//
// NOTE: This is potentially hoisting the load, so we need to insert
// compensating destroys.
auto *svi = cast<SingleValueInstruction>(user);
SILValue newValue;
if (auto *oldLoad = dyn_cast<LoadInst>(svi)) {
SILBuilderWithScope localBuilder(oldLoad, Builder);
// If the old load is trivial and our enum addr is non-trivial, we need to
// use a load_borrow here. We know that the unchecked_enum_data will
// produce a trivial value meaning that we can just do a
// load_borrow/immediately end the lifetime here.
if (oldLoad->getOwnershipQualifier() == LoadOwnershipQualifier::Trivial &&
!enumAddr->getType().isTrivial(Builder.getFunction())) {
localBuilder.emitScopedBorrowOperation(
loc, enumAddr, [&](SILValue newLoad) {
newValue = localBuilder.createUncheckedEnumData(
loc, newLoad, enumElt, payloadType);
});
} else {
auto newLoad = localBuilder.emitLoadValueOperation(
loc, enumAddr, oldLoad->getOwnershipQualifier());
newValue = localBuilder.createUncheckedEnumData(loc, newLoad, enumElt,
payloadType);
}
} else if (auto *lbi = cast<LoadBorrowInst>(svi)) {
SILBuilderWithScope localBuilder(lbi, Builder);
auto newLoad = localBuilder.emitLoadBorrowOperation(loc, enumAddr);
for (auto ui = lbi->consuming_use_begin(), ue = lbi->consuming_use_end();
ui != ue; ui = lbi->consuming_use_begin()) {
// We already checked that all of our uses here are end_borrow above.
assert(isa<EndBorrowInst>(ui->getUser()) &&
"Expected only end_borrow consuming uses");
ui->set(newLoad);
}
// Any lifetime ending uses of our original load_borrow have been
// rewritten by the previous loop to be on the new load_borrow. The reason
// that we must do this is end_borrows only are placed on borrow
// introducing guaranteed values and our unchecked_enum_data (unlike the
// old load_borrow of the same type) is not one.
newValue = localBuilder.createUncheckedEnumData(loc, newLoad, enumElt,
payloadType);
}
assert(newValue);
// Replace all uses of the old load with the newValue and erase the old
// load.
replaceInstUsesWith(*svi, newValue);
eraseInstFromFunction(*svi);
}
return eraseInstFromFunction(*tedai);
}
SILInstruction *SILCombiner::visitCondBranchInst(CondBranchInst *CBI) {
// NOTE: All of the following optimizations do invalidates branches by
// replacing the branches, but do not modify the underlying CFG properties
// such as dominance and reachability.
// cond_br(xor(x, 1)), t_label, f_label -> cond_br x, f_label, t_label
// cond_br(x == 0), t_label, f_label -> cond_br x, f_label, t_label
// cond_br(x != 1), t_label, f_label -> cond_br x, f_label, t_label
SILValue X;
if (match(CBI->getCondition(),
m_CombineOr(
// xor(x, 1)
m_ApplyInst(BuiltinValueKind::Xor, m_SILValue(X), m_One()),
// xor(1,x)
m_ApplyInst(BuiltinValueKind::Xor, m_One(), m_SILValue(X)),
// x == 0
m_ApplyInst(BuiltinValueKind::ICMP_EQ, m_SILValue(X), m_Zero()),
// x != 1
m_ApplyInst(BuiltinValueKind::ICMP_NE, m_SILValue(X),
m_One()))) &&
X->getType() ==
SILType::getBuiltinIntegerType(1, CBI->getModule().getASTContext())) {
SmallVector<SILValue, 4> OrigTrueArgs, OrigFalseArgs;
for (const auto Op : CBI->getTrueArgs())
OrigTrueArgs.push_back(Op);
for (const auto Op : CBI->getFalseArgs())
OrigFalseArgs.push_back(Op);
return Builder.createCondBranch(CBI->getLoc(), X,
CBI->getFalseBB(), OrigFalseArgs,
CBI->getTrueBB(), OrigTrueArgs);
}
// cond_br (select_enum) -> switch_enum
// This pattern often occurs as a result of using optionals.
if (auto *SEI = dyn_cast<SelectEnumInst>(CBI->getCondition())) {
// No bb args should be passed
if (!CBI->getTrueArgs().empty() || !CBI->getFalseArgs().empty())
return nullptr;
auto EnumOperandTy = SEI->getEnumOperand()->getType();
// Type should be loadable and copyable.
// TODO: Generalize to work without copying address-only or noncopyable
// values.
if (!EnumOperandTy.isLoadable(*SEI->getFunction())) {
return nullptr;
}
if (EnumOperandTy.isMoveOnly()) {
return nullptr;
}
// Result of the select_enum should be a boolean.
if (SEI->getType() != CBI->getCondition()->getType())
return nullptr;
// If any of cond_br edges are critical edges, do not perform
// the transformation, as SIL in canonical form may
// only have critical edges that are originating from cond_br
// instructions.
if (!CBI->getTrueBB()->getSinglePredecessorBlock())
return nullptr;
if (!CBI->getFalseBB()->getSinglePredecessorBlock())
return nullptr;
SILBasicBlock *DefaultBB = nullptr;
match_integer<0> Zero;
if (SEI->hasDefault()) {
// Default result should be an integer constant.
if (!isa<IntegerLiteralInst>(SEI->getDefaultResult()))
return nullptr;
bool isFalse = match(SEI->getDefaultResult(), Zero);
// Pick the default BB.
DefaultBB = isFalse ? CBI->getFalseBB() : CBI->getTrueBB();
}
if (!DefaultBB) {
// Find the targets for the majority of cases and pick it
// as a default BB.
unsigned TrueBBCases = 0;
unsigned FalseBBCases = 0;
for (int i = 0, e = SEI->getNumCases(); i < e; ++i) {
auto Pair = SEI->getCase(i);
if (isa<IntegerLiteralInst>(Pair.second)) {
bool isFalse = match(Pair.second, Zero);
if (!isFalse) {
++TrueBBCases;
} else {
++FalseBBCases;
}
continue;
}
return nullptr;
}
if (FalseBBCases > TrueBBCases)
DefaultBB = CBI->getFalseBB();
else
DefaultBB = CBI->getTrueBB();
}
assert(DefaultBB && "Default should be defined at this point");
unsigned NumTrueBBCases = 0;
unsigned NumFalseBBCases = 0;
if (DefaultBB == CBI->getFalseBB())
++NumFalseBBCases;
else
++NumTrueBBCases;
// We can now convert cond_br(select_enum) into switch_enum.
SmallVector<std::pair<EnumElementDecl *, SILBasicBlock *>, 8> Cases;
for (int i = 0, e = SEI->getNumCases(); i < e; ++i) {
auto Pair = SEI->getCase(i);
// Bail if one of the results is not an integer constant.
if (!isa<IntegerLiteralInst>(Pair.second))
return nullptr;
// Add a switch case.
bool isFalse = match(Pair.second, Zero);
if (!isFalse && DefaultBB != CBI->getTrueBB()) {
Cases.push_back(std::make_pair(Pair.first, CBI->getTrueBB()));
++NumTrueBBCases;
}
if (isFalse && DefaultBB != CBI->getFalseBB()) {
Cases.push_back(std::make_pair(Pair.first, CBI->getFalseBB()));
++NumFalseBBCases;
}
}
// Bail if a switch_enum would introduce a critical edge.
if (NumTrueBBCases > 1 || NumFalseBBCases > 1)
return nullptr;
if (!hasOwnership()) {
return Builder.createSwitchEnum(SEI->getLoc(), SEI->getEnumOperand(),
DefaultBB, Cases);
}
// If we do have ownership, we need to do significantly more
// work. Specifically:
//
// 1. Our select_enum may not be right next to our cond_br, so we need to
// lifetime extend our enum parameter to our switch_enum.
//
// 2. A switch_enum needs to propagate its operands into destination block
// arguments. We need to create those.
//
// 3. In each destination block, we need to create an argument and end the
// lifetime of that argument.
SILValue selectEnumOperand = SEI->getEnumOperand();
SILValue switchEnumOperand = selectEnumOperand;
if (selectEnumOperand->getOwnershipKind() != OwnershipKind::None) {
switchEnumOperand =
makeCopiedValueAvailable(selectEnumOperand, Builder.getInsertionBB());
}
auto *switchEnum = Builder.createSwitchEnum(
SEI->getLoc(), switchEnumOperand, DefaultBB, Cases);
auto enumOperandType = SEI->getEnumOperand()->getType();
for (auto pair : Cases) {
// We only need to create the phi argument if our case doesn't have an
// associated value.
auto *enumEltDecl = pair.first;
if (!enumEltDecl->hasAssociatedValues())
continue;
auto *block = pair.second;
auto enumEltType =
enumOperandType.getEnumElementType(enumEltDecl, block->getParent());
auto *arg = switchEnum->createResult(block, enumEltType);
SILBuilderWithScope innerBuilder(arg->getNextInstruction(), Builder);
// The switch enum may change ownership resulting in Guaranteed or None.
if (arg->getOwnershipKind() == OwnershipKind::Owned) {
auto loc = RegularLocation::getAutoGeneratedLocation();
innerBuilder.emitDestroyValueOperation(loc, arg);
}
}
if (auto defaultArg = switchEnum->createDefaultResult()) {
SILBuilderWithScope innerBuilder(defaultArg->getNextInstruction(), SEI);
// The switch enum may change ownership resulting in Guaranteed or None.
if (defaultArg->getOwnershipKind() == OwnershipKind::Owned) {
auto loc = RegularLocation::getAutoGeneratedLocation();
innerBuilder.emitDestroyValueOperation(loc, defaultArg);
}
}
return switchEnum;
}
return nullptr;
}
SILInstruction *SILCombiner::visitSelectEnumInst(SelectEnumInst *SEI) {
// Canonicalize a select_enum: if the default refers to exactly one case, then
// replace the default with that case.
if (SEI->hasDefault()) {
NullablePtr<EnumElementDecl> elementDecl = SEI->getUniqueCaseForDefault();
if (elementDecl.isNonNull()) {
// Construct a new instruction by copying all the case entries.
SmallVector<std::pair<EnumElementDecl *, SILValue>, 4> CaseValues;
for (int idx = 0, numIdcs = SEI->getNumCases(); idx < numIdcs; ++idx) {
CaseValues.push_back(SEI->getCase(idx));
}
// Add the default-entry of the original instruction as case-entry.
CaseValues.push_back(
std::make_pair(elementDecl.get(), SEI->getDefaultResult()));
return Builder.createSelectEnum(SEI->getLoc(), SEI->getEnumOperand(),
SEI->getType(), SILValue(), CaseValues);
}
}
// TODO: We should be able to flat-out replace the select_enum instruction
// with the selected value in another pass. For parity with the enum_is_tag
// combiner pass, handle integer literals for now.
auto *EI = dyn_cast<EnumInst>(SEI->getEnumOperand());
if (!EI)
return nullptr;
SILValue selected;
for (unsigned i = 0, e = SEI->getNumCases(); i < e; ++i) {
auto casePair = SEI->getCase(i);
if (casePair.first == EI->getElement()) {
selected = casePair.second;
break;
}
}
if (!selected)
selected = SEI->getDefaultResult();
if (auto *ILI = dyn_cast<IntegerLiteralInst>(selected)) {
return Builder.createIntegerLiteral(ILI->getLoc(), ILI->getType(),
ILI->getValue());
}
return nullptr;
}
SILInstruction *SILCombiner::visitTupleExtractInst(TupleExtractInst *TEI) {
// tuple_extract(apply([add|sub|...]overflow(x, 0)), 1) -> 0
// if it can be proven that no overflow can happen.
if (TEI->getFieldIndex() == 1) {
Builder.setCurrentDebugScope(TEI->getDebugScope());
if (auto *BI = dyn_cast<BuiltinInst>(TEI->getOperand()))
if (!canOverflow(BI))
return Builder.createIntegerLiteral(TEI->getLoc(), TEI->getType(),
APInt(1, 0));
}
return nullptr;
}
SILInstruction *SILCombiner::visitFixLifetimeInst(FixLifetimeInst *fli) {
// fix_lifetime(alloc_stack) -> fix_lifetime(load(alloc_stack))
Builder.setCurrentDebugScope(fli->getDebugScope());
if (auto *ai = dyn_cast<AllocStackInst>(fli->getOperand())) {
if (fli->getOperand()->getType().isLoadable(*fli->getFunction())) {
// load when ossa is disabled
auto load = Builder.emitLoadBorrowOperation(fli->getLoc(), ai);
Builder.createFixLifetime(fli->getLoc(), load);
// no-op when ossa is disabled
Builder.emitEndBorrowOperation(fli->getLoc(), load);
return eraseInstFromFunction(*fli);
}
}
return nullptr;
}
static std::optional<SILType>
shouldReplaceCallByContiguousArrayStorageAnyObject(SILFunction &F,
CanType storageMetaTy) {
auto metaTy = dyn_cast<MetatypeType>(storageMetaTy);
if (!metaTy || metaTy->getRepresentation() != MetatypeRepresentation::Thick)
return std::nullopt;
auto storageTy = metaTy.getInstanceType()->getCanonicalType();
if (!storageTy->is_ContiguousArrayStorage())
return std::nullopt;
auto boundGenericTy = dyn_cast<BoundGenericType>(storageTy);
if (!boundGenericTy)
return std::nullopt;
// On SwiftStdlib 5.7 we can replace the call.
auto &ctxt = storageMetaTy->getASTContext();
auto deployment = AvailabilityContext::forDeploymentTarget(ctxt);
if (!deployment.isContainedIn(ctxt.getSwift57Availability()))
return std::nullopt;
auto genericArgs = boundGenericTy->getGenericArgs();
if (genericArgs.size() != 1)
return std::nullopt;
auto ty = genericArgs[0]->getCanonicalType();
if (!ty->getClassOrBoundGenericClass() && !ty->isObjCExistentialType())
return std::nullopt;
// C++ foreign reference types have custom release/retain operations and are
// not AnyObjects.
if (ty->isForeignReferenceType())
return std::nullopt;
auto anyObjectTy = ctxt.getAnyObjectType();
auto arrayStorageTy =
BoundGenericClassType::get(ctxt.get_ContiguousArrayStorageDecl(), nullptr,
{anyObjectTy})
->getCanonicalType();
return F.getTypeLowering(arrayStorageTy).getLoweredType();
}
SILInstruction *
SILCombiner::
visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
SmallVector<SILValue, 4> Counts;
auto getCounts = [&] (AllocRefDynamicInst *AI) -> ArrayRef<SILValue> {
for (Operand &Op : AI->getTailAllocatedCounts()) {
Counts.push_back(Op.get());
}
return Counts;
};
// %1 = metatype $X.Type
// %2 = alloc_ref_dynamic %1 : $X.Type, Y
// ->
// alloc_ref X
Builder.setCurrentDebugScope(ARDI->getDebugScope());
SILValue MDVal = ARDI->getMetatypeOperand();
while (auto *UCI = dyn_cast<UpcastInst>(MDVal)) {
// For simplicity ignore a cast of an `alloc_ref [stack]`. It would need more
// work to keep its `dealloc_stack_ref` correct.
if (ARDI->canAllocOnStack())
return nullptr;
MDVal = UCI->getOperand();
}
SingleValueInstruction *NewInst = nullptr;
if (auto *MI = dyn_cast<MetatypeInst>(MDVal)) {
auto MetaTy = MI->getType().castTo<MetatypeType>();
auto InstanceTy = MetaTy.getInstanceType();
if (auto SelfTy = dyn_cast<DynamicSelfType>(InstanceTy))
InstanceTy = SelfTy.getSelfType();
auto SILInstanceTy = SILType::getPrimitiveObjectType(InstanceTy);
if (!SILInstanceTy.getClassOrBoundGenericClass())
return nullptr;
NewInst = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
ARDI->isObjC(), ARDI->canAllocOnStack(),
/*isBare=*/ false,
ARDI->getTailAllocatedTypes(),
getCounts(ARDI));
} else if (isa<SILArgument>(MDVal)) {
// checked_cast_br [exact] $Y.Type to $X.Type, bbSuccess, bbFailure
// ...
// bbSuccess(%T: $X.Type)
// alloc_ref_dynamic %T : $X.Type, $X
// ->
// alloc_ref $X
auto *PredBB = ARDI->getParent()->getSinglePredecessorBlock();
if (!PredBB)
return nullptr;
auto *CCBI = dyn_cast<CheckedCastBranchInst>(PredBB->getTerminator());
if (CCBI && CCBI->isExact() && ARDI->getParent() == CCBI->getSuccessBB()) {
auto MetaTy = cast<MetatypeType>(CCBI->getTargetFormalType());
auto InstanceTy = MetaTy.getInstanceType();
if (auto SelfTy = dyn_cast<DynamicSelfType>(InstanceTy))
InstanceTy = SelfTy.getSelfType();
auto SILInstanceTy = SILType::getPrimitiveObjectType(InstanceTy);
if (!SILInstanceTy.getClassOrBoundGenericClass())
return nullptr;
NewInst = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
ARDI->isObjC(), ARDI->canAllocOnStack(),
/*isBare=*/ false,
ARDI->getTailAllocatedTypes(),
getCounts(ARDI));
}
} else if (auto *AI = dyn_cast<ApplyInst>(MDVal)) {
if (ARDI->canAllocOnStack())
return nullptr;
SILFunction *SF = AI->getReferencedFunctionOrNull();
if (!SF)
return nullptr;
if (!SF->hasSemanticsAttr(semantics::ARRAY_GET_CONTIGUOUSARRAYSTORAGETYPE))
return nullptr;
auto use = AI->getSingleUse();
if (!use || use->getUser() != ARDI)
return nullptr;
auto storageTy = AI->getType().getASTType();
// getContiguousArrayStorageType<SomeClass> =>
// ContiguousArrayStorage<AnyObject>
auto instanceTy = shouldReplaceCallByContiguousArrayStorageAnyObject(
*AI->getFunction(), storageTy);
if (!instanceTy)
return nullptr;
NewInst = Builder.createAllocRef(
ARDI->getLoc(), *instanceTy, ARDI->isObjC(), false,
/*isBare=*/ false,
ARDI->getTailAllocatedTypes(), getCounts(ARDI));
NewInst = Builder.createUncheckedRefCast(ARDI->getLoc(), NewInst,
ARDI->getType());
return NewInst;
}
if (NewInst && NewInst->getType() != ARDI->getType()) {
// In case the argument was an upcast of the metatype, we have to upcast the
// resulting reference.
assert(!ARDI->canAllocOnStack() && "upcasting alloc_ref [stack] not supported");
NewInst = Builder.createUpcast(ARDI->getLoc(), NewInst, ARDI->getType());
}
return NewInst;
}
/// Returns true if \p val is a literal instruction or a struct of a literal
/// instruction.
/// What we want to catch here is a UnsafePointer<Int8> of a string literal.
static bool isLiteral(SILValue val) {
while (auto *str = dyn_cast<StructInst>(val)) {
if (str->getNumOperands() != 1)
return false;
val = str->getOperand(0);
}
return isa<LiteralInst>(val);
}
SILInstruction *SILCombiner::visitMarkDependenceInst(MarkDependenceInst *mdi) {
if (!mdi->getFunction()->hasOwnership()) {
// Simplify the base operand of a MarkDependenceInst to eliminate
// unnecessary instructions that aren't adding value.
//
// Conversions to Optional.Some(x) often happen here, this isn't important
// for us, we can just depend on 'x' directly.
if (auto *eiBase = dyn_cast<EnumInst>(mdi->getBase())) {
if (eiBase->hasOperand()) {
mdi->setBase(eiBase->getOperand());
if (eiBase->use_empty()) {
eraseInstFromFunction(*eiBase);
}
return mdi;
}
}
// Conversions from a class to AnyObject also happen a lot, we can just
// depend on the class reference.
if (auto *ier = dyn_cast<InitExistentialRefInst>(mdi->getBase())) {
mdi->setBase(ier->getOperand());
if (ier->use_empty())
eraseInstFromFunction(*ier);
return mdi;
}
// Conversions from a class to AnyObject also happen a lot, we can just
// depend on the class reference.
if (auto *oeri = dyn_cast<OpenExistentialRefInst>(mdi->getBase())) {
mdi->setBase(oeri->getOperand());
if (oeri->use_empty())
eraseInstFromFunction(*oeri);
return mdi;
}
}
// Sometimes due to specialization/builtins, we can get a mark_dependence
// whose base is a trivial typed object. In such a case, the mark_dependence
// does not have a meaning, so just eliminate it.
{
SILType baseType = mdi->getBase()->getType();
if (baseType.isObject() && baseType.isTrivial(*mdi->getFunction())) {
SILValue value = mdi->getValue();
mdi->replaceAllUsesWith(value);
return eraseInstFromFunction(*mdi);
}
}
if (isLiteral(mdi->getValue())) {
// A literal lives forever, so no mark_dependence is needed.
// This pattern can occur after StringOptimization when a utf8CString of
// a literal is replace by the string_literal itself.
replaceInstUsesWith(*mdi, mdi->getValue());
return eraseInstFromFunction(*mdi);
}
return nullptr;
}
SILInstruction *
SILCombiner::visitClassifyBridgeObjectInst(ClassifyBridgeObjectInst *cboi) {
auto *urc = dyn_cast<UncheckedRefCastInst>(cboi->getOperand());
if (!urc)
return nullptr;
auto type = urc->getOperand()->getType().getASTType();
if (ClassDecl *cd = type->getClassOrBoundGenericClass()) {
if (!cd->isObjC()) {
auto int1Ty = SILType::getBuiltinIntegerType(1, Builder.getASTContext());
SILValue zero = Builder.createIntegerLiteral(cboi->getLoc(), int1Ty, 0);
return Builder.createTuple(cboi->getLoc(), {zero, zero});
}
}
return nullptr;
}
/// Returns true if reference counting and debug_value users of a global_value
/// can be deleted.
static bool checkGlobalValueUsers(SILValue val,
SmallVectorImpl<SILInstruction *> &toDelete) {
for (Operand *use : val->getUses()) {
SILInstruction *user = use->getUser();
if (isa<RefCountingInst>(user) || isa<DebugValueInst>(user)) {
toDelete.push_back(user);
continue;
}
if (auto *upCast = dyn_cast<UpcastInst>(user)) {
if (!checkGlobalValueUsers(upCast, toDelete))
return false;
continue;
}
// Projection instructions don't access the object header, so they don't
// prevent deleting reference counting instructions.
if (isa<RefElementAddrInst>(user) || isa<RefTailAddrInst>(user))
continue;
return false;
}
return true;
}
SILInstruction *
SILCombiner::legacyVisitGlobalValueInst(GlobalValueInst *globalValue) {
// Delete all reference count instructions on a global_value if the only other
// users are projections (ref_element_addr and ref_tail_addr).
SmallVector<SILInstruction *, 8> toDelete;
if (!checkGlobalValueUsers(globalValue, toDelete))
return nullptr;
for (SILInstruction *inst : toDelete) {
eraseInstFromFunction(*inst);
}
return nullptr;
}
// Simplify `differentiable_function_extract` of `differentiable_function`.
//
// Before:
// %diff_func = differentiable_function(%orig, %jvp, %vjp)
// %orig' = differentiable_function_extract [original] %diff_func
// %jvp' = differentiable_function_extract [jvp] %diff_func
// %vjp' = differentiable_function_extract [vjp] %diff_func
//
// After:
// %orig' = %orig
// %jvp' = %jvp
// %vjp' = %vjp
SILInstruction *
SILCombiner::visitDifferentiableFunctionExtractInst(DifferentiableFunctionExtractInst *DFEI) {
auto *DFI = dyn_cast<DifferentiableFunctionInst>(DFEI->getOperand());
if (!DFI)
return nullptr;
if (!DFI->hasExtractee(DFEI->getExtractee()))
return nullptr;
SILValue newValue = DFI->getExtractee(DFEI->getExtractee());
// If the type of the `differentiable_function` operand does not precisely
// match the type of the original `differentiable_function_extract`,
// create a `convert_function`.
if (newValue->getType() != DFEI->getType()) {
CanSILFunctionType opTI = newValue->getType().castTo<SILFunctionType>();
CanSILFunctionType resTI = DFEI->getType().castTo<SILFunctionType>();
if (!opTI->isABICompatibleWith(resTI, *DFEI->getFunction()).isCompatible())
return nullptr;
std::tie(newValue, std::ignore) =
castValueToABICompatibleType(&Builder, DFEI->getLoc(),
newValue,
newValue->getType(), DFEI->getType(), {});
}
replaceInstUsesWith(*DFEI, newValue);
return eraseInstFromFunction(*DFEI);
}
// Simplify `pack_length` with constant-length pack.
//
// Before:
// %len = pack_length $Pack{Int, String, Float}
//
// After:
// %len = integer_literal Builtin.Word, 3
SILInstruction *SILCombiner::visitPackLengthInst(PackLengthInst *PLI) {
auto PackTy = PLI->getPackType();
if (!PackTy->containsPackExpansionType()) {
return Builder.createIntegerLiteral(PLI->getLoc(), PLI->getType(),
PackTy->getNumElements());
}
return nullptr;
}
// Simplify `pack_element_get` where the index is a `dynamic_pack_index` with
// a constant operand.
//
// Before:
// %idx = integer_literal Builtin.Word, N
// %pack_idx = dynamic_pack_index %Pack{Int, String, Float}, %idx
// %pack_elt = pack_element_get %pack_value, %pack_idx, @element("...")
//
// After:
// %pack_idx = scalar_pack_index %Pack{Int, String, Float}, N
// %concrete_elt = pack_element_get %pack_value, %pack_idx, <<concrete type>>
// %pack_elt = unchecked_addr_cast %concrete_elt, @element("...")
SILInstruction *SILCombiner::visitPackElementGetInst(PackElementGetInst *PEGI) {
auto *DPII = dyn_cast<DynamicPackIndexInst>(PEGI->getIndex());
if (DPII == nullptr)
return nullptr;
auto PackTy = PEGI->getPackType();
if (PackTy->containsPackExpansionType())
return nullptr;
auto *Op = dyn_cast<IntegerLiteralInst>(DPII->getOperand());
if (Op == nullptr)
return nullptr;
if (Op->getValue().uge(PackTy->getNumElements()))
return nullptr;
unsigned Index = Op->getValue().getZExtValue();
auto *SPII = Builder.createScalarPackIndex(
DPII->getLoc(), Index, DPII->getIndexedPackType());
auto ElementTy = SILType::getPrimitiveAddressType(
PEGI->getPackType().getElementType(Index));
auto *NewPEGI = Builder.createPackElementGet(
PEGI->getLoc(), SPII, PEGI->getPack(),
ElementTy);
return Builder.createUncheckedAddrCast(
PEGI->getLoc(), NewPEGI, PEGI->getElementType());
}
// Simplify `tuple_pack_element_addr` where the index is a `dynamic_pack_index`
//with a constant operand.
//
// Before:
// %idx = integer_literal Builtin.Word, N
// %pack_idx = dynamic_pack_index %Pack{Int, String, Float}, %idx
// %tuple_elt = tuple_pack_element_addr %tuple_value, %pack_idx, @element("...")
//
// After:
// %concrete_elt = tuple_element_addr %tuple_value, N
// %tuple_elt = unchecked_addr_cast %concrete_elt, @element("...")
SILInstruction *
SILCombiner::visitTuplePackElementAddrInst(TuplePackElementAddrInst *TPEAI) {
auto *DPII = dyn_cast<DynamicPackIndexInst>(TPEAI->getIndex());
if (DPII == nullptr)
return nullptr;
auto PackTy = DPII->getIndexedPackType();
if (PackTy->containsPackExpansionType())
return nullptr;
auto *Op = dyn_cast<IntegerLiteralInst>(DPII->getOperand());
if (Op == nullptr)
return nullptr;
if (Op->getValue().uge(PackTy->getNumElements()))
return nullptr;
unsigned Index = Op->getValue().getZExtValue();
auto *TEAI = Builder.createTupleElementAddr(
TPEAI->getLoc(), TPEAI->getTuple(), Index);
return Builder.createUncheckedAddrCast(
TPEAI->getLoc(), TEAI, TPEAI->getElementType());
}
// This is a hack. When optimizing a simple pack expansion expression which
// forms a tuple from a pack, like `(repeat each t)`, after the above
// peepholes we end up with:
//
// %src = unchecked_addr_cast %real_src, @element("...")
// %dst = unchecked_addr_cast %real_dst, @element("...")
// copy_addr %src, %dst
//
// Simplify this to
//
// copy_addr %real_src, %real_dst
//
// Assuming that %real_src and %real_dst have the same type.
//
// In this simple case, this eliminates the opened element archetype entirely.
// However, a more principled peephole would be to transform an
// open_pack_element with a scalar index by replacing all usages of the
// element archetype with a concrete type.
SILInstruction *
SILCombiner::visitCopyAddrInst(CopyAddrInst *CAI) {
auto *Src = dyn_cast<UncheckedAddrCastInst>(CAI->getSrc());
auto *Dst = dyn_cast<UncheckedAddrCastInst>(CAI->getDest());
if (Src == nullptr || Dst == nullptr)
return nullptr;
if (Src->getType() != Dst->getType() ||
!Src->getType().is<ElementArchetypeType>())
return nullptr;
if (Src->getOperand()->getType() != Dst->getOperand()->getType())
return nullptr;
return Builder.createCopyAddr(
CAI->getLoc(), Src->getOperand(), Dst->getOperand(),
CAI->isTakeOfSrc(), CAI->isInitializationOfDest());
}
|