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