1 //===-- AVRISelLowering.cpp - AVR DAG Lowering Implementation -------------===//
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 defines the interfaces that AVR uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #include "AVRISelLowering.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/CodeGen/CallingConvLower.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/Support/ErrorHandling.h"
28 #include "AVRMachineFunctionInfo.h"
29 #include "AVRTargetMachine.h"
30 #include "MCTargetDesc/AVRMCTargetDesc.h"
34 AVRTargetLowering::AVRTargetLowering(AVRTargetMachine &tm)
35 : TargetLowering(tm) {
36 // Set up the register classes.
37 addRegisterClass(MVT::i8, &AVR::GPR8RegClass);
38 addRegisterClass(MVT::i16, &AVR::DREGSRegClass);
40 // Compute derived properties from the register classes.
41 computeRegisterProperties(tm.getSubtargetImpl()->getRegisterInfo());
43 setBooleanContents(ZeroOrOneBooleanContent);
44 setBooleanVectorContents(ZeroOrOneBooleanContent);
45 setSchedulingPreference(Sched::RegPressure);
46 setStackPointerRegisterToSaveRestore(AVR::SP);
47 setSupportsUnalignedAtomics(true);
49 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
50 setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
52 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
53 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
54 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
55 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
57 for (MVT VT : MVT::integer_valuetypes()) {
58 for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) {
59 setLoadExtAction(N, VT, MVT::i1, Promote);
60 setLoadExtAction(N, VT, MVT::i8, Expand);
64 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
66 // sub (x, imm) gets canonicalized to add (x, -imm), so for illegal types
67 // revert into a sub since we don't have an add with immediate instruction.
68 setOperationAction(ISD::ADD, MVT::i32, Custom);
69 setOperationAction(ISD::ADD, MVT::i64, Custom);
71 // our shift instructions are only able to shift 1 bit at a time, so handle
72 // this in a custom way.
73 setOperationAction(ISD::SRA, MVT::i8, Custom);
74 setOperationAction(ISD::SHL, MVT::i8, Custom);
75 setOperationAction(ISD::SRL, MVT::i8, Custom);
76 setOperationAction(ISD::SRA, MVT::i16, Custom);
77 setOperationAction(ISD::SHL, MVT::i16, Custom);
78 setOperationAction(ISD::SRL, MVT::i16, Custom);
79 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
80 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
81 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
83 setOperationAction(ISD::ROTL, MVT::i8, Custom);
84 setOperationAction(ISD::ROTL, MVT::i16, Custom);
85 setOperationAction(ISD::ROTR, MVT::i8, Custom);
86 setOperationAction(ISD::ROTR, MVT::i16, Custom);
88 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
89 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
90 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
91 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
92 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
94 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
95 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
96 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
97 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
98 setOperationAction(ISD::SETCC, MVT::i8, Custom);
99 setOperationAction(ISD::SETCC, MVT::i16, Custom);
100 setOperationAction(ISD::SETCC, MVT::i32, Custom);
101 setOperationAction(ISD::SETCC, MVT::i64, Custom);
102 setOperationAction(ISD::SELECT, MVT::i8, Expand);
103 setOperationAction(ISD::SELECT, MVT::i16, Expand);
105 setOperationAction(ISD::BSWAP, MVT::i16, Expand);
107 // Add support for postincrement and predecrement load/stores.
108 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
109 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
110 setIndexedLoadAction(ISD::PRE_DEC, MVT::i8, Legal);
111 setIndexedLoadAction(ISD::PRE_DEC, MVT::i16, Legal);
112 setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal);
113 setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal);
114 setIndexedStoreAction(ISD::PRE_DEC, MVT::i8, Legal);
115 setIndexedStoreAction(ISD::PRE_DEC, MVT::i16, Legal);
117 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
119 setOperationAction(ISD::VASTART, MVT::Other, Custom);
120 setOperationAction(ISD::VAEND, MVT::Other, Expand);
121 setOperationAction(ISD::VAARG, MVT::Other, Expand);
122 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
124 // Atomic operations which must be lowered to rtlib calls
125 for (MVT VT : MVT::integer_valuetypes()) {
126 setOperationAction(ISD::ATOMIC_SWAP, VT, Expand);
127 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Expand);
128 setOperationAction(ISD::ATOMIC_LOAD_NAND, VT, Expand);
129 setOperationAction(ISD::ATOMIC_LOAD_MAX, VT, Expand);
130 setOperationAction(ISD::ATOMIC_LOAD_MIN, VT, Expand);
131 setOperationAction(ISD::ATOMIC_LOAD_UMAX, VT, Expand);
132 setOperationAction(ISD::ATOMIC_LOAD_UMIN, VT, Expand);
135 // Division/remainder
136 setOperationAction(ISD::UDIV, MVT::i8, Expand);
137 setOperationAction(ISD::UDIV, MVT::i16, Expand);
138 setOperationAction(ISD::UREM, MVT::i8, Expand);
139 setOperationAction(ISD::UREM, MVT::i16, Expand);
140 setOperationAction(ISD::SDIV, MVT::i8, Expand);
141 setOperationAction(ISD::SDIV, MVT::i16, Expand);
142 setOperationAction(ISD::SREM, MVT::i8, Expand);
143 setOperationAction(ISD::SREM, MVT::i16, Expand);
145 // Make division and modulus custom
146 for (MVT VT : MVT::integer_valuetypes()) {
147 setOperationAction(ISD::UDIVREM, VT, Custom);
148 setOperationAction(ISD::SDIVREM, VT, Custom);
151 // Do not use MUL. The AVR instructions are closer to SMUL_LOHI &co.
152 setOperationAction(ISD::MUL, MVT::i8, Expand);
153 setOperationAction(ISD::MUL, MVT::i16, Expand);
155 // Expand 16 bit multiplications.
156 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
157 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
159 for (MVT VT : MVT::integer_valuetypes()) {
160 setOperationAction(ISD::MULHS, VT, Expand);
161 setOperationAction(ISD::MULHU, VT, Expand);
164 for (MVT VT : MVT::integer_valuetypes()) {
165 setOperationAction(ISD::CTPOP, VT, Expand);
166 setOperationAction(ISD::CTLZ, VT, Expand);
167 setOperationAction(ISD::CTTZ, VT, Expand);
170 for (MVT VT : MVT::integer_valuetypes()) {
171 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
172 // TODO: The generated code is pretty poor. Investigate using the
173 // same "shift and subtract with carry" trick that we do for
174 // extending 8-bit to 16-bit. This may require infrastructure
175 // improvements in how we treat 16-bit "registers" to be feasible.
178 // Division rtlib functions (not supported)
179 setLibcallName(RTLIB::SDIV_I8, nullptr);
180 setLibcallName(RTLIB::SDIV_I16, nullptr);
181 setLibcallName(RTLIB::SDIV_I32, nullptr);
182 setLibcallName(RTLIB::SDIV_I64, nullptr);
183 setLibcallName(RTLIB::SDIV_I128, nullptr);
184 setLibcallName(RTLIB::UDIV_I8, nullptr);
185 setLibcallName(RTLIB::UDIV_I16, nullptr);
186 setLibcallName(RTLIB::UDIV_I32, nullptr);
187 setLibcallName(RTLIB::UDIV_I64, nullptr);
188 setLibcallName(RTLIB::UDIV_I128, nullptr);
190 // Modulus rtlib functions (not supported)
191 setLibcallName(RTLIB::SREM_I8, nullptr);
192 setLibcallName(RTLIB::SREM_I16, nullptr);
193 setLibcallName(RTLIB::SREM_I32, nullptr);
194 setLibcallName(RTLIB::SREM_I64, nullptr);
195 setLibcallName(RTLIB::SREM_I128, nullptr);
196 setLibcallName(RTLIB::UREM_I8, nullptr);
197 setLibcallName(RTLIB::UREM_I16, nullptr);
198 setLibcallName(RTLIB::UREM_I32, nullptr);
199 setLibcallName(RTLIB::UREM_I64, nullptr);
200 setLibcallName(RTLIB::UREM_I128, nullptr);
202 // Division and modulus rtlib functions
203 setLibcallName(RTLIB::SDIVREM_I8, "__divmodqi4");
204 setLibcallName(RTLIB::SDIVREM_I16, "__divmodhi4");
205 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
206 setLibcallName(RTLIB::SDIVREM_I64, "__divmoddi4");
207 setLibcallName(RTLIB::SDIVREM_I128, "__divmodti4");
208 setLibcallName(RTLIB::UDIVREM_I8, "__udivmodqi4");
209 setLibcallName(RTLIB::UDIVREM_I16, "__udivmodhi4");
210 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
211 setLibcallName(RTLIB::UDIVREM_I64, "__udivmoddi4");
212 setLibcallName(RTLIB::UDIVREM_I128, "__udivmodti4");
214 // Several of the runtime library functions use a special calling conv
215 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::AVR_BUILTIN);
216 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::AVR_BUILTIN);
217 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::AVR_BUILTIN);
218 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::AVR_BUILTIN);
220 // Trigonometric rtlib functions
221 setLibcallName(RTLIB::SIN_F32, "sin");
222 setLibcallName(RTLIB::COS_F32, "cos");
224 setMinFunctionAlignment(1);
225 setMinimumJumpTableEntries(INT_MAX);
228 const char *AVRTargetLowering::getTargetNodeName(unsigned Opcode) const {
257 EVT AVRTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
259 assert(!VT.isVector() && "No AVR SetCC type for vectors!");
263 SDValue AVRTargetLowering::LowerShifts(SDValue Op, SelectionDAG &DAG) const {
264 //:TODO: this function has to be completely rewritten to produce optimal
265 // code, for now it's producing very long but correct code.
267 const SDNode *N = Op.getNode();
268 EVT VT = Op.getValueType();
271 // Expand non-constant shifts to loops.
272 if (!isa<ConstantSDNode>(N->getOperand(1))) {
273 switch (Op.getOpcode()) {
275 llvm_unreachable("Invalid shift opcode!");
277 return DAG.getNode(AVRISD::LSLLOOP, dl, VT, N->getOperand(0),
280 return DAG.getNode(AVRISD::LSRLOOP, dl, VT, N->getOperand(0),
283 return DAG.getNode(AVRISD::ROLLOOP, dl, VT, N->getOperand(0),
286 return DAG.getNode(AVRISD::RORLOOP, dl, VT, N->getOperand(0),
289 return DAG.getNode(AVRISD::ASRLOOP, dl, VT, N->getOperand(0),
294 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
295 SDValue Victim = N->getOperand(0);
297 switch (Op.getOpcode()) {
314 llvm_unreachable("Invalid shift opcode");
317 while (ShiftAmount--) {
318 Victim = DAG.getNode(Opc8, dl, VT, Victim);
324 SDValue AVRTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
325 unsigned Opcode = Op->getOpcode();
326 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
327 "Invalid opcode for Div/Rem lowering");
328 bool IsSigned = (Opcode == ISD::SDIVREM);
329 EVT VT = Op->getValueType(0);
330 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
333 switch (VT.getSimpleVT().SimpleTy) {
335 llvm_unreachable("Unexpected request for libcall!");
337 LC = IsSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8;
340 LC = IsSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16;
343 LC = IsSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32;
346 LC = IsSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64;
349 LC = IsSigned ? RTLIB::SDIVREM_I128 : RTLIB::UDIVREM_I128;
353 SDValue InChain = DAG.getEntryNode();
355 TargetLowering::ArgListTy Args;
356 TargetLowering::ArgListEntry Entry;
357 for (SDValue const &Value : Op->op_values()) {
359 Entry.Ty = Value.getValueType().getTypeForEVT(*DAG.getContext());
360 Entry.IsSExt = IsSigned;
361 Entry.IsZExt = !IsSigned;
362 Args.push_back(Entry);
365 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
366 getPointerTy(DAG.getDataLayout()));
368 Type *RetTy = (Type *)StructType::get(Ty, Ty);
371 TargetLowering::CallLoweringInfo CLI(DAG);
374 .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
376 .setSExtResult(IsSigned)
377 .setZExtResult(!IsSigned);
379 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
380 return CallInfo.first;
383 SDValue AVRTargetLowering::LowerGlobalAddress(SDValue Op,
384 SelectionDAG &DAG) const {
385 auto DL = DAG.getDataLayout();
387 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
388 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
390 // Create the TargetGlobalAddress node, folding in the constant offset.
392 DAG.getTargetGlobalAddress(GV, SDLoc(Op), getPointerTy(DL), Offset);
393 return DAG.getNode(AVRISD::WRAPPER, SDLoc(Op), getPointerTy(DL), Result);
396 SDValue AVRTargetLowering::LowerBlockAddress(SDValue Op,
397 SelectionDAG &DAG) const {
398 auto DL = DAG.getDataLayout();
399 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
401 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(DL));
403 return DAG.getNode(AVRISD::WRAPPER, SDLoc(Op), getPointerTy(DL), Result);
406 /// IntCCToAVRCC - Convert a DAG integer condition code to an AVR CC.
407 static AVRCC::CondCodes intCCToAVRCC(ISD::CondCode CC) {
410 llvm_unreachable("Unknown condition code!");
412 return AVRCC::COND_EQ;
414 return AVRCC::COND_NE;
416 return AVRCC::COND_GE;
418 return AVRCC::COND_LT;
420 return AVRCC::COND_SH;
422 return AVRCC::COND_LO;
426 /// Returns appropriate AVR CMP/CMPC nodes and corresponding condition code for
427 /// the given operands.
428 SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
429 SDValue &AVRcc, SelectionDAG &DAG,
432 EVT VT = LHS.getValueType();
433 bool UseTest = false;
439 // Swap operands and reverse the branching condition.
445 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
446 switch (C->getSExtValue()) {
448 // When doing lhs > -1 use a tst instruction on the top part of lhs
449 // and use brpl instead of using a chain of cp/cpc.
451 AVRcc = DAG.getConstant(AVRCC::COND_PL, DL, MVT::i8);
455 // Turn lhs > 0 into 0 < lhs since 0 can be materialized with
456 // __zero_reg__ in lhs.
458 LHS = DAG.getConstant(0, DL, VT);
463 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows
464 // us to fold the constant into the cmp instruction.
465 RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
472 // Swap operands and reverse the branching condition.
478 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
479 switch (C->getSExtValue()) {
481 // Turn lhs < 1 into 0 >= lhs since 0 can be materialized with
482 // __zero_reg__ in lhs.
484 LHS = DAG.getConstant(0, DL, VT);
489 // When doing lhs < 0 use a tst instruction on the top part of lhs
490 // and use brmi instead of using a chain of cp/cpc.
492 AVRcc = DAG.getConstant(AVRCC::COND_MI, DL, MVT::i8);
500 // Swap operands and reverse the branching condition.
506 // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
507 // fold the constant into the cmp instruction.
508 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
509 RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
513 // Swap operands and reverse the branching condition.
520 // Expand 32 and 64 bit comparisons with custom CMP and CMPC nodes instead of
521 // using the default and/or/xor expansion code which is much longer.
522 if (VT == MVT::i32) {
523 SDValue LHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS,
524 DAG.getIntPtrConstant(0, DL));
525 SDValue LHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS,
526 DAG.getIntPtrConstant(1, DL));
527 SDValue RHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS,
528 DAG.getIntPtrConstant(0, DL));
529 SDValue RHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS,
530 DAG.getIntPtrConstant(1, DL));
533 // When using tst we only care about the highest part.
534 SDValue Top = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHShi,
535 DAG.getIntPtrConstant(1, DL));
536 Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue, Top);
538 Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHSlo, RHSlo);
539 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHShi, RHShi, Cmp);
541 } else if (VT == MVT::i64) {
542 SDValue LHS_0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS,
543 DAG.getIntPtrConstant(0, DL));
544 SDValue LHS_1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS,
545 DAG.getIntPtrConstant(1, DL));
547 SDValue LHS0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_0,
548 DAG.getIntPtrConstant(0, DL));
549 SDValue LHS1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_0,
550 DAG.getIntPtrConstant(1, DL));
551 SDValue LHS2 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_1,
552 DAG.getIntPtrConstant(0, DL));
553 SDValue LHS3 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_1,
554 DAG.getIntPtrConstant(1, DL));
556 SDValue RHS_0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS,
557 DAG.getIntPtrConstant(0, DL));
558 SDValue RHS_1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS,
559 DAG.getIntPtrConstant(1, DL));
561 SDValue RHS0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_0,
562 DAG.getIntPtrConstant(0, DL));
563 SDValue RHS1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_0,
564 DAG.getIntPtrConstant(1, DL));
565 SDValue RHS2 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_1,
566 DAG.getIntPtrConstant(0, DL));
567 SDValue RHS3 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_1,
568 DAG.getIntPtrConstant(1, DL));
571 // When using tst we only care about the highest part.
572 SDValue Top = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHS3,
573 DAG.getIntPtrConstant(1, DL));
574 Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue, Top);
576 Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHS0, RHS0);
577 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS1, RHS1, Cmp);
578 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS2, RHS2, Cmp);
579 Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS3, RHS3, Cmp);
581 } else if (VT == MVT::i8 || VT == MVT::i16) {
583 // When using tst we only care about the highest part.
584 Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue,
587 : DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8,
588 LHS, DAG.getIntPtrConstant(1, DL)));
590 Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHS, RHS);
593 llvm_unreachable("Invalid comparison size");
596 // When using a test instruction AVRcc is already set.
598 AVRcc = DAG.getConstant(intCCToAVRCC(CC), DL, MVT::i8);
604 SDValue AVRTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
605 SDValue Chain = Op.getOperand(0);
606 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
607 SDValue LHS = Op.getOperand(2);
608 SDValue RHS = Op.getOperand(3);
609 SDValue Dest = Op.getOperand(4);
613 SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, dl);
615 return DAG.getNode(AVRISD::BRCOND, dl, MVT::Other, Chain, Dest, TargetCC,
619 SDValue AVRTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
620 SDValue LHS = Op.getOperand(0);
621 SDValue RHS = Op.getOperand(1);
622 SDValue TrueV = Op.getOperand(2);
623 SDValue FalseV = Op.getOperand(3);
624 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
628 SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, dl);
630 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
631 SDValue Ops[] = {TrueV, FalseV, TargetCC, Cmp};
633 return DAG.getNode(AVRISD::SELECT_CC, dl, VTs, Ops);
636 SDValue AVRTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
637 SDValue LHS = Op.getOperand(0);
638 SDValue RHS = Op.getOperand(1);
639 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
643 SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, DL);
645 SDValue TrueV = DAG.getConstant(1, DL, Op.getValueType());
646 SDValue FalseV = DAG.getConstant(0, DL, Op.getValueType());
647 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
648 SDValue Ops[] = {TrueV, FalseV, TargetCC, Cmp};
650 return DAG.getNode(AVRISD::SELECT_CC, DL, VTs, Ops);
653 SDValue AVRTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
654 const MachineFunction &MF = DAG.getMachineFunction();
655 const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
656 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
657 auto DL = DAG.getDataLayout();
660 // Vastart just stores the address of the VarArgsFrameIndex slot into the
661 // memory location argument.
662 SDValue FI = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(), getPointerTy(DL));
664 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
665 MachinePointerInfo(SV), 0);
668 SDValue AVRTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
669 switch (Op.getOpcode()) {
671 llvm_unreachable("Don't know how to custom lower this!");
677 return LowerShifts(Op, DAG);
678 case ISD::GlobalAddress:
679 return LowerGlobalAddress(Op, DAG);
680 case ISD::BlockAddress:
681 return LowerBlockAddress(Op, DAG);
683 return LowerBR_CC(Op, DAG);
685 return LowerSELECT_CC(Op, DAG);
687 return LowerSETCC(Op, DAG);
689 return LowerVASTART(Op, DAG);
692 return LowerDivRem(Op, DAG);
698 /// Replace a node with an illegal result type
699 /// with a new node built out of custom code.
700 void AVRTargetLowering::ReplaceNodeResults(SDNode *N,
701 SmallVectorImpl<SDValue> &Results,
702 SelectionDAG &DAG) const {
705 switch (N->getOpcode()) {
707 // Convert add (x, imm) into sub (x, -imm).
708 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
709 SDValue Sub = DAG.getNode(
710 ISD::SUB, DL, N->getValueType(0), N->getOperand(0),
711 DAG.getConstant(-C->getAPIntValue(), DL, C->getValueType(0)));
712 Results.push_back(Sub);
717 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
719 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
720 Results.push_back(Res.getValue(I));
727 /// Return true if the addressing mode represented
728 /// by AM is legal for this target, for a load/store of the specified type.
729 bool AVRTargetLowering::isLegalAddressingMode(const DataLayout &DL,
730 const AddrMode &AM, Type *Ty,
731 unsigned AS, Instruction *I) const {
732 int64_t Offs = AM.BaseOffs;
734 // Allow absolute addresses.
735 if (AM.BaseGV && !AM.HasBaseReg && AM.Scale == 0 && Offs == 0) {
739 // Flash memory instructions only allow zero offsets.
740 if (isa<PointerType>(Ty) && AS == AVR::ProgramMemory) {
744 // Allow reg+<6bit> offset.
747 if (AM.BaseGV == 0 && AM.HasBaseReg && AM.Scale == 0 && isUInt<6>(Offs)) {
754 /// Returns true by value, base pointer and
755 /// offset pointer and addressing mode by reference if the node's address
756 /// can be legally represented as pre-indexed load / store address.
757 bool AVRTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
759 ISD::MemIndexedMode &AM,
760 SelectionDAG &DAG) const {
765 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
766 VT = LD->getMemoryVT();
767 Op = LD->getBasePtr().getNode();
768 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
770 if (AVR::isProgramMemoryAccess(LD)) {
773 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
774 VT = ST->getMemoryVT();
775 Op = ST->getBasePtr().getNode();
776 if (AVR::isProgramMemoryAccess(ST)) {
783 if (VT != MVT::i8 && VT != MVT::i16) {
787 if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB) {
791 if (const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
792 int RHSC = RHS->getSExtValue();
793 if (Op->getOpcode() == ISD::SUB)
796 if ((VT == MVT::i16 && RHSC != -2) || (VT == MVT::i8 && RHSC != -1)) {
800 Base = Op->getOperand(0);
801 Offset = DAG.getConstant(RHSC, DL, MVT::i8);
810 /// Returns true by value, base pointer and
811 /// offset pointer and addressing mode by reference if this node can be
812 /// combined with a load / store to form a post-indexed load / store.
813 bool AVRTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
816 ISD::MemIndexedMode &AM,
817 SelectionDAG &DAG) const {
821 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
822 VT = LD->getMemoryVT();
823 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
825 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
826 VT = ST->getMemoryVT();
827 if (AVR::isProgramMemoryAccess(ST)) {
834 if (VT != MVT::i8 && VT != MVT::i16) {
838 if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB) {
842 if (const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
843 int RHSC = RHS->getSExtValue();
844 if (Op->getOpcode() == ISD::SUB)
846 if ((VT == MVT::i16 && RHSC != 2) || (VT == MVT::i8 && RHSC != 1)) {
850 Base = Op->getOperand(0);
851 Offset = DAG.getConstant(RHSC, DL, MVT::i8);
860 bool AVRTargetLowering::isOffsetFoldingLegal(
861 const GlobalAddressSDNode *GA) const {
865 //===----------------------------------------------------------------------===//
866 // Formal Arguments Calling Convention Implementation
867 //===----------------------------------------------------------------------===//
869 #include "AVRGenCallingConv.inc"
871 /// For each argument in a function store the number of pieces it is composed
873 static void parseFunctionArgs(const SmallVectorImpl<ISD::InputArg> &Ins,
874 SmallVectorImpl<unsigned> &Out) {
875 for (const ISD::InputArg &Arg : Ins) {
876 if(Arg.PartOffset > 0) continue;
877 unsigned Bytes = ((Arg.ArgVT.getSizeInBits()) + 7) / 8;
879 Out.push_back((Bytes + 1) / 2);
883 /// For external symbols there is no function prototype information so we
884 /// have to rely directly on argument sizes.
885 static void parseExternFuncCallArgs(const SmallVectorImpl<ISD::OutputArg> &In,
886 SmallVectorImpl<unsigned> &Out) {
887 for (unsigned i = 0, e = In.size(); i != e;) {
890 while ((i != e) && (In[i].PartOffset == Offset)) {
891 Offset += In[i].VT.getStoreSize();
899 static StringRef getFunctionName(TargetLowering::CallLoweringInfo &CLI) {
900 SDValue Callee = CLI.Callee;
902 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee)) {
903 return G->getSymbol();
906 if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
907 return G->getGlobal()->getName();
910 llvm_unreachable("don't know how to get the name for this callee");
913 /// Analyze incoming and outgoing function arguments. We need custom C++ code
914 /// to handle special constraints in the ABI like reversing the order of the
915 /// pieces of splitted arguments. In addition, all pieces of a certain argument
916 /// have to be passed either using registers or the stack but never mixing both.
917 static void analyzeStandardArguments(TargetLowering::CallLoweringInfo *CLI,
918 const Function *F, const DataLayout *TD,
919 const SmallVectorImpl<ISD::OutputArg> *Outs,
920 const SmallVectorImpl<ISD::InputArg> *Ins,
921 CallingConv::ID CallConv,
922 SmallVectorImpl<CCValAssign> &ArgLocs,
923 CCState &CCInfo, bool IsCall, bool IsVarArg) {
924 static const MCPhysReg RegList8[] = {AVR::R24, AVR::R22, AVR::R20,
925 AVR::R18, AVR::R16, AVR::R14,
926 AVR::R12, AVR::R10, AVR::R8};
927 static const MCPhysReg RegList16[] = {AVR::R25R24, AVR::R23R22, AVR::R21R20,
928 AVR::R19R18, AVR::R17R16, AVR::R15R14,
929 AVR::R13R12, AVR::R11R10, AVR::R9R8};
931 // Variadic functions do not need all the analisys below.
933 CCInfo.AnalyzeCallOperands(*Outs, ArgCC_AVR_Vararg);
935 CCInfo.AnalyzeFormalArguments(*Ins, ArgCC_AVR_Vararg);
940 // Fill in the Args array which will contain original argument sizes.
941 SmallVector<unsigned, 8> Args;
943 parseExternFuncCallArgs(*Outs, Args);
945 assert(F != nullptr && "function should not be null");
946 parseFunctionArgs(*Ins, Args);
949 unsigned RegsLeft = array_lengthof(RegList8), ValNo = 0;
950 // Variadic functions always use the stack.
951 bool UsesStack = false;
952 for (unsigned i = 0, pos = 0, e = Args.size(); i != e; ++i) {
953 unsigned Size = Args[i];
955 // If we have a zero-sized argument, don't attempt to lower it.
956 // AVR-GCC does not support zero-sized arguments and so we need not
957 // worry about ABI compatibility.
958 if (Size == 0) continue;
960 MVT LocVT = (IsCall) ? (*Outs)[pos].VT : (*Ins)[pos].VT;
962 // If we have plenty of regs to pass the whole argument do it.
963 if (!UsesStack && (Size <= RegsLeft)) {
964 const MCPhysReg *RegList = (LocVT == MVT::i16) ? RegList16 : RegList8;
966 for (unsigned j = 0; j != Size; ++j) {
967 unsigned Reg = CCInfo.AllocateReg(
968 ArrayRef<MCPhysReg>(RegList, array_lengthof(RegList8)));
970 CCValAssign::getReg(ValNo++, LocVT, Reg, LocVT, CCValAssign::Full));
974 // Reverse the order of the pieces to agree with the "big endian" format
975 // required in the calling convention ABI.
976 std::reverse(ArgLocs.begin() + pos, ArgLocs.begin() + pos + Size);
978 // Pass the rest of arguments using the stack.
980 for (unsigned j = 0; j != Size; ++j) {
981 unsigned Offset = CCInfo.AllocateStack(
982 TD->getTypeAllocSize(EVT(LocVT).getTypeForEVT(CCInfo.getContext())),
983 TD->getABITypeAlignment(
984 EVT(LocVT).getTypeForEVT(CCInfo.getContext())));
985 CCInfo.addLoc(CCValAssign::getMem(ValNo++, LocVT, Offset, LocVT,
993 static void analyzeBuiltinArguments(TargetLowering::CallLoweringInfo &CLI,
994 const Function *F, const DataLayout *TD,
995 const SmallVectorImpl<ISD::OutputArg> *Outs,
996 const SmallVectorImpl<ISD::InputArg> *Ins,
997 CallingConv::ID CallConv,
998 SmallVectorImpl<CCValAssign> &ArgLocs,
999 CCState &CCInfo, bool IsCall, bool IsVarArg) {
1000 StringRef FuncName = getFunctionName(CLI);
1002 if (FuncName.startswith("__udivmod") || FuncName.startswith("__divmod")) {
1003 CCInfo.AnalyzeCallOperands(*Outs, ArgCC_AVR_BUILTIN_DIV);
1005 analyzeStandardArguments(&CLI, F, TD, Outs, Ins,
1006 CallConv, ArgLocs, CCInfo,
1011 static void analyzeArguments(TargetLowering::CallLoweringInfo *CLI,
1012 const Function *F, const DataLayout *TD,
1013 const SmallVectorImpl<ISD::OutputArg> *Outs,
1014 const SmallVectorImpl<ISD::InputArg> *Ins,
1015 CallingConv::ID CallConv,
1016 SmallVectorImpl<CCValAssign> &ArgLocs,
1017 CCState &CCInfo, bool IsCall, bool IsVarArg) {
1019 case CallingConv::AVR_BUILTIN: {
1020 analyzeBuiltinArguments(*CLI, F, TD, Outs, Ins,
1021 CallConv, ArgLocs, CCInfo,
1026 analyzeStandardArguments(CLI, F, TD, Outs, Ins,
1027 CallConv, ArgLocs, CCInfo,
1034 SDValue AVRTargetLowering::LowerFormalArguments(
1035 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1036 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, SelectionDAG &DAG,
1037 SmallVectorImpl<SDValue> &InVals) const {
1038 MachineFunction &MF = DAG.getMachineFunction();
1039 MachineFrameInfo &MFI = MF.getFrameInfo();
1040 auto DL = DAG.getDataLayout();
1042 // Assign locations to all of the incoming arguments.
1043 SmallVector<CCValAssign, 16> ArgLocs;
1044 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1047 analyzeArguments(nullptr, &MF.getFunction(), &DL, 0, &Ins, CallConv, ArgLocs, CCInfo,
1051 for (CCValAssign &VA : ArgLocs) {
1053 // Arguments stored on registers.
1054 if (VA.isRegLoc()) {
1055 EVT RegVT = VA.getLocVT();
1056 const TargetRegisterClass *RC;
1057 if (RegVT == MVT::i8) {
1058 RC = &AVR::GPR8RegClass;
1059 } else if (RegVT == MVT::i16) {
1060 RC = &AVR::DREGSRegClass;
1062 llvm_unreachable("Unknown argument type!");
1065 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1066 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1068 // :NOTE: Clang should not promote any i8 into i16 but for safety the
1069 // following code will handle zexts or sexts generated by other
1070 // front ends. Otherwise:
1071 // If this is an 8 bit value, it is really passed promoted
1072 // to 16 bits. Insert an assert[sz]ext to capture this, then
1073 // truncate to the right size.
1074 switch (VA.getLocInfo()) {
1076 llvm_unreachable("Unknown loc info!");
1077 case CCValAssign::Full:
1079 case CCValAssign::BCvt:
1080 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
1082 case CCValAssign::SExt:
1083 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1084 DAG.getValueType(VA.getValVT()));
1085 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1087 case CCValAssign::ZExt:
1088 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1089 DAG.getValueType(VA.getValVT()));
1090 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1094 InVals.push_back(ArgValue);
1097 assert(VA.isMemLoc());
1099 EVT LocVT = VA.getLocVT();
1101 // Create the frame index object for this incoming parameter.
1102 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1103 VA.getLocMemOffset(), true);
1105 // Create the SelectionDAG nodes corresponding to a load
1106 // from this parameter.
1107 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DL));
1108 InVals.push_back(DAG.getLoad(LocVT, dl, Chain, FIN,
1109 MachinePointerInfo::getFixedStack(MF, FI),
1114 // If the function takes variable number of arguments, make a frame index for
1115 // the start of the first vararg value... for expansion of llvm.va_start.
1117 unsigned StackSize = CCInfo.getNextStackOffset();
1118 AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
1120 AFI->setVarArgsFrameIndex(MFI.CreateFixedObject(2, StackSize, true));
1126 //===----------------------------------------------------------------------===//
1127 // Call Calling Convention Implementation
1128 //===----------------------------------------------------------------------===//
1130 SDValue AVRTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
1131 SmallVectorImpl<SDValue> &InVals) const {
1132 SelectionDAG &DAG = CLI.DAG;
1134 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1135 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1136 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1137 SDValue Chain = CLI.Chain;
1138 SDValue Callee = CLI.Callee;
1139 bool &isTailCall = CLI.IsTailCall;
1140 CallingConv::ID CallConv = CLI.CallConv;
1141 bool isVarArg = CLI.IsVarArg;
1143 MachineFunction &MF = DAG.getMachineFunction();
1145 // AVR does not yet support tail call optimization.
1148 // Analyze operands of the call, assigning locations to each operand.
1149 SmallVector<CCValAssign, 16> ArgLocs;
1150 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1153 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1154 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1155 // node so that legalize doesn't hack it.
1156 const Function *F = nullptr;
1157 if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1158 const GlobalValue *GV = G->getGlobal();
1160 F = cast<Function>(GV);
1162 DAG.getTargetGlobalAddress(GV, DL, getPointerTy(DAG.getDataLayout()));
1163 } else if (const ExternalSymbolSDNode *ES =
1164 dyn_cast<ExternalSymbolSDNode>(Callee)) {
1165 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(),
1166 getPointerTy(DAG.getDataLayout()));
1169 analyzeArguments(&CLI, F, &DAG.getDataLayout(), &Outs, 0, CallConv, ArgLocs, CCInfo,
1172 // Get a count of how many bytes are to be pushed on the stack.
1173 unsigned NumBytes = CCInfo.getNextStackOffset();
1175 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
1177 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1179 // First, walk the register assignments, inserting copies.
1181 bool HasStackArgs = false;
1182 for (AI = 0, AE = ArgLocs.size(); AI != AE; ++AI) {
1183 CCValAssign &VA = ArgLocs[AI];
1184 EVT RegVT = VA.getLocVT();
1185 SDValue Arg = OutVals[AI];
1187 // Promote the value if needed. With Clang this should not happen.
1188 switch (VA.getLocInfo()) {
1190 llvm_unreachable("Unknown loc info!");
1191 case CCValAssign::Full:
1193 case CCValAssign::SExt:
1194 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, RegVT, Arg);
1196 case CCValAssign::ZExt:
1197 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, RegVT, Arg);
1199 case CCValAssign::AExt:
1200 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, RegVT, Arg);
1202 case CCValAssign::BCvt:
1203 Arg = DAG.getNode(ISD::BITCAST, DL, RegVT, Arg);
1207 // Stop when we encounter a stack argument, we need to process them
1208 // in reverse order in the loop below.
1209 if (VA.isMemLoc()) {
1210 HasStackArgs = true;
1214 // Arguments that can be passed on registers must be kept in the RegsToPass
1216 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1219 // Second, stack arguments have to walked in reverse order by inserting
1220 // chained stores, this ensures their order is not changed by the scheduler
1221 // and that the push instruction sequence generated is correct, otherwise they
1222 // can be freely intermixed.
1224 for (AE = AI, AI = ArgLocs.size(); AI != AE; --AI) {
1225 unsigned Loc = AI - 1;
1226 CCValAssign &VA = ArgLocs[Loc];
1227 SDValue Arg = OutVals[Loc];
1229 assert(VA.isMemLoc());
1231 // SP points to one stack slot further so add one to adjust it.
1232 SDValue PtrOff = DAG.getNode(
1233 ISD::ADD, DL, getPointerTy(DAG.getDataLayout()),
1234 DAG.getRegister(AVR::SP, getPointerTy(DAG.getDataLayout())),
1235 DAG.getIntPtrConstant(VA.getLocMemOffset() + 1, DL));
1238 DAG.getStore(Chain, DL, Arg, PtrOff,
1239 MachinePointerInfo::getStack(MF, VA.getLocMemOffset()),
1244 // Build a sequence of copy-to-reg nodes chained together with token chain and
1245 // flag operands which copy the outgoing args into registers. The InFlag in
1246 // necessary since all emited instructions must be stuck together.
1248 for (auto Reg : RegsToPass) {
1249 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, InFlag);
1250 InFlag = Chain.getValue(1);
1253 // Returns a chain & a flag for retval copy to use.
1254 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1255 SmallVector<SDValue, 8> Ops;
1256 Ops.push_back(Chain);
1257 Ops.push_back(Callee);
1259 // Add argument registers to the end of the list so that they are known live
1261 for (auto Reg : RegsToPass) {
1262 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1265 // Add a register mask operand representing the call-preserved registers.
1266 const AVRTargetMachine &TM = (const AVRTargetMachine &)getTargetMachine();
1267 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
1268 const uint32_t *Mask =
1269 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
1270 assert(Mask && "Missing call preserved mask for calling convention");
1271 Ops.push_back(DAG.getRegisterMask(Mask));
1273 if (InFlag.getNode()) {
1274 Ops.push_back(InFlag);
1277 Chain = DAG.getNode(AVRISD::CALL, DL, NodeTys, Ops);
1278 InFlag = Chain.getValue(1);
1280 // Create the CALLSEQ_END node.
1281 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true),
1282 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
1285 InFlag = Chain.getValue(1);
1288 // Handle result values, copying them out of physregs into vregs that we
1290 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, DL, DAG,
1294 /// Lower the result values of a call into the
1295 /// appropriate copies out of appropriate physical registers.
1297 SDValue AVRTargetLowering::LowerCallResult(
1298 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
1299 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, SelectionDAG &DAG,
1300 SmallVectorImpl<SDValue> &InVals) const {
1302 // Assign locations to each value returned by this call.
1303 SmallVector<CCValAssign, 16> RVLocs;
1304 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1307 // Handle runtime calling convs.
1308 auto CCFunction = CCAssignFnForReturn(CallConv);
1309 CCInfo.AnalyzeCallResult(Ins, CCFunction);
1311 if (CallConv != CallingConv::AVR_BUILTIN && RVLocs.size() > 1) {
1312 // Reverse splitted return values to get the "big endian" format required
1313 // to agree with the calling convention ABI.
1314 std::reverse(RVLocs.begin(), RVLocs.end());
1317 // Copy all of the result registers out of their specified physreg.
1318 for (CCValAssign const &RVLoc : RVLocs) {
1319 Chain = DAG.getCopyFromReg(Chain, dl, RVLoc.getLocReg(), RVLoc.getValVT(),
1322 InFlag = Chain.getValue(2);
1323 InVals.push_back(Chain.getValue(0));
1329 //===----------------------------------------------------------------------===//
1330 // Return Value Calling Convention Implementation
1331 //===----------------------------------------------------------------------===//
1333 CCAssignFn *AVRTargetLowering::CCAssignFnForReturn(CallingConv::ID CC) const {
1335 case CallingConv::AVR_BUILTIN:
1336 return RetCC_AVR_BUILTIN;
1343 AVRTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1344 MachineFunction &MF, bool isVarArg,
1345 const SmallVectorImpl<ISD::OutputArg> &Outs,
1346 LLVMContext &Context) const
1348 SmallVector<CCValAssign, 16> RVLocs;
1349 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
1351 auto CCFunction = CCAssignFnForReturn(CallConv);
1352 return CCInfo.CheckReturn(Outs, CCFunction);
1356 AVRTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1358 const SmallVectorImpl<ISD::OutputArg> &Outs,
1359 const SmallVectorImpl<SDValue> &OutVals,
1360 const SDLoc &dl, SelectionDAG &DAG) const {
1361 // CCValAssign - represent the assignment of the return value to locations.
1362 SmallVector<CCValAssign, 16> RVLocs;
1364 // CCState - Info about the registers and stack slot.
1365 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1368 // Analyze return values.
1369 auto CCFunction = CCAssignFnForReturn(CallConv);
1370 CCInfo.AnalyzeReturn(Outs, CCFunction);
1372 // If this is the first return lowered for this function, add the regs to
1373 // the liveout set for the function.
1374 MachineFunction &MF = DAG.getMachineFunction();
1375 unsigned e = RVLocs.size();
1377 // Reverse splitted return values to get the "big endian" format required
1378 // to agree with the calling convention ABI.
1380 std::reverse(RVLocs.begin(), RVLocs.end());
1384 SmallVector<SDValue, 4> RetOps(1, Chain);
1385 // Copy the result values into the output registers.
1386 for (unsigned i = 0; i != e; ++i) {
1387 CCValAssign &VA = RVLocs[i];
1388 assert(VA.isRegLoc() && "Can only return in registers!");
1390 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
1392 // Guarantee that all emitted copies are stuck together with flags.
1393 Flag = Chain.getValue(1);
1394 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1397 // Don't emit the ret/reti instruction when the naked attribute is present in
1398 // the function being compiled.
1399 if (MF.getFunction().getAttributes().hasAttribute(
1400 AttributeList::FunctionIndex, Attribute::Naked)) {
1405 (CallConv == CallingConv::AVR_INTR || CallConv == CallingConv::AVR_SIGNAL)
1409 RetOps[0] = Chain; // Update chain.
1411 if (Flag.getNode()) {
1412 RetOps.push_back(Flag);
1415 return DAG.getNode(RetOpc, dl, MVT::Other, RetOps);
1418 //===----------------------------------------------------------------------===//
1420 //===----------------------------------------------------------------------===//
1422 MachineBasicBlock *AVRTargetLowering::insertShift(MachineInstr &MI,
1423 MachineBasicBlock *BB) const {
1425 const TargetRegisterClass *RC;
1426 MachineFunction *F = BB->getParent();
1427 MachineRegisterInfo &RI = F->getRegInfo();
1428 const AVRTargetMachine &TM = (const AVRTargetMachine &)getTargetMachine();
1429 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
1430 DebugLoc dl = MI.getDebugLoc();
1432 switch (MI.getOpcode()) {
1434 llvm_unreachable("Invalid shift opcode!");
1437 RC = &AVR::GPR8RegClass;
1441 RC = &AVR::DREGSRegClass;
1445 RC = &AVR::GPR8RegClass;
1449 RC = &AVR::DREGSRegClass;
1453 RC = &AVR::GPR8RegClass;
1457 RC = &AVR::DREGSRegClass;
1461 RC = &AVR::GPR8RegClass;
1465 RC = &AVR::DREGSRegClass;
1469 RC = &AVR::GPR8RegClass;
1473 RC = &AVR::DREGSRegClass;
1477 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1479 MachineFunction::iterator I;
1480 for (I = BB->getIterator(); I != F->end() && &(*I) != BB; ++I);
1481 if (I != F->end()) ++I;
1483 // Create loop block.
1484 MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1485 MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1487 F->insert(I, LoopBB);
1488 F->insert(I, RemBB);
1490 // Update machine-CFG edges by transferring all successors of the current
1491 // block to the block containing instructions after shift.
1492 RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
1494 RemBB->transferSuccessorsAndUpdatePHIs(BB);
1496 // Add adges BB => LoopBB => RemBB, BB => RemBB, LoopBB => LoopBB.
1497 BB->addSuccessor(LoopBB);
1498 BB->addSuccessor(RemBB);
1499 LoopBB->addSuccessor(RemBB);
1500 LoopBB->addSuccessor(LoopBB);
1502 unsigned ShiftAmtReg = RI.createVirtualRegister(&AVR::LD8RegClass);
1503 unsigned ShiftAmtReg2 = RI.createVirtualRegister(&AVR::LD8RegClass);
1504 unsigned ShiftReg = RI.createVirtualRegister(RC);
1505 unsigned ShiftReg2 = RI.createVirtualRegister(RC);
1506 unsigned ShiftAmtSrcReg = MI.getOperand(2).getReg();
1507 unsigned SrcReg = MI.getOperand(1).getReg();
1508 unsigned DstReg = MI.getOperand(0).getReg();
1513 BuildMI(BB, dl, TII.get(AVR::CPIRdK)).addReg(ShiftAmtSrcReg).addImm(0);
1514 BuildMI(BB, dl, TII.get(AVR::BREQk)).addMBB(RemBB);
1517 // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1518 // ShiftAmt = phi [%N, BB], [%ShiftAmt2, LoopBB]
1519 // ShiftReg2 = shift ShiftReg
1520 // ShiftAmt2 = ShiftAmt - 1;
1521 BuildMI(LoopBB, dl, TII.get(AVR::PHI), ShiftReg)
1526 BuildMI(LoopBB, dl, TII.get(AVR::PHI), ShiftAmtReg)
1527 .addReg(ShiftAmtSrcReg)
1529 .addReg(ShiftAmtReg2)
1531 BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2).addReg(ShiftReg);
1532 BuildMI(LoopBB, dl, TII.get(AVR::SUBIRdK), ShiftAmtReg2)
1533 .addReg(ShiftAmtReg)
1535 BuildMI(LoopBB, dl, TII.get(AVR::BRNEk)).addMBB(LoopBB);
1538 // DestReg = phi [%SrcReg, BB], [%ShiftReg, LoopBB]
1539 BuildMI(*RemBB, RemBB->begin(), dl, TII.get(AVR::PHI), DstReg)
1545 MI.eraseFromParent(); // The pseudo instruction is gone now.
1549 static bool isCopyMulResult(MachineBasicBlock::iterator const &I) {
1550 if (I->getOpcode() == AVR::COPY) {
1551 unsigned SrcReg = I->getOperand(1).getReg();
1552 return (SrcReg == AVR::R0 || SrcReg == AVR::R1);
1558 // The mul instructions wreak havock on our zero_reg R1. We need to clear it
1559 // after the result has been evacuated. This is probably not the best way to do
1560 // it, but it works for now.
1561 MachineBasicBlock *AVRTargetLowering::insertMul(MachineInstr &MI,
1562 MachineBasicBlock *BB) const {
1563 const AVRTargetMachine &TM = (const AVRTargetMachine &)getTargetMachine();
1564 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
1565 MachineBasicBlock::iterator I(MI);
1566 ++I; // in any case insert *after* the mul instruction
1567 if (isCopyMulResult(I))
1569 if (isCopyMulResult(I))
1571 BuildMI(*BB, I, MI.getDebugLoc(), TII.get(AVR::EORRdRr), AVR::R1)
1578 AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
1579 MachineBasicBlock *MBB) const {
1580 int Opc = MI.getOpcode();
1582 // Pseudo shift instructions with a non constant shift amount are expanded
1595 return insertShift(MI, MBB);
1598 return insertMul(MI, MBB);
1601 assert((Opc == AVR::Select16 || Opc == AVR::Select8) &&
1602 "Unexpected instr type to insert");
1604 const AVRInstrInfo &TII = (const AVRInstrInfo &)*MI.getParent()
1608 DebugLoc dl = MI.getDebugLoc();
1610 // To "insert" a SELECT instruction, we insert the diamond
1611 // control-flow pattern. The incoming instruction knows the
1612 // destination vreg to set, the condition code register to branch
1613 // on, the true/false values to select between, and a branch opcode
1616 MachineFunction *MF = MBB->getParent();
1617 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
1618 MachineBasicBlock *trueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1619 MachineBasicBlock *falseMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1621 MachineFunction::iterator I;
1622 for (I = MF->begin(); I != MF->end() && &(*I) != MBB; ++I);
1623 if (I != MF->end()) ++I;
1624 MF->insert(I, trueMBB);
1625 MF->insert(I, falseMBB);
1627 // Transfer remaining instructions and all successors of the current
1628 // block to the block which will contain the Phi node for the
1630 trueMBB->splice(trueMBB->begin(), MBB,
1631 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
1632 trueMBB->transferSuccessorsAndUpdatePHIs(MBB);
1634 AVRCC::CondCodes CC = (AVRCC::CondCodes)MI.getOperand(3).getImm();
1635 BuildMI(MBB, dl, TII.getBrCond(CC)).addMBB(trueMBB);
1636 BuildMI(MBB, dl, TII.get(AVR::RJMPk)).addMBB(falseMBB);
1637 MBB->addSuccessor(falseMBB);
1638 MBB->addSuccessor(trueMBB);
1640 // Unconditionally flow back to the true block
1641 BuildMI(falseMBB, dl, TII.get(AVR::RJMPk)).addMBB(trueMBB);
1642 falseMBB->addSuccessor(trueMBB);
1644 // Set up the Phi node to determine where we came from
1645 BuildMI(*trueMBB, trueMBB->begin(), dl, TII.get(AVR::PHI), MI.getOperand(0).getReg())
1646 .addReg(MI.getOperand(1).getReg())
1648 .addReg(MI.getOperand(2).getReg())
1651 MI.eraseFromParent(); // The pseudo instruction is gone now.
1655 //===----------------------------------------------------------------------===//
1656 // Inline Asm Support
1657 //===----------------------------------------------------------------------===//
1659 AVRTargetLowering::ConstraintType
1660 AVRTargetLowering::getConstraintType(StringRef Constraint) const {
1661 if (Constraint.size() == 1) {
1662 // See http://www.nongnu.org/avr-libc/user-manual/inline_asm.html
1663 switch (Constraint[0]) {
1664 case 'a': // Simple upper registers
1665 case 'b': // Base pointer registers pairs
1666 case 'd': // Upper register
1667 case 'l': // Lower registers
1668 case 'e': // Pointer register pairs
1669 case 'q': // Stack pointer register
1670 case 'r': // Any register
1671 case 'w': // Special upper register pairs
1672 return C_RegisterClass;
1673 case 't': // Temporary register
1674 case 'x': case 'X': // Pointer register pair X
1675 case 'y': case 'Y': // Pointer register pair Y
1676 case 'z': case 'Z': // Pointer register pair Z
1678 case 'Q': // A memory address based on Y or Z pointer with displacement.
1680 case 'G': // Floating point constant
1681 case 'I': // 6-bit positive integer constant
1682 case 'J': // 6-bit negative integer constant
1683 case 'K': // Integer constant (Range: 2)
1684 case 'L': // Integer constant (Range: 0)
1685 case 'M': // 8-bit integer constant
1686 case 'N': // Integer constant (Range: -1)
1687 case 'O': // Integer constant (Range: 8, 16, 24)
1688 case 'P': // Integer constant (Range: 1)
1689 case 'R': // Integer constant (Range: -6 to 5)x
1696 return TargetLowering::getConstraintType(Constraint);
1700 AVRTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const {
1701 // Not sure if this is actually the right thing to do, but we got to do
1702 // *something* [agnat]
1703 switch (ConstraintCode[0]) {
1705 return InlineAsm::Constraint_Q;
1707 return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
1710 AVRTargetLowering::ConstraintWeight
1711 AVRTargetLowering::getSingleConstraintMatchWeight(
1712 AsmOperandInfo &info, const char *constraint) const {
1713 ConstraintWeight weight = CW_Invalid;
1714 Value *CallOperandVal = info.CallOperandVal;
1716 // If we don't have a value, we can't do a match,
1717 // but allow it at the lowest weight.
1718 // (this behaviour has been copied from the ARM backend)
1719 if (!CallOperandVal) {
1723 // Look at the constraint type.
1724 switch (*constraint) {
1726 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1731 weight = CW_Register;
1742 weight = CW_SpecificReg;
1745 if (const ConstantFP *C = dyn_cast<ConstantFP>(CallOperandVal)) {
1747 weight = CW_Constant;
1752 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1753 if (isUInt<6>(C->getZExtValue())) {
1754 weight = CW_Constant;
1759 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1760 if ((C->getSExtValue() >= -63) && (C->getSExtValue() <= 0)) {
1761 weight = CW_Constant;
1766 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1767 if (C->getZExtValue() == 2) {
1768 weight = CW_Constant;
1773 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1774 if (C->getZExtValue() == 0) {
1775 weight = CW_Constant;
1780 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1781 if (isUInt<8>(C->getZExtValue())) {
1782 weight = CW_Constant;
1787 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1788 if (C->getSExtValue() == -1) {
1789 weight = CW_Constant;
1794 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1795 if ((C->getZExtValue() == 8) || (C->getZExtValue() == 16) ||
1796 (C->getZExtValue() == 24)) {
1797 weight = CW_Constant;
1802 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1803 if (C->getZExtValue() == 1) {
1804 weight = CW_Constant;
1809 if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1810 if ((C->getSExtValue() >= -6) && (C->getSExtValue() <= 5)) {
1811 weight = CW_Constant;
1823 std::pair<unsigned, const TargetRegisterClass *>
1824 AVRTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1825 StringRef Constraint,
1827 auto STI = static_cast<const AVRTargetMachine &>(this->getTargetMachine())
1828 .getSubtargetImpl();
1830 // We only support i8 and i16.
1832 //:FIXME: remove this assert for now since it gets sometimes executed
1833 // assert((VT == MVT::i16 || VT == MVT::i8) && "Wrong operand type.");
1835 if (Constraint.size() == 1) {
1836 switch (Constraint[0]) {
1837 case 'a': // Simple upper registers r16..r23.
1838 return std::make_pair(0U, &AVR::LD8loRegClass);
1839 case 'b': // Base pointer registers: y, z.
1840 return std::make_pair(0U, &AVR::PTRDISPREGSRegClass);
1841 case 'd': // Upper registers r16..r31.
1842 return std::make_pair(0U, &AVR::LD8RegClass);
1843 case 'l': // Lower registers r0..r15.
1844 return std::make_pair(0U, &AVR::GPR8loRegClass);
1845 case 'e': // Pointer register pairs: x, y, z.
1846 return std::make_pair(0U, &AVR::PTRREGSRegClass);
1847 case 'q': // Stack pointer register: SPH:SPL.
1848 return std::make_pair(0U, &AVR::GPRSPRegClass);
1849 case 'r': // Any register: r0..r31.
1851 return std::make_pair(0U, &AVR::GPR8RegClass);
1853 assert(VT == MVT::i16 && "inline asm constraint too large");
1854 return std::make_pair(0U, &AVR::DREGSRegClass);
1855 case 't': // Temporary register: r0.
1856 return std::make_pair(unsigned(AVR::R0), &AVR::GPR8RegClass);
1857 case 'w': // Special upper register pairs: r24, r26, r28, r30.
1858 return std::make_pair(0U, &AVR::IWREGSRegClass);
1859 case 'x': // Pointer register pair X: r27:r26.
1861 return std::make_pair(unsigned(AVR::R27R26), &AVR::PTRREGSRegClass);
1862 case 'y': // Pointer register pair Y: r29:r28.
1864 return std::make_pair(unsigned(AVR::R29R28), &AVR::PTRREGSRegClass);
1865 case 'z': // Pointer register pair Z: r31:r30.
1867 return std::make_pair(unsigned(AVR::R31R30), &AVR::PTRREGSRegClass);
1873 return TargetLowering::getRegForInlineAsmConstraint(STI->getRegisterInfo(),
1877 void AVRTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
1878 std::string &Constraint,
1879 std::vector<SDValue> &Ops,
1880 SelectionDAG &DAG) const {
1881 SDValue Result(0, 0);
1883 EVT Ty = Op.getValueType();
1885 // Currently only support length 1 constraints.
1886 if (Constraint.length() != 1) {
1890 char ConstraintLetter = Constraint[0];
1891 switch (ConstraintLetter) {
1894 // Deal with integers first:
1904 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1909 int64_t CVal64 = C->getSExtValue();
1910 uint64_t CUVal64 = C->getZExtValue();
1911 switch (ConstraintLetter) {
1913 if (!isUInt<6>(CUVal64))
1915 Result = DAG.getTargetConstant(CUVal64, DL, Ty);
1918 if (CVal64 < -63 || CVal64 > 0)
1920 Result = DAG.getTargetConstant(CVal64, DL, Ty);
1925 Result = DAG.getTargetConstant(CUVal64, DL, Ty);
1930 Result = DAG.getTargetConstant(CUVal64, DL, Ty);
1933 if (!isUInt<8>(CUVal64))
1935 // i8 type may be printed as a negative number,
1936 // e.g. 254 would be printed as -2,
1937 // so we force it to i16 at least.
1938 if (Ty.getSimpleVT() == MVT::i8) {
1941 Result = DAG.getTargetConstant(CUVal64, DL, Ty);
1946 Result = DAG.getTargetConstant(CVal64, DL, Ty);
1948 case 'O': // 8, 16, 24
1949 if (CUVal64 != 8 && CUVal64 != 16 && CUVal64 != 24)
1951 Result = DAG.getTargetConstant(CUVal64, DL, Ty);
1956 Result = DAG.getTargetConstant(CUVal64, DL, Ty);
1959 if (CVal64 < -6 || CVal64 > 5)
1961 Result = DAG.getTargetConstant(CVal64, DL, Ty);
1968 const ConstantFPSDNode *FC = dyn_cast<ConstantFPSDNode>(Op);
1969 if (!FC || !FC->isZero())
1971 // Soften float to i8 0
1972 Result = DAG.getTargetConstant(0, DL, MVT::i8);
1976 if (Result.getNode()) {
1977 Ops.push_back(Result);
1981 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
1984 unsigned AVRTargetLowering::getRegisterByName(const char *RegName,
1986 SelectionDAG &DAG) const {
1989 if (VT == MVT::i8) {
1990 Reg = StringSwitch<unsigned>(RegName)
1991 .Case("r0", AVR::R0).Case("r1", AVR::R1).Case("r2", AVR::R2)
1992 .Case("r3", AVR::R3).Case("r4", AVR::R4).Case("r5", AVR::R5)
1993 .Case("r6", AVR::R6).Case("r7", AVR::R7).Case("r8", AVR::R8)
1994 .Case("r9", AVR::R9).Case("r10", AVR::R10).Case("r11", AVR::R11)
1995 .Case("r12", AVR::R12).Case("r13", AVR::R13).Case("r14", AVR::R14)
1996 .Case("r15", AVR::R15).Case("r16", AVR::R16).Case("r17", AVR::R17)
1997 .Case("r18", AVR::R18).Case("r19", AVR::R19).Case("r20", AVR::R20)
1998 .Case("r21", AVR::R21).Case("r22", AVR::R22).Case("r23", AVR::R23)
1999 .Case("r24", AVR::R24).Case("r25", AVR::R25).Case("r26", AVR::R26)
2000 .Case("r27", AVR::R27).Case("r28", AVR::R28).Case("r29", AVR::R29)
2001 .Case("r30", AVR::R30).Case("r31", AVR::R31)
2002 .Case("X", AVR::R27R26).Case("Y", AVR::R29R28).Case("Z", AVR::R31R30)
2005 Reg = StringSwitch<unsigned>(RegName)
2006 .Case("r0", AVR::R1R0).Case("r2", AVR::R3R2)
2007 .Case("r4", AVR::R5R4).Case("r6", AVR::R7R6)
2008 .Case("r8", AVR::R9R8).Case("r10", AVR::R11R10)
2009 .Case("r12", AVR::R13R12).Case("r14", AVR::R15R14)
2010 .Case("r16", AVR::R17R16).Case("r18", AVR::R19R18)
2011 .Case("r20", AVR::R21R20).Case("r22", AVR::R23R22)
2012 .Case("r24", AVR::R25R24).Case("r26", AVR::R27R26)
2013 .Case("r28", AVR::R29R28).Case("r30", AVR::R31R30)
2014 .Case("X", AVR::R27R26).Case("Y", AVR::R29R28).Case("Z", AVR::R31R30)
2021 report_fatal_error("Invalid register name global variable");
2024 } // end of namespace llvm