Revision control

Line Code
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/CacheIRCompiler.h"

#include "jit/IonIC.h"

#include "jit/MacroAssembler-inl.h"

using namespace js;
using namespace js::jit;

using mozilla::Maybe;

ValueOperand
CacheRegisterAllocator::useValueRegister(MacroAssembler& masm, ValOperandId op)
{
    OperandLocation& loc = operandLocations_[op.id()];

    switch (loc.kind()) {
      case OperandLocation::ValueReg:
        currentOpRegs_.add(loc.valueReg());
        return loc.valueReg();

      case OperandLocation::ValueStack: {
        ValueOperand reg = allocateValueRegister(masm);
        popValue(masm, &loc, reg);
        return reg;
      }

      case OperandLocation::Constant: {
        ValueOperand reg = allocateValueRegister(masm);
        masm.moveValue(loc.constant(), reg);
        loc.setValueReg(reg);
        return reg;
      }

      case OperandLocation::PayloadReg: {
        ValueOperand reg = allocateValueRegister(masm);
        masm.tagValue(loc.payloadType(), loc.payloadReg(), reg);
        MOZ_ASSERT(!currentOpRegs_.has(loc.payloadReg()), "Payload register shouldn't be in use");
        availableRegs_.add(loc.payloadReg());
        loc.setValueReg(reg);
        return reg;
      }

      case OperandLocation::PayloadStack: {
        ValueOperand reg = allocateValueRegister(masm);
        popPayload(masm, &loc, reg.scratchReg());
        masm.tagValue(loc.payloadType(), reg.scratchReg(), reg);
        loc.setValueReg(reg);
        return reg;
      }
      case OperandLocation::Uninitialized:
        break;
    }

    MOZ_CRASH();
}

ValueOperand
CacheRegisterAllocator::useFixedValueRegister(MacroAssembler& masm, ValOperandId valId,
                                              ValueOperand reg)
{
    allocateFixedValueRegister(masm, reg);

    OperandLocation& loc = operandLocations_[valId.id()];
    switch (loc.kind()) {
      case OperandLocation::ValueReg:
        masm.moveValue(loc.valueReg(), reg);
        MOZ_ASSERT(!currentOpRegs_.aliases(loc.valueReg()), "Register shouldn't be in use");
        availableRegs_.add(loc.valueReg());
        break;
      case OperandLocation::ValueStack:
        popValue(masm, &loc, reg);
        break;
      case OperandLocation::Constant:
        masm.moveValue(loc.constant(), reg);
        break;
      case OperandLocation::PayloadReg:
        masm.tagValue(loc.payloadType(), loc.payloadReg(), reg);
        MOZ_ASSERT(!currentOpRegs_.has(loc.payloadReg()), "Register shouldn't be in use");
        availableRegs_.add(loc.payloadReg());
        break;
      case OperandLocation::PayloadStack:
        popPayload(masm, &loc, reg.scratchReg());
        masm.tagValue(loc.payloadType(), reg.scratchReg(), reg);
        break;
      case OperandLocation::Uninitialized:
        MOZ_CRASH();
    }

    loc.setValueReg(reg);
    return reg;
}

Register
CacheRegisterAllocator::useRegister(MacroAssembler& masm, TypedOperandId typedId)
{
    OperandLocation& loc = operandLocations_[typedId.id()];
    switch (loc.kind()) {
      case OperandLocation::PayloadReg:
        currentOpRegs_.add(loc.payloadReg());
        return loc.payloadReg();

      case OperandLocation::ValueReg: {
        // It's possible the value is still boxed: as an optimization, we unbox
        // the first time we use a value as object.
        ValueOperand val = loc.valueReg();
        availableRegs_.add(val);
        Register reg = val.scratchReg();
        availableRegs_.take(reg);
        masm.unboxObject(val, reg);
        loc.setPayloadReg(reg, typedId.type());
        currentOpRegs_.add(reg);
        return reg;
      }

      case OperandLocation::PayloadStack: {
        Register reg = allocateRegister(masm);
        popPayload(masm, &loc, reg);
        return reg;
      }

      case OperandLocation::ValueStack: {
        // The value is on the stack, but boxed. If it's on top of the stack we
        // unbox it and then remove it from the stack, else we just unbox.
        Register reg = allocateRegister(masm);
        if (loc.valueStack() == stackPushed_) {
            masm.unboxObject(Address(masm.getStackPointer(), 0), reg);
            masm.addToStackPtr(Imm32(sizeof(js::Value)));
            MOZ_ASSERT(stackPushed_ >= sizeof(js::Value));
            stackPushed_ -= sizeof(js::Value);
        } else {
            MOZ_ASSERT(loc.valueStack() < stackPushed_);
            masm.unboxObject(Address(masm.getStackPointer(), stackPushed_ - loc.valueStack()),
                             reg);
        }
        loc.setPayloadReg(reg, typedId.type());
        return reg;
      }

      case OperandLocation::Constant: {
        Value v = loc.constant();
        Register reg = allocateRegister(masm);
        if (v.isString())
            masm.movePtr(ImmGCPtr(v.toString()), reg);
        else if (v.isSymbol())
            masm.movePtr(ImmGCPtr(v.toSymbol()), reg);
        else
            MOZ_CRASH("Unexpected Value");
        loc.setPayloadReg(reg, v.extractNonDoubleType());
        return reg;
      }

      case OperandLocation::Uninitialized:
        break;
    }

    MOZ_CRASH();
}

Register
CacheRegisterAllocator::defineRegister(MacroAssembler& masm, TypedOperandId typedId)
{
    OperandLocation& loc = operandLocations_[typedId.id()];
    MOZ_ASSERT(loc.kind() == OperandLocation::Uninitialized);

    Register reg = allocateRegister(masm);
    loc.setPayloadReg(reg, typedId.type());
    return reg;
}

ValueOperand
CacheRegisterAllocator::defineValueRegister(MacroAssembler& masm, ValOperandId val)
{
    OperandLocation& loc = operandLocations_[val.id()];
    MOZ_ASSERT(loc.kind() == OperandLocation::Uninitialized);

    ValueOperand reg = allocateValueRegister(masm);
    loc.setValueReg(reg);
    return reg;
}

void
CacheRegisterAllocator::freeDeadOperandRegisters()
{
    // See if any operands are dead so we can reuse their registers. Note that
    // we skip the input operands, as those are also used by failure paths, and
    // we currently don't track those uses.
    for (size_t i = writer_.numInputOperands(); i < operandLocations_.length(); i++) {
        if (!writer_.operandIsDead(i, currentInstruction_))
            continue;

        OperandLocation& loc = operandLocations_[i];
        switch (loc.kind()) {
          case OperandLocation::PayloadReg:
            availableRegs_.add(loc.payloadReg());
            break;
          case OperandLocation::ValueReg:
            availableRegs_.add(loc.valueReg());
            break;
          case OperandLocation::Uninitialized:
          case OperandLocation::PayloadStack:
          case OperandLocation::ValueStack:
          case OperandLocation::Constant:
            break;
        }
        loc.setUninitialized();
    }
}

