llvm.org GIT mirror llvm / e072ed7 lib / Target / ARM64 / ARM64InstrInfo.cpp
e072ed7

Tree @e072ed7 (Download .tar.gz)

ARM64InstrInfo.cpp @e072ed7raw · history · blame

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
//===- ARM64InstrInfo.cpp - ARM64 Instruction Information -----------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the ARM64 implementation of the TargetInstrInfo class.
//
//===----------------------------------------------------------------------===//

#include "ARM64InstrInfo.h"
#include "ARM64Subtarget.h"
#include "MCTargetDesc/ARM64AddressingModes.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/PseudoSourceValue.h"
#include "llvm/MC/MCInst.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TargetRegistry.h"

using namespace llvm;

#define GET_INSTRINFO_CTOR_DTOR
#include "ARM64GenInstrInfo.inc"

ARM64InstrInfo::ARM64InstrInfo(const ARM64Subtarget &STI)
    : ARM64GenInstrInfo(ARM64::ADJCALLSTACKDOWN, ARM64::ADJCALLSTACKUP),
      RI(this, &STI), Subtarget(STI) {}

/// GetInstSize - Return the number of bytes of code the specified
/// instruction may be.  This returns the maximum number of bytes.
unsigned ARM64InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
  const MCInstrDesc &Desc = MI->getDesc();

  switch (Desc.getOpcode()) {
  default:
    // Anything not explicitly designated otherwise is a nomal 4-byte insn.
    return 4;
  case TargetOpcode::DBG_VALUE:
  case TargetOpcode::EH_LABEL:
  case TargetOpcode::IMPLICIT_DEF:
  case TargetOpcode::KILL:
    return 0;
  }

  llvm_unreachable("GetInstSizeInBytes()- Unable to determin insn size");
}

static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target,
                            SmallVectorImpl<MachineOperand> &Cond) {
  // Block ends with fall-through condbranch.
  switch (LastInst->getOpcode()) {
  default:
    llvm_unreachable("Unknown branch instruction?");
  case ARM64::Bcc:
    Target = LastInst->getOperand(1).getMBB();
    Cond.push_back(LastInst->getOperand(0));
    break;
  case ARM64::CBZW:
  case ARM64::CBZX:
  case ARM64::CBNZW:
  case ARM64::CBNZX:
    Target = LastInst->getOperand(1).getMBB();
    Cond.push_back(MachineOperand::CreateImm(-1));
    Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
    Cond.push_back(LastInst->getOperand(0));
    break;
  case ARM64::TBZW:
  case ARM64::TBZX:
  case ARM64::TBNZW:
  case ARM64::TBNZX:
    Target = LastInst->getOperand(2).getMBB();
    Cond.push_back(MachineOperand::CreateImm(-1));
    Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
    Cond.push_back(LastInst->getOperand(0));
    Cond.push_back(LastInst->getOperand(1));
  }
}

// Branch analysis.
bool ARM64InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
                                   MachineBasicBlock *&TBB,
                                   MachineBasicBlock *&FBB,
                                   SmallVectorImpl<MachineOperand> &Cond,
                                   bool AllowModify) const {
  // If the block has no terminators, it just falls into the block after it.
  MachineBasicBlock::iterator I = MBB.end();
  if (I == MBB.begin())
    return false;
  --I;
  while (I->isDebugValue()) {
    if (I == MBB.begin())
      return false;
    --I;
  }
  if (!isUnpredicatedTerminator(I))
    return false;

  // Get the last instruction in the block.
  MachineInstr *LastInst = I;

  // If there is only one terminator instruction, process it.
  unsigned LastOpc = LastInst->getOpcode();
  if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
    if (isUncondBranchOpcode(LastOpc)) {
      TBB = LastInst->getOperand(0).getMBB();
      return false;
    }
    if (isCondBranchOpcode(LastOpc)) {
      // Block ends with fall-through condbranch.
      parseCondBranch(LastInst, TBB, Cond);
      return false;
    }
    return true; // Can't handle indirect branch.
  }

  // Get the instruction before it if it is a terminator.
  MachineInstr *SecondLastInst = I;
  unsigned SecondLastOpc = SecondLastInst->getOpcode();

  // If AllowModify is true and the block ends with two or more unconditional
  // branches, delete all but the first unconditional branch.
  if (AllowModify && isUncondBranchOpcode(LastOpc)) {
    while (isUncondBranchOpcode(SecondLastOpc)) {
      LastInst->eraseFromParent();
      LastInst = SecondLastInst;
      LastOpc = LastInst->getOpcode();
      if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
        // Return now the only terminator is an unconditional branch.
        TBB = LastInst->getOperand(0).getMBB();
        return false;
      } else {
        SecondLastInst = I;
        SecondLastOpc = SecondLastInst->getOpcode();
      }
    }
  }

  // If there are three terminators, we don't know what sort of block this is.
  if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
    return true;

  // If the block ends with a B and a Bcc, handle it.
  if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
    parseCondBranch(SecondLastInst, TBB, Cond);
    FBB = LastInst->getOperand(0).getMBB();
    return false;
  }

  // If the block ends with two unconditional branches, handle it.  The second
  // one is not executed, so remove it.
  if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
    TBB = SecondLastInst->getOperand(0).getMBB();
    I = LastInst;
    if (AllowModify)
      I->eraseFromParent();
    return false;
  }

  // ...likewise if it ends with an indirect branch followed by an unconditional
  // branch.
  if (isIndirectBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
    I = LastInst;
    if (AllowModify)
      I->eraseFromParent();
    return true;
  }

  // Otherwise, can't handle this.
  return true;
}

bool ARM64InstrInfo::ReverseBranchCondition(
    SmallVectorImpl<MachineOperand> &Cond) const {
  if (Cond[0].getImm() != -1) {
    // Regular Bcc
    ARM64CC::CondCode CC = (ARM64CC::CondCode)(int)Cond[0].getImm();
    Cond[0].setImm(ARM64CC::getInvertedCondCode(CC));
  } else {
    // Folded compare-and-branch
    switch (Cond[1].getImm()) {
    default:
      llvm_unreachable("Unknown conditional branch!");
    case ARM64::CBZW:
      Cond[1].setImm(ARM64::CBNZW);
      break;
    case ARM64::CBNZW:
      Cond[1].setImm(ARM64::CBZW);
      break;
    case ARM64::CBZX:
      Cond[1].setImm(ARM64::CBNZX);
      break;
    case ARM64::CBNZX:
      Cond[1].setImm(ARM64::CBZX);
      break;
    case ARM64::TBZW:
      Cond[1].setImm(ARM64::TBNZW);
      break;
    case ARM64::TBNZW:
      Cond[1].setImm(ARM64::TBZW);
      break;
    case ARM64::TBZX:
      Cond[1].setImm(ARM64::TBNZX);
      break;
    case ARM64::TBNZX:
      Cond[1].setImm(ARM64::TBZX);
      break;
    }
  }

  return false;
}

unsigned ARM64InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
  MachineBasicBlock::iterator I = MBB.end();
  if (I == MBB.begin())
    return 0;
  --I;
  while (I->isDebugValue()) {
    if (I == MBB.begin())
      return 0;
    --I;
  }
  if (!isUncondBranchOpcode(I->getOpcode()) &&
      !isCondBranchOpcode(I->getOpcode()))
    return 0;

  // Remove the branch.
  I->eraseFromParent();

  I = MBB.end();

  if (I == MBB.begin())
    return 1;
  --I;
  if (!isCondBranchOpcode(I->getOpcode()))
    return 1;

  // Remove the branch.
  I->eraseFromParent();
  return 2;
}

void ARM64InstrInfo::instantiateCondBranch(
    MachineBasicBlock &MBB, DebugLoc DL, MachineBasicBlock *TBB,
    const SmallVectorImpl<MachineOperand> &Cond) const {
  if (Cond[0].getImm() != -1) {
    // Regular Bcc
    BuildMI(&MBB, DL, get(ARM64::Bcc)).addImm(Cond[0].getImm()).addMBB(TBB);
  } else {
    // Folded compare-and-branch
    const MachineInstrBuilder MIB =
        BuildMI(&MBB, DL, get(Cond[1].getImm())).addReg(Cond[2].getReg());
    if (Cond.size() > 3)
      MIB.addImm(Cond[3].getImm());
    MIB.addMBB(TBB);
  }
}

unsigned ARM64InstrInfo::InsertBranch(
    MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
    const SmallVectorImpl<MachineOperand> &Cond, DebugLoc DL) const {
  // Shouldn't be a fall through.
  assert(TBB && "InsertBranch must not be told to insert a fallthrough");

  if (!FBB) {
    if (Cond.empty()) // Unconditional branch?
      BuildMI(&MBB, DL, get(ARM64::B)).addMBB(TBB);
    else
      instantiateCondBranch(MBB, DL, TBB, Cond);
    return 1;
  }

  // Two-way conditional branch.
  instantiateCondBranch(MBB, DL, TBB, Cond);
  BuildMI(&MBB, DL, get(ARM64::B)).addMBB(FBB);
  return 2;
}

// Find the original register that VReg is copied from.
static unsigned removeCopies(const MachineRegisterInfo &MRI, unsigned VReg) {
  while (TargetRegisterInfo::isVirtualRegister(VReg)) {
    const MachineInstr *DefMI = MRI.getVRegDef(VReg);
    if (!DefMI->isFullCopy())
      return VReg;
    VReg = DefMI->getOperand(1).getReg();
  }
  return VReg;
}

