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