Path: blob/main/contrib/llvm-project/llvm/lib/Target/AVR/AVRRegisterInfo.cpp
96353 views
//===-- AVRRegisterInfo.cpp - AVR Register Information --------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the AVR implementation of the TargetRegisterInfo class.9//10//===----------------------------------------------------------------------===//1112#include "AVRRegisterInfo.h"1314#include "llvm/ADT/BitVector.h"15#include "llvm/CodeGen/MachineFrameInfo.h"16#include "llvm/CodeGen/MachineFunction.h"17#include "llvm/CodeGen/MachineInstrBuilder.h"18#include "llvm/CodeGen/MachineRegisterInfo.h"19#include "llvm/CodeGen/TargetFrameLowering.h"20#include "llvm/IR/Function.h"2122#include "AVR.h"23#include "AVRInstrInfo.h"24#include "AVRMachineFunctionInfo.h"25#include "AVRTargetMachine.h"26#include "MCTargetDesc/AVRMCTargetDesc.h"2728#define GET_REGINFO_TARGET_DESC29#include "AVRGenRegisterInfo.inc"3031namespace llvm {3233AVRRegisterInfo::AVRRegisterInfo() : AVRGenRegisterInfo(0) {}3435const uint16_t *36AVRRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {37const AVRMachineFunctionInfo *AFI = MF->getInfo<AVRMachineFunctionInfo>();38const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();39if (STI.hasTinyEncoding())40return AFI->isInterruptOrSignalHandler() ? CSR_InterruptsTiny_SaveList41: CSR_NormalTiny_SaveList;42else43return AFI->isInterruptOrSignalHandler() ? CSR_Interrupts_SaveList44: CSR_Normal_SaveList;45}4647const uint32_t *48AVRRegisterInfo::getCallPreservedMask(const MachineFunction &MF,49CallingConv::ID CC) const {50const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();51return STI.hasTinyEncoding() ? CSR_NormalTiny_RegMask : CSR_Normal_RegMask;52}5354BitVector AVRRegisterInfo::getReservedRegs(const MachineFunction &MF) const {55BitVector Reserved(getNumRegs());5657// Reserve the intermediate result registers r1 and r258// The result of instructions like 'mul' is always stored here.59// R0/R1/R1R0 are always reserved on both avr and avrtiny.60Reserved.set(AVR::R0);61Reserved.set(AVR::R1);62Reserved.set(AVR::R1R0);6364// Reserve the stack pointer.65Reserved.set(AVR::SPL);66Reserved.set(AVR::SPH);67Reserved.set(AVR::SP);6869// Reserve R2~R17 only on avrtiny.70if (MF.getSubtarget<AVRSubtarget>().hasTinyEncoding()) {71// Reserve 8-bit registers R2~R15, Rtmp(R16) and Zero(R17).72for (unsigned Reg = AVR::R2; Reg <= AVR::R17; Reg++)73Reserved.set(Reg);74// Reserve 16-bit registers R3R2~R18R17.75for (unsigned Reg = AVR::R3R2; Reg <= AVR::R18R17; Reg++)76Reserved.set(Reg);77}7879// We tenatively reserve the frame pointer register r29:r28 because the80// function may require one, but we cannot tell until register allocation81// is complete, which can be too late.82//83// Instead we just unconditionally reserve the Y register.84//85// TODO: Write a pass to enumerate functions which reserved the Y register86// but didn't end up needing a frame pointer. In these, we can87// convert one or two of the spills inside to use the Y register.88Reserved.set(AVR::R28);89Reserved.set(AVR::R29);90Reserved.set(AVR::R29R28);9192return Reserved;93}9495const TargetRegisterClass *96AVRRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,97const MachineFunction &MF) const {98const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();99if (TRI->isTypeLegalForClass(*RC, MVT::i16)) {100return &AVR::DREGSRegClass;101}102103if (TRI->isTypeLegalForClass(*RC, MVT::i8)) {104return &AVR::GPR8RegClass;105}106107llvm_unreachable("Invalid register size");108}109110/// Fold a frame offset shared between two add instructions into a single one.111static void foldFrameOffset(MachineBasicBlock::iterator &II, int &Offset,112Register DstReg) {113MachineInstr &MI = *II;114int Opcode = MI.getOpcode();115116// Don't bother trying if the next instruction is not an add or a sub.117if ((Opcode != AVR::SUBIWRdK) && (Opcode != AVR::ADIWRdK)) {118return;119}120121// Check that DstReg matches with next instruction, otherwise the instruction122// is not related to stack address manipulation.123if (DstReg != MI.getOperand(0).getReg()) {124return;125}126127// Add the offset in the next instruction to our offset.128switch (Opcode) {129case AVR::SUBIWRdK:130Offset += -MI.getOperand(2).getImm();131break;132case AVR::ADIWRdK:133Offset += MI.getOperand(2).getImm();134break;135}136137// Finally remove the instruction.138II++;139MI.eraseFromParent();140}141142bool AVRRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,143int SPAdj, unsigned FIOperandNum,144RegScavenger *RS) const {145assert(SPAdj == 0 && "Unexpected SPAdj value");146147MachineInstr &MI = *II;148DebugLoc dl = MI.getDebugLoc();149MachineBasicBlock &MBB = *MI.getParent();150const MachineFunction &MF = *MBB.getParent();151const AVRTargetMachine &TM = (const AVRTargetMachine &)MF.getTarget();152const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();153const MachineFrameInfo &MFI = MF.getFrameInfo();154const TargetFrameLowering *TFI = TM.getSubtargetImpl()->getFrameLowering();155const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();156int FrameIndex = MI.getOperand(FIOperandNum).getIndex();157int Offset = MFI.getObjectOffset(FrameIndex);158159// Add one to the offset because SP points to an empty slot.160Offset += MFI.getStackSize() - TFI->getOffsetOfLocalArea() + 1;161// Fold incoming offset.162Offset += MI.getOperand(FIOperandNum + 1).getImm();163164// This is actually "load effective address" of the stack slot165// instruction. We have only two-address instructions, thus we need to166// expand it into move + add.167if (MI.getOpcode() == AVR::FRMIDX) {168Register DstReg = MI.getOperand(0).getReg();169assert(DstReg != AVR::R29R28 && "Dest reg cannot be the frame pointer");170171// Copy the frame pointer.172if (STI.hasMOVW()) {173BuildMI(MBB, MI, dl, TII.get(AVR::MOVWRdRr), DstReg)174.addReg(AVR::R29R28);175} else {176Register DstLoReg, DstHiReg;177splitReg(DstReg, DstLoReg, DstHiReg);178BuildMI(MBB, MI, dl, TII.get(AVR::MOVRdRr), DstLoReg)179.addReg(AVR::R28);180BuildMI(MBB, MI, dl, TII.get(AVR::MOVRdRr), DstHiReg)181.addReg(AVR::R29);182}183184assert(Offset > 0 && "Invalid offset");185186// We need to materialize the offset via an add instruction.187unsigned Opcode;188189II++; // Skip over the FRMIDX instruction.190191// Generally, to load a frame address two add instructions are emitted that192// could get folded into a single one:193// movw r31:r30, r29:r28194// adiw r31:r30, 29195// adiw r31:r30, 16196// to:197// movw r31:r30, r29:r28198// adiw r31:r30, 45199if (II != MBB.end())200foldFrameOffset(II, Offset, DstReg);201202// Select the best opcode based on DstReg and the offset size.203switch (DstReg) {204case AVR::R25R24:205case AVR::R27R26:206case AVR::R31R30: {207if (isUInt<6>(Offset) && STI.hasADDSUBIW()) {208Opcode = AVR::ADIWRdK;209break;210}211[[fallthrough]];212}213default: {214// This opcode will get expanded into a pair of subi/sbci.215Opcode = AVR::SUBIWRdK;216Offset = -Offset;217break;218}219}220221MachineInstr *New = BuildMI(MBB, II, dl, TII.get(Opcode), DstReg)222.addReg(DstReg, RegState::Kill)223.addImm(Offset);224New->getOperand(3).setIsDead();225226MI.eraseFromParent(); // remove FRMIDX227228return false;229}230231// On most AVRs, we can use an offset up to 62 for load/store with232// displacement (63 for byte values, 62 for word values). However, the233// "reduced tiny" cores don't support load/store with displacement. So for234// them, we force an offset of 0 meaning that any positive offset will require235// adjusting the frame pointer.236int MaxOffset = STI.hasTinyEncoding() ? 0 : 62;237238// If the offset is too big we have to adjust and restore the frame pointer239// to materialize a valid load/store with displacement.240//: TODO: consider using only one adiw/sbiw chain for more than one frame241//: index242if (Offset > MaxOffset) {243unsigned AddOpc = AVR::ADIWRdK, SubOpc = AVR::SBIWRdK;244int AddOffset = Offset - MaxOffset;245246// For huge offsets where adiw/sbiw cannot be used use a pair of subi/sbci.247if ((Offset - MaxOffset) > 63 || !STI.hasADDSUBIW()) {248AddOpc = AVR::SUBIWRdK;249SubOpc = AVR::SUBIWRdK;250AddOffset = -AddOffset;251}252253// It is possible that the spiller places this frame instruction in between254// a compare and branch, invalidating the contents of SREG set by the255// compare instruction because of the add/sub pairs. Conservatively save and256// restore SREG before and after each add/sub pair.257BuildMI(MBB, II, dl, TII.get(AVR::INRdA), STI.getTmpRegister())258.addImm(STI.getIORegSREG());259260MachineInstr *New = BuildMI(MBB, II, dl, TII.get(AddOpc), AVR::R29R28)261.addReg(AVR::R29R28, RegState::Kill)262.addImm(AddOffset);263New->getOperand(3).setIsDead();264265// Restore SREG.266BuildMI(MBB, std::next(II), dl, TII.get(AVR::OUTARr))267.addImm(STI.getIORegSREG())268.addReg(STI.getTmpRegister(), RegState::Kill);269270// No need to set SREG as dead here otherwise if the next instruction is a271// cond branch it will be using a dead register.272BuildMI(MBB, std::next(II), dl, TII.get(SubOpc), AVR::R29R28)273.addReg(AVR::R29R28, RegState::Kill)274.addImm(Offset - MaxOffset);275276Offset = MaxOffset;277}278279MI.getOperand(FIOperandNum).ChangeToRegister(AVR::R29R28, false);280assert(isUInt<6>(Offset) && "Offset is out of range");281MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);282return false;283}284285Register AVRRegisterInfo::getFrameRegister(const MachineFunction &MF) const {286const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();287if (TFI->hasFP(MF)) {288// The Y pointer register289return AVR::R28;290}291292return AVR::SP;293}294295const TargetRegisterClass *296AVRRegisterInfo::getPointerRegClass(const MachineFunction &MF,297unsigned Kind) const {298// FIXME: Currently we're using avr-gcc as reference, so we restrict299// ptrs to Y and Z regs. Though avr-gcc has buggy implementation300// of memory constraint, so we can fix it and bit avr-gcc here ;-)301return &AVR::PTRDISPREGSRegClass;302}303304void AVRRegisterInfo::splitReg(Register Reg, Register &LoReg,305Register &HiReg) const {306assert(AVR::DREGSRegClass.contains(Reg) && "can only split 16-bit registers");307308LoReg = getSubReg(Reg, AVR::sub_lo);309HiReg = getSubReg(Reg, AVR::sub_hi);310}311312bool AVRRegisterInfo::shouldCoalesce(313MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg,314const TargetRegisterClass *DstRC, unsigned DstSubReg,315const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {316if (this->getRegClass(AVR::PTRDISPREGSRegClassID)->hasSubClassEq(NewRC)) {317return false;318}319320return TargetRegisterInfo::shouldCoalesce(MI, SrcRC, SubReg, DstRC, DstSubReg,321NewRC, LIS);322}323324} // end of namespace llvm325326327