// Determine if VReg is defined by an instruction that can be folded into a
// csel instruction. If so, return the folded opcode, and the replacement
// register.
static unsigned canFoldIntoCSel(const MachineRegisterInfo &MRI, unsigned VReg,
                                unsigned *NewVReg = nullptr) {
  VReg = removeCopies(MRI, VReg);
  if (!TargetRegisterInfo::isVirtualRegister(VReg))
    return 0;

  bool Is64Bit = ARM64::GPR64allRegClass.hasSubClassEq(MRI.getRegClass(VReg));
  const MachineInstr *DefMI = MRI.getVRegDef(VReg);
  unsigned Opc = 0;
  unsigned SrcOpNum = 0;
  switch (DefMI->getOpcode()) {
  case ARM64::ADDSXri:
  case ARM64::ADDSWri:
    // if NZCV is used, do not fold.
    if (DefMI->findRegisterDefOperandIdx(ARM64::NZCV, true) == -1)
      return 0;
  // fall-through to ADDXri and ADDWri.
  case ARM64::ADDXri:
  case ARM64::ADDWri:
    // add x, 1 -> csinc.
    if (!DefMI->getOperand(2).isImm() || DefMI->getOperand(2).getImm() != 1 ||
        DefMI->getOperand(3).getImm() != 0)
      return 0;
    SrcOpNum = 1;
    Opc = Is64Bit ? ARM64::CSINCXr : ARM64::CSINCWr;
    break;

  case ARM64::ORNXrr:
  case ARM64::ORNWrr: {
    // not x -> csinv, represented as orn dst, xzr, src.
    unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
    if (ZReg != ARM64::XZR && ZReg != ARM64::WZR)
      return 0;
    SrcOpNum = 2;
    Opc = Is64Bit ? ARM64::CSINVXr : ARM64::CSINVWr;
    break;
  }

  case ARM64::SUBSXrr:
  case ARM64::SUBSWrr:
    // if NZCV is used, do not fold.
    if (DefMI->findRegisterDefOperandIdx(ARM64::NZCV, true) == -1)
      return 0;
  // fall-through to SUBXrr and SUBWrr.
  case ARM64::SUBXrr:
  case ARM64::SUBWrr: {
    // neg x -> csneg, represented as sub dst, xzr, src.
    unsigned ZReg = removeCopies(MRI, DefMI->getOperand(1).getReg());
    if (ZReg != ARM64::XZR && ZReg != ARM64::WZR)
      return 0;
    SrcOpNum = 2;
    Opc = Is64Bit ? ARM64::CSNEGXr : ARM64::CSNEGWr;
    break;
  }
  default:
    return 0;
  }
  assert(Opc && SrcOpNum && "Missing parameters");

  if (NewVReg)
    *NewVReg = DefMI->getOperand(SrcOpNum).getReg();
  return Opc;
}

bool ARM64InstrInfo::canInsertSelect(
    const MachineBasicBlock &MBB, const SmallVectorImpl<MachineOperand> &Cond,
    unsigned TrueReg, unsigned FalseReg, int &CondCycles, int &TrueCycles,
    int &FalseCycles) const {
  // Check register classes.
  const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
  const TargetRegisterClass *RC =
      RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
  if (!RC)
    return false;

  // Expanding cbz/tbz requires an extra cycle of latency on the condition.
  unsigned ExtraCondLat = Cond.size() != 1;

  // GPRs are handled by csel.
  // FIXME: Fold in x+1, -x, and ~x when applicable.
  if (ARM64::GPR64allRegClass.hasSubClassEq(RC) ||
      ARM64::GPR32allRegClass.hasSubClassEq(RC)) {
    // Single-cycle csel, csinc, csinv, and csneg.
    CondCycles = 1 + ExtraCondLat;
    TrueCycles = FalseCycles = 1;
    if (canFoldIntoCSel(MRI, TrueReg))
      TrueCycles = 0;
    else if (canFoldIntoCSel(MRI, FalseReg))
      FalseCycles = 0;
    return true;
  }

  // Scalar floating point is handled by fcsel.
  // FIXME: Form fabs, fmin, and fmax when applicable.
  if (ARM64::FPR64RegClass.hasSubClassEq(RC) ||
      ARM64::FPR32RegClass.hasSubClassEq(RC)) {
    CondCycles = 5 + ExtraCondLat;
    TrueCycles = FalseCycles = 2;
    return true;
  }

  // Can't do vectors.
  return false;
}

void ARM64InstrInfo::insertSelect(MachineBasicBlock &MBB,
                                  MachineBasicBlock::iterator I, DebugLoc DL,
                                  unsigned DstReg,
                                  const SmallVectorImpl<MachineOperand> &Cond,
                                  unsigned TrueReg, unsigned FalseReg) const {
  MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();

  // Parse the condition code, see parseCondBranch() above.
  ARM64CC::CondCode CC;
  switch (Cond.size()) {
  default:
    llvm_unreachable("Unknown condition opcode in Cond");
  case 1: // b.cc
    CC = ARM64CC::CondCode(Cond[0].getImm());
    break;
  case 3: { // cbz/cbnz
    // We must insert a compare against 0.
    bool Is64Bit;
    switch (Cond[1].getImm()) {
    default:
      llvm_unreachable("Unknown branch opcode in Cond");
    case ARM64::CBZW:
      Is64Bit = 0;
      CC = ARM64CC::EQ;
      break;
    case ARM64::CBZX:
      Is64Bit = 1;
      CC = ARM64CC::EQ;
      break;
    case ARM64::CBNZW:
      Is64Bit = 0;
      CC = ARM64CC::NE;
      break;
    case ARM64::CBNZX:
      Is64Bit = 1;
      CC = ARM64CC::NE;
      break;
    }
    unsigned SrcReg = Cond[2].getReg();
    if (Is64Bit) {
      // cmp reg, #0 is actually subs xzr, reg, #0.
      MRI.constrainRegClass(SrcReg, &ARM64::GPR64spRegClass);
      BuildMI(MBB, I, DL, get(ARM64::SUBSXri), ARM64::XZR)
          .addReg(SrcReg)
          .addImm(0)
          .addImm(0);
    } else {
      MRI.constrainRegClass(SrcReg, &ARM64::GPR32spRegClass);
      BuildMI(MBB, I, DL, get(ARM64::SUBSWri), ARM64::WZR)
          .addReg(SrcReg)
          .addImm(0)
          .addImm(0);
    }
    break;
  }
  case 4: { // tbz/tbnz
    // We must insert a tst instruction.
    switch (Cond[1].getImm()) {
    default:
      llvm_unreachable("Unknown branch opcode in Cond");
    case ARM64::TBZW:
    case ARM64::TBZX:
      CC = ARM64CC::EQ;
      break;
    case ARM64::TBNZW:
    case ARM64::TBNZX:
      CC = ARM64CC::NE;
      break;
    }
    // cmp reg, #foo is actually ands xzr, reg, #1<<foo.
    if (Cond[1].getImm() == ARM64::TBZW || Cond[1].getImm() == ARM64::TBNZW)
      BuildMI(MBB, I, DL, get(ARM64::ANDSWri), ARM64::WZR)
          .addReg(Cond[2].getReg())
          .addImm(ARM64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 32));
    else
      BuildMI(MBB, I, DL, get(ARM64::ANDSXri), ARM64::XZR)
          .addReg(Cond[2].getReg())
          .addImm(ARM64_AM::encodeLogicalImmediate(1ull << Cond[3].getImm(), 64));
    break;
  }
  }

  unsigned Opc = 0;
  const TargetRegisterClass *RC = nullptr;
  bool TryFold = false;
  if (MRI.constrainRegClass(DstReg, &ARM64::GPR64RegClass)) {
    RC = &ARM64::GPR64RegClass;
    Opc = ARM64::CSELXr;
    TryFold = true;
  } else if (MRI.constrainRegClass(DstReg, &ARM64::GPR32RegClass)) {
    RC = &ARM64::GPR32RegClass;
    Opc = ARM64::CSELWr;
    TryFold = true;
  } else if (MRI.constrainRegClass(DstReg, &ARM64::FPR64RegClass)) {
    RC = &ARM64::FPR64RegClass;
    Opc = ARM64::FCSELDrrr;
  } else if (MRI.constrainRegClass(DstReg, &ARM64::FPR32RegClass)) {
    RC = &ARM64::FPR32RegClass;
    Opc = ARM64::FCSELSrrr;
  }
  assert(RC && "Unsupported regclass");

  // Try folding simple instructions into the csel.
  if (TryFold) {
    unsigned NewVReg = 0;
    unsigned FoldedOpc = canFoldIntoCSel(MRI, TrueReg, &NewVReg);
    if (FoldedOpc) {
      // The folded opcodes csinc, csinc and csneg apply the operation to
      // FalseReg, so we need to invert the condition.
      CC = ARM64CC::getInvertedCondCode(CC);
      TrueReg = FalseReg;
    } else
      FoldedOpc = canFoldIntoCSel(MRI, FalseReg, &NewVReg);

    // Fold the operation. Leave any dead instructions for DCE to clean up.
    if (FoldedOpc) {
      FalseReg = NewVReg;
      Opc = FoldedOpc;
      // The extends the live range of NewVReg.
      MRI.clearKillFlags(NewVReg);
    }
  }

  // Pull all virtual register into the appropriate class.
  MRI.constrainRegClass(TrueReg, RC);
  MRI.constrainRegClass(FalseReg, RC);

  // Insert the csel.
  BuildMI(MBB, I, DL, get(Opc), DstReg).addReg(TrueReg).addReg(FalseReg).addImm(
      CC);
}

bool ARM64InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
                                           unsigned &SrcReg, unsigned &DstReg,
                                           unsigned &SubIdx) const {
  switch (MI.getOpcode()) {
  default:
    return false;
  case ARM64::SBFMXri: // aka sxtw
  case ARM64::UBFMXri: // aka uxtw
    // Check for the 32 -> 64 bit extension case, these instructions can do
    // much more.
    if (MI.getOperand(2).getImm() != 0 || MI.getOperand(3).getImm() != 31)
      return false;
    // This is a signed or unsigned 32 -> 64 bit extension.
    SrcReg = MI.getOperand(1).getReg();
    DstReg = MI.getOperand(0).getReg();
    SubIdx = ARM64::sub_32;
    return true;
  }
}

