1 //===-- AVRInstrInfo.td - AVR Instruction defs -------------*- tablegen -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the AVR instructions in TableGen format.
12 //===----------------------------------------------------------------------===//
14 include "AVRInstrFormats.td"
16 //===----------------------------------------------------------------------===//
18 //===----------------------------------------------------------------------===//
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>]>;
31 //===----------------------------------------------------------------------===//
32 // AVR Specific Node Definitions
33 //===----------------------------------------------------------------------===//
35 def AVRretflag : SDNode<"AVRISD::RET_FLAG", SDTNone,
36 [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
37 def AVRretiflag : SDNode<"AVRISD::RETI_FLAG", SDTNone,
38 [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
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]>;
45 def AVRcall : SDNode<"AVRISD::CALL", SDT_AVRCall,
46 [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue, SDNPVariadic]>;
48 def AVRWrapper : SDNode<"AVRISD::WRAPPER", SDT_AVRWrapper>;
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]>;
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>;
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>;
71 //===----------------------------------------------------------------------===//
72 // AVR Operands, Complex Patterns and Transformations Definitions.
73 //===----------------------------------------------------------------------===//
75 def imm8_neg_XFORM : SDNodeXForm<imm,
77 return CurDAG->getTargetConstant(-N->getAPIntValue(), SDLoc(N), MVT::i8);
80 def imm16_neg_XFORM : SDNodeXForm<imm,
82 return CurDAG->getTargetConstant(-N->getAPIntValue(), SDLoc(N), MVT::i16);
85 def imm0_63_neg : PatLeaf<(imm),
87 int64_t val = -N->getSExtValue();
88 return val >= 0 && val < 64;
91 def uimm6 : PatLeaf<(imm), [{ return isUInt<6>(N->getZExtValue()); }]>;
93 def ioaddr_XFORM : SDNodeXForm<imm,
95 return CurDAG->getTargetConstant(uint8_t(N->getZExtValue()) - 0x20, SDLoc(N), MVT::i8);
98 def iobitpos8_XFORM : SDNodeXForm<imm,
100 return CurDAG->getTargetConstant(Log2_32(uint8_t(N->getZExtValue())),
104 def iobitposn8_XFORM : SDNodeXForm<imm,
106 return CurDAG->getTargetConstant(Log2_32(uint8_t(~N->getZExtValue())),
110 def ioaddr8 : PatLeaf<(imm),
112 uint64_t val = N->getZExtValue();
113 return val >= 0x20 && val < 0x60;
116 def lowioaddr8 : PatLeaf<(imm),
118 uint64_t val = N->getZExtValue();
119 return val >= 0x20 && val < 0x40;
122 def ioaddr16 : PatLeaf<(imm),
124 uint64_t val = N->getZExtValue();
125 return val >= 0x20 && val < 0x5f;
128 def iobitpos8 : PatLeaf<(imm),
130 return isPowerOf2_32(uint8_t(N->getZExtValue()));
131 }], iobitpos8_XFORM>;
133 def iobitposn8 : PatLeaf<(imm),
135 return isPowerOf2_32(uint8_t(~N->getZExtValue()));
136 }], iobitposn8_XFORM>;
138 def MemriAsmOperand : AsmOperandClass {
140 let ParserMethod = "parseMemriOperand";
143 /// Address operand for `reg+imm` used by STD and LDD.
144 def memri : Operand<iPTR>
146 let MIOperandInfo = (ops PTRDISPREGS, i16imm);
148 let PrintMethod = "printMemri";
149 let EncoderMethod = "encodeMemri";
151 let ParserMatchClass = MemriAsmOperand;
154 // Address operand for `SP+imm` used by STD{W}SPQRr
155 def memspi : Operand<iPTR>
157 let MIOperandInfo = (ops GPRSP, i16imm);
160 def imm_com8 : Operand<i8>
162 let EncoderMethod = "encodeComplement";
164 let MIOperandInfo = (ops i8imm);
167 def relbrtarget_7 : Operand<OtherVT>
169 let PrintMethod = "printPCRelImm";
170 let EncoderMethod = "encodeRelCondBrTarget<AVR::fixup_7_pcrel>";
173 def brtarget_13 : Operand<OtherVT>
175 let PrintMethod = "printPCRelImm";
176 let EncoderMethod = "encodeRelCondBrTarget<AVR::fixup_13_pcrel>";
179 // The target of a 22 or 16-bit call/jmp instruction.
180 def call_target : Operand<iPTR>
182 let EncoderMethod = "encodeCallTarget";
185 // A 16-bit address (which can lead to an R_AVR_16 relocation).
186 def imm16 : Operand<i16>
188 let EncoderMethod = "encodeImm<AVR::fixup_16, 2>";
191 /// A 6-bit immediate used in the ADIW/SBIW instructions.
192 def imm_arith6 : Operand<i16>
194 let EncoderMethod = "encodeImm<AVR::fixup_6_adiw, 0>";
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>
201 let EncoderMethod = "encodeImm<AVR::fixup_ldi, 0>";
204 /// A 5-bit port number used in SBIC and friends (the `FIOBIT` format).
205 def imm_port5 : Operand<i8>
207 let EncoderMethod = "encodeImm<AVR::fixup_port5, 0>";
210 /// A 6-bit port number used in the `IN` instruction and friends (the
212 def imm_port6 : Operand<i8>
214 let EncoderMethod = "encodeImm<AVR::fixup_port6, 0>";
217 // Addressing mode pattern reg+imm6
218 def addr : ComplexPattern<iPTR, 2, "SelectAddr", [], [SDNPWantRoot]>;
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
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
231 def LDSTPtrReg : Operand<i16>
233 let MIOperandInfo = (ops PTRREGS);
234 let EncoderMethod = "encodeLDSTPtrReg";
236 let ParserMatchClass = PtrRegAsmOperand;
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>
244 let MIOperandInfo = (ops PTRDISPREGS);
245 let EncoderMethod = "encodeLDSTPtrReg";
247 let ParserMatchClass = PtrRegAsmOperand;
250 //===----------------------------------------------------------------------===//
251 // AVR predicates for subtarget features
252 //===----------------------------------------------------------------------===//
254 def HasSRAM : Predicate<"Subtarget->hasSRAM()">,
255 AssemblerPredicate<"FeatureSRAM">;
257 def HasJMPCALL : Predicate<"Subtarget->hasJMPCALL()">,
258 AssemblerPredicate<"FeatureJMPCALL">;
260 def HasIJMPCALL : Predicate<"Subtarget->hasIJMPCALL()">,
261 AssemblerPredicate<"FeatureIJMPCALL">;
263 def HasEIJMPCALL : Predicate<"Subtarget->hasEIJMPCALL()">,
264 AssemblerPredicate<"FeatureEIJMPCALL">;
266 def HasADDSUBIW : Predicate<"Subtarget->hasADDSUBIW()">,
267 AssemblerPredicate<"FeatureADDSUBIW">;
269 def HasSmallStack : Predicate<"Subtarget->HasSmallStack()">,
270 AssemblerPredicate<"FeatureSmallStack">;
272 def HasMOVW : Predicate<"Subtarget->hasMOVW()">,
273 AssemblerPredicate<"FeatureMOVW">;
275 def HasLPM : Predicate<"Subtarget->hasLPM()">,
276 AssemblerPredicate<"FeatureLPM">;
278 def HasLPMX : Predicate<"Subtarget->hasLPMX()">,
279 AssemblerPredicate<"FeatureLPMX">;
281 def HasELPM : Predicate<"Subtarget->hasELPM()">,
282 AssemblerPredicate<"FeatureELPM">;
284 def HasELPMX : Predicate<"Subtarget->hasELPMX()">,
285 AssemblerPredicate<"FeatureELPMX">;
287 def HasSPM : Predicate<"Subtarget->hasSPM()">,
288 AssemblerPredicate<"FeatureSPM">;
290 def HasSPMX : Predicate<"Subtarget->hasSPMX()">,
291 AssemblerPredicate<"FeatureSPMX">;
293 def HasDES : Predicate<"Subtarget->hasDES()">,
294 AssemblerPredicate<"FeatureDES">;
296 def SupportsRMW : Predicate<"Subtarget->supportsRMW()">,
297 AssemblerPredicate<"FeatureRMW">;
299 def SupportsMultiplication : Predicate<"Subtarget->supportsMultiplication()">,
300 AssemblerPredicate<"FeatureMultiplication">;
302 def HasBREAK : Predicate<"Subtarget->hasBREAK()">,
303 AssemblerPredicate<"FeatureBREAK">;
305 def HasTinyEncoding : Predicate<"Subtarget->hasTinyEncoding()">,
306 AssemblerPredicate<"FeatureTinyEncoding">;
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)>;
321 //===----------------------------------------------------------------------===//
322 //===----------------------------------------------------------------------===//
323 // AVR Instruction list
324 //===----------------------------------------------------------------------===//
325 //===----------------------------------------------------------------------===//
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],
335 def ADJCALLSTACKDOWN : Pseudo<(outs),
336 (ins i16imm:$amt, i16imm:$amt2),
338 [(AVRcallseq_start timm:$amt, timm:$amt2)]>;
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],
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),
348 [(AVRcallseq_end timm:$amt1, timm:$amt2)]>;
351 //===----------------------------------------------------------------------===//
353 //===----------------------------------------------------------------------===//
354 let isCommutable = 1,
355 Constraints = "$src = $rd",
359 // Adds two 8-bit registers.
360 def ADDRdRr : FRdRr<0b0000,
363 (ins GPR8:$src, GPR8:$rr),
365 [(set i8:$rd, (add i8:$src, i8:$rr)),
368 // ADDW Rd+1:Rd, Rr+1:Rr
369 // Pseudo instruction to add four 8-bit registers as two 16-bit values.
374 def ADDWRdRr : Pseudo<(outs DREGS:$rd),
375 (ins DREGS:$src, DREGS:$rr),
377 [(set i16:$rd, (add i16:$src, i16:$rr)),
381 // Adds two 8-bit registers with carry.
383 def ADCRdRr : FRdRr<0b0001,
386 (ins GPR8:$src, GPR8:$rr),
388 [(set i8:$rd, (adde i8:$src, i8:$rr)),
391 // ADCW Rd+1:Rd, Rr+1:Rr
392 // Pseudo instruction to add four 8-bit registers as two 16-bit values with
399 def ADCWRdRr : Pseudo<(outs DREGS:$rd),
400 (ins DREGS:$src, DREGS:$rr),
402 [(set i16:$rd, (adde i16:$src, i16:$rr)),
406 // Adds an immediate 6-bit value K to Rd, placing the result in Rd.
407 def ADIWRdK : FWRdK<0b0,
409 (ins IWREGS:$src, imm_arith6:$k),
411 [(set i16:$rd, (add i16:$src, uimm6:$k)),
413 Requires<[HasADDSUBIW]>;
416 //===----------------------------------------------------------------------===//
418 //===----------------------------------------------------------------------===//
419 let Constraints = "$src = $rd",
423 // Subtracts the 8-bit value of Rr from Rd and places the value in Rd.
424 def SUBRdRr : FRdRr<0b0001,
427 (ins GPR8:$src, GPR8:$rr),
429 [(set i8:$rd, (sub i8:$src, i8:$rr)),
432 // SUBW Rd+1:Rd, Rr+1:Rr
433 // Subtracts two 16-bit values and places the result into Rd.
438 def SUBWRdRr : Pseudo<(outs DREGS:$rd),
439 (ins DREGS:$src, DREGS:$rr),
441 [(set i16:$rd, (sub i16:$src, i16:$rr)),
444 def SUBIRdK : FRdK<0b0101,
446 (ins LD8:$src, imm_ldi8:$k),
448 [(set i8:$rd, (sub i8:$src, imm:$k)),
451 // SUBIW Rd+1:Rd, K+1:K
456 def SUBIWRdK : Pseudo<(outs DLDREGS:$rd),
457 (ins DLDREGS:$src, i16imm:$rr),
459 [(set i16:$rd, (sub i16:$src, imm:$rr)),
462 def SBIWRdK : FWRdK<0b1,
464 (ins IWREGS:$src, imm_arith6:$k),
466 [(set i16:$rd, (sub i16:$src, uimm6:$k)),
468 Requires<[HasADDSUBIW]>;
470 // Subtract with carry operations which must read the carry flag in SREG.
473 def SBCRdRr : FRdRr<0b0000,
476 (ins GPR8:$src, GPR8:$rr),
478 [(set i8:$rd, (sube i8:$src, i8:$rr)),
481 // SBCW Rd+1:Rd, Rr+1:Rr
486 def SBCWRdRr : Pseudo<(outs DREGS:$rd),
487 (ins DREGS:$src, DREGS:$rr),
489 [(set i16:$rd, (sube i16:$src, i16:$rr)),
492 def SBCIRdK : FRdK<0b0100,
494 (ins LD8:$src, imm_ldi8:$k),
496 [(set i8:$rd, (sube i8:$src, imm:$k)),
499 // SBCIW Rd+1:Rd, K+1:K
502 def SBCIWRdK : Pseudo<(outs DLDREGS:$rd),
503 (ins DLDREGS:$src, i16imm:$rr),
505 [(set i16:$rd, (sube i16:$src, imm:$rr)),
510 //===----------------------------------------------------------------------===//
511 // Increment and Decrement
512 //===----------------------------------------------------------------------===//
513 let Constraints = "$src = $rd",
516 def INCRd : FRd<0b1001,
521 [(set i8:$rd, (add i8:$src, 1)), (implicit SREG)]>;
523 def DECRd : FRd<0b1001,
528 [(set i8:$rd, (add i8:$src, -1)), (implicit SREG)]>;
531 //===----------------------------------------------------------------------===//
533 //===----------------------------------------------------------------------===//
535 let isCommutable = 1,
536 Defs = [R1, R0, SREG] in
539 // Multiplies Rd by Rr and places the result into R1:R0.
540 let usesCustomInserter = 1 in {
541 def MULRdRr : FRdRr<0b1001, 0b11,
543 (ins GPR8:$lhs, GPR8:$rhs),
545 [/*(set R1, R0, (smullohi i8:$lhs, i8:$rhs))*/]>,
546 Requires<[SupportsMultiplication]>;
548 def MULSRdRr : FMUL2RdRr<0,
550 (ins GPR8:$lhs, GPR8:$rhs),
553 Requires<[SupportsMultiplication]>;
556 def MULSURdRr : FMUL2RdRr<1,
558 (ins GPR8:$lhs, GPR8:$rhs),
561 Requires<[SupportsMultiplication]>;
563 def FMUL : FFMULRdRr<0b01,
565 (ins GPR8:$lhs, GPR8:$rhs),
568 Requires<[SupportsMultiplication]>;
570 def FMULS : FFMULRdRr<0b10,
572 (ins GPR8:$lhs, GPR8:$rhs),
575 Requires<[SupportsMultiplication]>;
577 def FMULSU : FFMULRdRr<0b11,
579 (ins GPR8:$lhs, GPR8:$rhs),
580 "fmulsu\t$lhs, $rhs",
582 Requires<[SupportsMultiplication]>;
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),
593 //===----------------------------------------------------------------------===//
595 //===----------------------------------------------------------------------===//
596 let Constraints = "$src = $rd",
599 // Register-Register logic instructions (which have the
600 // property of commutativity).
601 let isCommutable = 1 in
603 def ANDRdRr : FRdRr<0b0010,
606 (ins GPR8:$src, GPR8:$rr),
608 [(set i8:$rd, (and i8:$src, i8:$rr)),
611 // ANDW Rd+1:Rd, Rr+1:Rr
616 def ANDWRdRr : Pseudo<(outs DREGS:$rd),
617 (ins DREGS:$src, DREGS:$rr),
619 [(set i16:$rd, (and i16:$src, i16:$rr)),
622 def ORRdRr : FRdRr<0b0010,
625 (ins GPR8:$src, GPR8:$rr),
627 [(set i8:$rd, (or i8:$src, i8:$rr)),
630 // ORW Rd+1:Rd, Rr+1:Rr
635 def ORWRdRr : Pseudo<(outs DREGS:$rd),
636 (ins DREGS:$src, DREGS:$rr),
638 [(set i16:$rd, (or i16:$src, i16:$rr)),
641 def EORRdRr : FRdRr<0b0010,
644 (ins GPR8:$src, GPR8:$rr),
646 [(set i8:$rd, (xor i8:$src, i8:$rr)),
649 // EORW Rd+1:Rd, Rr+1:Rr
654 def EORWRdRr : Pseudo<(outs DREGS:$rd),
655 (ins DREGS:$src, DREGS:$rr),
657 [(set i16:$rd, (xor i16:$src, i16:$rr)),
661 def ANDIRdK : FRdK<0b0111,
663 (ins LD8:$src, imm_ldi8:$k),
665 [(set i8:$rd, (and i8:$src, imm:$k)),
668 // ANDI Rd+1:Rd, K+1:K
673 def ANDIWRdK : Pseudo<(outs DLDREGS:$rd),
674 (ins DLDREGS:$src, i16imm:$k),
676 [(set i16:$rd, (and i16:$src, imm:$k)),
679 def ORIRdK : FRdK<0b0110,
681 (ins LD8:$src, imm_ldi8:$k),
683 [(set i8:$rd, (or i8:$src, imm:$k)),
686 // ORIW Rd+1:Rd, K+1,K
691 def ORIWRdK : Pseudo<(outs DLDREGS:$rd),
692 (ins DLDREGS:$src, i16imm:$rr),
694 [(set i16:$rd, (or i16:$src, imm:$rr)),
698 //===----------------------------------------------------------------------===//
699 // One's/Two's Complement
700 //===----------------------------------------------------------------------===//
701 let Constraints = "$src = $rd",
704 def COMRd : FRd<0b1001,
709 [(set i8:$rd, (not i8:$src)), (implicit SREG)]>;
716 def COMWRd : Pseudo<(outs DREGS:$rd),
719 [(set i16:$rd, (not i16:$src)), (implicit SREG)]>;
721 //:TODO: optimize NEG for wider types
722 def NEGRd : FRd<0b1001,
727 [(set i8:$rd, (ineg i8:$src)), (implicit SREG)]>;
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>;
736 def TSTRd : FTST<0b0010,
743 //===----------------------------------------------------------------------===//
745 //===----------------------------------------------------------------------===//
752 (ins brtarget_13:$target),
756 let isIndirectBranch = 1,
758 def IJMP : F16<0b1001010000001001,
763 Requires<[HasIJMPCALL]>;
765 let isIndirectBranch = 1,
767 def EIJMP : F16<0b1001010000011001,
772 Requires<[HasEIJMPCALL]>;
774 def JMPk : F32BRk<0b110,
776 (ins call_target:$k),
779 Requires<[HasJMPCALL]>;
782 //===----------------------------------------------------------------------===//
784 //===----------------------------------------------------------------------===//
787 // SP is marked as a use to prevent stack-pointer assignments that appear
788 // immediately before calls from potentially appearing dead.
792 (ins brtarget_13:$target),
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,
804 Requires<[HasIJMPCALL]>;
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,
814 Requires<[HasEIJMPCALL]>;
816 // SP is marked as a use to prevent stack-pointer assignments that appear
817 // immediately before calls from potentially appearing dead.
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.
822 def CALLk : F32BRk<0b111,
824 (ins call_target:$k),
827 Requires<[HasJMPCALL]>;
830 //===----------------------------------------------------------------------===//
831 // Return instructions.
832 //===----------------------------------------------------------------------===//
833 let isTerminator = 1,
837 def RET : F16<0b1001010100001000,
843 def RETI : F16<0b1001010100011000,
850 //===----------------------------------------------------------------------===//
851 // Compare operations.
852 //===----------------------------------------------------------------------===//
856 // Compare Rd and Rr, skipping the next instruction if they are equal.
860 def CPSE : FRdRr<0b0001,
863 (ins GPR8:$rd, GPR8:$rr),
867 def CPRdRr : FRdRr<0b0001,
870 (ins GPR8:$rd, GPR8:$rr),
872 [(AVRcmp i8:$rd, i8:$rr), (implicit SREG)]>;
874 // CPW Rd+1:Rd, Rr+1:Rr
879 def CPWRdRr : Pseudo<(outs),
880 (ins DREGS:$src, DREGS:$src2),
882 [(AVRcmp i16:$src, i16:$src2), (implicit SREG)]>;
885 def CPCRdRr : FRdRr<0b0000,
888 (ins GPR8:$rd, GPR8:$rr),
890 [(AVRcmpc i8:$rd, i8:$rr), (implicit SREG)]>;
892 // CPCW Rd+1:Rd. Rr+1:Rr
898 def CPCWRdRr : Pseudo<(outs),
899 (ins DREGS:$src, DREGS:$src2),
901 [(AVRcmpc i16:$src, i16:$src2), (implicit SREG)]>;
904 // Compares a register with an 8 bit immediate.
905 def CPIRdK : FRdK<0b0011,
907 (ins LD8:$rd, imm_ldi8:$k),
909 [(AVRcmp i8:$rd, imm:$k), (implicit SREG)]>;
912 //===----------------------------------------------------------------------===//
913 // Register conditional skipping/branching operations.
914 //===----------------------------------------------------------------------===//
918 // Conditional skipping on GPR register bits, and
919 // conditional skipping on IO register bits.
922 def SBRCRrB : FRdB<0b10,
924 (ins GPR8:$rr, i8imm:$b),
928 def SBRSRrB : FRdB<0b11,
930 (ins GPR8:$rr, i8imm:$b),
934 def SBICAb : FIOBIT<0b01,
936 (ins imm_port5:$a, i8imm:$b),
940 def SBISAb : FIOBIT<0b11,
942 (ins imm_port5:$a, i8imm:$b),
947 // Relative branches on status flag bits.
951 // Branch if `s` flag in status register is set.
954 (ins i8imm:$s, relbrtarget_7:$k),
959 // Branch if `s` flag in status register is clear.
962 (ins i8imm:$s, relbrtarget_7:$k),
970 // Branch if carry flag is set
971 def : InstAlias<"brcs\t$k", (BRBSsk 0, relbrtarget_7:$k)>;
974 // Branch if carry flag is clear
975 def : InstAlias<"brcc\t$k", (BRBCsk 0, relbrtarget_7:$k)>;
978 // Branch if half carry flag is set
979 def : InstAlias<"brhs\t$k", (BRBSsk 5, relbrtarget_7:$k)>;
982 // Branch if half carry flag is clear
983 def : InstAlias<"brhc\t$k", (BRBCsk 5, relbrtarget_7:$k)>;
986 // Branch if the T flag is set
987 def : InstAlias<"brts\t$k", (BRBSsk 6, relbrtarget_7:$k)>;
990 // Branch if the T flag is clear
991 def : InstAlias<"brtc\t$k", (BRBCsk 6, relbrtarget_7:$k)>;
994 // Branch if the overflow flag is set
995 def : InstAlias<"brvs\t$k", (BRBSsk 3, relbrtarget_7:$k)>;
998 // Branch if the overflow flag is clear
999 def : InstAlias<"brvc\t$k", (BRBCsk 3, relbrtarget_7:$k)>;
1002 // Branch if the global interrupt flag is enabled
1003 def : InstAlias<"brie\t$k", (BRBSsk 7, relbrtarget_7:$k)>;
1006 // Branch if the global interrupt flag is disabled
1007 def : InstAlias<"brid\t$k", (BRBCsk 7, relbrtarget_7:$k)>;
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.
1018 def BREQk : FBRsk<0,
1021 (ins relbrtarget_7:$target),
1023 [(AVRbrcond bb:$target, AVR_COND_EQ)]>;
1025 def BRNEk : FBRsk<1,
1028 (ins relbrtarget_7:$target),
1030 [(AVRbrcond bb:$target, AVR_COND_NE)]>;
1033 def BRSHk : FBRsk<1,
1036 (ins relbrtarget_7:$target),
1038 [(AVRbrcond bb:$target, AVR_COND_SH)]>;
1040 def BRLOk : FBRsk<0,
1043 (ins relbrtarget_7:$target),
1045 [(AVRbrcond bb:$target, AVR_COND_LO)]>;
1047 def BRMIk : FBRsk<0,
1050 (ins relbrtarget_7:$target),
1052 [(AVRbrcond bb:$target, AVR_COND_MI)]>;
1054 def BRPLk : FBRsk<1,
1057 (ins relbrtarget_7:$target),
1059 [(AVRbrcond bb:$target, AVR_COND_PL)]>;
1061 def BRGEk : FBRsk<1,
1064 (ins relbrtarget_7:$target),
1066 [(AVRbrcond bb:$target, AVR_COND_GE)]>;
1068 def BRLTk : FBRsk<0,
1071 (ins relbrtarget_7:$target),
1073 [(AVRbrcond bb:$target, AVR_COND_LT)]>;
1076 //===----------------------------------------------------------------------===//
1077 // Data transfer instructions
1078 //===----------------------------------------------------------------------===//
1079 // 8 and 16-bit register move instructions.
1080 let hasSideEffects = 0 in
1082 def MOVRdRr : FRdRr<0b0010,
1089 def MOVWRdRr : FMOVWRdRr<(outs DREGS:$dst),
1093 Requires<[HasMOVW]>;
1096 // Load immediate values into registers.
1097 let isReMaterializable = 1 in
1099 def LDIRdK : FRdK<0b1110,
1103 [(set i8:$rd, imm:$k)]>;
1105 // LDIW Rd+1:Rd, K+1:K
1110 def LDIWRdK : Pseudo<(outs DLDREGS:$dst),
1113 [(set i16:$dst, imm:$src)]>;
1116 // Load from data space into register.
1117 let canFoldAsLoad = 1,
1118 isReMaterializable = 1 in
1120 def LDSRdK : F32DM<0b0,
1124 [(set i8:$rd, (load imm:$k))]>,
1125 Requires<[HasSRAM]>;
1127 // LDSW Rd+1:Rd, K+1:K
1131 // lds Rd+1 (K+1:K) + 1
1132 def LDSWRdK : Pseudo<(outs DREGS:$dst),
1135 [(set i16:$dst, (load imm:$src))]>,
1136 Requires<[HasSRAM]>;
1140 let canFoldAsLoad = 1,
1141 isReMaterializable = 1 in
1143 def LDRdPtr : FSTLD<0,
1146 (ins LDSTPtrReg:$ptrreg),
1147 "ld\t$reg, $ptrreg",
1148 [(set GPR8:$reg, (load i16:$ptrreg))]>,
1149 Requires<[HasSRAM]>;
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]>;
1164 // Indirect loads (with postincrement or predecrement).
1167 Constraints = "$ptrreg = $base_wb,@earlyclobber $reg" in
1169 def LDRdPtrPi : FSTLD<0,
1171 (outs GPR8:$reg, PTRREGS:$base_wb),
1172 (ins LDSTPtrReg:$ptrreg),
1173 "ld\t$reg, $ptrreg+",
1175 Requires<[HasSRAM]>;
1181 def LDWRdPtrPi : Pseudo<(outs DREGS:$reg, PTRREGS:$base_wb),
1182 (ins PTRREGS:$ptrreg),
1183 "ldw\t$reg, $ptrreg+",
1185 Requires<[HasSRAM]>;
1187 def LDRdPtrPd : FSTLD<0,
1189 (outs GPR8:$reg, PTRREGS:$base_wb),
1190 (ins LDSTPtrReg:$ptrreg),
1191 "ld\t$reg, -$ptrreg",
1193 Requires<[HasSRAM]>;
1200 def LDWRdPtrPd : Pseudo<(outs DREGS:$reg, PTRREGS:$base_wb),
1201 (ins PTRREGS:$ptrreg),
1202 "ldw\t$reg, -$ptrreg",
1204 Requires<[HasSRAM]>;
1207 // Load indirect with displacement operations.
1208 let canFoldAsLoad = 1,
1209 isReMaterializable = 1 in
1211 let Constraints = "@earlyclobber $reg" in
1212 def LDDRdPtrQ : FSTDLDD<0,
1215 "ldd\t$reg, $memri",
1216 [(set i8:$reg, (load addr:$memri))]>,
1217 Requires<[HasSRAM]>;
1219 // LDDW Rd+1:Rd, P+q
1224 let Constraints = "@earlyclobber $dst" in
1225 def LDDWRdPtrQ : Pseudo<(outs DREGS:$dst),
1227 "lddw\t$dst, $memri",
1228 [(set i16:$dst, (load addr:$memri))]>,
1229 Requires<[HasSRAM]>;
1231 // An identical pseudo instruction to LDDWRdPtrQ, expect restricted to the Y
1232 // register and without the @earlyclobber flag.
1234 // Used to work around a bug caused by the register allocator not
1235 // being able to handle the expansion of a COPY into an machine instruction
1236 // that has an earlyclobber flag. This is because the register allocator will
1237 // try expand a copy from a register slot into an earlyclobber instruction.
1238 // Instructions that are earlyclobber need to be in a dedicated earlyclobber slot.
1240 // This pseudo instruction can be used pre-AVR pseudo expansion in order to
1241 // get a frame index load without directly using earlyclobber instructions.
1243 // The pseudo expansion pass trivially expands this into LDDWRdPtrQ.
1245 // This instruction may be removed once PR13375 is fixed.
1247 hasSideEffects = 0 in
1248 def LDDWRdYQ : Pseudo<(outs DREGS:$dst),
1250 "lddw\t$dst, $memri",
1252 Requires<[HasSRAM]>;
1255 class AtomicLoad<PatFrag Op, RegisterClass DRC,
1256 RegisterClass PTRRC> :
1257 Pseudo<(outs DRC:$rd), (ins PTRRC:$rr), "atomic_op",
1258 [(set DRC:$rd, (Op i16:$rr))]>;
1260 class AtomicStore<PatFrag Op, RegisterClass DRC,
1261 RegisterClass PTRRC> :
1262 Pseudo<(outs), (ins PTRRC:$rd, DRC:$rr), "atomic_op",
1263 [(Op i16:$rd, DRC:$rr)]>;
1265 class AtomicLoadOp<PatFrag Op, RegisterClass DRC,
1266 RegisterClass PTRRC> :
1267 Pseudo<(outs DRC:$rd), (ins PTRRC:$rr, DRC:$operand),
1269 [(set DRC:$rd, (Op i16:$rr, DRC:$operand))]>;
1271 // FIXME: I think 16-bit atomic binary ops need to mark
1274 // Atomic instructions
1275 // ===================
1277 // These are all expanded by AVRExpandPseudoInsts
1279 // 8-bit operations can use any pointer register because
1280 // they are expanded directly into an LD/ST instruction.
1282 // 16-bit operations use 16-bit load/store postincrement instructions,
1283 // which require PTRDISPREGS.
1285 def AtomicLoad8 : AtomicLoad<atomic_load_8, GPR8, PTRREGS>;
1286 def AtomicLoad16 : AtomicLoad<atomic_load_16, DREGS, PTRDISPREGS>;
1288 def AtomicStore8 : AtomicStore<atomic_store_8, GPR8, PTRREGS>;
1289 def AtomicStore16 : AtomicStore<atomic_store_16, DREGS, PTRDISPREGS>;
1291 class AtomicLoadOp8<PatFrag Op> : AtomicLoadOp<Op, GPR8, PTRREGS>;
1292 class AtomicLoadOp16<PatFrag Op> : AtomicLoadOp<Op, DREGS, PTRDISPREGS>;
1294 def AtomicLoadAdd8 : AtomicLoadOp8<atomic_load_add_8>;
1295 def AtomicLoadAdd16 : AtomicLoadOp16<atomic_load_add_16>;
1296 def AtomicLoadSub8 : AtomicLoadOp8<atomic_load_sub_8>;
1297 def AtomicLoadSub16 : AtomicLoadOp16<atomic_load_sub_16>;
1298 def AtomicLoadAnd8 : AtomicLoadOp8<atomic_load_and_8>;
1299 def AtomicLoadAnd16 : AtomicLoadOp16<atomic_load_and_16>;
1300 def AtomicLoadOr8 : AtomicLoadOp8<atomic_load_or_8>;
1301 def AtomicLoadOr16 : AtomicLoadOp16<atomic_load_or_16>;
1302 def AtomicLoadXor8 : AtomicLoadOp8<atomic_load_xor_8>;
1303 def AtomicLoadXor16 : AtomicLoadOp16<atomic_load_xor_16>;
1304 def AtomicFence : Pseudo<(outs), (ins), "atomic_fence",
1305 [(atomic_fence imm, imm)]>;
1307 // Indirect store from register to data space.
1308 def STSKRr : F32DM<0b1,
1310 (ins imm16:$k, GPR8:$rd),
1312 [(store i8:$rd, imm:$k)]>,
1313 Requires<[HasSRAM]>;
1315 // STSW K+1:K, Rr+1:Rr
1318 // sts Rr+1, (K+1:K) + 1
1320 def STSWKRr : Pseudo<(outs),
1321 (ins i16imm:$dst, DREGS:$src),
1323 [(store i16:$src, imm:$dst)]>,
1324 Requires<[HasSRAM]>;
1328 // Stores the value of Rr into the location addressed by pointer P.
1329 def STPtrRr : FSTLD<1,
1332 (ins LDSTPtrReg:$ptrreg, GPR8:$reg),
1333 "st\t$ptrreg, $reg",
1334 [(store GPR8:$reg, i16:$ptrreg)]>,
1335 Requires<[HasSRAM]>;
1338 // Stores the value of Rr into the location addressed by pointer P.
1343 def STWPtrRr : Pseudo<(outs),
1344 (ins PTRDISPREGS:$ptrreg, DREGS:$reg),
1345 "stw\t$ptrreg, $reg",
1346 [(store i16:$reg, i16:$ptrreg)]>,
1347 Requires<[HasSRAM]>;
1349 // Indirect stores (with postincrement or predecrement).
1350 let Constraints = "$ptrreg = $base_wb,@earlyclobber $base_wb" in
1354 // Stores the value of Rr into the location addressed by pointer P.
1355 // Post increments P.
1356 def STPtrPiRr : FSTLD<1,
1358 (outs LDSTPtrReg:$base_wb),
1359 (ins LDSTPtrReg:$ptrreg, GPR8:$reg, i8imm:$offs),
1360 "st\t$ptrreg+, $reg",
1362 (post_store GPR8:$reg, i16:$ptrreg, imm:$offs))]>,
1363 Requires<[HasSRAM]>;
1366 // Stores the value of Rr into the location addressed by pointer P.
1367 // Post increments P.
1372 def STWPtrPiRr : Pseudo<(outs PTRREGS:$base_wb),
1373 (ins PTRREGS:$ptrreg, DREGS:$trh, i8imm:$offs),
1374 "stw\t$ptrreg+, $trh",
1375 [(set PTRREGS:$base_wb,
1376 (post_store DREGS:$trh, PTRREGS:$ptrreg, imm:$offs))]>,
1377 Requires<[HasSRAM]>;
1380 // Stores the value of Rr into the location addressed by pointer P.
1381 // Pre decrements P.
1382 def STPtrPdRr : FSTLD<1,
1384 (outs LDSTPtrReg:$base_wb),
1385 (ins LDSTPtrReg:$ptrreg, GPR8:$reg, i8imm:$offs),
1386 "st\t-$ptrreg, $reg",
1388 (pre_store GPR8:$reg, i16:$ptrreg, imm:$offs))]>,
1389 Requires<[HasSRAM]>;
1392 // Stores the value of Rr into the location addressed by pointer P.
1393 // Pre decrements P.
1398 def STWPtrPdRr : Pseudo<(outs PTRREGS:$base_wb),
1399 (ins PTRREGS:$ptrreg, DREGS:$reg, i8imm:$offs),
1400 "stw\t-$ptrreg, $reg",
1401 [(set PTRREGS:$base_wb,
1402 (pre_store i16:$reg, i16:$ptrreg, imm:$offs))]>,
1403 Requires<[HasSRAM]>;
1406 // Store indirect with displacement operations.
1408 // Stores the value of Rr into the location addressed by pointer P with a
1409 // displacement of q. Does not modify P.
1410 def STDPtrQRr : FSTDLDD<1,
1412 (ins memri:$memri, GPR8:$reg),
1413 "std\t$memri, $reg",
1414 [(store i8:$reg, addr:$memri)]>,
1415 Requires<[HasSRAM]>;
1417 // STDW P+q, Rr+1:Rr
1418 // Stores the value of Rr into the location addressed by pointer P with a
1419 // displacement of q. Does not modify P.
1424 def STDWPtrQRr : Pseudo<(outs),
1425 (ins memri:$memri, DREGS:$src),
1426 "stdw\t$memri, $src",
1427 [(store i16:$src, addr:$memri)]>,
1428 Requires<[HasSRAM]>;
1431 // Load program memory operations.
1432 let canFoldAsLoad = 1,
1433 isReMaterializable = 1,
1435 hasSideEffects = 0 in
1439 def LPM : F16<0b1001010111001000,
1446 def LPMRdZ : FLPMX<0,
1452 Requires<[HasLPMX]>;
1454 // Load program memory, while postincrementing the Z register.
1455 let Defs = [R31R30] in
1457 def LPMRdZPi : FLPMX<0,
1463 Requires<[HasLPMX]>;
1465 def LPMWRdZ : Pseudo<(outs DREGS:$dst),
1469 Requires<[HasLPMX]>;
1471 def LPMWRdZPi : Pseudo<(outs DREGS:$dst),
1475 Requires<[HasLPMX]>;
1479 // Extended load program memory operations.
1481 hasSideEffects = 0 in
1485 def ELPM : F16<0b1001010111011000,
1490 Requires<[HasELPM]>;
1492 def ELPMRdZ : FLPMX<1,
1498 Requires<[HasELPMX]>;
1500 let Defs = [R31R30] in
1501 def ELPMRdZPi : FLPMX<1,
1507 Requires<[HasELPMX]>;
1510 // Store program memory operations.
1511 let Uses = [R1, R0] in
1513 let Uses = [R31R30, R1, R0] in
1514 def SPM : F16<0b1001010111101000,
1521 let Defs = [R31R30] in
1522 def SPMZPi : F16<0b1001010111111000,
1527 Requires<[HasSPMX]>;
1530 // Read data from IO location operations.
1531 let canFoldAsLoad = 1,
1532 isReMaterializable = 1 in
1534 def INRdA : FIORdA<(outs GPR8:$dst),
1535 (ins imm_port6:$src),
1537 [(set i8:$dst, (load ioaddr8:$src))]>;
1539 def INWRdA : Pseudo<(outs DREGS:$dst),
1540 (ins imm_port6:$src),
1542 [(set i16:$dst, (load ioaddr16:$src))]>;
1545 // Write data to IO location operations.
1546 def OUTARr : FIOARr<(outs),
1547 (ins imm_port6:$dst, GPR8:$src),
1549 [(store i8:$src, ioaddr8:$dst)]>;
1551 def OUTWARr : Pseudo<(outs),
1552 (ins imm_port6:$dst, DREGS:$src),
1554 [(store i16:$src, ioaddr16:$dst)]>;
1556 // Stack push/pop operations.
1559 hasSideEffects = 0 in
1561 // Stack push operations.
1564 def PUSHRr : FRd<0b1001,
1570 Requires<[HasSRAM]>;
1572 def PUSHWRr : Pseudo<(outs),
1576 Requires<[HasSRAM]>;
1579 // Stack pop operations.
1582 def POPRd : FRd<0b1001,
1588 Requires<[HasSRAM]>;
1590 def POPWRd : Pseudo<(outs DREGS:$reg),
1594 Requires<[HasSRAM]>;
1598 // Read-Write-Modify (RMW) instructions.
1599 def XCHZRd : FZRd<0b100,
1604 Requires<[SupportsRMW]>;
1606 def LASZRd : FZRd<0b101,
1611 Requires<[SupportsRMW]>;
1613 def LACZRd : FZRd<0b110,
1618 Requires<[SupportsRMW]>;
1620 def LATZRd : FZRd<0b111,
1625 Requires<[SupportsRMW]>;
1627 //===----------------------------------------------------------------------===//
1628 // Bit and bit-test instructions
1629 //===----------------------------------------------------------------------===//
1631 // Bit shift/rotate operations.
1632 let Constraints = "$src = $rd",
1635 def LSLRd : FRdRr<0b0000,
1640 [(set i8:$rd, (AVRlsl i8:$src)), (implicit SREG)]>;
1642 def LSLWRd : Pseudo<(outs DREGS:$rd),
1645 [(set i16:$rd, (AVRlsl i16:$src)), (implicit SREG)]>;
1647 def LSRRd : FRd<0b1001,
1652 [(set i8:$rd, (AVRlsr i8:$src)), (implicit SREG)]>;
1654 def LSRWRd : Pseudo<(outs DREGS:$rd),
1657 [(set i16:$rd, (AVRlsr i16:$src)), (implicit SREG)]>;
1659 def ASRRd : FRd<0b1001,
1664 [(set i8:$rd, (AVRasr i8:$src)), (implicit SREG)]>;
1666 def ASRWRd : Pseudo<(outs DREGS:$rd),
1669 [(set i16:$rd, (AVRasr i16:$src)), (implicit SREG)]>;
1671 // Bit rotate operations.
1672 let Uses = [SREG] in
1674 def ROLRd : FRdRr<0b0001,
1679 [(set i8:$rd, (AVRrol i8:$src)), (implicit SREG)]>;
1681 def ROLWRd : Pseudo<(outs DREGS:$rd),
1684 [(set i16:$rd, (AVRrol i16:$src)), (implicit SREG)]>;
1686 def RORRd : FRd<0b1001,
1691 [(set i8:$rd, (AVRror i8:$src)), (implicit SREG)]>;
1693 def RORWRd : Pseudo<(outs DREGS:$rd),
1696 [(set i16:$rd, (AVRror i16:$src)), (implicit SREG)]>;
1701 // Swaps the high and low nibbles in a register.
1702 let Constraints = "$src = $rd" in
1703 def SWAPRd : FRd<0b1001,
1708 [(set i8:$rd, (bswap i8:$src))]>;
1710 // IO register bit set/clear operations.
1711 //:TODO: add patterns when popcount(imm)==2 to be expanded with 2 sbi/cbi
1712 // instead of in+ori+out which requires one more instr.
1713 def SBIAb : FIOBIT<0b10,
1715 (ins imm_port5:$addr, i8imm:$bit),
1717 [(store (or (i8 (load lowioaddr8:$addr)), iobitpos8:$bit),
1718 lowioaddr8:$addr)]>;
1720 def CBIAb : FIOBIT<0b00,
1722 (ins imm_port5:$addr, i8imm:$bit),
1724 [(store (and (i8 (load lowioaddr8:$addr)), iobitposn8:$bit),
1725 lowioaddr8:$addr)]>;
1727 // Status register bit load/store operations.
1728 let Defs = [SREG] in
1729 def BST : FRdB<0b01,
1731 (ins GPR8:$rd, i8imm:$b),
1735 let Uses = [SREG] in
1736 def BLD : FRdB<0b00,
1738 (ins GPR8:$rd, i8imm:$b),
1742 // Set/clear bit in register operations.
1743 let Constraints = "$src = $rd",
1747 // Alias for ORI Rd, K
1748 def SBRRdK : FRdK<0b0110,
1750 (ins LD8:$src, imm_ldi8:$k),
1752 [(set i8:$rd, (or i8:$src, imm:$k)),
1756 // Alias for `ANDI Rd, COM(K)` where COM(K) is the complement of K.
1757 // FIXME: This uses the 'complement' encoder. We need it to also use the
1758 // imm_ldi8 encoder. This will cause no fixups to be created on this instruction.
1759 def CBRRdK : FRdK<0b0111,
1761 (ins LD8:$src, imm_com8:$k),
1767 // Alias for EOR Rd, Rd
1769 // Clears all bits in a register.
1770 def CLR : InstAlias<"clr\t$rd", (EORRdRr GPR8:$rd, GPR8:$rd)>;
1773 // Alias for LDI Rd, 0xff
1775 // Sets all bits in a register.
1776 def : InstAlias<"ser\t$rd", (LDIRdK LD8:$rd, 0xff), 0>;
1778 let Defs = [SREG] in
1785 let Defs = [SREG] in
1792 // Set/clear aliases for the carry (C) status flag (bit 0).
1793 def : InstAlias<"sec", (BSETs 0)>;
1794 def : InstAlias<"clc", (BCLRs 0)>;
1796 // Set/clear aliases for the zero (Z) status flag (bit 1).
1797 def : InstAlias<"sez", (BSETs 1)>;
1798 def : InstAlias<"clz", (BCLRs 1)>;
1800 // Set/clear aliases for the negative (N) status flag (bit 2).
1801 def : InstAlias<"sen", (BSETs 2)>;
1802 def : InstAlias<"cln", (BCLRs 2)>;
1804 // Set/clear aliases for the overflow (V) status flag (bit 3).
1805 def : InstAlias<"sev", (BSETs 3)>;
1806 def : InstAlias<"clv", (BCLRs 3)>;
1808 // Set/clear aliases for the signed (S) status flag (bit 4).
1809 def : InstAlias<"ses", (BSETs 4)>;
1810 def : InstAlias<"cls", (BCLRs 4)>;
1812 // Set/clear aliases for the half-carry (H) status flag (bit 5).
1813 def : InstAlias<"seh", (BSETs 5)>;
1814 def : InstAlias<"clh", (BCLRs 5)>;
1816 // Set/clear aliases for the T status flag (bit 6).
1817 def : InstAlias<"set", (BSETs 6)>;
1818 def : InstAlias<"clt", (BCLRs 6)>;
1820 // Set/clear aliases for the interrupt (I) status flag (bit 7).
1821 def : InstAlias<"sei", (BSETs 7)>;
1822 def : InstAlias<"cli", (BCLRs 7)>;
1824 //===----------------------------------------------------------------------===//
1825 // Special/Control instructions
1826 //===----------------------------------------------------------------------===//
1829 // Breakpoint instruction
1831 // <|1001|0101|1001|1000>
1832 def BREAK : F16<0b1001010110011000,
1837 Requires<[HasBREAK]>;
1840 // No-operation instruction
1842 // <|0000|0000|0000|0000>
1843 def NOP : F16<0b0000000000000000,
1850 // Sleep instruction
1852 // <|1001|0101|1000|1000>
1853 def SLEEP : F16<0b1001010110001000,
1862 // <|1001|0101|1010|1000>
1863 def WDR : F16<0b1001010110101000,
1869 //===----------------------------------------------------------------------===//
1870 // Pseudo instructions for later expansion
1871 //===----------------------------------------------------------------------===//
1873 //:TODO: Optimize this for wider types AND optimize the following code
1874 // compile int foo(char a, char b, char c, char d) {return d+b;}
1875 // looks like a missed sext_inreg opportunity.
1876 def SEXT : ExtensionPseudo<
1880 [(set i16:$dst, (sext i8:$src)), (implicit SREG)]
1883 def ZEXT : ExtensionPseudo<
1887 [(set i16:$dst, (zext i8:$src)), (implicit SREG)]
1890 // This pseudo gets expanded into a movw+adiw thus it clobbers SREG.
1892 hasSideEffects = 0 in
1893 def FRMIDX : Pseudo<(outs DLDREGS:$dst),
1894 (ins DLDREGS:$src, i16imm:$src2),
1895 "frmidx\t$dst, $src, $src2",
1898 // This pseudo is either converted to a regular store or a push which clobbers
1900 def STDSPQRr : StorePseudo<
1902 (ins memspi:$dst, GPR8:$src),
1903 "stdstk\t$dst, $src",
1904 [(store i8:$src, addr:$dst)]
1907 // This pseudo is either converted to a regular store or a push which clobbers
1909 def STDWSPQRr : StorePseudo<
1911 (ins memspi:$dst, DREGS:$src),
1912 "stdwstk\t$dst, $src",
1913 [(store i16:$src, addr:$dst)]
1916 // SP read/write pseudos.
1917 let hasSideEffects = 0 in
1920 def SPREAD : Pseudo<
1923 "spread\t$dst, $src",
1928 def SPWRITE : Pseudo<
1931 "spwrite\t$dst, $src",
1935 def Select8 : SelectPseudo<
1937 (ins GPR8:$src, GPR8:$src2, i8imm:$cc),
1939 [(set i8:$dst, (AVRselectcc i8:$src, i8:$src2, imm:$cc))]
1942 def Select16 : SelectPseudo<
1944 (ins DREGS:$src, DREGS:$src2, i8imm:$cc),
1945 "# Select16 PSEUDO",
1946 [(set i16:$dst, (AVRselectcc i16:$src, i16:$src2, imm:$cc))]
1949 def Lsl8 : ShiftPseudo<
1951 (ins GPR8:$src, GPR8:$cnt),
1953 [(set i8:$dst, (AVRlslLoop i8:$src, i8:$cnt))]
1956 def Lsl16 : ShiftPseudo<
1958 (ins DREGS:$src, GPR8:$cnt),
1960 [(set i16:$dst, (AVRlslLoop i16:$src, i8:$cnt))]
1963 def Lsr8 : ShiftPseudo<
1965 (ins GPR8:$src, GPR8:$cnt),
1967 [(set i8:$dst, (AVRlsrLoop i8:$src, i8:$cnt))]
1970 def Lsr16 : ShiftPseudo<
1972 (ins DREGS:$src, GPR8:$cnt),
1974 [(set i16:$dst, (AVRlsrLoop i16:$src, i8:$cnt))]
1977 def Rol8 : ShiftPseudo<
1979 (ins GPR8:$src, GPR8:$cnt),
1981 [(set i8:$dst, (AVRrolLoop i8:$src, i8:$cnt))]
1984 def Rol16 : ShiftPseudo<
1986 (ins DREGS:$src, GPR8:$cnt),
1988 [(set i16:$dst, (AVRrolLoop i16:$src, i8:$cnt))]
1991 def Ror8 : ShiftPseudo<
1993 (ins GPR8:$src, GPR8:$cnt),
1995 [(set i8:$dst, (AVRrorLoop i8:$src, i8:$cnt))]
1998 def Ror16 : ShiftPseudo<
2000 (ins DREGS:$src, GPR8:$cnt),
2002 [(set i16:$dst, (AVRrorLoop i16:$src, i8:$cnt))]
2005 def Asr8 : ShiftPseudo<
2007 (ins GPR8:$src, GPR8:$cnt),
2009 [(set i8:$dst, (AVRasrLoop i8:$src, i8:$cnt))]
2012 def Asr16 : ShiftPseudo<
2014 (ins DREGS:$src, GPR8:$cnt),
2016 [(set i16:$dst, (AVRasrLoop i16:$src, i8:$cnt))]
2020 //===----------------------------------------------------------------------===//
2021 // Non-Instruction Patterns
2022 //===----------------------------------------------------------------------===//
2024 //:TODO: look in x86InstrCompiler.td for odd encoding trick related to
2025 // add x, 128 -> sub x, -128. Clang is emitting an eor for this (ldi+eor)
2027 // the add instruction always writes the carry flag
2028 def : Pat<(addc i8:$src, i8:$src2),
2029 (ADDRdRr i8:$src, i8:$src2)>;
2030 def : Pat<(addc DREGS:$src, DREGS:$src2),
2031 (ADDWRdRr DREGS:$src, DREGS:$src2)>;
2033 // all sub instruction variants always writes the carry flag
2034 def : Pat<(subc i8:$src, i8:$src2),
2035 (SUBRdRr i8:$src, i8:$src2)>;
2036 def : Pat<(subc i16:$src, i16:$src2),
2037 (SUBWRdRr i16:$src, i16:$src2)>;
2038 def : Pat<(subc i8:$src, imm:$src2),
2039 (SUBIRdK i8:$src, imm:$src2)>;
2040 def : Pat<(subc i16:$src, imm:$src2),
2041 (SUBIWRdK i16:$src, imm:$src2)>;
2043 // These patterns convert add (x, -imm) to sub (x, imm) since we dont have
2044 // any add with imm instructions. Also take care of the adiw/sbiw instructions.
2045 def : Pat<(add i16:$src1, imm0_63_neg:$src2),
2046 (SBIWRdK i16:$src1, (imm0_63_neg:$src2))>;
2047 def : Pat<(add i16:$src1, imm:$src2),
2048 (SUBIWRdK i16:$src1, (imm16_neg_XFORM imm:$src2))>;
2049 def : Pat<(addc i16:$src1, imm:$src2),
2050 (SUBIWRdK i16:$src1, (imm16_neg_XFORM imm:$src2))>;
2051 def : Pat<(adde i16:$src1, imm:$src2),
2052 (SBCIWRdK i16:$src1, (imm16_neg_XFORM imm:$src2))>;
2054 def : Pat<(add i8:$src1, imm:$src2),
2055 (SUBIRdK i8:$src1, (imm8_neg_XFORM imm:$src2))>;
2056 def : Pat<(addc i8:$src1, imm:$src2),
2057 (SUBIRdK i8:$src1, (imm8_neg_XFORM imm:$src2))>;
2058 def : Pat<(adde i8:$src1, imm:$src2),
2059 (SBCIRdK i8:$src1, (imm8_neg_XFORM imm:$src2))>;
2062 def : Pat<(AVRcall (i16 tglobaladdr:$dst)),
2063 (CALLk tglobaladdr:$dst)>;
2064 def : Pat<(AVRcall (i16 texternalsym:$dst)),
2065 (CALLk texternalsym:$dst)>;
2068 def : Pat<(i16 (anyext i8:$src)),
2069 (INSERT_SUBREG (i16 (IMPLICIT_DEF)), i8:$src, sub_lo)>;
2072 def : Pat<(i8 (trunc i16:$src)),
2073 (EXTRACT_SUBREG i16:$src, sub_lo)>;
2076 def : Pat<(sext_inreg i16:$src, i8),
2077 (SEXT (i8 (EXTRACT_SUBREG i16:$src, sub_lo)))>;
2080 def : Pat<(i16 (AVRWrapper tglobaladdr:$dst)),
2081 (LDIWRdK tglobaladdr:$dst)>;
2082 def : Pat<(add i16:$src, (AVRWrapper tglobaladdr:$src2)),
2083 (SUBIWRdK i16:$src, tglobaladdr:$src2)>;
2084 def : Pat<(i8 (load (AVRWrapper tglobaladdr:$dst))),
2085 (LDSRdK tglobaladdr:$dst)>;
2086 def : Pat<(i16 (load (AVRWrapper tglobaladdr:$dst))),
2087 (LDSWRdK tglobaladdr:$dst)>;
2088 def : Pat<(store i8:$src, (i16 (AVRWrapper tglobaladdr:$dst))),
2089 (STSKRr tglobaladdr:$dst, i8:$src)>;
2090 def : Pat<(store i16:$src, (i16 (AVRWrapper tglobaladdr:$dst))),
2091 (STSWKRr tglobaladdr:$dst, i16:$src)>;
2094 def : Pat<(i16 (AVRWrapper tblockaddress:$dst)),
2095 (LDIWRdK tblockaddress:$dst)>;
2097 // hi-reg truncation : trunc(int16 >> 8)
2098 //:FIXME: i think it's better to emit an extract subreg node in the DAG than
2099 // all this mess once we get optimal shift code
2100 // lol... I think so, too. [@agnat]
2101 def : Pat<(i8 (trunc (AVRlsr (AVRlsr (AVRlsr (AVRlsr (AVRlsr (AVRlsr (AVRlsr
2102 (AVRlsr DREGS:$src)))))))))),
2103 (EXTRACT_SUBREG DREGS:$src, sub_hi)>;
2105 // :FIXME: DAGCombiner produces an shl node after legalization from these seq:
2106 // BR_JT -> (mul x, 2) -> (shl x, 1)
2107 def : Pat<(shl i16:$src1, (i8 1)),
2108 (LSLWRd i16:$src1)>;