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