/// analyzeCompare - For a comparison instruction, return the source registers
/// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
/// Return true if the comparison instruction can be analyzed.
bool ARM64InstrInfo::analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
                                    unsigned &SrcReg2, int &CmpMask,
                                    int &CmpValue) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case ARM64::SUBSWrr:
  case ARM64::SUBSWrs:
  case ARM64::SUBSWrx:
  case ARM64::SUBSXrr:
  case ARM64::SUBSXrs:
  case ARM64::SUBSXrx:
  case ARM64::ADDSWrr:
  case ARM64::ADDSWrs:
  case ARM64::ADDSWrx:
  case ARM64::ADDSXrr:
  case ARM64::ADDSXrs:
  case ARM64::ADDSXrx:
    // Replace SUBSWrr with SUBWrr if NZCV is not used.
    SrcReg = MI->getOperand(1).getReg();
    SrcReg2 = MI->getOperand(2).getReg();
    CmpMask = ~0;
    CmpValue = 0;
    return true;
  case ARM64::SUBSWri:
  case ARM64::ADDSWri:
  case ARM64::SUBSXri:
  case ARM64::ADDSXri:
    SrcReg = MI->getOperand(1).getReg();
    SrcReg2 = 0;
    CmpMask = ~0;
    CmpValue = MI->getOperand(2).getImm();
    return true;
  case ARM64::ANDSWri:
  case ARM64::ANDSXri:
    // ANDS does not use the same encoding scheme as the others xxxS
    // instructions.
    SrcReg = MI->getOperand(1).getReg();
    SrcReg2 = 0;
    CmpMask = ~0;
    CmpValue = ARM64_AM::decodeLogicalImmediate(
        MI->getOperand(2).getImm(),
        MI->getOpcode() == ARM64::ANDSWri ? 32 : 64);
    return true;
  }

  return false;
}

static bool UpdateOperandRegClass(MachineInstr *Instr) {
  MachineBasicBlock *MBB = Instr->getParent();
  assert(MBB && "Can't get MachineBasicBlock here");
  MachineFunction *MF = MBB->getParent();
  assert(MF && "Can't get MachineFunction here");
  const TargetMachine *TM = &MF->getTarget();
  const TargetInstrInfo *TII = TM->getInstrInfo();
  const TargetRegisterInfo *TRI = TM->getRegisterInfo();
  MachineRegisterInfo *MRI = &MF->getRegInfo();

  for (unsigned OpIdx = 0, EndIdx = Instr->getNumOperands(); OpIdx < EndIdx;
       ++OpIdx) {
    MachineOperand &MO = Instr->getOperand(OpIdx);
    const TargetRegisterClass *OpRegCstraints =
        Instr->getRegClassConstraint(OpIdx, TII, TRI);

    // If there's no constraint, there's nothing to do.
    if (!OpRegCstraints)
      continue;
    // If the operand is a frame index, there's nothing to do here.
    // A frame index operand will resolve correctly during PEI.
    if (MO.isFI())
      continue;

    assert(MO.isReg() &&
           "Operand has register constraints without being a register!");

    unsigned Reg = MO.getReg();
    if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
      if (!OpRegCstraints->contains(Reg))
        return false;
    } else if (!OpRegCstraints->hasSubClassEq(MRI->getRegClass(Reg)) &&
               !MRI->constrainRegClass(Reg, OpRegCstraints))
      return false;
  }

  return true;
}

/// optimizeCompareInstr - Convert the instruction supplying the argument to the
/// comparison into one that sets the zero bit in the flags register.
bool ARM64InstrInfo::optimizeCompareInstr(
    MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
    int CmpValue, const MachineRegisterInfo *MRI) const {

  // Replace SUBSWrr with SUBWrr if NZCV is not used.
  int Cmp_NZCV = CmpInstr->findRegisterDefOperandIdx(ARM64::NZCV, true);
  if (Cmp_NZCV != -1) {
    unsigned NewOpc;
    switch (CmpInstr->getOpcode()) {
    default:
      return false;
    case ARM64::ADDSWrr:      NewOpc = ARM64::ADDWrr; break;
    case ARM64::ADDSWri:      NewOpc = ARM64::ADDWri; break;
    case ARM64::ADDSWrs:      NewOpc = ARM64::ADDWrs; break;
    case ARM64::ADDSWrx:      NewOpc = ARM64::ADDWrx; break;
    case ARM64::ADDSXrr:      NewOpc = ARM64::ADDXrr; break;
    case ARM64::ADDSXri:      NewOpc = ARM64::ADDXri; break;
    case ARM64::ADDSXrs:      NewOpc = ARM64::ADDXrs; break;
    case ARM64::ADDSXrx:      NewOpc = ARM64::ADDXrx; break;
    case ARM64::SUBSWrr:      NewOpc = ARM64::SUBWrr; break;
    case ARM64::SUBSWri:      NewOpc = ARM64::SUBWri; break;
    case ARM64::SUBSWrs:      NewOpc = ARM64::SUBWrs; break;
    case ARM64::SUBSWrx:      NewOpc = ARM64::SUBWrx; break;
    case ARM64::SUBSXrr:      NewOpc = ARM64::SUBXrr; break;
    case ARM64::SUBSXri:      NewOpc = ARM64::SUBXri; break;
    case ARM64::SUBSXrs:      NewOpc = ARM64::SUBXrs; break;
    case ARM64::SUBSXrx:      NewOpc = ARM64::SUBXrx; break;
    }

    const MCInstrDesc &MCID = get(NewOpc);
    CmpInstr->setDesc(MCID);
    CmpInstr->RemoveOperand(Cmp_NZCV);
    bool succeeded = UpdateOperandRegClass(CmpInstr);
    (void)succeeded;
    assert(succeeded && "Some operands reg class are incompatible!");
    return true;
  }

  // Continue only if we have a "ri" where immediate is zero.
  if (CmpValue != 0 || SrcReg2 != 0)
    return false;

  // CmpInstr is a Compare instruction if destination register is not used.
  if (!MRI->use_nodbg_empty(CmpInstr->getOperand(0).getReg()))
    return false;

  // Get the unique definition of SrcReg.
  MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
  if (!MI)
    return false;

  // We iterate backward, starting from the instruction before CmpInstr and
  // stop when reaching the definition of the source register or done with the
  // basic block, to check whether NZCV is used or modified in between.
  MachineBasicBlock::iterator I = CmpInstr, E = MI,
                              B = CmpInstr->getParent()->begin();

  // Early exit if CmpInstr is at the beginning of the BB.
  if (I == B)
    return false;

  // Check whether the definition of SrcReg is in the same basic block as
  // Compare. If not, we can't optimize away the Compare.
  if (MI->getParent() != CmpInstr->getParent())
    return false;

  // Check that NZCV isn't set between the comparison instruction and the one we
  // want to change.
  const TargetRegisterInfo *TRI = &getRegisterInfo();
  for (--I; I != E; --I) {
    const MachineInstr &Instr = *I;

    if (Instr.modifiesRegister(ARM64::NZCV, TRI) ||
        Instr.readsRegister(ARM64::NZCV, TRI))
      // This instruction modifies or uses NZCV after the one we want to
      // change. We can't do this transformation.
      return false;
    if (I == B)
      // The 'and' is below the comparison instruction.
      return false;
  }

  unsigned NewOpc = MI->getOpcode();
  switch (MI->getOpcode()) {
  default:
    return false;
  case ARM64::ADDSWrr:
  case ARM64::ADDSWri:
  case ARM64::ADDSXrr:
  case ARM64::ADDSXri:
  case ARM64::SUBSWrr:
  case ARM64::SUBSWri:
  case ARM64::SUBSXrr:
  case ARM64::SUBSXri:
    break;
  case ARM64::ADDWrr:    NewOpc = ARM64::ADDSWrr; break;
  case ARM64::ADDWri:    NewOpc = ARM64::ADDSWri; break;
  case ARM64::ADDXrr:    NewOpc = ARM64::ADDSXrr; break;
  case ARM64::ADDXri:    NewOpc = ARM64::ADDSXri; break;
  case ARM64::ADCWr:     NewOpc = ARM64::ADCSWr; break;
  case ARM64::ADCXr:     NewOpc = ARM64::ADCSXr; break;
  case ARM64::SUBWrr:    NewOpc = ARM64::SUBSWrr; break;
  case ARM64::SUBWri:    NewOpc = ARM64::SUBSWri; break;
  case ARM64::SUBXrr:    NewOpc = ARM64::SUBSXrr; break;
  case ARM64::SUBXri:    NewOpc = ARM64::SUBSXri; break;
  case ARM64::SBCWr:     NewOpc = ARM64::SBCSWr; break;
  case ARM64::SBCXr:     NewOpc = ARM64::SBCSXr; break;
  case ARM64::ANDWri:    NewOpc = ARM64::ANDSWri; break;
  case ARM64::ANDXri:    NewOpc = ARM64::ANDSXri; break;
  }

  // Scan forward for the use of NZCV.
  // When checking against MI: if it's a conditional code requires
  // checking of V bit, then this is not safe to do.
  // It is safe to remove CmpInstr if NZCV is redefined or killed.
  // If we are done with the basic block, we need to check whether NZCV is
  // live-out.
  bool IsSafe = false;
  for (MachineBasicBlock::iterator I = CmpInstr,
                                   E = CmpInstr->getParent()->end();
       !IsSafe && ++I != E;) {
    const MachineInstr &Instr = *I;
    for (unsigned IO = 0, EO = Instr.getNumOperands(); !IsSafe && IO != EO;
         ++IO) {
      const MachineOperand &MO = Instr.getOperand(IO);
      if (MO.isRegMask() && MO.clobbersPhysReg(ARM64::NZCV)) {
        IsSafe = true;
        break;
      }
      if (!MO.isReg() || MO.getReg() != ARM64::NZCV)
        continue;
      if (MO.isDef()) {
        IsSafe = true;
        break;
      }

      // Decode the condition code.
      unsigned Opc = Instr.getOpcode();
      ARM64CC::CondCode CC;
      switch (Opc) {
      default:
        return false;
      case ARM64::Bcc:
        CC = (ARM64CC::CondCode)Instr.getOperand(IO - 2).getImm();
        break;
      case ARM64::CSINVWr:
      case ARM64::CSINVXr:
      case ARM64::CSINCWr:
      case ARM64::CSINCXr:
      case ARM64::CSELWr:
      case ARM64::CSELXr:
      case ARM64::CSNEGWr:
      case ARM64::CSNEGXr:
      case ARM64::FCSELSrrr:
      case ARM64::FCSELDrrr:
        CC = (ARM64CC::CondCode)Instr.getOperand(IO - 1).getImm();
        break;
      }

      // It is not safe to remove Compare instruction if Overflow(V) is used.
      switch (CC) {
      default:
        // NZCV can be used multiple times, we should continue.
        break;
      case ARM64CC::VS:
      case ARM64CC::VC:
      case ARM64CC::GE:
      case ARM64CC::LT:
      case ARM64CC::GT:
      case ARM64CC::LE:
        return false;
      }
    }
  }

  // If NZCV is not killed nor re-defined, we should check whether it is
  // live-out. If it is live-out, do not optimize.
  if (!IsSafe) {
    MachineBasicBlock *ParentBlock = CmpInstr->getParent();
    for (auto *MBB : ParentBlock->successors())
      if (MBB->isLiveIn(ARM64::NZCV))
        return false;
  }

  // Update the instruction to set NZCV.
  MI->setDesc(get(NewOpc));
  CmpInstr->eraseFromParent();
  bool succeeded = UpdateOperandRegClass(MI);
  (void)succeeded;
  assert(succeeded && "Some operands reg class are incompatible!");
  MI->addRegisterDefined(ARM64::NZCV, TRI);
  return true;
}