void
CacheRegisterAllocator::discardStack(MacroAssembler& masm)
{
    // This should only be called when we are no longer using the operands,
    // as we're discarding everything from the native stack. Set all operand
    // locations to Uninitialized to catch bugs.
    for (size_t i = 0; i < operandLocations_.length(); i++)
        operandLocations_[i].setUninitialized();

    if (stackPushed_ > 0) {
        masm.addToStackPtr(Imm32(stackPushed_));
        stackPushed_ = 0;
    }
}

Register
CacheRegisterAllocator::allocateRegister(MacroAssembler& masm)
{
    if (availableRegs_.empty())
        freeDeadOperandRegisters();

    if (availableRegs_.empty()) {
        // Still no registers available, try to spill unused operands to
        // the stack.
        for (size_t i = 0; i < operandLocations_.length(); i++) {
            OperandLocation& loc = operandLocations_[i];
            if (loc.kind() == OperandLocation::PayloadReg) {
                Register reg = loc.payloadReg();
                if (currentOpRegs_.has(reg))
                    continue;

                spillOperandToStack(masm, &loc);
                availableRegs_.add(reg);
                break; // We got a register, so break out of the loop.
            }
            if (loc.kind() == OperandLocation::ValueReg) {
                ValueOperand reg = loc.valueReg();
                if (currentOpRegs_.aliases(reg))
                    continue;

                spillOperandToStack(masm, &loc);
                availableRegs_.add(reg);
                break; // Break out of the loop.
            }
        }
    }

    if (availableRegs_.empty() && !availableRegsAfterSpill_.empty()) {
        Register reg = availableRegsAfterSpill_.takeAny();
        masm.push(reg);
        stackPushed_ += sizeof(uintptr_t);

        masm.propagateOOM(spilledRegs_.append(SpilledRegister(reg, stackPushed_)));

        availableRegs_.add(reg);
    }

    // At this point, there must be a free register.
    MOZ_RELEASE_ASSERT(!availableRegs_.empty());

    Register reg = availableRegs_.takeAny();
    currentOpRegs_.add(reg);
    return reg;
}

void
CacheRegisterAllocator::allocateFixedRegister(MacroAssembler& masm, Register reg)
{
    // Fixed registers should be allocated first, to ensure they're
    // still available.
    MOZ_ASSERT(!currentOpRegs_.has(reg), "Register is in use");

    freeDeadOperandRegisters();

    if (availableRegs_.has(reg)) {
        availableRegs_.take(reg);
        currentOpRegs_.add(reg);
        return;
    }

    // The register must be used by some operand. Spill it to the stack.
    for (size_t i = 0; i < operandLocations_.length(); i++) {
        OperandLocation& loc = operandLocations_[i];
        if (loc.kind() == OperandLocation::PayloadReg) {
            if (loc.payloadReg() != reg)
                continue;

            spillOperandToStackOrRegister(masm, &loc);
            currentOpRegs_.add(reg);
            return;
        }
        if (loc.kind() == OperandLocation::ValueReg) {
            if (!loc.valueReg().aliases(reg))
                continue;

            ValueOperand valueReg = loc.valueReg();
            spillOperandToStackOrRegister(masm, &loc);

            availableRegs_.add(valueReg);
            availableRegs_.take(reg);
            currentOpRegs_.add(reg);
            return;
        }
    }

    MOZ_CRASH("Invalid register");
}

void
CacheRegisterAllocator::allocateFixedValueRegister(MacroAssembler& masm, ValueOperand reg)
{
#ifdef JS_NUNBOX32
    allocateFixedRegister(masm, reg.payloadReg());
    allocateFixedRegister(masm, reg.typeReg());
#else
    allocateFixedRegister(masm, reg.valueReg());
#endif
}

ValueOperand
CacheRegisterAllocator::allocateValueRegister(MacroAssembler& masm)
{
#ifdef JS_NUNBOX32
    Register reg1 = allocateRegister(masm);
    Register reg2 = allocateRegister(masm);
    return ValueOperand(reg1, reg2);
#else
    Register reg = allocateRegister(masm);
    return ValueOperand(reg);
#endif
}

bool
CacheRegisterAllocator::init()
{
    if (!origInputLocations_.resize(writer_.numInputOperands()))
        return false;
    if (!operandLocations_.resize(writer_.numOperandIds()))
        return false;
    return true;
}

void
CacheRegisterAllocator::initAvailableRegsAfterSpill()
{
    // Registers not in availableRegs_ and not used by input operands are
    // available after being spilled.
    availableRegsAfterSpill_.set() =
        GeneralRegisterSet::Intersect(GeneralRegisterSet::Not(availableRegs_.set()),
                                      GeneralRegisterSet::Not(inputRegisterSet()));
}

GeneralRegisterSet
CacheRegisterAllocator::inputRegisterSet() const
{
    MOZ_ASSERT(origInputLocations_.length() == writer_.numInputOperands());

    AllocatableGeneralRegisterSet result;
    for (size_t i = 0; i < writer_.numInputOperands(); i++) {
        const OperandLocation& loc = operandLocations_[i];
        MOZ_ASSERT(loc == origInputLocations_[i]);

        switch (loc.kind()) {
          case OperandLocation::PayloadReg:
            result.add(loc.payloadReg());
            continue;
          case OperandLocation::ValueReg:
            result.add(loc.valueReg());
            continue;
          case OperandLocation::PayloadStack:
          case OperandLocation::ValueStack:
          case OperandLocation::Constant:
            continue;
          case OperandLocation::Uninitialized:
            break;
        }
        MOZ_CRASH("Invalid kind");
    }

    return result.set();
}

JSValueType
CacheRegisterAllocator::knownType(ValOperandId val) const
{
    const OperandLocation& loc = operandLocations_[val.id()];

    switch (loc.kind()) {
      case OperandLocation::ValueReg:
      case OperandLocation::ValueStack:
        return JSVAL_TYPE_UNKNOWN;

      case OperandLocation::PayloadStack:
      case OperandLocation::PayloadReg:
        return loc.payloadType();

      case OperandLocation::Constant:
        return loc.constant().isDouble()
               ? JSVAL_TYPE_DOUBLE
               : loc.constant().extractNonDoubleType();

      case OperandLocation::Uninitialized:
        break;
    }

    MOZ_CRASH("Invalid kind");
}

void
CacheRegisterAllocator::initInputLocation(size_t i, const TypedOrValueRegister& reg)
{
    if (reg.hasValue()) {
        initInputLocation(i, reg.valueReg());
    } else {
        MOZ_ASSERT(!reg.typedReg().isFloat());
        initInputLocation(i, reg.typedReg().gpr(), ValueTypeFromMIRType(reg.type()));
    }
}

void
CacheRegisterAllocator::initInputLocation(size_t i, const ConstantOrRegister& value)
{
    if (value.constant())
        initInputLocation(i, value.value());
    else
        initInputLocation(i, value.reg());
}

