1//===-- AVRISelDAGToDAG.cpp - A dag to dag inst selector for AVR ----------===//
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 an instruction selector for the AVR target.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AVR.h"
14#include "AVRMachineFunctionInfo.h"
15#include "AVRRegisterInfo.h"
16#include "AVRTargetMachine.h"
17#include "MCTargetDesc/AVRMCTargetDesc.h"
18
19#include "llvm/CodeGen/ISDOpcodes.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SelectionDAGNodes.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/CodeGen.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30
31#define DEBUG_TYPE "avr-isel"
32#define PASS_NAME "AVR DAG->DAG Instruction Selection"
33
34using namespace llvm;
35
36namespace {
37
38/// Lowers LLVM IR (in DAG form) to AVR MC instructions (in DAG form).
39class AVRDAGToDAGISel : public SelectionDAGISel {
40public:
41 AVRDAGToDAGISel() = delete;
42
43 AVRDAGToDAGISel(AVRTargetMachine &TM, CodeGenOptLevel OptLevel)
44 : SelectionDAGISel(TM, OptLevel), Subtarget(nullptr) {}
45
46 bool runOnMachineFunction(MachineFunction &MF) override;
47
48 void PostprocessISelDAG() override;
49
50 bool SelectAddr(SDNode *Op, SDValue N, SDValue &Base, SDValue &Disp);
51
52 bool selectAlignedFrameLoad(SDNode *N);
53 bool selectAlignedFrameStore(SDNode *N);
54 bool selectIndexedLoad(SDNode *N);
55
56 unsigned selectIndexedProgMemLoad(const LoadSDNode *LD, MVT VT, int Bank);
57
58 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
59 InlineAsm::ConstraintCode ConstraintCode,
60 std::vector<SDValue> &OutOps) override;
61
62// Include the pieces autogenerated from the target description.
63#include "AVRGenDAGISel.inc"
64
65private:
66 bool isPostProcessDone;
67
68 void Select(SDNode *N) override;
69 bool trySelect(SDNode *N);
70
71 template <unsigned NodeType> bool select(SDNode *N);
72 bool selectMultiplication(SDNode *N);
73
74 const AVRSubtarget *Subtarget;
75};
76
77class AVRDAGToDAGISelLegacy : public SelectionDAGISelLegacy {
78public:
79 static char ID;
80 AVRDAGToDAGISelLegacy(AVRTargetMachine &TM, CodeGenOptLevel OptLevel)
81 : SelectionDAGISelLegacy(
82 ID, std::make_unique<AVRDAGToDAGISel>(args&: TM, args&: OptLevel)) {}
83};
84
85} // namespace
86
87char AVRDAGToDAGISelLegacy::ID = 0;
88
89INITIALIZE_PASS(AVRDAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false, false)
90
91bool AVRDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
92 Subtarget = &MF.getSubtarget<AVRSubtarget>();
93 isPostProcessDone = false;
94
95 AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
96
97 // If our function has aligned allocas (e.g. `alloca i16, align 16`), we will
98 // need an aligned stack register to address them.
99 //
100 // Since we don't know this information upfront, assume we *will* need aligned
101 // stack register and aligned stack space - if this proves false,
102 // post-processing below will undo these two:
103 AFI->AlignedStackReg =
104 MF.getRegInfo().createVirtualRegister(RegClass: &AVR::DLDREGSRegClass);
105
106 AFI->AlignedStackObjectIdx =
107 MF.getFrameInfo().CreateStackObject(Size: 1, Alignment: Align(1), isSpillSlot: false);
108
109 // We've just allocated a virtual register, so let's drop the "NoVRegs" flag
110 // in case it was active.
111 MF.getProperties().resetNoVRegs();
112
113 return SelectionDAGISel::runOnMachineFunction(mf&: MF);
114}
115
116void AVRDAGToDAGISel::PostprocessISelDAG() {
117 if (isPostProcessDone) {
118 // Sometimes post-processing gets run multiple times on a single function -
119 // because our algorithm is not idempotent (we generate FRMSP instruction
120 // and reset function's alignment), it can only run once per function.
121 //
122 // This flags gets toggled on at the end of this function and it gets reset
123 // during `runOnMachineFunction()`.
124 return;
125 }
126
127 MachineFrameInfo &MFI = MF->getFrameInfo();
128 AVRMachineFunctionInfo *AFI = MF->getInfo<AVRMachineFunctionInfo>();
129
130 uint64_t Offset = 0;
131 uint64_t Alignment = 1;
132
133 // Move all aligned objects into their own stack, `AvrAlign`.
134 for (int FI = 0, MaxFI = MFI.getObjectIndexEnd(); FI != MaxFI; ++FI) {
135 if (MFI.getObjectAlign(ObjectIdx: FI).value() > 1) {
136 Offset = alignTo(Size: Offset, A: MFI.getObjectAlign(ObjectIdx: FI));
137
138 MFI.setStackID(ObjectIdx: FI, ID: TargetStackID::AvrAlign);
139
140 // Usually offsets are assigned by the prologue/epilogue inserter, but PEI
141 // doesn't touch objects that lay outside the default stack - so, seizing
142 // the day, let's assign the offset ourselves.
143 MFI.setObjectOffset(ObjectIdx: FI, SPOffset: Offset);
144
145 Offset += MFI.getObjectSize(ObjectIdx: FI);
146 Alignment = std::max(a: Alignment, b: MFI.getObjectAlign(ObjectIdx: FI).value());
147 }
148 }
149
150 // If we had any aligned objects, allocate the aligned stack register.
151 if (Offset > 0) {
152 BuildMI(BB: &MF->front(), MIMD: DebugLoc(), MCID: TII->get(Opcode: AVR::FRMSP))
153 .addReg(RegNo: AFI->AlignedStackReg, Flags: RegState::Define)
154 .addImm(Val: Alignment);
155
156 MFI.setObjectSize(ObjectIdx: AFI->AlignedStackObjectIdx, Size: Offset + Alignment - 1);
157 MFI.setMaxAlign(Align(1));
158 } else {
159 MFI.RemoveStackObject(ObjectIdx: AFI->AlignedStackObjectIdx);
160 }
161
162 isPostProcessDone = true;
163}
164
165bool AVRDAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
166 SDValue &Disp) {
167 SDLoc dl(Op);
168 auto DL = CurDAG->getDataLayout();
169 MVT PtrVT = getTargetLowering()->getPointerTy(DL);
170
171 // if the address is a frame index get the TargetFrameIndex.
172 if (const FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Val&: N)) {
173 Base = CurDAG->getTargetFrameIndex(FI: FIN->getIndex(), VT: PtrVT);
174 Disp = CurDAG->getTargetConstant(Val: 0, DL: dl, VT: MVT::i8);
175
176 return true;
177 }
178
179 // Match simple Reg + uimm6 operands.
180 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
181 !CurDAG->isBaseWithConstantOffset(Op: N)) {
182 return false;
183 }
184
185 if (const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Val: N.getOperand(i: 1))) {
186 int RHSC = (int)RHS->getZExtValue();
187
188 // Convert negative offsets into positives ones.
189 if (N.getOpcode() == ISD::SUB) {
190 RHSC = -RHSC;
191 }
192
193 // <#Frame index + const>
194 // Allow folding offsets bigger than 63 so the frame pointer can be used
195 // directly instead of copying it around by adjusting and restoring it for
196 // each access.
197 if (N.getOperand(i: 0).getOpcode() == ISD::FrameIndex) {
198 int FI = cast<FrameIndexSDNode>(Val: N.getOperand(i: 0))->getIndex();
199
200 Base = CurDAG->getTargetFrameIndex(FI, VT: PtrVT);
201 Disp = CurDAG->getTargetConstant(Val: RHSC, DL: dl, VT: MVT::i16);
202
203 return true;
204 }
205
206 // The value type of the memory instruction determines what is the maximum
207 // offset allowed.
208 MVT VT = cast<MemSDNode>(Val: Op)->getMemoryVT().getSimpleVT();
209
210 // We only accept offsets that fit in 6 bits (unsigned), with the exception
211 // of 16-bit loads - those can only go up to 62, because we desugar them
212 // into a pair of 8-bit loads like `ldd rx, RHSC` + `ldd ry, RHSC + 1`.
213 bool OkI8 = VT == MVT::i8 && RHSC <= 63;
214 bool OkI16 = VT == MVT::i16 && RHSC <= 62;
215
216 if (OkI8 || OkI16) {
217 Base = N.getOperand(i: 0);
218 Disp = CurDAG->getTargetConstant(Val: RHSC, DL: dl, VT: MVT::i8);
219
220 return true;
221 }
222 }
223
224 return false;
225}
226
227bool AVRDAGToDAGISel::selectAlignedFrameLoad(SDNode *N) {
228 LoadSDNode *LD = cast<LoadSDNode>(Val: N);
229 const SDValue LDB = LD->getBasePtr();
230
231 // ---
232
233 int InIndex;
234 int InOffset;
235
236 switch (LDB->getOpcode()) {
237 case ISD::FrameIndex:
238 InIndex = cast<FrameIndexSDNode>(Val: LDB)->getIndex();
239 InOffset = 0;
240 break;
241
242 case ISD::ADD:
243 case ISD::OR:
244 if (LDB->getOperand(Num: 0)->getOpcode() == ISD::FrameIndex &&
245 LDB->getOperand(Num: 1)->getOpcode() == ISD::Constant) {
246 InIndex = cast<FrameIndexSDNode>(Val: LDB->getOperand(Num: 0))->getIndex();
247 InOffset = cast<ConstantSDNode>(Val: LDB->getOperand(Num: 1))->getZExtValue();
248 } else {
249 return false;
250 }
251 break;
252
253 default:
254 return false;
255 }
256
257 // ---
258
259 uint64_t Alignment = MF->getFrameInfo().getObjectAlign(ObjectIdx: InIndex).value();
260
261 if (Alignment == 1) {
262 return false;
263 }
264
265 // ---
266
267 unsigned Opcode;
268
269 switch (LD->getMemoryVT().getSimpleVT().SimpleTy) {
270 case MVT::i8:
271 Opcode = AVR::LDRdPtr;
272 break;
273 case MVT::i16:
274 Opcode = AVR::LDWRdPtr;
275 break;
276 default:
277 return false;
278 }
279
280 // ---
281
282 MVT PointerTy = getTargetLowering()->getPointerTy(DL: CurDAG->getDataLayout());
283 Register StackReg = MF->getInfo<AVRMachineFunctionInfo>()->AlignedStackReg;
284
285 SDValue Index = CurDAG->getTargetFrameIndex(FI: InIndex, VT: PointerTy);
286 SDValue Offset = CurDAG->getTargetConstant(Val: InOffset, DL: SDLoc(N), VT: MVT::i16);
287
288 SDNode *ResNode =
289 CurDAG->getMachineNode(Opcode: AVR::FRMIDX, dl: SDLoc(N), VT: PointerTy, Op1: Index, Op2: Offset,
290 Op3: CurDAG->getRegister(Reg: StackReg, VT: PointerTy));
291
292 CurDAG->SelectNodeTo(N, MachineOpc: Opcode, VT1: LD->getMemoryVT().getSimpleVT(), VT2: MVT::Other,
293 Op1: SDValue(ResNode, 0), Op2: LD->getChain());
294
295 return true;
296}
297
298bool AVRDAGToDAGISel::selectAlignedFrameStore(SDNode *N) {
299 StoreSDNode *ST = cast<StoreSDNode>(Val: N);
300 const SDValue STB = ST->getBasePtr();
301
302 // ---
303
304 int InIndex;
305 int InOffset;
306
307 switch (STB->getOpcode()) {
308 case ISD::FrameIndex:
309 InIndex = cast<FrameIndexSDNode>(Val: STB)->getIndex();
310 InOffset = 0;
311 break;
312
313 case ISD::ADD:
314 case ISD::OR:
315 if (STB->getOperand(Num: 0)->getOpcode() == ISD::FrameIndex &&
316 STB->getOperand(Num: 1)->getOpcode() == ISD::Constant) {
317 InIndex = cast<FrameIndexSDNode>(Val: STB->getOperand(Num: 0))->getIndex();
318 InOffset = cast<ConstantSDNode>(Val: STB->getOperand(Num: 1))->getZExtValue();
319 } else {
320 return false;
321 }
322 break;
323
324 default:
325 return false;
326 }
327
328 // ---
329
330 uint64_t Alignment = MF->getFrameInfo().getObjectAlign(ObjectIdx: InIndex).value();
331
332 if (Alignment == 1) {
333 return false;
334 }
335
336 // ---
337
338 unsigned Opcode;
339
340 switch (ST->getMemoryVT().getSimpleVT().SimpleTy) {
341 case MVT::i8:
342 Opcode = AVR::STPtrRr;
343 break;
344 case MVT::i16:
345 Opcode = AVR::STWPtrRr;
346 break;
347 default:
348 return false;
349 }
350
351 // ---
352
353 MVT PointerTy = getTargetLowering()->getPointerTy(DL: CurDAG->getDataLayout());
354 Register StackReg = MF->getInfo<AVRMachineFunctionInfo>()->AlignedStackReg;
355
356 SDValue Index = CurDAG->getTargetFrameIndex(FI: InIndex, VT: PointerTy);
357 SDValue Offset = CurDAG->getTargetConstant(Val: InOffset, DL: SDLoc(N), VT: MVT::i16);
358
359 SDNode *AddrNode =
360 CurDAG->getMachineNode(Opcode: AVR::FRMIDX, dl: SDLoc(N), VT: PointerTy, Op1: Index, Op2: Offset,
361 Op3: CurDAG->getRegister(Reg: StackReg, VT: PointerTy));
362
363 SDNode *ResNode = CurDAG->getMachineNode(
364 Opcode, dl: SDLoc(N), VT: MVT::Other,
365 Ops: {SDValue(AddrNode, 0), ST->getValue(), ST->getChain()});
366
367 CurDAG->setNodeMemRefs(N: cast<MachineSDNode>(Val: ResNode), NewMemRefs: {ST->getMemOperand()});
368
369 ReplaceUses(F: SDValue(N, 0), T: SDValue(ResNode, 0));
370 CurDAG->RemoveDeadNode(N);
371
372 return true;
373}
374
375bool AVRDAGToDAGISel::selectIndexedLoad(SDNode *N) {
376 const LoadSDNode *LD = cast<LoadSDNode>(Val: N);
377 ISD::MemIndexedMode AM = LD->getAddressingMode();
378 MVT VT = LD->getMemoryVT().getSimpleVT();
379 auto PtrVT = getTargetLowering()->getPointerTy(DL: CurDAG->getDataLayout());
380
381 // We only care if this load uses a POSTINC or PREDEC mode.
382 if ((LD->getExtensionType() != ISD::NON_EXTLOAD) ||
383 (AM != ISD::POST_INC && AM != ISD::PRE_DEC)) {
384
385 return false;
386 }
387
388 unsigned Opcode = 0;
389 bool isPre = (AM == ISD::PRE_DEC);
390 int Offs = cast<ConstantSDNode>(Val: LD->getOffset())->getSExtValue();
391
392 switch (VT.SimpleTy) {
393 case MVT::i8: {
394 if ((!isPre && Offs != 1) || (isPre && Offs != -1)) {
395 return false;
396 }
397
398 Opcode = (isPre) ? AVR::LDRdPtrPd : AVR::LDRdPtrPi;
399 break;
400 }
401 case MVT::i16: {
402 if ((!isPre && Offs != 2) || (isPre && Offs != -2)) {
403 return false;
404 }
405
406 Opcode = (isPre) ? AVR::LDWRdPtrPd : AVR::LDWRdPtrPi;
407 break;
408 }
409 default:
410 return false;
411 }
412
413 SDNode *ResNode =
414 CurDAG->getMachineNode(Opcode, dl: SDLoc(N), VT1: VT, VT2: PtrVT, VT3: MVT::Other,
415 Op1: LD->getBasePtr(), Op2: LD->getChain());
416 ReplaceUses(F: N, T: ResNode);
417 CurDAG->RemoveDeadNode(N);
418
419 return true;
420}
421
422unsigned AVRDAGToDAGISel::selectIndexedProgMemLoad(const LoadSDNode *LD, MVT VT,
423 int Bank) {
424 // Progmem indexed loads only work in POSTINC mode.
425 if (LD->getExtensionType() != ISD::NON_EXTLOAD ||
426 LD->getAddressingMode() != ISD::POST_INC)
427 return 0;
428
429 // Feature ELPM is needed for loading from extended program memory.
430 assert((Bank == 0 || Subtarget->hasELPM()) &&
431 "cannot load from extended program memory on this mcu");
432
433 unsigned Opcode = 0;
434 int Offs = cast<ConstantSDNode>(Val: LD->getOffset())->getSExtValue();
435
436 if (VT.SimpleTy == MVT::i8 && Offs == 1 && Bank == 0)
437 Opcode = AVR::LPMRdZPi;
438
439 // TODO: Implements the expansion of the following pseudo instructions.
440 // LPMWRdZPi: type == MVT::i16, offset == 2, Bank == 0.
441 // ELPMBRdZPi: type == MVT::i8, offset == 1, Bank > 0.
442 // ELPMWRdZPi: type == MVT::i16, offset == 2, Bank > 0.
443
444 return Opcode;
445}
446
447bool AVRDAGToDAGISel::SelectInlineAsmMemoryOperand(
448 const SDValue &Op, InlineAsm::ConstraintCode ConstraintCode,
449 std::vector<SDValue> &OutOps) {
450 assert((ConstraintCode == InlineAsm::ConstraintCode::m ||
451 ConstraintCode == InlineAsm::ConstraintCode::Q) &&
452 "Unexpected asm memory constraint");
453
454 MachineRegisterInfo &RI = MF->getRegInfo();
455 const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();
456 const TargetLowering &TL = *STI.getTargetLowering();
457 SDLoc dl(Op);
458 auto DL = CurDAG->getDataLayout();
459
460 const RegisterSDNode *RegNode = dyn_cast<RegisterSDNode>(Val: Op);
461
462 // If address operand is of PTRDISPREGS class, all is OK, then.
463 if (RegNode &&
464 RI.getRegClass(Reg: RegNode->getReg()) == &AVR::PTRDISPREGSRegClass) {
465 OutOps.push_back(x: Op);
466 return false;
467 }
468
469 if (Op->getOpcode() == ISD::FrameIndex) {
470 SDValue Base, Disp;
471
472 if (SelectAddr(Op: Op.getNode(), N: Op, Base, Disp)) {
473 OutOps.push_back(x: Base);
474 OutOps.push_back(x: Disp);
475
476 return false;
477 }
478
479 return true;
480 }
481
482 // Select global addresses.
483 if (Op.getOpcode() == AVRISD::WRAPPER) {
484 SDValue Sub = Op.getOperand(i: 0);
485 if (Sub.getOpcode() == ISD::TargetGlobalAddress &&
486 (Sub.getValueType() == MVT::i16 || Sub.getValueType() == MVT::i8)) {
487 OutOps.push_back(x: Sub);
488 return false;
489 }
490 }
491
492 // If Op is add 'register, immediate' and
493 // register is either virtual register or register of PTRDISPREGSRegClass
494 if (Op->getOpcode() == ISD::ADD || Op->getOpcode() == ISD::SUB) {
495 SDValue CopyFromRegOp = Op->getOperand(Num: 0);
496 SDValue ImmOp = Op->getOperand(Num: 1);
497 ConstantSDNode *ImmNode = dyn_cast<ConstantSDNode>(Val&: ImmOp);
498
499 unsigned Reg;
500 bool CanHandleRegImmOpt = ImmNode && ImmNode->getAPIntValue().ult(RHS: 64);
501
502 if (CopyFromRegOp->getOpcode() == ISD::CopyFromReg) {
503 RegisterSDNode *RegNode =
504 cast<RegisterSDNode>(Val: CopyFromRegOp->getOperand(Num: 1));
505 Reg = RegNode->getReg();
506 CanHandleRegImmOpt &= (Register::isVirtualRegister(Reg) ||
507 AVR::PTRDISPREGSRegClass.contains(Reg));
508 } else {
509 CanHandleRegImmOpt = false;
510 }
511
512 // If we detect proper case - correct virtual register class
513 // if needed and go to another inlineasm operand.
514 if (CanHandleRegImmOpt) {
515 SDValue Base, Disp;
516
517 if (RI.getRegClass(Reg) != &AVR::PTRDISPREGSRegClass) {
518 SDLoc dl(CopyFromRegOp);
519
520 Register VReg = RI.createVirtualRegister(RegClass: &AVR::PTRDISPREGSRegClass);
521
522 SDValue CopyToReg =
523 CurDAG->getCopyToReg(Chain: CopyFromRegOp, dl, Reg: VReg, N: CopyFromRegOp);
524
525 SDValue NewCopyFromRegOp =
526 CurDAG->getCopyFromReg(Chain: CopyToReg, dl, Reg: VReg, VT: TL.getPointerTy(DL));
527
528 Base = NewCopyFromRegOp;
529 } else {
530 Base = CopyFromRegOp;
531 }
532
533 if (ImmNode->getValueType(ResNo: 0) != MVT::i8) {
534 Disp = CurDAG->getTargetConstant(Val: ImmNode->getZExtValue(), DL: dl, VT: MVT::i8);
535 } else {
536 Disp = ImmOp;
537 }
538
539 OutOps.push_back(x: Base);
540 OutOps.push_back(x: Disp);
541
542 return false;
543 }
544 }
545
546 // More generic case.
547 // Create chain that puts Op into pointer register
548 // and return that register.
549 Register VReg = RI.createVirtualRegister(RegClass: &AVR::PTRDISPREGSRegClass);
550
551 SDValue CopyToReg = CurDAG->getCopyToReg(Chain: Op, dl, Reg: VReg, N: Op);
552 SDValue CopyFromReg =
553 CurDAG->getCopyFromReg(Chain: CopyToReg, dl, Reg: VReg, VT: TL.getPointerTy(DL));
554
555 OutOps.push_back(x: CopyFromReg);
556
557 return false;
558}
559
560// Convert the frameindex into a temp instruction that will hold the effective
561// address of the final stack slot.
562template <> bool AVRDAGToDAGISel::select<ISD::FrameIndex>(SDNode *N) {
563 auto DL = CurDAG->getDataLayout();
564 auto PointerTy = getTargetLowering()->getPointerTy(DL);
565
566 int FI = cast<FrameIndexSDNode>(Val: N)->getIndex();
567 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT: PointerTy);
568 SDValue Offset = CurDAG->getTargetConstant(Val: 0, DL: SDLoc(N), VT: MVT::i16);
569 uint64_t Alignment = MF->getFrameInfo().getObjectAlign(ObjectIdx: FI).value();
570
571 if (Alignment == 1) {
572 CurDAG->SelectNodeTo(N, MachineOpc: AVR::FRMIDX, VT: PointerTy, Op1: TFI, Op2: Offset,
573 Op3: CurDAG->getRegister(Reg: AVR::R29R28, VT: PointerTy));
574 } else {
575 auto StackReg = MF->getInfo<AVRMachineFunctionInfo>()->AlignedStackReg;
576
577 CurDAG->SelectNodeTo(N, MachineOpc: AVR::FRMIDX, VT: PointerTy, Op1: TFI, Op2: Offset,
578 Op3: CurDAG->getRegister(Reg: StackReg, VT: PointerTy));
579 }
580
581 return true;
582}
583
584template <> bool AVRDAGToDAGISel::select<ISD::STORE>(SDNode *N) {
585 if (selectAlignedFrameStore(N)) {
586 return true;
587 }
588
589 // Use the STD{W}SPQRr pseudo instruction when passing arguments through
590 // the stack on function calls for further expansion during the PEI phase.
591 const StoreSDNode *ST = cast<StoreSDNode>(Val: N);
592 SDValue BasePtr = ST->getBasePtr();
593
594 // Early exit when the base pointer is a frame index node or a constant.
595 if (isa<FrameIndexSDNode>(Val: BasePtr) || isa<ConstantSDNode>(Val: BasePtr) ||
596 BasePtr.isUndef()) {
597 return false;
598 }
599
600 const RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Val: BasePtr.getOperand(i: 0));
601 // Only stores where SP is the base pointer are valid.
602 if (!RN || (RN->getReg() != AVR::SP)) {
603 return false;
604 }
605
606 int CST = (int)BasePtr.getConstantOperandVal(i: 1);
607 SDValue Chain = ST->getChain();
608 EVT VT = ST->getValue().getValueType();
609 SDLoc DL(N);
610 SDValue Offset = CurDAG->getTargetConstant(Val: CST, DL, VT: MVT::i16);
611 SDValue Ops[] = {BasePtr.getOperand(i: 0), Offset, ST->getValue(), Chain};
612 unsigned Opc = (VT == MVT::i16) ? AVR::STDWSPQRr : AVR::STDSPQRr;
613
614 SDNode *ResNode = CurDAG->getMachineNode(Opcode: Opc, dl: DL, VT: MVT::Other, Ops);
615
616 // Transfer memory operands.
617 CurDAG->setNodeMemRefs(N: cast<MachineSDNode>(Val: ResNode), NewMemRefs: {ST->getMemOperand()});
618
619 ReplaceUses(F: SDValue(N, 0), T: SDValue(ResNode, 0));
620 CurDAG->RemoveDeadNode(N);
621
622 return true;
623}
624
625template <> bool AVRDAGToDAGISel::select<ISD::LOAD>(SDNode *N) {
626 const LoadSDNode *LD = cast<LoadSDNode>(Val: N);
627 if (!AVR::isProgramMemoryAccess(N: LD)) {
628 return selectAlignedFrameLoad(N) || selectIndexedLoad(N);
629 }
630
631 if (!Subtarget->hasLPM())
632 report_fatal_error(reason: "cannot load from program memory on this mcu");
633
634 int ProgMemBank = AVR::getProgramMemoryBank(N: LD);
635 if (ProgMemBank < 0 || ProgMemBank > 5)
636 report_fatal_error(reason: "unexpected program memory bank");
637 if (ProgMemBank > 0 && !Subtarget->hasELPM())
638 report_fatal_error(reason: "unexpected program memory bank");
639
640 // This is a flash memory load, move the pointer into R31R30 and emit
641 // the lpm instruction.
642 MVT VT = LD->getMemoryVT().getSimpleVT();
643 SDValue Chain = LD->getChain();
644 SDValue Ptr = LD->getBasePtr();
645 SDNode *ResNode;
646 SDLoc DL(N);
647
648 Chain = CurDAG->getCopyToReg(Chain, dl: DL, Reg: AVR::R31R30, N: Ptr, Glue: SDValue());
649 Ptr = CurDAG->getCopyFromReg(Chain, dl: DL, Reg: AVR::R31R30, VT: MVT::i16,
650 Glue: Chain.getValue(R: 1));
651
652 // Check if the opcode can be converted into an indexed load.
653 if (unsigned LPMOpc = selectIndexedProgMemLoad(LD, VT, Bank: ProgMemBank)) {
654 // It is legal to fold the load into an indexed load.
655 if (ProgMemBank == 0) {
656 ResNode =
657 CurDAG->getMachineNode(Opcode: LPMOpc, dl: DL, VT1: VT, VT2: MVT::i16, VT3: MVT::Other, Ops: Ptr);
658 } else {
659 // Do not combine the LDI instruction into the ELPM pseudo instruction,
660 // since it may be reused by other ELPM pseudo instructions.
661 SDValue NC = CurDAG->getTargetConstant(Val: ProgMemBank, DL, VT: MVT::i8);
662 auto *NP = CurDAG->getMachineNode(Opcode: AVR::LDIRdK, dl: DL, VT: MVT::i8, Op1: NC);
663 ResNode = CurDAG->getMachineNode(Opcode: LPMOpc, dl: DL, VT1: VT, VT2: MVT::i16, VT3: MVT::Other,
664 Op1: Ptr, Op2: SDValue(NP, 0));
665 }
666 } else {
667 // Selecting an indexed load is not legal, fallback to a normal load.
668 switch (VT.SimpleTy) {
669 case MVT::i8:
670 if (ProgMemBank == 0) {
671 unsigned Opc = Subtarget->hasLPMX() ? AVR::LPMRdZ : AVR::LPMBRdZ;
672 ResNode = CurDAG->getMachineNode(Opcode: Opc, dl: DL, VT1: MVT::i8, VT2: MVT::Other, Ops: Ptr);
673 } else {
674 // Do not combine the LDI instruction into the ELPM pseudo instruction,
675 // since it may be reused by other ELPM pseudo instructions.
676 SDValue NC = CurDAG->getTargetConstant(Val: ProgMemBank, DL, VT: MVT::i8);
677 auto *NP = CurDAG->getMachineNode(Opcode: AVR::LDIRdK, dl: DL, VT: MVT::i8, Op1: NC);
678 ResNode = CurDAG->getMachineNode(Opcode: AVR::ELPMBRdZ, dl: DL, VT1: MVT::i8, VT2: MVT::Other,
679 Op1: Ptr, Op2: SDValue(NP, 0));
680 }
681 break;
682 case MVT::i16:
683 if (ProgMemBank == 0) {
684 ResNode =
685 CurDAG->getMachineNode(Opcode: AVR::LPMWRdZ, dl: DL, VT1: MVT::i16, VT2: MVT::Other, Ops: Ptr);
686 } else {
687 // Do not combine the LDI instruction into the ELPM pseudo instruction,
688 // since LDI requires the destination register in range R16~R31.
689 SDValue NC = CurDAG->getTargetConstant(Val: ProgMemBank, DL, VT: MVT::i8);
690 auto *NP = CurDAG->getMachineNode(Opcode: AVR::LDIRdK, dl: DL, VT: MVT::i8, Op1: NC);
691 ResNode = CurDAG->getMachineNode(Opcode: AVR::ELPMWRdZ, dl: DL, VT1: MVT::i16,
692 VT2: MVT::Other, Op1: Ptr, Op2: SDValue(NP, 0));
693 }
694 break;
695 default:
696 llvm_unreachable("Unsupported VT!");
697 }
698 }
699
700 // Transfer memory operands.
701 CurDAG->setNodeMemRefs(N: cast<MachineSDNode>(Val: ResNode), NewMemRefs: {LD->getMemOperand()});
702
703 ReplaceUses(F: SDValue(N, 0), T: SDValue(ResNode, 0));
704 ReplaceUses(F: SDValue(N, 1), T: SDValue(ResNode, 1));
705 CurDAG->RemoveDeadNode(N);
706
707 return true;
708}
709
710template <> bool AVRDAGToDAGISel::select<AVRISD::CALL>(SDNode *N) {
711 SDValue InGlue;
712 SDValue Chain = N->getOperand(Num: 0);
713 SDValue Callee = N->getOperand(Num: 1);
714 unsigned LastOpNum = N->getNumOperands() - 1;
715
716 // Direct calls are autogenerated.
717 unsigned Op = Callee.getOpcode();
718 if (Op == ISD::TargetGlobalAddress || Op == ISD::TargetExternalSymbol) {
719 return false;
720 }
721
722 // Skip the incoming flag if present
723 if (N->getOperand(Num: LastOpNum).getValueType() == MVT::Glue) {
724 --LastOpNum;
725 }
726
727 SDLoc DL(N);
728 Chain = CurDAG->getCopyToReg(Chain, dl: DL, Reg: AVR::R31R30, N: Callee, Glue: InGlue);
729 SmallVector<SDValue, 8> Ops;
730 Ops.push_back(Elt: CurDAG->getRegister(Reg: AVR::R31R30, VT: MVT::i16));
731
732 // Map all operands into the new node.
733 for (unsigned i = 2, e = LastOpNum + 1; i != e; ++i) {
734 Ops.push_back(Elt: N->getOperand(Num: i));
735 }
736
737 Ops.push_back(Elt: Chain);
738 Ops.push_back(Elt: Chain.getValue(R: 1));
739
740 SDNode *ResNode = CurDAG->getMachineNode(
741 Opcode: Subtarget->hasEIJMPCALL() ? AVR::EICALL : AVR::ICALL, dl: DL, VT1: MVT::Other,
742 VT2: MVT::Glue, Ops);
743
744 ReplaceUses(F: SDValue(N, 0), T: SDValue(ResNode, 0));
745 ReplaceUses(F: SDValue(N, 1), T: SDValue(ResNode, 1));
746 CurDAG->RemoveDeadNode(N);
747
748 return true;
749}
750
751template <> bool AVRDAGToDAGISel::select<ISD::BRIND>(SDNode *N) {
752 SDValue Chain = N->getOperand(Num: 0);
753 SDValue JmpAddr = N->getOperand(Num: 1);
754
755 SDLoc DL(N);
756 // Move the destination address of the indirect branch into R31R30.
757 Chain = CurDAG->getCopyToReg(Chain, dl: DL, Reg: AVR::R31R30, N: JmpAddr);
758 SDNode *ResNode = CurDAG->getMachineNode(Opcode: AVR::IJMP, dl: DL, VT: MVT::Other, Op1: Chain);
759
760 ReplaceUses(F: SDValue(N, 0), T: SDValue(ResNode, 0));
761 CurDAG->RemoveDeadNode(N);
762
763 return true;
764}
765
766bool AVRDAGToDAGISel::selectMultiplication(llvm::SDNode *N) {
767 SDLoc DL(N);
768 MVT Type = N->getSimpleValueType(ResNo: 0);
769
770 assert(Type == MVT::i8 && "unexpected value type");
771
772 bool isSigned = N->getOpcode() == ISD::SMUL_LOHI;
773 unsigned MachineOp = isSigned ? AVR::MULSRdRr : AVR::MULRdRr;
774
775 SDValue Lhs = N->getOperand(Num: 0);
776 SDValue Rhs = N->getOperand(Num: 1);
777 SDNode *Mul = CurDAG->getMachineNode(Opcode: MachineOp, dl: DL, VT: MVT::Glue, Op1: Lhs, Op2: Rhs);
778 SDValue InChain = CurDAG->getEntryNode();
779 SDValue InGlue = SDValue(Mul, 0);
780
781 // Copy the low half of the result, if it is needed.
782 if (N->hasAnyUseOfValue(Value: 0)) {
783 SDValue CopyFromLo =
784 CurDAG->getCopyFromReg(Chain: InChain, dl: DL, Reg: AVR::R0, VT: Type, Glue: InGlue);
785
786 ReplaceUses(F: SDValue(N, 0), T: CopyFromLo);
787
788 InChain = CopyFromLo.getValue(R: 1);
789 InGlue = CopyFromLo.getValue(R: 2);
790 }
791
792 // Copy the high half of the result, if it is needed.
793 if (N->hasAnyUseOfValue(Value: 1)) {
794 SDValue CopyFromHi =
795 CurDAG->getCopyFromReg(Chain: InChain, dl: DL, Reg: AVR::R1, VT: Type, Glue: InGlue);
796
797 ReplaceUses(F: SDValue(N, 1), T: CopyFromHi);
798
799 InChain = CopyFromHi.getValue(R: 1);
800 InGlue = CopyFromHi.getValue(R: 2);
801 }
802
803 CurDAG->RemoveDeadNode(N);
804
805 // We need to clear R1. This is currently done (dirtily)
806 // using a custom inserter.
807
808 return true;
809}
810
811void AVRDAGToDAGISel::Select(SDNode *N) {
812 // If we have a custom node, we already have selected!
813 if (N->isMachineOpcode()) {
814 LLVM_DEBUG(errs() << "== "; N->dump(CurDAG); errs() << "\n");
815 N->setNodeId(-1);
816 return;
817 }
818
819 // See if subclasses can handle this node.
820 if (trySelect(N))
821 return;
822
823 // Select the default instruction
824 SelectCode(N);
825}
826
827bool AVRDAGToDAGISel::trySelect(SDNode *N) {
828 unsigned Opcode = N->getOpcode();
829
830 switch (Opcode) {
831 // Nodes we fully handle.
832 case ISD::FrameIndex:
833 return select<ISD::FrameIndex>(N);
834 case ISD::BRIND:
835 return select<ISD::BRIND>(N);
836 case ISD::UMUL_LOHI:
837 case ISD::SMUL_LOHI:
838 return selectMultiplication(N);
839
840 // Nodes we handle partially. Other cases are autogenerated
841 case ISD::STORE:
842 return select<ISD::STORE>(N);
843 case ISD::LOAD:
844 return select<ISD::LOAD>(N);
845 case AVRISD::CALL:
846 return select<AVRISD::CALL>(N);
847 default:
848 return false;
849 }
850}
851
852AVRISelDAGToDAGPass::AVRISelDAGToDAGPass(AVRTargetMachine &TM,
853 CodeGenOptLevel OptLevel)
854 : SelectionDAGISelPass(std::make_unique<AVRDAGToDAGISel>(args&: TM, args&: OptLevel)) {}
855
856FunctionPass *llvm::createAVRISelDagLegacyPass(AVRTargetMachine &TM,
857 CodeGenOptLevel OptLevel) {
858 return new AVRDAGToDAGISelLegacy(TM, OptLevel);
859}
860