/// Return true if this is this instruction has a non-zero immediate
bool ARM64InstrInfo::hasShiftedReg(const MachineInstr *MI) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case ARM64::ADDSWrs:
  case ARM64::ADDSXrs:
  case ARM64::ADDWrs:
  case ARM64::ADDXrs:
  case ARM64::ANDSWrs:
  case ARM64::ANDSXrs:
  case ARM64::ANDWrs:
  case ARM64::ANDXrs:
  case ARM64::BICSWrs:
  case ARM64::BICSXrs:
  case ARM64::BICWrs:
  case ARM64::BICXrs:
  case ARM64::CRC32Brr:
  case ARM64::CRC32CBrr:
  case ARM64::CRC32CHrr:
  case ARM64::CRC32CWrr:
  case ARM64::CRC32CXrr:
  case ARM64::CRC32Hrr:
  case ARM64::CRC32Wrr:
  case ARM64::CRC32Xrr:
  case ARM64::EONWrs:
  case ARM64::EONXrs:
  case ARM64::EORWrs:
  case ARM64::EORXrs:
  case ARM64::ORNWrs:
  case ARM64::ORNXrs:
  case ARM64::ORRWrs:
  case ARM64::ORRXrs:
  case ARM64::SUBSWrs:
  case ARM64::SUBSXrs:
  case ARM64::SUBWrs:
  case ARM64::SUBXrs:
    if (MI->getOperand(3).isImm()) {
      unsigned val = MI->getOperand(3).getImm();
      return (val != 0);
    }
    break;
  }
  return false;
}

/// Return true if this is this instruction has a non-zero immediate
bool ARM64InstrInfo::hasExtendedReg(const MachineInstr *MI) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case ARM64::ADDSWrx:
  case ARM64::ADDSXrx:
  case ARM64::ADDSXrx64:
  case ARM64::ADDWrx:
  case ARM64::ADDXrx:
  case ARM64::ADDXrx64:
  case ARM64::SUBSWrx:
  case ARM64::SUBSXrx:
  case ARM64::SUBSXrx64:
  case ARM64::SUBWrx:
  case ARM64::SUBXrx:
  case ARM64::SUBXrx64:
    if (MI->getOperand(3).isImm()) {
      unsigned val = MI->getOperand(3).getImm();
      return (val != 0);
    }
    break;
  }

  return false;
}

// Return true if this instruction simply sets its single destination register
// to zero. This is equivalent to a register rename of the zero-register.
bool ARM64InstrInfo::isGPRZero(const MachineInstr *MI) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case ARM64::MOVZWi:
  case ARM64::MOVZXi: // movz Rd, #0 (LSL #0)
    if (MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) {
      assert(MI->getDesc().getNumOperands() == 3 &&
             MI->getOperand(2).getImm() == 0 && "invalid MOVZi operands");
      return true;
    }
    break;
  case ARM64::ANDWri: // and Rd, Rzr, #imm
    return MI->getOperand(1).getReg() == ARM64::WZR;
  case ARM64::ANDXri:
    return MI->getOperand(1).getReg() == ARM64::XZR;
  case TargetOpcode::COPY:
    return MI->getOperand(1).getReg() == ARM64::WZR;
  }
  return false;
}

// Return true if this instruction simply renames a general register without
// modifying bits.
bool ARM64InstrInfo::isGPRCopy(const MachineInstr *MI) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case TargetOpcode::COPY: {
    // GPR32 copies will by lowered to ORRXrs
    unsigned DstReg = MI->getOperand(0).getReg();
    return (ARM64::GPR32RegClass.contains(DstReg) ||
            ARM64::GPR64RegClass.contains(DstReg));
  }
  case ARM64::ORRXrs: // orr Xd, Xzr, Xm (LSL #0)
    if (MI->getOperand(1).getReg() == ARM64::XZR) {
      assert(MI->getDesc().getNumOperands() == 4 &&
             MI->getOperand(3).getImm() == 0 && "invalid ORRrs operands");
      return true;
    }
  case ARM64::ADDXri: // add Xd, Xn, #0 (LSL #0)
    if (MI->getOperand(2).getImm() == 0) {
      assert(MI->getDesc().getNumOperands() == 4 &&
             MI->getOperand(3).getImm() == 0 && "invalid ADDXri operands");
      return true;
    }
  }
  return false;
}

// Return true if this instruction simply renames a general register without
// modifying bits.
bool ARM64InstrInfo::isFPRCopy(const MachineInstr *MI) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case TargetOpcode::COPY: {
    // FPR64 copies will by lowered to ORR.16b
    unsigned DstReg = MI->getOperand(0).getReg();
    return (ARM64::FPR64RegClass.contains(DstReg) ||
            ARM64::FPR128RegClass.contains(DstReg));
  }
  case ARM64::ORRv16i8:
    if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
      assert(MI->getDesc().getNumOperands() == 3 && MI->getOperand(0).isReg() &&
             "invalid ORRv16i8 operands");
      return true;
    }
  }
  return false;
}

unsigned ARM64InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
                                             int &FrameIndex) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case ARM64::LDRWui:
  case ARM64::LDRXui:
  case ARM64::LDRBui:
  case ARM64::LDRHui:
  case ARM64::LDRSui:
  case ARM64::LDRDui:
  case ARM64::LDRQui:
    if (MI->getOperand(0).getSubReg() == 0 && MI->getOperand(1).isFI() &&
        MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) {
      FrameIndex = MI->getOperand(1).getIndex();
      return MI->getOperand(0).getReg();
    }
    break;
  }

  return 0;
}

unsigned ARM64InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
                                            int &FrameIndex) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case ARM64::STRWui:
  case ARM64::STRXui:
  case ARM64::STRBui:
  case ARM64::STRHui:
  case ARM64::STRSui:
  case ARM64::STRDui:
  case ARM64::STRQui:
    if (MI->getOperand(0).getSubReg() == 0 && MI->getOperand(1).isFI() &&
        MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) {
      FrameIndex = MI->getOperand(1).getIndex();
      return MI->getOperand(0).getReg();
    }
    break;
  }
  return 0;
}

/// Return true if this is load/store scales or extends its register offset.
/// This refers to scaling a dynamic index as opposed to scaled immediates.
/// MI should be a memory op that allows scaled addressing.
bool ARM64InstrInfo::isScaledAddr(const MachineInstr *MI) const {
  switch (MI->getOpcode()) {
  default:
    break;
  case ARM64::LDRBBroW:
  case ARM64::LDRBroW:
  case ARM64::LDRDroW:
  case ARM64::LDRHHroW:
  case ARM64::LDRHroW:
  case ARM64::LDRQroW:
  case ARM64::LDRSBWroW:
  case ARM64::LDRSBXroW:
  case ARM64::LDRSHWroW:
  case ARM64::LDRSHXroW:
  case ARM64::LDRSWroW:
  case ARM64::LDRSroW:
  case ARM64::LDRWroW:
  case ARM64::LDRXroW:
  case ARM64::STRBBroW:
  case ARM64::STRBroW:
  case ARM64::STRDroW:
  case ARM64::STRHHroW:
  case ARM64::STRHroW:
  case ARM64::STRQroW:
  case ARM64::STRSroW:
  case ARM64::STRWroW:
  case ARM64::STRXroW:
  case ARM64::LDRBBroX:
  case ARM64::LDRBroX:
  case ARM64::LDRDroX:
  case ARM64::LDRHHroX:
  case ARM64::LDRHroX:
  case ARM64::LDRQroX:
  case ARM64::LDRSBWroX:
  case ARM64::LDRSBXroX:
  case ARM64::LDRSHWroX:
  case ARM64::LDRSHXroX:
  case ARM64::LDRSWroX:
  case ARM64::LDRSroX:
  case ARM64::LDRWroX:
  case ARM64::LDRXroX:
  case ARM64::STRBBroX:
  case ARM64::STRBroX:
  case ARM64::STRDroX:
  case ARM64::STRHHroX:
  case ARM64::STRHroX:
  case ARM64::STRQroX:
  case ARM64::STRSroX:
  case ARM64::STRWroX:
  case ARM64::STRXroX:

    unsigned Val = MI->getOperand(3).getImm();
    ARM64_AM::ShiftExtendType ExtType = ARM64_AM::getMemExtendType(Val);
    return (ExtType != ARM64_AM::UXTX) || ARM64_AM::getMemDoShift(Val);
  }
  return false;
}

