xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AVR/AVRInstrInfo.td (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1//===-- AVRInstrInfo.td - AVR Instruction defs -------------*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file describes the AVR instructions in TableGen format.
10//
11//===----------------------------------------------------------------------===//
12
13include "AVRInstrFormats.td"
14
15//===----------------------------------------------------------------------===//
16// AVR Type Profiles
17//===----------------------------------------------------------------------===//
18
19def SDT_AVRCallSeqStart : SDCallSeqStart<[SDTCisVT<0, i16>, SDTCisVT<1, i16>]>;
20def SDT_AVRCallSeqEnd : SDCallSeqEnd<[SDTCisVT<0, i16>, SDTCisVT<1, i16>]>;
21def SDT_AVRCall : SDTypeProfile<0, -1, [SDTCisVT<0, iPTR>]>;
22def SDT_AVRWrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>, SDTCisPtrTy<0>]>;
23def SDT_AVRBrcond
24    : SDTypeProfile<0, 2, [SDTCisVT<0, OtherVT>, SDTCisVT<1, i8>]>;
25def SDT_AVRCmp : SDTypeProfile<0, 2, [SDTCisSameAs<0, 1>]>;
26def SDT_AVRTst : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
27def SDT_AVRSelectCC
28    : SDTypeProfile<1, 3,
29                    [SDTCisSameAs<0, 1>, SDTCisSameAs<1, 2>, SDTCisVT<3, i8>]>;
30
31//===----------------------------------------------------------------------===//
32// AVR Specific Node Definitions
33//===----------------------------------------------------------------------===//
34
35def AVRretflag : SDNode<"AVRISD::RET_FLAG", SDTNone,
36                        [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
37def AVRretiflag : SDNode<"AVRISD::RETI_FLAG", SDTNone,
38                         [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
39
40def AVRcallseq_start : SDNode<"ISD::CALLSEQ_START", SDT_AVRCallSeqStart,
41                              [SDNPHasChain, SDNPOutGlue]>;
42def AVRcallseq_end : SDNode<"ISD::CALLSEQ_END", SDT_AVRCallSeqEnd,
43                            [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
44
45def AVRcall : SDNode<"AVRISD::CALL", SDT_AVRCall,
46                     [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue, SDNPVariadic]>;
47
48def AVRWrapper : SDNode<"AVRISD::WRAPPER", SDT_AVRWrapper>;
49
50def AVRbrcond
51    : SDNode<"AVRISD::BRCOND", SDT_AVRBrcond, [SDNPHasChain, SDNPInGlue]>;
52def AVRcmp : SDNode<"AVRISD::CMP", SDT_AVRCmp, [SDNPOutGlue]>;
53def AVRcmpc : SDNode<"AVRISD::CMPC", SDT_AVRCmp, [SDNPInGlue, SDNPOutGlue]>;
54def AVRtst : SDNode<"AVRISD::TST", SDT_AVRTst, [SDNPOutGlue]>;
55def AVRselectcc : SDNode<"AVRISD::SELECT_CC", SDT_AVRSelectCC, [SDNPInGlue]>;
56
57// Shift nodes.
58def AVRlsl : SDNode<"AVRISD::LSL", SDTIntUnaryOp>;
59def AVRlsr : SDNode<"AVRISD::LSR", SDTIntUnaryOp>;
60def AVRrol : SDNode<"AVRISD::ROL", SDTIntUnaryOp>;
61def AVRror : SDNode<"AVRISD::ROR", SDTIntUnaryOp>;
62def AVRasr : SDNode<"AVRISD::ASR", SDTIntUnaryOp>;
63def AVRlslhi : SDNode<"AVRISD::LSLHI", SDTIntUnaryOp>;
64def AVRlsrlo : SDNode<"AVRISD::LSRLO", SDTIntUnaryOp>;
65def AVRasrlo : SDNode<"AVRISD::ASRLO", SDTIntUnaryOp>;
66def AVRlslbn : SDNode<"AVRISD::LSLBN", SDTIntBinOp>;
67def AVRlsrbn : SDNode<"AVRISD::LSRBN", SDTIntBinOp>;
68def AVRasrbn : SDNode<"AVRISD::ASRBN", SDTIntBinOp>;
69def AVRlslwn : SDNode<"AVRISD::LSLWN", SDTIntBinOp>;
70def AVRlsrwn : SDNode<"AVRISD::LSRWN", SDTIntBinOp>;
71def AVRasrwn : SDNode<"AVRISD::ASRWN", SDTIntBinOp>;
72
73// Pseudo shift nodes for non-constant shift amounts.
74def AVRlslLoop : SDNode<"AVRISD::LSLLOOP", SDTIntShiftOp>;
75def AVRlsrLoop : SDNode<"AVRISD::LSRLOOP", SDTIntShiftOp>;
76def AVRrolLoop : SDNode<"AVRISD::ROLLOOP", SDTIntShiftOp>;
77def AVRrorLoop : SDNode<"AVRISD::RORLOOP", SDTIntShiftOp>;
78def AVRasrLoop : SDNode<"AVRISD::ASRLOOP", SDTIntShiftOp>;
79
80// SWAP node.
81def AVRSwap : SDNode<"AVRISD::SWAP", SDTIntUnaryOp>;
82
83//===----------------------------------------------------------------------===//
84// AVR Operands, Complex Patterns and Transformations Definitions.
85//===----------------------------------------------------------------------===//
86
87def imm8_neg_XFORM : SDNodeXForm<imm, [{
88                                   return CurDAG->getTargetConstant(
89                                       -N->getAPIntValue(), SDLoc(N), MVT::i8);
90                                 }]>;
91
92def imm16_neg_XFORM
93    : SDNodeXForm<imm, [{
94                    return CurDAG->getTargetConstant(-N->getAPIntValue(),
95                                                     SDLoc(N), MVT::i16);
96                  }]>;
97
98def imm0_63_neg : PatLeaf<(imm), [{
99                            int64_t val = -N->getSExtValue();
100                            return val >= 0 && val < 64;
101                          }],
102                          imm16_neg_XFORM>;
103
104def uimm6 : PatLeaf<(imm), [{ return isUInt<6>(N->getZExtValue()); }]>;
105
106// imm_com8_XFORM - Return the complement of a imm_com8 value
107def imm_com8_XFORM
108    : SDNodeXForm<imm, [{
109                    return CurDAG->getTargetConstant(
110                        ~((uint8_t) N->getZExtValue()), SDLoc(N), MVT::i8);
111                  }]>;
112
113// imm_com8 - Match an immediate that is a complement
114// of a 8-bit immediate.
115// Note: this pattern doesn't require an encoder method and such, as it's
116// only used on aliases (Pat<> and InstAlias<>). The actual encoding
117// is handled by the destination instructions, which use imm_com8.
118def imm_com8_asmoperand : AsmOperandClass { let Name = "ImmCom8"; }
119def imm_com8 : Operand<i8> { let ParserMatchClass = imm_com8_asmoperand; }
120
121def ioaddr_XFORM
122    : SDNodeXForm<imm, [{
123                    uint8_t offset = Subtarget->getIORegisterOffset();
124                    return CurDAG->getTargetConstant(
125                        uint8_t(N->getZExtValue()) - offset, SDLoc(N), MVT::i8);
126                  }]>;
127
128def iobitpos8_XFORM
129    : SDNodeXForm<imm, [{
130                    return CurDAG->getTargetConstant(
131                        Log2_32(uint8_t(N->getZExtValue())), SDLoc(N), MVT::i8);
132                  }]>;
133
134def iobitposn8_XFORM : SDNodeXForm<imm, [{
135                                     return CurDAG->getTargetConstant(
136                                         Log2_32(uint8_t(~N->getZExtValue())),
137                                         SDLoc(N), MVT::i8);
138                                   }]>;
139
140def ioaddr8 : PatLeaf<(imm), [{
141                        uint8_t offset = Subtarget->getIORegisterOffset();
142                        uint64_t val = N->getZExtValue() - offset;
143                        return val < 0x40;
144                      }],
145                      ioaddr_XFORM>;
146
147def lowioaddr8 : PatLeaf<(imm), [{
148                           uint8_t offset = Subtarget->getIORegisterOffset();
149                           uint64_t val = N->getZExtValue() - offset;
150                           return val < 0x20;
151                         }],
152                         ioaddr_XFORM>;
153
154def ioaddr16 : PatLeaf<(imm), [{
155                         uint8_t offset = Subtarget->getIORegisterOffset();
156                         uint64_t val = N->getZExtValue() - offset;
157                         return val < 0x3f;
158                       }],
159                       ioaddr_XFORM>;
160
161def iobitpos8
162    : PatLeaf<(imm), [{ return isPowerOf2_32(uint8_t(N->getZExtValue())); }],
163              iobitpos8_XFORM>;
164
165def iobitposn8
166    : PatLeaf<(imm), [{ return isPowerOf2_32(uint8_t(~N->getZExtValue())); }],
167              iobitposn8_XFORM>;
168
169def MemriAsmOperand : AsmOperandClass {
170  let Name = "Memri";
171  let ParserMethod = "parseMemriOperand";
172}
173
174/// Address operand for `reg+imm` used by STD and LDD.
175def memri : Operand<iPTR> {
176  let MIOperandInfo = (ops PTRDISPREGS, i16imm);
177
178  let PrintMethod = "printMemri";
179  let EncoderMethod = "encodeMemri";
180
181  let ParserMatchClass = MemriAsmOperand;
182}
183
184// Address operand for `SP+imm` used by STD{W}SPQRr
185def memspi : Operand<iPTR> { let MIOperandInfo = (ops GPRSP, i16imm); }
186
187def relbrtarget_7 : Operand<OtherVT> {
188  let PrintMethod = "printPCRelImm";
189  let EncoderMethod = "encodeRelCondBrTarget<AVR::fixup_7_pcrel>";
190}
191
192def brtarget_13 : Operand<OtherVT> {
193  let PrintMethod = "printPCRelImm";
194  let EncoderMethod = "encodeRelCondBrTarget<AVR::fixup_13_pcrel>";
195}
196
197def rcalltarget_13 : Operand<i16> {
198  let PrintMethod = "printPCRelImm";
199  let EncoderMethod = "encodeRelCondBrTarget<AVR::fixup_13_pcrel>";
200}
201
202// The target of a 22 or 16-bit call/jmp instruction.
203def call_target : Operand<iPTR> {
204  let EncoderMethod = "encodeCallTarget";
205  let DecoderMethod = "decodeCallTarget";
206}
207
208// A 16-bit address (which can lead to an R_AVR_16 relocation).
209def imm16 : Operand<i16> { let EncoderMethod = "encodeImm<AVR::fixup_16, 2>"; }
210
211/// A 6-bit immediate used in the ADIW/SBIW instructions.
212def imm_arith6 : Operand<i16> {
213  let EncoderMethod = "encodeImm<AVR::fixup_6_adiw, 0>";
214}
215
216/// An 8-bit immediate inside an instruction with the same format
217/// as the `LDI` instruction (the `FRdK` format).
218def imm_ldi8 : Operand<i8> {
219  let EncoderMethod = "encodeImm<AVR::fixup_ldi, 0>";
220}
221
222/// A 5-bit port number used in SBIC and friends (the `FIOBIT` format).
223def imm_port5 : Operand<i8> {
224  let EncoderMethod = "encodeImm<AVR::fixup_port5, 0>";
225}
226
227/// A 6-bit port number used in the `IN` instruction and friends (the
228/// `FIORdA` format.
229def imm_port6 : Operand<i8> {
230  let EncoderMethod = "encodeImm<AVR::fixup_port6, 0>";
231}
232
233// Addressing mode pattern reg+imm6
234def addr : ComplexPattern<iPTR, 2, "SelectAddr", [], [SDNPWantRoot]>;
235
236// AsmOperand class for a pointer register.
237// Used with the LD/ST family of instructions.
238// See FSTLD in AVRInstrFormats.td
239def PtrRegAsmOperand : AsmOperandClass { let Name = "Reg"; }
240
241// A special operand type for the LD/ST instructions.
242// It converts the pointer register number into a two-bit field used in the
243// instruction.
244def LDSTPtrReg : Operand<i16> {
245  let MIOperandInfo = (ops PTRREGS);
246  let EncoderMethod = "encodeLDSTPtrReg";
247
248  let ParserMatchClass = PtrRegAsmOperand;
249}
250
251// A special operand type for the LDD/STD instructions.
252// It behaves identically to the LD/ST version, except restricts
253// the pointer registers to Y and Z.
254def LDDSTDPtrReg : Operand<i16> {
255  let MIOperandInfo = (ops PTRDISPREGS);
256  let EncoderMethod = "encodeLDSTPtrReg";
257
258  let ParserMatchClass = PtrRegAsmOperand;
259}
260
261//===----------------------------------------------------------------------===//
262// AVR predicates for subtarget features
263//===----------------------------------------------------------------------===//
264
265def HasSRAM : Predicate<"Subtarget->hasSRAM()">,
266              AssemblerPredicate<(all_of FeatureSRAM)>;
267
268def HasJMPCALL : Predicate<"Subtarget->hasJMPCALL()">,
269                 AssemblerPredicate<(all_of FeatureJMPCALL)>;
270
271def HasIJMPCALL : Predicate<"Subtarget->hasIJMPCALL()">,
272                  AssemblerPredicate<(all_of FeatureIJMPCALL)>;
273
274def HasEIJMPCALL : Predicate<"Subtarget->hasEIJMPCALL()">,
275                   AssemblerPredicate<(all_of FeatureEIJMPCALL)>;
276
277def HasADDSUBIW : Predicate<"Subtarget->hasADDSUBIW()">,
278                  AssemblerPredicate<(all_of FeatureADDSUBIW)>;
279
280def HasSmallStack : Predicate<"Subtarget->HasSmallStack()">,
281                    AssemblerPredicate<(all_of FeatureSmallStack)>;
282
283def HasMOVW : Predicate<"Subtarget->hasMOVW()">,
284              AssemblerPredicate<(all_of FeatureMOVW)>;
285
286def HasLPM : Predicate<"Subtarget->hasLPM()">,
287             AssemblerPredicate<(all_of FeatureLPM)>;
288
289def HasLPMX : Predicate<"Subtarget->hasLPMX()">,
290              AssemblerPredicate<(all_of FeatureLPMX)>;
291
292def HasELPM : Predicate<"Subtarget->hasELPM()">,
293              AssemblerPredicate<(all_of FeatureELPM)>;
294
295def HasELPMX : Predicate<"Subtarget->hasELPMX()">,
296               AssemblerPredicate<(all_of FeatureELPMX)>;
297
298def HasSPM : Predicate<"Subtarget->hasSPM()">,
299             AssemblerPredicate<(all_of FeatureSPM)>;
300
301def HasSPMX : Predicate<"Subtarget->hasSPMX()">,
302              AssemblerPredicate<(all_of FeatureSPMX)>;
303
304def HasDES : Predicate<"Subtarget->hasDES()">,
305             AssemblerPredicate<(all_of FeatureDES)>;
306
307def SupportsRMW : Predicate<"Subtarget->supportsRMW()">,
308                  AssemblerPredicate<(all_of FeatureRMW)>;
309
310def SupportsMultiplication : Predicate<"Subtarget->supportsMultiplication()">,
311                             AssemblerPredicate<(all_of FeatureMultiplication)>;
312
313def HasBREAK : Predicate<"Subtarget->hasBREAK()">,
314               AssemblerPredicate<(all_of FeatureBREAK)>;
315
316def HasTinyEncoding : Predicate<"Subtarget->hasTinyEncoding()">,
317                      AssemblerPredicate<(all_of FeatureTinyEncoding)>;
318
319// AVR specific condition code. These correspond to AVR_*_COND in
320// AVRInstrInfo.td. They must be kept in synch.
321def AVR_COND_EQ : PatLeaf<(i8 0)>;
322def AVR_COND_NE : PatLeaf<(i8 1)>;
323def AVR_COND_GE : PatLeaf<(i8 2)>;
324def AVR_COND_LT : PatLeaf<(i8 3)>;
325def AVR_COND_SH : PatLeaf<(i8 4)>;
326def AVR_COND_LO : PatLeaf<(i8 5)>;
327def AVR_COND_MI : PatLeaf<(i8 6)>;
328def AVR_COND_PL : PatLeaf<(i8 7)>;
329
330//===----------------------------------------------------------------------===//
331//===----------------------------------------------------------------------===//
332// AVR Instruction list
333//===----------------------------------------------------------------------===//
334//===----------------------------------------------------------------------===//
335
336// ADJCALLSTACKDOWN/UP implicitly use/def SP because they may be expanded into
337// a stack adjustment and the codegen must know that they may modify the stack
338// pointer before prolog-epilog rewriting occurs.
339// Pessimistically assume ADJCALLSTACKDOWN / ADJCALLSTACKUP will become
340// sub / add which can clobber SREG.
341let Defs = [SP, SREG], Uses = [SP] in {
342  def ADJCALLSTACKDOWN : Pseudo<(outs),
343                                (ins i16imm
344                                 : $amt, i16imm
345                                 : $amt2),
346                                "#ADJCALLSTACKDOWN", [(AVRcallseq_start timm
347                                                       : $amt, timm
348                                                       : $amt2)]>;
349
350  // R31R30 is used to update SP. It is normally free because it is a
351  // call-clobbered register but it is necessary to set it as a def as the
352  // register allocator might use it in rare cases (for rematerialization, it
353  // seems). hasSideEffects needs to be set to true so this instruction isn't
354  // considered dead.
355  let Defs = [R31R30], hasSideEffects = 1 in def ADJCALLSTACKUP
356      : Pseudo<(outs),
357               (ins i16imm
358                : $amt1, i16imm
359                : $amt2),
360               "#ADJCALLSTACKUP", [(AVRcallseq_end timm
361                                    : $amt1, timm
362                                    : $amt2)]>;
363}
364
365//===----------------------------------------------------------------------===//
366// Addition
367//===----------------------------------------------------------------------===//
368let isCommutable = 1, Constraints = "$src = $rd", Defs = [SREG] in {
369  // ADD Rd, Rr
370  // Adds two 8-bit registers.
371  def ADDRdRr
372      : FRdRr<0b0000, 0b11,
373              (outs GPR8
374               : $rd),
375              (ins GPR8
376               : $src, GPR8
377               : $rr),
378              "add\t$rd, $rr",
379              [(set i8
380                : $rd, (add i8
381                        : $src, i8
382                        : $rr)),
383               (implicit SREG)]>;
384
385  // ADDW Rd+1:Rd, Rr+1:Rr
386  // Pseudo instruction to add four 8-bit registers as two 16-bit values.
387  //
388  // Expands to:
389  // add Rd,    Rr
390  // adc Rd+1, Rr+1
391  def ADDWRdRr
392      : Pseudo<(outs DREGS
393                : $rd),
394               (ins DREGS
395                : $src, DREGS
396                : $rr),
397               "addw\t$rd, $rr",
398               [(set i16
399                 : $rd, (add i16
400                         : $src, i16
401                         : $rr)),
402                (implicit SREG)]>;
403
404  // ADC Rd, Rr
405  // Adds two 8-bit registers with carry.
406  let Uses = [SREG] in def ADCRdRr
407      : FRdRr<0b0001, 0b11,
408              (outs GPR8
409               : $rd),
410              (ins GPR8
411               : $src, GPR8
412               : $rr),
413              "adc\t$rd, $rr",
414              [(set i8
415                : $rd, (adde i8
416                        : $src, i8
417                        : $rr)),
418               (implicit SREG)]>;
419
420  // ADCW Rd+1:Rd, Rr+1:Rr
421  // Pseudo instruction to add four 8-bit registers as two 16-bit values with
422  // carry.
423  //
424  // Expands to:
425  // adc Rd,   Rr
426  // adc Rd+1, Rr+1
427  let Uses = [SREG] in def ADCWRdRr : Pseudo<(outs DREGS
428                                              : $rd),
429                                             (ins DREGS
430                                              : $src, DREGS
431                                              : $rr),
432                                             "adcw\t$rd, $rr", [
433                                               (set i16
434                                                : $rd, (adde i16
435                                                        : $src, i16
436                                                        : $rr)),
437                                               (implicit SREG)
438                                             ]>;
439
440  // AIDW Rd, k
441  // Adds an immediate 6-bit value K to Rd, placing the result in Rd.
442  def ADIWRdK
443      : FWRdK<0b0,
444              (outs IWREGS
445               : $rd),
446              (ins IWREGS
447               : $src, imm_arith6
448               : $k),
449              "adiw\t$rd, $k",
450              [(set i16
451                : $rd, (add i16
452                        : $src, uimm6
453                        : $k)),
454               (implicit SREG)]>,
455        Requires<[HasADDSUBIW]>;
456}
457
458//===----------------------------------------------------------------------===//
459// Subtraction
460//===----------------------------------------------------------------------===//
461let Constraints = "$src = $rd", Defs = [SREG] in {
462  // SUB Rd, Rr
463  // Subtracts the 8-bit value of Rr from Rd and places the value in Rd.
464  def SUBRdRr
465      : FRdRr<0b0001, 0b10,
466              (outs GPR8
467               : $rd),
468              (ins GPR8
469               : $src, GPR8
470               : $rr),
471              "sub\t$rd, $rr",
472              [(set i8
473                : $rd, (sub i8
474                        : $src, i8
475                        : $rr)),
476               (implicit SREG)]>;
477
478  // SUBW Rd+1:Rd, Rr+1:Rr
479  // Subtracts two 16-bit values and places the result into Rd.
480  //
481  // Expands to:
482  // sub Rd,   Rr
483  // sbc Rd+1, Rr+1
484  def SUBWRdRr
485      : Pseudo<(outs DREGS
486                : $rd),
487               (ins DREGS
488                : $src, DREGS
489                : $rr),
490               "subw\t$rd, $rr",
491               [(set i16
492                 : $rd, (sub i16
493                         : $src, i16
494                         : $rr)),
495                (implicit SREG)]>;
496
497  def SUBIRdK
498      : FRdK<0b0101,
499             (outs LD8
500              : $rd),
501             (ins LD8
502              : $src, imm_ldi8
503              : $k),
504             "subi\t$rd, $k",
505             [(set i8
506               : $rd, (sub i8
507                       : $src, imm
508                       : $k)),
509              (implicit SREG)]>;
510
511  // SUBIW Rd+1:Rd, K+1:K
512  //
513  // Expands to:
514  // subi Rd,   K
515  // sbci Rd+1, K+1
516  def SUBIWRdK
517      : Pseudo<(outs DLDREGS
518                : $rd),
519               (ins DLDREGS
520                : $src, i16imm
521                : $rr),
522               "subiw\t$rd, $rr",
523               [(set i16
524                 : $rd, (sub i16
525                         : $src, imm
526                         : $rr)),
527                (implicit SREG)]>;
528
529  def SBIWRdK
530      : FWRdK<0b1,
531              (outs IWREGS
532               : $rd),
533              (ins IWREGS
534               : $src, imm_arith6
535               : $k),
536              "sbiw\t$rd, $k",
537              [(set i16
538                : $rd, (sub i16
539                        : $src, uimm6
540                        : $k)),
541               (implicit SREG)]>,
542        Requires<[HasADDSUBIW]>;
543
544  // Subtract with carry operations which must read the carry flag in SREG.
545  let Uses = [SREG] in {
546    def SBCRdRr
547        : FRdRr<0b0000, 0b10,
548                (outs GPR8
549                 : $rd),
550                (ins GPR8
551                 : $src, GPR8
552                 : $rr),
553                "sbc\t$rd, $rr",
554                [(set i8
555                  : $rd, (sube i8
556                          : $src, i8
557                          : $rr)),
558                 (implicit SREG)]>;
559
560    // SBCW Rd+1:Rd, Rr+1:Rr
561    //
562    // Expands to:
563    // sbc Rd,   Rr
564    // sbc Rd+1, Rr+1
565    def SBCWRdRr : Pseudo<(outs DREGS
566                           : $rd),
567                          (ins DREGS
568                           : $src, DREGS
569                           : $rr),
570                          "sbcw\t$rd, $rr", [
571                            (set i16
572                             : $rd, (sube i16
573                                     : $src, i16
574                                     : $rr)),
575                            (implicit SREG)
576                          ]>;
577
578    def SBCIRdK
579        : FRdK<0b0100,
580               (outs LD8
581                : $rd),
582               (ins LD8
583                : $src, imm_ldi8
584                : $k),
585               "sbci\t$rd, $k",
586               [(set i8
587                 : $rd, (sube i8
588                         : $src, imm
589                         : $k)),
590                (implicit SREG)]>;
591
592    // SBCIW Rd+1:Rd, K+1:K
593    // sbci Rd,   K
594    // sbci Rd+1, K+1
595    def SBCIWRdK : Pseudo<(outs DLDREGS
596                           : $rd),
597                          (ins DLDREGS
598                           : $src, i16imm
599                           : $rr),
600                          "sbciw\t$rd, $rr", [
601                            (set i16
602                             : $rd, (sube i16
603                                     : $src, imm
604                                     : $rr)),
605                            (implicit SREG)
606                          ]>;
607  }
608}
609
610//===----------------------------------------------------------------------===//
611// Increment and Decrement
612//===----------------------------------------------------------------------===//
613let Constraints = "$src = $rd", Defs = [SREG] in {
614  def INCRd
615      : FRd<0b1001, 0b0100011,
616            (outs GPR8
617             : $rd),
618            (ins GPR8
619             : $src),
620            "inc\t$rd", [(set i8
621                          : $rd, (add i8
622                                  : $src, 1)),
623                         (implicit SREG)]>;
624
625  def DECRd
626      : FRd<0b1001, 0b0101010,
627            (outs GPR8
628             : $rd),
629            (ins GPR8
630             : $src),
631            "dec\t$rd", [(set i8
632                          : $rd, (add i8
633                                  : $src, -1)),
634                         (implicit SREG)]>;
635}
636
637//===----------------------------------------------------------------------===//
638// Multiplication
639//===----------------------------------------------------------------------===//
640
641let isCommutable = 1, Defs = [R1, R0, SREG] in {
642  // MUL Rd, Rr
643  // Multiplies Rd by Rr and places the result into R1:R0.
644  let usesCustomInserter = 1 in {
645    def MULRdRr : FRdRr<0b1001, 0b11, (outs),
646                        (ins GPR8
647                         : $lhs, GPR8
648                         : $rhs),
649                        "mul\t$lhs, $rhs",
650                        [/*(set R1, R0, (smullohi i8:$lhs, i8:$rhs))*/]>,
651                  Requires<[SupportsMultiplication]>;
652
653    def MULSRdRr : FMUL2RdRr<0, (outs),
654                             (ins LD8
655                              : $lhs, LD8
656                              : $rhs),
657                             "muls\t$lhs, $rhs", []>,
658                   Requires<[SupportsMultiplication]>;
659  }
660
661  def MULSURdRr : FMUL2RdRr<1, (outs),
662                            (ins LD8lo
663                             : $lhs, LD8lo
664                             : $rhs),
665                            "mulsu\t$lhs, $rhs", []>,
666                  Requires<[SupportsMultiplication]>;
667
668  def FMUL : FFMULRdRr<0b01, (outs),
669                       (ins LD8lo
670                        : $lhs, LD8lo
671                        : $rhs),
672                       "fmul\t$lhs, $rhs", []>,
673             Requires<[SupportsMultiplication]>;
674
675  def FMULS : FFMULRdRr<0b10, (outs),
676                        (ins LD8lo
677                         : $lhs, LD8lo
678                         : $rhs),
679                        "fmuls\t$lhs, $rhs", []>,
680              Requires<[SupportsMultiplication]>;
681
682  def FMULSU : FFMULRdRr<0b11, (outs),
683                         (ins LD8lo
684                          : $lhs, LD8lo
685                          : $rhs),
686                         "fmulsu\t$lhs, $rhs", []>,
687               Requires<[SupportsMultiplication]>;
688}
689
690let Defs =
691    [R15, R14, R13, R12, R11, R10, R9, R8, R7, R6, R5, R4, R3, R2, R1,
692     R0] in def DESK : FDES<(outs),
693                            (ins i8imm
694                             : $k),
695                            "des\t$k", []>,
696    Requires<[HasDES]>;
697
698//===----------------------------------------------------------------------===//
699// Logic
700//===----------------------------------------------------------------------===//
701let Constraints = "$src = $rd", Defs = [SREG] in {
702  // Register-Register logic instructions (which have the
703  // property of commutativity).
704  let isCommutable = 1 in {
705    def ANDRdRr
706        : FRdRr<0b0010, 0b00,
707                (outs GPR8
708                 : $rd),
709                (ins GPR8
710                 : $src, GPR8
711                 : $rr),
712                "and\t$rd, $rr",
713                [(set i8
714                  : $rd, (and i8
715                          : $src, i8
716                          : $rr)),
717                 (implicit SREG)]>;
718
719    // ANDW Rd+1:Rd, Rr+1:Rr
720    //
721    // Expands to:
722    // and Rd,   Rr
723    // and Rd+1, Rr+1
724    def ANDWRdRr : Pseudo<(outs DREGS
725                           : $rd),
726                          (ins DREGS
727                           : $src, DREGS
728                           : $rr),
729                          "andw\t$rd, $rr", [
730                            (set i16
731                             : $rd, (and i16
732                                     : $src, i16
733                                     : $rr)),
734                            (implicit SREG)
735                          ]>;
736
737    def ORRdRr
738        : FRdRr<0b0010, 0b10,
739                (outs GPR8
740                 : $rd),
741                (ins GPR8
742                 : $src, GPR8
743                 : $rr),
744                "or\t$rd, $rr",
745                [(set i8
746                  : $rd, (or i8
747                          : $src, i8
748                          : $rr)),
749                 (implicit SREG)]>;
750
751    // ORW Rd+1:Rd, Rr+1:Rr
752    //
753    // Expands to:
754    // or Rd,   Rr
755    // or Rd+1, Rr+1
756    def ORWRdRr : Pseudo<(outs DREGS
757                          : $rd),
758                         (ins DREGS
759                          : $src, DREGS
760                          : $rr),
761                         "orw\t$rd, $rr", [
762                           (set i16
763                            : $rd, (or i16
764                                    : $src, i16
765                                    : $rr)),
766                           (implicit SREG)
767                         ]>;
768
769    def EORRdRr
770        : FRdRr<0b0010, 0b01,
771                (outs GPR8
772                 : $rd),
773                (ins GPR8
774                 : $src, GPR8
775                 : $rr),
776                "eor\t$rd, $rr",
777                [(set i8
778                  : $rd, (xor i8
779                          : $src, i8
780                          : $rr)),
781                 (implicit SREG)]>;
782
783    // EORW Rd+1:Rd, Rr+1:Rr
784    //
785    // Expands to:
786    // eor Rd,   Rr
787    // eor Rd+1, Rr+1
788    def EORWRdRr : Pseudo<(outs DREGS
789                           : $rd),
790                          (ins DREGS
791                           : $src, DREGS
792                           : $rr),
793                          "eorw\t$rd, $rr", [
794                            (set i16
795                             : $rd, (xor i16
796                                     : $src, i16
797                                     : $rr)),
798                            (implicit SREG)
799                          ]>;
800  }
801
802  def ANDIRdK
803      : FRdK<0b0111,
804             (outs LD8
805              : $rd),
806             (ins LD8
807              : $src, imm_ldi8
808              : $k),
809             "andi\t$rd, $k",
810             [(set i8
811               : $rd, (and i8
812                       : $src, imm
813                       : $k)),
814              (implicit SREG)]>;
815
816  // ANDI Rd+1:Rd, K+1:K
817  //
818  // Expands to:
819  // andi Rd,   K
820  // andi Rd+1, K+1
821  def ANDIWRdK
822      : Pseudo<(outs DLDREGS
823                : $rd),
824               (ins DLDREGS
825                : $src, i16imm
826                : $k),
827               "andiw\t$rd, $k",
828               [(set i16
829                 : $rd, (and i16
830                         : $src, imm
831                         : $k)),
832                (implicit SREG)]>;
833
834  def ORIRdK
835      : FRdK<0b0110,
836             (outs LD8
837              : $rd),
838             (ins LD8
839              : $src, imm_ldi8
840              : $k),
841             "ori\t$rd, $k",
842             [(set i8
843               : $rd, (or i8
844                       : $src, imm
845                       : $k)),
846              (implicit SREG)]>;
847
848  // ORIW Rd+1:Rd, K+1,K
849  //
850  // Expands to:
851  // ori Rd,   K
852  // ori Rd+1, K+1
853  def ORIWRdK
854      : Pseudo<(outs DLDREGS
855                : $rd),
856               (ins DLDREGS
857                : $src, i16imm
858                : $rr),
859               "oriw\t$rd, $rr",
860               [(set i16
861                 : $rd, (or i16
862                         : $src, imm
863                         : $rr)),
864                (implicit SREG)]>;
865}
866
867//===----------------------------------------------------------------------===//
868// One's/Two's Complement
869//===----------------------------------------------------------------------===//
870let Constraints = "$src = $rd", Defs = [SREG] in {
871  def COMRd
872      : FRd<0b1001, 0b0100000,
873            (outs GPR8
874             : $rd),
875            (ins GPR8
876             : $src),
877            "com\t$rd", [(set i8
878                          : $rd, (not i8
879                                  : $src)),
880                         (implicit SREG)]>;
881
882  // COMW Rd+1:Rd
883  //
884  // Expands to:
885  // com Rd
886  // com Rd+1
887  def COMWRd : Pseudo<(outs DREGS
888                       : $rd),
889                      (ins DREGS
890                       : $src),
891                      "comw\t$rd",
892                      [(set i16
893                        : $rd, (not i16
894                                : $src)),
895                       (implicit SREG)]>;
896
897  def NEGRd
898      : FRd<0b1001, 0b0100001,
899            (outs GPR8
900             : $rd),
901            (ins GPR8
902             : $src),
903            "neg\t$rd", [(set i8
904                          : $rd, (ineg i8
905                                  : $src)),
906                         (implicit SREG)]>;
907
908  // NEGW Rd+1:Rd
909  //
910  // Expands to:
911  // neg Rd+1
912  // neg Rd
913  // sbc Rd+1, r1
914  def NEGWRd : Pseudo<(outs DREGS
915                       : $rd),
916                      (ins DREGS
917                       : $src),
918                      "negw\t$rd",
919                      [(set i16
920                        : $rd, (ineg i16
921                                : $src)),
922                       (implicit SREG)]>;
923}
924
925// TST Rd
926// Test for zero of minus.
927// This operation is identical to a `Rd AND Rd`.
928def : InstAlias<"tst\t$rd", (ANDRdRr GPR8 : $rd, GPR8 : $rd)>;
929
930// SBR Rd, K
931//
932// Mnemonic alias to 'ORI Rd, K'. Same bit pattern, same operands,
933// same everything.
934def : InstAlias<"sbr\t$rd, $k",
935                (ORIRdK LD8
936                 : $rd, imm_ldi8
937                 : $k),
938                /* Disable display, so we don't override ORI */ 0>;
939
940//===----------------------------------------------------------------------===//
941// Jump instructions
942//===----------------------------------------------------------------------===//
943let isBarrier = 1, isBranch = 1, isTerminator = 1 in {
944  def RJMPk : FBRk<0, (outs),
945                   (ins brtarget_13
946                    : $target),
947                   "rjmp\t$target", [(br bb
948                                      : $target)]>;
949
950  let isIndirectBranch = 1,
951      Uses = [R31R30] in def IJMP
952      : F16<0b1001010000001001, (outs), (ins), "ijmp", []>,
953      Requires<[HasIJMPCALL]>;
954
955  let isIndirectBranch = 1,
956      Uses = [R31R30] in def EIJMP
957      : F16<0b1001010000011001, (outs), (ins), "eijmp", []>,
958      Requires<[HasEIJMPCALL]>;
959
960  def JMPk : F32BRk<0b110, (outs),
961                    (ins call_target
962                     : $k),
963                    "jmp\t$k", []>,
964             Requires<[HasJMPCALL]>;
965}
966
967//===----------------------------------------------------------------------===//
968// Call instructions
969//===----------------------------------------------------------------------===//
970let isCall = 1 in {
971  // SP is marked as a use to prevent stack-pointer assignments that appear
972  // immediately before calls from potentially appearing dead.
973  let Uses = [SP] in def RCALLk : FBRk<1, (outs), (ins rcalltarget_13:$k),
974                                       "rcall\t$k", [(AVRcall imm:$k)]>;
975
976  // SP is marked as a use to prevent stack-pointer assignments that appear
977  // immediately before calls from potentially appearing dead.
978  let Uses = [SP, R31R30] in def ICALL
979      : F16<0b1001010100001001, (outs), (ins variable_ops), "icall", []>,
980      Requires<[HasIJMPCALL]>;
981
982  // SP is marked as a use to prevent stack-pointer assignments that appear
983  // immediately before calls from potentially appearing dead.
984  let Uses = [SP, R31R30] in def EICALL
985      : F16<0b1001010100011001, (outs), (ins variable_ops), "eicall", []>,
986      Requires<[HasEIJMPCALL]>;
987
988  // SP is marked as a use to prevent stack-pointer assignments that appear
989  // immediately before calls from potentially appearing dead.
990  //
991  // TODO: the imm field can be either 16 or 22 bits in devices with more
992  // than 64k of ROM, fix it once we support the largest devices.
993  let Uses = [SP] in def CALLk : F32BRk<0b111, (outs), (ins call_target:$k),
994                                        "call\t$k", [(AVRcall imm:$k)]>,
995      Requires<[HasJMPCALL]>;
996}
997
998//===----------------------------------------------------------------------===//
999// Return instructions.
1000//===----------------------------------------------------------------------===//
1001let isTerminator = 1, isReturn = 1, isBarrier = 1 in {
1002  def RET : F16<0b1001010100001000, (outs), (ins), "ret", [(AVRretflag)]>;
1003
1004  def RETI : F16<0b1001010100011000, (outs), (ins), "reti", [(AVRretiflag)]>;
1005}
1006
1007//===----------------------------------------------------------------------===//
1008// Compare operations.
1009//===----------------------------------------------------------------------===//
1010let Defs = [SREG] in {
1011  // CPSE Rd, Rr
1012  // Compare Rd and Rr, skipping the next instruction if they are equal.
1013  let isBarrier = 1, isBranch = 1,
1014      isTerminator = 1 in def CPSE : FRdRr<0b0001, 0b00, (outs),
1015                                           (ins GPR8
1016                                            : $rd, GPR8
1017                                            : $rr),
1018                                           "cpse\t$rd, $rr", []>;
1019
1020  def CPRdRr
1021      : FRdRr<0b0001, 0b01, (outs),
1022              (ins GPR8
1023               : $rd, GPR8
1024               : $rr),
1025              "cp\t$rd, $rr", [(AVRcmp i8
1026                                : $rd, i8
1027                                : $rr),
1028                               (implicit SREG)]>;
1029
1030  // CPW Rd+1:Rd, Rr+1:Rr
1031  //
1032  // Expands to:
1033  // cp  Rd,   Rr
1034  // cpc Rd+1, Rr+1
1035  def CPWRdRr : Pseudo<(outs),
1036                       (ins DREGS
1037                        : $src, DREGS
1038                        : $src2),
1039                       "cpw\t$src, $src2",
1040                       [(AVRcmp i16
1041                         : $src, i16
1042                         : $src2),
1043                        (implicit SREG)]>;
1044
1045  let Uses = [SREG] in def CPCRdRr
1046      : FRdRr<0b0000, 0b01, (outs),
1047              (ins GPR8
1048               : $rd, GPR8
1049               : $rr),
1050              "cpc\t$rd, $rr", [(AVRcmpc i8
1051                                 : $rd, i8
1052                                 : $rr),
1053                                (implicit SREG)]>;
1054
1055  // CPCW Rd+1:Rd. Rr+1:Rr
1056  //
1057  // Expands to:
1058  // cpc Rd,   Rr
1059  // cpc Rd+1, Rr+1
1060  let Uses = [SREG] in def CPCWRdRr
1061      : Pseudo<(outs),
1062               (ins DREGS
1063                : $src, DREGS
1064                : $src2),
1065               "cpcw\t$src, $src2",
1066               [(AVRcmpc i16
1067                 : $src, i16
1068                 : $src2),
1069                (implicit SREG)]>;
1070
1071  // CPI Rd, K
1072  // Compares a register with an 8 bit immediate.
1073  def CPIRdK
1074      : FRdK<0b0011, (outs),
1075             (ins LD8
1076              : $rd, imm_ldi8
1077              : $k),
1078             "cpi\t$rd, $k", [(AVRcmp i8
1079                               : $rd, imm
1080                               : $k),
1081                              (implicit SREG)]>;
1082}
1083
1084//===----------------------------------------------------------------------===//
1085// Register conditional skipping/branching operations.
1086//===----------------------------------------------------------------------===//
1087let isBranch = 1, isTerminator = 1 in {
1088  // Conditional skipping on GPR register bits, and
1089  // conditional skipping on IO register bits.
1090  let isBarrier = 1 in {
1091    def SBRCRrB : FRdB<0b10, (outs),
1092                       (ins GPR8
1093                        : $rr, i8imm
1094                        : $b),
1095                       "sbrc\t$rr, $b", []>;
1096
1097    def SBRSRrB : FRdB<0b11, (outs),
1098                       (ins GPR8
1099                        : $rr, i8imm
1100                        : $b),
1101                       "sbrs\t$rr, $b", []>;
1102
1103    def SBICAb : FIOBIT<0b01, (outs),
1104                        (ins imm_port5
1105                         : $a, i8imm
1106                         : $b),
1107                        "sbic\t$a, $b", []>;
1108
1109    def SBISAb : FIOBIT<0b11, (outs),
1110                        (ins imm_port5
1111                         : $a, i8imm
1112                         : $b),
1113                        "sbis\t$a, $b", []>;
1114  }
1115
1116  // Relative branches on status flag bits.
1117  let Uses = [SREG] in {
1118    // BRBS s, k
1119    // Branch if `s` flag in status register is set.
1120    def BRBSsk : FSK<0, (outs),
1121                     (ins i8imm
1122                      : $s, relbrtarget_7
1123                      : $k),
1124                     "brbs\t$s, $k", []>;
1125
1126    // BRBC s, k
1127    // Branch if `s` flag in status register is clear.
1128    def BRBCsk : FSK<1, (outs),
1129                     (ins i8imm
1130                      : $s, relbrtarget_7
1131                      : $k),
1132                     "brbc\t$s, $k", []>;
1133  }
1134}
1135
1136// BRCS k
1137// Branch if carry flag is set
1138def : InstAlias<"brcs\t$k", (BRBSsk 0, relbrtarget_7 : $k)>;
1139
1140// BRCC k
1141// Branch if carry flag is clear
1142def : InstAlias<"brcc\t$k", (BRBCsk 0, relbrtarget_7 : $k)>;
1143
1144// BRHS k
1145// Branch if half carry flag is set
1146def : InstAlias<"brhs\t$k", (BRBSsk 5, relbrtarget_7 : $k)>;
1147
1148// BRHC k
1149// Branch if half carry flag is clear
1150def : InstAlias<"brhc\t$k", (BRBCsk 5, relbrtarget_7 : $k)>;
1151
1152// BRTS k
1153// Branch if the T flag is set
1154def : InstAlias<"brts\t$k", (BRBSsk 6, relbrtarget_7 : $k)>;
1155
1156// BRTC k
1157// Branch if the T flag is clear
1158def : InstAlias<"brtc\t$k", (BRBCsk 6, relbrtarget_7 : $k)>;
1159
1160// BRVS k
1161// Branch if the overflow flag is set
1162def : InstAlias<"brvs\t$k", (BRBSsk 3, relbrtarget_7 : $k)>;
1163
1164// BRVC k
1165// Branch if the overflow flag is clear
1166def : InstAlias<"brvc\t$k", (BRBCsk 3, relbrtarget_7 : $k)>;
1167
1168// BRIE k
1169// Branch if the global interrupt flag is enabled
1170def : InstAlias<"brie\t$k", (BRBSsk 7, relbrtarget_7 : $k)>;
1171
1172// BRID k
1173// Branch if the global interrupt flag is disabled
1174def : InstAlias<"brid\t$k", (BRBCsk 7, relbrtarget_7 : $k)>;
1175
1176//===----------------------------------------------------------------------===//
1177// PC-relative conditional branches
1178//===----------------------------------------------------------------------===//
1179// Based on status register. We cannot simplify these into instruction aliases
1180// because we also need to be able to specify a pattern to match for ISel.
1181let isBranch = 1, isTerminator = 1, Uses = [SREG] in {
1182  def BREQk : FBRsk<0, 0b001, (outs),
1183                    (ins relbrtarget_7
1184                     : $target),
1185                    "breq\t$target", [(AVRbrcond bb
1186                                       : $target, AVR_COND_EQ)]>;
1187
1188  def BRNEk : FBRsk<1, 0b001, (outs),
1189                    (ins relbrtarget_7
1190                     : $target),
1191                    "brne\t$target", [(AVRbrcond bb
1192                                       : $target, AVR_COND_NE)]>;
1193
1194  def BRSHk : FBRsk<1, 0b000, (outs),
1195                    (ins relbrtarget_7
1196                     : $target),
1197                    "brsh\t$target", [(AVRbrcond bb
1198                                       : $target, AVR_COND_SH)]>;
1199
1200  def BRLOk : FBRsk<0, 0b000, (outs),
1201                    (ins relbrtarget_7
1202                     : $target),
1203                    "brlo\t$target", [(AVRbrcond bb
1204                                       : $target, AVR_COND_LO)]>;
1205
1206  def BRMIk : FBRsk<0, 0b010, (outs),
1207                    (ins relbrtarget_7
1208                     : $target),
1209                    "brmi\t$target", [(AVRbrcond bb
1210                                       : $target, AVR_COND_MI)]>;
1211
1212  def BRPLk : FBRsk<1, 0b010, (outs),
1213                    (ins relbrtarget_7
1214                     : $target),
1215                    "brpl\t$target", [(AVRbrcond bb
1216                                       : $target, AVR_COND_PL)]>;
1217
1218  def BRGEk : FBRsk<1, 0b100, (outs),
1219                    (ins relbrtarget_7
1220                     : $target),
1221                    "brge\t$target", [(AVRbrcond bb
1222                                       : $target, AVR_COND_GE)]>;
1223
1224  def BRLTk : FBRsk<0, 0b100, (outs),
1225                    (ins relbrtarget_7
1226                     : $target),
1227                    "brlt\t$target", [(AVRbrcond bb
1228                                       : $target, AVR_COND_LT)]>;
1229}
1230
1231//===----------------------------------------------------------------------===//
1232// Data transfer instructions
1233//===----------------------------------------------------------------------===//
1234// 8 and 16-bit register move instructions.
1235let hasSideEffects = 0 in {
1236  def MOVRdRr : FRdRr<0b0010, 0b11,
1237                      (outs GPR8
1238                       : $rd),
1239                      (ins GPR8
1240                       : $rr),
1241                      "mov\t$rd, $rr", []>;
1242
1243  def MOVWRdRr : FMOVWRdRr<(outs DREGS
1244                            : $dst),
1245                           (ins DREGS
1246                            : $src),
1247                           "movw\t$dst, $src", []>,
1248                 Requires<[HasMOVW]>;
1249}
1250
1251// Load immediate values into registers.
1252let isReMaterializable = 1 in {
1253  def LDIRdK : FRdK<0b1110,
1254                    (outs LD8
1255                     : $rd),
1256                    (ins imm_ldi8
1257                     : $k),
1258                    "ldi\t$rd, $k", [(set i8
1259                                      : $rd, imm
1260                                      : $k)]>;
1261
1262  // LDIW Rd+1:Rd, K+1:K
1263  //
1264  // Expands to:
1265  // ldi Rd,   K
1266  // ldi Rd+1, K+1
1267  def LDIWRdK : Pseudo<(outs DLDREGS
1268                        : $dst),
1269                       (ins i16imm
1270                        : $src),
1271                       "ldiw\t$dst, $src", [(set i16
1272                                             : $dst, imm
1273                                             : $src)]>;
1274}
1275
1276// Load from data space into register.
1277let canFoldAsLoad = 1, isReMaterializable = 1 in {
1278  def LDSRdK : F32DM<0b0,
1279                     (outs GPR8
1280                      : $rd),
1281                     (ins imm16
1282                      : $k),
1283                     "lds\t$rd, $k", [(set i8
1284                                       : $rd, (load imm
1285                                               : $k))]>,
1286               Requires<[HasSRAM]>;
1287
1288  // LDSW Rd+1:Rd, K+1:K
1289  //
1290  // Expands to:
1291  // lds Rd,  (K+1:K)
1292  // lds Rd+1 (K+1:K) + 1
1293  def LDSWRdK : Pseudo<(outs DREGS
1294                        : $dst),
1295                       (ins i16imm
1296                        : $src),
1297                       "ldsw\t$dst, $src", [(set i16
1298                                             : $dst, (load imm
1299                                                      : $src))]>,
1300                Requires<[HasSRAM]>;
1301}
1302
1303// Indirect loads.
1304let canFoldAsLoad = 1, isReMaterializable = 1 in {
1305  def LDRdPtr : FSTLD<0, 0b00,
1306                      (outs GPR8
1307                       : $reg),
1308                      (ins LDSTPtrReg
1309                       : $ptrreg),
1310                      "ld\t$reg, $ptrreg", [(set GPR8
1311                                             : $reg, (load i16
1312                                                      : $ptrreg))]>,
1313                Requires<[HasSRAM]>;
1314
1315  // LDW Rd+1:Rd, P
1316  //
1317  // Expands to:
1318  // ld  Rd,   P
1319  // ldd Rd+1, P+1
1320  let Constraints = "@earlyclobber $reg" in def LDWRdPtr
1321      : Pseudo<(outs DREGS
1322                : $reg),
1323               (ins PTRDISPREGS
1324                : $ptrreg),
1325               "ldw\t$reg, $ptrreg", [(set i16
1326                                       : $reg, (load i16
1327                                                : $ptrreg))]>,
1328      Requires<[HasSRAM]>;
1329}
1330
1331// Indirect loads (with postincrement or predecrement).
1332let mayLoad = 1, hasSideEffects = 0,
1333    Constraints = "$ptrreg = $base_wb,@earlyclobber $reg" in {
1334  def LDRdPtrPi : FSTLD<0, 0b01,
1335                        (outs GPR8
1336                         : $reg, PTRREGS
1337                         : $base_wb),
1338                        (ins LDSTPtrReg
1339                         : $ptrreg),
1340                        "ld\t$reg, $ptrreg+", []>,
1341                  Requires<[HasSRAM]>;
1342
1343  // LDW Rd+1:Rd, P+
1344  // Expands to:
1345  // ld Rd,   P+
1346  // ld Rd+1, P+
1347  def LDWRdPtrPi : Pseudo<(outs DREGS
1348                           : $reg, PTRREGS
1349                           : $base_wb),
1350                          (ins PTRREGS
1351                           : $ptrreg),
1352                          "ldw\t$reg, $ptrreg+", []>,
1353                   Requires<[HasSRAM]>;
1354
1355  def LDRdPtrPd : FSTLD<0, 0b10,
1356                        (outs GPR8
1357                         : $reg, PTRREGS
1358                         : $base_wb),
1359                        (ins LDSTPtrReg
1360                         : $ptrreg),
1361                        "ld\t$reg, -$ptrreg", []>,
1362                  Requires<[HasSRAM]>;
1363
1364  // LDW Rd+1:Rd, -P
1365  //
1366  // Expands to:
1367  // ld Rd+1, -P
1368  // ld Rd,   -P
1369  def LDWRdPtrPd : Pseudo<(outs DREGS
1370                           : $reg, PTRREGS
1371                           : $base_wb),
1372                          (ins PTRREGS
1373                           : $ptrreg),
1374                          "ldw\t$reg, -$ptrreg", []>,
1375                   Requires<[HasSRAM]>;
1376}
1377
1378// Load indirect with displacement operations.
1379let canFoldAsLoad = 1, isReMaterializable = 1 in {
1380  let Constraints = "@earlyclobber $reg" in def LDDRdPtrQ
1381      : FSTDLDD<0,
1382                (outs GPR8
1383                 : $reg),
1384                (ins memri
1385                 : $memri),
1386                "ldd\t$reg, $memri", [(set i8
1387                                       : $reg, (load addr
1388                                                : $memri))]>,
1389      Requires<[HasSRAM]>;
1390
1391  // LDDW Rd+1:Rd, P+q
1392  //
1393  // Expands to:
1394  // ldd Rd,   P+q
1395  // ldd Rd+1, P+q+1
1396  let Constraints = "@earlyclobber $dst" in def LDDWRdPtrQ
1397      : Pseudo<(outs DREGS
1398                : $dst),
1399               (ins memri
1400                : $memri),
1401               "lddw\t$dst, $memri", [(set i16
1402                                       : $dst, (load addr
1403                                                : $memri))]>,
1404      Requires<[HasSRAM]>;
1405
1406  // An identical pseudo instruction to LDDWRdPtrQ, expect restricted to the Y
1407  // register and without the @earlyclobber flag.
1408  //
1409  // Used to work around a bug caused by the register allocator not
1410  // being able to handle the expansion of a COPY into an machine instruction
1411  // that has an earlyclobber flag. This is because the register allocator will
1412  // try expand a copy from a register slot into an earlyclobber instruction.
1413  // Instructions that are earlyclobber need to be in a dedicated earlyclobber
1414  // slot.
1415  //
1416  // This pseudo instruction can be used pre-AVR pseudo expansion in order to
1417  // get a frame index load without directly using earlyclobber instructions.
1418  //
1419  // The pseudo expansion pass trivially expands this into LDDWRdPtrQ.
1420  //
1421  // This instruction may be removed once PR13375 is fixed.
1422  let mayLoad = 1,
1423      hasSideEffects = 0 in def LDDWRdYQ : Pseudo<(outs DREGS
1424                                                   : $dst),
1425                                                  (ins memri
1426                                                   : $memri),
1427                                                  "lddw\t$dst, $memri", []>,
1428      Requires<[HasSRAM]>;
1429}
1430
1431class AtomicLoad<PatFrag Op, RegisterClass DRC, RegisterClass PTRRC>
1432    : Pseudo<(outs DRC
1433              : $rd),
1434             (ins PTRRC
1435              : $rr),
1436             "atomic_op", [(set DRC
1437                            : $rd, (Op i16
1438                                    : $rr))]>;
1439
1440class AtomicStore<PatFrag Op, RegisterClass DRC, RegisterClass PTRRC>
1441    : Pseudo<(outs),
1442             (ins PTRRC
1443              : $rd, DRC
1444              : $rr),
1445             "atomic_op", [(Op i16
1446                            : $rd, DRC
1447                            : $rr)]>;
1448
1449let Constraints =
1450    "@earlyclobber $rd" in class AtomicLoadOp<PatFrag Op, RegisterClass DRC,
1451                                              RegisterClass PTRRC>
1452    : Pseudo<(outs DRC
1453              : $rd),
1454             (ins PTRRC
1455              : $rr, DRC
1456              : $operand),
1457             "atomic_op", [(set DRC
1458                            : $rd, (Op i16
1459                                    : $rr, DRC
1460                                    : $operand))]>;
1461
1462// FIXME: I think 16-bit atomic binary ops need to mark
1463// r0 as clobbered.
1464
1465// Atomic instructions
1466// ===================
1467//
1468// These are all expanded by AVRExpandPseudoInsts
1469//
1470// 8-bit operations can use any pointer register because
1471// they are expanded directly into an LD/ST instruction.
1472//
1473// 16-bit operations use 16-bit load/store postincrement instructions,
1474// which require PTRDISPREGS.
1475
1476def AtomicLoad8 : AtomicLoad<atomic_load_8, GPR8, PTRREGS>;
1477def AtomicLoad16 : AtomicLoad<atomic_load_16, DREGS, PTRDISPREGS>;
1478
1479def AtomicStore8 : AtomicStore<atomic_store_8, GPR8, PTRREGS>;
1480def AtomicStore16 : AtomicStore<atomic_store_16, DREGS, PTRDISPREGS>;
1481
1482class AtomicLoadOp8<PatFrag Op> : AtomicLoadOp<Op, GPR8, PTRREGS>;
1483class AtomicLoadOp16<PatFrag Op> : AtomicLoadOp<Op, DREGS, PTRDISPREGS>;
1484
1485def AtomicLoadAdd8 : AtomicLoadOp8<atomic_load_add_8>;
1486def AtomicLoadAdd16 : AtomicLoadOp16<atomic_load_add_16>;
1487def AtomicLoadSub8 : AtomicLoadOp8<atomic_load_sub_8>;
1488def AtomicLoadSub16 : AtomicLoadOp16<atomic_load_sub_16>;
1489def AtomicLoadAnd8 : AtomicLoadOp8<atomic_load_and_8>;
1490def AtomicLoadAnd16 : AtomicLoadOp16<atomic_load_and_16>;
1491def AtomicLoadOr8 : AtomicLoadOp8<atomic_load_or_8>;
1492def AtomicLoadOr16 : AtomicLoadOp16<atomic_load_or_16>;
1493def AtomicLoadXor8 : AtomicLoadOp8<atomic_load_xor_8>;
1494def AtomicLoadXor16 : AtomicLoadOp16<atomic_load_xor_16>;
1495def AtomicFence
1496    : Pseudo<(outs), (ins), "atomic_fence", [(atomic_fence timm, timm)]>;
1497
1498// Indirect store from register to data space.
1499def STSKRr : F32DM<0b1, (outs),
1500                   (ins imm16
1501                    : $k, GPR8
1502                    : $rd),
1503                   "sts\t$k, $rd", [(store i8
1504                                     : $rd, imm
1505                                     : $k)]>,
1506             Requires<[HasSRAM]>;
1507
1508// STSW K+1:K, Rr+1:Rr
1509//
1510// Expands to:
1511// sts Rr+1, (K+1:K) + 1
1512// sts Rr,   (K+1:K)
1513def STSWKRr : Pseudo<(outs),
1514                     (ins i16imm
1515                      : $dst, DREGS
1516                      : $src),
1517                     "stsw\t$dst, $src", [(store i16
1518                                           : $src, imm
1519                                           : $dst)]>,
1520              Requires<[HasSRAM]>;
1521
1522// Indirect stores.
1523// ST P, Rr
1524// Stores the value of Rr into the location addressed by pointer P.
1525def STPtrRr : FSTLD<1, 0b00, (outs),
1526                    (ins LDSTPtrReg
1527                     : $ptrreg, GPR8
1528                     : $reg),
1529                    "st\t$ptrreg, $reg", [(store GPR8
1530                                           : $reg, i16
1531                                           : $ptrreg)]>,
1532              Requires<[HasSRAM]>;
1533
1534// STW P, Rr+1:Rr
1535// Stores the value of Rr into the location addressed by pointer P.
1536//
1537// Expands to:
1538// st P, Rr
1539// std P+1, Rr+1
1540def STWPtrRr : Pseudo<(outs),
1541                      (ins PTRDISPREGS
1542                       : $ptrreg, DREGS
1543                       : $reg),
1544                      "stw\t$ptrreg, $reg", [(store i16
1545                                              : $reg, i16
1546                                              : $ptrreg)]>,
1547               Requires<[HasSRAM]>;
1548
1549// Indirect stores (with postincrement or predecrement).
1550let Constraints = "$ptrreg = $base_wb,@earlyclobber $base_wb" in {
1551
1552  // ST P+, Rr
1553  // Stores the value of Rr into the location addressed by pointer P.
1554  // Post increments P.
1555  def STPtrPiRr : FSTLD<1, 0b01,
1556                        (outs LDSTPtrReg
1557                         : $base_wb),
1558                        (ins LDSTPtrReg
1559                         : $ptrreg, GPR8
1560                         : $reg, i8imm
1561                         : $offs),
1562                        "st\t$ptrreg+, $reg", [(set i16
1563                                                : $base_wb, (post_store GPR8
1564                                                             : $reg, i16
1565                                                             : $ptrreg, imm
1566                                                             : $offs))]>,
1567                  Requires<[HasSRAM]>;
1568
1569  // STW P+, Rr+1:Rr
1570  // Stores the value of Rr into the location addressed by pointer P.
1571  // Post increments P.
1572  //
1573  // Expands to:
1574  // st P+, Rr
1575  // st P+, Rr+1
1576  def STWPtrPiRr : Pseudo<(outs PTRREGS
1577                           : $base_wb),
1578                          (ins PTRREGS
1579                           : $ptrreg, DREGS
1580                           : $trh, i8imm
1581                           : $offs),
1582                          "stw\t$ptrreg+, $trh", [(set PTRREGS
1583                                                   : $base_wb, (post_store DREGS
1584                                                                : $trh, PTRREGS
1585                                                                : $ptrreg, imm
1586                                                                : $offs))]>,
1587                   Requires<[HasSRAM]>;
1588
1589  // ST -P, Rr
1590  // Stores the value of Rr into the location addressed by pointer P.
1591  // Pre decrements P.
1592  def STPtrPdRr : FSTLD<1, 0b10,
1593                        (outs LDSTPtrReg
1594                         : $base_wb),
1595                        (ins LDSTPtrReg
1596                         : $ptrreg, GPR8
1597                         : $reg, i8imm
1598                         : $offs),
1599                        "st\t-$ptrreg, $reg", [(set i16
1600                                                : $base_wb, (pre_store GPR8
1601                                                             : $reg, i16
1602                                                             : $ptrreg, imm
1603                                                             : $offs))]>,
1604                  Requires<[HasSRAM]>;
1605
1606  // STW -P, Rr+1:Rr
1607  // Stores the value of Rr into the location addressed by pointer P.
1608  // Pre decrements P.
1609  //
1610  // Expands to:
1611  // st -P, Rr+1
1612  // st -P, Rr
1613  def STWPtrPdRr : Pseudo<(outs PTRREGS
1614                           : $base_wb),
1615                          (ins PTRREGS
1616                           : $ptrreg, DREGS
1617                           : $reg, i8imm
1618                           : $offs),
1619                          "stw\t-$ptrreg, $reg", [(set PTRREGS
1620                                                   : $base_wb, (pre_store i16
1621                                                                : $reg, i16
1622                                                                : $ptrreg, imm
1623                                                                : $offs))]>,
1624                   Requires<[HasSRAM]>;
1625}
1626
1627// Store indirect with displacement operations.
1628// STD P+q, Rr
1629// Stores the value of Rr into the location addressed by pointer P with a
1630// displacement of q. Does not modify P.
1631def STDPtrQRr : FSTDLDD<1, (outs),
1632                        (ins memri
1633                         : $memri, GPR8
1634                         : $reg),
1635                        "std\t$memri, $reg", [(store i8
1636                                               : $reg, addr
1637                                               : $memri)]>,
1638                Requires<[HasSRAM]>;
1639
1640// STDW P+q, Rr+1:Rr
1641// Stores the value of Rr into the location addressed by pointer P with a
1642// displacement of q. Does not modify P.
1643//
1644// Expands to:
1645// std P+q,   Rr
1646// std P+q+1, Rr+1
1647def STDWPtrQRr : Pseudo<(outs),
1648                        (ins memri
1649                         : $memri, DREGS
1650                         : $src),
1651                        "stdw\t$memri, $src", [(store i16
1652                                                : $src, addr
1653                                                : $memri)]>,
1654                 Requires<[HasSRAM]>;
1655
1656// Load program memory operations.
1657let canFoldAsLoad = 1, isReMaterializable = 1, mayLoad = 1,
1658    hasSideEffects = 0 in {
1659  let Defs = [R0],
1660      Uses = [R31R30] in def LPM
1661      : F16<0b1001010111001000, (outs), (ins), "lpm", []>,
1662      Requires<[HasLPM]>;
1663
1664  def LPMRdZ : FLPMX<0, 0,
1665                     (outs GPR8
1666                      : $dst),
1667                     (ins ZREG
1668                      : $z),
1669                     "lpm\t$dst, $z", []>,
1670               Requires<[HasLPMX]>;
1671
1672  // Load program memory, while postincrementing the Z register.
1673  let Defs = [R31R30] in {
1674    def LPMRdZPi : FLPMX<0, 1,
1675                         (outs GPR8
1676                          : $dst),
1677                         (ins ZREG
1678                          : $z),
1679                         "lpm\t$dst, $z+", []>,
1680                   Requires<[HasLPMX]>;
1681
1682    def LPMWRdZ : Pseudo<(outs DREGS
1683                          : $dst),
1684                         (ins ZREG
1685                          : $z),
1686                         "lpmw\t$dst, $z", []>,
1687                  Requires<[HasLPMX]>;
1688
1689    def LPMWRdZPi : Pseudo<(outs DREGS
1690                            : $dst),
1691                           (ins ZREG
1692                            : $z),
1693                           "lpmw\t$dst, $z+", []>,
1694                    Requires<[HasLPMX]>;
1695  }
1696}
1697
1698// Extended load program memory operations.
1699let mayLoad = 1, hasSideEffects = 0 in {
1700  let Defs = [R0],
1701      Uses = [R31R30] in def ELPM
1702      : F16<0b1001010111011000, (outs), (ins), "elpm", []>,
1703      Requires<[HasELPM]>;
1704
1705  def ELPMRdZ : FLPMX<1, 0, (outs GPR8:$dst), (ins ZREG:$z),
1706                      "elpm\t$dst, $z", []>,
1707                Requires<[HasELPMX]>;
1708
1709  let Defs = [R31R30] in {
1710    def ELPMRdZPi : FLPMX<1, 1, (outs GPR8:$dst), (ins ZREG:$z),
1711                          "elpm\t$dst, $z+", []>,
1712                    Requires<[HasELPMX]>;
1713  }
1714
1715  // These pseudos are combination of the OUT and ELPM instructions.
1716  let Defs = [R31R30], hasSideEffects = 1 in {
1717    def ELPMBRdZ : Pseudo<(outs GPR8:$dst), (ins ZREG:$z, LD8:$p),
1718                          "elpmb\t$dst, $z, $p", []>,
1719                   Requires<[HasELPMX]>;
1720
1721    def ELPMWRdZ : Pseudo<(outs DREGS:$dst), (ins ZREG:$z, LD8:$p),
1722                          "elpmw\t$dst, $z, $p", []>,
1723                   Requires<[HasELPMX]>;
1724
1725    def ELPMBRdZPi : Pseudo<(outs GPR8:$dst), (ins ZREG:$z, LD8:$p),
1726                            "elpmb\t$dst, $z+, $p", []>,
1727                     Requires<[HasELPMX]>;
1728
1729    def ELPMWRdZPi : Pseudo<(outs DREGS:$dst), (ins ZREG:$z, LD8:$p),
1730                            "elpmw\t$dst, $z+, $p", []>,
1731                     Requires<[HasELPMX]>;
1732  }
1733}
1734
1735// Store program memory operations.
1736let Uses = [R1, R0] in {
1737  let Uses = [R31R30, R1, R0] in def SPM
1738      : F16<0b1001010111101000, (outs), (ins), "spm", []>,
1739      Requires<[HasSPM]>;
1740
1741  let Defs = [R31R30] in def SPMZPi : F16<0b1001010111111000, (outs),
1742                                          (ins ZREG
1743                                           : $z),
1744                                          "spm $z+", []>,
1745      Requires<[HasSPMX]>;
1746}
1747
1748// Read data from IO location operations.
1749let canFoldAsLoad = 1, isReMaterializable = 1 in {
1750  def INRdA : FIORdA<(outs GPR8
1751                      : $dst),
1752                     (ins imm_port6
1753                      : $src),
1754                     "in\t$dst, $src", [(set i8
1755                                         : $dst, (load ioaddr8
1756                                                  : $src))]>;
1757
1758  def INWRdA : Pseudo<(outs DREGS
1759                       : $dst),
1760                      (ins imm_port6
1761                       : $src),
1762                      "inw\t$dst, $src", [(set i16
1763                                           : $dst, (load ioaddr16
1764                                                    : $src))]>;
1765}
1766
1767// Write data to IO location operations.
1768def OUTARr : FIOARr<(outs),
1769                    (ins imm_port6
1770                     : $dst, GPR8
1771                     : $src),
1772                    "out\t$dst, $src", [(store i8
1773                                         : $src, ioaddr8
1774                                         : $dst)]>;
1775
1776def OUTWARr : Pseudo<(outs),
1777                     (ins imm_port6
1778                      : $dst, DREGS
1779                      : $src),
1780                     "outw\t$dst, $src", [(store i16
1781                                           : $src, ioaddr16
1782                                           : $dst)]>;
1783
1784// Stack push/pop operations.
1785let Defs = [SP], Uses = [SP], hasSideEffects = 0 in {
1786  // Stack push operations.
1787  let mayStore = 1 in {
1788    def PUSHRr : FRd<0b1001, 0b0011111, (outs),
1789                     (ins GPR8
1790                      : $reg),
1791                     "push\t$reg", []>,
1792                 Requires<[HasSRAM]>;
1793
1794    def PUSHWRr : Pseudo<(outs),
1795                         (ins DREGS
1796                          : $reg),
1797                         "pushw\t$reg", []>,
1798                  Requires<[HasSRAM]>;
1799  }
1800
1801  // Stack pop operations.
1802  let mayLoad = 1 in {
1803    def POPRd : FRd<0b1001, 0b0001111,
1804                    (outs GPR8
1805                     : $reg),
1806                    (ins), "pop\t$reg", []>,
1807                Requires<[HasSRAM]>;
1808
1809    def POPWRd : Pseudo<(outs DREGS
1810                         : $reg),
1811                        (ins), "popw\t$reg", []>,
1812                 Requires<[HasSRAM]>;
1813  }
1814}
1815
1816// Read-Write-Modify (RMW) instructions.
1817def XCHZRd : FZRd<0b100,
1818                  (outs GPR8
1819                   : $rd),
1820                  (ins ZREG
1821                   : $z),
1822                  "xch\t$z, $rd", []>,
1823             Requires<[SupportsRMW]>;
1824
1825def LASZRd : FZRd<0b101,
1826                  (outs GPR8
1827                   : $rd),
1828                  (ins ZREG
1829                   : $z),
1830                  "las\t$z, $rd", []>,
1831             Requires<[SupportsRMW]>;
1832
1833def LACZRd : FZRd<0b110,
1834                  (outs GPR8
1835                   : $rd),
1836                  (ins ZREG
1837                   : $z),
1838                  "lac\t$z, $rd", []>,
1839             Requires<[SupportsRMW]>;
1840
1841def LATZRd : FZRd<0b111,
1842                  (outs GPR8
1843                   : $rd),
1844                  (ins ZREG
1845                   : $z),
1846                  "lat\t$z, $rd", []>,
1847             Requires<[SupportsRMW]>;
1848
1849//===----------------------------------------------------------------------===//
1850// Bit and bit-test instructions
1851//===----------------------------------------------------------------------===//
1852
1853// Bit shift/rotate operations.
1854let Constraints = "$src = $rd", Defs = [SREG] in {
1855  // 8-bit LSL is an alias of ADD Rd, Rd
1856
1857  def LSLWRd : Pseudo<(outs DREGS
1858                       : $rd),
1859                      (ins DREGS
1860                       : $src),
1861                      "lslw\t$rd",
1862                      [(set i16
1863                        : $rd, (AVRlsl i16
1864                                : $src)),
1865                       (implicit SREG)]>;
1866
1867  def LSLWHiRd : Pseudo<(outs DREGS:$rd), (ins DREGS:$src), "lslwhi\t$rd",
1868                        [(set i16:$rd, (AVRlslhi i16:$src)), (implicit SREG)]>;
1869
1870  def LSLWNRd : Pseudo<(outs DLDREGS
1871                        : $rd),
1872                       (ins DREGS
1873                        : $src, imm16
1874                        : $bits),
1875                       "lslwn\t$rd, $bits", [
1876                         (set i16
1877                          : $rd, (AVRlslwn i16
1878                                  : $src, imm
1879                                  : $bits)),
1880                         (implicit SREG)
1881                       ]>;
1882
1883  def LSLBNRd : Pseudo<(outs LD8
1884                        : $rd),
1885                       (ins GPR8
1886                        : $src, imm_ldi8
1887                        : $bits),
1888                       "lslbn\t$rd, $bits", [
1889                         (set i8
1890                          : $rd, (AVRlslbn i8
1891                                  : $src, imm
1892                                  : $bits)),
1893                         (implicit SREG)
1894                       ]>;
1895
1896  def LSRRd
1897      : FRd<0b1001, 0b0100110,
1898            (outs GPR8
1899             : $rd),
1900            (ins GPR8
1901             : $src),
1902            "lsr\t$rd", [(set i8
1903                          : $rd, (AVRlsr i8
1904                                  : $src)),
1905                         (implicit SREG)]>;
1906
1907  def LSRWRd : Pseudo<(outs DREGS
1908                       : $rd),
1909                      (ins DREGS
1910                       : $src),
1911                      "lsrw\t$rd",
1912                      [(set i16
1913                        : $rd, (AVRlsr i16
1914                                : $src)),
1915                       (implicit SREG)]>;
1916
1917  def LSRWLoRd : Pseudo<(outs DREGS:$rd), (ins DREGS:$src), "lsrwlo\t$rd",
1918                        [(set i16:$rd, (AVRlsrlo i16:$src)), (implicit SREG)]>;
1919
1920  def LSRWNRd : Pseudo<(outs DLDREGS
1921                        : $rd),
1922                       (ins DREGS
1923                        : $src, imm16
1924                        : $bits),
1925                       "lsrwn\t$rd, $bits", [
1926                         (set i16
1927                          : $rd, (AVRlsrwn i16
1928                                  : $src, imm
1929                                  : $bits)),
1930                         (implicit SREG)
1931                       ]>;
1932
1933  def LSRBNRd : Pseudo<(outs LD8
1934                        : $rd),
1935                       (ins GPR8
1936                        : $src, imm_ldi8
1937                        : $bits),
1938                       "lsrbn\t$rd, $bits", [
1939                         (set i8
1940                          : $rd, (AVRlsrbn i8
1941                                  : $src, imm
1942                                  : $bits)),
1943                         (implicit SREG)
1944                       ]>;
1945
1946  def ASRRd
1947      : FRd<0b1001, 0b0100101,
1948            (outs GPR8
1949             : $rd),
1950            (ins GPR8
1951             : $src),
1952            "asr\t$rd", [(set i8
1953                          : $rd, (AVRasr i8
1954                                  : $src)),
1955                         (implicit SREG)]>;
1956
1957  def ASRWNRd : Pseudo<(outs DLDREGS
1958                        : $rd),
1959                       (ins DREGS
1960                        : $src, imm16
1961                        : $bits),
1962                       "asrwn\t$rd, $bits", [
1963                         (set i16
1964                          : $rd, (AVRasrwn i16
1965                                  : $src, imm
1966                                  : $bits)),
1967                         (implicit SREG)
1968                       ]>;
1969
1970  def ASRBNRd : Pseudo<(outs LD8
1971                        : $rd),
1972                       (ins GPR8
1973                        : $src, imm_ldi8
1974                        : $bits),
1975                       "asrbn\t$rd, $bits", [
1976                         (set i8
1977                          : $rd, (AVRasrbn i8
1978                                  : $src, imm
1979                                  : $bits)),
1980                         (implicit SREG)
1981                       ]>;
1982
1983  def ASRWRd : Pseudo<(outs DREGS
1984                       : $rd),
1985                      (ins DREGS
1986                       : $src),
1987                      "asrw\t$rd",
1988                      [(set i16
1989                        : $rd, (AVRasr i16
1990                                : $src)),
1991                       (implicit SREG)]>;
1992
1993  def ASRWLoRd : Pseudo<(outs DREGS:$rd), (ins DREGS:$src), "asrwlo\t$rd",
1994                        [(set i16:$rd, (AVRasrlo i16:$src)), (implicit SREG)]>;
1995
1996  def ROLBRd : Pseudo<(outs GPR8
1997                       : $rd),
1998                      (ins GPR8
1999                       : $src),
2000                      "rolb\t$rd",
2001                      [(set i8
2002                        : $rd, (AVRrol i8
2003                                : $src)),
2004                       (implicit SREG)]>;
2005
2006  def RORBRd : Pseudo<(outs GPR8
2007                       : $rd),
2008                      (ins GPR8
2009                       : $src),
2010                      "rorb\t$rd",
2011                      [(set i8
2012                        : $rd, (AVRror i8
2013                                : $src)),
2014                       (implicit SREG)]>;
2015
2016  // Bit rotate operations.
2017  let Uses = [SREG] in {
2018
2019    def ROLWRd
2020        : Pseudo<(outs DREGS
2021                  : $rd),
2022                 (ins DREGS
2023                  : $src),
2024                 "rolw\t$rd",
2025                 [(set i16
2026                   : $rd, (AVRrol i16
2027                           : $src)),
2028                  (implicit SREG)]>;
2029
2030    def RORRd : FRd<0b1001, 0b0100111,
2031                    (outs GPR8
2032                     : $rd),
2033                    (ins GPR8
2034                     : $src),
2035                    "ror\t$rd", []>;
2036
2037    def RORWRd
2038        : Pseudo<(outs DREGS
2039                  : $rd),
2040                 (ins DREGS
2041                  : $src),
2042                 "rorw\t$rd",
2043                 [(set i16
2044                   : $rd, (AVRror i16
2045                           : $src)),
2046                  (implicit SREG)]>;
2047  }
2048}
2049
2050// SWAP Rd
2051// Swaps the high and low nibbles in a register.
2052let Constraints =
2053    "$src = $rd" in def SWAPRd : FRd<0b1001, 0b0100010,
2054                                     (outs GPR8
2055                                      : $rd),
2056                                     (ins GPR8
2057                                      : $src),
2058                                     "swap\t$rd", [(set i8
2059                                                    : $rd, (AVRSwap i8
2060                                                            : $src))]>;
2061
2062// IO register bit set/clear operations.
2063//: TODO: add patterns when popcount(imm)==2 to be expanded with 2 sbi/cbi
2064// instead of in+ori+out which requires one more instr.
2065def SBIAb : FIOBIT<0b10, (outs),
2066                   (ins imm_port5
2067                    : $addr, i8imm
2068                    : $bit),
2069                   "sbi\t$addr, $bit", [(store(or(i8(load lowioaddr8
2070                                                     : $addr)),
2071                                               iobitpos8
2072                                               : $bit),
2073                                         lowioaddr8
2074                                         : $addr)]>;
2075
2076def CBIAb : FIOBIT<0b00, (outs),
2077                   (ins imm_port5
2078                    : $addr, i8imm
2079                    : $bit),
2080                   "cbi\t$addr, $bit", [(store(and(i8(load lowioaddr8
2081                                                      : $addr)),
2082                                               iobitposn8
2083                                               : $bit),
2084                                         lowioaddr8
2085                                         : $addr)]>;
2086
2087// Status register bit load/store operations.
2088let Defs = [SREG] in def BST : FRdB<0b01, (outs),
2089                                    (ins GPR8
2090                                     : $rd, i8imm
2091                                     : $b),
2092                                    "bst\t$rd, $b", []>;
2093
2094let Constraints = "$src = $rd",
2095    Uses = [SREG] in def BLD : FRdB<0b00,
2096                                    (outs GPR8
2097                                     : $rd),
2098                                    (ins GPR8
2099                                     : $src, i8imm
2100                                     : $b),
2101                                    "bld\t$rd, $b", []>;
2102
2103def CBR : InstAlias<"cbr\t$rd, $k", (ANDIRdK LD8 : $rd, imm_com8 : $k), 0>;
2104
2105// CLR Rd
2106// Alias for EOR Rd, Rd
2107// -------------
2108// Clears all bits in a register.
2109def CLR : InstAlias<"clr\t$rd", (EORRdRr GPR8 : $rd, GPR8 : $rd)>;
2110
2111// LSL Rd
2112// Alias for ADD Rd, Rd
2113// --------------
2114// Logical shift left one bit.
2115def LSL : InstAlias<"lsl\t$rd", (ADDRdRr GPR8 : $rd, GPR8 : $rd)>;
2116
2117def ROL : InstAlias<"rol\t$rd", (ADCRdRr GPR8 : $rd, GPR8 : $rd)>;
2118
2119// SER Rd
2120// Alias for LDI Rd, 0xff
2121// ---------
2122// Sets all bits in a register.
2123def : InstAlias<"ser\t$rd", (LDIRdK LD8 : $rd, 0xff), 0>;
2124
2125let Defs = [SREG] in def BSETs : FS<0, (outs),
2126                                    (ins i8imm
2127                                     : $s),
2128                                    "bset\t$s", []>;
2129
2130let Defs = [SREG] in def BCLRs : FS<1, (outs),
2131                                    (ins i8imm
2132                                     : $s),
2133                                    "bclr\t$s", []>;
2134
2135// Set/clear aliases for the carry (C) status flag (bit 0).
2136def : InstAlias<"sec", (BSETs 0)>;
2137def : InstAlias<"clc", (BCLRs 0)>;
2138
2139// Set/clear aliases for the zero (Z) status flag (bit 1).
2140def : InstAlias<"sez", (BSETs 1)>;
2141def : InstAlias<"clz", (BCLRs 1)>;
2142
2143// Set/clear aliases for the negative (N) status flag (bit 2).
2144def : InstAlias<"sen", (BSETs 2)>;
2145def : InstAlias<"cln", (BCLRs 2)>;
2146
2147// Set/clear aliases for the overflow (V) status flag (bit 3).
2148def : InstAlias<"sev", (BSETs 3)>;
2149def : InstAlias<"clv", (BCLRs 3)>;
2150
2151// Set/clear aliases for the signed (S) status flag (bit 4).
2152def : InstAlias<"ses", (BSETs 4)>;
2153def : InstAlias<"cls", (BCLRs 4)>;
2154
2155// Set/clear aliases for the half-carry (H) status flag (bit 5).
2156def : InstAlias<"seh", (BSETs 5)>;
2157def : InstAlias<"clh", (BCLRs 5)>;
2158
2159// Set/clear aliases for the T status flag (bit 6).
2160def : InstAlias<"set", (BSETs 6)>;
2161def : InstAlias<"clt", (BCLRs 6)>;
2162
2163// Set/clear aliases for the interrupt (I) status flag (bit 7).
2164def : InstAlias<"sei", (BSETs 7)>;
2165def : InstAlias<"cli", (BCLRs 7)>;
2166
2167//===----------------------------------------------------------------------===//
2168// Special/Control instructions
2169//===----------------------------------------------------------------------===//
2170
2171// BREAK
2172// Breakpoint instruction
2173// ---------
2174// <|1001|0101|1001|1000>
2175def BREAK : F16<0b1001010110011000, (outs), (ins), "break", []>,
2176            Requires<[HasBREAK]>;
2177
2178// NOP
2179// No-operation instruction
2180// ---------
2181// <|0000|0000|0000|0000>
2182def NOP : F16<0b0000000000000000, (outs), (ins), "nop", []>;
2183
2184// SLEEP
2185// Sleep instruction
2186// ---------
2187// <|1001|0101|1000|1000>
2188def SLEEP : F16<0b1001010110001000, (outs), (ins), "sleep", []>;
2189
2190// WDR
2191// Watchdog reset
2192// ---------
2193// <|1001|0101|1010|1000>
2194def WDR : F16<0b1001010110101000, (outs), (ins), "wdr", []>;
2195
2196//===----------------------------------------------------------------------===//
2197// Pseudo instructions for later expansion
2198//===----------------------------------------------------------------------===//
2199
2200//: TODO: Optimize this for wider types AND optimize the following code
2201//       compile int foo(char a, char b, char c, char d) {return d+b;}
2202//       looks like a missed sext_inreg opportunity.
2203def SEXT
2204    : ExtensionPseudo<(outs DREGS
2205                       : $dst),
2206                      (ins GPR8
2207                       : $src),
2208                      "sext\t$dst, $src",
2209                      [(set i16
2210                        : $dst, (sext i8
2211                                 : $src)),
2212                       (implicit SREG)]>;
2213
2214def ZEXT
2215    : ExtensionPseudo<(outs DREGS
2216                       : $dst),
2217                      (ins GPR8
2218                       : $src),
2219                      "zext\t$dst, $src",
2220                      [(set i16
2221                        : $dst, (zext i8
2222                                 : $src)),
2223                       (implicit SREG)]>;
2224
2225// This pseudo gets expanded into a movw+adiw thus it clobbers SREG.
2226let Defs = [SREG],
2227    hasSideEffects = 0 in def FRMIDX : Pseudo<(outs DLDREGS
2228                                               : $dst),
2229                                              (ins DLDREGS
2230                                               : $src, i16imm
2231                                               : $src2),
2232                                              "frmidx\t$dst, $src, $src2", []>;
2233
2234// This pseudo is either converted to a regular store or a push which clobbers
2235// SP.
2236def STDSPQRr : StorePseudo<(outs),
2237                           (ins memspi
2238                            : $dst, GPR8
2239                            : $src),
2240                           "stdstk\t$dst, $src", [(store i8
2241                                                   : $src, addr
2242                                                   : $dst)]>;
2243
2244// This pseudo is either converted to a regular store or a push which clobbers
2245// SP.
2246def STDWSPQRr : StorePseudo<(outs),
2247                            (ins memspi
2248                             : $dst, DREGS
2249                             : $src),
2250                            "stdwstk\t$dst, $src", [(store i16
2251                                                     : $src, addr
2252                                                     : $dst)]>;
2253
2254// SP read/write pseudos.
2255let hasSideEffects = 0 in {
2256  let Uses = [SP] in def SPREAD : Pseudo<(outs DREGS
2257                                          : $dst),
2258                                         (ins GPRSP
2259                                          : $src),
2260                                         "spread\t$dst, $src", []>;
2261
2262  let Defs = [SP] in def SPWRITE : Pseudo<(outs GPRSP
2263                                           : $dst),
2264                                          (ins DREGS
2265                                           : $src),
2266                                          "spwrite\t$dst, $src", []>;
2267}
2268
2269def Select8 : SelectPseudo<(outs GPR8
2270                            : $dst),
2271                           (ins GPR8
2272                            : $src, GPR8
2273                            : $src2, i8imm
2274                            : $cc),
2275                           "# Select8 PSEUDO", [(set i8
2276                                                 : $dst, (AVRselectcc i8
2277                                                          : $src, i8
2278                                                          : $src2, imm
2279                                                          : $cc))]>;
2280
2281def Select16 : SelectPseudo<(outs DREGS
2282                             : $dst),
2283                            (ins DREGS
2284                             : $src, DREGS
2285                             : $src2, i8imm
2286                             : $cc),
2287                            "# Select16 PSEUDO", [(set i16
2288                                                   : $dst, (AVRselectcc i16
2289                                                            : $src, i16
2290                                                            : $src2, imm
2291                                                            : $cc))]>;
2292
2293def Lsl8 : ShiftPseudo<(outs GPR8
2294                        : $dst),
2295                       (ins GPR8
2296                        : $src, GPR8
2297                        : $cnt),
2298                       "# Lsl8 PSEUDO", [(set i8
2299                                          : $dst, (AVRlslLoop i8
2300                                                   : $src, i8
2301                                                   : $cnt))]>;
2302
2303def Lsl16 : ShiftPseudo<(outs DREGS
2304                         : $dst),
2305                        (ins DREGS
2306                         : $src, GPR8
2307                         : $cnt),
2308                        "# Lsl16 PSEUDO", [(set i16
2309                                            : $dst, (AVRlslLoop i16
2310                                                     : $src, i8
2311                                                     : $cnt))]>;
2312
2313def Lsr8 : ShiftPseudo<(outs GPR8
2314                        : $dst),
2315                       (ins GPR8
2316                        : $src, GPR8
2317                        : $cnt),
2318                       "# Lsr8 PSEUDO", [(set i8
2319                                          : $dst, (AVRlsrLoop i8
2320                                                   : $src, i8
2321                                                   : $cnt))]>;
2322
2323def Lsr16 : ShiftPseudo<(outs DREGS
2324                         : $dst),
2325                        (ins DREGS
2326                         : $src, GPR8
2327                         : $cnt),
2328                        "# Lsr16 PSEUDO", [(set i16
2329                                            : $dst, (AVRlsrLoop i16
2330                                                     : $src, i8
2331                                                     : $cnt))]>;
2332
2333def Rol8 : ShiftPseudo<(outs GPR8
2334                        : $dst),
2335                       (ins GPR8
2336                        : $src, GPR8
2337                        : $cnt),
2338                       "# Rol8 PSEUDO", [(set i8
2339                                          : $dst, (AVRrolLoop i8
2340                                                   : $src, i8
2341                                                   : $cnt))]>;
2342
2343def Rol16 : ShiftPseudo<(outs DREGS
2344                         : $dst),
2345                        (ins DREGS
2346                         : $src, GPR8
2347                         : $cnt),
2348                        "# Rol16 PSEUDO", [(set i16
2349                                            : $dst, (AVRrolLoop i16
2350                                                     : $src, i8
2351                                                     : $cnt))]>;
2352
2353def Ror8 : ShiftPseudo<(outs GPR8
2354                        : $dst),
2355                       (ins GPR8
2356                        : $src, GPR8
2357                        : $cnt),
2358                       "# Ror8 PSEUDO", [(set i8
2359                                          : $dst, (AVRrorLoop i8
2360                                                   : $src, i8
2361                                                   : $cnt))]>;
2362
2363def Ror16 : ShiftPseudo<(outs DREGS
2364                         : $dst),
2365                        (ins DREGS
2366                         : $src, GPR8
2367                         : $cnt),
2368                        "# Ror16 PSEUDO", [(set i16
2369                                            : $dst, (AVRrorLoop i16
2370                                                     : $src, i8
2371                                                     : $cnt))]>;
2372
2373def Asr8 : ShiftPseudo<(outs GPR8
2374                        : $dst),
2375                       (ins GPR8
2376                        : $src, GPR8
2377                        : $cnt),
2378                       "# Asr8 PSEUDO", [(set i8
2379                                          : $dst, (AVRasrLoop i8
2380                                                   : $src, i8
2381                                                   : $cnt))]>;
2382
2383def Asr16 : ShiftPseudo<(outs DREGS
2384                         : $dst),
2385                        (ins DREGS
2386                         : $src, GPR8
2387                         : $cnt),
2388                        "# Asr16 PSEUDO", [(set i16
2389                                            : $dst, (AVRasrLoop i16
2390                                                     : $src, i8
2391                                                     : $cnt))]>;
2392
2393// lowered to a copy from R1, which contains the value zero.
2394let usesCustomInserter=1 in
2395def CopyR1 : Pseudo<(outs GPR8:$rd), (ins), "clrz\t$rd", [(set i8:$rd, 0)]>;
2396
2397//===----------------------------------------------------------------------===//
2398// Non-Instruction Patterns
2399//===----------------------------------------------------------------------===//
2400
2401//: TODO: look in x86InstrCompiler.td for odd encoding trick related to
2402// add x, 128 -> sub x, -128. Clang is emitting an eor for this (ldi+eor)
2403
2404// the add instruction always writes the carry flag
2405def : Pat<(addc i8 : $src, i8 : $src2), (ADDRdRr i8 : $src, i8 : $src2)>;
2406def : Pat<(addc DREGS
2407           : $src, DREGS
2408           : $src2),
2409          (ADDWRdRr DREGS
2410           : $src, DREGS
2411           : $src2)>;
2412
2413// all sub instruction variants always writes the carry flag
2414def : Pat<(subc i8 : $src, i8 : $src2), (SUBRdRr i8 : $src, i8 : $src2)>;
2415def : Pat<(subc i16 : $src, i16 : $src2), (SUBWRdRr i16 : $src, i16 : $src2)>;
2416def : Pat<(subc i8 : $src, imm : $src2), (SUBIRdK i8 : $src, imm : $src2)>;
2417def : Pat<(subc i16 : $src, imm : $src2), (SUBIWRdK i16 : $src, imm : $src2)>;
2418
2419// These patterns convert add (x, -imm) to sub (x, imm) since we dont have
2420// any add with imm instructions. Also take care of the adiw/sbiw instructions.
2421def : Pat<(add i16
2422           : $src1, imm0_63_neg
2423           : $src2),
2424          (SBIWRdK i16
2425           : $src1, (imm0_63_neg
2426                     : $src2))>;
2427def : Pat<(add i16
2428           : $src1, imm
2429           : $src2),
2430          (SUBIWRdK i16
2431           : $src1, (imm16_neg_XFORM imm
2432                     : $src2))>;
2433def : Pat<(addc i16
2434           : $src1, imm
2435           : $src2),
2436          (SUBIWRdK i16
2437           : $src1, (imm16_neg_XFORM imm
2438                     : $src2))>;
2439
2440def : Pat<(add i8
2441           : $src1, imm
2442           : $src2),
2443          (SUBIRdK i8
2444           : $src1, (imm8_neg_XFORM imm
2445                     : $src2))>;
2446def : Pat<(addc i8
2447           : $src1, imm
2448           : $src2),
2449          (SUBIRdK i8
2450           : $src1, (imm8_neg_XFORM imm
2451                     : $src2))>;
2452def : Pat<(adde i8
2453           : $src1, imm
2454           : $src2),
2455          (SBCIRdK i8
2456           : $src1, (imm8_neg_XFORM imm
2457                     : $src2))>;
2458
2459// Calls.
2460let Predicates = [HasJMPCALL] in {
2461  def : Pat<(AVRcall(i16 tglobaladdr:$dst)), (CALLk tglobaladdr:$dst)>;
2462  def : Pat<(AVRcall(i16 texternalsym:$dst)), (CALLk texternalsym:$dst)>;
2463}
2464def : Pat<(AVRcall(i16 tglobaladdr:$dst)), (RCALLk tglobaladdr:$dst)>;
2465def : Pat<(AVRcall(i16 texternalsym:$dst)), (RCALLk texternalsym:$dst)>;
2466
2467// `anyext`
2468def : Pat<(i16(anyext i8
2469               : $src)),
2470          (INSERT_SUBREG(i16(IMPLICIT_DEF)), i8
2471           : $src, sub_lo)>;
2472
2473// `trunc`
2474def : Pat<(i8(trunc i16 : $src)), (EXTRACT_SUBREG i16 : $src, sub_lo)>;
2475
2476// sext_inreg
2477def : Pat<(sext_inreg i16
2478           : $src, i8),
2479          (SEXT(i8(EXTRACT_SUBREG i16
2480                   : $src, sub_lo)))>;
2481
2482// GlobalAddress
2483def : Pat<(i16(AVRWrapper tglobaladdr : $dst)), (LDIWRdK tglobaladdr : $dst)>;
2484def : Pat<(add i16
2485           : $src, (AVRWrapper tglobaladdr
2486                    : $src2)),
2487          (SUBIWRdK i16
2488           : $src, tglobaladdr
2489           : $src2)>;
2490def : Pat<(i8(load(AVRWrapper tglobaladdr
2491                   : $dst))),
2492          (LDSRdK tglobaladdr
2493           : $dst)>;
2494def : Pat<(i16(load(AVRWrapper tglobaladdr
2495                    : $dst))),
2496          (LDSWRdK tglobaladdr
2497           : $dst)>;
2498def : Pat<(store i8
2499           : $src, (i16(AVRWrapper tglobaladdr
2500                        : $dst))),
2501          (STSKRr tglobaladdr
2502           : $dst, i8
2503           : $src)>;
2504def : Pat<(store i16
2505           : $src, (i16(AVRWrapper tglobaladdr
2506                        : $dst))),
2507          (STSWKRr tglobaladdr
2508           : $dst, i16
2509           : $src)>;
2510
2511// BlockAddress
2512def : Pat<(i16(AVRWrapper tblockaddress
2513               : $dst)),
2514          (LDIWRdK tblockaddress
2515           : $dst)>;
2516
2517def : Pat<(i8(trunc(AVRlsrwn DLDREGS
2518                    : $src, (i16 8)))),
2519          (EXTRACT_SUBREG DREGS
2520           : $src, sub_hi)>;
2521
2522// :FIXME: DAGCombiner produces an shl node after legalization from these seq:
2523// BR_JT -> (mul x, 2) -> (shl x, 1)
2524def : Pat<(shl i16 : $src1, (i8 1)), (LSLWRd i16 : $src1)>;
2525
2526// Lowering of 'tst' node to 'TST' instruction.
2527// TST is an alias of AND Rd, Rd.
2528def : Pat<(AVRtst i8 : $rd), (ANDRdRr GPR8 : $rd, GPR8 : $rd)>;
2529
2530// Lowering of 'lsl' node to 'LSL' instruction.
2531// LSL is an alias of 'ADD Rd, Rd'
2532def : Pat<(AVRlsl i8 : $rd), (ADDRdRr GPR8 : $rd, GPR8 : $rd)>;
2533