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
|
/*
* Copyright (C) 2024-2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "AirAllocateRegistersByGreedy.h"
#if ENABLE(B3_JIT)
#include "AirArgInlines.h"
#include "AirCode.h"
#include "AirFixSpillsAfterTerminals.h"
#include "AirPhaseInsertionSet.h"
#include "AirInstInlines.h"
#include "AirLiveness.h"
#include "AirPadInterference.h"
#include "AirPhaseScope.h"
#include "AirRegLiveness.h"
#include "AirTmpMap.h"
#include "AirTmpWidthInlines.h"
#include "AirUseCounts.h"
#include <wtf/IterationStatus.h>
#include <wtf/ListDump.h>
#include <wtf/PriorityQueue.h>
#include <wtf/Range.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC { namespace B3 { namespace Air {
namespace Greedy {
// Experiments
static constexpr bool eagerGroups = true;
static constexpr bool eagerGroupsSplitFully = false;
static constexpr bool eagerGroupsExhaustiveSearch = false;
// Quickly filters out short ranges from live range splitting consideration.
static constexpr size_t splitMinRangeSize = 8;
static constexpr float unspillableCost = std::numeric_limits<float>::infinity();
static constexpr float fastTmpSpillCost = std::numeric_limits<float>::max();
static_assert(unspillableCost > fastTmpSpillCost);
// Phase constants used for the PhaseInsertionSet. Ensures that the fixup and spill/fill instructions
// inserted in a particular gap ends up in the correct order.
static constexpr unsigned spillStore = 0;
static constexpr unsigned splitMoveTo = 1;
static constexpr unsigned splitMoveFrom = 2;
static constexpr unsigned spillLoad = 3;
static bool verbose() { return Options::airGreedyRegAllocVerbose(); }
// Terminology / core data-structures:
//
// Point: a position within the IR stream. There are two points associated with each
// instruction: early and late. The early point for an instruction occurs immediately
// before the instruction and the late occurs immediately following. Early use/defs
// for that instruction are modeled as occurring at the early point whereas late use/defs
// are modeled as occurring at the late point. The late point for an instruction and
// early point for the subsequent instruction are distinct in order to avoid false
// conflicts, e.g. when a late use is followed by an early def.
//
// Interval: contiguous set of points, represented by a half open interval [begin, end).
//
// LiveRange: a set of intervals, usually used to model the liveness of a particular Tmp.
// In this implementation, Intervals of a LiveRange must be non-overlapping and
// non-contigous (if contigous, they should have been merged), and stored in sorted order.
//
// RegisterRange: a set of intervals, usually used to model assignments to a particular
// register at each Point. Each Interval of a RegisterRange is associated with a single
// Tmp, which may be a register Tmp (to represent fixed register lifetime and clobbers) or
// a Tmp that is currently assigned to this register. This structure models which Tmp occupies
// the register at each Point. Tmps and their associated Intervals can be assigned and
// evicted to/from the RegisterRange as the register allocation algorithm progresses.
//
// Algorithm:
//
// 1. Initialization:
// a. Define where Points are located in the IR.
// b. Run liveness analysis and build a LiveRange for each Tmp (including fixed registers).
// c. Run analysis to determine the cost to spill each Tmp.
// d. Build metadata related to Tmp coalescing. Eagerly group Tmps that can be coalesced.
// 2. Register allocation:
// a. Process each Tmp in order of priority. Priority is mostly related to the "stage" of the
// Tmp, whether a Tmp has a preferred register, and the size of a Tmp's LiveRange. The idea
// is to fit the most constrained Tmps first, and let the smaller and less constrained ranges
// fit into the remaining gaps.
// b. Each Tmp is driven through a state machine (see Stage enum), where the further a Tmp
// progresses, the more aggressive we try to fit it somewhere.
// - First, simply try to find space where the LiveRange can fit in a RegisterRange.
// - If that's not successful, we may evict LiveRanges in favor of a LiveRange with higher
// spill cost. The evicted LiveRanges will requeued for further processing.
// - If that's not successful, we may split up eagerly coalesced Tmps and process each
// subgroup individually. We also try other forms of splitting LiveRanges, which will
// produce new Tmps/LiveRanges, some of which may be assignable to registers.
// - Finally, if all else fails, the Tmp is spilled and new Tmps for the spill/fill fixups
// are queued for processing.
// 3. Finalization: fixup IR code is inserted to handle Tmps' split ranges and spills.
//
// Note that stack slots are allocated during a subsequent compiler phase.
using Point = size_t;
using Interval = WTF::Range<Point>;
class LiveRange {
public:
LiveRange() = default;
inline void validate()
{
#if ASSERT_ENABLED
size_t size = 0;
Interval* prevInterval = nullptr;
for (auto& interval : m_intervals) {
ASSERT(interval.begin() < interval.end());
ASSERT(!prevInterval || prevInterval->end() < interval.begin());
size += interval.distance();
prevInterval = &interval;
}
ASSERT(size == m_size);
#endif
}
// interval must come before all the intervals already in this LiveRange.
void prepend(Interval interval)
{
ASSERT(interval);
if (m_intervals.isEmpty() || interval.end() < m_intervals.first().begin())
m_intervals.prepend(interval);
else {
ASSERT(interval.end() == m_intervals.first().begin());
m_intervals.first() |= interval;
}
m_size += interval.distance();
validate();
}
// interval must come after all the intervals already in this LiveRange.
void append(Interval interval)
{
ASSERT(interval);
if (m_intervals.isEmpty() || m_intervals.last().end() < interval.begin())
m_intervals.append(interval);
else {
ASSERT(m_intervals.last().end() == interval.begin());
m_intervals.last() |= interval;
}
m_size += interval.distance();
validate();
}
void crossBasicBlockBoundary()
{
m_crossesBasicBlockBoundary = true;
}
bool isLocal() const
{
return !m_crossesBasicBlockBoundary && m_intervals.size() <= 1;
}
const Deque<Interval>& intervals() const
{
return m_intervals;
}
size_t size()
{
return m_size;
}
bool overlaps(LiveRange& other)
{
auto otherIter = other.intervals().begin();
auto otherEnd = other.intervals().end();
for (auto interval : intervals()) {
while (otherIter != otherEnd && otherIter->end() <= interval.begin())
++otherIter; // otherIter was entirely before interval
if (otherIter == otherEnd)
return false;
// Either otherIter overlaps interval or otherIter is entirely after interval.
if (otherIter->begin() < interval.end())
return true;
}
return false;
}
static LiveRange merge(const LiveRange& a, const LiveRange& b)
{
auto getEarliestAndAdvance = [](auto& aIter, const auto aEnd, auto& bIter, const auto bEnd) {
auto postInc = [](auto& iter) {
Interval interval = *iter;
++iter;
return interval;
};
ASSERT(aIter != aEnd || bIter != bEnd);
if (aIter == aEnd)
return postInc(bIter);
if (bIter == bEnd)
return postInc(aIter);
if (aIter->begin() < bIter->begin())
return postInc(aIter);
return postInc(bIter);
};
LiveRange result;
auto aIter = a.intervals().begin();
auto aEnd = a.intervals().end();
auto bIter = b.intervals().begin();
auto bEnd = b.intervals().end();
Interval current = { };
while (aIter != aEnd || bIter != bEnd) {
Interval next = getEarliestAndAdvance(aIter, aEnd, bIter, bEnd);
if (current.overlaps(next)) {
current |= next;
continue;
}
if (current)
result.append(current);
current = next;
}
if (current)
result.append(current);
result.validate();
return result;
}
static LiveRange subtract(const LiveRange& a, const LiveRange& b)
{
LiveRange result;
auto aIter = a.intervals().begin();
auto bIter = b.intervals().begin();
if (aIter == a.intervals().end())
return result;
Interval interval = *aIter;
++aIter;
while (true) {
// Skip over intervals in b that come before the current interval of a.
while (bIter != b.intervals().end() && bIter->end() <= interval.begin())
++bIter;
if (bIter != b.intervals().end() && bIter->overlaps(interval)) {
// Overlap: Split the interval into 0, 1 or 2 intervals.
if (interval.begin() < bIter->begin())
result.append({ interval.begin(), bIter->begin() });
if (bIter->end() < interval.end()) {
// Process remaining portion of the interval.
interval = { bIter->end(), interval.end() };
continue;
}
} else {
// No overlap: include entire interval in result.
result.append(interval);
}
// Finished processing interval of a; move on to the next.
if (aIter == a.intervals().end())
break;
interval = *aIter;
++aIter;
}
result.validate();
return result;
}
void dump(PrintStream& out) const
{
WTF::CommaPrinter comma;
out.print("{ ");
for (auto& interval : intervals())
out.print(comma, interval);
out.print(" }[", m_size, "]");
if (isLocal())
out.print("(local)");
}
private:
Deque<Interval> m_intervals;
size_t m_size { 0 }; // Sum of the distances over m_intervals
bool m_crossesBasicBlockBoundary { false };
};
enum class Stage {
New,
Unspillable,
TryAllocate,
TrySplit,
Spill,
MaxInQueue = Spill,
Assigned,
Coalesced,
Spilled,
Replaced,
};
class TmpPriority {
private:
// Priority is encoded into a 64-bit unsigned integer for fast comparison.
static constexpr size_t stageBits = 3;
static_assert(static_cast<uint64_t>(Stage::MaxInQueue) < (1ULL << stageBits));
static constexpr size_t stageShift = 64 - stageBits;
static constexpr size_t maybeCoalescableBits = 1;
static constexpr size_t maybeCoalescableShift = stageShift - maybeCoalescableBits;
static constexpr size_t isGlobalBits = 1;
static constexpr size_t isGlobalShift = maybeCoalescableShift - isGlobalBits;
static constexpr size_t rangeSizeBits = 39;
static constexpr size_t rangeSizeShift = isGlobalShift - rangeSizeBits;
static constexpr size_t tmpIndexBits = 20;
static constexpr size_t tmpIndexShift = rangeSizeShift - tmpIndexBits;
static_assert(!tmpIndexShift);
inline void packPriority(uint64_t val, size_t numBits, size_t shift, bool reverse)
{
const uint64_t mask = (1ull << numBits) - 1;
val &= mask;
if (reverse)
val = mask - val;
m_priority |= val << shift;
}
public:
TmpPriority(Tmp tmp, Stage stage, size_t rangeSizeOrStart, bool maybeCoalescable, bool isGlobal)
: m_tmp(tmp)
{
ASSERT(!tmp.isReg());
ASSERT(stage <= Stage::MaxInQueue);
// Earlier stages are higher priority.
packPriority(static_cast<uint64_t>(stage), stageBits, stageShift, true);
packPriority(maybeCoalescable, maybeCoalescableBits, maybeCoalescableShift, false);
packPriority(isGlobal, isGlobalBits, isGlobalShift, false);
// If range is global, then rangeSizeOrStart is size, and larger ranges are higher priority.
// If range is local, then rangeSizeOrStart is start, and earlier ranges are higher priority.
packPriority(rangeSizeOrStart, rangeSizeBits, rangeSizeShift, !isGlobal);
// Make a strict total order for determinism.
packPriority(tmp.tmpIndex(), tmpIndexBits, tmpIndexShift, true);
}
Tmp tmp() { return m_tmp; }
Stage stage() const
{
const uint64_t mask = (1 << stageBits) - 1;
uint64_t stageReversed = m_priority >> stageShift;
stageReversed &= mask;
return static_cast<Stage>(mask - stageReversed);
}
void dump(PrintStream& out) const
{
out.print("<", m_tmp, ", ", WTF::RawHex(m_priority), ">");
}
static bool isHigherPriority(const TmpPriority& left, const TmpPriority& right)
{
return left.m_priority > right.m_priority;
}
private:
Tmp m_tmp;
uint64_t m_priority { 0 };
};
class RegisterRange {
public:
RegisterRange() = default;
struct AllocatedInterval {
Tmp tmp;
Interval interval;
bool operator<(const AllocatedInterval& other) const
{
return this->interval.end() < other.interval.end();
}
void dump(PrintStream& out) const
{
out.print("{ ", tmp, " ", interval, " }");
}
};
using AllocatedIntervalSet = StdSet<AllocatedInterval>;
void add(Tmp tmp, LiveRange& range)
{
ASSERT(!hasConflict(range)); // Can't add overlapping LiveRanges
for (auto& interval : range.intervals()) {
ASSERT(interval != Interval()); // Strict ordering requires no empty intervals.
m_allocations.insert({ tmp, interval });
}
}
void evict(Tmp tmp, LiveRange& range)
{
for (auto& interval : range.intervals()) {
auto r = m_allocations.erase({ tmp, interval });
ASSERT_UNUSED(r, r == 1);
}
}
bool hasConflict(LiveRange& range)
{
bool hasConflict = false;
forEachConflict(range, [&](auto&) -> IterationStatus {
hasConflict = true;
return IterationStatus::Done;
});
return hasConflict;
}
// func is called with each (Tmp, Interval) pair (i.e. AllocatedInterval) of this
// RegisterRange that overlaps with the given LiveRange 'range'.
//
// func is allowed to modify this RegisterRange, e.g. by calling evict().
// func must not modify 'range' for the duration of this forEachConflict invocation.
template<typename Func>
void forEachConflict(const LiveRange& range, const Func& func)
{
auto rangeIter = range.intervals().begin();
auto rangeEnd = range.intervals().end();
if (rangeIter == rangeEnd)
return;
Point nextSearch = rangeIter->begin();
while (true) {
AllocatedInterval conflict;
{
auto conflictIter = findFirstIntervalEndingAfter(nextSearch);
if (conflictIter == m_allocations.end())
return; // End of 'm_allocations', so no more potential conflicts
if (rangeIter->end() <= conflictIter->interval.begin()) {
// No more conflicts with this 'range' interval. Move on to the next interval in 'range'.
if (++rangeIter == rangeEnd)
return; // End of 'range', so no more potential conflicts
// Start searching for conflicts of the next 'range' interval.
nextSearch = rangeIter->begin();
continue;
}
// Found a conflict. There may be additional conflicts of this 'range' interval, so advance
// the search position beyond this conflict but don't advance the 'range' interval.
conflict = *conflictIter;
nextSearch = conflictIter->interval.end();
}
// 'func' can invalidate iterators of 'm_allocations'.
if (func(conflict) == IterationStatus::Done)
return;
}
}
void dump(PrintStream& out) const
{
CommaPrinter comma;
out.print("[");
for (auto& alloc : m_allocations) {
out.print(comma);
out.print(alloc);
}
out.print("]");
}
private:
AllocatedIntervalSet::iterator findFirstIntervalEndingAfter(Point point)
{
Interval query(point);
// pos can be 0, yet we can't express a non-empty interval with end==0, so instead of looking
// for the first interval ending after pos we find the first interval ending at or after pos+1.
ASSERT(query.end() == point + 1);
auto iter = m_allocations.lower_bound({ Tmp(), query });
ASSERT(iter == m_allocations.end() || iter->interval.end() > point);
return iter;
}
AllocatedIntervalSet m_allocations;
};
// Auxillary register allocator data per Tmp.
struct TmpData {
struct CoalescableWith {
void dump(PrintStream& out) const
{
out.print("(", tmp, ", ", weight, ")");
}
Tmp tmp;
float weight;
};
void dump(PrintStream& out) const
{
out.print("{stage = ", stage, " liveRange = ", liveRange, ", preferredReg = ", preferredReg,
", coalescables = ", listDump(coalescables), ", subGroup0 = ", subGroup0, ", subGroup1 = ", subGroup1,
", spillCost = ", spillCost, ", assigned = ", assigned, ", spilled = ", pointerDump(spillSlot), ", splitMetadataIndex = ", splitMetadataIndex, "}");
}
bool isGroup()
{
ASSERT(!subGroup0 == !subGroup1);
return !!subGroup0;
}
void validate()
{
ASSERT(!(spillSlot && assigned));
ASSERT(!!assigned == (stage == Stage::Assigned));
ASSERT_IMPLIES(spillSlot, stage == Stage::Spilled);
ASSERT_IMPLIES(spillSlot, spillCost != unspillableCost);
ASSERT_IMPLIES(spillSlot, !isGroup()); // Should have been split
ASSERT_IMPLIES(!spillCost, !liveRange.size());
ASSERT_IMPLIES(assigned, !parentGroup); // Only top-most should be assigned
ASSERT_IMPLIES(coalescables.size(), !isGroup()); // Only bottom-most should have coalescables
}
Stage stage { Stage::New };
LiveRange liveRange;
float spillCost { 0.0f };
Reg preferredReg;
Vector<CoalescableWith> coalescables;
Tmp parentGroup;
Tmp subGroup0, subGroup1;
size_t splitMetadataIndex { 0 };
Reg assigned;
StackSlot* spillSlot { nullptr };
};
// SplitMetadata tracks a Tmp that is split and the new Tmps that are used to carry the value
// across the "gaps".
struct SplitMetadata {
void dump(PrintStream& out) const
{
out.print(originalTmp, " : { ", listDump(gapTmps), " } ");
}
Tmp originalTmp;
Vector<Tmp> gapTmps;
};
class GreedyAllocator {
public:
GreedyAllocator(Code& code)
: m_code(code)
, m_blockToHeadPoint(code.size())
, m_tailPoints(code.size())
, m_map(code)
, m_splitMetadata(1) // Sacrifice index 0.
, m_regRanges(Reg::maxIndex() + 1)
, m_insertionSets(code.size())
, m_useCounts(m_code)
, m_tmpWidth(m_code)
{
}
void run()
{
// FIXME: reconsider use of padIntereference, https://bugs.webkit.org/show_bug.cgi?id=288122
padInterference(m_code);
buildRegisterSets();
buildIndices();
buildLiveRanges();
initSpillCosts<GP>();
initSpillCosts<FP>();
finalizeGroups<GP>();
finalizeGroups<FP>();
allocateRegisters<GP>();
allocateRegisters<FP>();
insertFixupCode();
validateAssignments<GP>();
validateAssignments<FP>();
assignRegisters();
fixSpillsAfterTerminals(m_code);
}
private:
static constexpr Point pointsPerInst = 2;
void buildRegisterSets()
{
forEachBank([&] (Bank bank) {
m_allowedRegistersInPriorityOrder[bank] = m_code.regsInPriorityOrder(bank);
for (Reg r : m_allowedRegistersInPriorityOrder[bank])
m_allAllowedRegisters.add(r, IgnoreVectors);
});
m_allAllowedRegistersWholeWidth = m_allAllowedRegisters.toRegisterSet().includeWholeRegisterWidth();
}
void buildIndices()
{
Point headPosition = 0;
Point tailPosition = 0;
for (size_t i = 0; i < m_code.size(); i++) {
BasicBlock* block = m_code[i];
if (!block) {
m_tailPoints[i] = tailPosition;
continue;
}
// Two points per instruction: early and late.
tailPosition = headPosition + 2 * block->size() - 1;
m_blockToHeadPoint[block] = headPosition;
m_tailPoints[i] = tailPosition;
headPosition += block->size() * pointsPerInst;
}
}
BasicBlock* findBlockContainingPoint(Point point)
{
auto iter = std::lower_bound(m_tailPoints.begin(), m_tailPoints.end(), point);
ASSERT(iter != m_tailPoints.end()); // Should ask only about legal instruction boundaries.
size_t blockIndex = std::distance(m_tailPoints.begin(), iter);
BasicBlock* block = m_code[blockIndex];
ASSERT(positionOfHead(block) <= point && point <= positionOfTail(block));
return block;
}
Point positionOfHead(BasicBlock* block)
{
return m_blockToHeadPoint[block];
}
Point positionOfTail(BasicBlock* block)
{
return positionOfHead(block) + block->size() * pointsPerInst - 1;
}
static size_t instIndex(Point positionOfHead, Point point)
{
return (point - positionOfHead) / pointsPerInst;
}
static Point positionOfEarly(Interval interval)
{
return interval.begin() & ~static_cast<Point>(1);
}
static Interval earlyInterval(Point positionOfEarly)
{
ASSERT(!(positionOfEarly & 1));
return Interval(positionOfEarly);
}
static Interval lateInterval(Point positionOfEarly)
{
ASSERT(!(positionOfEarly & 1));
return Interval(positionOfEarly + 1);
}
static Interval earlyAndLateInterval(Point positionOfEarly)
{
return earlyInterval(positionOfEarly) | lateInterval(positionOfEarly);
}
static Interval interval(Point positionOfEarly, Arg::Timing timing)
{
switch (timing) {
case Arg::OnlyEarly:
return earlyInterval(positionOfEarly);
case Arg::OnlyLate:
return lateInterval(positionOfEarly);
case Arg::EarlyAndLate:
return earlyAndLateInterval(positionOfEarly);
}
ASSERT_NOT_REACHED();
return Interval();
}
static Interval intervalForSpill(Point positionOfEarly, Arg::Role role)
{
Arg::Timing timing = Arg::timing(role);
switch (timing) {
case Arg::OnlyEarly:
if (Arg::isAnyDef(role))
return earlyAndLateInterval(positionOfEarly); // We have a spill store after this insn.
return earlyInterval(positionOfEarly);
case Arg::OnlyLate:
if (Arg::isAnyUse(role))
return earlyAndLateInterval(positionOfEarly); // We had a spill load before this insn.
return lateInterval(positionOfEarly);
case Arg::EarlyAndLate:
return earlyAndLateInterval(positionOfEarly);
}
ASSERT_NOT_REACHED();
return Interval();
}
Tmp groupForTmp(Tmp tmp)
{
while (Tmp parent = m_map[tmp].parentGroup)
tmp = parent;
return tmp;
}
Reg assignedReg(Tmp tmp)
{
return m_map[groupForTmp(tmp)].assigned;
}
StackSlot* spillSlot(Tmp tmp)
{
return m_map[groupForTmp(tmp)].spillSlot;
}
float adjustedBlockFrequency(BasicBlock* block)
{
float freq = block->frequency();
if (UNLIKELY(!m_fastBlocks.saw(block)))
freq *= Options::rareBlockPenalty();
return freq;
}
// Debug code to verify that the results of register allocation and finalization fixup is valid.
// That is, no two Tmps simultaneously alive share the same register (unless they were coalesced).
template<Bank bank>
void validateAssignments()
{
if (!Options::airValidateGreedRegAlloc())
return;
bool anyFailures = false;
auto fail = [&](BasicBlock* block, Tmp a, Tmp b) {
dataLogLn("AIR GREEDY REGISTER ALLOCATION VALIDATION FAILURE");
dataLogLn(" In BB", *block);
dataLogLn(" tmp = ", a, " : ", m_map[a]);
dataLogLn(" tmp = ", b, " : ", m_map[b]);
anyFailures = true;
};
auto checkConflicts = [&](BasicBlock* block, const typename TmpLiveness<bank>::LocalCalc& localCalc) {
for (Tmp a : localCalc.live()) {
Tmp aGrp = groupForTmp(a);
Reg aReg = assignedReg(a);
if (!aReg)
continue;
for (Tmp b : localCalc.live()) {
Tmp bGrp = groupForTmp(b);
Reg bReg = assignedReg(b);
if (aGrp == bGrp) {
// Coalesced a & b so they better have the same register.
if (aReg != bReg)
fail(block, a, b);
} else {
// a & b interfere so b must either have been spilled or assigned a different register.
if (!bReg)
continue;
if (aReg == bReg)
fail(block, a, b);
}
}
}
};
TmpLiveness<bank> liveness(m_code);
for (BasicBlock* block : m_code) {
typename TmpLiveness<bank>::LocalCalc localCalc(liveness, block);
for (unsigned instIndex = block->size(); instIndex--;) {
checkConflicts(block, localCalc);
localCalc.execute(instIndex);
}
checkConflicts(block, localCalc);
}
if (anyFailures) {
dataLogLn("IR:");
dataLogLn(m_code);
RELEASE_ASSERT_NOT_REACHED();
}
}
void buildLiveRanges()
{
CompilerTimingScope timingScope("Air"_s, "GreedyRegAlloc::buildLiveRanges"_s);
UnifiedTmpLiveness liveness(m_code);
TmpMap<Interval> activeIntervals(m_code);
// Find non-rare blocks.
m_fastBlocks.push(m_code[0]);
while (BasicBlock* block = m_fastBlocks.pop()) {
for (FrequentedBlock& successor : block->successors()) {
if (!successor.isRare())
m_fastBlocks.push(successor.block());
}
}
// addMaybeCoalescable is used during the first pass to collect all potentially
// coalescables pairs. i.e. pairs of Tmps 'a' and 'b' such that there exists a
// 'Move a, b' instruction. These pairs will be pruned after liveness analysis
// based on conflicting defs. We do this rather than simply requiring that the
// LiveRanges of coalescable tmps do not overlap so that we can handle Tmp copies, e.g.:
//
// Move a, b
// Use a
// Use b
//
// The LiveRanges of a and b overlap, but the move is still coalescable (unless there
// is a non-coalescable-move def of 'a' or 'b' during the lifetime of the other).
auto addMaybeCoalescable = [&](Tmp a, Tmp b, BasicBlock* block) {
if (a == b)
return;
TmpData& tmpData = m_map[a];
float freq = adjustedBlockFrequency(block);
for (auto& with : tmpData.coalescables) {
if (with.tmp == b) {
with.weight += freq;
return;
}
}
tmpData.coalescables.append({ b, freq });
};
auto coalescableMoveSrc = [&](Inst& inst) {
return mayBeCoalescable(inst) ? inst.args[0].tmp() : Tmp();
};
auto isLiveAt = [&](Tmp tmp, Point point) {
if (activeIntervals[tmp])
return true;
// Tmp may have had a dead def at point (e.g. clobber).
auto& intervals = m_map[tmp].liveRange.intervals();
if (intervals.isEmpty())
return false;
return intervals.first().contains(point);
};
// Remove def from any coalescable pair of a live tmp. We now know from liveness analysis
// that these pairs are not coalescable.
auto pruneCoalescable = [&](Inst& inst, Tmp def, Point point) {
TmpData& defData = m_map[def];
if (!defData.coalescables.size())
return;
Tmp movSrc = coalescableMoveSrc(inst);
dataLogLnIf(verbose(), "Checking affinity ", inst, " def=", def, " movSrc=", movSrc);
defData.coalescables.removeAllMatching([&](TmpData::CoalescableWith& with) {
ASSERT(with.tmp != def);
if (with.tmp != movSrc && isLiveAt(with.tmp, point)) {
dataLogLnIf(verbose(), "Pruning affinity ", def, " ", with.tmp);
m_map[with.tmp].coalescables.removeAllMatching([def](TmpData::CoalescableWith& with) {
return with.tmp == def;
});
return true;
}
return false;
});
};
auto closeInterval = [&](Tmp tmp) {
ASSERT(activeIntervals[tmp] != Interval());
m_map[tmp].liveRange.prepend(activeIntervals[tmp]);
activeIntervals[tmp] = Interval();
};
// First pass: collect all the potential coalescable pairs of Tmps.
for (BasicBlock* block : m_code) {
if (!block)
continue;
for (Inst& inst : block->insts()) {
if (mayBeCoalescable(inst)) {
ASSERT(inst.args.size() == 2);
if (inst.args[0].isReg() || inst.args[1].isReg()) {
unsigned regIdx = inst.args[0].isReg() ? 0 : 1;
Reg reg = inst.args[regIdx].reg();
if (m_allAllowedRegisters.contains(reg, IgnoreVectors)) {
Tmp other = inst.args[regIdx ^ 1].tmp();
if (!m_map[other].preferredReg)
m_map[other].preferredReg = inst.args[regIdx].reg();
}
} else {
ASSERT(inst.args[0].isTmp() && inst.args[1].isTmp());
addMaybeCoalescable(inst.args[0].tmp(), inst.args[1].tmp(), block);
addMaybeCoalescable(inst.args[1].tmp(), inst.args[0].tmp(), block);
}
}
}
}
// Second pass: Run liveness analysis and build the LiveRange for each Tmp. Also,
// prune conflicts from the coalescables.
BasicBlock* blockAfter = nullptr;
Vector<Tmp, 8> earlyUses, earlyDefs, lateUses, lateDefs;
for (size_t blockIndex = m_code.size(); blockIndex--;) {
BasicBlock* block = m_code[blockIndex];
if (!block)
continue;
Point positionOfHead = this->positionOfHead(block);
Point positionOfTail = this->positionOfTail(block);
if (verbose()) {
dataLog("At BB", pointerDump(block), "\n");
dataLog(" positionOfHead = ", positionOfHead, "\n");
dataLog(" positionOfTail = ", positionOfTail, "\n");
}
for (Tmp tmp : liveness.liveAtTail(block))
activeIntervals[tmp] |= Interval(positionOfTail); // FIXME: could just set interval start
if (blockAfter) {
for (Tmp tmp : liveness.liveAtHead(blockAfter)) {
if (activeIntervals[tmp].contains(positionOfTail))
m_map[tmp].liveRange.crossBasicBlockBoundary();
else {
// If tmp was live at the head of the next block but no longer live, close
// the current interval.
ASSERT(activeIntervals[tmp].begin() == this->positionOfHead(blockAfter));
closeInterval(tmp);
}
}
}
for (unsigned instIndex = block->size(); instIndex--;) {
Inst& inst = block->at(instIndex);
Point positionOfEarly = positionOfHead + instIndex * pointsPerInst;
lateUses.shrink(0);
lateDefs.shrink(0);
earlyUses.shrink(0);
earlyDefs.shrink(0);
inst.forEachTmp([&](Tmp& tmp, Arg::Role role, Bank, Width) {
if (Arg::isLateUse(role))
lateUses.append(tmp);
if (Arg::isLateDef(role))
lateDefs.append(tmp);
if (Arg::isEarlyUse(role))
earlyUses.append(tmp);
if (Arg::isEarlyDef(role))
earlyDefs.append(tmp);
});
for (Tmp tmp : lateUses)
activeIntervals[tmp] |= lateInterval(positionOfEarly);
for (Tmp tmp : lateDefs) {
activeIntervals[tmp] |= lateInterval(positionOfEarly);
closeInterval(tmp);
pruneCoalescable(inst, tmp, positionOfEarly + 1);
}
for (Tmp tmp : earlyUses)
activeIntervals[tmp] |= earlyInterval(positionOfEarly);
for (Tmp tmp : earlyDefs) {
activeIntervals[tmp] |= earlyInterval(positionOfEarly);
closeInterval(tmp);
pruneCoalescable(inst, tmp, positionOfEarly);
}
if (inst.kind.opcode == Patch) {
auto clobberReg = [&](Reg reg, Interval interval) {
Tmp tmp = Tmp(reg);
bool isAlive = !!activeIntervals[tmp];
activeIntervals[tmp] |= interval;
if (!isAlive)
closeInterval(tmp);
};
inst.extraClobberedRegs().forEachWithWidthAndPreserved(
[&](Reg reg, Width, PreservedWidth) {
clobberReg(reg, lateInterval(positionOfEarly));
});
inst.extraEarlyClobberedRegs().forEachWithWidthAndPreserved(
[&](Reg reg, Width, PreservedWidth) {
clobberReg(reg, earlyInterval(positionOfEarly));
});
}
}
for (Tmp tmp : liveness.liveAtHead(block))
activeIntervals[tmp] |= Interval(positionOfHead);
blockAfter = block;
}
if (blockAfter) {
for (Tmp tmp : liveness.liveAtHead(blockAfter)) {
ASSERT(activeIntervals[tmp].begin() == this->positionOfHead(blockAfter));
closeInterval(tmp);
}
}
#if ASSERT_ENABLED
m_code.forEachTmp([&](Tmp tmp) {
ASSERT(!activeIntervals[tmp]);
});
#endif
if (verbose()) {
dataLog("Intervals:\n");
auto dumpRegTmpData = [&](Reg r) {
TmpData& tmpData = m_map[Tmp(r)];
if (tmpData.liveRange.size())
dataLog(" ", r, ": ", m_map[Tmp(r)], "\n");
};
for (Reg r : m_allowedRegistersInPriorityOrder[GP])
dumpRegTmpData(r);
for (Reg r : m_allowedRegistersInPriorityOrder[FP])
dumpRegTmpData(r);
m_code.forEachTmp([&](Tmp tmp) {
dataLog(" ", tmp, ": ", m_map[tmp], "\n");
});
}
}
template<typename Func>
IterationStatus forEachTmpInGroup(Tmp grp, const Func& func)
{
TmpData& data = m_map[grp];
if (!data.isGroup())
return func(grp);
ASSERT(data.subGroup0 && data.subGroup1);
if (forEachTmpInGroup(data.subGroup0, func) == IterationStatus::Done)
return IterationStatus::Done;
return forEachTmpInGroup(data.subGroup1, func);
}
template <Bank bank>
void finalizeGroups()
{
CompilerTimingScope timingScope("Air"_s, "GreedyRegAlloc::finalizeGroups"_s);
struct Move {
Tmp tmp0, tmp1;
float weight;
void dump(PrintStream& out) const
{
out.print(tmp0, ", ", tmp1, " ", weight);
}
};
Vector<Move> moves;
m_code.forEachTmp<bank>([&](Tmp tmp) {
ASSERT(!tmp.isReg());
TmpData& data = m_map[tmp];
std::sort(data.coalescables.begin(), data.coalescables.end(),
[this] (const auto& a, const auto& b) -> bool {
if (a.weight != b.weight)
return a.weight > b.weight;
// Favor coalescing shorter live ranges.
auto aSize = m_map[a.tmp].liveRange.size();
auto bSize = m_map[b.tmp].liveRange.size();
if (aSize != bSize)
return aSize < bSize;
return a.tmp.tmpIndex(bank) < b.tmp.tmpIndex(bank);
});
if (!eagerGroups)
return;
for (auto& with : m_map[tmp].coalescables) {
if (tmp.tmpIndex(bank) < with.tmp.tmpIndex(bank))
moves.append({ tmp, with.tmp, with.weight });
}
});
ASSERT_IMPLIES(!eagerGroups, moves.isEmpty());
std::sort(moves.begin(), moves.end(),
[](Move& a, Move& b) -> bool {
if (a.weight != b.weight)
return a.weight > b.weight;
if (a.tmp0.tmpIndex(bank) != b.tmp1.tmpIndex(bank))
return a.tmp0.tmpIndex(bank) < a.tmp0.tmpIndex(bank);
ASSERT(a.tmp1.tmpIndex(bank) != b.tmp1.tmpIndex(bank));
return a.tmp1.tmpIndex(bank) < b.tmp1.tmpIndex(bank);
});
auto hasConflict = [this](Tmp group0, Tmp group1) {
bool conflicts = false;
forEachTmpInGroup(group0, [&](Tmp tmp0) {
ASSERT(!conflicts);
TmpData& data0 = m_map[tmp0];
ASSERT(!data0.subGroup0 && !data0.subGroup1);
forEachTmpInGroup(group1, [&](Tmp tmp1) {
ASSERT(!conflicts);
ASSERT(tmp0 != tmp1);
TmpData& data1 = m_map[tmp1];
if (!data0.coalescables.containsIf([tmp1](auto& with) { return with.tmp == tmp1; })
&& data0.liveRange.overlaps(data1.liveRange)) {
conflicts = true;
return IterationStatus::Done;
}
return IterationStatus::Continue;
});
return conflicts ? IterationStatus::Done : IterationStatus::Continue;
});
return conflicts;
};
auto addSubGroup = [this](Tmp group, TmpData& groupData, Tmp& subGroupField, Tmp subGroup) {
TmpData& subGroupData = m_map[subGroup];
subGroupField = subGroup;
subGroupData.parentGroup = group;
subGroupData.stage = Stage::Coalesced;
groupData.liveRange = LiveRange::merge(groupData.liveRange, subGroupData.liveRange);
groupData.spillCost += subGroupData.spillCost;
if (!groupData.preferredReg)
groupData.preferredReg = subGroupData.preferredReg;
Width defWidth, useWidth;
defWidth = std::max(m_tmpWidth.defWidth(group), m_tmpWidth.defWidth(subGroup));
useWidth = std::max(m_tmpWidth.useWidth(group), m_tmpWidth.useWidth(subGroup));
m_tmpWidth.setWidths(group, useWidth, defWidth);
};
for (Move& move : moves) {
dataLogLnIf(verbose(), "Processing move: ", move);
Tmp group0 = groupForTmp(move.tmp0);
Tmp group1 = groupForTmp(move.tmp1);
if (group0 == group1) {
dataLogLnIf(verbose(), "Already grouped transitively into ", group0);
continue;
}
if (!hasConflict(group0, group1)) {
Tmp newGrp = m_code.newTmp(bank);
TmpData newGrpData;
m_tmpWidth.setWidths(newGrp, Width8, Width8);
addSubGroup(newGrp, newGrpData, newGrpData.subGroup0, group0);
addSubGroup(newGrp, newGrpData, newGrpData.subGroup1, group1);
newGrpData.validate();
m_map.append(newGrp, newGrpData);
dataLogLnIf(verbose(), "Created group ", newGrp, ": ", m_map[newGrp]);
}
}
if (verbose()) {
m_code.forEachTmp<bank>([&](Tmp tmp) {
TmpData& data = m_map[tmp];
if (!data.parentGroup && data.isGroup()) {
dataLog("Group: ", tmp, " = { ");
CommaPrinter comma;
forEachTmpInGroup(tmp, [&comma](Tmp member) {
dataLog(comma, member);
return IterationStatus::Continue;
});
dataLogLn(" }");
}
});
}
}
template<Bank bank>
void initSpillCosts()
{
CompilerTimingScope timingScope("Air"_s, "GreedyRegAlloc::initSpillCosts"_s);
for (Reg reg : m_allowedRegistersInPriorityOrder[bank])
m_map[Tmp(reg)].spillCost = unspillableCost;
// FIXME: tmps alive only in one gap should be unspillable.
m_code.forEachTmp<bank>([&](Tmp tmp) {
ASSERT(tmp.bank() == bank);
ASSERT(!tmp.isReg());
auto index = AbsoluteTmpMapper<bank>::absoluteIndex(tmp);
float spillCost = m_useCounts.numWarmUsesAndDefs<bank>(index);
if (bank == GP && m_useCounts.isConstDef<GP>(index))
spillCost /= 2; // Can rematerialize rather than spill in many cases.
ASSERT(m_map[tmp].spillCost == 0.0f);
m_map[tmp].spillCost = spillCost;
});
m_code.forEachFastTmp([&](Tmp tmp) {
if (tmp.bank() != bank)
return;
m_map[tmp].spillCost = fastTmpSpillCost;
dataLogLnIf(verbose(), "FastTmp: ", tmp);
});
}
// newTmp creates and returns a new tmp that can hold the values of 'from'.
// Note that all TmpData references invalidated since it may expand/realloc the TmpData map.
Tmp newTmp(Tmp from, float spillCost, Interval interval)
{
Tmp tmp = m_code.newTmp(from.bank());
m_tmpWidth.setWidths(tmp, m_tmpWidth.useWidth(from), m_tmpWidth.defWidth(from));
m_map.append(tmp, TmpData());
TmpData& tmpData = m_map[tmp];
tmpData.liveRange.prepend(interval);
tmpData.spillCost = spillCost;
tmpData.validate();
return tmp;
}
Tmp addSpillTmpWithInterval(Tmp spilledTmp, Interval interval)
{
Tmp tmp = newTmp(spilledTmp, unspillableCost, interval);
dataLogLnIf(verbose(), "New spill for ", spilledTmp, " tmp: ", tmp, ": ", m_map[tmp]);
setStageAndEnqueue(tmp, m_map[tmp], Stage::Unspillable);
return tmp;
}
void dumpRegRanges(Bank bank)
{
for (Reg r : m_allowedRegistersInPriorityOrder[bank])
dataLogLn(" regRanges[", r, "]: ", m_regRanges[r]);
}
void setStageAndEnqueue(Tmp tmp, TmpData& tmpData, Stage stage)
{
ASSERT(!tmp.isReg());
ASSERT(stage == Stage::Unspillable || stage == Stage::TryAllocate || stage == Stage::TrySplit || stage == Stage::Spill);
ASSERT_IMPLIES(!tmpData.spillCost, !tmpData.liveRange.size());
ASSERT(!tmpData.parentGroup); // Group member should not be enquened
ASSERT_IMPLIES(!eagerGroups, !tmpData.isGroup());
tmpData.validate();
tmpData.stage = stage;
size_t rangeSizeOrStart = tmpData.liveRange.size();
if (tmpData.liveRange.isLocal())
rangeSizeOrStart = tmpData.liveRange.intervals().first().begin();
m_queue.enqueue({ tmp, stage, rangeSizeOrStart, tmpData.preferredReg || tmpData.coalescables.size(), !tmpData.liveRange.isLocal() });
dataLogLnIf(verbose(), "Enqueued (", stage, ") ", tmp);
}
template <Bank bank>
void allocateRegisters()
{
CompilerTimingScope timingScope("Air"_s, "GreedyRegAlloc::allocateRegisters"_s);
for (Reg reg : m_allowedRegistersInPriorityOrder[bank])
assign(Tmp(reg), m_map[Tmp(reg)], reg);
m_code.forEachTmp<bank>([&](Tmp tmp) {
ASSERT(!tmp.isReg());
TmpData& tmpData = m_map[tmp];
if (tmpData.parentGroup) {
ASSERT(eagerGroups);
return;
}
if (tmpData.liveRange.intervals().isEmpty())
return;
setStageAndEnqueue(tmp, tmpData, Stage::TryAllocate);
});
do {
while (!m_queue.isEmpty()) {
auto entry = m_queue.dequeue();
Tmp tmp = entry.tmp();
ASSERT(tmp && tmp.bank() == bank);
TmpData& tmpData = m_map[tmp];
if (verbose()) {
dataLogLn("Pop: ", entry, " tmp: ", tmpData);
dumpRegRanges(bank);
}
if (tmpData.stage == Stage::Replaced)
continue; // Tmp no longer relevant
if (tryAllocate<bank>(tmp, tmpData))
continue;
if (tmpData.stage != Stage::TrySplit && tryEvict<bank>(tmp, tmpData))
continue;
ASSERT(&tmpData == &m_map[tmp]); // Verify m_map hasn't been resized on this path
switch (tmpData.stage) {
case Stage::TryAllocate: {
// If we couldn't allocate tmp, allow it to split next time.
Stage nextStage = Stage::TrySplit;
// If we already know splitting won't be profitable, skip it.
if (!tmpData.isGroup() && tmpData.liveRange.size() < splitMinRangeSize)
nextStage = Stage::Spill;
setStageAndEnqueue(tmp, tmpData, nextStage);
continue;
}
case Stage::TrySplit:
if (!trySplit<bank>(tmp, tmpData))
setStageAndEnqueue(tmp, tmpData, Stage::Spill);
continue;
case Stage::Spill:
ASSERT(queueContainsOnlySpills()); // FIXME: remove
spill(tmp, tmpData);
continue;
case Stage::Unspillable:
// Unspillables must have been allocated during tryAllocate or tryEvict.
case Stage::New:
case Stage::Assigned:
case Stage::Spilled:
case Stage::Coalesced:
case Stage::Replaced:
dataLogLn("Invalid stage tmp = ", tmp, " tmpData = ", tmpData);
// Tmps in these stages should not have been enqueued.
RELEASE_ASSERT_NOT_REACHED();
}
RELEASE_ASSERT_NOT_REACHED();
}
if (m_didSpill) {
emitSpillCodeAndEnqueueNewTmps<bank>();
m_didSpill = false;
}
// Process the spill/fill tmps,
} while (!m_queue.isEmpty());
}
template <Bank bank>
bool tryAllocate(Tmp tmp, TmpData& tmpData)
{
ASSERT(&m_map[tmp] == &tmpData);
ASSERT(!assignedReg(tmp));
ASSERT(!tmpData.parentGroup);
auto tryAllocateToReg = [&](Reg r) {
LiveRange& liveRange = tmpData.liveRange;
RegisterRange& regRanges = m_regRanges[r];
if (!regRanges.hasConflict(liveRange)) {
assign(tmp, tmpData, r);
return true;
}
return false;
};
ScalarRegisterSet alreadyAttempted;
if (eagerGroupsExhaustiveSearch) {
// FIXME: this will check coalescables within the group, which is wasteful and common.
// But without doing this, we won't try to coalescables between partially split groups.
IterationStatus status = forEachTmpInGroup(tmp, [&](Tmp member) {
for (auto& with : m_map[member].coalescables) {
Reg r = assignedReg(with.tmp);
if (r) {
if (tryAllocateToReg(r))
return IterationStatus::Done;
alreadyAttempted.add(r, IgnoreVectors);
}
}
return IterationStatus::Continue;
});
if (status == IterationStatus::Done) {
ASSERT(tmpData.assigned);
return true;
}
} else {
for (auto& with : tmpData.coalescables) {
Reg r = m_map[with.tmp].assigned;
if (r) {
if (tryAllocateToReg(r))
return true;
alreadyAttempted.add(r, IgnoreVectors);
}
}
}
ASSERT(!tmpData.assigned);
if (tmpData.preferredReg) {
if (tryAllocateToReg(tmpData.preferredReg))
return true;
alreadyAttempted.add(tmpData.preferredReg, IgnoreVectors);
}
for (Reg r : m_allowedRegistersInPriorityOrder[bank]) {
if (alreadyAttempted.contains(r, IgnoreVectors))
continue;
if (tryAllocateToReg(r))
return true;
}
return false;
}
template <Bank bank>
bool tryEvict(Tmp tmp, TmpData& tmpData)
{
ASSERT(&m_map[tmp] == &tmpData);
ASSERT(tmp.bank() == bank);
Reg bestEvictReg;
float minSpillCost = unspillableCost;
BitVector visited(m_code.numTmps(bank));
LiveRange& liveRange = tmpData.liveRange;
for (Reg r : m_allowedRegistersInPriorityOrder[bank]) {
float conflictsSpillCost = 0.0f;
visited.clearAll();
m_regRanges[r].forEachConflict(liveRange,
[&](auto& conflict) -> IterationStatus {
if (conflict.tmp.isReg()) {
// Conflicts with a fixed register use/def, cannot evict.
conflictsSpillCost = unspillableCost;
return IterationStatus::Done;
}
unsigned conflictTmpIndex = conflict.tmp.tmpIndex(bank);
if (visited.quickGet(conflictTmpIndex))
return IterationStatus::Continue;
visited.quickSet(conflictTmpIndex);
auto cost = m_map[conflict.tmp].spillCost;
if (cost == unspillableCost) {
conflictsSpillCost = unspillableCost;
return IterationStatus::Done;
}
conflictsSpillCost += cost;
return conflictsSpillCost >= minSpillCost ? IterationStatus::Done : IterationStatus::Continue;
});
if (conflictsSpillCost < minSpillCost) {
minSpillCost = conflictsSpillCost;
bestEvictReg = r;
}
}
if (minSpillCost >= tmpData.spillCost) {
// If 'tmp' was unspillable, we better have found at least one suitable register.
RELEASE_ASSERT(tmpData.spillCost != unspillableCost);
return false;
}
// It's cheaper to spill all the already-assigned conflicting tmps, so evict them in favor of assigning 'tmp'.
m_regRanges[bestEvictReg].forEachConflict(liveRange,
[&](auto& conflict) -> IterationStatus {
TmpData& conflictData = m_map[conflict.tmp];
evict(conflict.tmp, conflictData, bestEvictReg);
setStageAndEnqueue(conflict.tmp, conflictData, Stage::TryAllocate);
return IterationStatus::Continue;
});
assign(tmp, tmpData, bestEvictReg);
return true;
}
void assign(Tmp tmp, TmpData& tmpData, Reg reg)
{
m_regRanges[reg].add(tmp, tmpData.liveRange);
ASSERT(tmpData.stage != Stage::Assigned && tmpData.stage != Stage::Spilled);
tmpData.stage = Stage::Assigned;
tmpData.assigned = reg;
dataLogLnIf(verbose(), "Assigned ", tmp, " to ", reg);
tmpData.validate();
}
void evict(Tmp tmp, TmpData& tmpData, Reg reg)
{
ASSERT(tmpData.stage == Stage::Assigned);
ASSERT(tmpData.spillCost != unspillableCost);
ASSERT(tmpData.assigned == reg);
m_regRanges[reg].evict(tmp, tmpData.liveRange);
tmpData.stage = Stage::New;
tmpData.assigned = Reg();
dataLogLnIf(verbose(), "Evicted ", tmp, " from ", reg);
tmpData.validate();
}
template<Bank bank>
bool trySplit(Tmp tmp, TmpData& tmpData)
{
ASSERT(tmpData.spillCost != unspillableCost); // Should have evicted.
if (trySplitGroup(tmp, tmpData))
return true;
return trySplitAroundClobbers<bank>(tmp, tmpData);
}
bool trySplitGroup(Tmp tmp, TmpData& tmpData)
{
if (!tmpData.isGroup())
return false;
ASSERT(eagerGroups);
auto enqueueSubgroup = [&](Tmp subGrp) {
m_map[subGrp].parentGroup = Tmp();
setStageAndEnqueue(subGrp, m_map[subGrp], Stage::TryAllocate);
};
if (eagerGroupsSplitFully) {
forEachTmpInGroup(tmp, [&](Tmp member) {
enqueueSubgroup(member);
return IterationStatus::Continue;
});
} else {
enqueueSubgroup(tmpData.subGroup0);
enqueueSubgroup(tmpData.subGroup1);
}
tmpData.stage = Stage::Replaced;
dataLogLnIf(verbose(), "Split (group) ", tmp);
tmpData.validate();
return true;
}
template<Bank bank>
bool trySplitAroundClobbers(Tmp tmp, TmpData& tmpData)
{
if (tmpData.splitMetadataIndex)
return false; // Already split around clobbers
if (tmpData.liveRange.size() < splitMinRangeSize)
return false; // Not enough instructions to be worthwhile
auto instUsesOrDefsTmp = [](Inst& inst, Tmp tmp) {
bool result = false;
inst.forEachTmpFast([&](Tmp useOrDef) {
result |= useOrDef == tmp;
});
return result;
};
Reg bestSplitReg;
float minSplitCost = unspillableCost;
for (Reg r : m_allowedRegistersInPriorityOrder[bank]) {
float splitCost = 0.0f;
m_regRanges[r].forEachConflict(tmpData.liveRange,
[&](auto& conflict) -> IterationStatus {
if (conflict.tmp.isReg() && conflict.interval.distance() == 1) {
// Block freq * rare block penalty
BasicBlock* block = findBlockContainingPoint(conflict.interval.begin());
unsigned instIndex = this->instIndex(positionOfHead(block), conflict.interval.begin());
Inst& inst = block->at(instIndex);
if (instUsesOrDefsTmp(inst, tmp)) {
// If the inst that clobbers regs also use/def the tmp trying to be split, then
// can't split the tmp around this clobber.
// FIXME: could allow uses, but then we'd have to make split tmp conflict with any
// spill tmps used by this instruction, so unclear if that's better.
splitCost = unspillableCost;
return IterationStatus::Done;
}
// Times 2 for 'MOV tmp, gapTmp' and 'MOV gapTmp, tmp'
splitCost += adjustedBlockFrequency(block) * 2;
return IterationStatus::Continue;
}
// Conflicts with another Tmp already assigned to this register so splitting around the clobbers won't help.
splitCost = unspillableCost;
return IterationStatus::Done;
});
if (splitCost < minSplitCost) {
minSplitCost = splitCost;
bestSplitReg = r;
}
}
ASSERT(tmpData.spillCost != unspillableCost); // Should have evicted.
if (minSplitCost >= unspillableCost)
return false; // Other conflicts exist, so splitting is not productive
// Multiplier of 0 means split around clobbers at every opportunity. The higher the multiplier,
// the less often the split will be applied (i.e. treats splitting as more costly).
if (minSplitCost * Options::airGreedyRegAllocSplitMultiplier() >= tmpData.spillCost)
return false; // Better to spill than to split
LiveRange holeRange;
m_regRanges[bestSplitReg].forEachConflict(tmpData.liveRange,
[&](auto& conflict) -> IterationStatus {
ASSERT(conflict.tmp.isReg() && conflict.interval.distance() == 1);
// Punched hole should always include the instructions late interval so the
// split tmp won't be modeled as conflicting with late defs.
Interval hole = conflict.interval | lateInterval(positionOfEarly(conflict.interval));
ASSERT(hole.distance() == 1 || hole.distance() == 2);
holeRange.append(hole);
return IterationStatus::Continue;
});
tmpData.liveRange = LiveRange::subtract(tmpData.liveRange, holeRange);
tmpData.splitMetadataIndex = m_splitMetadata.size();
setStageAndEnqueue(tmp, tmpData, Stage::TryAllocate);
SplitMetadata metadata;
metadata.originalTmp = tmp;
// Create tmps to carry the value across register clobbering instructions. These tmps
// might spill or be assigned another register.
for (Interval hole : holeRange.intervals()) {
BasicBlock* block = findBlockContainingPoint(hole.begin());
float freq = 2 * adjustedBlockFrequency(block);
// padInterference() ensures this.
// FIXME: reconsider that, see https://bugs.webkit.org/show_bug.cgi?id=288122
ASSERT(hole.begin() > positionOfHead(block));
// Model gapTmp interference with any other tmp split at this location by starting
// the gapTmp's range one position before the hole. Otherwise, the same register
// may be chosen for the gapTmp and another splitTmp, which wouldn't be valid
// since there could be a cycle among the set of fixup Move instructions.
// An alternative would be to use the Shuffle opcode (which can handle that
// rotation of register assignments) but that would trigger an extra liveness
// analysis (see lowerAfterRegAlloc()), and that's unlikely to be worth it.
Interval gapInterval = hole | Interval(hole.begin() - 1);
Tmp gapTmp = newTmp(tmp, freq, gapInterval);
metadata.gapTmps.append(gapTmp);
setStageAndEnqueue(gapTmp, m_map[gapTmp], Stage::TryAllocate);
}
dataLogLnIf(verbose(), "Split (clobbers): reg = ", bestSplitReg, " spillCost = ", m_map[tmp].spillCost, " splitCost = ", minSplitCost, " split tmp = ", metadata);
m_splitMetadata.append(WTFMove(metadata));
return true;
}
static unsigned stackSlotMinimumWidth(Width width)
{
if (width <= Width32)
return 4;
if (width <= Width64)
return 8;
ASSERT(width == Width128);
return 16;
}
void spill(Tmp tmp, TmpData& tmpData)
{
RELEASE_ASSERT(tmpData.spillCost != unspillableCost);
ASSERT(tmpData.assigned == Reg());
ASSERT(!tmpData.isGroup()); // Should have been split
tmpData.stage = Stage::Spilled;
dataLogLnIf(verbose(), "Spilled ", tmp);
if (tmpData.splitMetadataIndex) {
// Splitting didn't prevent originalTmp from spilling after all, so no point assigning
// registers or stack slots to the gap tmps for this split.
dataLogLnIf(verbose(), " evicting tmps created during split");
auto& metadata = m_splitMetadata[tmpData.splitMetadataIndex];
ASSERT(metadata.originalTmp == tmp);
for (Tmp gapTmp : metadata.gapTmps) {
Reg reg = m_map[gapTmp].assigned;
if (reg)
evict(gapTmp, m_map[gapTmp], reg);
m_map[gapTmp].stage = Stage::Replaced;
}
}
// Batch the generation of spill/fill tmps so that we can limit traversals of the code while
// not tracking each tmp's use/defs explicitly.
m_didSpill = true;
tmpData.validate();
}
bool queueContainsOnlySpills()
{
for (auto& elem : m_queue) {
if (elem.stage() != Stage::Spill)
return false;
}
return true;
}
Opcode moveOpcode(Tmp tmp)
{
Opcode move = Oops;
Width width = m_tmpWidth.requiredWidth(tmp);
switch (stackSlotMinimumWidth(width)) {
case 4:
move = tmp.bank() == GP ? Move32 : MoveFloat;
break;
case 8:
move = tmp.bank() == GP ? Move : MoveDouble;
break;
case 16:
ASSERT(tmp.bank() == FP);
move = MoveVector;
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
return move;
}
template <Bank bank>
void emitSpillCodeAndEnqueueNewTmps()
{
m_code.forEachTmp<bank>([&](Tmp tmp) {
TmpData& tmpData = m_map[tmp];
if (tmpData.stage == Stage::Spilled && !tmpData.spillSlot)
tmpData.spillSlot = m_code.addStackSlot(stackSlotMinimumWidth(m_tmpWidth.requiredWidth(tmp)), StackSlotKind::Spill);
});
for (BasicBlock* block : m_code) {
Point positionOfHead = this->positionOfHead(block);
for (unsigned instIndex = 0; instIndex < block->size(); ++instIndex) {
Inst& inst = block->at(instIndex);
unsigned indexOfEarly = positionOfHead + instIndex * 2;
// The TmpWidth analysis will say that a Move only stores 32 bits into the destination,
// if the source only had 32 bits worth of non-zero bits. Same for the source: it will
// only claim to read 32 bits from the source if only 32 bits of the destination are
// read. Note that we only apply this logic if this turns into a load or store, since
// Move is the canonical way to move data between GPRs.
bool canUseMove32IfDidSpill = false;
bool didSpill = false;
bool needScratch = false;
Tmp scratchForTmp;
if (bank == GP && inst.kind.opcode == Move) {
if ((inst.args[0].isTmp() && m_tmpWidth.width(inst.args[0].tmp()) <= Width32)
|| (inst.args[1].isTmp() && m_tmpWidth.width(inst.args[1].tmp()) <= Width32))
canUseMove32IfDidSpill = true;
}
// Try to replace the register use by memory use when possible.
inst.forEachArg(
[&] (Arg& arg, Arg::Role role, Bank argBank, Width width) {
if (!arg.isTmp())
return;
if (argBank != bank)
return;
if (arg.isReg())
return;
StackSlot* spilled = spillSlot(arg.tmp());
if (!spilled)
return;
bool needScratchIfSpilledInPlace = false;
if (!inst.admitsStack(arg)) {
switch (inst.kind.opcode) {
case Move:
case MoveDouble:
case MoveFloat:
case Move32: {
unsigned argIndex = &arg - &inst.args[0];
unsigned otherArgIndex = argIndex ^ 1;
Arg otherArg = inst.args[otherArgIndex];
if (inst.args.size() == 2
&& otherArg.isStack()
&& otherArg.stackSlot()->isSpill()) {
needScratchIfSpilledInPlace = true;
break;
}
return;
}
default:
return;
}
}
// If the Tmp holds a constant then we want to rematerialize its
// value rather than loading it from the stack. In order for that
// optimization to kick in, we need to avoid placing the Tmp's stack
// address into the instruction.
if (!Arg::isColdUse(role) && m_useCounts.isConstDef<bank>(AbsoluteTmpMapper<bank>::absoluteIndex(arg.tmp())))
return;
Width spillWidth = m_tmpWidth.requiredWidth(arg.tmp());
if (Arg::isAnyDef(role) && width < spillWidth) {
// Either there are users of this tmp who will use more than width,
// or there are producers who will produce more than width non-zero
// bits.
// FIXME: It's not clear why we should have to return here. We have
// a ZDef fixup in allocateStack. And if this isn't a ZDef, then it
// doesn't seem like it matters what happens to the high bits. Note
// that this isn't the case where we're storing more than what the
// spill slot can hold - we already got that covered because we
// stretch the spill slot on demand. One possibility is that it's ZDefs of
// smaller width than 32-bit.
// https://bugs.webkit.org/show_bug.cgi?id=169823
return;
}
ASSERT(inst.kind.opcode == Move || !(Arg::isAnyUse(role) && width > spillWidth));
if (spillWidth != Width32)
canUseMove32IfDidSpill = false;
spilled->ensureSize(canUseMove32IfDidSpill ? 4 : bytesForWidth(width));
didSpill = true;
if (needScratchIfSpilledInPlace) {
needScratch = true;
scratchForTmp = arg.tmp();
}
arg = Arg::stack(spilled);
});
if (didSpill && canUseMove32IfDidSpill)
inst.kind.opcode = Move32;
if (needScratch) {
ASSERT(scratchForTmp != Tmp());
Tmp tmp = addSpillTmpWithInterval(scratchForTmp, intervalForSpill(indexOfEarly, Arg::Scratch));
inst.args.append(tmp);
RELEASE_ASSERT(inst.args.size() == 3);
// WTF::Liveness and Air::LivenessAdapter do not handle a late-def/use followed by early-def
// correctly. While this register allocator does handle it correctly (since it models distinct
// late and early points between instructions (i.e. intervalForSpill() won't overlap for different
// scratch Tmps)), insert a Nop so that subsequent liveness analysis correctly compute lifetime interference
// when there are back-to-back Move spill-spill-scratch instructions (scratch is early-def, late-use).
// See https://bugs.webkit.org/show_bug.cgi?id=163548#c2 for more info.
// FIXME: reconsider this, https://bugs.webkit.org/show_bug.cgi?id=288122
m_insertionSets[block].insert(instIndex, spillLoad, Nop, inst.origin);
continue;
}
// For every other case, add Load/Store as needed.
inst.forEachTmp([&] (Tmp& tmp, Arg::Role role, Bank argBank, Width) {
if (tmp.isReg() || argBank != bank)
return;
StackSlot* spilled = spillSlot(tmp);
if (!spilled)
return;
Opcode move = moveOpcode(tmp);
auto oldTmp = tmp;
tmp = addSpillTmpWithInterval(tmp, intervalForSpill(indexOfEarly, role));
if (role == Arg::Scratch)
return;
Arg arg = Arg::stack(spilled);
if (Arg::isAnyUse(role)) {
auto tryRematerialize = [&]() {
if constexpr (bank == GP) {
auto oldIndex = AbsoluteTmpMapper<bank>::absoluteIndex(oldTmp);
if (m_useCounts.isConstDef<bank>(oldIndex)) {
int64_t value = m_useCounts.constant<bank>(oldIndex);
if (Arg::isValidImmForm(value) && isValidForm(Move, Arg::Imm, Arg::Tmp)) {
m_insertionSets[block].insert(instIndex, spillLoad, Move, inst.origin, Arg::imm(value), tmp);
dataLogLnIf(verbose(), "Rematerialized (imm) ", oldTmp, ": ", tmp, " <- ", WTF::RawHex(value));
return true;
}
if (isValidForm(Move, Arg::BigImm, Arg::Tmp)) {
m_insertionSets[block].insert(instIndex, spillLoad, Move, inst.origin, Arg::bigImm(value), tmp);
dataLogLnIf(verbose(), "Rematerialized (bigImm) ", oldTmp, ": ", tmp, " <- ", WTF::RawHex(value));
return true;
}
}
}
return false;
};
if (!tryRematerialize())
m_insertionSets[block].insert(instIndex, spillLoad, move, inst.origin, arg, tmp);
}
if (Arg::isAnyDef(role))
m_insertionSets[block].insert(instIndex + 1, spillStore, move, inst.origin, tmp, arg);
});
ASSERT(inst.isValidForm());
}
}
}
void insertFixupCode()
{
for (auto& metadata : m_splitMetadata) {
if (!metadata.originalTmp)
continue;
if (spillSlot(metadata.originalTmp))
continue; // If spilled, better to not split after all. See spill().
ASSERT(assignedReg(metadata.originalTmp));
// Emit moves to and from the gapTmps (or stack stot) that fill the split holes.
for (Tmp gapTmp : metadata.gapTmps) {
TmpData& gapData = m_map[gapTmp];
for (auto& interval : gapData.liveRange.intervals()) {
ASSERT(interval.distance() == 2);
Point lastPoint = interval.end() - 1;
BasicBlock* block = findBlockContainingPoint(lastPoint);
unsigned instIndex = this->instIndex(positionOfHead(block), lastPoint);
Inst& inst = block->at(instIndex);
Arg arg = gapTmp;
StackSlot* spilled = spillSlot(gapTmp);
if (spilled)
arg = Arg::stack(spilled);
Opcode move = moveOpcode(gapTmp);
m_insertionSets[block].insert(instIndex, splitMoveFrom, move, inst.origin, metadata.originalTmp, arg);
m_insertionSets[block].insert(instIndex + 1, splitMoveTo, move, inst.origin, arg, metadata.originalTmp);
dataLogLnIf(verbose(), "Inserted Moves around clobber tmp=", metadata.originalTmp, " gapTmp=", gapTmp, " gapReg = ", assignedReg(gapTmp), " block=", *block, " index=", instIndex, " inst = ", inst);
}
}
}
for (BasicBlock* block : m_code)
m_insertionSets[block].execute(block);
}
bool mayBeCoalescable(Inst& inst)
{
switch (inst.kind.opcode) {
case Move:
case Move32:
case MoveFloat:
case MoveDouble:
case MoveVector:
break;
default:
return false;
}
// Avoid the three-argument coalescable spill moves.
if (inst.args.size() != 2)
return false;
if (!inst.args[0].isTmp() || !inst.args[1].isTmp())
return false;
// We can coalesce a Move32 so long as either of the following holds:
// - The input is already zero-filled.
// - The output only cares about the low 32 bits.
//
// Note that the input property requires an analysis over ZDef's, so it's only valid so long
// as the input gets a register. We don't know if the input gets a register, but we do know
// that if it doesn't get a register then we will still emit this Move32.
if (inst.kind.opcode == Move32 && !is32Bit() && m_tmpWidth.defWidth(inst.args[0].tmp()) > Width32)
return false;
return true;
}
void assignRegisters()
{
CompilerTimingScope timingScope("Air"_s, "GreedyRegAlloc::assignRegisters"_s);
if (verbose()) {
dataLog("About to assign registers. State of all tmps:\n");
m_code.forEachTmp(
[&] (Tmp tmp) {
dataLog(" ", tmp, ": ", m_map[tmp], "\n");
});
dataLog("IR:\n");
dataLog(m_code);
}
for (BasicBlock* block : m_code) {
for (Inst& inst : *block) {
bool mayBeCoalescable = this->mayBeCoalescable(inst);
dataLogLnIf(verbose(), "At: ", inst, mayBeCoalescable ? " [coalescable]" : "");
if constexpr (isX86_64()) {
// Move32 is cheaper if we know that it's equivalent to a Move in x86_64. It's
// equivalent if the destination's high bits are not observable or if the source's high
// bits are all zero.
if (inst.kind.opcode == Move && inst.args[0].isTmp() && inst.args[1].isTmp()) {
if (m_tmpWidth.useWidth(inst.args[1].tmp()) <= Width32 || m_tmpWidth.defWidth(inst.args[0].tmp()) <= Width32)
inst.kind.opcode = Move32;
}
}
if constexpr (isARM64()) {
// On the other hand, on ARM64, Move is cheaper than Move32. We would like to use Move instead of Move32.
// Move32 on ARM64 is explicitly selected in B3LowerToAir for ZExt32 for example. But using ZDef information
// here can optimize it from Move32 to Move.
if (inst.kind.opcode == Move32 && inst.args[0].isTmp() && inst.args[1].isTmp()) {
if (m_tmpWidth.defWidth(inst.args[0].tmp()) <= Width32)
inst.kind.opcode = Move;
}
}
inst.forEachTmpFast([&](Tmp& tmp) {
if (tmp.isReg())
return;
Reg reg = assignedReg(tmp);
if (!reg) {
dataLog("Failed to allocate reg for: ", tmp, "\n");
RELEASE_ASSERT_NOT_REACHED();
}
tmp = Tmp(reg);
});
ASSERT(inst.isValidForm());
if (mayBeCoalescable && inst.args[0].isTmp() && inst.args[1].isTmp()
&& inst.args[0].tmp() == inst.args[1].tmp())
inst = Inst();
}
// Remove all the useless moves we created in this block.
block->insts().removeAllMatching([&] (const Inst& inst) {
return !inst;
});
}
}
Code& m_code;
Vector<Reg> m_allowedRegistersInPriorityOrder[numBanks];
ScalarRegisterSet m_allAllowedRegisters;
RegisterSet m_allAllowedRegistersWholeWidth;
IndexMap<BasicBlock*, Point> m_blockToHeadPoint;
Vector<Point> m_tailPoints;
TmpMap<TmpData> m_map;
Vector<SplitMetadata> m_splitMetadata;
IndexMap<Reg, RegisterRange> m_regRanges;
PriorityQueue<TmpPriority, TmpPriority::isHigherPriority> m_queue;
IndexMap<BasicBlock*, PhaseInsertionSet> m_insertionSets;
BlockWorklist m_fastBlocks;
UseCounts m_useCounts;
TmpWidth m_tmpWidth;
bool m_didSpill { false };
};
} // namespace JSC::B3::Air::Greedy
void allocateRegistersByGreedy(Code& code)
{
RELEASE_ASSERT(!code.usesSIMD());
PhaseScope phaseScope(code, "allocateRegistersAndStackByGreedy"_s);
dataLogIf(Greedy::verbose(), "Air before greedy register allocation:\n", code);
Greedy::GreedyAllocator allocator(code);
allocator.run();
dataLogIf(Greedy::verbose(), "Air after greedy register allocation:\n", code);
}
} } } // namespace JSC::B3::Air
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif // ENABLE(B3_JIT)
|