/// Check all MachineMemOperands for a hint to suppress pairing.
bool ARM64InstrInfo::isLdStPairSuppressed(const MachineInstr *MI) const {
  assert(MOSuppressPair < (1 << MachineMemOperand::MOTargetNumBits) &&
         "Too many target MO flags");
  for (auto *MM : MI->memoperands()) {
    if (MM->getFlags() &
        (MOSuppressPair << MachineMemOperand::MOTargetStartBit)) {
      return true;
    }
  }
  return false;
}

/// Set a flag on the first MachineMemOperand to suppress pairing.
void ARM64InstrInfo::suppressLdStPair(MachineInstr *MI) const {
  if (MI->memoperands_empty())
    return;

  assert(MOSuppressPair < (1 << MachineMemOperand::MOTargetNumBits) &&
         "Too many target MO flags");
  (*MI->memoperands_begin())
      ->setFlags(MOSuppressPair << MachineMemOperand::MOTargetStartBit);
}

bool ARM64InstrInfo::getLdStBaseRegImmOfs(MachineInstr *LdSt, unsigned &BaseReg,
                                          unsigned &Offset,
                                          const TargetRegisterInfo *TRI) const {
  switch (LdSt->getOpcode()) {
  default:
    return false;
  case ARM64::STRSui:
  case ARM64::STRDui:
  case ARM64::STRQui:
  case ARM64::STRXui:
  case ARM64::STRWui:
  case ARM64::LDRSui:
  case ARM64::LDRDui:
  case ARM64::LDRQui:
  case ARM64::LDRXui:
  case ARM64::LDRWui:
    if (!LdSt->getOperand(1).isReg() || !LdSt->getOperand(2).isImm())
      return false;
    BaseReg = LdSt->getOperand(1).getReg();
    MachineFunction &MF = *LdSt->getParent()->getParent();
    unsigned Width = getRegClass(LdSt->getDesc(), 0, TRI, MF)->getSize();
    Offset = LdSt->getOperand(2).getImm() * Width;
    return true;
  };
}

/// Detect opportunities for ldp/stp formation.
///
/// Only called for LdSt for which getLdStBaseRegImmOfs returns true.
bool ARM64InstrInfo::shouldClusterLoads(MachineInstr *FirstLdSt,
                                        MachineInstr *SecondLdSt,
                                        unsigned NumLoads) const {
  // Only cluster up to a single pair.
  if (NumLoads > 1)
    return false;
  if (FirstLdSt->getOpcode() != SecondLdSt->getOpcode())
    return false;
  // getLdStBaseRegImmOfs guarantees that oper 2 isImm.
  unsigned Ofs1 = FirstLdSt->getOperand(2).getImm();
  // Allow 6 bits of positive range.
  if (Ofs1 > 64)
    return false;
  // The caller should already have ordered First/SecondLdSt by offset.
  unsigned Ofs2 = SecondLdSt->getOperand(2).getImm();
  return Ofs1 + 1 == Ofs2;
}

bool ARM64InstrInfo::shouldScheduleAdjacent(MachineInstr *First,
                                            MachineInstr *Second) const {
  // Cyclone can fuse CMN, CMP followed by Bcc.

  // FIXME: B0 can also fuse:
  // AND, BIC, ORN, ORR, or EOR (optional S) followed by Bcc or CBZ or CBNZ.
  if (Second->getOpcode() != ARM64::Bcc)
    return false;
  switch (First->getOpcode()) {
  default:
    return false;
  case ARM64::SUBSWri:
  case ARM64::ADDSWri:
  case ARM64::ANDSWri:
  case ARM64::SUBSXri:
  case ARM64::ADDSXri:
  case ARM64::ANDSXri:
    return true;
  }
}

MachineInstr *ARM64InstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
                                                       int FrameIx,
                                                       uint64_t Offset,
                                                       const MDNode *MDPtr,
                                                       DebugLoc DL) const {
  MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM64::DBG_VALUE))
                                .addFrameIndex(FrameIx)
                                .addImm(0)
                                .addImm(Offset)
                                .addMetadata(MDPtr);
  return &*MIB;
}

static const MachineInstrBuilder &AddSubReg(const MachineInstrBuilder &MIB,
                                            unsigned Reg, unsigned SubIdx,
                                            unsigned State,
                                            const TargetRegisterInfo *TRI) {
  if (!SubIdx)
    return MIB.addReg(Reg, State);

  if (TargetRegisterInfo::isPhysicalRegister(Reg))
    return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
  return MIB.addReg(Reg, State, SubIdx);
}

static bool forwardCopyWillClobberTuple(unsigned DestReg, unsigned SrcReg,
                                        unsigned NumRegs) {
  // We really want the positive remainder mod 32 here, that happens to be
  // easily obtainable with a mask.
  return ((DestReg - SrcReg) & 0x1f) < NumRegs;
}

void ARM64InstrInfo::copyPhysRegTuple(MachineBasicBlock &MBB,
                                      MachineBasicBlock::iterator I,
                                      DebugLoc DL, unsigned DestReg,
                                      unsigned SrcReg, bool KillSrc,
                                      unsigned Opcode,
                                      llvm::ArrayRef<unsigned> Indices) const {
  assert(getSubTarget().hasNEON() &&
         "Unexpected register copy without NEON");
  const TargetRegisterInfo *TRI = &getRegisterInfo();
  uint16_t DestEncoding = TRI->getEncodingValue(DestReg);
  uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
  unsigned NumRegs = Indices.size();

  int SubReg = 0, End = NumRegs, Incr = 1;
  if (forwardCopyWillClobberTuple(DestEncoding, SrcEncoding, NumRegs)) {
    SubReg = NumRegs - 1;
    End = -1;
    Incr = -1;
  }

  for (; SubReg != End; SubReg += Incr) {
    const MachineInstrBuilder &MIB = BuildMI(MBB, I, DL, get(Opcode));
    AddSubReg(MIB, DestReg, Indices[SubReg], RegState::Define, TRI);
    AddSubReg(MIB, SrcReg, Indices[SubReg], 0, TRI);
    AddSubReg(MIB, SrcReg, Indices[SubReg], getKillRegState(KillSrc), TRI);
  }
}