void
CacheRegisterAllocator::spillOperandToStack(MacroAssembler& masm, OperandLocation* loc)
{
    MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());

    if (loc->kind() == OperandLocation::ValueReg) {
        stackPushed_ += sizeof(js::Value);
        masm.pushValue(loc->valueReg());
        loc->setValueStack(stackPushed_);
        return;
    }

    MOZ_ASSERT(loc->kind() == OperandLocation::PayloadReg);

    stackPushed_ += sizeof(uintptr_t);
    masm.push(loc->payloadReg());
    loc->setPayloadStack(stackPushed_, loc->payloadType());
}

void
CacheRegisterAllocator::spillOperandToStackOrRegister(MacroAssembler& masm, OperandLocation* loc)
{
    MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());

    // If enough registers are available, use them.
    if (loc->kind() == OperandLocation::ValueReg) {
        static const size_t BoxPieces = sizeof(Value) / sizeof(uintptr_t);
        if (availableRegs_.set().size() >= BoxPieces) {
            ValueOperand reg = availableRegs_.takeAnyValue();
            masm.moveValue(loc->valueReg(), reg);
            loc->setValueReg(reg);
            return;
        }
    } else {
        MOZ_ASSERT(loc->kind() == OperandLocation::PayloadReg);
        if (!availableRegs_.empty()) {
            Register reg = availableRegs_.takeAny();
            masm.movePtr(loc->payloadReg(), reg);
            loc->setPayloadReg(reg, loc->payloadType());
            return;
        }
    }

    // Not enough registers available, spill to the stack.
    spillOperandToStack(masm, loc);
}

void
CacheRegisterAllocator::popPayload(MacroAssembler& masm, OperandLocation* loc, Register dest)
{
    MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
    MOZ_ASSERT(stackPushed_ >= sizeof(uintptr_t));

    // The payload is on the stack. If it's on top of the stack we can just
    // pop it, else we emit a load.
    if (loc->payloadStack() == stackPushed_) {
        masm.pop(dest);
        stackPushed_ -= sizeof(uintptr_t);
    } else {
        MOZ_ASSERT(loc->payloadStack() < stackPushed_);
        masm.loadPtr(Address(masm.getStackPointer(), stackPushed_ - loc->payloadStack()), dest);
    }

    loc->setPayloadReg(dest, loc->payloadType());
}

void
CacheRegisterAllocator::popValue(MacroAssembler& masm, OperandLocation* loc, ValueOperand dest)
{
    MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
    MOZ_ASSERT(stackPushed_ >= sizeof(js::Value));

    // The Value is on the stack. If it's on top of the stack we can just
    // pop it, else we emit a load.
    if (loc->valueStack() == stackPushed_) {
        masm.popValue(dest);
        stackPushed_ -= sizeof(js::Value);
    } else {
        MOZ_ASSERT(loc->valueStack() < stackPushed_);
        masm.loadValue(Address(masm.getStackPointer(), stackPushed_ - loc->valueStack()), dest);
    }

    loc->setValueReg(dest);
}

bool
OperandLocation::aliasesReg(const OperandLocation& other) const
{
    MOZ_ASSERT(&other != this);

    switch (other.kind_) {
      case PayloadReg:
        return aliasesReg(other.payloadReg());
      case ValueReg:
        return aliasesReg(other.valueReg());
      case PayloadStack:
      case ValueStack:
      case Constant:
        return false;
      case Uninitialized:
        break;
    }

    MOZ_CRASH("Invalid kind");
}

void
CacheRegisterAllocator::restoreInputState(MacroAssembler& masm, bool shouldDiscardStack)
{
    size_t numInputOperands = origInputLocations_.length();
    MOZ_ASSERT(writer_.numInputOperands() == numInputOperands);

    for (size_t j = 0; j < numInputOperands; j++) {
        const OperandLocation& dest = origInputLocations_[j];
        OperandLocation& cur = operandLocations_[j];
        if (dest == cur)
            continue;

        auto autoAssign = mozilla::MakeScopeExit([&] { cur = dest; });

        // We have a cycle if a destination register will be used later
        // as source register. If that happens, just push the current value
        // on the stack and later get it from there.
        for (size_t k = j + 1; k < numInputOperands; k++) {
            OperandLocation& laterSource = operandLocations_[k];
            if (dest.aliasesReg(laterSource))
                spillOperandToStack(masm, &laterSource);
        }

        if (dest.kind() == OperandLocation::ValueReg) {
            // We have to restore a Value register.
            switch (cur.kind()) {
              case OperandLocation::ValueReg:
                masm.moveValue(cur.valueReg(), dest.valueReg());
                continue;
              case OperandLocation::PayloadReg:
                masm.tagValue(cur.payloadType(), cur.payloadReg(), dest.valueReg());
                continue;
              case OperandLocation::PayloadStack: {
                Register scratch = dest.valueReg().scratchReg();
                popPayload(masm, &cur, scratch);
                masm.tagValue(cur.payloadType(), scratch, dest.valueReg());
                continue;
              }
              case OperandLocation::ValueStack:
                popValue(masm, &cur, dest.valueReg());
                continue;
              case OperandLocation::Constant:
              case OperandLocation::Uninitialized:
                break;
            }
        } else if (dest.kind() == OperandLocation::PayloadReg) {
            // We have to restore a payload register.
            switch (cur.kind()) {
              case OperandLocation::ValueReg:
                MOZ_ASSERT(dest.payloadType() != JSVAL_TYPE_DOUBLE);
                masm.unboxNonDouble(cur.valueReg(), dest.payloadReg());
                continue;
              case OperandLocation::PayloadReg:
                MOZ_ASSERT(cur.payloadType() == dest.payloadType());
                masm.mov(cur.payloadReg(), dest.payloadReg());
                continue;
              case OperandLocation::PayloadStack: {
                MOZ_ASSERT(cur.payloadType() == dest.payloadType());
                popPayload(masm, &cur, dest.payloadReg());
                continue;
              }
              case OperandLocation::ValueStack:
                MOZ_ASSERT(stackPushed_ >= sizeof(js::Value));
                MOZ_ASSERT(cur.valueStack() <= stackPushed_);
                MOZ_ASSERT(dest.payloadType() != JSVAL_TYPE_DOUBLE);
                masm.unboxNonDouble(Address(masm.getStackPointer(), stackPushed_ - cur.valueStack()),
                                    dest.payloadReg());
                continue;
              case OperandLocation::Constant:
              case OperandLocation::Uninitialized:
                break;
            }
        } else if (dest.kind() == OperandLocation::Constant) {
            // Nothing to do.
            continue;
        }

        MOZ_CRASH("Invalid kind");
    }

    for (const SpilledRegister& spill : spilledRegs_) {
        MOZ_ASSERT(stackPushed_ >= sizeof(uintptr_t));

        if (spill.stackPushed == stackPushed_) {
            masm.pop(spill.reg);
            stackPushed_ -= sizeof(uintptr_t);
        } else {
            MOZ_ASSERT(spill.stackPushed < stackPushed_);
            masm.loadPtr(Address(masm.getStackPointer(), stackPushed_ - spill.stackPushed),
                         spill.reg);
        }
    }

    if (shouldDiscardStack)
        discardStack(masm);
}

size_t
CacheIRStubInfo::stubDataSize() const
{
    size_t field = 0;
    size_t size = 0;
    while (true) {
        StubField::Type type = fieldType(field++);
        if (type == StubField::Type::Limit)
            return size;
        size += StubField::sizeInBytes(type);
    }
}

