Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

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

include "AVRInstrFormats.td"

//===----------------------------------------------------------------------===//
// AVR Type Profiles
//===----------------------------------------------------------------------===//

def SDT_AVRCallSeqStart : SDCallSeqStart<[SDTCisVT<0, i16>, SDTCisVT<1, i16>]>;
def SDT_AVRCallSeqEnd : SDCallSeqEnd<[SDTCisVT<0, i16>, SDTCisVT<1, i16>]>;
def SDT_AVRCall : SDTypeProfile<0, -1, [SDTCisVT<0, iPTR>]>;
def SDT_AVRWrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>, SDTCisPtrTy<0>]>;
def SDT_AVRBrcond : SDTypeProfile<0, 2,
                                  [SDTCisVT<0, OtherVT>, SDTCisVT<1, i8>]>;
def SDT_AVRCmp : SDTypeProfile<0, 2, [SDTCisSameAs<0, 1>]>;
def SDT_AVRTst : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
def SDT_AVRSelectCC : SDTypeProfile<1, 3, [SDTCisSameAs<0, 1>,
                                    SDTCisSameAs<1, 2>, SDTCisVT<3, i8>]>;

//===----------------------------------------------------------------------===//
// AVR Specific Node Definitions
//===----------------------------------------------------------------------===//