void ARM64InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
                                 MachineBasicBlock::iterator I, DebugLoc DL,
                                 unsigned DestReg, unsigned SrcReg,
                                 bool KillSrc) const {
  if (ARM64::GPR32spRegClass.contains(DestReg) &&
      (ARM64::GPR32spRegClass.contains(SrcReg) || SrcReg == ARM64::WZR)) {
    const TargetRegisterInfo *TRI = &getRegisterInfo();

    if (DestReg == ARM64::WSP || SrcReg == ARM64::WSP) {
      // If either operand is WSP, expand to ADD #0.
      if (Subtarget.hasZeroCycleRegMove()) {
        // Cyclone recognizes "ADD Xd, Xn, #0" as a zero-cycle register move.
        unsigned DestRegX = TRI->getMatchingSuperReg(DestReg, ARM64::sub_32,
                                                     &ARM64::GPR64spRegClass);
        unsigned SrcRegX = TRI->getMatchingSuperReg(SrcReg, ARM64::sub_32,
                                                    &ARM64::GPR64spRegClass);
        // This instruction is reading and writing X registers.  This may upset
        // the register scavenger and machine verifier, so we need to indicate
        // that we are reading an undefined value from SrcRegX, but a proper
        // value from SrcReg.
        BuildMI(MBB, I, DL, get(ARM64::ADDXri), DestRegX)
            .addReg(SrcRegX, RegState::Undef)
            .addImm(0)
            .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, 0))
            .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
      } else {
        BuildMI(MBB, I, DL, get(ARM64::ADDWri), DestReg)
            .addReg(SrcReg, getKillRegState(KillSrc))
            .addImm(0)
            .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, 0));
      }
    } else if (SrcReg == ARM64::WZR && Subtarget.hasZeroCycleZeroing()) {
      BuildMI(MBB, I, DL, get(ARM64::MOVZWi), DestReg).addImm(0).addImm(
          ARM64_AM::getShifterImm(ARM64_AM::LSL, 0));
    } else {
      if (Subtarget.hasZeroCycleRegMove()) {
        // Cyclone recognizes "ORR Xd, XZR, Xm" as a zero-cycle register move.
        unsigned DestRegX = TRI->getMatchingSuperReg(DestReg, ARM64::sub_32,
                                                     &ARM64::GPR64spRegClass);
        unsigned SrcRegX = TRI->getMatchingSuperReg(SrcReg, ARM64::sub_32,
                                                    &ARM64::GPR64spRegClass);
        // This instruction is reading and writing X registers.  This may upset
        // the register scavenger and machine verifier, so we need to indicate
        // that we are reading an undefined value from SrcRegX, but a proper
        // value from SrcReg.
        BuildMI(MBB, I, DL, get(ARM64::ORRXrr), DestRegX)
            .addReg(ARM64::XZR)
            .addReg(SrcRegX, RegState::Undef)
            .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
      } else {
        // Otherwise, expand to ORR WZR.
        BuildMI(MBB, I, DL, get(ARM64::ORRWrr), DestReg)
            .addReg(ARM64::WZR)
            .addReg(SrcReg, getKillRegState(KillSrc));
      }
    }
    return;
  }

  if (ARM64::GPR64spRegClass.contains(DestReg) &&
      (ARM64::GPR64spRegClass.contains(SrcReg) || SrcReg == ARM64::XZR)) {
    if (DestReg == ARM64::SP || SrcReg == ARM64::SP) {
      // If either operand is SP, expand to ADD #0.
      BuildMI(MBB, I, DL, get(ARM64::ADDXri), DestReg)
          .addReg(SrcReg, getKillRegState(KillSrc))
          .addImm(0)
          .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, 0));
    } else if (SrcReg == ARM64::XZR && Subtarget.hasZeroCycleZeroing()) {
      BuildMI(MBB, I, DL, get(ARM64::MOVZXi), DestReg).addImm(0).addImm(
          ARM64_AM::getShifterImm(ARM64_AM::LSL, 0));
    } else {
      // Otherwise, expand to ORR XZR.
      BuildMI(MBB, I, DL, get(ARM64::ORRXrr), DestReg)
          .addReg(ARM64::XZR)
          .addReg(SrcReg, getKillRegState(KillSrc));
    }
    return;
  }

  // Copy a DDDD register quad by copying the individual sub-registers.
  if (ARM64::DDDDRegClass.contains(DestReg) &&
      ARM64::DDDDRegClass.contains(SrcReg)) {
    static const unsigned Indices[] = { ARM64::dsub0, ARM64::dsub1,
                                        ARM64::dsub2, ARM64::dsub3 };
    copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, ARM64::ORRv8i8,
                     Indices);
    return;
  }

  // Copy a DDD register triple by copying the individual sub-registers.
  if (ARM64::DDDRegClass.contains(DestReg) &&
      ARM64::DDDRegClass.contains(SrcReg)) {
    static const unsigned Indices[] = { ARM64::dsub0, ARM64::dsub1,
                                        ARM64::dsub2 };
    copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, ARM64::ORRv8i8,
                     Indices);
    return;
  }

  // Copy a DD register pair by copying the individual sub-registers.
  if (ARM64::DDRegClass.contains(DestReg) &&
      ARM64::DDRegClass.contains(SrcReg)) {
    static const unsigned Indices[] = { ARM64::dsub0, ARM64::dsub1 };
    copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, ARM64::ORRv8i8,
                     Indices);
    return;
  }

  // Copy a QQQQ register quad by copying the individual sub-registers.
  if (ARM64::QQQQRegClass.contains(DestReg) &&
      ARM64::QQQQRegClass.contains(SrcReg)) {
    static const unsigned Indices[] = { ARM64::qsub0, ARM64::qsub1,
                                        ARM64::qsub2, ARM64::qsub3 };
    copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, ARM64::ORRv16i8,
                     Indices);
    return;
  }

  // Copy a QQQ register triple by copying the individual sub-registers.
  if (ARM64::QQQRegClass.contains(DestReg) &&
      ARM64::QQQRegClass.contains(SrcReg)) {
    static const unsigned Indices[] = { ARM64::qsub0, ARM64::qsub1,
                                        ARM64::qsub2 };
    copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, ARM64::ORRv16i8,
                     Indices);
    return;
  }

  // Copy a QQ register pair by copying the individual sub-registers.
  if (ARM64::QQRegClass.contains(DestReg) &&
      ARM64::QQRegClass.contains(SrcReg)) {
    static const unsigned Indices[] = { ARM64::qsub0, ARM64::qsub1 };
    copyPhysRegTuple(MBB, I, DL, DestReg, SrcReg, KillSrc, ARM64::ORRv16i8,
                     Indices);
    return;
  }

  if (ARM64::FPR128RegClass.contains(DestReg) &&
      ARM64::FPR128RegClass.contains(SrcReg)) {
    if(getSubTarget().hasNEON()) {
      BuildMI(MBB, I, DL, get(ARM64::ORRv16i8), DestReg).addReg(SrcReg).addReg(
          SrcReg, getKillRegState(KillSrc));
    } else {
      BuildMI(MBB, I, DL, get(ARM64::STRQpre))
        .addReg(SrcReg, getKillRegState(KillSrc))
        .addReg(ARM64::SP)
        .addImm(-16);
      BuildMI(MBB, I, DL, get(ARM64::LDRQpre))
        .addReg(DestReg, RegState::Define)
        .addReg(ARM64::SP)
        .addImm(16);
    }
    return;
  }

  if (ARM64::FPR64RegClass.contains(DestReg) &&
      ARM64::FPR64RegClass.contains(SrcReg)) {
    if(getSubTarget().hasNEON()) {
      DestReg =
          RI.getMatchingSuperReg(DestReg, ARM64::dsub, &ARM64::FPR128RegClass);
      SrcReg =
          RI.getMatchingSuperReg(SrcReg, ARM64::dsub, &ARM64::FPR128RegClass);
      BuildMI(MBB, I, DL, get(ARM64::ORRv16i8), DestReg).addReg(SrcReg).addReg(
          SrcReg, getKillRegState(KillSrc));
    } else {
      BuildMI(MBB, I, DL, get(ARM64::FMOVDr), DestReg)
          .addReg(SrcReg, getKillRegState(KillSrc));
    }
    return;
  }

  if (ARM64::FPR32RegClass.contains(DestReg) &&
      ARM64::FPR32RegClass.contains(SrcReg)) {
    if(getSubTarget().hasNEON()) {
      DestReg =
          RI.getMatchingSuperReg(DestReg, ARM64::ssub, &ARM64::FPR128RegClass);
      SrcReg =
          RI.getMatchingSuperReg(SrcReg, ARM64::ssub, &ARM64::FPR128RegClass);
      BuildMI(MBB, I, DL, get(ARM64::ORRv16i8), DestReg).addReg(SrcReg).addReg(
          SrcReg, getKillRegState(KillSrc));
    } else {
      BuildMI(MBB, I, DL, get(ARM64::FMOVSr), DestReg)
          .addReg(SrcReg, getKillRegState(KillSrc));
    }
    return;
  }

  if (ARM64::FPR16RegClass.contains(DestReg) &&
      ARM64::FPR16RegClass.contains(SrcReg)) {
    if(getSubTarget().hasNEON()) {
      DestReg =
          RI.getMatchingSuperReg(DestReg, ARM64::hsub, &ARM64::FPR128RegClass);
      SrcReg =
          RI.getMatchingSuperReg(SrcReg, ARM64::hsub, &ARM64::FPR128RegClass);
      BuildMI(MBB, I, DL, get(ARM64::ORRv16i8), DestReg).addReg(SrcReg).addReg(
          SrcReg, getKillRegState(KillSrc));
    } else {
      DestReg =
          RI.getMatchingSuperReg(DestReg, ARM64::hsub, &ARM64::FPR32RegClass);
      SrcReg =
          RI.getMatchingSuperReg(SrcReg, ARM64::hsub, &ARM64::FPR32RegClass);
      BuildMI(MBB, I, DL, get(ARM64::FMOVSr), DestReg)
          .addReg(SrcReg, getKillRegState(KillSrc));
    }
    return;
  }

  if (ARM64::FPR8RegClass.contains(DestReg) &&
      ARM64::FPR8RegClass.contains(SrcReg)) {
    if(getSubTarget().hasNEON()) {
      DestReg =
          RI.getMatchingSuperReg(DestReg, ARM64::bsub, &ARM64::FPR128RegClass);
      SrcReg =
          RI.getMatchingSuperReg(SrcReg, ARM64::bsub, &ARM64::FPR128RegClass);
      BuildMI(MBB, I, DL, get(ARM64::ORRv16i8), DestReg).addReg(SrcReg).addReg(
          SrcReg, getKillRegState(KillSrc));
    } else {
      DestReg =
          RI.getMatchingSuperReg(DestReg, ARM64::bsub, &ARM64::FPR32RegClass);
      SrcReg =
          RI.getMatchingSuperReg(SrcReg, ARM64::bsub, &ARM64::FPR32RegClass);
      BuildMI(MBB, I, DL, get(ARM64::FMOVSr), DestReg)
          .addReg(SrcReg, getKillRegState(KillSrc));
    }
    return;
  }

  // Copies between GPR64 and FPR64.
  if (ARM64::FPR64RegClass.contains(DestReg) &&
      ARM64::GPR64RegClass.contains(SrcReg)) {
    BuildMI(MBB, I, DL, get(ARM64::FMOVXDr), DestReg)
        .addReg(SrcReg, getKillRegState(KillSrc));
    return;
  }
  if (ARM64::GPR64RegClass.contains(DestReg) &&
      ARM64::FPR64RegClass.contains(SrcReg)) {
    BuildMI(MBB, I, DL, get(ARM64::FMOVDXr), DestReg)
        .addReg(SrcReg, getKillRegState(KillSrc));
    return;
  }
  // Copies between GPR32 and FPR32.
  if (ARM64::FPR32RegClass.contains(DestReg) &&
      ARM64::GPR32RegClass.contains(SrcReg)) {
    BuildMI(MBB, I, DL, get(ARM64::FMOVWSr), DestReg)
        .addReg(SrcReg, getKillRegState(KillSrc));
    return;
  }
  if (ARM64::GPR32RegClass.contains(DestReg) &&
      ARM64::FPR32RegClass.contains(SrcReg)) {
    BuildMI(MBB, I, DL, get(ARM64::FMOVSWr), DestReg)
        .addReg(SrcReg, getKillRegState(KillSrc));
    return;
  }

  assert(0 && "unimplemented reg-to-reg copy");
}