void
CacheIRStubInfo::copyStubData(ICStub* src, ICStub* dest) const
{
    uint8_t* srcBytes = reinterpret_cast<uint8_t*>(src);
    uint8_t* destBytes = reinterpret_cast<uint8_t*>(dest);

    size_t field = 0;
    size_t offset = 0;
    while (true) {
        StubField::Type type = fieldType(field);
        switch (type) {
          case StubField::Type::RawWord:
            *reinterpret_cast<uintptr_t*>(destBytes + offset) =
                *reinterpret_cast<uintptr_t*>(srcBytes + offset);
            break;
          case StubField::Type::RawInt64:
          case StubField::Type::DOMExpandoGeneration:
            *reinterpret_cast<uint64_t*>(destBytes + offset) =
                *reinterpret_cast<uint64_t*>(srcBytes + offset);
            break;
          case StubField::Type::Shape:
            getStubField<ICStub, Shape*>(dest, offset).init(getStubField<ICStub, Shape*>(src, offset));
            break;
          case StubField::Type::JSObject:
            getStubField<ICStub, JSObject*>(dest, offset).init(getStubField<ICStub, JSObject*>(src, offset));
            break;
          case StubField::Type::ObjectGroup:
            getStubField<ICStub, ObjectGroup*>(dest, offset).init(getStubField<ICStub, ObjectGroup*>(src, offset));
            break;
          case StubField::Type::Symbol:
            getStubField<ICStub, JS::Symbol*>(dest, offset).init(getStubField<ICStub, JS::Symbol*>(src, offset));
            break;
          case StubField::Type::String:
            getStubField<ICStub, JSString*>(dest, offset).init(getStubField<ICStub, JSString*>(src, offset));
            break;
          case StubField::Type::Id:
            getStubField<ICStub, jsid>(dest, offset).init(getStubField<ICStub, jsid>(src, offset));
            break;
          case StubField::Type::Value:
            getStubField<ICStub, Value>(dest, offset).init(getStubField<ICStub, Value>(src, offset));
            break;
          case StubField::Type::Limit:
            return; // Done.
        }
        field++;
        offset += StubField::sizeInBytes(type);
    }
}

template <typename T>
static GCPtr<T>*
AsGCPtr(uintptr_t* ptr)
{
    return reinterpret_cast<GCPtr<T>*>(ptr);
}

template<class Stub, class T>
GCPtr<T>&
CacheIRStubInfo::getStubField(Stub* stub, uint32_t offset) const
{
    uint8_t* stubData = (uint8_t*)stub + stubDataOffset_;
    MOZ_ASSERT(uintptr_t(stubData) % sizeof(uintptr_t) == 0);

    return *AsGCPtr<T>((uintptr_t*)(stubData + offset));
}

template GCPtr<Shape*>& CacheIRStubInfo::getStubField<ICStub>(ICStub* stub, uint32_t offset) const;
template GCPtr<ObjectGroup*>& CacheIRStubInfo::getStubField<ICStub>(ICStub* stub, uint32_t offset) const;
template GCPtr<JSObject*>& CacheIRStubInfo::getStubField<ICStub>(ICStub* stub, uint32_t offset) const;
template GCPtr<JSString*>& CacheIRStubInfo::getStubField<ICStub>(ICStub* stub, uint32_t offset) const;
template GCPtr<JS::Symbol*>& CacheIRStubInfo::getStubField<ICStub>(ICStub* stub, uint32_t offset) const;
template GCPtr<JS::Value>& CacheIRStubInfo::getStubField<ICStub>(ICStub* stub, uint32_t offset) const;
template GCPtr<jsid>& CacheIRStubInfo::getStubField<ICStub>(ICStub* stub, uint32_t offset) const;

template <typename T, typename V>
static void
InitGCPtr(uintptr_t* ptr, V val)
{
    AsGCPtr<T>(ptr)->init(mozilla::BitwiseCast<T>(val));
}

void
CacheIRWriter::copyStubData(uint8_t* dest) const
{
    MOZ_ASSERT(!failed());

    uintptr_t* destWords = reinterpret_cast<uintptr_t*>(dest);

    for (const StubField& field : stubFields_) {
        switch (field.type()) {
          case StubField::Type::RawWord:
            *destWords = field.asWord();
            break;
          case StubField::Type::Shape:
            InitGCPtr<Shape*>(destWords, field.asWord());
            break;
          case StubField::Type::JSObject:
            InitGCPtr<JSObject*>(destWords, field.asWord());
            break;
          case StubField::Type::ObjectGroup:
            InitGCPtr<ObjectGroup*>(destWords, field.asWord());
            break;
          case StubField::Type::Symbol:
            InitGCPtr<JS::Symbol*>(destWords, field.asWord());
            break;
          case StubField::Type::String:
            InitGCPtr<JSString*>(destWords, field.asWord());
            break;
          case StubField::Type::Id:
            InitGCPtr<jsid>(destWords, field.asWord());
            break;
          case StubField::Type::RawInt64:
          case StubField::Type::DOMExpandoGeneration:
            *reinterpret_cast<uint64_t*>(destWords) = field.asInt64();
            break;
          case StubField::Type::Value:
            InitGCPtr<JS::Value>(destWords, field.asInt64());
            break;
          case StubField::Type::Limit:
            MOZ_CRASH("Invalid type");
        }
        destWords += StubField::sizeInBytes(field.type()) / sizeof(uintptr_t);
    }
}

template <typename T>
void
jit::TraceCacheIRStub(JSTracer* trc, T* stub, const CacheIRStubInfo* stubInfo)
{
    uint32_t field = 0;
    size_t offset = 0;
    while (true) {
        StubField::Type fieldType = stubInfo->fieldType(field);
        switch (fieldType) {
          case StubField::Type::RawWord:
          case StubField::Type::RawInt64:
          case StubField::Type::DOMExpandoGeneration:
            break;
          case StubField::Type::Shape:
            TraceNullableEdge(trc, &stubInfo->getStubField<T, Shape*>(stub, offset),
                              "cacheir-shape");
            break;
          case StubField::Type::ObjectGroup:
            TraceNullableEdge(trc, &stubInfo->getStubField<T, ObjectGroup*>(stub, offset),
                              "cacheir-group");
            break;
          case StubField::Type::JSObject:
            TraceNullableEdge(trc, &stubInfo->getStubField<T, JSObject*>(stub, offset),
                              "cacheir-object");
            break;
          case StubField::Type::Symbol:
            TraceNullableEdge(trc, &stubInfo->getStubField<T, JS::Symbol*>(stub, offset),
                              "cacheir-symbol");
            break;
          case StubField::Type::String:
            TraceNullableEdge(trc, &stubInfo->getStubField<T, JSString*>(stub, offset),
                              "cacheir-string");
            break;
          case StubField::Type::Id:
            TraceEdge(trc, &stubInfo->getStubField<T, jsid>(stub, offset), "cacheir-id");
            break;
          case StubField::Type::Value:
            TraceEdge(trc, &stubInfo->getStubField<T, JS::Value>(stub, offset),
                      "cacheir-value");
            break;
          case StubField::Type::Limit:
            return; // Done.
        }
        field++;
        offset += StubField::sizeInBytes(fieldType);
    }
}