def AVRretflag : SDNode<"AVRISD::RET_FLAG", SDTNone,
                        [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
def AVRretiflag : SDNode<"AVRISD::RETI_FLAG", SDTNone,
                         [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;

def AVRcallseq_start : SDNode<"ISD::CALLSEQ_START", SDT_AVRCallSeqStart,
                              [SDNPHasChain, SDNPOutGlue]>;
def AVRcallseq_end : SDNode<"ISD::CALLSEQ_END", SDT_AVRCallSeqEnd,
                            [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;

def AVRcall : SDNode<"AVRISD::CALL", SDT_AVRCall,
                     [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue, SDNPVariadic]>;

def AVRWrapper : SDNode<"AVRISD::WRAPPER", SDT_AVRWrapper>;

def AVRbrcond : SDNode<"AVRISD::BRCOND", SDT_AVRBrcond,
                       [SDNPHasChain, SDNPInGlue]>;
def AVRcmp : SDNode<"AVRISD::CMP", SDT_AVRCmp, [SDNPOutGlue]>;
def AVRcmpc : SDNode<"AVRISD::CMPC", SDT_AVRCmp, [SDNPInGlue, SDNPOutGlue]>;
def AVRtst : SDNode<"AVRISD::TST", SDT_AVRTst, [SDNPOutGlue]>;
def AVRselectcc: SDNode<"AVRISD::SELECT_CC", SDT_AVRSelectCC, [SDNPInGlue]>;

// Shift nodes.
def AVRlsl : SDNode<"AVRISD::LSL", SDTIntUnaryOp>;
def AVRlsr : SDNode<"AVRISD::LSR", SDTIntUnaryOp>;
def AVRrol : SDNode<"AVRISD::ROL", SDTIntUnaryOp>;
def AVRror : SDNode<"AVRISD::ROR", SDTIntUnaryOp>;
def AVRasr : SDNode<"AVRISD::ASR", SDTIntUnaryOp>;

// Pseudo shift nodes for non-constant shift amounts.
def AVRlslLoop : SDNode<"AVRISD::LSLLOOP", SDTIntShiftOp>;
def AVRlsrLoop : SDNode<"AVRISD::LSRLOOP", SDTIntShiftOp>;
def AVRrolLoop : SDNode<"AVRISD::ROLLOOP", SDTIntShiftOp>;
def AVRrorLoop : SDNode<"AVRISD::RORLOOP", SDTIntShiftOp>;
def AVRasrLoop : SDNode<"AVRISD::ASRLOOP", SDTIntShiftOp>;

//===----------------------------------------------------------------------===//
// AVR Operands, Complex Patterns and Transformations Definitions.
//===----------------------------------------------------------------------===//

def imm8_neg_XFORM : SDNodeXForm<imm,
[{
  return CurDAG->getTargetConstant(-N->getAPIntValue(), SDLoc(N), MVT::i8);
}]>;

def imm16_neg_XFORM : SDNodeXForm<imm,
[{
  return CurDAG->getTargetConstant(-N->getAPIntValue(), SDLoc(N), MVT::i16);
}]>;

def imm0_63_neg : PatLeaf<(imm),
[{
  int64_t val = -N->getSExtValue();
  return val >= 0 && val < 64;
}], imm16_neg_XFORM>;

def uimm6 : PatLeaf<(imm), [{ return isUInt<6>(N->getZExtValue()); }]>;

// imm_com8_XFORM - Return the complement of a imm_com8 value
def imm_com8_XFORM : SDNodeXForm<imm, [{
  return CurDAG->getTargetConstant(~((uint8_t)N->getZExtValue()), SDLoc(N),
                                   MVT::i8);
}]>;

// imm_com8 - Match an immediate that is a complement
// of a 8-bit immediate.
// Note: this pattern doesn't require an encoder method and such, as it's
// only used on aliases (Pat<> and InstAlias<>). The actual encoding
// is handled by the destination instructions, which use imm_com8.
def imm_com8_asmoperand : AsmOperandClass { let Name = "ImmCom8"; }
def imm_com8 : Operand<i8> {
  let ParserMatchClass = imm_com8_asmoperand;
}

def ioaddr_XFORM : SDNodeXForm<imm,
[{
  uint8_t offset = Subtarget->getIORegisterOffset();
  return CurDAG->getTargetConstant(uint8_t(N->getZExtValue()) - offset,
                                   SDLoc(N), MVT::i8);
}]>;

def iobitpos8_XFORM : SDNodeXForm<imm,
[{
  return CurDAG->getTargetConstant(Log2_32(uint8_t(N->getZExtValue())),
                                   SDLoc(N), MVT::i8);
}]>;

def iobitposn8_XFORM : SDNodeXForm<imm,
[{
  return CurDAG->getTargetConstant(Log2_32(uint8_t(~N->getZExtValue())),
                                   SDLoc(N), MVT::i8);
}]>;

def ioaddr8 : PatLeaf<(imm),
[{
  uint8_t offset = Subtarget->getIORegisterOffset();
  uint64_t val = N->getZExtValue() - offset;
  return val < 0x40;
}], ioaddr_XFORM>;

def lowioaddr8 : PatLeaf<(imm),
[{
  uint8_t offset = Subtarget->getIORegisterOffset();
  uint64_t val = N->getZExtValue() - offset;
  return val < 0x20;
}], ioaddr_XFORM>;

def ioaddr16 : PatLeaf<(imm),
[{
  uint8_t offset = Subtarget->getIORegisterOffset();
  uint64_t val = N->getZExtValue() - offset;
  return val < 0x3f;
}], ioaddr_XFORM>;

def iobitpos8 : PatLeaf<(imm),
[{
  return isPowerOf2_32(uint8_t(N->getZExtValue()));
}], iobitpos8_XFORM>;

def iobitposn8 : PatLeaf<(imm),
[{
  return isPowerOf2_32(uint8_t(~N->getZExtValue()));
}], iobitposn8_XFORM>;

def MemriAsmOperand : AsmOperandClass {
  let Name = "Memri";
  let ParserMethod = "parseMemriOperand";
}

/// Address operand for `reg+imm` used by STD and LDD.
def memri : Operand<iPTR>
{
  let MIOperandInfo = (ops PTRDISPREGS, i16imm);

  let PrintMethod = "printMemri";
  let EncoderMethod = "encodeMemri";

  let ParserMatchClass = MemriAsmOperand;
}

// Address operand for `SP+imm` used by STD{W}SPQRr
def memspi : Operand<iPTR>
{
  let MIOperandInfo = (ops GPRSP, i16imm);
}

def relbrtarget_7 : Operand<OtherVT>
{
    let PrintMethod   = "printPCRelImm";
    let EncoderMethod = "encodeRelCondBrTarget<AVR::fixup_7_pcrel>";
}

def brtarget_13 : Operand<OtherVT>
{
    let PrintMethod   = "printPCRelImm";
    let EncoderMethod = "encodeRelCondBrTarget<AVR::fixup_13_pcrel>";
}

// The target of a 22 or 16-bit call/jmp instruction.
def call_target : Operand<iPTR>
{
    let EncoderMethod = "encodeCallTarget";
    let DecoderMethod = "decodeCallTarget";
}

// A 16-bit address (which can lead to an R_AVR_16 relocation).
def imm16 : Operand<i16>
{
    let EncoderMethod = "encodeImm<AVR::fixup_16, 2>";
}

/// A 6-bit immediate used in the ADIW/SBIW instructions.
def imm_arith6 : Operand<i16>
{
    let EncoderMethod = "encodeImm<AVR::fixup_6_adiw, 0>";
}

/// An 8-bit immediate inside an instruction with the same format
/// as the `LDI` instruction (the `FRdK` format).
def imm_ldi8 : Operand<i8>
{
    let EncoderMethod = "encodeImm<AVR::fixup_ldi, 0>";
}

/// A 5-bit port number used in SBIC and friends (the `FIOBIT` format).
def imm_port5 : Operand<i8>
{
    let EncoderMethod = "encodeImm<AVR::fixup_port5, 0>";
}

/// A 6-bit port number used in the `IN` instruction and friends (the
/// `FIORdA` format.
def imm_port6 : Operand<i8>
{
    let EncoderMethod = "encodeImm<AVR::fixup_port6, 0>";
}

// Addressing mode pattern reg+imm6
def addr : ComplexPattern<iPTR, 2, "SelectAddr", [], [SDNPWantRoot]>;

// AsmOperand class for a pointer register.
// Used with the LD/ST family of instructions.
// See FSTLD in AVRInstrFormats.td
def PtrRegAsmOperand : AsmOperandClass
{
   let Name = "Reg";
}

// A special operand type for the LD/ST instructions.
// It converts the pointer register number into a two-bit field used in the
// instruction.
def LDSTPtrReg : Operand<i16>
{
    let MIOperandInfo = (ops PTRREGS);
    let EncoderMethod = "encodeLDSTPtrReg";

    let ParserMatchClass = PtrRegAsmOperand;
}

// A special operand type for the LDD/STD instructions.
// It behaves identically to the LD/ST version, except restricts
// the pointer registers to Y and Z.
def LDDSTDPtrReg : Operand<i16>
{
    let MIOperandInfo = (ops PTRDISPREGS);
    let EncoderMethod = "encodeLDSTPtrReg";

    let ParserMatchClass = PtrRegAsmOperand;
}

//===----------------------------------------------------------------------===//
// AVR predicates for subtarget features
//===----------------------------------------------------------------------===//

def HasSRAM       :    Predicate<"Subtarget->hasSRAM()">,
                         AssemblerPredicate<(all_of FeatureSRAM)>;

def HasJMPCALL    :    Predicate<"Subtarget->hasJMPCALL()">,
                         AssemblerPredicate<(all_of FeatureJMPCALL)>;

def HasIJMPCALL   :    Predicate<"Subtarget->hasIJMPCALL()">,
                         AssemblerPredicate<(all_of FeatureIJMPCALL)>;

def HasEIJMPCALL  :    Predicate<"Subtarget->hasEIJMPCALL()">,
                         AssemblerPredicate<(all_of FeatureEIJMPCALL)>;

def HasADDSUBIW   :    Predicate<"Subtarget->hasADDSUBIW()">,
                         AssemblerPredicate<(all_of FeatureADDSUBIW)>;

def HasSmallStack :    Predicate<"Subtarget->HasSmallStack()">,
                         AssemblerPredicate<(all_of FeatureSmallStack)>;

def HasMOVW       :    Predicate<"Subtarget->hasMOVW()">,
                         AssemblerPredicate<(all_of FeatureMOVW)>;

def HasLPM        :    Predicate<"Subtarget->hasLPM()">,
                         AssemblerPredicate<(all_of FeatureLPM)>;

def HasLPMX       :    Predicate<"Subtarget->hasLPMX()">,
                         AssemblerPredicate<(all_of FeatureLPMX)>;

def HasELPM       :    Predicate<"Subtarget->hasELPM()">,
                         AssemblerPredicate<(all_of FeatureELPM)>;

def HasELPMX      :    Predicate<"Subtarget->hasELPMX()">,
                         AssemblerPredicate<(all_of FeatureELPMX)>;

def HasSPM        :    Predicate<"Subtarget->hasSPM()">,
                         AssemblerPredicate<(all_of FeatureSPM)>;

def HasSPMX       :    Predicate<"Subtarget->hasSPMX()">,
                         AssemblerPredicate<(all_of FeatureSPMX)>;

def HasDES        :    Predicate<"Subtarget->hasDES()">,
                         AssemblerPredicate<(all_of FeatureDES)>;

def SupportsRMW   :    Predicate<"Subtarget->supportsRMW()">,
                         AssemblerPredicate<(all_of FeatureRMW)>;

def SupportsMultiplication : Predicate<"Subtarget->supportsMultiplication()">,
                               AssemblerPredicate<(all_of FeatureMultiplication)>;

def HasBREAK      :    Predicate<"Subtarget->hasBREAK()">,
                         AssemblerPredicate<(all_of FeatureBREAK)>;

def HasTinyEncoding : Predicate<"Subtarget->hasTinyEncoding()">,
                        AssemblerPredicate<(all_of FeatureTinyEncoding)>;


// AVR specific condition code. These correspond to AVR_*_COND in
// AVRInstrInfo.td. They must be kept in synch.
def AVR_COND_EQ : PatLeaf<(i8 0)>;
def AVR_COND_NE : PatLeaf<(i8 1)>;
def AVR_COND_GE : PatLeaf<(i8 2)>;
def AVR_COND_LT : PatLeaf<(i8 3)>;
def AVR_COND_SH : PatLeaf<(i8 4)>;
def AVR_COND_LO : PatLeaf<(i8 5)>;
def AVR_COND_MI : PatLeaf<(i8 6)>;
def AVR_COND_PL : PatLeaf<(i8 7)>;


//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
// AVR Instruction list
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//

// ADJCALLSTACKDOWN/UP implicitly use/def SP because they may be expanded into
// a stack adjustment and the codegen must know that they may modify the stack
// pointer before prolog-epilog rewriting occurs.
// Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
// sub / add which can clobber SREG.
let Defs = [SP, SREG],
Uses = [SP] in
{
  def ADJCALLSTACKDOWN : Pseudo<(outs),
                                (ins i16imm:$amt, i16imm:$amt2),
                                "#ADJCALLSTACKDOWN",
                                [(AVRcallseq_start timm:$amt, timm:$amt2)]>;

  // R31R30 is used to update SP, since it is a scratch reg and this instruction
  // is placed after the function call then R31R30 should be always free.
  //let Defs = [R31R30],
  //Uses = [R31R30] in
  //:TODO: if we enable this, the pseudo is killed because it looks dead
  def ADJCALLSTACKUP : Pseudo<(outs),
                              (ins i16imm:$amt1, i16imm:$amt2),
                              "#ADJCALLSTACKUP",
                              [(AVRcallseq_end timm:$amt1, timm:$amt2)]>;
}

//===----------------------------------------------------------------------===//
// Addition
//===----------------------------------------------------------------------===//
let isCommutable = 1,
Constraints = "$src = $rd",
Defs = [SREG] in
{
  // ADD Rd, Rr
  // Adds two 8-bit registers.
  def ADDRdRr : FRdRr<0b0000,
                      0b11,
                      (outs GPR8:$rd),
                      (ins GPR8:$src, GPR8:$rr),
                      "add\t$rd, $rr",
                      [(set i8:$rd, (add i8:$src, i8:$rr)),
                       (implicit SREG)]>;

  // ADDW Rd+1:Rd, Rr+1:Rr
  // Pseudo instruction to add four 8-bit registers as two 16-bit values.
  //
  // Expands to:
  // add Rd,    Rr
  // adc Rd+1, Rr+1
  def ADDWRdRr : Pseudo<(outs DREGS:$rd),
                        (ins DREGS:$src, DREGS:$rr),
                        "addw\t$rd, $rr",
                        [(set i16:$rd, (add i16:$src, i16:$rr)),
                         (implicit SREG)]>;

  // ADC Rd, Rr
  // Adds two 8-bit registers with carry.
  let Uses = [SREG] in
  def ADCRdRr : FRdRr<0b0001,
                      0b11,
                      (outs GPR8:$rd),
                      (ins GPR8:$src, GPR8:$rr),
                      "adc\t$rd, $rr",
                      [(set i8:$rd, (adde i8:$src, i8:$rr)),
                       (implicit SREG)]>;

  // ADCW Rd+1:Rd, Rr+1:Rr
  // Pseudo instruction to add four 8-bit registers as two 16-bit values with
  // carry.
  //
  // Expands to:
  // adc Rd,   Rr
  // adc Rd+1, Rr+1
  let Uses = [SREG] in
  def ADCWRdRr : Pseudo<(outs DREGS:$rd),
                        (ins DREGS:$src, DREGS:$rr),
                        "adcw\t$rd, $rr",
                        [(set i16:$rd, (adde i16:$src, i16:$rr)),
                         (implicit SREG)]>;

  // AIDW Rd, k
  // Adds an immediate 6-bit value K to Rd, placing the result in Rd.
  def ADIWRdK : FWRdK<0b0,
                      (outs IWREGS:$rd),
                      (ins IWREGS:$src, imm_arith6:$k),
                      "adiw\t$rd, $k",
                      [(set i16:$rd, (add i16:$src, uimm6:$k)),
                       (implicit SREG)]>,
                Requires<[HasADDSUBIW]>;
}

//===----------------------------------------------------------------------===//
// Subtraction
//===----------------------------------------------------------------------===//
let Constraints = "$src = $rd",
Defs = [SREG] in
{
  // SUB Rd, Rr
  // Subtracts the 8-bit value of Rr from Rd and places the value in Rd.
  def SUBRdRr : FRdRr<0b0001,
                      0b10,
                      (outs GPR8:$rd),
                      (ins GPR8:$src, GPR8:$rr),
                      "sub\t$rd, $rr",
                      [(set i8:$rd, (sub i8:$src, i8:$rr)),
                       (implicit SREG)]>;

  // SUBW Rd+1:Rd, Rr+1:Rr
  // Subtracts two 16-bit values and places the result into Rd.
  //
  // Expands to:
  // sub Rd,   Rr
  // sbc Rd+1, Rr+1
  def SUBWRdRr : Pseudo<(outs DREGS:$rd),
                        (ins DREGS:$src, DREGS:$rr),
                        "subw\t$rd, $rr",
                        [(set i16:$rd, (sub i16:$src, i16:$rr)),
                         (implicit SREG)]>;

  def SUBIRdK : FRdK<0b0101,
                     (outs LD8:$rd),
                     (ins LD8:$src, imm_ldi8:$k),
                     "subi\t$rd, $k",
                     [(set i8:$rd, (sub i8:$src, imm:$k)),
                      (implicit SREG)]>;

  // SUBIW Rd+1:Rd, K+1:K
  //
  // Expands to:
  // subi Rd,   K
  // sbci Rd+1, K+1
  def SUBIWRdK : Pseudo<(outs DLDREGS:$rd),
                        (ins DLDREGS:$src, i16imm:$rr),
                        "subiw\t$rd, $rr",
                        [(set i16:$rd, (sub i16:$src, imm:$rr)),
                         (implicit SREG)]>;

  def SBIWRdK : FWRdK<0b1,
                      (outs IWREGS:$rd),
                      (ins IWREGS:$src, imm_arith6:$k),
                      "sbiw\t$rd, $k",
                      [(set i16:$rd, (sub i16:$src, uimm6:$k)),
                       (implicit SREG)]>,
                Requires<[HasADDSUBIW]>;

  // Subtract with carry operations which must read the carry flag in SREG.
  let Uses = [SREG] in
  {
    def SBCRdRr : FRdRr<0b0000,
                        0b10,
                        (outs GPR8:$rd),
                        (ins GPR8:$src, GPR8:$rr),
                        "sbc\t$rd, $rr",
                        [(set i8:$rd, (sube i8:$src, i8:$rr)),
                         (implicit SREG)]>;

    // SBCW Rd+1:Rd, Rr+1:Rr
    //
    // Expands to:
    // sbc Rd,   Rr
    // sbc Rd+1, Rr+1
    def SBCWRdRr : Pseudo<(outs DREGS:$rd),
                          (ins DREGS:$src, DREGS:$rr),
                          "sbcw\t$rd, $rr",
                          [(set i16:$rd, (sube i16:$src, i16:$rr)),
                           (implicit SREG)]>;

    def SBCIRdK : FRdK<0b0100,
                       (outs LD8:$rd),
                       (ins LD8:$src, imm_ldi8:$k),
                       "sbci\t$rd, $k",
                       [(set i8:$rd, (sube i8:$src, imm:$k)),
                        (implicit SREG)]>;

    // SBCIW Rd+1:Rd, K+1:K
    // sbci Rd,   K
    // sbci Rd+1, K+1
    def SBCIWRdK : Pseudo<(outs DLDREGS:$rd),
                          (ins DLDREGS:$src, i16imm:$rr),
                          "sbciw\t$rd, $rr",
                          [(set i16:$rd, (sube i16:$src, imm:$rr)),
                           (implicit SREG)]>;
  }
}

//===----------------------------------------------------------------------===//
// Increment and Decrement
//===----------------------------------------------------------------------===//
let Constraints = "$src = $rd",
Defs = [SREG] in
{
  def INCRd : FRd<0b1001,
                  0b0100011,
                  (outs GPR8:$rd),
                  (ins GPR8:$src),
                  "inc\t$rd",
                  [(set i8:$rd, (add i8:$src, 1)), (implicit SREG)]>;

  def DECRd : FRd<0b1001,
                  0b0101010,
                  (outs GPR8:$rd),
                  (ins GPR8:$src),
                  "dec\t$rd",
                  [(set i8:$rd, (add i8:$src, -1)), (implicit SREG)]>;
}

//===----------------------------------------------------------------------===//
// Multiplication
//===----------------------------------------------------------------------===//

let isCommutable = 1,
Defs = [R1, R0, SREG] in
{
  // MUL Rd, Rr
  // Multiplies Rd by Rr and places the result into R1:R0.
  let usesCustomInserter = 1 in {
    def MULRdRr : FRdRr<0b1001, 0b11,
                        (outs),
                        (ins GPR8:$lhs, GPR8:$rhs),
                        "mul\t$lhs, $rhs",
                        [/*(set R1, R0, (smullohi i8:$lhs, i8:$rhs))*/]>,
                    Requires<[SupportsMultiplication]>;

    def MULSRdRr : FMUL2RdRr<0,
                             (outs),
                             (ins LD8:$lhs, LD8:$rhs),
                             "muls\t$lhs, $rhs",
                             []>,
                   Requires<[SupportsMultiplication]>;
  }

  def MULSURdRr : FMUL2RdRr<1,
                            (outs),
                            (ins LD8lo:$lhs, LD8lo:$rhs),
                            "mulsu\t$lhs, $rhs",
                            []>,
                  Requires<[SupportsMultiplication]>;

  def FMUL : FFMULRdRr<0b01,
                       (outs),
                       (ins LD8lo:$lhs, LD8lo:$rhs),
                       "fmul\t$lhs, $rhs",
                       []>,
             Requires<[SupportsMultiplication]>;

  def FMULS : FFMULRdRr<0b10,
                        (outs),
                        (ins LD8lo:$lhs, LD8lo:$rhs),
                        "fmuls\t$lhs, $rhs",
                        []>,
              Requires<[SupportsMultiplication]>;

  def FMULSU : FFMULRdRr<0b11,
                         (outs),
                         (ins LD8lo:$lhs, LD8lo:$rhs),
                         "fmulsu\t$lhs, $rhs",
                         []>,
               Requires<[SupportsMultiplication]>;
}

let Defs = [R15, R14, R13, R12, R11, R10, R9,
            R8, R7, R6, R5, R4, R3, R2, R1, R0] in
def DESK : FDES<(outs),
                (ins i8imm:$k),
                "des\t$k",
                []>,
           Requires<[HasDES]>;

//===----------------------------------------------------------------------===//
// Logic
//===----------------------------------------------------------------------===//
let Constraints = "$src = $rd",
Defs = [SREG] in
{
  // Register-Register logic instructions (which have the
  // property of commutativity).
  let isCommutable = 1 in
  {
    def ANDRdRr : FRdRr<0b0010,
                        0b00,
                        (outs GPR8:$rd),
                        (ins GPR8:$src, GPR8:$rr),
                        "and\t$rd, $rr",
                        [(set i8:$rd, (and i8:$src, i8:$rr)),
                         (implicit SREG)]>;

    // ANDW Rd+1:Rd, Rr+1:Rr
    //
    // Expands to:
    // and Rd,   Rr
    // and Rd+1, Rr+1
    def ANDWRdRr : Pseudo<(outs DREGS:$rd),
                          (ins DREGS:$src, DREGS:$rr),
                          "andw\t$rd, $rr",
                          [(set i16:$rd, (and i16:$src, i16:$rr)),
                           (implicit SREG)]>;

    def ORRdRr : FRdRr<0b0010,
                       0b10,
                       (outs GPR8:$rd),
                       (ins GPR8:$src, GPR8:$rr),
                       "or\t$rd, $rr",
                       [(set i8:$rd, (or i8:$src, i8:$rr)),
                        (implicit SREG)]>;

    // ORW Rd+1:Rd, Rr+1:Rr
    //
    // Expands to:
    // or Rd,   Rr
    // or Rd+1, Rr+1
    def ORWRdRr : Pseudo<(outs DREGS:$rd),
                         (ins DREGS:$src, DREGS:$rr),
                         "orw\t$rd, $rr",
                         [(set i16:$rd, (or i16:$src, i16:$rr)),
                          (implicit SREG)]>;

    def EORRdRr : FRdRr<0b0010,
                        0b01,
                        (outs GPR8:$rd),
                        (ins GPR8:$src, GPR8:$rr),
                        "eor\t$rd, $rr",
                        [(set i8:$rd, (xor i8:$src, i8:$rr)),
                         (implicit SREG)]>;

    // EORW Rd+1:Rd, Rr+1:Rr
    //
    // Expands to:
    // eor Rd,   Rr
    // eor Rd+1, Rr+1
    def EORWRdRr : Pseudo<(outs DREGS:$rd),
                          (ins DREGS:$src, DREGS:$rr),
                          "eorw\t$rd, $rr",
                          [(set i16:$rd, (xor i16:$src, i16:$rr)),
                           (implicit SREG)]>;
  }

  def ANDIRdK : FRdK<0b0111,
                     (outs LD8:$rd),
                     (ins LD8:$src, imm_ldi8:$k),
                     "andi\t$rd, $k",
                     [(set i8:$rd, (and i8:$src, imm:$k)),
                      (implicit SREG)]>;

  // ANDI Rd+1:Rd, K+1:K
  //
  // Expands to:
  // andi Rd,   K
  // andi Rd+1, K+1
  def ANDIWRdK : Pseudo<(outs DLDREGS:$rd),
                        (ins DLDREGS:$src, i16imm:$k),
                        "andiw\t$rd, $k",
                        [(set i16:$rd, (and i16:$src, imm:$k)),
                         (implicit SREG)]>;

  def ORIRdK : FRdK<0b0110,
                    (outs LD8:$rd),
                    (ins LD8:$src, imm_ldi8:$k),
                    "ori\t$rd, $k",
                    [(set i8:$rd, (or i8:$src, imm:$k)),
                     (implicit SREG)]>;

  // ORIW Rd+1:Rd, K+1,K
  //
  // Expands to:
  // ori Rd,   K
  // ori Rd+1, K+1
  def ORIWRdK : Pseudo<(outs DLDREGS:$rd),
                       (ins DLDREGS:$src, i16imm:$rr),
                       "oriw\t$rd, $rr",
                       [(set i16:$rd, (or i16:$src, imm:$rr)),
                        (implicit SREG)]>;
}

//===----------------------------------------------------------------------===//
// One's/Two's Complement
//===----------------------------------------------------------------------===//
let Constraints = "$src = $rd",
Defs = [SREG] in
{
  def COMRd : FRd<0b1001,
                  0b0100000,
                  (outs GPR8:$rd),
                  (ins GPR8:$src),
                  "com\t$rd",
                  [(set i8:$rd, (not i8:$src)), (implicit SREG)]>;

  // COMW Rd+1:Rd
  //
  // Expands to:
  // com Rd
  // com Rd+1
  def COMWRd : Pseudo<(outs DREGS:$rd),
                      (ins DREGS:$src),
                      "comw\t$rd",
                      [(set i16:$rd, (not i16:$src)), (implicit SREG)]>;

  //:TODO: optimize NEG for wider types
  def NEGRd : FRd<0b1001,
                  0b0100001,
                  (outs GPR8:$rd),
                  (ins GPR8:$src),
                  "neg\t$rd",
                  [(set i8:$rd, (ineg i8:$src)), (implicit SREG)]>;
}

// TST Rd
// Test for zero of minus.
// This operation is identical to a `Rd AND Rd`.
def : InstAlias<"tst\t$rd", (ANDRdRr GPR8:$rd, GPR8:$rd)>;

// SBR Rd, K
//
// Mnemonic alias to 'ORI Rd, K'. Same bit pattern, same operands,
// same everything.
def : InstAlias<"sbr\t$rd, $k",
                (ORIRdK LD8:$rd, imm_ldi8:$k),
                /* Disable display, so we don't override ORI */ 0>;

//===----------------------------------------------------------------------===//
// Jump instructions
//===----------------------------------------------------------------------===//
let isBarrier = 1,
isBranch = 1,
isTerminator = 1 in
{
  def RJMPk : FBRk<0,
                   (outs),
                   (ins brtarget_13:$target),
                   "rjmp\t$target",
                   [(br bb:$target)]>;

  let isIndirectBranch = 1,
  Uses = [R31R30] in
  def IJMP : F16<0b1001010000001001,
                 (outs),
                 (ins),
                 "ijmp",
                 []>,
             Requires<[HasIJMPCALL]>;

  let isIndirectBranch = 1,
  Uses = [R31R30] in
  def EIJMP : F16<0b1001010000011001,
                  (outs),
                  (ins),
                  "eijmp",
                  []>,
              Requires<[HasEIJMPCALL]>;

  def JMPk : F32BRk<0b110,
                    (outs),
                    (ins call_target:$k),
                    "jmp\t$k",
                    []>,
             Requires<[HasJMPCALL]>;
}

//===----------------------------------------------------------------------===//
// Call instructions
//===----------------------------------------------------------------------===//
let isCall = 1 in
{
  // SP is marked as a use to prevent stack-pointer assignments that appear
  // immediately before calls from potentially appearing dead.
  let Uses = [SP] in
  def RCALLk : FBRk<1,
                    (outs),
                    (ins brtarget_13:$target),
                    "rcall\t$target",
                    []>;

  // SP is marked as a use to prevent stack-pointer assignments that appear
  // immediately before calls from potentially appearing dead.
  let Uses = [SP, R31R30] in
  def ICALL : F16<0b1001010100001001,
                  (outs),
                  (ins variable_ops),
                  "icall",
                  []>,
              Requires<[HasIJMPCALL]>;

  // SP is marked as a use to prevent stack-pointer assignments that appear
  // immediately before calls from potentially appearing dead.
  let Uses = [SP, R31R30] in
  def EICALL : F16<0b1001010100011001,
                   (outs),
                   (ins variable_ops),
                   "eicall",
                   []>,
               Requires<[HasEIJMPCALL]>;

  // SP is marked as a use to prevent stack-pointer assignments that appear
  // immediately before calls from potentially appearing dead.
  //
  //:TODO: the imm field can be either 16 or 22 bits in devices with more
  // than 64k of ROM, fix it once we support the largest devices.
  let Uses = [SP] in
  def CALLk : F32BRk<0b111,
                     (outs),
                     (ins call_target:$k),
                     "call\t$k",
                     [(AVRcall imm:$k)]>,
              Requires<[HasJMPCALL]>;
}

//===----------------------------------------------------------------------===//
// Return instructions.
//===----------------------------------------------------------------------===//
let isTerminator = 1,
isReturn = 1,
isBarrier = 1 in
{
  def RET : F16<0b1001010100001000,
                (outs),
                (ins),
                "ret",
                [(AVRretflag)]>;

  def RETI : F16<0b1001010100011000,
                 (outs),
                 (ins),
                 "reti",
                 [(AVRretiflag)]>;
}

//===----------------------------------------------------------------------===//
// Compare operations.
//===----------------------------------------------------------------------===//
let Defs = [SREG] in
{
  // CPSE Rd, Rr
  // Compare Rd and Rr, skipping the next instruction if they are equal.
  let isBarrier = 1,
  isBranch = 1,
  isTerminator = 1 in
  def CPSE : FRdRr<0b0001,
                   0b00,
                   (outs),
                   (ins GPR8:$rd, GPR8:$rr),
                   "cpse\t$rd, $rr",
                   []>;

  def CPRdRr : FRdRr<0b0001,
                     0b01,
                     (outs),
                     (ins GPR8:$rd, GPR8:$rr),
                     "cp\t$rd, $rr",
                     [(AVRcmp i8:$rd, i8:$rr), (implicit SREG)]>;

  // CPW Rd+1:Rd, Rr+1:Rr
  //
  // Expands to:
  // cp  Rd,   Rr
  // cpc Rd+1, Rr+1
  def CPWRdRr : Pseudo<(outs),
                       (ins DREGS:$src, DREGS:$src2),
                       "cpw\t$src, $src2",
                       [(AVRcmp i16:$src, i16:$src2), (implicit SREG)]>;

  let Uses = [SREG] in
  def CPCRdRr : FRdRr<0b0000,
                      0b01,
                      (outs),
                      (ins GPR8:$rd, GPR8:$rr),
                      "cpc\t$rd, $rr",
                      [(AVRcmpc i8:$rd, i8:$rr), (implicit SREG)]>;

  // CPCW Rd+1:Rd. Rr+1:Rr
  //
  // Expands to:
  // cpc Rd,   Rr
  // cpc Rd+1, Rr+1
  let Uses = [SREG] in
  def CPCWRdRr : Pseudo<(outs),
                        (ins DREGS:$src, DREGS:$src2),
                        "cpcw\t$src, $src2",
                        [(AVRcmpc i16:$src, i16:$src2), (implicit SREG)]>;

  // CPI Rd, K
  // Compares a register with an 8 bit immediate.
  def CPIRdK : FRdK<0b0011,
                    (outs),
                    (ins LD8:$rd, imm_ldi8:$k),
                    "cpi\t$rd, $k",
                    [(AVRcmp i8:$rd, imm:$k), (implicit SREG)]>;
}

//===----------------------------------------------------------------------===//
// Register conditional skipping/branching operations.
//===----------------------------------------------------------------------===//
let isBranch = 1,
isTerminator = 1 in
{
  // Conditional skipping on GPR register bits, and
  // conditional skipping on IO register bits.
  let isBarrier = 1 in
  {
    def SBRCRrB : FRdB<0b10,
                       (outs),
                       (ins GPR8:$rr, i8imm:$b),
                       "sbrc\t$rr, $b",
                       []>;

    def SBRSRrB : FRdB<0b11,
                       (outs),
                       (ins GPR8:$rr, i8imm:$b),
                       "sbrs\t$rr, $b",
                       []>;

    def SBICAb : FIOBIT<0b01,
                        (outs),
                        (ins imm_port5:$a, i8imm:$b),
                        "sbic\t$a, $b",
                        []>;

    def SBISAb : FIOBIT<0b11,
                        (outs),
                        (ins imm_port5:$a, i8imm:$b),
                        "sbis\t$a, $b",
                        []>;
  }

  // Relative branches on status flag bits.
  let Uses = [SREG] in
  {
    // BRBS s, k
    // Branch if `s` flag in status register is set.
    def BRBSsk : FSK<0,
                     (outs),
                     (ins i8imm:$s, relbrtarget_7:$k),
                     "brbs\t$s, $k",
                     []>;

    // BRBC s, k
    // Branch if `s` flag in status register is clear.
    def BRBCsk : FSK<1,
                     (outs),
                     (ins i8imm:$s, relbrtarget_7:$k),
                     "brbc\t$s, $k",
                     []>;
  }
}


// BRCS k
// Branch if carry flag is set
def : InstAlias<"brcs\t$k", (BRBSsk 0, relbrtarget_7:$k)>;

// BRCC k
// Branch if carry flag is clear
def : InstAlias<"brcc\t$k", (BRBCsk 0, relbrtarget_7:$k)>;

// BRHS k
// Branch if half carry flag is set
def : InstAlias<"brhs\t$k", (BRBSsk 5, relbrtarget_7:$k)>;

// BRHC k
// Branch if half carry flag is clear
def : InstAlias<"brhc\t$k", (BRBCsk 5, relbrtarget_7:$k)>;

// BRTS k
// Branch if the T flag is set
def : InstAlias<"brts\t$k", (BRBSsk 6, relbrtarget_7:$k)>;

// BRTC k
// Branch if the T flag is clear
def : InstAlias<"brtc\t$k", (BRBCsk 6, relbrtarget_7:$k)>;

// BRVS k
// Branch if the overflow flag is set
def : InstAlias<"brvs\t$k", (BRBSsk 3, relbrtarget_7:$k)>;

// BRVC k
// Branch if the overflow flag is clear
def : InstAlias<"brvc\t$k", (BRBCsk 3, relbrtarget_7:$k)>;

// BRIE k
// Branch if the global interrupt flag is enabled
def : InstAlias<"brie\t$k", (BRBSsk 7, relbrtarget_7:$k)>;

// BRID k
// Branch if the global interrupt flag is disabled
def : InstAlias<"brid\t$k", (BRBCsk 7, relbrtarget_7:$k)>;

//===----------------------------------------------------------------------===//
// PC-relative conditional branches
//===----------------------------------------------------------------------===//
// Based on status register. We cannot simplify these into instruction aliases
// because we also need to be able to specify a pattern to match for ISel.
let isBranch = 1,
isTerminator = 1,
Uses = [SREG] in
{
  def BREQk : FBRsk<0,
                    0b001,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "breq\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_EQ)]>;

  def BRNEk : FBRsk<1,
                    0b001,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "brne\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_NE)]>;


  def BRSHk : FBRsk<1,
                    0b000,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "brsh\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_SH)]>;

  def BRLOk : FBRsk<0,
                    0b000,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "brlo\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_LO)]>;

  def BRMIk : FBRsk<0,
                    0b010,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "brmi\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_MI)]>;

  def BRPLk : FBRsk<1,
                    0b010,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "brpl\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_PL)]>;

  def BRGEk : FBRsk<1,
                    0b100,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "brge\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_GE)]>;

  def BRLTk : FBRsk<0,
                    0b100,
                    (outs),
                    (ins relbrtarget_7:$target),
                    "brlt\t$target",
                    [(AVRbrcond bb:$target, AVR_COND_LT)]>;
}

//===----------------------------------------------------------------------===//
// Data transfer instructions
//===----------------------------------------------------------------------===//
// 8 and 16-bit register move instructions.
let hasSideEffects = 0 in
{
  def MOVRdRr : FRdRr<0b0010,
                      0b11,
                      (outs GPR8:$rd),
                      (ins GPR8:$rr),
                      "mov\t$rd, $rr",
                      []>;

  def MOVWRdRr : FMOVWRdRr<(outs DREGS:$dst),
                           (ins DREGS:$src),
                           "movw\t$dst, $src",
                           []>,
                 Requires<[HasMOVW]>;
}

// Load immediate values into registers.
let isReMaterializable = 1 in
{
  def LDIRdK : FRdK<0b1110,
                    (outs LD8:$rd),
                    (ins imm_ldi8:$k),
                    "ldi\t$rd, $k",
                    [(set i8:$rd, imm:$k)]>;

  // LDIW Rd+1:Rd, K+1:K
  //
  // Expands to:
  // ldi Rd,   K
  // ldi Rd+1, K+1
  def LDIWRdK : Pseudo<(outs DLDREGS:$dst),
                       (ins i16imm:$src),
                       "ldiw\t$dst, $src",
                       [(set i16:$dst, imm:$src)]>;
}

// Load from data space into register.
let canFoldAsLoad = 1,
isReMaterializable = 1 in
{
  def LDSRdK : F32DM<0b0,
                     (outs GPR8:$rd),
                     (ins imm16:$k),
                     "lds\t$rd, $k",
                     [(set i8:$rd, (load imm:$k))]>,
               Requires<[HasSRAM]>;

  // LDSW Rd+1:Rd, K+1:K
  //
  // Expands to:
  // lds Rd,  (K+1:K)
  // lds Rd+1 (K+1:K) + 1
  def LDSWRdK : Pseudo<(outs DREGS:$dst),
                       (ins i16imm:$src),
                       "ldsw\t$dst, $src",
                       [(set i16:$dst, (load imm:$src))]>,
                Requires<[HasSRAM]>;
}

// Indirect loads.
let canFoldAsLoad = 1,
isReMaterializable = 1 in
{
  def LDRdPtr : FSTLD<0,
                      0b00,
                      (outs GPR8:$reg),
                      (ins LDSTPtrReg:$ptrreg),
                      "ld\t$reg, $ptrreg",
                      [(set GPR8:$reg, (load i16:$ptrreg))]>,
                Requires<[HasSRAM]>;

  // LDW Rd+1:Rd, P
  //
  // Expands to:
  // ld  Rd,   P
  // ldd Rd+1, P+1
  let Constraints = "@earlyclobber $reg" in
  def LDWRdPtr : Pseudo<(outs DREGS:$reg),
                        (ins PTRDISPREGS:$ptrreg),
                        "ldw\t$reg, $ptrreg",
                        [(set i16:$reg, (load i16:$ptrreg))]>,
                 Requires<[HasSRAM]>;
}

// Indirect loads (with postincrement or predecrement).
let mayLoad = 1,
hasSideEffects = 0,
Constraints = "$ptrreg = $base_wb,@earlyclobber $reg" in
{
  def LDRdPtrPi : FSTLD<0,
                        0b01,
                        (outs GPR8:$reg, PTRREGS:$base_wb),
                        (ins LDSTPtrReg:$ptrreg),
                        "ld\t$reg, $ptrreg+",
                        []>,
                  Requires<[HasSRAM]>;

  // LDW Rd+1:Rd, P+
  // Expands to:
  // ld Rd,   P+
  // ld Rd+1, P+
  def LDWRdPtrPi : Pseudo<(outs DREGS:$reg, PTRREGS:$base_wb),
                          (ins PTRREGS:$ptrreg),
                          "ldw\t$reg, $ptrreg+",
                          []>,
                   Requires<[HasSRAM]>;

  def LDRdPtrPd : FSTLD<0,
                        0b10,
                        (outs GPR8:$reg, PTRREGS:$base_wb),
                        (ins LDSTPtrReg:$ptrreg),
                        "ld\t$reg, -$ptrreg",
                        []>,
                  Requires<[HasSRAM]>;

  // LDW Rd+1:Rd, -P
  //
  // Expands to:
  // ld Rd+1, -P
  // ld Rd,   -P
  def LDWRdPtrPd : Pseudo<(outs DREGS:$reg, PTRREGS:$base_wb),
                          (ins PTRREGS:$ptrreg),
                          "ldw\t$reg, -$ptrreg",
                          []>,
                   Requires<[HasSRAM]>;
}

// Load indirect with displacement operations.
let canFoldAsLoad = 1,
isReMaterializable = 1 in
{
  let Constraints = "@earlyclobber $reg" in
  def LDDRdPtrQ : FSTDLDD<0,
                          (outs GPR8:$reg),
                          (ins memri:$memri),
                          "ldd\t$reg, $memri",
                          [(set i8:$reg, (load addr:$memri))]>,
                  Requires<[HasSRAM]>;

  // LDDW Rd+1:Rd, P+q
  //
  // Expands to:
  // ldd Rd,   P+q
  // ldd Rd+1, P+q+1
  let Constraints = "@earlyclobber $dst" in
  def LDDWRdPtrQ : Pseudo<(outs DREGS_WITHOUT_YZ_WORKAROUND:$dst),
                          (ins memri:$memri),
                          "lddw\t$dst, $memri",
                          [(set i16:$dst, (load addr:$memri))]>,
                   Requires<[HasSRAM]>;

  // An identical pseudo instruction to LDDWRdPtrQ, expect restricted to the Y
  // register and without the @earlyclobber flag.
  //
  // Used to work around a bug caused by the register allocator not
  // being able to handle the expansion of a COPY into an machine instruction
  // that has an earlyclobber flag. This is because the register allocator will
  // try expand a copy from a register slot into an earlyclobber instruction.
  // Instructions that are earlyclobber need to be in a dedicated earlyclobber slot.
  //
  // This pseudo instruction can be used pre-AVR pseudo expansion in order to
  // get a frame index load without directly using earlyclobber instructions.
  //
  // The pseudo expansion pass trivially expands this into LDDWRdPtrQ.
  //
  // This instruction may be removed once PR13375 is fixed.
  let mayLoad = 1,
  hasSideEffects = 0 in
  def LDDWRdYQ : Pseudo<(outs DREGS:$dst),
                        (ins memri:$memri),
                        "lddw\t$dst, $memri",
                        []>,
                 Requires<[HasSRAM]>;
}

class AtomicLoad<PatFrag Op, RegisterClass DRC,
                 RegisterClass PTRRC> :
  Pseudo<(outs DRC:$rd), (ins PTRRC:$rr), "atomic_op",
         [(set DRC:$rd, (Op i16:$rr))]>;

class AtomicStore<PatFrag Op, RegisterClass DRC,
                  RegisterClass PTRRC> :
  Pseudo<(outs), (ins PTRRC:$rd, DRC:$rr), "atomic_op",
         [(Op i16:$rd, DRC:$rr)]>;

class AtomicLoadOp<PatFrag Op, RegisterClass DRC,
                   RegisterClass PTRRC> :
  Pseudo<(outs DRC:$rd), (ins PTRRC:$rr, DRC:$operand),
         "atomic_op",
         [(set DRC:$rd, (Op i16:$rr, DRC:$operand))]>;

// FIXME: I think 16-bit atomic binary ops need to mark
// r0 as clobbered.

// Atomic instructions
// ===================
//
// These are all expanded by AVRExpandPseudoInsts
//
// 8-bit operations can use any pointer register because
// they are expanded directly into an LD/ST instruction.
//
// 16-bit operations use 16-bit load/store postincrement instructions,
// which require PTRDISPREGS.

def AtomicLoad8   : AtomicLoad<atomic_load_8, GPR8, PTRREGS>;
def AtomicLoad16  : AtomicLoad<atomic_load_16, DREGS, PTRDISPREGS>;

def AtomicStore8  : AtomicStore<atomic_store_8, GPR8, PTRREGS>;
def AtomicStore16 : AtomicStore<atomic_store_16, DREGS, PTRDISPREGS>;

class AtomicLoadOp8<PatFrag Op> : AtomicLoadOp<Op, GPR8, PTRREGS>;
class AtomicLoadOp16<PatFrag Op> : AtomicLoadOp<Op, DREGS, PTRDISPREGS>;

def AtomicLoadAdd8  : AtomicLoadOp8<atomic_load_add_8>;
def AtomicLoadAdd16 : AtomicLoadOp16<atomic_load_add_16>;
def AtomicLoadSub8  : AtomicLoadOp8<atomic_load_sub_8>;
def AtomicLoadSub16 : AtomicLoadOp16<atomic_load_sub_16>;
def AtomicLoadAnd8  : AtomicLoadOp8<atomic_load_and_8>;
def AtomicLoadAnd16 : AtomicLoadOp16<atomic_load_and_16>;
def AtomicLoadOr8   : AtomicLoadOp8<atomic_load_or_8>;
def AtomicLoadOr16  : AtomicLoadOp16<atomic_load_or_16>;
def AtomicLoadXor8  : AtomicLoadOp8<atomic_load_xor_8>;
def AtomicLoadXor16 : AtomicLoadOp16<atomic_load_xor_16>;
def AtomicFence     : Pseudo<(outs), (ins), "atomic_fence",
                             [(atomic_fence timm, timm)]>;

// Indirect store from register to data space.
def STSKRr : F32DM<0b1,
                   (outs),
                   (ins imm16:$k, GPR8:$rd),
                   "sts\t$k, $rd",
                   [(store i8:$rd, imm:$k)]>,
             Requires<[HasSRAM]>;

// STSW K+1:K, Rr+1:Rr
//
// Expands to:
// sts Rr+1, (K+1:K) + 1
// sts Rr,   (K+1:K)
def STSWKRr : Pseudo<(outs),
                     (ins i16imm:$dst, DREGS:$src),
                     "stsw\t$dst, $src",
                     [(store i16:$src, imm:$dst)]>,
              Requires<[HasSRAM]>;

// Indirect stores.
// ST P, Rr
// Stores the value of Rr into the location addressed by pointer P.
def STPtrRr : FSTLD<1,
                    0b00,
                    (outs),
                    (ins LDSTPtrReg:$ptrreg, GPR8:$reg),
                    "st\t$ptrreg, $reg",
                    [(store GPR8:$reg, i16:$ptrreg)]>,
              Requires<[HasSRAM]>;

// STW P, Rr+1:Rr
// Stores the value of Rr into the location addressed by pointer P.
//
// Expands to:
// st P, Rr
// std P+1, Rr+1
def STWPtrRr : Pseudo<(outs),
                      (ins PTRDISPREGS:$ptrreg, DREGS:$reg),
                      "stw\t$ptrreg, $reg",
                      [(store i16:$reg, i16:$ptrreg)]>,
               Requires<[HasSRAM]>;

// Indirect stores (with postincrement or predecrement).
let Constraints = "$ptrreg = $base_wb,@earlyclobber $base_wb" in
{

  // ST P+, Rr
  // Stores the value of Rr into the location addressed by pointer P.
  // Post increments P.
  def STPtrPiRr : FSTLD<1,
                        0b01,
                        (outs LDSTPtrReg:$base_wb),
                        (ins LDSTPtrReg:$ptrreg, GPR8:$reg, i8imm:$offs),
                        "st\t$ptrreg+, $reg",
                        [(set i16:$base_wb,
                         (post_store GPR8:$reg, i16:$ptrreg, imm:$offs))]>,
                  Requires<[HasSRAM]>;

  // STW P+, Rr+1:Rr
  // Stores the value of Rr into the location addressed by pointer P.
  // Post increments P.
  //
  // Expands to:
  // st P+, Rr
  // st P+, Rr+1
  def STWPtrPiRr : Pseudo<(outs PTRREGS:$base_wb),
                          (ins PTRREGS:$ptrreg, DREGS:$trh, i8imm:$offs),
                          "stw\t$ptrreg+, $trh",
                          [(set PTRREGS:$base_wb,
                           (post_store DREGS:$trh, PTRREGS:$ptrreg, imm:$offs))]>,
                   Requires<[HasSRAM]>;

  // ST -P, Rr
  // Stores the value of Rr into the location addressed by pointer P.
  // Pre decrements P.
  def STPtrPdRr : FSTLD<1,
                        0b10,
                        (outs LDSTPtrReg:$base_wb),
                        (ins LDSTPtrReg:$ptrreg, GPR8:$reg, i8imm:$offs),
                        "st\t-$ptrreg, $reg",
                        [(set i16:$base_wb,
                         (pre_store GPR8:$reg, i16:$ptrreg, imm:$offs))]>,
                  Requires<[HasSRAM]>;

  // STW -P, Rr+1:Rr
  // Stores the value of Rr into the location addressed by pointer P.
  // Pre decrements P.
  //
  // Expands to:
  // st -P, Rr+1
  // st -P, Rr
  def STWPtrPdRr : Pseudo<(outs PTRREGS:$base_wb),
                          (ins PTRREGS:$ptrreg, DREGS:$reg, i8imm:$offs),
                          "stw\t-$ptrreg, $reg",
                          [(set PTRREGS:$base_wb,
                           (pre_store i16:$reg, i16:$ptrreg, imm:$offs))]>,
                   Requires<[HasSRAM]>;
}

// Store indirect with displacement operations.
// STD P+q, Rr
// Stores the value of Rr into the location addressed by pointer P with a
// displacement of q. Does not modify P.
def STDPtrQRr : FSTDLDD<1,
                        (outs),
                        (ins memri:$memri, GPR8:$reg),
                        "std\t$memri, $reg",
                        [(store i8:$reg, addr:$memri)]>,
                Requires<[HasSRAM]>;

// STDW P+q, Rr+1:Rr
// Stores the value of Rr into the location addressed by pointer P with a
// displacement of q. Does not modify P.
//
// Expands to:
// std P+q,   Rr
// std P+q+1, Rr+1
def STDWPtrQRr : Pseudo<(outs),
                        (ins memri:$memri, DREGS:$src),
                        "stdw\t$memri, $src",
                        [(store i16:$src, addr:$memri)]>,
                 Requires<[HasSRAM]>;


// Load program memory operations.
let canFoldAsLoad = 1,
isReMaterializable = 1,
mayLoad = 1,
hasSideEffects = 0 in
{
  let Defs = [R0],
      Uses = [R31R30] in
  def LPM : F16<0b1001010111001000,
                (outs),
                (ins),
                "lpm",
                []>,
            Requires<[HasLPM]>;

  def LPMRdZ : FLPMX<0,
                     0,
                     (outs GPR8:$dst),
                     (ins ZREG:$z),
                     "lpm\t$dst, $z",
                     []>,
               Requires<[HasLPMX]>;

  // Load program memory, while postincrementing the Z register.
  let Defs = [R31R30] in
  {
    def LPMRdZPi : FLPMX<0,
                         1,
                         (outs GPR8:$dst),
                         (ins ZREG:$z),
                         "lpm\t$dst, $z+",
                         []>,
                   Requires<[HasLPMX]>;

    def LPMWRdZ : Pseudo<(outs DREGS:$dst),
                         (ins ZREG:$z),
                         "lpmw\t$dst, $z",
                         []>,
                  Requires<[HasLPMX]>;

    def LPMWRdZPi : Pseudo<(outs DREGS:$dst),
                           (ins ZREG:$z),
                           "lpmw\t$dst, $z+",
                           []>,
                    Requires<[HasLPMX]>;
  }
}

// Extended load program memory operations.
let mayLoad = 1,
hasSideEffects = 0 in
{
  let Defs = [R0],
      Uses = [R31R30] in
  def ELPM : F16<0b1001010111011000,
                 (outs),
                 (ins),
                 "elpm",
                 []>,
             Requires<[HasELPM]>;

  def ELPMRdZ : FLPMX<1,
                      0,
                      (outs GPR8:$dst),
                      (ins ZREG:$z),
                      "elpm\t$dst, $z",
                      []>,
                Requires<[HasELPMX]>;

  let Defs = [R31R30] in
  def ELPMRdZPi : FLPMX<1,
                        1,
                        (outs GPR8:$dst),
                        (ins ZREG: $z),
                        "elpm\t$dst, $z+",
                        []>,
                  Requires<[HasELPMX]>;
}

// Store program memory operations.
let Uses = [R1, R0] in
{
  let Uses = [R31R30, R1, R0] in
  def SPM : F16<0b1001010111101000,
                (outs),
                (ins),
                "spm",
                []>,
            Requires<[HasSPM]>;

  let Defs = [R31R30] in
  def SPMZPi : F16<0b1001010111111000,
                   (outs),
                   (ins ZREG:$z),
                   "spm $z+",
                   []>,
               Requires<[HasSPMX]>;
}

// Read data from IO location operations.
let canFoldAsLoad = 1,
isReMaterializable = 1 in
{
  def INRdA : FIORdA<(outs GPR8:$dst),
                     (ins imm_port6:$src),
                     "in\t$dst, $src",
                     [(set i8:$dst, (load ioaddr8:$src))]>;

  def INWRdA : Pseudo<(outs DREGS:$dst),
                      (ins imm_port6:$src),
                      "inw\t$dst, $src",
                      [(set i16:$dst, (load ioaddr16:$src))]>;
}

// Write data to IO location operations.
def OUTARr : FIOARr<(outs),
                    (ins imm_port6:$dst, GPR8:$src),
                    "out\t$dst, $src",
                    [(store i8:$src, ioaddr8:$dst)]>;

def OUTWARr : Pseudo<(outs),
                     (ins imm_port6:$dst, DREGS:$src),
                     "outw\t$dst, $src",
                     [(store i16:$src, ioaddr16:$dst)]>;

// Stack push/pop operations.
let Defs = [SP],
Uses = [SP],
hasSideEffects = 0 in
{
  // Stack push operations.
  let mayStore = 1 in
  {
    def PUSHRr : FRd<0b1001,
                     0b0011111,
                     (outs),
                     (ins GPR8:$reg),
                     "push\t$reg",
                     []>,
                 Requires<[HasSRAM]>;

    def PUSHWRr : Pseudo<(outs),
                         (ins DREGS:$reg),
                         "pushw\t$reg",
                         []>,
                  Requires<[HasSRAM]>;
  }

  // Stack pop operations.
  let mayLoad = 1 in
  {
    def POPRd : FRd<0b1001,
                    0b0001111,
                    (outs GPR8:$reg),
                    (ins),
                    "pop\t$reg",
                    []>,
                Requires<[HasSRAM]>;

    def POPWRd : Pseudo<(outs DREGS:$reg),
                        (ins),
                        "popw\t$reg",
                        []>,
                 Requires<[HasSRAM]>;
  }
}

// Read-Write-Modify (RMW) instructions.
def XCHZRd : FZRd<0b100,
                  (outs GPR8:$rd),
                  (ins ZREG:$z),
                  "xch\t$z, $rd",
                  []>,
             Requires<[SupportsRMW]>;

def LASZRd : FZRd<0b101,
                  (outs GPR8:$rd),
                  (ins ZREG:$z),
                  "las\t$z, $rd",
                  []>,
             Requires<[SupportsRMW]>;

def LACZRd : FZRd<0b110,
                  (outs GPR8:$rd),
                  (ins ZREG:$z),
                  "lac\t$z, $rd",
                  []>,
             Requires<[SupportsRMW]>;

def LATZRd : FZRd<0b111,
                  (outs GPR8:$rd),
                  (ins ZREG:$z),
                  "lat\t$z, $rd",
                  []>,
             Requires<[SupportsRMW]>;

//===----------------------------------------------------------------------===//
// Bit and bit-test instructions
//===----------------------------------------------------------------------===//

// Bit shift/rotate operations.
let Constraints = "$src = $rd",
Defs = [SREG] in
{
  // 8-bit LSL is an alias of ADD Rd, Rd

  def LSLWRd : Pseudo<(outs DREGS:$rd),
                      (ins DREGS:$src),
                      "lslw\t$rd",
                      [(set i16:$rd, (AVRlsl i16:$src)), (implicit SREG)]>;

  def LSRRd : FRd<0b1001,
                  0b0100110,
                  (outs GPR8:$rd),
                  (ins GPR8:$src),
                  "lsr\t$rd",
                  [(set i8:$rd, (AVRlsr i8:$src)), (implicit SREG)]>;

  def LSRWRd : Pseudo<(outs DREGS:$rd),
                      (ins DREGS:$src),
                      "lsrw\t$rd",
                      [(set i16:$rd, (AVRlsr i16:$src)), (implicit SREG)]>;

  def ASRRd : FRd<0b1001,
                  0b0100101,
                  (outs GPR8:$rd),
                  (ins GPR8:$src),
                  "asr\t$rd",
                  [(set i8:$rd, (AVRasr i8:$src)), (implicit SREG)]>;

  def ASRWRd : Pseudo<(outs DREGS:$rd),
                      (ins DREGS:$src),
                      "asrw\t$rd",
                      [(set i16:$rd, (AVRasr i16:$src)), (implicit SREG)]>;

  // Bit rotate operations.
  let Uses = [SREG] in
  {
    // 8-bit ROL is an alias of ADC Rd, Rd

    def ROLBRd : Pseudo<(outs GPR8:$rd),
                        (ins GPR8:$src),
                        "rolb\t$rd",
                        [(set i8:$rd, (AVRrol i8:$src)), (implicit SREG)]>;

    def RORBRd : Pseudo<(outs GPR8:$rd),
                        (ins GPR8:$src),
                        "rorb\t$rd",
                        [(set i8:$rd, (AVRror i8:$src)), (implicit SREG)]>;

    def ROLWRd : Pseudo<(outs DREGS:$rd),
                        (ins DREGS:$src),
                        "rolw\t$rd",
                        [(set i16:$rd, (AVRrol i16:$src)), (implicit SREG)]>;

    def RORRd : FRd<0b1001,
                    0b0100111,
                    (outs GPR8:$rd),
                    (ins GPR8:$src),
                    "ror\t$rd",
                    []>;

    def RORWRd : Pseudo<(outs DREGS:$rd),
                        (ins DREGS:$src),
                        "rorw\t$rd",
                        [(set i16:$rd, (AVRror i16:$src)), (implicit SREG)]>;
  }
}

// SWAP Rd
// Swaps the high and low nibbles in a register.
let Constraints = "$src = $rd" in
def SWAPRd : FRd<0b1001,
                 0b0100010,
                 (outs GPR8:$rd),
                 (ins GPR8:$src),
                 "swap\t$rd",
                 [(set i8:$rd, (bswap i8:$src))]>;

// IO register bit set/clear operations.
//:TODO: add patterns when popcount(imm)==2 to be expanded with 2 sbi/cbi
// instead of in+ori+out which requires one more instr.
def SBIAb : FIOBIT<0b10,
                   (outs),
                   (ins imm_port5:$addr, i8imm:$bit),
                   "sbi\t$addr, $bit",
                   [(store (or (i8 (load lowioaddr8:$addr)), iobitpos8:$bit),
                     lowioaddr8:$addr)]>;

def CBIAb : FIOBIT<0b00,
                   (outs),
                   (ins imm_port5:$addr, i8imm:$bit),
                   "cbi\t$addr, $bit",
                   [(store (and (i8 (load lowioaddr8:$addr)), iobitposn8:$bit),
                     lowioaddr8:$addr)]>;

// Status register bit load/store operations.
let Defs = [SREG] in
def BST : FRdB<0b01,
               (outs),
               (ins GPR8:$rd, i8imm:$b),
               "bst\t$rd, $b",
               []>;

let Uses = [SREG] in
def BLD : FRdB<0b00,
               (outs),
               (ins GPR8:$rd, i8imm:$b),
               "bld\t$rd, $b",
               []>;

def CBR : InstAlias<"cbr\t$rd, $k", (ANDIRdK LD8:$rd, imm_com8:$k), 0>;

// CLR Rd
// Alias for EOR Rd, Rd
// -------------
// Clears all bits in a register.
def CLR : InstAlias<"clr\t$rd", (EORRdRr GPR8:$rd, GPR8:$rd)>;

// LSL Rd
// Alias for ADD Rd, Rd
// --------------
// Logical shift left one bit.
def LSL : InstAlias<"lsl\t$rd", (ADDRdRr GPR8:$rd, GPR8:$rd)>;

def ROL : InstAlias<"rol\t$rd", (ADCRdRr GPR8:$rd, GPR8:$rd)>;

// SER Rd
// Alias for LDI Rd, 0xff
// ---------
// Sets all bits in a register.
def : InstAlias<"ser\t$rd", (LDIRdK LD8:$rd, 0xff), 0>;

let Defs = [SREG] in
def BSETs : FS<0,
               (outs),
               (ins i8imm:$s),
               "bset\t$s",
               []>;

let Defs = [SREG] in
def BCLRs : FS<1,
               (outs),
               (ins i8imm:$s),
               "bclr\t$s",
               []>;

// Set/clear aliases for the carry (C) status flag (bit 0).
def : InstAlias<"sec", (BSETs 0)>;
def : InstAlias<"clc", (BCLRs 0)>;

// Set/clear aliases for the zero (Z) status flag (bit 1).
def : InstAlias<"sez", (BSETs 1)>;
def : InstAlias<"clz", (BCLRs 1)>;

// Set/clear aliases for the negative (N) status flag (bit 2).
def : InstAlias<"sen", (BSETs 2)>;
def : InstAlias<"cln", (BCLRs 2)>;

// Set/clear aliases for the overflow (V) status flag (bit 3).
def : InstAlias<"sev", (BSETs 3)>;
def : InstAlias<"clv", (BCLRs 3)>;

// Set/clear aliases for the signed (S) status flag (bit 4).
def : InstAlias<"ses", (BSETs 4)>;
def : InstAlias<"cls", (BCLRs 4)>;

// Set/clear aliases for the half-carry (H) status flag (bit 5).
def : InstAlias<"seh", (BSETs 5)>;
def : InstAlias<"clh", (BCLRs 5)>;

// Set/clear aliases for the T status flag (bit 6).
def : InstAlias<"set", (BSETs 6)>;
def : InstAlias<"clt", (BCLRs 6)>;

// Set/clear aliases for the interrupt (I) status flag (bit 7).
def : InstAlias<"sei", (BSETs 7)>;
def : InstAlias<"cli", (BCLRs 7)>;

//===----------------------------------------------------------------------===//
// Special/Control instructions
//===----------------------------------------------------------------------===//

// BREAK
// Breakpoint instruction
// ---------
// <|1001|0101|1001|1000>
def BREAK : F16<0b1001010110011000,
                (outs),
                (ins),
                "break",
                []>,
            Requires<[HasBREAK]>;

// NOP
// No-operation instruction
// ---------
// <|0000|0000|0000|0000>
def NOP : F16<0b0000000000000000,
              (outs),
              (ins),
              "nop",
              []>;

// SLEEP
// Sleep instruction
// ---------
// <|1001|0101|1000|1000>
def SLEEP : F16<0b1001010110001000,
                (outs),
                (ins),
                "sleep",
                []>;

// WDR
// Watchdog reset
// ---------
// <|1001|0101|1010|1000>
def WDR : F16<0b1001010110101000,
              (outs),
              (ins),
              "wdr",
              []>;

//===----------------------------------------------------------------------===//
// Pseudo instructions for later expansion
//===----------------------------------------------------------------------===//

//:TODO: Optimize this for wider types AND optimize the following code
//       compile int foo(char a, char b, char c, char d) {return d+b;}
//       looks like a missed sext_inreg opportunity.
def SEXT : ExtensionPseudo<
  (outs DREGS:$dst),
  (ins GPR8:$src),
  "sext\t$dst, $src",
  [(set i16:$dst, (sext i8:$src)), (implicit SREG)]
>;

def ZEXT : ExtensionPseudo<
  (outs DREGS:$dst),
  (ins GPR8:$src),
  "zext\t$dst, $src",
  [(set i16:$dst, (zext i8:$src)), (implicit SREG)]
>;

// This pseudo gets expanded into a movw+adiw thus it clobbers SREG.
let Defs = [SREG],
    hasSideEffects = 0 in
def FRMIDX : Pseudo<(outs DLDREGS:$dst),
                    (ins DLDREGS:$src, i16imm:$src2),
                    "frmidx\t$dst, $src, $src2",
                    []>;

// This pseudo is either converted to a regular store or a push which clobbers
// SP.
def STDSPQRr : StorePseudo<
  (outs),
  (ins memspi:$dst, GPR8:$src),
  "stdstk\t$dst, $src",
  [(store i8:$src, addr:$dst)]
>;

// This pseudo is either converted to a regular store or a push which clobbers
// SP.
def STDWSPQRr : StorePseudo<
  (outs),
  (ins memspi:$dst, DREGS:$src),
  "stdwstk\t$dst, $src",
  [(store i16:$src, addr:$dst)]
>;

// SP read/write pseudos.
let hasSideEffects = 0 in
{
  let Uses = [SP] in
  def SPREAD : Pseudo<
    (outs DREGS:$dst),
    (ins GPRSP:$src),
    "spread\t$dst, $src",
    []
  >;

  let Defs = [SP] in
  def SPWRITE : Pseudo<
    (outs GPRSP:$dst),
    (ins DREGS:$src),
    "spwrite\t$dst, $src",
    []>;
}

def Select8 : SelectPseudo<
  (outs GPR8:$dst),
  (ins GPR8:$src, GPR8:$src2, i8imm:$cc),
  "# Select8 PSEUDO",
  [(set i8:$dst, (AVRselectcc i8:$src, i8:$src2, imm:$cc))]
>;

def Select16 : SelectPseudo<
  (outs DREGS:$dst),
  (ins DREGS:$src, DREGS:$src2, i8imm:$cc),
  "# Select16 PSEUDO",
  [(set i16:$dst, (AVRselectcc i16:$src, i16:$src2, imm:$cc))]
>;

def Lsl8 : ShiftPseudo<
  (outs GPR8:$dst),
  (ins GPR8:$src, GPR8:$cnt),
  "# Lsl8 PSEUDO",
  [(set i8:$dst, (AVRlslLoop i8:$src, i8:$cnt))]
>;

def Lsl16 : ShiftPseudo<
  (outs DREGS:$dst),
  (ins DREGS:$src, GPR8:$cnt),
  "# Lsl16 PSEUDO",
  [(set i16:$dst, (AVRlslLoop i16:$src, i8:$cnt))]
>;

def Lsr8 : ShiftPseudo<
  (outs GPR8:$dst),
  (ins GPR8:$src, GPR8:$cnt),
  "# Lsr8 PSEUDO",
  [(set i8:$dst, (AVRlsrLoop i8:$src, i8:$cnt))]
>;

def Lsr16 : ShiftPseudo<
  (outs DREGS:$dst),
   (ins DREGS:$src, GPR8:$cnt),
   "# Lsr16 PSEUDO",
   [(set i16:$dst, (AVRlsrLoop i16:$src, i8:$cnt))]
>;

def Rol8 : ShiftPseudo<
  (outs GPR8:$dst),
  (ins GPR8:$src, GPR8:$cnt),
  "# Rol8 PSEUDO",
  [(set i8:$dst, (AVRrolLoop i8:$src, i8:$cnt))]
>;

def Rol16 : ShiftPseudo<
  (outs DREGS:$dst),
  (ins DREGS:$src, GPR8:$cnt),
  "# Rol16 PSEUDO",
  [(set i16:$dst, (AVRrolLoop i16:$src, i8:$cnt))]
>;

def Ror8 : ShiftPseudo<
  (outs GPR8:$dst),
  (ins GPR8:$src, GPR8:$cnt),
  "# Ror8 PSEUDO",
  [(set i8:$dst, (AVRrorLoop i8:$src, i8:$cnt))]
>;

def Ror16 : ShiftPseudo<
  (outs DREGS:$dst),
  (ins DREGS:$src, GPR8:$cnt),
  "# Ror16 PSEUDO",
  [(set i16:$dst, (AVRrorLoop i16:$src, i8:$cnt))]
>;

def Asr8 : ShiftPseudo<
  (outs GPR8:$dst),
  (ins GPR8:$src, GPR8:$cnt),
  "# Asr8 PSEUDO",
  [(set i8:$dst, (AVRasrLoop i8:$src, i8:$cnt))]
>;

def Asr16 : ShiftPseudo<
  (outs DREGS:$dst),
   (ins DREGS:$src, GPR8:$cnt),
   "# Asr16 PSEUDO",
   [(set i16:$dst, (AVRasrLoop i16:$src, i8:$cnt))]
>;


//===----------------------------------------------------------------------===//
// Non-Instruction Patterns
//===----------------------------------------------------------------------===//

//:TODO: look in x86InstrCompiler.td for odd encoding trick related to
// add x, 128 -> sub x, -128. Clang is emitting an eor for this (ldi+eor)

// the add instruction always writes the carry flag
def : Pat<(addc i8:$src, i8:$src2),
          (ADDRdRr i8:$src, i8:$src2)>;
def : Pat<(addc DREGS:$src, DREGS:$src2),
          (ADDWRdRr DREGS:$src, DREGS:$src2)>;

// all sub instruction variants always writes the carry flag
def : Pat<(subc i8:$src, i8:$src2),
          (SUBRdRr i8:$src, i8:$src2)>;
def : Pat<(subc i16:$src, i16:$src2),
          (SUBWRdRr i16:$src, i16:$src2)>;
def : Pat<(subc i8:$src, imm:$src2),
          (SUBIRdK i8:$src, imm:$src2)>;
def : Pat<(subc i16:$src, imm:$src2),
          (SUBIWRdK i16:$src, imm:$src2)>;

// These patterns convert add (x, -imm) to sub (x, imm) since we dont have
// any add with imm instructions. Also take care of the adiw/sbiw instructions.
def : Pat<(add i16:$src1, imm0_63_neg:$src2),
          (SBIWRdK i16:$src1, (imm0_63_neg:$src2))>;
def : Pat<(add i16:$src1, imm:$src2),
          (SUBIWRdK i16:$src1, (imm16_neg_XFORM imm:$src2))>;
def : Pat<(addc i16:$src1, imm:$src2),
          (SUBIWRdK i16:$src1, (imm16_neg_XFORM imm:$src2))>;

def : Pat<(add i8:$src1, imm:$src2),
          (SUBIRdK i8:$src1, (imm8_neg_XFORM imm:$src2))>;
def : Pat<(addc i8:$src1, imm:$src2),
          (SUBIRdK i8:$src1, (imm8_neg_XFORM imm:$src2))>;
def : Pat<(adde i8:$src1, imm:$src2),
          (SBCIRdK i8:$src1, (imm8_neg_XFORM imm:$src2))>;

// Calls.
def : Pat<(AVRcall (i16 tglobaladdr:$dst)),
          (CALLk tglobaladdr:$dst)>;
def : Pat<(AVRcall (i16 texternalsym:$dst)),
          (CALLk texternalsym:$dst)>;

// `anyext`
def : Pat<(i16 (anyext i8:$src)),
          (INSERT_SUBREG (i16 (IMPLICIT_DEF)), i8:$src, sub_lo)>;

// `trunc`
def : Pat<(i8 (trunc i16:$src)),
          (EXTRACT_SUBREG i16:$src, sub_lo)>;

// sext_inreg
def : Pat<(sext_inreg i16:$src, i8),
          (SEXT (i8 (EXTRACT_SUBREG i16:$src, sub_lo)))>;

// GlobalAddress
def : Pat<(i16 (AVRWrapper tglobaladdr:$dst)),
          (LDIWRdK tglobaladdr:$dst)>;
def : Pat<(add i16:$src, (AVRWrapper tglobaladdr:$src2)),
          (SUBIWRdK i16:$src, tglobaladdr:$src2)>;
def : Pat<(i8 (load (AVRWrapper tglobaladdr:$dst))),
          (LDSRdK tglobaladdr:$dst)>;
def : Pat<(i16 (load (AVRWrapper tglobaladdr:$dst))),
          (LDSWRdK tglobaladdr:$dst)>;
def : Pat<(store i8:$src, (i16 (AVRWrapper tglobaladdr:$dst))),
          (STSKRr tglobaladdr:$dst, i8:$src)>;
def : Pat<(store i16:$src, (i16 (AVRWrapper tglobaladdr:$dst))),
          (STSWKRr tglobaladdr:$dst, i16:$src)>;

// BlockAddress
def : Pat<(i16 (AVRWrapper tblockaddress:$dst)),
          (LDIWRdK tblockaddress:$dst)>;

// hi-reg truncation : trunc(int16 >> 8)
//:FIXME: i think it's better to emit an extract subreg node in the DAG than
// all this mess once we get optimal shift code
// lol... I think so, too. [@agnat]
def : Pat<(i8 (trunc (AVRlsr (AVRlsr (AVRlsr (AVRlsr (AVRlsr (AVRlsr (AVRlsr
                     (AVRlsr DREGS:$src)))))))))),
          (EXTRACT_SUBREG DREGS:$src, sub_hi)>;

// :FIXME: DAGCombiner produces an shl node after legalization from these seq:
// BR_JT -> (mul x, 2) -> (shl x, 1)
def : Pat<(shl i16:$src1, (i8 1)),
          (LSLWRd i16:$src1)>;

// Lowering of 'tst' node to 'TST' instruction.
// TST is an alias of AND Rd, Rd.
def : Pat<(AVRtst i8:$rd),
          (ANDRdRr GPR8:$rd, GPR8:$rd)>;

// Lowering of 'lsl' node to 'LSL' instruction.
// LSL is an alias of 'ADD Rd, Rd'
def : Pat<(AVRlsl i8:$rd),
          (ADDRdRr GPR8:$rd, GPR8:$rd)>;