void ARM64InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
                                         MachineBasicBlock::iterator MBBI,
                                         unsigned SrcReg, bool isKill, int FI,
                                         const TargetRegisterClass *RC,
                                         const TargetRegisterInfo *TRI) const {
  DebugLoc DL;
  if (MBBI != MBB.end())
    DL = MBBI->getDebugLoc();
  MachineFunction &MF = *MBB.getParent();
  MachineFrameInfo &MFI = *MF.getFrameInfo();
  unsigned Align = MFI.getObjectAlignment(FI);

  MachinePointerInfo PtrInfo(PseudoSourceValue::getFixedStack(FI));
  MachineMemOperand *MMO = MF.getMachineMemOperand(
      PtrInfo, MachineMemOperand::MOStore, MFI.getObjectSize(FI), Align);
  unsigned Opc = 0;
  bool Offset = true;
  switch (RC->getSize()) {
  case 1:
    if (ARM64::FPR8RegClass.hasSubClassEq(RC))
      Opc = ARM64::STRBui;
    break;
  case 2:
    if (ARM64::FPR16RegClass.hasSubClassEq(RC))
      Opc = ARM64::STRHui;
    break;
  case 4:
    if (ARM64::GPR32allRegClass.hasSubClassEq(RC)) {
      Opc = ARM64::STRWui;
      if (TargetRegisterInfo::isVirtualRegister(SrcReg))
        MF.getRegInfo().constrainRegClass(SrcReg, &ARM64::GPR32RegClass);
      else
        assert(SrcReg != ARM64::WSP);
    } else if (ARM64::FPR32RegClass.hasSubClassEq(RC))
      Opc = ARM64::STRSui;
    break;
  case 8:
    if (ARM64::GPR64allRegClass.hasSubClassEq(RC)) {
      Opc = ARM64::STRXui;
      if (TargetRegisterInfo::isVirtualRegister(SrcReg))
        MF.getRegInfo().constrainRegClass(SrcReg, &ARM64::GPR64RegClass);
      else
        assert(SrcReg != ARM64::SP);
    } else if (ARM64::FPR64RegClass.hasSubClassEq(RC))
      Opc = ARM64::STRDui;
    break;
  case 16:
    if (ARM64::FPR128RegClass.hasSubClassEq(RC))
      Opc = ARM64::STRQui;
    else if (ARM64::DDRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register store without NEON");
      Opc = ARM64::ST1Twov1d, Offset = false;
    }
    break;
  case 24:
    if (ARM64::DDDRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register store without NEON");
      Opc = ARM64::ST1Threev1d, Offset = false;
    }
    break;
  case 32:
    if (ARM64::DDDDRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register store without NEON");
      Opc = ARM64::ST1Fourv1d, Offset = false;
    } else if (ARM64::QQRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register store without NEON");
      Opc = ARM64::ST1Twov2d, Offset = false;
    }
    break;
  case 48:
    if (ARM64::QQQRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register store without NEON");
      Opc = ARM64::ST1Threev2d, Offset = false;
    }
    break;
  case 64:
    if (ARM64::QQQQRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register store without NEON");
      Opc = ARM64::ST1Fourv2d, Offset = false;
    }
    break;
  }
  assert(Opc && "Unknown register class");

  const MachineInstrBuilder &MI = BuildMI(MBB, MBBI, DL, get(Opc))
                                      .addReg(SrcReg, getKillRegState(isKill))
                                      .addFrameIndex(FI);

  if (Offset)
    MI.addImm(0);
  MI.addMemOperand(MMO);
}

void ARM64InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
                                          MachineBasicBlock::iterator MBBI,
                                          unsigned DestReg, int FI,
                                          const TargetRegisterClass *RC,
                                          const TargetRegisterInfo *TRI) const {
  DebugLoc DL;
  if (MBBI != MBB.end())
    DL = MBBI->getDebugLoc();
  MachineFunction &MF = *MBB.getParent();
  MachineFrameInfo &MFI = *MF.getFrameInfo();
  unsigned Align = MFI.getObjectAlignment(FI);
  MachinePointerInfo PtrInfo(PseudoSourceValue::getFixedStack(FI));
  MachineMemOperand *MMO = MF.getMachineMemOperand(
      PtrInfo, MachineMemOperand::MOLoad, MFI.getObjectSize(FI), Align);

  unsigned Opc = 0;
  bool Offset = true;
  switch (RC->getSize()) {
  case 1:
    if (ARM64::FPR8RegClass.hasSubClassEq(RC))
      Opc = ARM64::LDRBui;
    break;
  case 2:
    if (ARM64::FPR16RegClass.hasSubClassEq(RC))
      Opc = ARM64::LDRHui;
    break;
  case 4:
    if (ARM64::GPR32allRegClass.hasSubClassEq(RC)) {
      Opc = ARM64::LDRWui;
      if (TargetRegisterInfo::isVirtualRegister(DestReg))
        MF.getRegInfo().constrainRegClass(DestReg, &ARM64::GPR32RegClass);
      else
        assert(DestReg != ARM64::WSP);
    } else if (ARM64::FPR32RegClass.hasSubClassEq(RC))
      Opc = ARM64::LDRSui;
    break;
  case 8:
    if (ARM64::GPR64allRegClass.hasSubClassEq(RC)) {
      Opc = ARM64::LDRXui;
      if (TargetRegisterInfo::isVirtualRegister(DestReg))
        MF.getRegInfo().constrainRegClass(DestReg, &ARM64::GPR64RegClass);
      else
        assert(DestReg != ARM64::SP);
    } else if (ARM64::FPR64RegClass.hasSubClassEq(RC))
      Opc = ARM64::LDRDui;
    break;
  case 16:
    if (ARM64::FPR128RegClass.hasSubClassEq(RC))
      Opc = ARM64::LDRQui;
    else if (ARM64::DDRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register load without NEON");
      Opc = ARM64::LD1Twov1d, Offset = false;
    }
    break;
  case 24:
    if (ARM64::DDDRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register load without NEON");
      Opc = ARM64::LD1Threev1d, Offset = false;
    }
    break;
  case 32:
    if (ARM64::DDDDRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register load without NEON");
      Opc = ARM64::LD1Fourv1d, Offset = false;
    } else if (ARM64::QQRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register load without NEON");
      Opc = ARM64::LD1Twov2d, Offset = false;
    }
    break;
  case 48:
    if (ARM64::QQQRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register load without NEON");
      Opc = ARM64::LD1Threev2d, Offset = false;
    }
    break;
  case 64:
    if (ARM64::QQQQRegClass.hasSubClassEq(RC)) {
      assert(getSubTarget().hasNEON() &&
             "Unexpected register load without NEON");
      Opc = ARM64::LD1Fourv2d, Offset = false;
    }
    break;
  }
  assert(Opc && "Unknown register class");

  const MachineInstrBuilder &MI = BuildMI(MBB, MBBI, DL, get(Opc))
                                      .addReg(DestReg, getDefRegState(true))
                                      .addFrameIndex(FI);
  if (Offset)
    MI.addImm(0);
  MI.addMemOperand(MMO);
}

void llvm::emitFrameOffset(MachineBasicBlock &MBB,
                           MachineBasicBlock::iterator MBBI, DebugLoc DL,
                           unsigned DestReg, unsigned SrcReg, int Offset,
                           const ARM64InstrInfo *TII, MachineInstr::MIFlag Flag,
                           bool SetNZCV) {
  if (DestReg == SrcReg && Offset == 0)
    return;

  bool isSub = Offset < 0;
  if (isSub)
    Offset = -Offset;

  // FIXME: If the offset won't fit in 24-bits, compute the offset into a
  // scratch register.  If DestReg is a virtual register, use it as the
  // scratch register; otherwise, create a new virtual register (to be
  // replaced by the scavenger at the end of PEI).  That case can be optimized
  // slightly if DestReg is SP which is always 16-byte aligned, so the scratch
  // register can be loaded with offset%8 and the add/sub can use an extending
  // instruction with LSL#3.
  // Currently the function handles any offsets but generates a poor sequence
  // of code.
  //  assert(Offset < (1 << 24) && "unimplemented reg plus immediate");

  unsigned Opc;
  if (SetNZCV)
    Opc = isSub ? ARM64::SUBSXri : ARM64::ADDSXri;
  else
    Opc = isSub ? ARM64::SUBXri : ARM64::ADDXri;
  const unsigned MaxEncoding = 0xfff;
  const unsigned ShiftSize = 12;
  const unsigned MaxEncodableValue = MaxEncoding << ShiftSize;
  while (((unsigned)Offset) >= (1 << ShiftSize)) {
    unsigned ThisVal;
    if (((unsigned)Offset) > MaxEncodableValue) {
      ThisVal = MaxEncodableValue;
    } else {
      ThisVal = Offset & MaxEncodableValue;
    }
    assert((ThisVal >> ShiftSize) <= MaxEncoding &&
           "Encoding cannot handle value that big");
    BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
        .addReg(SrcReg)
        .addImm(ThisVal >> ShiftSize)
        .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, ShiftSize))
        .setMIFlag(Flag);

    SrcReg = DestReg;
    Offset -= ThisVal;
    if (Offset == 0)
      return;
  }
  BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
      .addReg(SrcReg)
      .addImm(Offset)
      .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, 0))
      .setMIFlag(Flag);
}

