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