16d8078f9SDylan McKay //===-- AVRRegisterInfo.cpp - AVR Register Information --------------------===//
26d8078f9SDylan McKay //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66d8078f9SDylan McKay //
76d8078f9SDylan McKay //===----------------------------------------------------------------------===//
86d8078f9SDylan McKay //
96d8078f9SDylan McKay // This file contains the AVR implementation of the TargetRegisterInfo class.
106d8078f9SDylan McKay //
116d8078f9SDylan McKay //===----------------------------------------------------------------------===//
126d8078f9SDylan McKay 
136d8078f9SDylan McKay #include "AVRRegisterInfo.h"
146d8078f9SDylan McKay 
156d8078f9SDylan McKay #include "llvm/ADT/BitVector.h"
166d8078f9SDylan McKay #include "llvm/CodeGen/MachineFrameInfo.h"
176d8078f9SDylan McKay #include "llvm/CodeGen/MachineFunction.h"
186d8078f9SDylan McKay #include "llvm/CodeGen/MachineInstrBuilder.h"
1945eb4c7eSDylan McKay #include "llvm/CodeGen/MachineRegisterInfo.h"
201be62f03SDavid Blaikie #include "llvm/CodeGen/TargetFrameLowering.h"
215449d2daSShivam Gupta #include "llvm/IR/Function.h"
226d8078f9SDylan McKay 
236d8078f9SDylan McKay #include "AVR.h"
246d8078f9SDylan McKay #include "AVRInstrInfo.h"
25339b3426SDylan McKay #include "AVRMachineFunctionInfo.h"
266d8078f9SDylan McKay #include "AVRTargetMachine.h"
276d8078f9SDylan McKay #include "MCTargetDesc/AVRMCTargetDesc.h"
286d8078f9SDylan McKay 
296d8078f9SDylan McKay #define GET_REGINFO_TARGET_DESC
306d8078f9SDylan McKay #include "AVRGenRegisterInfo.inc"
316d8078f9SDylan McKay 
326d8078f9SDylan McKay namespace llvm {
336d8078f9SDylan McKay 
AVRRegisterInfo()346d8078f9SDylan McKay AVRRegisterInfo::AVRRegisterInfo() : AVRGenRegisterInfo(0) {}
356d8078f9SDylan McKay 
366d8078f9SDylan McKay const uint16_t *
getCalleeSavedRegs(const MachineFunction * MF) const376d8078f9SDylan McKay AVRRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
38339b3426SDylan McKay   const AVRMachineFunctionInfo *AFI = MF->getInfo<AVRMachineFunctionInfo>();
39*f319c245SBen Shi   const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();
40*f319c245SBen Shi   if (STI.hasTinyEncoding())
41*f319c245SBen Shi     return AFI->isInterruptOrSignalHandler() ? CSR_InterruptsTiny_SaveList
42*f319c245SBen Shi                                              : CSR_NormalTiny_SaveList;
43*f319c245SBen Shi   else
445449d2daSShivam Gupta     return AFI->isInterruptOrSignalHandler() ? CSR_Interrupts_SaveList
45339b3426SDylan McKay                                              : CSR_Normal_SaveList;
466d8078f9SDylan McKay }
476d8078f9SDylan McKay 
486d8078f9SDylan McKay const uint32_t *
getCallPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const496d8078f9SDylan McKay AVRRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
506d8078f9SDylan McKay                                       CallingConv::ID CC) const {
51*f319c245SBen Shi   const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
52*f319c245SBen Shi   return STI.hasTinyEncoding() ? CSR_NormalTiny_RegMask : CSR_Normal_RegMask;
536d8078f9SDylan McKay }
546d8078f9SDylan McKay 
getReservedRegs(const MachineFunction & MF) const556d8078f9SDylan McKay BitVector AVRRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
566d8078f9SDylan McKay   BitVector Reserved(getNumRegs());
576d8078f9SDylan McKay 
586d8078f9SDylan McKay   // Reserve the intermediate result registers r1 and r2
596d8078f9SDylan McKay   // The result of instructions like 'mul' is always stored here.
60*f319c245SBen Shi   // R0/R1/R1R0 are always reserved on both avr and avrtiny.
616d8078f9SDylan McKay   Reserved.set(AVR::R0);
626d8078f9SDylan McKay   Reserved.set(AVR::R1);
636d8078f9SDylan McKay   Reserved.set(AVR::R1R0);
646d8078f9SDylan McKay 
656d8078f9SDylan McKay   // Reserve the stack pointer.
666d8078f9SDylan McKay   Reserved.set(AVR::SPL);
676d8078f9SDylan McKay   Reserved.set(AVR::SPH);
686d8078f9SDylan McKay   Reserved.set(AVR::SP);
696d8078f9SDylan McKay 
70*f319c245SBen Shi   // Reserve R2~R17 only on avrtiny.
71*f319c245SBen Shi   if (MF.getSubtarget<AVRSubtarget>().hasTinyEncoding()) {
72*f319c245SBen Shi     // Reserve 8-bit registers R2~R15, Rtmp(R16) and Zero(R17).
73*f319c245SBen Shi     for (unsigned Reg = AVR::R2; Reg <= AVR::R17; Reg++)
74*f319c245SBen Shi       Reserved.set(Reg);
75*f319c245SBen Shi     // Reserve 16-bit registers R3R2~R18R17.
76*f319c245SBen Shi     for (unsigned Reg = AVR::R3R2; Reg <= AVR::R18R17; Reg++)
77*f319c245SBen Shi       Reserved.set(Reg);
78*f319c245SBen Shi   }
79*f319c245SBen Shi 
804aedb8a6SDylan McKay   // We tenatively reserve the frame pointer register r29:r28 because the
814aedb8a6SDylan McKay   // function may require one, but we cannot tell until register allocation
824aedb8a6SDylan McKay   // is complete, which can be too late.
834aedb8a6SDylan McKay   //
844aedb8a6SDylan McKay   // Instead we just unconditionally reserve the Y register.
854aedb8a6SDylan McKay   //
864aedb8a6SDylan McKay   // TODO: Write a pass to enumerate functions which reserved the Y register
874aedb8a6SDylan McKay   //       but didn't end up needing a frame pointer. In these, we can
884aedb8a6SDylan McKay   //       convert one or two of the spills inside to use the Y register.
896d8078f9SDylan McKay   Reserved.set(AVR::R28);
906d8078f9SDylan McKay   Reserved.set(AVR::R29);
916d8078f9SDylan McKay   Reserved.set(AVR::R29R28);
926d8078f9SDylan McKay 
936d8078f9SDylan McKay   return Reserved;
946d8078f9SDylan McKay }
956d8078f9SDylan McKay 
966d8078f9SDylan McKay const TargetRegisterClass *
getLargestLegalSuperClass(const TargetRegisterClass * RC,const MachineFunction & MF) const976d8078f9SDylan McKay AVRRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
986d8078f9SDylan McKay                                            const MachineFunction &MF) const {
99c8e8e2a0SKrzysztof Parzyszek   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
100c8e8e2a0SKrzysztof Parzyszek   if (TRI->isTypeLegalForClass(*RC, MVT::i16)) {
1016d8078f9SDylan McKay     return &AVR::DREGSRegClass;
1026d8078f9SDylan McKay   }
1036d8078f9SDylan McKay 
104c8e8e2a0SKrzysztof Parzyszek   if (TRI->isTypeLegalForClass(*RC, MVT::i8)) {
1056d8078f9SDylan McKay     return &AVR::GPR8RegClass;
1066d8078f9SDylan McKay   }
1076d8078f9SDylan McKay 
1086d8078f9SDylan McKay   llvm_unreachable("Invalid register size");
1096d8078f9SDylan McKay }
1106d8078f9SDylan McKay 
1116d8078f9SDylan McKay /// Fold a frame offset shared between two add instructions into a single one.
foldFrameOffset(MachineBasicBlock::iterator & II,int & Offset,Register DstReg)112ea6eb813SJim Lin static void foldFrameOffset(MachineBasicBlock::iterator &II, int &Offset,
113ea6eb813SJim Lin                             Register DstReg) {
114b224d985SDylan McKay   MachineInstr &MI = *II;
1156d8078f9SDylan McKay   int Opcode = MI.getOpcode();
1166d8078f9SDylan McKay 
1176d8078f9SDylan McKay   // Don't bother trying if the next instruction is not an add or a sub.
1186d8078f9SDylan McKay   if ((Opcode != AVR::SUBIWRdK) && (Opcode != AVR::ADIWRdK)) {
1196d8078f9SDylan McKay     return;
1206d8078f9SDylan McKay   }
1216d8078f9SDylan McKay 
1226d8078f9SDylan McKay   // Check that DstReg matches with next instruction, otherwise the instruction
1236d8078f9SDylan McKay   // is not related to stack address manipulation.
1246d8078f9SDylan McKay   if (DstReg != MI.getOperand(0).getReg()) {
1256d8078f9SDylan McKay     return;
1266d8078f9SDylan McKay   }
1276d8078f9SDylan McKay 
1286d8078f9SDylan McKay   // Add the offset in the next instruction to our offset.
1296d8078f9SDylan McKay   switch (Opcode) {
1306d8078f9SDylan McKay   case AVR::SUBIWRdK:
1316d8078f9SDylan McKay     Offset += -MI.getOperand(2).getImm();
1326d8078f9SDylan McKay     break;
1336d8078f9SDylan McKay   case AVR::ADIWRdK:
1346d8078f9SDylan McKay     Offset += MI.getOperand(2).getImm();
1356d8078f9SDylan McKay     break;
1366d8078f9SDylan McKay   }
1376d8078f9SDylan McKay 
1386d8078f9SDylan McKay   // Finally remove the instruction.
139b224d985SDylan McKay   II++;
1406d8078f9SDylan McKay   MI.eraseFromParent();
1416d8078f9SDylan McKay }
1426d8078f9SDylan McKay 
eliminateFrameIndex(MachineBasicBlock::iterator II,int SPAdj,unsigned FIOperandNum,RegScavenger * RS) const1436d8078f9SDylan McKay void AVRRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1446d8078f9SDylan McKay                                           int SPAdj, unsigned FIOperandNum,
1456d8078f9SDylan McKay                                           RegScavenger *RS) const {
1466d8078f9SDylan McKay   assert(SPAdj == 0 && "Unexpected SPAdj value");
1476d8078f9SDylan McKay 
1486d8078f9SDylan McKay   MachineInstr &MI = *II;
1496d8078f9SDylan McKay   DebugLoc dl = MI.getDebugLoc();
1506d8078f9SDylan McKay   MachineBasicBlock &MBB = *MI.getParent();
1516d8078f9SDylan McKay   const MachineFunction &MF = *MBB.getParent();
1526d8078f9SDylan McKay   const AVRTargetMachine &TM = (const AVRTargetMachine &)MF.getTarget();
1536d8078f9SDylan McKay   const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
154941a705bSMatthias Braun   const MachineFrameInfo &MFI = MF.getFrameInfo();
1556d8078f9SDylan McKay   const TargetFrameLowering *TFI = TM.getSubtargetImpl()->getFrameLowering();
15631666478SAyke van Laethem   const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
1576d8078f9SDylan McKay   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
158941a705bSMatthias Braun   int Offset = MFI.getObjectOffset(FrameIndex);
1596d8078f9SDylan McKay 
1606d8078f9SDylan McKay   // Add one to the offset because SP points to an empty slot.
161941a705bSMatthias Braun   Offset += MFI.getStackSize() - TFI->getOffsetOfLocalArea() + 1;
1626d8078f9SDylan McKay   // Fold incoming offset.
1636d8078f9SDylan McKay   Offset += MI.getOperand(FIOperandNum + 1).getImm();
1646d8078f9SDylan McKay 
1656d8078f9SDylan McKay   // This is actually "load effective address" of the stack slot
1666d8078f9SDylan McKay   // instruction. We have only two-address instructions, thus we need to
1676d8078f9SDylan McKay   // expand it into move + add.
1686d8078f9SDylan McKay   if (MI.getOpcode() == AVR::FRMIDX) {
1696d8078f9SDylan McKay     MI.setDesc(TII.get(AVR::MOVWRdRr));
1706d8078f9SDylan McKay     MI.getOperand(FIOperandNum).ChangeToRegister(AVR::R29R28, false);
17137b37838SShengchen Kan     MI.removeOperand(2);
1726d8078f9SDylan McKay 
1736d8078f9SDylan McKay     assert(Offset > 0 && "Invalid offset");
1746d8078f9SDylan McKay 
1756d8078f9SDylan McKay     // We need to materialize the offset via an add instruction.
1766d8078f9SDylan McKay     unsigned Opcode;
1770c476111SDaniel Sanders     Register DstReg = MI.getOperand(0).getReg();
1786d8078f9SDylan McKay     assert(DstReg != AVR::R29R28 && "Dest reg cannot be the frame pointer");
1796d8078f9SDylan McKay 
180b224d985SDylan McKay     II++; // Skip over the FRMIDX (and now MOVW) instruction.
181b224d985SDylan McKay 
1826d8078f9SDylan McKay     // Generally, to load a frame address two add instructions are emitted that
1836d8078f9SDylan McKay     // could get folded into a single one:
1846d8078f9SDylan McKay     //  movw    r31:r30, r29:r28
1856d8078f9SDylan McKay     //  adiw    r31:r30, 29
1866d8078f9SDylan McKay     //  adiw    r31:r30, 16
1876d8078f9SDylan McKay     // to:
1886d8078f9SDylan McKay     //  movw    r31:r30, r29:r28
1896d8078f9SDylan McKay     //  adiw    r31:r30, 45
190b224d985SDylan McKay     if (II != MBB.end())
191b224d985SDylan McKay       foldFrameOffset(II, Offset, DstReg);
1926d8078f9SDylan McKay 
1936d8078f9SDylan McKay     // Select the best opcode based on DstReg and the offset size.
1946d8078f9SDylan McKay     switch (DstReg) {
1956d8078f9SDylan McKay     case AVR::R25R24:
1966d8078f9SDylan McKay     case AVR::R27R26:
1976d8078f9SDylan McKay     case AVR::R31R30: {
1986d8078f9SDylan McKay       if (isUInt<6>(Offset)) {
1996d8078f9SDylan McKay         Opcode = AVR::ADIWRdK;
2006d8078f9SDylan McKay         break;
2016d8078f9SDylan McKay       }
202b03fd12cSJustin Bogner       LLVM_FALLTHROUGH;
2036d8078f9SDylan McKay     }
2046d8078f9SDylan McKay     default: {
2056d8078f9SDylan McKay       // This opcode will get expanded into a pair of subi/sbci.
2066d8078f9SDylan McKay       Opcode = AVR::SUBIWRdK;
2076d8078f9SDylan McKay       Offset = -Offset;
2086d8078f9SDylan McKay       break;
2096d8078f9SDylan McKay     }
2106d8078f9SDylan McKay     }
2116d8078f9SDylan McKay 
212b224d985SDylan McKay     MachineInstr *New = BuildMI(MBB, II, dl, TII.get(Opcode), DstReg)
2136d8078f9SDylan McKay                             .addReg(DstReg, RegState::Kill)
2146d8078f9SDylan McKay                             .addImm(Offset);
2156d8078f9SDylan McKay     New->getOperand(3).setIsDead();
2166d8078f9SDylan McKay 
2176d8078f9SDylan McKay     return;
2186d8078f9SDylan McKay   }
2196d8078f9SDylan McKay 
2206d8078f9SDylan McKay   // If the offset is too big we have to adjust and restore the frame pointer
2216d8078f9SDylan McKay   // to materialize a valid load/store with displacement.
2225449d2daSShivam Gupta   //: TODO: consider using only one adiw/sbiw chain for more than one frame
2235449d2daSShivam Gupta   //: index
224c4b002bfSDylan McKay   if (Offset > 62) {
2256d8078f9SDylan McKay     unsigned AddOpc = AVR::ADIWRdK, SubOpc = AVR::SBIWRdK;
2266d8078f9SDylan McKay     int AddOffset = Offset - 63 + 1;
2276d8078f9SDylan McKay 
2286d8078f9SDylan McKay     // For huge offsets where adiw/sbiw cannot be used use a pair of subi/sbci.
2296d8078f9SDylan McKay     if ((Offset - 63 + 1) > 63) {
2306d8078f9SDylan McKay       AddOpc = AVR::SUBIWRdK;
2316d8078f9SDylan McKay       SubOpc = AVR::SUBIWRdK;
2326d8078f9SDylan McKay       AddOffset = -AddOffset;
2336d8078f9SDylan McKay     }
2346d8078f9SDylan McKay 
2356d8078f9SDylan McKay     // It is possible that the spiller places this frame instruction in between
2366d8078f9SDylan McKay     // a compare and branch, invalidating the contents of SREG set by the
2376d8078f9SDylan McKay     // compare instruction because of the add/sub pairs. Conservatively save and
2386d8078f9SDylan McKay     // restore SREG before and after each add/sub pair.
23931666478SAyke van Laethem     BuildMI(MBB, II, dl, TII.get(AVR::INRdA), AVR::R0)
24031666478SAyke van Laethem         .addImm(STI.getIORegSREG());
2416d8078f9SDylan McKay 
2426d8078f9SDylan McKay     MachineInstr *New = BuildMI(MBB, II, dl, TII.get(AddOpc), AVR::R29R28)
2436d8078f9SDylan McKay                             .addReg(AVR::R29R28, RegState::Kill)
2446d8078f9SDylan McKay                             .addImm(AddOffset);
2456d8078f9SDylan McKay     New->getOperand(3).setIsDead();
2466d8078f9SDylan McKay 
2476d8078f9SDylan McKay     // Restore SREG.
2486d8078f9SDylan McKay     BuildMI(MBB, std::next(II), dl, TII.get(AVR::OUTARr))
24931666478SAyke van Laethem         .addImm(STI.getIORegSREG())
2506d8078f9SDylan McKay         .addReg(AVR::R0, RegState::Kill);
2516d8078f9SDylan McKay 
2526d8078f9SDylan McKay     // No need to set SREG as dead here otherwise if the next instruction is a
2536d8078f9SDylan McKay     // cond branch it will be using a dead register.
254b251cc0dSFangrui Song     BuildMI(MBB, std::next(II), dl, TII.get(SubOpc), AVR::R29R28)
2556d8078f9SDylan McKay         .addReg(AVR::R29R28, RegState::Kill)
2566d8078f9SDylan McKay         .addImm(Offset - 63 + 1);
2576d8078f9SDylan McKay 
2586d8078f9SDylan McKay     Offset = 62;
2596d8078f9SDylan McKay   }
2606d8078f9SDylan McKay 
2616d8078f9SDylan McKay   MI.getOperand(FIOperandNum).ChangeToRegister(AVR::R29R28, false);
2626d8078f9SDylan McKay   assert(isUInt<6>(Offset) && "Offset is out of range");
2636d8078f9SDylan McKay   MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
2646d8078f9SDylan McKay }
2656d8078f9SDylan McKay 
getFrameRegister(const MachineFunction & MF) const26688139c14SAyke van Laethem Register AVRRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
2676d8078f9SDylan McKay   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
2686d8078f9SDylan McKay   if (TFI->hasFP(MF)) {
2696d8078f9SDylan McKay     // The Y pointer register
2706d8078f9SDylan McKay     return AVR::R28;
2716d8078f9SDylan McKay   }
2726d8078f9SDylan McKay 
2736d8078f9SDylan McKay   return AVR::SP;
2746d8078f9SDylan McKay }
2756d8078f9SDylan McKay 
2766d8078f9SDylan McKay const TargetRegisterClass *
getPointerRegClass(const MachineFunction & MF,unsigned Kind) const2776d8078f9SDylan McKay AVRRegisterInfo::getPointerRegClass(const MachineFunction &MF,
2786d8078f9SDylan McKay                                     unsigned Kind) const {
2796d8078f9SDylan McKay   // FIXME: Currently we're using avr-gcc as reference, so we restrict
2806d8078f9SDylan McKay   // ptrs to Y and Z regs. Though avr-gcc has buggy implementation
2816d8078f9SDylan McKay   // of memory constraint, so we can fix it and bit avr-gcc here ;-)
2826d8078f9SDylan McKay   return &AVR::PTRDISPREGSRegClass;
2836d8078f9SDylan McKay }
2846d8078f9SDylan McKay 
splitReg(Register Reg,Register & LoReg,Register & HiReg) const285ea6eb813SJim Lin void AVRRegisterInfo::splitReg(Register Reg, Register &LoReg,
286ea6eb813SJim Lin                                Register &HiReg) const {
28782ef7709SDylan McKay   assert(AVR::DREGSRegClass.contains(Reg) && "can only split 16-bit registers");
28882ef7709SDylan McKay 
28982ef7709SDylan McKay   LoReg = getSubReg(Reg, AVR::sub_lo);
29082ef7709SDylan McKay   HiReg = getSubReg(Reg, AVR::sub_hi);
29182ef7709SDylan McKay }
29282ef7709SDylan McKay 
shouldCoalesce(MachineInstr * MI,const TargetRegisterClass * SrcRC,unsigned SubReg,const TargetRegisterClass * DstRC,unsigned DstSubReg,const TargetRegisterClass * NewRC,LiveIntervals & LIS) const2935449d2daSShivam Gupta bool AVRRegisterInfo::shouldCoalesce(
2945449d2daSShivam Gupta     MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg,
2955449d2daSShivam Gupta     const TargetRegisterClass *DstRC, unsigned DstSubReg,
2965449d2daSShivam Gupta     const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {
29745eb4c7eSDylan McKay   if (this->getRegClass(AVR::PTRDISPREGSRegClassID)->hasSubClassEq(NewRC)) {
29845eb4c7eSDylan McKay     return false;
29945eb4c7eSDylan McKay   }
30045eb4c7eSDylan McKay 
3015449d2daSShivam Gupta   return TargetRegisterInfo::shouldCoalesce(MI, SrcRC, SubReg, DstRC, DstSubReg,
3025449d2daSShivam Gupta                                             NewRC, LIS);
30345eb4c7eSDylan McKay }
30445eb4c7eSDylan McKay 
3056d8078f9SDylan McKay } // end of namespace llvm
306