MachineInstr *
ARM64InstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
                                      const SmallVectorImpl<unsigned> &Ops,
                                      int FrameIndex) const {
  // This is a bit of a hack. Consider this instruction:
  //
  //   %vreg0<def> = COPY %SP; GPR64all:%vreg0
  //
  // We explicitly chose GPR64all for the virtual register so such a copy might
  // be eliminated by RegisterCoalescer. However, that may not be possible, and
  // %vreg0 may even spill. We can't spill %SP, and since it is in the GPR64all
  // register class, TargetInstrInfo::foldMemoryOperand() is going to try.
  //
  // To prevent that, we are going to constrain the %vreg0 register class here.
  //
  // <rdar://problem/11522048>
  //
  if (MI->isCopy()) {
    unsigned DstReg = MI->getOperand(0).getReg();
    unsigned SrcReg = MI->getOperand(1).getReg();
    if (SrcReg == ARM64::SP && TargetRegisterInfo::isVirtualRegister(DstReg)) {
      MF.getRegInfo().constrainRegClass(DstReg, &ARM64::GPR64RegClass);
      return nullptr;
    }
    if (DstReg == ARM64::SP && TargetRegisterInfo::isVirtualRegister(SrcReg)) {
      MF.getRegInfo().constrainRegClass(SrcReg, &ARM64::GPR64RegClass);
      return nullptr;
    }
  }

  // Cannot fold.
  return nullptr;
}

int llvm::isARM64FrameOffsetLegal(const MachineInstr &MI, int &Offset,
                                  bool *OutUseUnscaledOp,
                                  unsigned *OutUnscaledOp,
                                  int *EmittableOffset) {
  int Scale = 1;
  bool IsSigned = false;
  // The ImmIdx should be changed case by case if it is not 2.
  unsigned ImmIdx = 2;
  unsigned UnscaledOp = 0;
  // Set output values in case of early exit.
  if (EmittableOffset)
    *EmittableOffset = 0;
  if (OutUseUnscaledOp)
    *OutUseUnscaledOp = false;
  if (OutUnscaledOp)
    *OutUnscaledOp = 0;
  switch (MI.getOpcode()) {
  default:
    assert(0 && "unhandled opcode in rewriteARM64FrameIndex");
  // Vector spills/fills can't take an immediate offset.
  case ARM64::LD1Twov2d:
  case ARM64::LD1Threev2d:
  case ARM64::LD1Fourv2d:
  case ARM64::LD1Twov1d:
  case ARM64::LD1Threev1d:
  case ARM64::LD1Fourv1d:
  case ARM64::ST1Twov2d:
  case ARM64::ST1Threev2d:
  case ARM64::ST1Fourv2d:
  case ARM64::ST1Twov1d:
  case ARM64::ST1Threev1d:
  case ARM64::ST1Fourv1d:
    return ARM64FrameOffsetCannotUpdate;
  case ARM64::PRFMui:
    Scale = 8;
    UnscaledOp = ARM64::PRFUMi;
    break;
  case ARM64::LDRXui:
    Scale = 8;
    UnscaledOp = ARM64::LDURXi;
    break;
  case ARM64::LDRWui:
    Scale = 4;
    UnscaledOp = ARM64::LDURWi;
    break;
  case ARM64::LDRBui:
    Scale = 1;
    UnscaledOp = ARM64::LDURBi;
    break;
  case ARM64::LDRHui:
    Scale = 2;
    UnscaledOp = ARM64::LDURHi;
    break;
  case ARM64::LDRSui:
    Scale = 4;
    UnscaledOp = ARM64::LDURSi;
    break;
  case ARM64::LDRDui:
    Scale = 8;
    UnscaledOp = ARM64::LDURDi;
    break;
  case ARM64::LDRQui:
    Scale = 16;
    UnscaledOp = ARM64::LDURQi;
    break;
  case ARM64::LDRBBui:
    Scale = 1;
    UnscaledOp = ARM64::LDURBBi;
    break;
  case ARM64::LDRHHui:
    Scale = 2;
    UnscaledOp = ARM64::LDURHHi;
    break;
  case ARM64::LDRSBXui:
    Scale = 1;
    UnscaledOp = ARM64::LDURSBXi;
    break;
  case ARM64::LDRSBWui:
    Scale = 1;
    UnscaledOp = ARM64::LDURSBWi;
    break;
  case ARM64::LDRSHXui:
    Scale = 2;
    UnscaledOp = ARM64::LDURSHXi;
    break;
  case ARM64::LDRSHWui:
    Scale = 2;
    UnscaledOp = ARM64::LDURSHWi;
    break;
  case ARM64::LDRSWui:
    Scale = 4;
    UnscaledOp = ARM64::LDURSWi;
    break;

  case ARM64::STRXui:
    Scale = 8;
    UnscaledOp = ARM64::STURXi;
    break;
  case ARM64::STRWui:
    Scale = 4;
    UnscaledOp = ARM64::STURWi;
    break;
  case ARM64::STRBui:
    Scale = 1;
    UnscaledOp = ARM64::STURBi;
    break;
  case ARM64::STRHui:
    Scale = 2;
    UnscaledOp = ARM64::STURHi;
    break;
  case ARM64::STRSui:
    Scale = 4;
    UnscaledOp = ARM64::STURSi;
    break;
  case ARM64::STRDui:
    Scale = 8;
    UnscaledOp = ARM64::STURDi;
    break;
  case ARM64::STRQui:
    Scale = 16;
    UnscaledOp = ARM64::STURQi;
    break;
  case ARM64::STRBBui:
    Scale = 1;
    UnscaledOp = ARM64::STURBBi;
    break;
  case ARM64::STRHHui:
    Scale = 2;
    UnscaledOp = ARM64::STURHHi;
    break;

  case ARM64::LDPXi:
  case ARM64::LDPDi:
  case ARM64::STPXi:
  case ARM64::STPDi:
    IsSigned = true;
    Scale = 8;
    break;
  case ARM64::LDPQi:
  case ARM64::STPQi:
    IsSigned = true;
    Scale = 16;
    break;
  case ARM64::LDPWi:
  case ARM64::LDPSi:
  case ARM64::STPWi:
  case ARM64::STPSi:
    IsSigned = true;
    Scale = 4;
    break;

  case ARM64::LDURXi:
  case ARM64::LDURWi:
  case ARM64::LDURBi:
  case ARM64::LDURHi:
  case ARM64::LDURSi:
  case ARM64::LDURDi:
  case ARM64::LDURQi:
  case ARM64::LDURHHi:
  case ARM64::LDURBBi:
  case ARM64::LDURSBXi:
  case ARM64::LDURSBWi:
  case ARM64::LDURSHXi:
  case ARM64::LDURSHWi:
  case ARM64::LDURSWi:
  case ARM64::STURXi:
  case ARM64::STURWi:
  case ARM64::STURBi:
  case ARM64::STURHi:
  case ARM64::STURSi:
  case ARM64::STURDi:
  case ARM64::STURQi:
  case ARM64::STURBBi:
  case ARM64::STURHHi:
    Scale = 1;
    break;
  }

  Offset += MI.getOperand(ImmIdx).getImm() * Scale;

  bool useUnscaledOp = false;
  // If the offset doesn't match the scale, we rewrite the instruction to
  // use the unscaled instruction instead. Likewise, if we have a negative
  // offset (and have an unscaled op to use).
  if ((Offset & (Scale - 1)) != 0 || (Offset < 0 && UnscaledOp != 0))
    useUnscaledOp = true;

  // Use an unscaled addressing mode if the instruction has a negative offset
  // (or if the instruction is already using an unscaled addressing mode).
  unsigned MaskBits;
  if (IsSigned) {
    // ldp/stp instructions.
    MaskBits = 7;
    Offset /= Scale;
  } else if (UnscaledOp == 0 || useUnscaledOp) {
    MaskBits = 9;
    IsSigned = true;
    Scale = 1;
  } else {
    MaskBits = 12;
    IsSigned = false;
    Offset /= Scale;
  }

  // Attempt to fold address computation.
  int MaxOff = (1 << (MaskBits - IsSigned)) - 1;
  int MinOff = (IsSigned ? (-MaxOff - 1) : 0);
  if (Offset >= MinOff && Offset <= MaxOff) {
    if (EmittableOffset)
      *EmittableOffset = Offset;
    Offset = 0;
  } else {
    int NewOff = Offset < 0 ? MinOff : MaxOff;
    if (EmittableOffset)
      *EmittableOffset = NewOff;
    Offset = (Offset - NewOff) * Scale;
  }
  if (OutUseUnscaledOp)
    *OutUseUnscaledOp = useUnscaledOp;
  if (OutUnscaledOp)
    *OutUnscaledOp = UnscaledOp;
  return ARM64FrameOffsetCanUpdate |
         (Offset == 0 ? ARM64FrameOffsetIsLegal : 0);
}

bool llvm::rewriteARM64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
                                  unsigned FrameReg, int &Offset,
                                  const ARM64InstrInfo *TII) {
  unsigned Opcode = MI.getOpcode();
  unsigned ImmIdx = FrameRegIdx + 1;

  if (Opcode == ARM64::ADDSXri || Opcode == ARM64::ADDXri) {
    Offset += MI.getOperand(ImmIdx).getImm();
    emitFrameOffset(*MI.getParent(), MI, MI.getDebugLoc(),
                    MI.getOperand(0).getReg(), FrameReg, Offset, TII,
                    MachineInstr::NoFlags, (Opcode == ARM64::ADDSXri));
    MI.eraseFromParent();
    Offset = 0;
    return true;
  }

  int NewOffset;
  unsigned UnscaledOp;
  bool UseUnscaledOp;
  int Status = isARM64FrameOffsetLegal(MI, Offset, &UseUnscaledOp, &UnscaledOp,
                                       &NewOffset);
  if (Status & ARM64FrameOffsetCanUpdate) {
    if (Status & ARM64FrameOffsetIsLegal)
      // Replace the FrameIndex with FrameReg.
      MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
    if (UseUnscaledOp)
      MI.setDesc(TII->get(UnscaledOp));

    MI.getOperand(ImmIdx).ChangeToImmediate(NewOffset);
    return Offset == 0;
  }

  return false;
}

void ARM64InstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
  NopInst.setOpcode(ARM64::HINT);
  NopInst.addOperand(MCOperand::CreateImm(0));
}