LLVM 24.0.0git
AVRRegisterInfo.cpp
Go to the documentation of this file.
1//===-- AVRRegisterInfo.cpp - AVR Register Information --------------------===//
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 contains the AVR implementation of the TargetRegisterInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AVRRegisterInfo.h"
14
15#include "llvm/ADT/BitVector.h"
20
21#include "AVR.h"
22#include "AVRInstrInfo.h"
24#include "AVRTargetMachine.h"
26
27#define GET_REGINFO_TARGET_DESC
28#include "AVRGenRegisterInfo.inc"
29
30namespace llvm {
31
33
34const uint16_t *
37 const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>();
38 if (STI.hasTinyEncoding())
39 return AFI->isInterruptOrSignalHandler() ? CSR_InterruptsTiny_SaveList
40 : CSR_NormalTiny_SaveList;
41 else
42 return AFI->isInterruptOrSignalHandler() ? CSR_Interrupts_SaveList
43 : CSR_Normal_SaveList;
44}
45
46const uint32_t *
48 CallingConv::ID CC) const {
49 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
50 return STI.hasTinyEncoding() ? CSR_NormalTiny_RegMask : CSR_Normal_RegMask;
51}
52
54 BitVector Reserved(getNumRegs());
55
56 // Reserve the intermediate result registers r1 and r2
57 // The result of instructions like 'mul' is always stored here.
58 // R0/R1/R1R0 are always reserved on both avr and avrtiny.
59 Reserved.set(AVR::R0);
60 Reserved.set(AVR::R1);
61 Reserved.set(AVR::R1R0);
62
63 // Reserve the stack pointer.
64 Reserved.set(AVR::SPL);
65 Reserved.set(AVR::SPH);
66 Reserved.set(AVR::SP);
67
68 // Reserve R2~R17 only on avrtiny.
69 if (MF.getSubtarget<AVRSubtarget>().hasTinyEncoding()) {
70 // Reserve 8-bit registers R2~R15, Rtmp(R16) and Zero(R17).
71 for (unsigned Reg = AVR::R2; Reg <= AVR::R17; Reg++)
72 Reserved.set(Reg);
73 // Reserve 16-bit registers R3R2~R18R17.
74 for (unsigned Reg = AVR::R3R2; Reg <= AVR::R18R17; Reg++)
75 Reserved.set(Reg);
76 }
77
78 // We tenatively reserve the frame pointer register r29:r28 because the
79 // function may require one, but we cannot tell until register allocation
80 // is complete, which can be too late.
81 //
82 // Instead we just unconditionally reserve the Y register.
83 //
84 // TODO: Write a pass to enumerate functions which reserved the Y register
85 // but didn't end up needing a frame pointer. In these, we can
86 // convert one or two of the spills inside to use the Y register.
87 Reserved.set(AVR::R28);
88 Reserved.set(AVR::R29);
89 Reserved.set(AVR::R29R28);
90
91 return Reserved;
92}
93
96 const MachineFunction &MF) const {
98 if (TRI->isTypeLegalForClass(*RC, MVT::i16)) {
99 return &AVR::DREGSRegClass;
100 }
101
102 if (TRI->isTypeLegalForClass(*RC, MVT::i8)) {
103 return &AVR::GPR8RegClass;
104 }
105
106 llvm_unreachable("Invalid register size");
107}
108
109/// Fold a frame offset shared between two add instructions into a single one.
111 Register DstReg) {
112 MachineInstr &MI = *II;
113 int Opcode = MI.getOpcode();
114
115 // Don't bother trying if the next instruction is not an add or a sub.
116 if ((Opcode != AVR::SUBIWRdK) && (Opcode != AVR::ADIWRdK)) {
117 return;
118 }
119
120 // Check that DstReg matches with next instruction, otherwise the instruction
121 // is not related to stack address manipulation.
122 if (DstReg != MI.getOperand(0).getReg()) {
123 return;
124 }
125
126 // Add the offset in the next instruction to our offset.
127 switch (Opcode) {
128 case AVR::SUBIWRdK:
129 Offset += -MI.getOperand(2).getImm();
130 break;
131 case AVR::ADIWRdK:
132 Offset += MI.getOperand(2).getImm();
133 break;
134 }
135
136 // Finally remove the instruction.
137 II++;
138 MI.eraseFromParent();
139}
140
142 int SPAdj, unsigned FIOperandNum,
143 RegScavenger *RS) const {
144 assert(SPAdj == 0 && "Unexpected SPAdj value");
145
146 MachineInstr &MI = *II;
147 DebugLoc dl = MI.getDebugLoc();
148 MachineBasicBlock &MBB = *MI.getParent();
149 const MachineFunction &MF = *MBB.getParent();
150 const AVRTargetMachine &TM = (const AVRTargetMachine &)MF.getTarget();
153 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
154
155 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
156 int FrameOffset = MI.getOperand(FIOperandNum + 1).getImm();
157 Register FrameReg;
158
159 int Offset =
160 TFI->getFrameIndexReference(MF, FrameIndex, FrameReg).getFixed() +
161 FrameOffset;
162
163 // This is actually "load effective address" of the stack slot
164 // instruction. We have only two-address instructions, thus we need to
165 // expand it into move + add.
166 if (MI.getOpcode() == AVR::FRMIDX) {
167 Register DstReg = MI.getOperand(0).getReg();
168
169 FrameReg = MI.getOperand(FIOperandNum + 2).getReg();
170
171 if (DstReg != FrameReg) {
172 TII.copyPhysReg(MBB, MI, dl, DstReg, FrameReg, false, false, false);
173 }
174
175 if (Offset > 0) {
176 // Skip over the FRMIDX instruction.
177 II++;
178
179 // Generally, to load a frame address two add instructions are emitted
180 // that could get folded into a single one:
181 // movw r31:r30, r29:r28
182 // adiw r31:r30, 29
183 // adiw r31:r30, 16
184 // to:
185 // movw r31:r30, r29:r28
186 // adiw r31:r30, 45
187 if (II != MBB.end())
188 foldFrameOffset(II, Offset, DstReg);
189
190 BuildMI(MBB, II, dl, TII.get(AVR::ADIWRdKP), DstReg)
191 .addReg(DstReg, RegState::Kill)
192 .addImm(Offset)
193 .setOperandDead(3); // implicit-def $sreg
194 }
195
196 // Remove FRMIDX.
197 MI.eraseFromParent();
198
199 // Since we removed an instruction, we return true.
200 return true;
201 }
202
203 // On most AVRs, we can use an offset up to 62 for load/store with
204 // displacement (63 for byte values, 62 for word values). However, the
205 // "reduced tiny" cores don't support load/store with displacement. So for
206 // them, we force an offset of 0 meaning that any positive offset will require
207 // adjusting the frame pointer.
208 int MaxOffset = STI.hasTinyEncoding() ? 0 : 62;
209
210 // If the offset is too big we have to adjust and restore the frame pointer
211 // to materialize a valid load/store with displacement.
212 //: TODO: consider using only one adiw/sbiw chain for more than one frame
213 //: index
214 if (Offset > MaxOffset) {
215 unsigned AddOpc = AVR::ADIWRdK, SubOpc = AVR::SBIWRdK;
216 int AddOffset = Offset - MaxOffset;
217
218 // For huge offsets where adiw/sbiw cannot be used use a pair of subi/sbci.
219 if ((Offset - MaxOffset) > 63 || !STI.hasADDSUBIW()) {
220 AddOpc = AVR::SUBIWRdK;
221 SubOpc = AVR::SUBIWRdK;
222 AddOffset = -AddOffset;
223 }
224
225 // It is possible that the spiller places this frame instruction in between
226 // a compare and branch, invalidating the contents of SREG set by the
227 // compare instruction because of the add/sub pairs. Conservatively save and
228 // restore SREG before and after each add/sub pair.
229 BuildMI(MBB, II, dl, TII.get(AVR::INRdA), STI.getTmpRegister())
230 .addImm(STI.getIORegSREG());
231
232 MachineInstr *New = BuildMI(MBB, II, dl, TII.get(AddOpc), AVR::R29R28)
233 .addReg(AVR::R29R28, RegState::Kill)
234 .addImm(AddOffset);
235
236 New->getOperand(3).setIsDead();
237
238 // Restore SREG.
239 BuildMI(MBB, std::next(II), dl, TII.get(AVR::OUTARr))
240 .addImm(STI.getIORegSREG())
242
243 // No need to set SREG as dead here otherwise if the next instruction is a
244 // cond branch it will be using a dead register.
245 BuildMI(MBB, std::next(II), dl, TII.get(SubOpc), AVR::R29R28)
246 .addReg(AVR::R29R28, RegState::Kill)
247 .addImm(Offset - MaxOffset);
248
249 Offset = MaxOffset;
250 }
251
252 assert(isUInt<6>(Offset) && "Offset is out of range");
253
254 MI.getOperand(FIOperandNum).ChangeToRegister(AVR::R29R28, false);
255 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
256
257 // Since we didn't remove an instruction, we return false.
258 return false;
259}
260
263 if (TFI->hasFP(MF)) {
264 // The Y pointer register
265 return AVR::R28;
266 }
267
268 return AVR::SP;
269}
270
273 // FIXME: Currently we're using avr-gcc as reference, so we restrict
274 // ptrs to Y and Z regs. Though avr-gcc has buggy implementation
275 // of memory constraint, so we can fix it and bit avr-gcc here ;-)
276 return &AVR::PTRDISPREGSRegClass;
277}
278
280 Register &HiReg) const {
281 assert(AVR::DREGSRegClass.contains(Reg) && "can only split 16-bit registers");
282
283 LoReg = getSubReg(Reg, AVR::sub_lo);
284 HiReg = getSubReg(Reg, AVR::sub_hi);
285}
286
288 MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg,
289 const TargetRegisterClass *DstRC, unsigned DstSubReg,
290 const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {
291 if (this->getRegClass(AVR::PTRDISPREGSRegClassID)->hasSubClassEq(NewRC)) {
292 return false;
293 }
294
295 return TargetRegisterInfo::shouldCoalesce(MI, SrcRC, SubReg, DstRC, DstSubReg,
296 NewRC, LIS);
297}
298
299} // end of namespace llvm
static const TargetRegisterClass * getRegClass(const MachineInstr &MI, Register Reg)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file implements the BitVector class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register Reg
Register const TargetRegisterInfo * TRI
uint64_t IntrinsicInst * II
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
Contains AVR-specific information for each MachineFunction.
bool isInterruptOrSignalHandler() const
Checks if the function is some form of interrupt service routine.
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &MF) const override
bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
Stack Frame Processing Methods.
const uint16_t * getCalleeSavedRegs(const MachineFunction *MF=nullptr) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
bool shouldCoalesce(MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, const TargetRegisterClass *DstRC, unsigned DstSubReg, const TargetRegisterClass *NewRC, LiveIntervals &LIS) const override
const TargetRegisterClass * getPointerRegClass(unsigned Kind=0) const override
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID CC) const override
Register getFrameRegister(const MachineFunction &MF) const override
void splitReg(Register Reg, Register &LoReg, Register &HiReg) const
Splits a 16-bit DREGS register into the lo/hi register pair.
A specific AVR target MCU.
Register getTmpRegister() const
const TargetFrameLowering * getFrameLowering() const override
const AVRInstrInfo * getInstrInfo() const override
int getIORegSREG() const
A generic AVR implementation.
const AVRSubtarget * getSubtargetImpl() const
A debug info location.
Definition DebugLoc.h:126
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & setOperandDead(unsigned OpIdx) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static StackOffset getFixed(int64_t Fixed)
Definition TypeSize.h:39
Information about stack frame layout on the target.
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual bool shouldCoalesce(MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, const TargetRegisterClass *DstRC, unsigned DstSubReg, const TargetRegisterClass *NewRC, LiveIntervals &LIS) const
Subtarget Hooks.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
static void foldFrameOffset(MachineBasicBlock::iterator &II, int &Offset, Register DstReg)
Fold a frame offset shared between two add instructions into a single one.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58