LLVM  9.0.0svn
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"
19 #include "llvm/IR/Function.h"
21 
22 #include "AVR.h"
23 #include "AVRInstrInfo.h"
24 #include "AVRTargetMachine.h"
26 
27 #define GET_REGINFO_TARGET_DESC
28 #include "AVRGenRegisterInfo.inc"
29 
30 namespace llvm {
31 
33 
34 const uint16_t *
37 
38  return ((CC == CallingConv::AVR_INTR || CC == CallingConv::AVR_SIGNAL)
39  ? CSR_Interrupts_SaveList
40  : CSR_Normal_SaveList);
41 }
42 
43 const uint32_t *
45  CallingConv::ID CC) const {
46  return ((CC == CallingConv::AVR_INTR || CC == CallingConv::AVR_SIGNAL)
47  ? CSR_Interrupts_RegMask
48  : CSR_Normal_RegMask);
49 }
50 
52  BitVector Reserved(getNumRegs());
53 
54  // Reserve the intermediate result registers r1 and r2
55  // The result of instructions like 'mul' is always stored here.
56  Reserved.set(AVR::R0);
57  Reserved.set(AVR::R1);
58  Reserved.set(AVR::R1R0);
59 
60  // Reserve the stack pointer.
61  Reserved.set(AVR::SPL);
62  Reserved.set(AVR::SPH);
63  Reserved.set(AVR::SP);
64 
65  // We tenatively reserve the frame pointer register r29:r28 because the
66  // function may require one, but we cannot tell until register allocation
67  // is complete, which can be too late.
68  //
69  // Instead we just unconditionally reserve the Y register.
70  //
71  // TODO: Write a pass to enumerate functions which reserved the Y register
72  // but didn't end up needing a frame pointer. In these, we can
73  // convert one or two of the spills inside to use the Y register.
74  Reserved.set(AVR::R28);
75  Reserved.set(AVR::R29);
76  Reserved.set(AVR::R29R28);
77 
78  return Reserved;
79 }
80 
81 const TargetRegisterClass *
83  const MachineFunction &MF) const {
85  if (TRI->isTypeLegalForClass(*RC, MVT::i16)) {
86  return &AVR::DREGSRegClass;
87  }
88 
89  if (TRI->isTypeLegalForClass(*RC, MVT::i8)) {
90  return &AVR::GPR8RegClass;
91  }
92 
93  llvm_unreachable("Invalid register size");
94 }
95 
96 /// Fold a frame offset shared between two add instructions into a single one.
97 static void foldFrameOffset(MachineBasicBlock::iterator &II, int &Offset, unsigned DstReg) {
98  MachineInstr &MI = *II;
99  int Opcode = MI.getOpcode();
100 
101  // Don't bother trying if the next instruction is not an add or a sub.
102  if ((Opcode != AVR::SUBIWRdK) && (Opcode != AVR::ADIWRdK)) {
103  return;
104  }
105 
106  // Check that DstReg matches with next instruction, otherwise the instruction
107  // is not related to stack address manipulation.
108  if (DstReg != MI.getOperand(0).getReg()) {
109  return;
110  }
111 
112  // Add the offset in the next instruction to our offset.
113  switch (Opcode) {
114  case AVR::SUBIWRdK:
115  Offset += -MI.getOperand(2).getImm();
116  break;
117  case AVR::ADIWRdK:
118  Offset += MI.getOperand(2).getImm();
119  break;
120  }
121 
122  // Finally remove the instruction.
123  II++;
124  MI.eraseFromParent();
125 }
126 
128  int SPAdj, unsigned FIOperandNum,
129  RegScavenger *RS) const {
130  assert(SPAdj == 0 && "Unexpected SPAdj value");
131 
132  MachineInstr &MI = *II;
133  DebugLoc dl = MI.getDebugLoc();
134  MachineBasicBlock &MBB = *MI.getParent();
135  const MachineFunction &MF = *MBB.getParent();
136  const AVRTargetMachine &TM = (const AVRTargetMachine &)MF.getTarget();
138  const MachineFrameInfo &MFI = MF.getFrameInfo();
140  int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
141  int Offset = MFI.getObjectOffset(FrameIndex);
142 
143  // Add one to the offset because SP points to an empty slot.
144  Offset += MFI.getStackSize() - TFI->getOffsetOfLocalArea() + 1;
145  // Fold incoming offset.
146  Offset += MI.getOperand(FIOperandNum + 1).getImm();
147 
148  // This is actually "load effective address" of the stack slot
149  // instruction. We have only two-address instructions, thus we need to
150  // expand it into move + add.
151  if (MI.getOpcode() == AVR::FRMIDX) {
152  MI.setDesc(TII.get(AVR::MOVWRdRr));
153  MI.getOperand(FIOperandNum).ChangeToRegister(AVR::R29R28, false);
154  MI.RemoveOperand(2);
155 
156  assert(Offset > 0 && "Invalid offset");
157 
158  // We need to materialize the offset via an add instruction.
159  unsigned Opcode;
160  unsigned DstReg = MI.getOperand(0).getReg();
161  assert(DstReg != AVR::R29R28 && "Dest reg cannot be the frame pointer");
162 
163  II++; // Skip over the FRMIDX (and now MOVW) instruction.
164 
165  // Generally, to load a frame address two add instructions are emitted that
166  // could get folded into a single one:
167  // movw r31:r30, r29:r28
168  // adiw r31:r30, 29
169  // adiw r31:r30, 16
170  // to:
171  // movw r31:r30, r29:r28
172  // adiw r31:r30, 45
173  if (II != MBB.end())
174  foldFrameOffset(II, Offset, DstReg);
175 
176  // Select the best opcode based on DstReg and the offset size.
177  switch (DstReg) {
178  case AVR::R25R24:
179  case AVR::R27R26:
180  case AVR::R31R30: {
181  if (isUInt<6>(Offset)) {
182  Opcode = AVR::ADIWRdK;
183  break;
184  }
186  }
187  default: {
188  // This opcode will get expanded into a pair of subi/sbci.
189  Opcode = AVR::SUBIWRdK;
190  Offset = -Offset;
191  break;
192  }
193  }
194 
195  MachineInstr *New = BuildMI(MBB, II, dl, TII.get(Opcode), DstReg)
196  .addReg(DstReg, RegState::Kill)
197  .addImm(Offset);
198  New->getOperand(3).setIsDead();
199 
200  return;
201  }
202 
203  // If the offset is too big we have to adjust and restore the frame pointer
204  // to materialize a valid load/store with displacement.
205  //:TODO: consider using only one adiw/sbiw chain for more than one frame index
206  if (Offset > 62) {
207  unsigned AddOpc = AVR::ADIWRdK, SubOpc = AVR::SBIWRdK;
208  int AddOffset = Offset - 63 + 1;
209 
210  // For huge offsets where adiw/sbiw cannot be used use a pair of subi/sbci.
211  if ((Offset - 63 + 1) > 63) {
212  AddOpc = AVR::SUBIWRdK;
213  SubOpc = AVR::SUBIWRdK;
214  AddOffset = -AddOffset;
215  }
216 
217  // It is possible that the spiller places this frame instruction in between
218  // a compare and branch, invalidating the contents of SREG set by the
219  // compare instruction because of the add/sub pairs. Conservatively save and
220  // restore SREG before and after each add/sub pair.
221  BuildMI(MBB, II, dl, TII.get(AVR::INRdA), AVR::R0).addImm(0x3f);
222 
223  MachineInstr *New = BuildMI(MBB, II, dl, TII.get(AddOpc), AVR::R29R28)
224  .addReg(AVR::R29R28, RegState::Kill)
225  .addImm(AddOffset);
226  New->getOperand(3).setIsDead();
227 
228  // Restore SREG.
229  BuildMI(MBB, std::next(II), dl, TII.get(AVR::OUTARr))
230  .addImm(0x3f)
231  .addReg(AVR::R0, RegState::Kill);
232 
233  // No need to set SREG as dead here otherwise if the next instruction is a
234  // cond branch it will be using a dead register.
235  New = BuildMI(MBB, std::next(II), dl, TII.get(SubOpc), AVR::R29R28)
236  .addReg(AVR::R29R28, RegState::Kill)
237  .addImm(Offset - 63 + 1);
238 
239  Offset = 62;
240  }
241 
242  MI.getOperand(FIOperandNum).ChangeToRegister(AVR::R29R28, false);
243  assert(isUInt<6>(Offset) && "Offset is out of range");
244  MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
245 }
246 
249  if (TFI->hasFP(MF)) {
250  // The Y pointer register
251  return AVR::R28;
252  }
253 
254  return AVR::SP;
255 }
256 
257 const TargetRegisterClass *
259  unsigned Kind) const {
260  // FIXME: Currently we're using avr-gcc as reference, so we restrict
261  // ptrs to Y and Z regs. Though avr-gcc has buggy implementation
262  // of memory constraint, so we can fix it and bit avr-gcc here ;-)
263  return &AVR::PTRDISPREGSRegClass;
264 }
265 
267  unsigned &LoReg,
268  unsigned &HiReg) const {
269  assert(AVR::DREGSRegClass.contains(Reg) && "can only split 16-bit registers");
270 
271  LoReg = getSubReg(Reg, AVR::sub_lo);
272  HiReg = getSubReg(Reg, AVR::sub_hi);
273 }
274 
275 } // end of namespace llvm
const AVRInstrInfo * getInstrInfo() const override
Definition: AVRSubtarget.h:41
BitVector & set()
Definition: BitVector.h:397
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const override
void ChangeToRegister(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:382
unsigned getReg() const
getReg - Returns the register number.
unsigned Reg
BitVector getReservedRegs(const MachineFunction &MF) const override
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:33
void setIsDead(bool Val=true)
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
return AArch64::GPR64RegClass contains(Reg)
const AVRSubtarget * getSubtargetImpl() const
Used for AVR interrupt routines.
Definition: CallingConv.h:176
const HexagonInstrInfo * TII
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &MF) const override
virtual bool hasFP(const MachineFunction &MF) const =0
hasFP - Return true if the specified function should have a dedicated frame pointer register...
void eraseFromParent()
Unlink &#39;this&#39; from the containing basic block and delete it.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:408
A generic AVR implementation.
const uint16_t * getCalleeSavedRegs(const MachineFunction *MF=0) const override
bool isTypeLegalForClass(const TargetRegisterClass &RC, MVT T) const
Return true if the given TargetRegisterClass has the ValueType T.
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID CC) const override
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
void eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=NULL) const override
Stack Frame Processing Methods.
TargetInstrInfo - Interface to description of machine instruction set.
static void foldFrameOffset(MachineBasicBlock::iterator &II, int &Offset, unsigned DstReg)
Fold a frame offset shared between two add instructions into a single one.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void splitReg(unsigned Reg, unsigned &LoReg, unsigned &HiReg) const
Splits a 16-bit DREGS register into the lo/hi register pair.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
Information about stack frame layout on the target.
int64_t getImm() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:253
Representation of each machine instruction.
Definition: MachineInstr.h:63
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:44
virtual const TargetFrameLowering * getFrameLowering() const
Calling convention used for AVR signal routines.
Definition: CallingConv.h:179
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getFrameRegister(const MachineFunction &MF) const override
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
const TargetFrameLowering * getFrameLowering() const override
Definition: AVRSubtarget.h:42
IRTranslator LLVM IR MI
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:413
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...