template
void jit::TraceCacheIRStub(JSTracer* trc, ICStub* stub, const CacheIRStubInfo* stubInfo);

template
void jit::TraceCacheIRStub(JSTracer* trc, IonICStub* stub, const CacheIRStubInfo* stubInfo);

bool
CacheIRWriter::stubDataEqualsMaybeUpdate(uint8_t* stubData) const
{
    MOZ_ASSERT(!failed());

    const uintptr_t* stubDataWords = reinterpret_cast<const uintptr_t*>(stubData);

    // If DOMExpandoGeneration fields are different but all other stub fields
    // are exactly the same, we overwrite the old stub data instead of attaching
    // a new stub, as the old stub is never going to succeed. This works because
    // even Ion stubs read the DOMExpandoGeneration field from the stub instead
    // of baking it in.
    bool expandoGenerationIsDifferent = false;

    for (const StubField& field : stubFields_) {
        if (field.sizeIsWord()) {
            if (field.asWord() != *stubDataWords)
                return false;
            stubDataWords++;
            continue;
        }

        if (field.asInt64() != *reinterpret_cast<const uint64_t*>(stubDataWords)) {
            if (field.type() != StubField::Type::DOMExpandoGeneration)
                return false;
            expandoGenerationIsDifferent = true;
        }
        stubDataWords += sizeof(uint64_t) / sizeof(uintptr_t);
    }

    if (expandoGenerationIsDifferent)
        copyStubData(stubData);

    return true;
}

HashNumber
CacheIRStubKey::hash(const CacheIRStubKey::Lookup& l)
{
    HashNumber hash = mozilla::HashBytes(l.code, l.length);
    hash = mozilla::AddToHash(hash, uint32_t(l.kind));
    hash = mozilla::AddToHash(hash, uint32_t(l.engine));
    return hash;
}

bool
CacheIRStubKey::match(const CacheIRStubKey& entry, const CacheIRStubKey::Lookup& l)
{
    if (entry.stubInfo->kind() != l.kind)
        return false;

    if (entry.stubInfo->engine() != l.engine)
        return false;

    if (entry.stubInfo->codeLength() != l.length)
        return false;

    if (!mozilla::PodEqual(entry.stubInfo->code(), l.code, l.length))
        return false;

    return true;
}

CacheIRReader::CacheIRReader(const CacheIRStubInfo* stubInfo)
  : CacheIRReader(stubInfo->code(), stubInfo->code() + stubInfo->codeLength())
{}

CacheIRStubInfo*
CacheIRStubInfo::New(CacheKind kind, ICStubEngine engine, bool makesGCCalls,
                     uint32_t stubDataOffset, const CacheIRWriter& writer)
{
    size_t numStubFields = writer.numStubFields();
    size_t bytesNeeded = sizeof(CacheIRStubInfo) +
                         writer.codeLength() +
                         (numStubFields + 1); // +1 for the GCType::Limit terminator.
    uint8_t* p = js_pod_malloc<uint8_t>(bytesNeeded);
    if (!p)
        return nullptr;

    // Copy the CacheIR code.
    uint8_t* codeStart = p + sizeof(CacheIRStubInfo);
    mozilla::PodCopy(codeStart, writer.codeStart(), writer.codeLength());

    static_assert(sizeof(StubField::Type) == sizeof(uint8_t),
                  "StubField::Type must fit in uint8_t");

    // Copy the stub field types.
    uint8_t* fieldTypes = codeStart + writer.codeLength();
    for (size_t i = 0; i < numStubFields; i++)
        fieldTypes[i] = uint8_t(writer.stubFieldType(i));
    fieldTypes[numStubFields] = uint8_t(StubField::Type::Limit);

    return new(p) CacheIRStubInfo(kind, engine, makesGCCalls, stubDataOffset, codeStart,
                                  writer.codeLength(), fieldTypes);
}

bool
OperandLocation::operator==(const OperandLocation& other) const
{
    if (kind_ != other.kind_)
        return false;

    switch (kind()) {
      case Uninitialized:
        return true;
      case PayloadReg:
        return payloadReg() == other.payloadReg() && payloadType() == other.payloadType();
      case ValueReg:
        return valueReg() == other.valueReg();
      case PayloadStack:
        return payloadStack() == other.payloadStack() && payloadType() == other.payloadType();
      case ValueStack:
        return valueStack() == other.valueStack();
      case Constant:
        return constant() == other.constant();
    }

    MOZ_CRASH("Invalid OperandLocation kind");
}

AutoOutputRegister::AutoOutputRegister(CacheIRCompiler& compiler)
  : output_(compiler.outputUnchecked_.ref()),
    alloc_(compiler.allocator)
{
    if (output_.hasValue())
        alloc_.allocateFixedValueRegister(compiler.masm, output_.valueReg());
    else if (!output_.typedReg().isFloat())
        alloc_.allocateFixedRegister(compiler.masm, output_.typedReg().gpr());
}

AutoOutputRegister::~AutoOutputRegister()
{
    if (output_.hasValue())
        alloc_.releaseValueRegister(output_.valueReg());
    else if (!output_.typedReg().isFloat())
        alloc_.releaseRegister(output_.typedReg().gpr());
}

bool
FailurePath::canShareFailurePath(const FailurePath& other) const
{
    if (stackPushed_ != other.stackPushed_)
        return false;

    if (spilledRegs_.length() != other.spilledRegs_.length())
        return false;

    for (size_t i = 0; i < spilledRegs_.length(); i++) {
        if (spilledRegs_[i] != other.spilledRegs_[i])
	    return false;
    }

    MOZ_ASSERT(inputs_.length() == other.inputs_.length());

    for (size_t i = 0; i < inputs_.length(); i++) {
        if (inputs_[i] != other.inputs_[i])
	    return false;
    }
    return true;
}

bool
CacheIRCompiler::addFailurePath(FailurePath** failure)
{
    FailurePath newFailure;
    for (size_t i = 0; i < writer_.numInputOperands(); i++) {
        if (!newFailure.appendInput(allocator.operandLocation(i)))
            return false;
    }
    if (!newFailure.setSpilledRegs(allocator.spilledRegs()))
        return false;
    newFailure.setStackPushed(allocator.stackPushed());

    // Reuse the previous failure path if the current one is the same, to
    // avoid emitting duplicate code.
    if (failurePaths.length() > 0 && failurePaths.back().canShareFailurePath(newFailure)) {
        *failure = &failurePaths.back();
        return true;
    }

    if (!failurePaths.append(Move(newFailure)))
        return false;

    *failure = &failurePaths.back();
    return true;
}

bool
CacheIRCompiler::emitFailurePath(size_t index)
{
    FailurePath& failure = failurePaths[index];

    allocator.setStackPushed(failure.stackPushed());

    for (size_t i = 0; i < writer_.numInputOperands(); i++)
        allocator.setOperandLocation(i, failure.input(i));

    if (!allocator.setSpilledRegs(failure.spilledRegs()))
        return false;

    masm.bind(failure.label());
    allocator.restoreInputState(masm);
    return true;
}

