xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Target/AVR/AVRISelLowering.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 //===-- AVRISelLowering.cpp - AVR DAG Lowering Implementation -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interfaces that AVR uses to lower LLVM code into a
10 // selection DAG.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AVRISelLowering.h"
15 
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/ADT/STLExtras.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"
26 
27 #include "AVR.h"
28 #include "AVRMachineFunctionInfo.h"
29 #include "AVRSubtarget.h"
30 #include "AVRTargetMachine.h"
31 #include "MCTargetDesc/AVRMCTargetDesc.h"
32 
33 namespace llvm {
34 
AVRTargetLowering(const AVRTargetMachine & TM,const AVRSubtarget & STI)35 AVRTargetLowering::AVRTargetLowering(const AVRTargetMachine &TM,
36                                      const AVRSubtarget &STI)
37     : TargetLowering(TM), Subtarget(STI) {
38   // Set up the register classes.
39   addRegisterClass(MVT::i8, &AVR::GPR8RegClass);
40   addRegisterClass(MVT::i16, &AVR::DREGSRegClass);
41 
42   // Compute derived properties from the register classes.
43   computeRegisterProperties(Subtarget.getRegisterInfo());
44 
45   setBooleanContents(ZeroOrOneBooleanContent);
46   setBooleanVectorContents(ZeroOrOneBooleanContent);
47   setSchedulingPreference(Sched::RegPressure);
48   setStackPointerRegisterToSaveRestore(AVR::SP);
49   setSupportsUnalignedAtomics(true);
50 
51   setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
52   setOperationAction(ISD::BlockAddress, MVT::i16, Custom);
53 
54   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
55   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
56   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
57   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
58 
59   for (MVT VT : MVT::integer_valuetypes()) {
60     for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) {
61       setLoadExtAction(N, VT, MVT::i1, Promote);
62       setLoadExtAction(N, VT, MVT::i8, Expand);
63     }
64   }
65 
66   setTruncStoreAction(MVT::i16, MVT::i8, Expand);
67 
68   for (MVT VT : MVT::integer_valuetypes()) {
69     setOperationAction(ISD::ADDC, VT, Legal);
70     setOperationAction(ISD::SUBC, VT, Legal);
71     setOperationAction(ISD::ADDE, VT, Legal);
72     setOperationAction(ISD::SUBE, VT, Legal);
73   }
74 
75   // sub (x, imm) gets canonicalized to add (x, -imm), so for illegal types
76   // revert into a sub since we don't have an add with immediate instruction.
77   setOperationAction(ISD::ADD, MVT::i32, Custom);
78   setOperationAction(ISD::ADD, MVT::i64, Custom);
79 
80   // our shift instructions are only able to shift 1 bit at a time, so handle
81   // this in a custom way.
82   setOperationAction(ISD::SRA, MVT::i8, Custom);
83   setOperationAction(ISD::SHL, MVT::i8, Custom);
84   setOperationAction(ISD::SRL, MVT::i8, Custom);
85   setOperationAction(ISD::SRA, MVT::i16, Custom);
86   setOperationAction(ISD::SHL, MVT::i16, Custom);
87   setOperationAction(ISD::SRL, MVT::i16, Custom);
88   setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
89   setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
90   setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
91 
92   setOperationAction(ISD::ROTL, MVT::i8, Custom);
93   setOperationAction(ISD::ROTL, MVT::i16, Expand);
94   setOperationAction(ISD::ROTR, MVT::i8, Custom);
95   setOperationAction(ISD::ROTR, MVT::i16, Expand);
96 
97   setOperationAction(ISD::BR_CC, MVT::i8, Custom);
98   setOperationAction(ISD::BR_CC, MVT::i16, Custom);
99   setOperationAction(ISD::BR_CC, MVT::i32, Custom);
100   setOperationAction(ISD::BR_CC, MVT::i64, Custom);
101   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
102 
103   setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
104   setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
105   setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
106   setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
107   setOperationAction(ISD::SETCC, MVT::i8, Custom);
108   setOperationAction(ISD::SETCC, MVT::i16, Custom);
109   setOperationAction(ISD::SETCC, MVT::i32, Custom);
110   setOperationAction(ISD::SETCC, MVT::i64, Custom);
111   setOperationAction(ISD::SELECT, MVT::i8, Expand);
112   setOperationAction(ISD::SELECT, MVT::i16, Expand);
113 
114   setOperationAction(ISD::BSWAP, MVT::i16, Expand);
115 
116   // Add support for postincrement and predecrement load/stores.
117   setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
118   setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
119   setIndexedLoadAction(ISD::PRE_DEC, MVT::i8, Legal);
120   setIndexedLoadAction(ISD::PRE_DEC, MVT::i16, Legal);
121   setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal);
122   setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal);
123   setIndexedStoreAction(ISD::PRE_DEC, MVT::i8, Legal);
124   setIndexedStoreAction(ISD::PRE_DEC, MVT::i16, Legal);
125 
126   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
127 
128   setOperationAction(ISD::VASTART, MVT::Other, Custom);
129   setOperationAction(ISD::VAEND, MVT::Other, Expand);
130   setOperationAction(ISD::VAARG, MVT::Other, Expand);
131   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
132 
133   // Atomic operations which must be lowered to rtlib calls
134   for (MVT VT : MVT::integer_valuetypes()) {
135     setOperationAction(ISD::ATOMIC_SWAP, VT, Expand);
136     setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Expand);
137     setOperationAction(ISD::ATOMIC_LOAD_NAND, VT, Expand);
138     setOperationAction(ISD::ATOMIC_LOAD_MAX, VT, Expand);
139     setOperationAction(ISD::ATOMIC_LOAD_MIN, VT, Expand);
140     setOperationAction(ISD::ATOMIC_LOAD_UMAX, VT, Expand);
141     setOperationAction(ISD::ATOMIC_LOAD_UMIN, VT, Expand);
142   }
143 
144   // Division/remainder
145   setOperationAction(ISD::UDIV, MVT::i8, Expand);
146   setOperationAction(ISD::UDIV, MVT::i16, Expand);
147   setOperationAction(ISD::UREM, MVT::i8, Expand);
148   setOperationAction(ISD::UREM, MVT::i16, Expand);
149   setOperationAction(ISD::SDIV, MVT::i8, Expand);
150   setOperationAction(ISD::SDIV, MVT::i16, Expand);
151   setOperationAction(ISD::SREM, MVT::i8, Expand);
152   setOperationAction(ISD::SREM, MVT::i16, Expand);
153 
154   // Make division and modulus custom
155   setOperationAction(ISD::UDIVREM, MVT::i8, Custom);
156   setOperationAction(ISD::UDIVREM, MVT::i16, Custom);
157   setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
158   setOperationAction(ISD::SDIVREM, MVT::i8, Custom);
159   setOperationAction(ISD::SDIVREM, MVT::i16, Custom);
160   setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
161 
162   // Do not use MUL. The AVR instructions are closer to SMUL_LOHI &co.
163   setOperationAction(ISD::MUL, MVT::i8, Expand);
164   setOperationAction(ISD::MUL, MVT::i16, Expand);
165 
166   // Expand 16 bit multiplications.
167   setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
168   setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
169 
170   // Expand multiplications to libcalls when there is
171   // no hardware MUL.
172   if (!Subtarget.supportsMultiplication()) {
173     setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
174     setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
175   }
176 
177   for (MVT VT : MVT::integer_valuetypes()) {
178     setOperationAction(ISD::MULHS, VT, Expand);
179     setOperationAction(ISD::MULHU, VT, Expand);
180   }
181 
182   for (MVT VT : MVT::integer_valuetypes()) {
183     setOperationAction(ISD::CTPOP, VT, Expand);
184     setOperationAction(ISD::CTLZ, VT, Expand);
185     setOperationAction(ISD::CTTZ, VT, Expand);
186   }
187 
188   for (MVT VT : MVT::integer_valuetypes()) {
189     setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
190     // TODO: The generated code is pretty poor. Investigate using the
191     // same "shift and subtract with carry" trick that we do for
192     // extending 8-bit to 16-bit. This may require infrastructure
193     // improvements in how we treat 16-bit "registers" to be feasible.
194   }
195 
196   // Division rtlib functions (not supported), use divmod functions instead
197   setLibcallName(RTLIB::SDIV_I8, nullptr);
198   setLibcallName(RTLIB::SDIV_I16, nullptr);
199   setLibcallName(RTLIB::SDIV_I32, nullptr);
200   setLibcallName(RTLIB::UDIV_I8, nullptr);
201   setLibcallName(RTLIB::UDIV_I16, nullptr);
202   setLibcallName(RTLIB::UDIV_I32, nullptr);
203 
204   // Modulus rtlib functions (not supported), use divmod functions instead
205   setLibcallName(RTLIB::SREM_I8, nullptr);
206   setLibcallName(RTLIB::SREM_I16, nullptr);
207   setLibcallName(RTLIB::SREM_I32, nullptr);
208   setLibcallName(RTLIB::UREM_I8, nullptr);
209   setLibcallName(RTLIB::UREM_I16, nullptr);
210   setLibcallName(RTLIB::UREM_I32, nullptr);
211 
212   // Division and modulus rtlib functions
213   setLibcallName(RTLIB::SDIVREM_I8, "__divmodqi4");
214   setLibcallName(RTLIB::SDIVREM_I16, "__divmodhi4");
215   setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
216   setLibcallName(RTLIB::UDIVREM_I8, "__udivmodqi4");
217   setLibcallName(RTLIB::UDIVREM_I16, "__udivmodhi4");
218   setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
219 
220   // Several of the runtime library functions use a special calling conv
221   setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::AVR_BUILTIN);
222   setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::AVR_BUILTIN);
223   setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::AVR_BUILTIN);
224   setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::AVR_BUILTIN);
225 
226   // Trigonometric rtlib functions
227   setLibcallName(RTLIB::SIN_F32, "sin");
228   setLibcallName(RTLIB::COS_F32, "cos");
229 
230   setMinFunctionAlignment(Align(2));
231   setMinimumJumpTableEntries(UINT_MAX);
232 }
233 
getTargetNodeName(unsigned Opcode) const234 const char *AVRTargetLowering::getTargetNodeName(unsigned Opcode) const {
235 #define NODE(name)       \
236   case AVRISD::name:     \
237     return #name
238 
239   switch (Opcode) {
240   default:
241     return nullptr;
242     NODE(RET_FLAG);
243     NODE(RETI_FLAG);
244     NODE(CALL);
245     NODE(WRAPPER);
246     NODE(LSL);
247     NODE(LSR);
248     NODE(ROL);
249     NODE(ROR);
250     NODE(ASR);
251     NODE(LSLLOOP);
252     NODE(LSRLOOP);
253     NODE(ROLLOOP);
254     NODE(RORLOOP);
255     NODE(ASRLOOP);
256     NODE(BRCOND);
257     NODE(CMP);
258     NODE(CMPC);
259     NODE(TST);
260     NODE(SELECT_CC);
261 #undef NODE
262   }
263 }
264 
getSetCCResultType(const DataLayout & DL,LLVMContext &,EVT VT) const265 EVT AVRTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
266                                           EVT VT) const {
267   assert(!VT.isVector() && "No AVR SetCC type for vectors!");
268   return MVT::i8;
269 }
270 
LowerShifts(SDValue Op,SelectionDAG & DAG) const271 SDValue AVRTargetLowering::LowerShifts(SDValue Op, SelectionDAG &DAG) const {
272   //:TODO: this function has to be completely rewritten to produce optimal
273   // code, for now it's producing very long but correct code.
274   unsigned Opc8;
275   const SDNode *N = Op.getNode();
276   EVT VT = Op.getValueType();
277   SDLoc dl(N);
278   assert(isPowerOf2_32(VT.getSizeInBits()) &&
279          "Expected power-of-2 shift amount");
280 
281   // Expand non-constant shifts to loops.
282   if (!isa<ConstantSDNode>(N->getOperand(1))) {
283     switch (Op.getOpcode()) {
284     default:
285       llvm_unreachable("Invalid shift opcode!");
286     case ISD::SHL:
287       return DAG.getNode(AVRISD::LSLLOOP, dl, VT, N->getOperand(0),
288                          N->getOperand(1));
289     case ISD::SRL:
290       return DAG.getNode(AVRISD::LSRLOOP, dl, VT, N->getOperand(0),
291                          N->getOperand(1));
292     case ISD::ROTL: {
293       SDValue Amt = N->getOperand(1);
294       EVT AmtVT = Amt.getValueType();
295       Amt = DAG.getNode(ISD::AND, dl, AmtVT, Amt,
296                         DAG.getConstant(VT.getSizeInBits() - 1, dl, AmtVT));
297       return DAG.getNode(AVRISD::ROLLOOP, dl, VT, N->getOperand(0), Amt);
298     }
299     case ISD::ROTR: {
300       SDValue Amt = N->getOperand(1);
301       EVT AmtVT = Amt.getValueType();
302       Amt = DAG.getNode(ISD::AND, dl, AmtVT, Amt,
303                         DAG.getConstant(VT.getSizeInBits() - 1, dl, AmtVT));
304       return DAG.getNode(AVRISD::RORLOOP, dl, VT, N->getOperand(0), Amt);
305     }
306     case ISD::SRA:
307       return DAG.getNode(AVRISD::ASRLOOP, dl, VT, N->getOperand(0),
308                          N->getOperand(1));
309     }
310   }
311 
312   uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
313   SDValue Victim = N->getOperand(0);
314 
315   switch (Op.getOpcode()) {
316   case ISD::SRA:
317     Opc8 = AVRISD::ASR;
318     break;
319   case ISD::ROTL:
320     Opc8 = AVRISD::ROL;
321     ShiftAmount = ShiftAmount % VT.getSizeInBits();
322     break;
323   case ISD::ROTR:
324     Opc8 = AVRISD::ROR;
325     ShiftAmount = ShiftAmount % VT.getSizeInBits();
326     break;
327   case ISD::SRL:
328     Opc8 = AVRISD::LSR;
329     break;
330   case ISD::SHL:
331     Opc8 = AVRISD::LSL;
332     break;
333   default:
334     llvm_unreachable("Invalid shift opcode");
335   }
336 
337   // Optimize int8/int16 shifts.
338   if (VT.getSizeInBits() == 8) {
339     if (Op.getOpcode() == ISD::SHL && 4 <= ShiftAmount && ShiftAmount < 7) {
340       // Optimize LSL when 4 <= ShiftAmount <= 6.
341       Victim = DAG.getNode(AVRISD::SWAP, dl, VT, Victim);
342       Victim =
343           DAG.getNode(ISD::AND, dl, VT, Victim, DAG.getConstant(0xf0, dl, VT));
344       ShiftAmount -= 4;
345     } else if (Op.getOpcode() == ISD::SRL && 4 <= ShiftAmount &&
346                ShiftAmount < 7) {
347       // Optimize LSR when 4 <= ShiftAmount <= 6.
348       Victim = DAG.getNode(AVRISD::SWAP, dl, VT, Victim);
349       Victim =
350           DAG.getNode(ISD::AND, dl, VT, Victim, DAG.getConstant(0x0f, dl, VT));
351       ShiftAmount -= 4;
352     } else if (Op.getOpcode() == ISD::SHL && ShiftAmount == 7) {
353       // Optimize LSL when ShiftAmount == 7.
354       Victim = DAG.getNode(AVRISD::LSL7, dl, VT, Victim);
355       ShiftAmount = 0;
356     } else if (Op.getOpcode() == ISD::SRL && ShiftAmount == 7) {
357       // Optimize LSR when ShiftAmount == 7.
358       Victim = DAG.getNode(AVRISD::LSR7, dl, VT, Victim);
359       ShiftAmount = 0;
360     } else if (Op.getOpcode() == ISD::SRA && ShiftAmount == 7) {
361       // Optimize ASR when ShiftAmount == 7.
362       Victim = DAG.getNode(AVRISD::ASR7, dl, VT, Victim);
363       ShiftAmount = 0;
364     }
365   } else if (VT.getSizeInBits() == 16) {
366     if (4 <= ShiftAmount && ShiftAmount < 8)
367       switch (Op.getOpcode()) {
368       case ISD::SHL:
369         Victim = DAG.getNode(AVRISD::LSL4, dl, VT, Victim);
370         ShiftAmount -= 4;
371         break;
372       case ISD::SRL:
373         Victim = DAG.getNode(AVRISD::LSR4, dl, VT, Victim);
374         ShiftAmount -= 4;
375         break;
376       default:
377         break;
378       }
379     else if (8 <= ShiftAmount && ShiftAmount < 12)
380       switch (Op.getOpcode()) {
381       case ISD::SHL:
382         Victim = DAG.getNode(AVRISD::LSL8, dl, VT, Victim);
383         ShiftAmount -= 8;
384         break;
385       case ISD::SRL:
386         Victim = DAG.getNode(AVRISD::LSR8, dl, VT, Victim);
387         ShiftAmount -= 8;
388         break;
389       case ISD::SRA:
390         Victim = DAG.getNode(AVRISD::ASR8, dl, VT, Victim);
391         ShiftAmount -= 8;
392         break;
393       default:
394         break;
395       }
396     else if (12 <= ShiftAmount)
397       switch (Op.getOpcode()) {
398       case ISD::SHL:
399         Victim = DAG.getNode(AVRISD::LSL12, dl, VT, Victim);
400         ShiftAmount -= 12;
401         break;
402       case ISD::SRL:
403         Victim = DAG.getNode(AVRISD::LSR12, dl, VT, Victim);
404         ShiftAmount -= 12;
405         break;
406       default:
407         break;
408       }
409   }
410 
411   while (ShiftAmount--) {
412     Victim = DAG.getNode(Opc8, dl, VT, Victim);
413   }
414 
415   return Victim;
416 }
417 
LowerDivRem(SDValue Op,SelectionDAG & DAG) const418 SDValue AVRTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
419   unsigned Opcode = Op->getOpcode();
420   assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
421          "Invalid opcode for Div/Rem lowering");
422   bool IsSigned = (Opcode == ISD::SDIVREM);
423   EVT VT = Op->getValueType(0);
424   Type *Ty = VT.getTypeForEVT(*DAG.getContext());
425 
426   RTLIB::Libcall LC;
427   switch (VT.getSimpleVT().SimpleTy) {
428   default:
429     llvm_unreachable("Unexpected request for libcall!");
430   case MVT::i8:
431     LC = IsSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8;
432     break;
433   case MVT::i16:
434     LC = IsSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16;
435     break;
436   case MVT::i32:
437     LC = IsSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32;
438     break;
439   }
440 
441   SDValue InChain = DAG.getEntryNode();
442 
443   TargetLowering::ArgListTy Args;
444   TargetLowering::ArgListEntry Entry;
445   for (SDValue const &Value : Op->op_values()) {
446     Entry.Node = Value;
447     Entry.Ty = Value.getValueType().getTypeForEVT(*DAG.getContext());
448     Entry.IsSExt = IsSigned;
449     Entry.IsZExt = !IsSigned;
450     Args.push_back(Entry);
451   }
452 
453   SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
454                                          getPointerTy(DAG.getDataLayout()));
455 
456   Type *RetTy = (Type *)StructType::get(Ty, Ty);
457 
458   SDLoc dl(Op);
459   TargetLowering::CallLoweringInfo CLI(DAG);
460   CLI.setDebugLoc(dl)
461       .setChain(InChain)
462       .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
463       .setInRegister()
464       .setSExtResult(IsSigned)
465       .setZExtResult(!IsSigned);
466 
467   std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
468   return CallInfo.first;
469 }
470 
LowerGlobalAddress(SDValue Op,SelectionDAG & DAG) const471 SDValue AVRTargetLowering::LowerGlobalAddress(SDValue Op,
472                                               SelectionDAG &DAG) const {
473   auto DL = DAG.getDataLayout();
474 
475   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
476   int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
477 
478   // Create the TargetGlobalAddress node, folding in the constant offset.
479   SDValue Result =
480       DAG.getTargetGlobalAddress(GV, SDLoc(Op), getPointerTy(DL), Offset);
481   return DAG.getNode(AVRISD::WRAPPER, SDLoc(Op), getPointerTy(DL), Result);
482 }
483 
LowerBlockAddress(SDValue Op,SelectionDAG & DAG) const484 SDValue AVRTargetLowering::LowerBlockAddress(SDValue Op,
485                                              SelectionDAG &DAG) const {
486   auto DL = DAG.getDataLayout();
487   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
488 
489   SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(DL));
490 
491   return DAG.getNode(AVRISD::WRAPPER, SDLoc(Op), getPointerTy(DL), Result);
492 }
493 
494 /// IntCCToAVRCC - Convert a DAG integer condition code to an AVR CC.
intCCToAVRCC(ISD::CondCode CC)495 static AVRCC::CondCodes intCCToAVRCC(ISD::CondCode CC) {
496   switch (CC) {
497   default:
498     llvm_unreachable("Unknown condition code!");
499   case ISD::SETEQ:
500     return AVRCC::COND_EQ;
501   case ISD::SETNE:
502     return AVRCC::COND_NE;
503   case ISD::SETGE:
504     return AVRCC::COND_GE;
505   case ISD::SETLT:
506     return AVRCC::COND_LT;
507   case ISD::SETUGE:
508     return AVRCC::COND_SH;
509   case ISD::SETULT:
510     return AVRCC::COND_LO;
511   }
512 }
513 
514 /// Returns appropriate CP/CPI/CPC nodes code for the given 8/16-bit operands.
getAVRCmp(SDValue LHS,SDValue RHS,SelectionDAG & DAG,SDLoc DL) const515 SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS,
516                                      SelectionDAG &DAG, SDLoc DL) const {
517   assert((LHS.getSimpleValueType() == RHS.getSimpleValueType()) &&
518          "LHS and RHS have different types");
519   assert(((LHS.getSimpleValueType() == MVT::i16) ||
520           (LHS.getSimpleValueType() == MVT::i8)) && "invalid comparison type");
521 
522   SDValue Cmp;
523 
524   if (LHS.getSimpleValueType() == MVT::i16 && isa<ConstantSDNode>(RHS)) {
525     // Generate a CPI/CPC pair if RHS is a 16-bit constant.
526     SDValue LHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHS,
527                                 DAG.getIntPtrConstant(0, DL));
528     SDValue LHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHS,
529                                 DAG.getIntPtrConstant(1, DL));
530     SDValue RHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, RHS,
531                                 DAG.getIntPtrConstant(0, DL));
532     SDValue RHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, RHS,
533                                 DAG.getIntPtrConstant(1, DL));
534     Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHSlo, RHSlo);
535     Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHShi, RHShi, Cmp);
536   } else {
537     // Generate ordinary 16-bit comparison.
538     Cmp = DAG.getNode(AVRISD::CMP, DL, MVT::Glue, LHS, RHS);
539   }
540 
541   return Cmp;
542 }
543 
544 /// Returns appropriate AVR CMP/CMPC nodes and corresponding condition code for
545 /// the given operands.
getAVRCmp(SDValue LHS,SDValue RHS,ISD::CondCode CC,SDValue & AVRcc,SelectionDAG & DAG,SDLoc DL) const546 SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
547                                      SDValue &AVRcc, SelectionDAG &DAG,
548                                      SDLoc DL) const {
549   SDValue Cmp;
550   EVT VT = LHS.getValueType();
551   bool UseTest = false;
552 
553   switch (CC) {
554   default:
555     break;
556   case ISD::SETLE: {
557     // Swap operands and reverse the branching condition.
558     std::swap(LHS, RHS);
559     CC = ISD::SETGE;
560     break;
561   }
562   case ISD::SETGT: {
563     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
564       switch (C->getSExtValue()) {
565       case -1: {
566         // When doing lhs > -1 use a tst instruction on the top part of lhs
567         // and use brpl instead of using a chain of cp/cpc.
568         UseTest = true;
569         AVRcc = DAG.getConstant(AVRCC::COND_PL, DL, MVT::i8);
570         break;
571       }
572       case 0: {
573         // Turn lhs > 0 into 0 < lhs since 0 can be materialized with
574         // __zero_reg__ in lhs.
575         RHS = LHS;
576         LHS = DAG.getConstant(0, DL, VT);
577         CC = ISD::SETLT;
578         break;
579       }
580       default: {
581         // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows
582         // us to  fold the constant into the cmp instruction.
583         RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
584         CC = ISD::SETGE;
585         break;
586       }
587       }
588       break;
589     }
590     // Swap operands and reverse the branching condition.
591     std::swap(LHS, RHS);
592     CC = ISD::SETLT;
593     break;
594   }
595   case ISD::SETLT: {
596     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
597       switch (C->getSExtValue()) {
598       case 1: {
599         // Turn lhs < 1 into 0 >= lhs since 0 can be materialized with
600         // __zero_reg__ in lhs.
601         RHS = LHS;
602         LHS = DAG.getConstant(0, DL, VT);
603         CC = ISD::SETGE;
604         break;
605       }
606       case 0: {
607         // When doing lhs < 0 use a tst instruction on the top part of lhs
608         // and use brmi instead of using a chain of cp/cpc.
609         UseTest = true;
610         AVRcc = DAG.getConstant(AVRCC::COND_MI, DL, MVT::i8);
611         break;
612       }
613       }
614     }
615     break;
616   }
617   case ISD::SETULE: {
618     // Swap operands and reverse the branching condition.
619     std::swap(LHS, RHS);
620     CC = ISD::SETUGE;
621     break;
622   }
623   case ISD::SETUGT: {
624     // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
625     // fold the constant into the cmp instruction.
626     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
627       RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
628       CC = ISD::SETUGE;
629       break;
630     }
631     // Swap operands and reverse the branching condition.
632     std::swap(LHS, RHS);
633     CC = ISD::SETULT;
634     break;
635   }
636   }
637 
638   // Expand 32 and 64 bit comparisons with custom CMP and CMPC nodes instead of
639   // using the default and/or/xor expansion code which is much longer.
640   if (VT == MVT::i32) {
641     SDValue LHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS,
642                                 DAG.getIntPtrConstant(0, DL));
643     SDValue LHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS,
644                                 DAG.getIntPtrConstant(1, DL));
645     SDValue RHSlo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS,
646                                 DAG.getIntPtrConstant(0, DL));
647     SDValue RHShi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS,
648                                 DAG.getIntPtrConstant(1, DL));
649 
650     if (UseTest) {
651       // When using tst we only care about the highest part.
652       SDValue Top = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHShi,
653                                 DAG.getIntPtrConstant(1, DL));
654       Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue, Top);
655     } else {
656       Cmp = getAVRCmp(LHSlo, RHSlo, DAG, DL);
657       Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHShi, RHShi, Cmp);
658     }
659   } else if (VT == MVT::i64) {
660     SDValue LHS_0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS,
661                                 DAG.getIntPtrConstant(0, DL));
662     SDValue LHS_1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS,
663                                 DAG.getIntPtrConstant(1, DL));
664 
665     SDValue LHS0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_0,
666                                DAG.getIntPtrConstant(0, DL));
667     SDValue LHS1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_0,
668                                DAG.getIntPtrConstant(1, DL));
669     SDValue LHS2 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_1,
670                                DAG.getIntPtrConstant(0, DL));
671     SDValue LHS3 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, LHS_1,
672                                DAG.getIntPtrConstant(1, DL));
673 
674     SDValue RHS_0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS,
675                                 DAG.getIntPtrConstant(0, DL));
676     SDValue RHS_1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS,
677                                 DAG.getIntPtrConstant(1, DL));
678 
679     SDValue RHS0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_0,
680                                DAG.getIntPtrConstant(0, DL));
681     SDValue RHS1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_0,
682                                DAG.getIntPtrConstant(1, DL));
683     SDValue RHS2 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_1,
684                                DAG.getIntPtrConstant(0, DL));
685     SDValue RHS3 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i16, RHS_1,
686                                DAG.getIntPtrConstant(1, DL));
687 
688     if (UseTest) {
689       // When using tst we only care about the highest part.
690       SDValue Top = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8, LHS3,
691                                 DAG.getIntPtrConstant(1, DL));
692       Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue, Top);
693     } else {
694       Cmp = getAVRCmp(LHS0, RHS0, DAG, DL);
695       Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS1, RHS1, Cmp);
696       Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS2, RHS2, Cmp);
697       Cmp = DAG.getNode(AVRISD::CMPC, DL, MVT::Glue, LHS3, RHS3, Cmp);
698     }
699   } else if (VT == MVT::i8 || VT == MVT::i16) {
700     if (UseTest) {
701       // When using tst we only care about the highest part.
702       Cmp = DAG.getNode(AVRISD::TST, DL, MVT::Glue,
703                         (VT == MVT::i8)
704                             ? LHS
705                             : DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i8,
706                                           LHS, DAG.getIntPtrConstant(1, DL)));
707     } else {
708       Cmp = getAVRCmp(LHS, RHS, DAG, DL);
709     }
710   } else {
711     llvm_unreachable("Invalid comparison size");
712   }
713 
714   // When using a test instruction AVRcc is already set.
715   if (!UseTest) {
716     AVRcc = DAG.getConstant(intCCToAVRCC(CC), DL, MVT::i8);
717   }
718 
719   return Cmp;
720 }
721 
LowerBR_CC(SDValue Op,SelectionDAG & DAG) const722 SDValue AVRTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
723   SDValue Chain = Op.getOperand(0);
724   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
725   SDValue LHS = Op.getOperand(2);
726   SDValue RHS = Op.getOperand(3);
727   SDValue Dest = Op.getOperand(4);
728   SDLoc dl(Op);
729 
730   SDValue TargetCC;
731   SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, dl);
732 
733   return DAG.getNode(AVRISD::BRCOND, dl, MVT::Other, Chain, Dest, TargetCC,
734                      Cmp);
735 }
736 
LowerSELECT_CC(SDValue Op,SelectionDAG & DAG) const737 SDValue AVRTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
738   SDValue LHS = Op.getOperand(0);
739   SDValue RHS = Op.getOperand(1);
740   SDValue TrueV = Op.getOperand(2);
741   SDValue FalseV = Op.getOperand(3);
742   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
743   SDLoc dl(Op);
744 
745   SDValue TargetCC;
746   SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, dl);
747 
748   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
749   SDValue Ops[] = {TrueV, FalseV, TargetCC, Cmp};
750 
751   return DAG.getNode(AVRISD::SELECT_CC, dl, VTs, Ops);
752 }
753 
LowerSETCC(SDValue Op,SelectionDAG & DAG) const754 SDValue AVRTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
755   SDValue LHS = Op.getOperand(0);
756   SDValue RHS = Op.getOperand(1);
757   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
758   SDLoc DL(Op);
759 
760   SDValue TargetCC;
761   SDValue Cmp = getAVRCmp(LHS, RHS, CC, TargetCC, DAG, DL);
762 
763   SDValue TrueV = DAG.getConstant(1, DL, Op.getValueType());
764   SDValue FalseV = DAG.getConstant(0, DL, Op.getValueType());
765   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
766   SDValue Ops[] = {TrueV, FalseV, TargetCC, Cmp};
767 
768   return DAG.getNode(AVRISD::SELECT_CC, DL, VTs, Ops);
769 }
770 
LowerVASTART(SDValue Op,SelectionDAG & DAG) const771 SDValue AVRTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
772   const MachineFunction &MF = DAG.getMachineFunction();
773   const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
774   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
775   auto DL = DAG.getDataLayout();
776   SDLoc dl(Op);
777 
778   // Vastart just stores the address of the VarArgsFrameIndex slot into the
779   // memory location argument.
780   SDValue FI = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(), getPointerTy(DL));
781 
782   return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
783                       MachinePointerInfo(SV));
784 }
785 
LowerOperation(SDValue Op,SelectionDAG & DAG) const786 SDValue AVRTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
787   switch (Op.getOpcode()) {
788   default:
789     llvm_unreachable("Don't know how to custom lower this!");
790   case ISD::SHL:
791   case ISD::SRA:
792   case ISD::SRL:
793   case ISD::ROTL:
794   case ISD::ROTR:
795     return LowerShifts(Op, DAG);
796   case ISD::GlobalAddress:
797     return LowerGlobalAddress(Op, DAG);
798   case ISD::BlockAddress:
799     return LowerBlockAddress(Op, DAG);
800   case ISD::BR_CC:
801     return LowerBR_CC(Op, DAG);
802   case ISD::SELECT_CC:
803     return LowerSELECT_CC(Op, DAG);
804   case ISD::SETCC:
805     return LowerSETCC(Op, DAG);
806   case ISD::VASTART:
807     return LowerVASTART(Op, DAG);
808   case ISD::SDIVREM:
809   case ISD::UDIVREM:
810     return LowerDivRem(Op, DAG);
811   }
812 
813   return SDValue();
814 }
815 
816 /// Replace a node with an illegal result type
817 /// with a new node built out of custom code.
ReplaceNodeResults(SDNode * N,SmallVectorImpl<SDValue> & Results,SelectionDAG & DAG) const818 void AVRTargetLowering::ReplaceNodeResults(SDNode *N,
819                                            SmallVectorImpl<SDValue> &Results,
820                                            SelectionDAG &DAG) const {
821   SDLoc DL(N);
822 
823   switch (N->getOpcode()) {
824   case ISD::ADD: {
825     // Convert add (x, imm) into sub (x, -imm).
826     if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
827       SDValue Sub = DAG.getNode(
828           ISD::SUB, DL, N->getValueType(0), N->getOperand(0),
829           DAG.getConstant(-C->getAPIntValue(), DL, C->getValueType(0)));
830       Results.push_back(Sub);
831     }
832     break;
833   }
834   default: {
835     SDValue Res = LowerOperation(SDValue(N, 0), DAG);
836 
837     for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
838       Results.push_back(Res.getValue(I));
839 
840     break;
841   }
842   }
843 }
844 
845 /// Return true if the addressing mode represented
846 /// by AM is legal for this target, for a load/store of the specified type.
isLegalAddressingMode(const DataLayout & DL,const AddrMode & AM,Type * Ty,unsigned AS,Instruction * I) const847 bool AVRTargetLowering::isLegalAddressingMode(const DataLayout &DL,
848                                               const AddrMode &AM, Type *Ty,
849                                               unsigned AS, Instruction *I) const {
850   int64_t Offs = AM.BaseOffs;
851 
852   // Allow absolute addresses.
853   if (AM.BaseGV && !AM.HasBaseReg && AM.Scale == 0 && Offs == 0) {
854     return true;
855   }
856 
857   // Flash memory instructions only allow zero offsets.
858   if (isa<PointerType>(Ty) && AS == AVR::ProgramMemory) {
859     return false;
860   }
861 
862   // Allow reg+<6bit> offset.
863   if (Offs < 0)
864     Offs = -Offs;
865   if (AM.BaseGV == 0 && AM.HasBaseReg && AM.Scale == 0 && isUInt<6>(Offs)) {
866     return true;
867   }
868 
869   return false;
870 }
871 
872 /// Returns true by value, base pointer and
873 /// offset pointer and addressing mode by reference if the node's address
874 /// can be legally represented as pre-indexed load / store address.
getPreIndexedAddressParts(SDNode * N,SDValue & Base,SDValue & Offset,ISD::MemIndexedMode & AM,SelectionDAG & DAG) const875 bool AVRTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
876                                                   SDValue &Offset,
877                                                   ISD::MemIndexedMode &AM,
878                                                   SelectionDAG &DAG) const {
879   EVT VT;
880   const SDNode *Op;
881   SDLoc DL(N);
882 
883   if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
884     VT = LD->getMemoryVT();
885     Op = LD->getBasePtr().getNode();
886     if (LD->getExtensionType() != ISD::NON_EXTLOAD)
887       return false;
888     if (AVR::isProgramMemoryAccess(LD)) {
889       return false;
890     }
891   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
892     VT = ST->getMemoryVT();
893     Op = ST->getBasePtr().getNode();
894     if (AVR::isProgramMemoryAccess(ST)) {
895       return false;
896     }
897   } else {
898     return false;
899   }
900 
901   if (VT != MVT::i8 && VT != MVT::i16) {
902     return false;
903   }
904 
905   if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB) {
906     return false;
907   }
908 
909   if (const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
910     int RHSC = RHS->getSExtValue();
911     if (Op->getOpcode() == ISD::SUB)
912       RHSC = -RHSC;
913 
914     if ((VT == MVT::i16 && RHSC != -2) || (VT == MVT::i8 && RHSC != -1)) {
915       return false;
916     }
917 
918     Base = Op->getOperand(0);
919     Offset = DAG.getConstant(RHSC, DL, MVT::i8);
920     AM = ISD::PRE_DEC;
921 
922     return true;
923   }
924 
925   return false;
926 }
927 
928 /// Returns true by value, base pointer and
929 /// offset pointer and addressing mode by reference if this node can be
930 /// combined with a load / store to form a post-indexed load / store.
getPostIndexedAddressParts(SDNode * N,SDNode * Op,SDValue & Base,SDValue & Offset,ISD::MemIndexedMode & AM,SelectionDAG & DAG) const931 bool AVRTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
932                                                    SDValue &Base,
933                                                    SDValue &Offset,
934                                                    ISD::MemIndexedMode &AM,
935                                                    SelectionDAG &DAG) const {
936   EVT VT;
937   SDLoc DL(N);
938 
939   if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
940     VT = LD->getMemoryVT();
941     if (LD->getExtensionType() != ISD::NON_EXTLOAD)
942       return false;
943   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
944     VT = ST->getMemoryVT();
945     if (AVR::isProgramMemoryAccess(ST)) {
946       return false;
947     }
948   } else {
949     return false;
950   }
951 
952   if (VT != MVT::i8 && VT != MVT::i16) {
953     return false;
954   }
955 
956   if (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB) {
957     return false;
958   }
959 
960   if (const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1))) {
961     int RHSC = RHS->getSExtValue();
962     if (Op->getOpcode() == ISD::SUB)
963       RHSC = -RHSC;
964     if ((VT == MVT::i16 && RHSC != 2) || (VT == MVT::i8 && RHSC != 1)) {
965       return false;
966     }
967 
968     Base = Op->getOperand(0);
969     Offset = DAG.getConstant(RHSC, DL, MVT::i8);
970     AM = ISD::POST_INC;
971 
972     return true;
973   }
974 
975   return false;
976 }
977 
isOffsetFoldingLegal(const GlobalAddressSDNode * GA) const978 bool AVRTargetLowering::isOffsetFoldingLegal(
979     const GlobalAddressSDNode *GA) const {
980   return true;
981 }
982 
983 //===----------------------------------------------------------------------===//
984 //             Formal Arguments Calling Convention Implementation
985 //===----------------------------------------------------------------------===//
986 
987 #include "AVRGenCallingConv.inc"
988 
989 /// Registers for calling conventions, ordered in reverse as required by ABI.
990 /// Both arrays must be of the same length.
991 static const MCPhysReg RegList8[] = {
992     AVR::R25, AVR::R24, AVR::R23, AVR::R22, AVR::R21, AVR::R20,
993     AVR::R19, AVR::R18, AVR::R17, AVR::R16, AVR::R15, AVR::R14,
994     AVR::R13, AVR::R12, AVR::R11, AVR::R10, AVR::R9,  AVR::R8};
995 static const MCPhysReg RegList16[] = {
996     AVR::R26R25, AVR::R25R24, AVR::R24R23, AVR::R23R22,
997     AVR::R22R21, AVR::R21R20, AVR::R20R19, AVR::R19R18,
998     AVR::R18R17, AVR::R17R16, AVR::R16R15, AVR::R15R14,
999     AVR::R14R13, AVR::R13R12, AVR::R12R11, AVR::R11R10,
1000     AVR::R10R9,  AVR::R9R8};
1001 
1002 static_assert(array_lengthof(RegList8) == array_lengthof(RegList16),
1003         "8-bit and 16-bit register arrays must be of equal length");
1004 
1005 /// Analyze incoming and outgoing function arguments. We need custom C++ code
1006 /// to handle special constraints in the ABI.
1007 /// In addition, all pieces of a certain argument have to be passed either
1008 /// using registers or the stack but never mixing both.
1009 template <typename ArgT>
1010 static void
analyzeArguments(TargetLowering::CallLoweringInfo * CLI,const Function * F,const DataLayout * TD,const SmallVectorImpl<ArgT> & Args,SmallVectorImpl<CCValAssign> & ArgLocs,CCState & CCInfo)1011 analyzeArguments(TargetLowering::CallLoweringInfo *CLI, const Function *F,
1012                  const DataLayout *TD, const SmallVectorImpl<ArgT> &Args,
1013                  SmallVectorImpl<CCValAssign> &ArgLocs, CCState &CCInfo) {
1014   unsigned NumArgs = Args.size();
1015   // This is the index of the last used register, in RegList*.
1016   // -1 means R26 (R26 is never actually used in CC).
1017   int RegLastIdx = -1;
1018   // Once a value is passed to the stack it will always be used
1019   bool UseStack = false;
1020   for (unsigned i = 0; i != NumArgs;) {
1021     MVT VT = Args[i].VT;
1022     // We have to count the number of bytes for each function argument, that is
1023     // those Args with the same OrigArgIndex. This is important in case the
1024     // function takes an aggregate type.
1025     // Current argument will be between [i..j).
1026     unsigned ArgIndex = Args[i].OrigArgIndex;
1027     unsigned TotalBytes = VT.getStoreSize();
1028     unsigned j = i + 1;
1029     for (; j != NumArgs; ++j) {
1030       if (Args[j].OrigArgIndex != ArgIndex)
1031         break;
1032       TotalBytes += Args[j].VT.getStoreSize();
1033     }
1034     // Round up to even number of bytes.
1035     TotalBytes = alignTo(TotalBytes, 2);
1036     // Skip zero sized arguments
1037     if (TotalBytes == 0)
1038       continue;
1039     // The index of the first register to be used
1040     unsigned RegIdx = RegLastIdx + TotalBytes;
1041     RegLastIdx = RegIdx;
1042     // If there are not enough registers, use the stack
1043     if (RegIdx >= array_lengthof(RegList8)) {
1044       UseStack = true;
1045     }
1046     for (; i != j; ++i) {
1047       MVT VT = Args[i].VT;
1048 
1049       if (UseStack) {
1050         auto evt = EVT(VT).getTypeForEVT(CCInfo.getContext());
1051         unsigned Offset = CCInfo.AllocateStack(TD->getTypeAllocSize(evt),
1052                                                TD->getABITypeAlign(evt));
1053         CCInfo.addLoc(
1054             CCValAssign::getMem(i, VT, Offset, VT, CCValAssign::Full));
1055       } else {
1056         unsigned Reg;
1057         if (VT == MVT::i8) {
1058           Reg = CCInfo.AllocateReg(RegList8[RegIdx]);
1059         } else if (VT == MVT::i16) {
1060           Reg = CCInfo.AllocateReg(RegList16[RegIdx]);
1061         } else {
1062           llvm_unreachable(
1063               "calling convention can only manage i8 and i16 types");
1064         }
1065         assert(Reg && "register not available in calling convention");
1066         CCInfo.addLoc(CCValAssign::getReg(i, VT, Reg, VT, CCValAssign::Full));
1067         // Registers inside a particular argument are sorted in increasing order
1068         // (remember the array is reversed).
1069         RegIdx -= VT.getStoreSize();
1070       }
1071     }
1072   }
1073 }
1074 
1075 /// Count the total number of bytes needed to pass or return these arguments.
1076 template <typename ArgT>
getTotalArgumentsSizeInBytes(const SmallVectorImpl<ArgT> & Args)1077 static unsigned getTotalArgumentsSizeInBytes(const SmallVectorImpl<ArgT> &Args) {
1078   unsigned TotalBytes = 0;
1079 
1080   for (const ArgT& Arg : Args) {
1081     TotalBytes += Arg.VT.getStoreSize();
1082   }
1083   return TotalBytes;
1084 }
1085 
1086 /// Analyze incoming and outgoing value of returning from a function.
1087 /// The algorithm is similar to analyzeArguments, but there can only be
1088 /// one value, possibly an aggregate, and it is limited to 8 bytes.
1089 template <typename ArgT>
analyzeReturnValues(const SmallVectorImpl<ArgT> & Args,CCState & CCInfo)1090 static void analyzeReturnValues(const SmallVectorImpl<ArgT> &Args,
1091                                 CCState &CCInfo) {
1092   unsigned NumArgs = Args.size();
1093   unsigned TotalBytes = getTotalArgumentsSizeInBytes(Args);
1094   // CanLowerReturn() guarantees this assertion.
1095   assert(TotalBytes <= 8 && "return values greater than 8 bytes cannot be lowered");
1096 
1097   // GCC-ABI says that the size is rounded up to the next even number,
1098   // but actually once it is more than 4 it will always round up to 8.
1099   if (TotalBytes > 4) {
1100     TotalBytes = 8;
1101   } else {
1102     TotalBytes = alignTo(TotalBytes, 2);
1103   }
1104 
1105   // The index of the first register to use.
1106   int RegIdx = TotalBytes - 1;
1107   for (unsigned i = 0; i != NumArgs; ++i) {
1108     MVT VT = Args[i].VT;
1109     unsigned Reg;
1110     if (VT == MVT::i8) {
1111       Reg = CCInfo.AllocateReg(RegList8[RegIdx]);
1112     } else if (VT == MVT::i16) {
1113       Reg = CCInfo.AllocateReg(RegList16[RegIdx]);
1114     } else {
1115       llvm_unreachable("calling convention can only manage i8 and i16 types");
1116     }
1117     assert(Reg && "register not available in calling convention");
1118     CCInfo.addLoc(CCValAssign::getReg(i, VT, Reg, VT, CCValAssign::Full));
1119     // Registers sort in increasing order
1120     RegIdx -= VT.getStoreSize();
1121   }
1122 }
1123 
LowerFormalArguments(SDValue Chain,CallingConv::ID CallConv,bool isVarArg,const SmallVectorImpl<ISD::InputArg> & Ins,const SDLoc & dl,SelectionDAG & DAG,SmallVectorImpl<SDValue> & InVals) const1124 SDValue AVRTargetLowering::LowerFormalArguments(
1125     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
1126     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1127     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1128   MachineFunction &MF = DAG.getMachineFunction();
1129   MachineFrameInfo &MFI = MF.getFrameInfo();
1130   auto DL = DAG.getDataLayout();
1131 
1132   // Assign locations to all of the incoming arguments.
1133   SmallVector<CCValAssign, 16> ArgLocs;
1134   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1135                  *DAG.getContext());
1136 
1137   // Variadic functions do not need all the analysis below.
1138   if (isVarArg) {
1139     CCInfo.AnalyzeFormalArguments(Ins, ArgCC_AVR_Vararg);
1140   } else {
1141     analyzeArguments(nullptr, &MF.getFunction(), &DL, Ins, ArgLocs, CCInfo);
1142   }
1143 
1144   SDValue ArgValue;
1145   for (CCValAssign &VA : ArgLocs) {
1146 
1147     // Arguments stored on registers.
1148     if (VA.isRegLoc()) {
1149       EVT RegVT = VA.getLocVT();
1150       const TargetRegisterClass *RC;
1151       if (RegVT == MVT::i8) {
1152         RC = &AVR::GPR8RegClass;
1153       } else if (RegVT == MVT::i16) {
1154         RC = &AVR::DREGSRegClass;
1155       } else {
1156         llvm_unreachable("Unknown argument type!");
1157       }
1158 
1159       unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1160       ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1161 
1162       // :NOTE: Clang should not promote any i8 into i16 but for safety the
1163       // following code will handle zexts or sexts generated by other
1164       // front ends. Otherwise:
1165       // If this is an 8 bit value, it is really passed promoted
1166       // to 16 bits. Insert an assert[sz]ext to capture this, then
1167       // truncate to the right size.
1168       switch (VA.getLocInfo()) {
1169       default:
1170         llvm_unreachable("Unknown loc info!");
1171       case CCValAssign::Full:
1172         break;
1173       case CCValAssign::BCvt:
1174         ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
1175         break;
1176       case CCValAssign::SExt:
1177         ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1178                                DAG.getValueType(VA.getValVT()));
1179         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1180         break;
1181       case CCValAssign::ZExt:
1182         ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1183                                DAG.getValueType(VA.getValVT()));
1184         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1185         break;
1186       }
1187 
1188       InVals.push_back(ArgValue);
1189     } else {
1190       // Sanity check.
1191       assert(VA.isMemLoc());
1192 
1193       EVT LocVT = VA.getLocVT();
1194 
1195       // Create the frame index object for this incoming parameter.
1196       int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1197                                      VA.getLocMemOffset(), true);
1198 
1199       // Create the SelectionDAG nodes corresponding to a load
1200       // from this parameter.
1201       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DL));
1202       InVals.push_back(DAG.getLoad(LocVT, dl, Chain, FIN,
1203                                    MachinePointerInfo::getFixedStack(MF, FI)));
1204     }
1205   }
1206 
1207   // If the function takes variable number of arguments, make a frame index for
1208   // the start of the first vararg value... for expansion of llvm.va_start.
1209   if (isVarArg) {
1210     unsigned StackSize = CCInfo.getNextStackOffset();
1211     AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
1212 
1213     AFI->setVarArgsFrameIndex(MFI.CreateFixedObject(2, StackSize, true));
1214   }
1215 
1216   return Chain;
1217 }
1218 
1219 //===----------------------------------------------------------------------===//
1220 //                  Call Calling Convention Implementation
1221 //===----------------------------------------------------------------------===//
1222 
LowerCall(TargetLowering::CallLoweringInfo & CLI,SmallVectorImpl<SDValue> & InVals) const1223 SDValue AVRTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
1224                                      SmallVectorImpl<SDValue> &InVals) const {
1225   SelectionDAG &DAG = CLI.DAG;
1226   SDLoc &DL = CLI.DL;
1227   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1228   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1229   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1230   SDValue Chain = CLI.Chain;
1231   SDValue Callee = CLI.Callee;
1232   bool &isTailCall = CLI.IsTailCall;
1233   CallingConv::ID CallConv = CLI.CallConv;
1234   bool isVarArg = CLI.IsVarArg;
1235 
1236   MachineFunction &MF = DAG.getMachineFunction();
1237 
1238   // AVR does not yet support tail call optimization.
1239   isTailCall = false;
1240 
1241   // Analyze operands of the call, assigning locations to each operand.
1242   SmallVector<CCValAssign, 16> ArgLocs;
1243   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1244                  *DAG.getContext());
1245 
1246   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1247   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1248   // node so that legalize doesn't hack it.
1249   const Function *F = nullptr;
1250   if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1251     const GlobalValue *GV = G->getGlobal();
1252 
1253     F = cast<Function>(GV);
1254     Callee =
1255         DAG.getTargetGlobalAddress(GV, DL, getPointerTy(DAG.getDataLayout()));
1256   } else if (const ExternalSymbolSDNode *ES =
1257                  dyn_cast<ExternalSymbolSDNode>(Callee)) {
1258     Callee = DAG.getTargetExternalSymbol(ES->getSymbol(),
1259                                          getPointerTy(DAG.getDataLayout()));
1260   }
1261 
1262   // Variadic functions do not need all the analysis below.
1263   if (isVarArg) {
1264     CCInfo.AnalyzeCallOperands(Outs, ArgCC_AVR_Vararg);
1265   } else {
1266     analyzeArguments(&CLI, F, &DAG.getDataLayout(), Outs, ArgLocs, CCInfo);
1267   }
1268 
1269   // Get a count of how many bytes are to be pushed on the stack.
1270   unsigned NumBytes = CCInfo.getNextStackOffset();
1271 
1272   Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
1273 
1274   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1275 
1276   // First, walk the register assignments, inserting copies.
1277   unsigned AI, AE;
1278   bool HasStackArgs = false;
1279   for (AI = 0, AE = ArgLocs.size(); AI != AE; ++AI) {
1280     CCValAssign &VA = ArgLocs[AI];
1281     EVT RegVT = VA.getLocVT();
1282     SDValue Arg = OutVals[AI];
1283 
1284     // Promote the value if needed. With Clang this should not happen.
1285     switch (VA.getLocInfo()) {
1286     default:
1287       llvm_unreachable("Unknown loc info!");
1288     case CCValAssign::Full:
1289       break;
1290     case CCValAssign::SExt:
1291       Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, RegVT, Arg);
1292       break;
1293     case CCValAssign::ZExt:
1294       Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, RegVT, Arg);
1295       break;
1296     case CCValAssign::AExt:
1297       Arg = DAG.getNode(ISD::ANY_EXTEND, DL, RegVT, Arg);
1298       break;
1299     case CCValAssign::BCvt:
1300       Arg = DAG.getNode(ISD::BITCAST, DL, RegVT, Arg);
1301       break;
1302     }
1303 
1304     // Stop when we encounter a stack argument, we need to process them
1305     // in reverse order in the loop below.
1306     if (VA.isMemLoc()) {
1307       HasStackArgs = true;
1308       break;
1309     }
1310 
1311     // Arguments that can be passed on registers must be kept in the RegsToPass
1312     // vector.
1313     RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1314   }
1315 
1316   // Second, stack arguments have to walked in reverse order by inserting
1317   // chained stores, this ensures their order is not changed by the scheduler
1318   // and that the push instruction sequence generated is correct, otherwise they
1319   // can be freely intermixed.
1320   if (HasStackArgs) {
1321     for (AE = AI, AI = ArgLocs.size(); AI != AE; --AI) {
1322       unsigned Loc = AI - 1;
1323       CCValAssign &VA = ArgLocs[Loc];
1324       SDValue Arg = OutVals[Loc];
1325 
1326       assert(VA.isMemLoc());
1327 
1328       // SP points to one stack slot further so add one to adjust it.
1329       SDValue PtrOff = DAG.getNode(
1330           ISD::ADD, DL, getPointerTy(DAG.getDataLayout()),
1331           DAG.getRegister(AVR::SP, getPointerTy(DAG.getDataLayout())),
1332           DAG.getIntPtrConstant(VA.getLocMemOffset() + 1, DL));
1333 
1334       Chain =
1335           DAG.getStore(Chain, DL, Arg, PtrOff,
1336                        MachinePointerInfo::getStack(MF, VA.getLocMemOffset()));
1337     }
1338   }
1339 
1340   // Build a sequence of copy-to-reg nodes chained together with token chain and
1341   // flag operands which copy the outgoing args into registers.  The InFlag in
1342   // necessary since all emited instructions must be stuck together.
1343   SDValue InFlag;
1344   for (auto Reg : RegsToPass) {
1345     Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, InFlag);
1346     InFlag = Chain.getValue(1);
1347   }
1348 
1349   // Returns a chain & a flag for retval copy to use.
1350   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1351   SmallVector<SDValue, 8> Ops;
1352   Ops.push_back(Chain);
1353   Ops.push_back(Callee);
1354 
1355   // Add argument registers to the end of the list so that they are known live
1356   // into the call.
1357   for (auto Reg : RegsToPass) {
1358     Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1359   }
1360 
1361   // Add a register mask operand representing the call-preserved registers.
1362   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1363   const uint32_t *Mask =
1364       TRI->getCallPreservedMask(DAG.getMachineFunction(), CallConv);
1365   assert(Mask && "Missing call preserved mask for calling convention");
1366   Ops.push_back(DAG.getRegisterMask(Mask));
1367 
1368   if (InFlag.getNode()) {
1369     Ops.push_back(InFlag);
1370   }
1371 
1372   Chain = DAG.getNode(AVRISD::CALL, DL, NodeTys, Ops);
1373   InFlag = Chain.getValue(1);
1374 
1375   // Create the CALLSEQ_END node.
1376   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true),
1377                              DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
1378 
1379   if (!Ins.empty()) {
1380     InFlag = Chain.getValue(1);
1381   }
1382 
1383   // Handle result values, copying them out of physregs into vregs that we
1384   // return.
1385   return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, DL, DAG,
1386                          InVals);
1387 }
1388 
1389 /// Lower the result values of a call into the
1390 /// appropriate copies out of appropriate physical registers.
1391 ///
LowerCallResult(SDValue Chain,SDValue InFlag,CallingConv::ID CallConv,bool isVarArg,const SmallVectorImpl<ISD::InputArg> & Ins,const SDLoc & dl,SelectionDAG & DAG,SmallVectorImpl<SDValue> & InVals) const1392 SDValue AVRTargetLowering::LowerCallResult(
1393     SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
1394     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, SelectionDAG &DAG,
1395     SmallVectorImpl<SDValue> &InVals) const {
1396 
1397   // Assign locations to each value returned by this call.
1398   SmallVector<CCValAssign, 16> RVLocs;
1399   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1400                  *DAG.getContext());
1401 
1402   // Handle runtime calling convs.
1403   if (CallConv == CallingConv::AVR_BUILTIN) {
1404     CCInfo.AnalyzeCallResult(Ins, RetCC_AVR_BUILTIN);
1405   } else {
1406     analyzeReturnValues(Ins, CCInfo);
1407   }
1408 
1409   // Copy all of the result registers out of their specified physreg.
1410   for (CCValAssign const &RVLoc : RVLocs) {
1411     Chain = DAG.getCopyFromReg(Chain, dl, RVLoc.getLocReg(), RVLoc.getValVT(),
1412                                InFlag)
1413                 .getValue(1);
1414     InFlag = Chain.getValue(2);
1415     InVals.push_back(Chain.getValue(0));
1416   }
1417 
1418   return Chain;
1419 }
1420 
1421 //===----------------------------------------------------------------------===//
1422 //               Return Value Calling Convention Implementation
1423 //===----------------------------------------------------------------------===//
1424 
CanLowerReturn(CallingConv::ID CallConv,MachineFunction & MF,bool isVarArg,const SmallVectorImpl<ISD::OutputArg> & Outs,LLVMContext & Context) const1425 bool AVRTargetLowering::CanLowerReturn(
1426     CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
1427     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1428   if (CallConv == CallingConv::AVR_BUILTIN) {
1429     SmallVector<CCValAssign, 16> RVLocs;
1430     CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
1431     return CCInfo.CheckReturn(Outs, RetCC_AVR_BUILTIN);
1432   }
1433 
1434   unsigned TotalBytes = getTotalArgumentsSizeInBytes(Outs);
1435   return TotalBytes <= 8;
1436 }
1437 
1438 SDValue
LowerReturn(SDValue Chain,CallingConv::ID CallConv,bool isVarArg,const SmallVectorImpl<ISD::OutputArg> & Outs,const SmallVectorImpl<SDValue> & OutVals,const SDLoc & dl,SelectionDAG & DAG) const1439 AVRTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1440                                bool isVarArg,
1441                                const SmallVectorImpl<ISD::OutputArg> &Outs,
1442                                const SmallVectorImpl<SDValue> &OutVals,
1443                                const SDLoc &dl, SelectionDAG &DAG) const {
1444   // CCValAssign - represent the assignment of the return value to locations.
1445   SmallVector<CCValAssign, 16> RVLocs;
1446 
1447   // CCState - Info about the registers and stack slot.
1448   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1449                  *DAG.getContext());
1450 
1451   MachineFunction &MF = DAG.getMachineFunction();
1452 
1453   // Analyze return values.
1454   if (CallConv == CallingConv::AVR_BUILTIN) {
1455     CCInfo.AnalyzeReturn(Outs, RetCC_AVR_BUILTIN);
1456   } else {
1457     analyzeReturnValues(Outs, CCInfo);
1458   }
1459 
1460   SDValue Flag;
1461   SmallVector<SDValue, 4> RetOps(1, Chain);
1462   // Copy the result values into the output registers.
1463   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
1464     CCValAssign &VA = RVLocs[i];
1465     assert(VA.isRegLoc() && "Can only return in registers!");
1466 
1467     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
1468 
1469     // Guarantee that all emitted copies are stuck together with flags.
1470     Flag = Chain.getValue(1);
1471     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1472   }
1473 
1474   // Don't emit the ret/reti instruction when the naked attribute is present in
1475   // the function being compiled.
1476   if (MF.getFunction().getAttributes().hasAttribute(
1477           AttributeList::FunctionIndex, Attribute::Naked)) {
1478     return Chain;
1479   }
1480 
1481   const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
1482 
1483   unsigned RetOpc =
1484     AFI->isInterruptOrSignalHandler()
1485         ? AVRISD::RETI_FLAG
1486         : AVRISD::RET_FLAG;
1487 
1488   RetOps[0] = Chain; // Update chain.
1489 
1490   if (Flag.getNode()) {
1491     RetOps.push_back(Flag);
1492   }
1493 
1494   return DAG.getNode(RetOpc, dl, MVT::Other, RetOps);
1495 }
1496 
1497 //===----------------------------------------------------------------------===//
1498 //  Custom Inserters
1499 //===----------------------------------------------------------------------===//
1500 
insertShift(MachineInstr & MI,MachineBasicBlock * BB) const1501 MachineBasicBlock *AVRTargetLowering::insertShift(MachineInstr &MI,
1502                                                   MachineBasicBlock *BB) const {
1503   unsigned Opc;
1504   const TargetRegisterClass *RC;
1505   bool HasRepeatedOperand = false;
1506   MachineFunction *F = BB->getParent();
1507   MachineRegisterInfo &RI = F->getRegInfo();
1508   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1509   DebugLoc dl = MI.getDebugLoc();
1510 
1511   switch (MI.getOpcode()) {
1512   default:
1513     llvm_unreachable("Invalid shift opcode!");
1514   case AVR::Lsl8:
1515     Opc = AVR::ADDRdRr; // LSL is an alias of ADD Rd, Rd
1516     RC = &AVR::GPR8RegClass;
1517     HasRepeatedOperand = true;
1518     break;
1519   case AVR::Lsl16:
1520     Opc = AVR::LSLWRd;
1521     RC = &AVR::DREGSRegClass;
1522     break;
1523   case AVR::Asr8:
1524     Opc = AVR::ASRRd;
1525     RC = &AVR::GPR8RegClass;
1526     break;
1527   case AVR::Asr16:
1528     Opc = AVR::ASRWRd;
1529     RC = &AVR::DREGSRegClass;
1530     break;
1531   case AVR::Lsr8:
1532     Opc = AVR::LSRRd;
1533     RC = &AVR::GPR8RegClass;
1534     break;
1535   case AVR::Lsr16:
1536     Opc = AVR::LSRWRd;
1537     RC = &AVR::DREGSRegClass;
1538     break;
1539   case AVR::Rol8:
1540     Opc = AVR::ROLBRd;
1541     RC = &AVR::GPR8RegClass;
1542     break;
1543   case AVR::Rol16:
1544     Opc = AVR::ROLWRd;
1545     RC = &AVR::DREGSRegClass;
1546     break;
1547   case AVR::Ror8:
1548     Opc = AVR::RORBRd;
1549     RC = &AVR::GPR8RegClass;
1550     break;
1551   case AVR::Ror16:
1552     Opc = AVR::RORWRd;
1553     RC = &AVR::DREGSRegClass;
1554     break;
1555   }
1556 
1557   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1558 
1559   MachineFunction::iterator I;
1560   for (I = BB->getIterator(); I != F->end() && &(*I) != BB; ++I);
1561   if (I != F->end()) ++I;
1562 
1563   // Create loop block.
1564   MachineBasicBlock *LoopBB = F->CreateMachineBasicBlock(LLVM_BB);
1565   MachineBasicBlock *CheckBB = F->CreateMachineBasicBlock(LLVM_BB);
1566   MachineBasicBlock *RemBB = F->CreateMachineBasicBlock(LLVM_BB);
1567 
1568   F->insert(I, LoopBB);
1569   F->insert(I, CheckBB);
1570   F->insert(I, RemBB);
1571 
1572   // Update machine-CFG edges by transferring all successors of the current
1573   // block to the block containing instructions after shift.
1574   RemBB->splice(RemBB->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
1575                 BB->end());
1576   RemBB->transferSuccessorsAndUpdatePHIs(BB);
1577 
1578   // Add edges BB => LoopBB => CheckBB => RemBB, CheckBB => LoopBB.
1579   BB->addSuccessor(CheckBB);
1580   LoopBB->addSuccessor(CheckBB);
1581   CheckBB->addSuccessor(LoopBB);
1582   CheckBB->addSuccessor(RemBB);
1583 
1584   Register ShiftAmtReg = RI.createVirtualRegister(&AVR::GPR8RegClass);
1585   Register ShiftAmtReg2 = RI.createVirtualRegister(&AVR::GPR8RegClass);
1586   Register ShiftReg = RI.createVirtualRegister(RC);
1587   Register ShiftReg2 = RI.createVirtualRegister(RC);
1588   Register ShiftAmtSrcReg = MI.getOperand(2).getReg();
1589   Register SrcReg = MI.getOperand(1).getReg();
1590   Register DstReg = MI.getOperand(0).getReg();
1591 
1592   // BB:
1593   // rjmp CheckBB
1594   BuildMI(BB, dl, TII.get(AVR::RJMPk)).addMBB(CheckBB);
1595 
1596   // LoopBB:
1597   // ShiftReg2 = shift ShiftReg
1598   auto ShiftMI = BuildMI(LoopBB, dl, TII.get(Opc), ShiftReg2).addReg(ShiftReg);
1599   if (HasRepeatedOperand)
1600     ShiftMI.addReg(ShiftReg);
1601 
1602   // CheckBB:
1603   // ShiftReg = phi [%SrcReg, BB], [%ShiftReg2, LoopBB]
1604   // ShiftAmt = phi [%N,      BB], [%ShiftAmt2, LoopBB]
1605   // DestReg  = phi [%SrcReg, BB], [%ShiftReg,  LoopBB]
1606   // ShiftAmt2 = ShiftAmt - 1;
1607   // if (ShiftAmt2 >= 0) goto LoopBB;
1608   BuildMI(CheckBB, dl, TII.get(AVR::PHI), ShiftReg)
1609       .addReg(SrcReg)
1610       .addMBB(BB)
1611       .addReg(ShiftReg2)
1612       .addMBB(LoopBB);
1613   BuildMI(CheckBB, dl, TII.get(AVR::PHI), ShiftAmtReg)
1614       .addReg(ShiftAmtSrcReg)
1615       .addMBB(BB)
1616       .addReg(ShiftAmtReg2)
1617       .addMBB(LoopBB);
1618   BuildMI(CheckBB, dl, TII.get(AVR::PHI), DstReg)
1619       .addReg(SrcReg)
1620       .addMBB(BB)
1621       .addReg(ShiftReg2)
1622       .addMBB(LoopBB);
1623 
1624   BuildMI(CheckBB, dl, TII.get(AVR::DECRd), ShiftAmtReg2)
1625       .addReg(ShiftAmtReg);
1626   BuildMI(CheckBB, dl, TII.get(AVR::BRPLk)).addMBB(LoopBB);
1627 
1628   MI.eraseFromParent(); // The pseudo instruction is gone now.
1629   return RemBB;
1630 }
1631 
isCopyMulResult(MachineBasicBlock::iterator const & I)1632 static bool isCopyMulResult(MachineBasicBlock::iterator const &I) {
1633   if (I->getOpcode() == AVR::COPY) {
1634     Register SrcReg = I->getOperand(1).getReg();
1635     return (SrcReg == AVR::R0 || SrcReg == AVR::R1);
1636   }
1637 
1638   return false;
1639 }
1640 
1641 // The mul instructions wreak havock on our zero_reg R1. We need to clear it
1642 // after the result has been evacuated. This is probably not the best way to do
1643 // it, but it works for now.
insertMul(MachineInstr & MI,MachineBasicBlock * BB) const1644 MachineBasicBlock *AVRTargetLowering::insertMul(MachineInstr &MI,
1645                                                 MachineBasicBlock *BB) const {
1646   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1647   MachineBasicBlock::iterator I(MI);
1648   ++I; // in any case insert *after* the mul instruction
1649   if (isCopyMulResult(I))
1650     ++I;
1651   if (isCopyMulResult(I))
1652     ++I;
1653   BuildMI(*BB, I, MI.getDebugLoc(), TII.get(AVR::EORRdRr), AVR::R1)
1654       .addReg(AVR::R1)
1655       .addReg(AVR::R1);
1656   return BB;
1657 }
1658 
1659 MachineBasicBlock *
EmitInstrWithCustomInserter(MachineInstr & MI,MachineBasicBlock * MBB) const1660 AVRTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
1661                                                MachineBasicBlock *MBB) const {
1662   int Opc = MI.getOpcode();
1663 
1664   // Pseudo shift instructions with a non constant shift amount are expanded
1665   // into a loop.
1666   switch (Opc) {
1667   case AVR::Lsl8:
1668   case AVR::Lsl16:
1669   case AVR::Lsr8:
1670   case AVR::Lsr16:
1671   case AVR::Rol8:
1672   case AVR::Rol16:
1673   case AVR::Ror8:
1674   case AVR::Ror16:
1675   case AVR::Asr8:
1676   case AVR::Asr16:
1677     return insertShift(MI, MBB);
1678   case AVR::MULRdRr:
1679   case AVR::MULSRdRr:
1680     return insertMul(MI, MBB);
1681   }
1682 
1683   assert((Opc == AVR::Select16 || Opc == AVR::Select8) &&
1684          "Unexpected instr type to insert");
1685 
1686   const AVRInstrInfo &TII = (const AVRInstrInfo &)*MI.getParent()
1687                                 ->getParent()
1688                                 ->getSubtarget()
1689                                 .getInstrInfo();
1690   DebugLoc dl = MI.getDebugLoc();
1691 
1692   // To "insert" a SELECT instruction, we insert the diamond
1693   // control-flow pattern. The incoming instruction knows the
1694   // destination vreg to set, the condition code register to branch
1695   // on, the true/false values to select between, and a branch opcode
1696   // to use.
1697 
1698   MachineFunction *MF = MBB->getParent();
1699   const BasicBlock *LLVM_BB = MBB->getBasicBlock();
1700   MachineBasicBlock *FallThrough = MBB->getFallThrough();
1701 
1702   // If the current basic block falls through to another basic block,
1703   // we must insert an unconditional branch to the fallthrough destination
1704   // if we are to insert basic blocks at the prior fallthrough point.
1705   if (FallThrough != nullptr) {
1706     BuildMI(MBB, dl, TII.get(AVR::RJMPk)).addMBB(FallThrough);
1707   }
1708 
1709   MachineBasicBlock *trueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1710   MachineBasicBlock *falseMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1711 
1712   MachineFunction::iterator I;
1713   for (I = MF->begin(); I != MF->end() && &(*I) != MBB; ++I);
1714   if (I != MF->end()) ++I;
1715   MF->insert(I, trueMBB);
1716   MF->insert(I, falseMBB);
1717 
1718   // Transfer remaining instructions and all successors of the current
1719   // block to the block which will contain the Phi node for the
1720   // select.
1721   trueMBB->splice(trueMBB->begin(), MBB,
1722                   std::next(MachineBasicBlock::iterator(MI)), MBB->end());
1723   trueMBB->transferSuccessorsAndUpdatePHIs(MBB);
1724 
1725   AVRCC::CondCodes CC = (AVRCC::CondCodes)MI.getOperand(3).getImm();
1726   BuildMI(MBB, dl, TII.getBrCond(CC)).addMBB(trueMBB);
1727   BuildMI(MBB, dl, TII.get(AVR::RJMPk)).addMBB(falseMBB);
1728   MBB->addSuccessor(falseMBB);
1729   MBB->addSuccessor(trueMBB);
1730 
1731   // Unconditionally flow back to the true block
1732   BuildMI(falseMBB, dl, TII.get(AVR::RJMPk)).addMBB(trueMBB);
1733   falseMBB->addSuccessor(trueMBB);
1734 
1735   // Set up the Phi node to determine where we came from
1736   BuildMI(*trueMBB, trueMBB->begin(), dl, TII.get(AVR::PHI), MI.getOperand(0).getReg())
1737     .addReg(MI.getOperand(1).getReg())
1738     .addMBB(MBB)
1739     .addReg(MI.getOperand(2).getReg())
1740     .addMBB(falseMBB) ;
1741 
1742   MI.eraseFromParent(); // The pseudo instruction is gone now.
1743   return trueMBB;
1744 }
1745 
1746 //===----------------------------------------------------------------------===//
1747 //  Inline Asm Support
1748 //===----------------------------------------------------------------------===//
1749 
1750 AVRTargetLowering::ConstraintType
getConstraintType(StringRef Constraint) const1751 AVRTargetLowering::getConstraintType(StringRef Constraint) const {
1752   if (Constraint.size() == 1) {
1753     // See http://www.nongnu.org/avr-libc/user-manual/inline_asm.html
1754     switch (Constraint[0]) {
1755     default:
1756       break;
1757     case 'a': // Simple upper registers
1758     case 'b': // Base pointer registers pairs
1759     case 'd': // Upper register
1760     case 'l': // Lower registers
1761     case 'e': // Pointer register pairs
1762     case 'q': // Stack pointer register
1763     case 'r': // Any register
1764     case 'w': // Special upper register pairs
1765       return C_RegisterClass;
1766     case 't': // Temporary register
1767     case 'x': case 'X': // Pointer register pair X
1768     case 'y': case 'Y': // Pointer register pair Y
1769     case 'z': case 'Z': // Pointer register pair Z
1770       return C_Register;
1771     case 'Q': // A memory address based on Y or Z pointer with displacement.
1772       return C_Memory;
1773     case 'G': // Floating point constant
1774     case 'I': // 6-bit positive integer constant
1775     case 'J': // 6-bit negative integer constant
1776     case 'K': // Integer constant (Range: 2)
1777     case 'L': // Integer constant (Range: 0)
1778     case 'M': // 8-bit integer constant
1779     case 'N': // Integer constant (Range: -1)
1780     case 'O': // Integer constant (Range: 8, 16, 24)
1781     case 'P': // Integer constant (Range: 1)
1782     case 'R': // Integer constant (Range: -6 to 5)x
1783       return C_Immediate;
1784     }
1785   }
1786 
1787   return TargetLowering::getConstraintType(Constraint);
1788 }
1789 
1790 unsigned
getInlineAsmMemConstraint(StringRef ConstraintCode) const1791 AVRTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const {
1792   // Not sure if this is actually the right thing to do, but we got to do
1793   // *something* [agnat]
1794   switch (ConstraintCode[0]) {
1795   case 'Q':
1796     return InlineAsm::Constraint_Q;
1797   }
1798   return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
1799 }
1800 
1801 AVRTargetLowering::ConstraintWeight
getSingleConstraintMatchWeight(AsmOperandInfo & info,const char * constraint) const1802 AVRTargetLowering::getSingleConstraintMatchWeight(
1803     AsmOperandInfo &info, const char *constraint) const {
1804   ConstraintWeight weight = CW_Invalid;
1805   Value *CallOperandVal = info.CallOperandVal;
1806 
1807   // If we don't have a value, we can't do a match,
1808   // but allow it at the lowest weight.
1809   // (this behaviour has been copied from the ARM backend)
1810   if (!CallOperandVal) {
1811     return CW_Default;
1812   }
1813 
1814   // Look at the constraint type.
1815   switch (*constraint) {
1816   default:
1817     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1818     break;
1819   case 'd':
1820   case 'r':
1821   case 'l':
1822     weight = CW_Register;
1823     break;
1824   case 'a':
1825   case 'b':
1826   case 'e':
1827   case 'q':
1828   case 't':
1829   case 'w':
1830   case 'x': case 'X':
1831   case 'y': case 'Y':
1832   case 'z': case 'Z':
1833     weight = CW_SpecificReg;
1834     break;
1835   case 'G':
1836     if (const ConstantFP *C = dyn_cast<ConstantFP>(CallOperandVal)) {
1837       if (C->isZero()) {
1838         weight = CW_Constant;
1839       }
1840     }
1841     break;
1842   case 'I':
1843     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1844       if (isUInt<6>(C->getZExtValue())) {
1845         weight = CW_Constant;
1846       }
1847     }
1848     break;
1849   case 'J':
1850     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1851       if ((C->getSExtValue() >= -63) && (C->getSExtValue() <= 0)) {
1852         weight = CW_Constant;
1853       }
1854     }
1855     break;
1856   case 'K':
1857     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1858       if (C->getZExtValue() == 2) {
1859         weight = CW_Constant;
1860       }
1861     }
1862     break;
1863   case 'L':
1864     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1865       if (C->getZExtValue() == 0) {
1866         weight = CW_Constant;
1867       }
1868     }
1869     break;
1870   case 'M':
1871     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1872       if (isUInt<8>(C->getZExtValue())) {
1873         weight = CW_Constant;
1874       }
1875     }
1876     break;
1877   case 'N':
1878     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1879       if (C->getSExtValue() == -1) {
1880         weight = CW_Constant;
1881       }
1882     }
1883     break;
1884   case 'O':
1885     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1886       if ((C->getZExtValue() == 8) || (C->getZExtValue() == 16) ||
1887           (C->getZExtValue() == 24)) {
1888         weight = CW_Constant;
1889       }
1890     }
1891     break;
1892   case 'P':
1893     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1894       if (C->getZExtValue() == 1) {
1895         weight = CW_Constant;
1896       }
1897     }
1898     break;
1899   case 'R':
1900     if (const ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1901       if ((C->getSExtValue() >= -6) && (C->getSExtValue() <= 5)) {
1902         weight = CW_Constant;
1903       }
1904     }
1905     break;
1906   case 'Q':
1907     weight = CW_Memory;
1908     break;
1909   }
1910 
1911   return weight;
1912 }
1913 
1914 std::pair<unsigned, const TargetRegisterClass *>
getRegForInlineAsmConstraint(const TargetRegisterInfo * TRI,StringRef Constraint,MVT VT) const1915 AVRTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1916                                                 StringRef Constraint,
1917                                                 MVT VT) const {
1918   // We only support i8 and i16.
1919   //
1920   //:FIXME: remove this assert for now since it gets sometimes executed
1921   // assert((VT == MVT::i16 || VT == MVT::i8) && "Wrong operand type.");
1922 
1923   if (Constraint.size() == 1) {
1924     switch (Constraint[0]) {
1925     case 'a': // Simple upper registers r16..r23.
1926       return std::make_pair(0U, &AVR::LD8loRegClass);
1927     case 'b': // Base pointer registers: y, z.
1928       return std::make_pair(0U, &AVR::PTRDISPREGSRegClass);
1929     case 'd': // Upper registers r16..r31.
1930       return std::make_pair(0U, &AVR::LD8RegClass);
1931     case 'l': // Lower registers r0..r15.
1932       return std::make_pair(0U, &AVR::GPR8loRegClass);
1933     case 'e': // Pointer register pairs: x, y, z.
1934       return std::make_pair(0U, &AVR::PTRREGSRegClass);
1935     case 'q': // Stack pointer register: SPH:SPL.
1936       return std::make_pair(0U, &AVR::GPRSPRegClass);
1937     case 'r': // Any register: r0..r31.
1938       if (VT == MVT::i8)
1939         return std::make_pair(0U, &AVR::GPR8RegClass);
1940 
1941       return std::make_pair(0U, &AVR::DREGSRegClass);
1942     case 't': // Temporary register: r0.
1943       return std::make_pair(unsigned(AVR::R0), &AVR::GPR8RegClass);
1944     case 'w': // Special upper register pairs: r24, r26, r28, r30.
1945       return std::make_pair(0U, &AVR::IWREGSRegClass);
1946     case 'x': // Pointer register pair X: r27:r26.
1947     case 'X':
1948       return std::make_pair(unsigned(AVR::R27R26), &AVR::PTRREGSRegClass);
1949     case 'y': // Pointer register pair Y: r29:r28.
1950     case 'Y':
1951       return std::make_pair(unsigned(AVR::R29R28), &AVR::PTRREGSRegClass);
1952     case 'z': // Pointer register pair Z: r31:r30.
1953     case 'Z':
1954       return std::make_pair(unsigned(AVR::R31R30), &AVR::PTRREGSRegClass);
1955     default:
1956       break;
1957     }
1958   }
1959 
1960   return TargetLowering::getRegForInlineAsmConstraint(
1961       Subtarget.getRegisterInfo(), Constraint, VT);
1962 }
1963 
LowerAsmOperandForConstraint(SDValue Op,std::string & Constraint,std::vector<SDValue> & Ops,SelectionDAG & DAG) const1964 void AVRTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
1965                                                      std::string &Constraint,
1966                                                      std::vector<SDValue> &Ops,
1967                                                      SelectionDAG &DAG) const {
1968   SDValue Result(0, 0);
1969   SDLoc DL(Op);
1970   EVT Ty = Op.getValueType();
1971 
1972   // Currently only support length 1 constraints.
1973   if (Constraint.length() != 1) {
1974     return;
1975   }
1976 
1977   char ConstraintLetter = Constraint[0];
1978   switch (ConstraintLetter) {
1979   default:
1980     break;
1981   // Deal with integers first:
1982   case 'I':
1983   case 'J':
1984   case 'K':
1985   case 'L':
1986   case 'M':
1987   case 'N':
1988   case 'O':
1989   case 'P':
1990   case 'R': {
1991     const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1992     if (!C) {
1993       return;
1994     }
1995 
1996     int64_t CVal64 = C->getSExtValue();
1997     uint64_t CUVal64 = C->getZExtValue();
1998     switch (ConstraintLetter) {
1999     case 'I': // 0..63
2000       if (!isUInt<6>(CUVal64))
2001         return;
2002       Result = DAG.getTargetConstant(CUVal64, DL, Ty);
2003       break;
2004     case 'J': // -63..0
2005       if (CVal64 < -63 || CVal64 > 0)
2006         return;
2007       Result = DAG.getTargetConstant(CVal64, DL, Ty);
2008       break;
2009     case 'K': // 2
2010       if (CUVal64 != 2)
2011         return;
2012       Result = DAG.getTargetConstant(CUVal64, DL, Ty);
2013       break;
2014     case 'L': // 0
2015       if (CUVal64 != 0)
2016         return;
2017       Result = DAG.getTargetConstant(CUVal64, DL, Ty);
2018       break;
2019     case 'M': // 0..255
2020       if (!isUInt<8>(CUVal64))
2021         return;
2022       // i8 type may be printed as a negative number,
2023       // e.g. 254 would be printed as -2,
2024       // so we force it to i16 at least.
2025       if (Ty.getSimpleVT() == MVT::i8) {
2026         Ty = MVT::i16;
2027       }
2028       Result = DAG.getTargetConstant(CUVal64, DL, Ty);
2029       break;
2030     case 'N': // -1
2031       if (CVal64 != -1)
2032         return;
2033       Result = DAG.getTargetConstant(CVal64, DL, Ty);
2034       break;
2035     case 'O': // 8, 16, 24
2036       if (CUVal64 != 8 && CUVal64 != 16 && CUVal64 != 24)
2037         return;
2038       Result = DAG.getTargetConstant(CUVal64, DL, Ty);
2039       break;
2040     case 'P': // 1
2041       if (CUVal64 != 1)
2042         return;
2043       Result = DAG.getTargetConstant(CUVal64, DL, Ty);
2044       break;
2045     case 'R': // -6..5
2046       if (CVal64 < -6 || CVal64 > 5)
2047         return;
2048       Result = DAG.getTargetConstant(CVal64, DL, Ty);
2049       break;
2050     }
2051 
2052     break;
2053   }
2054   case 'G':
2055     const ConstantFPSDNode *FC = dyn_cast<ConstantFPSDNode>(Op);
2056     if (!FC || !FC->isZero())
2057       return;
2058     // Soften float to i8 0
2059     Result = DAG.getTargetConstant(0, DL, MVT::i8);
2060     break;
2061   }
2062 
2063   if (Result.getNode()) {
2064     Ops.push_back(Result);
2065     return;
2066   }
2067 
2068   return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
2069 }
2070 
getRegisterByName(const char * RegName,LLT VT,const MachineFunction & MF) const2071 Register AVRTargetLowering::getRegisterByName(const char *RegName, LLT VT,
2072                                               const MachineFunction &MF) const {
2073   Register Reg;
2074 
2075   if (VT == LLT::scalar(8)) {
2076     Reg = StringSwitch<unsigned>(RegName)
2077       .Case("r0", AVR::R0).Case("r1", AVR::R1).Case("r2", AVR::R2)
2078       .Case("r3", AVR::R3).Case("r4", AVR::R4).Case("r5", AVR::R5)
2079       .Case("r6", AVR::R6).Case("r7", AVR::R7).Case("r8", AVR::R8)
2080       .Case("r9", AVR::R9).Case("r10", AVR::R10).Case("r11", AVR::R11)
2081       .Case("r12", AVR::R12).Case("r13", AVR::R13).Case("r14", AVR::R14)
2082       .Case("r15", AVR::R15).Case("r16", AVR::R16).Case("r17", AVR::R17)
2083       .Case("r18", AVR::R18).Case("r19", AVR::R19).Case("r20", AVR::R20)
2084       .Case("r21", AVR::R21).Case("r22", AVR::R22).Case("r23", AVR::R23)
2085       .Case("r24", AVR::R24).Case("r25", AVR::R25).Case("r26", AVR::R26)
2086       .Case("r27", AVR::R27).Case("r28", AVR::R28).Case("r29", AVR::R29)
2087       .Case("r30", AVR::R30).Case("r31", AVR::R31)
2088       .Case("X", AVR::R27R26).Case("Y", AVR::R29R28).Case("Z", AVR::R31R30)
2089       .Default(0);
2090   } else {
2091     Reg = StringSwitch<unsigned>(RegName)
2092       .Case("r0", AVR::R1R0).Case("r2", AVR::R3R2)
2093       .Case("r4", AVR::R5R4).Case("r6", AVR::R7R6)
2094       .Case("r8", AVR::R9R8).Case("r10", AVR::R11R10)
2095       .Case("r12", AVR::R13R12).Case("r14", AVR::R15R14)
2096       .Case("r16", AVR::R17R16).Case("r18", AVR::R19R18)
2097       .Case("r20", AVR::R21R20).Case("r22", AVR::R23R22)
2098       .Case("r24", AVR::R25R24).Case("r26", AVR::R27R26)
2099       .Case("r28", AVR::R29R28).Case("r30", AVR::R31R30)
2100       .Case("X", AVR::R27R26).Case("Y", AVR::R29R28).Case("Z", AVR::R31R30)
2101       .Default(0);
2102   }
2103 
2104   if (Reg)
2105     return Reg;
2106 
2107   report_fatal_error("Invalid register name global variable");
2108 }
2109 
2110 } // end of namespace llvm
2111