bool
CacheIRCompiler::emitGuardIsObject()
{
    ValOperandId inputId = reader.valOperandId();
    if (allocator.knownType(inputId) == JSVAL_TYPE_OBJECT)
        return true;

    ValueOperand input = allocator.useValueRegister(masm, inputId);
    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;
    masm.branchTestObject(Assembler::NotEqual, input, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardIsObjectOrNull()
{
    ValOperandId inputId = reader.valOperandId();
    JSValueType knownType = allocator.knownType(inputId);
    if (knownType == JSVAL_TYPE_OBJECT || knownType == JSVAL_TYPE_NULL)
        return true;

    ValueOperand input = allocator.useValueRegister(masm, inputId);
    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    Label done;
    masm.branchTestObject(Assembler::Equal, input, &done);
    masm.branchTestNull(Assembler::NotEqual, input, failure->label());
    masm.bind(&done);
    return true;
}

bool
CacheIRCompiler::emitGuardIsString()
{
    ValOperandId inputId = reader.valOperandId();
    if (allocator.knownType(inputId) == JSVAL_TYPE_STRING)
        return true;

    ValueOperand input = allocator.useValueRegister(masm, inputId);
    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;
    masm.branchTestString(Assembler::NotEqual, input, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardIsSymbol()
{
    ValOperandId inputId = reader.valOperandId();
    if (allocator.knownType(inputId) == JSVAL_TYPE_SYMBOL)
        return true;

    ValueOperand input = allocator.useValueRegister(masm, inputId);
    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;
    masm.branchTestSymbol(Assembler::NotEqual, input, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardIsInt32Index()
{
    ValOperandId inputId = reader.valOperandId();
    Register output = allocator.defineRegister(masm, reader.int32OperandId());

    if (allocator.knownType(inputId) == JSVAL_TYPE_INT32) {
        Register input = allocator.useRegister(masm, Int32OperandId(inputId.id()));
        masm.move32(input, output);
        return true;
    }

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    ValueOperand input = allocator.useValueRegister(masm, inputId);

    Label notInt32, done;
    masm.branchTestInt32(Assembler::NotEqual, input, &notInt32);
    masm.unboxInt32(input, output);
    masm.jump(&done);

    masm.bind(&notInt32);

    if (cx_->runtime()->jitSupportsFloatingPoint) {
        masm.branchTestDouble(Assembler::NotEqual, input, failure->label());

        // If we're compiling a Baseline IC, FloatReg0 is always available.
        Label failurePopReg;
        if (mode_ != Mode::Baseline)
            masm.push(FloatReg0);

        masm.unboxDouble(input, FloatReg0);
        // ToPropertyKey(-0.0) is "0", so we can truncate -0.0 to 0 here.
        masm.convertDoubleToInt32(FloatReg0, output,
                                  (mode_ == Mode::Baseline) ? failure->label() : &failurePopReg,
                                  false);
        if (mode_ != Mode::Baseline) {
            masm.pop(FloatReg0);
            masm.jump(&done);

            masm.bind(&failurePopReg);
            masm.pop(FloatReg0);
            masm.jump(failure->label());
        }
    } else {
        masm.jump(failure->label());
    }

    masm.bind(&done);
    return true;
}

bool
CacheIRCompiler::emitGuardType()
{
    ValOperandId inputId = reader.valOperandId();
    JSValueType type = reader.valueType();

    if (allocator.knownType(inputId) == type)
        return true;

    ValueOperand input = allocator.useValueRegister(masm, inputId);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    switch (type) {
      case JSVAL_TYPE_STRING:
        masm.branchTestString(Assembler::NotEqual, input, failure->label());
        break;
      case JSVAL_TYPE_SYMBOL:
        masm.branchTestSymbol(Assembler::NotEqual, input, failure->label());
        break;
      case JSVAL_TYPE_INT32:
        masm.branchTestInt32(Assembler::NotEqual, input, failure->label());
        break;
      case JSVAL_TYPE_DOUBLE:
        masm.branchTestNumber(Assembler::NotEqual, input, failure->label());
        break;
      case JSVAL_TYPE_BOOLEAN:
        masm.branchTestBoolean(Assembler::NotEqual, input, failure->label());
        break;
      case JSVAL_TYPE_UNDEFINED:
        masm.branchTestUndefined(Assembler::NotEqual, input, failure->label());
        break;
      default:
        MOZ_CRASH("Unexpected type");
    }

    return true;
}

bool
CacheIRCompiler::emitGuardClass()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    AutoScratchRegister scratch(allocator, masm);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    const Class* clasp = nullptr;
    switch (reader.guardClassKind()) {
      case GuardClassKind::Array:
        clasp = &ArrayObject::class_;
        break;
      case GuardClassKind::UnboxedArray:
        clasp = &UnboxedArrayObject::class_;
        break;
      case GuardClassKind::MappedArguments:
        clasp = &MappedArgumentsObject::class_;
        break;
      case GuardClassKind::UnmappedArguments:
        clasp = &UnmappedArgumentsObject::class_;
        break;
      case GuardClassKind::WindowProxy:
        clasp = cx_->maybeWindowProxyClass();
        break;
    }

    MOZ_ASSERT(clasp);
    masm.branchTestObjClass(Assembler::NotEqual, obj, scratch, clasp, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardIsProxy()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    AutoScratchRegister scratch(allocator, masm);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    masm.branchTestObjectIsProxy(false, obj, scratch, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardNotDOMProxy()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    AutoScratchRegister scratch(allocator, masm);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    masm.branchTestProxyHandlerFamily(Assembler::Equal, obj, scratch,
                                      GetDOMProxyHandlerFamily(), failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardMagicValue()
{
    ValueOperand val = allocator.useValueRegister(masm, reader.valOperandId());
    JSWhyMagic magic = reader.whyMagic();

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    masm.branchTestMagicValue(Assembler::NotEqual, val, magic, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardNoUnboxedExpando()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    Address expandoAddr(obj, UnboxedPlainObject::offsetOfExpando());
    masm.branchPtr(Assembler::NotEqual, expandoAddr, ImmWord(0), failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardAndLoadUnboxedExpando()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register output = allocator.defineRegister(masm, reader.objOperandId());

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    Address expandoAddr(obj, UnboxedPlainObject::offsetOfExpando());
    masm.loadPtr(expandoAddr, output);
    masm.branchTestPtr(Assembler::Zero, output, output, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardNoDetachedTypedObjects()
{
    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    CheckForTypedObjectWithDetachedStorage(cx_, masm, failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardNoDenseElements()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    AutoScratchRegister scratch(allocator, masm);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    // Load obj->elements.
    masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);

    // Make sure there are no dense elements.
    Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
    masm.branch32(Assembler::NotEqual, initLength, Imm32(0), failure->label());
    return true;
}

bool
CacheIRCompiler::emitGuardAndGetIndexFromString()
{
    Register str = allocator.useRegister(masm, reader.stringOperandId());
    Register output = allocator.defineRegister(masm, reader.int32OperandId());

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    AllocatableRegisterSet regs(RegisterSet::Volatile());
    LiveRegisterSet save(regs.asLiveSet());
    masm.PushRegsInMask(save);
    regs.takeUnchecked(str);

    masm.setupUnalignedABICall(output);
    masm.passABIArg(str);
    masm.callWithABI(JS_FUNC_TO_DATA_PTR(void*, GetIndexFromString));
    masm.mov(ReturnReg, output);

    LiveRegisterSet ignore;
    ignore.add(output);
    masm.PopRegsInMaskIgnore(save, ignore);

    // GetIndexFromString returns a negative value on failure.
    masm.branchTest32(Assembler::Signed, output, output, failure->label());
    return true;
}

bool
CacheIRCompiler::emitLoadProto()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register reg = allocator.defineRegister(masm, reader.objOperandId());
    masm.loadObjProto(obj, reg);
    return true;
}

bool
CacheIRCompiler::emitLoadEnclosingEnvironment()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register reg = allocator.defineRegister(masm, reader.objOperandId());
    masm.extractObject(Address(obj, EnvironmentObject::offsetOfEnclosingEnvironment()), reg);
    return true;
}

bool
CacheIRCompiler::emitLoadDOMExpandoValue()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    ValueOperand val = allocator.defineValueRegister(masm, reader.valOperandId());

    masm.loadPtr(Address(obj, ProxyObject::offsetOfValues()), val.scratchReg());
    masm.loadValue(Address(val.scratchReg(),
                           ProxyObject::offsetOfExtraSlotInValues(GetDOMProxyExpandoSlot())),
                   val);
    return true;
}

bool
CacheIRCompiler::emitLoadDOMExpandoValueIgnoreGeneration()
{
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    ValueOperand output = allocator.defineValueRegister(masm, reader.valOperandId());

    // Determine the expando's Address.
    Register scratch = output.scratchReg();
    masm.loadPtr(Address(obj, ProxyObject::offsetOfValues()), scratch);
    Address expandoAddr(scratch, ProxyObject::offsetOfExtraSlotInValues(GetDOMProxyExpandoSlot()));

#ifdef DEBUG
    // Private values are stored as doubles, so assert we have a double.
    Label ok;
    masm.branchTestDouble(Assembler::Equal, expandoAddr, &ok);
    masm.assumeUnreachable("DOM expando is not a PrivateValue!");
    masm.bind(&ok);
#endif

    // Load the ExpandoAndGeneration* from the PrivateValue.
    masm.loadPrivate(expandoAddr, scratch);

    // Load expandoAndGeneration->expando into the output Value register.
    masm.loadValue(Address(scratch, ExpandoAndGeneration::offsetOfExpando()), output);
    return true;
}

bool
CacheIRCompiler::emitLoadUndefinedResult()
{
    AutoOutputRegister output(*this);
    if (output.hasValue())
        masm.moveValue(UndefinedValue(), output.valueReg());
    else
        masm.assumeUnreachable("Should have monitored undefined result");
    return true;
}

static void
EmitStoreResult(MacroAssembler& masm, Register reg, JSValueType type,
                const AutoOutputRegister& output)
{
    if (output.hasValue()) {
        masm.tagValue(type, reg, output.valueReg());
        return;
    }
    if (type == JSVAL_TYPE_INT32 && output.typedReg().isFloat()) {
        masm.convertInt32ToDouble(reg, output.typedReg().fpu());
        return;
    }
    if (type == output.type()) {
        masm.mov(reg, output.typedReg().gpr());
        return;
    }
    masm.assumeUnreachable("Should have monitored result");
}

bool
CacheIRCompiler::emitLoadInt32ArrayLengthResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
    masm.load32(Address(scratch, ObjectElements::offsetOfLength()), scratch);

    // Guard length fits in an int32.
    masm.branchTest32(Assembler::Signed, scratch, scratch, failure->label());
    EmitStoreResult(masm, scratch, JSVAL_TYPE_INT32, output);
    return true;
}

bool
CacheIRCompiler::emitLoadUnboxedArrayLengthResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    masm.load32(Address(obj, UnboxedArrayObject::offsetOfLength()), scratch);
    EmitStoreResult(masm, scratch, JSVAL_TYPE_INT32, output);
    return true;
}

bool
CacheIRCompiler::emitLoadArgumentsObjectLengthResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    // Get initial length value.
    masm.unboxInt32(Address(obj, ArgumentsObject::getInitialLengthSlotOffset()), scratch);

    // Test if length has been overridden.
    masm.branchTest32(Assembler::NonZero,
                      scratch,
                      Imm32(ArgumentsObject::LENGTH_OVERRIDDEN_BIT),
                      failure->label());

    // Shift out arguments length and return it. No need to type monitor
    // because this stub always returns int32.
    masm.rshiftPtr(Imm32(ArgumentsObject::PACKED_BITS_COUNT), scratch);
    EmitStoreResult(masm, scratch, JSVAL_TYPE_INT32, output);
    return true;
}

bool
CacheIRCompiler::emitLoadStringLengthResult()
{
    AutoOutputRegister output(*this);
    Register str = allocator.useRegister(masm, reader.stringOperandId());
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    masm.loadStringLength(str, scratch);
    EmitStoreResult(masm, scratch, JSVAL_TYPE_INT32, output);
    return true;
}

bool
CacheIRCompiler::emitLoadStringCharResult()
{
    AutoOutputRegister output(*this);
    Register str = allocator.useRegister(masm, reader.stringOperandId());
    Register index = allocator.useRegister(masm, reader.int32OperandId());
    AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
    AutoScratchRegister scratch2(allocator, masm);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    masm.branchIfRope(str, failure->label());

    // Bounds check, load string char.
    masm.branch32(Assembler::BelowOrEqual, Address(str, JSString::offsetOfLength()),
                  index, failure->label());
    masm.loadStringChar(str, index, scratch1);

    // Load StaticString for this char.
    masm.branch32(Assembler::AboveOrEqual, scratch1, Imm32(StaticStrings::UNIT_STATIC_LIMIT),
                  failure->label());
    masm.movePtr(ImmPtr(&cx_->staticStrings().unitStaticTable), scratch2);
    masm.loadPtr(BaseIndex(scratch2, scratch1, ScalePointer), scratch2);

    EmitStoreResult(masm, scratch2, JSVAL_TYPE_STRING, output);
    return true;
}

bool
CacheIRCompiler::emitLoadArgumentsObjectArgResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register index = allocator.useRegister(masm, reader.int32OperandId());
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    // Get initial length value.
    masm.unboxInt32(Address(obj, ArgumentsObject::getInitialLengthSlotOffset()), scratch);

    // Ensure no overridden length/element.
    masm.branchTest32(Assembler::NonZero,
                      scratch,
                      Imm32(ArgumentsObject::LENGTH_OVERRIDDEN_BIT |
                            ArgumentsObject::ELEMENT_OVERRIDDEN_BIT),
                      failure->label());

    // Bounds check.
    masm.rshift32(Imm32(ArgumentsObject::PACKED_BITS_COUNT), scratch);
    masm.branch32(Assembler::AboveOrEqual, index, scratch, failure->label());

    // Load ArgumentsData.
    masm.loadPrivate(Address(obj, ArgumentsObject::getDataSlotOffset()), scratch);

    // Fail if we have a RareArgumentsData (elements were deleted).
    masm.branchPtr(Assembler::NotEqual,
                   Address(scratch, offsetof(ArgumentsData, rareData)),
                   ImmWord(0),
                   failure->label());

    // Guard the argument is not a FORWARD_TO_CALL_SLOT MagicValue.
    BaseValueIndex argValue(scratch, index, ArgumentsData::offsetOfArgs());
    masm.branchTestMagic(Assembler::Equal, argValue, failure->label());
    masm.loadValue(argValue, output.valueReg());
    return true;
}

bool
CacheIRCompiler::emitLoadDenseElementResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register index = allocator.useRegister(masm, reader.int32OperandId());
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    // Load obj->elements.
    masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);

    // Bounds check.
    Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
    masm.branch32(Assembler::BelowOrEqual, initLength, index, failure->label());

    // Hole check.
    BaseObjectElementIndex element(scratch, index);
    masm.branchTestMagic(Assembler::Equal, element, failure->label());
    masm.loadTypedOrValue(element, output);
    return true;
}

bool
CacheIRCompiler::emitLoadDenseElementHoleResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register index = allocator.useRegister(masm, reader.int32OperandId());
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    if (!output.hasValue()) {
        masm.assumeUnreachable("Should have monitored undefined value after attaching stub");
        return true;
    }

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    // Make sure the index is nonnegative.
    masm.branch32(Assembler::LessThan, index, Imm32(0), failure->label());

    // Load obj->elements.
    masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);

    // Guard on the initialized length.
    Label hole;
    Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
    masm.branch32(Assembler::BelowOrEqual, initLength, index, &hole);

    // Load the value.
    Label done;
    masm.loadValue(BaseObjectElementIndex(scratch, index), output.valueReg());
    masm.branchTestMagic(Assembler::NotEqual, output.valueReg(), &done);

    // Load undefined for the hole.
    masm.bind(&hole);
    masm.moveValue(UndefinedValue(), output.valueReg());

    masm.bind(&done);
    return true;
}

bool
CacheIRCompiler::emitLoadUnboxedArrayElementResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register index = allocator.useRegister(masm, reader.int32OperandId());
    JSValueType elementType = reader.valueType();
    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    if (!output.hasValue() &&
        elementType != output.type() &&
        !(elementType == JSVAL_TYPE_INT32 && output.type() == JSVAL_TYPE_DOUBLE))
    {
        masm.assumeUnreachable("Should have monitored unboxed property type");
        return true;
    }

    // Bounds check.
    masm.load32(Address(obj, UnboxedArrayObject::offsetOfCapacityIndexAndInitializedLength()),
                scratch);
    masm.and32(Imm32(UnboxedArrayObject::InitializedLengthMask), scratch);
    masm.branch32(Assembler::BelowOrEqual, scratch, index, failure->label());

    // Load obj->elements.
    masm.loadPtr(Address(obj, UnboxedArrayObject::offsetOfElements()), scratch);

    // Load value.
    size_t width = UnboxedTypeSize(elementType);
    BaseIndex addr(scratch, index, ScaleFromElemWidth(width));
    masm.loadUnboxedProperty(addr, elementType, output);
    return true;
}

bool
CacheIRCompiler::emitLoadTypedElementResult()
{
    AutoOutputRegister output(*this);
    Register obj = allocator.useRegister(masm, reader.objOperandId());
    Register index = allocator.useRegister(masm, reader.int32OperandId());
    TypedThingLayout layout = reader.typedThingLayout();
    Scalar::Type type = reader.scalarType();

    AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);

    if (!output.hasValue()) {
        if (type == Scalar::Float32 || type == Scalar::Float64) {
            if (output.type() != JSVAL_TYPE_DOUBLE) {
                masm.assumeUnreachable("Should have monitored double after attaching stub");
                return true;
            }
        } else {
            if (output.type() != JSVAL_TYPE_INT32 && output.type() != JSVAL_TYPE_DOUBLE) {
                masm.assumeUnreachable("Should have monitored int32 after attaching stub");
                return true;
            }
        }
    }

    FailurePath* failure;
    if (!addFailurePath(&failure))
        return false;

    // Bounds check.
    LoadTypedThingLength(masm, layout, obj, scratch);
    masm.branch32(Assembler::BelowOrEqual, scratch, index, failure->label());

    // Load the elements vector.
    LoadTypedThingData(masm, layout, obj, scratch);

    // Load the value.
    BaseIndex source(scratch, index, ScaleFromElemWidth(Scalar::byteSize(type)));
    if (output.hasValue()) {
        masm.loadFromTypedArray(type, source, output.valueReg(), *allowDoubleResult_, scratch,
                                failure->label());
    } else {
        bool needGpr = (type == Scalar::Int8 || type == Scalar::Uint8 ||
                        type == Scalar::Int16 || type == Scalar::Uint16 ||
                        type == Scalar::Uint8Clamped || type == Scalar::Int32);
        if (needGpr && output.type() == JSVAL_TYPE_DOUBLE) {
            // Load the element as integer, then convert it to double.
            masm.loadFromTypedArray(type, source, AnyRegister(scratch), scratch, failure->label());
            masm.convertInt32ToDouble(source, output.typedReg().fpu());
        } else {
            masm.loadFromTypedArray(type, source, output.typedReg(), scratch, failure->label());
        }
    }
    return true;
}

void
CacheIRCompiler::emitLoadTypedObjectResultShared(const Address& fieldAddr, Register scratch,
                                                 TypedThingLayout layout, uint32_t typeDescr,
                                                 const AutoOutputRegister& output)
{
    MOZ_ASSERT(output.hasValue());

    if (SimpleTypeDescrKeyIsScalar(typeDescr)) {
        Scalar::Type type = ScalarTypeFromSimpleTypeDescrKey(typeDescr);
        masm.loadFromTypedArray(type, fieldAddr, output.valueReg(),
                                /* allowDouble = */ true, scratch, nullptr);
    } else {
        ReferenceTypeDescr::Type type = ReferenceTypeFromSimpleTypeDescrKey(typeDescr);
        switch (type) {
          case ReferenceTypeDescr::TYPE_ANY:
            masm.loadValue(fieldAddr, output.valueReg());
            break;

          case ReferenceTypeDescr::TYPE_OBJECT: {
            Label notNull, done;
            masm.loadPtr(fieldAddr, scratch);
            masm.branchTestPtr(Assembler::NonZero, scratch, scratch, &notNull);
            masm.moveValue(NullValue(), output.valueReg());
            masm.jump(&done);
            masm.bind(&notNull);
            masm.tagValue(JSVAL_TYPE_OBJECT, scratch, output.valueReg());
            masm.bind(&done);
            break;
          }

          case ReferenceTypeDescr::TYPE_STRING:
            masm.loadPtr(fieldAddr, scratch);
            masm.tagValue(JSVAL_TYPE_STRING, scratch, output.valueReg());
            break;

          default:
            MOZ_CRASH("Invalid ReferenceTypeDescr");
        }
    }
}