LLVM  9.0.0svn
ARCRegisterInfo.cpp
Go to the documentation of this file.
1 //===- ARCRegisterInfo.cpp - ARC Register Information -----------*- C++ -*-===//
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 ARC implementation of the MRegisterInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ARCRegisterInfo.h"
14 #include "ARC.h"
15 #include "ARCInstrInfo.h"
16 #include "ARCMachineFunctionInfo.h"
17 #include "ARCSubtarget.h"
18 #include "llvm/ADT/BitVector.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Support/Debug.h"
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "arc-reg-info"
34 
35 #define GET_REGINFO_TARGET_DESC
36 #include "ARCGenRegisterInfo.inc"
37 
39  const ARCInstrInfo &TII, unsigned Reg,
40  unsigned FrameReg, int Offset, int StackSize,
41  int ObjSize, RegScavenger *RS, int SPAdj) {
42  assert(RS && "Need register scavenger.");
43  MachineInstr &MI = *II;
44  MachineBasicBlock &MBB = *MI.getParent();
45  DebugLoc dl = MI.getDebugLoc();
46  unsigned BaseReg = FrameReg;
47  unsigned KillState = 0;
48  if (MI.getOpcode() == ARC::LD_rs9 && (Offset >= 256 || Offset < -256)) {
49  // Loads can always be reached with LD_rlimm.
50  BuildMI(MBB, II, dl, TII.get(ARC::LD_rlimm), Reg)
51  .addReg(BaseReg)
52  .addImm(Offset)
54  MBB.erase(II);
55  return;
56  }
57 
58  if (MI.getOpcode() != ARC::GETFI && (Offset >= 256 || Offset < -256)) {
59  // We need to use a scratch register to reach the far-away frame indexes.
60  BaseReg = RS->FindUnusedReg(&ARC::GPR32RegClass);
61  if (!BaseReg) {
62  // We can be sure that the scavenged-register slot is within the range
63  // of the load offset.
64  const TargetRegisterInfo *TRI =
66  BaseReg = RS->scavengeRegister(&ARC::GPR32RegClass, II, SPAdj);
67  assert(BaseReg && "Register scavenging failed.");
68  LLVM_DEBUG(dbgs() << "Scavenged register " << printReg(BaseReg, TRI)
69  << " for FrameReg=" << printReg(FrameReg, TRI)
70  << "+Offset=" << Offset << "\n");
71  (void)TRI;
72  RS->setRegUsed(BaseReg);
73  }
74  unsigned AddOpc = isUInt<6>(Offset) ? ARC::ADD_rru6 : ARC::ADD_rrlimm;
75  BuildMI(MBB, II, dl, TII.get(AddOpc))
76  .addReg(BaseReg, RegState::Define)
77  .addReg(FrameReg)
78  .addImm(Offset);
79  Offset = 0;
80  KillState = RegState::Kill;
81  }
82  switch (MI.getOpcode()) {
83  case ARC::LD_rs9:
84  assert((Offset % 4 == 0) && "LD needs 4 byte alignment.");
85  case ARC::LDH_rs9:
86  case ARC::LDH_X_rs9:
87  assert((Offset % 2 == 0) && "LDH needs 2 byte alignment.");
88  case ARC::LDB_rs9:
89  case ARC::LDB_X_rs9:
90  LLVM_DEBUG(dbgs() << "Building LDFI\n");
91  BuildMI(MBB, II, dl, TII.get(MI.getOpcode()), Reg)
92  .addReg(BaseReg, KillState)
93  .addImm(Offset)
95  break;
96  case ARC::ST_rs9:
97  assert((Offset % 4 == 0) && "ST needs 4 byte alignment.");
98  case ARC::STH_rs9:
99  assert((Offset % 2 == 0) && "STH needs 2 byte alignment.");
100  case ARC::STB_rs9:
101  LLVM_DEBUG(dbgs() << "Building STFI\n");
102  BuildMI(MBB, II, dl, TII.get(MI.getOpcode()))
103  .addReg(Reg, getKillRegState(MI.getOperand(0).isKill()))
104  .addReg(BaseReg, KillState)
105  .addImm(Offset)
107  break;
108  case ARC::GETFI:
109  LLVM_DEBUG(dbgs() << "Building GETFI\n");
110  BuildMI(MBB, II, dl,
111  TII.get(isUInt<6>(Offset) ? ARC::ADD_rru6 : ARC::ADD_rrlimm))
112  .addReg(Reg, RegState::Define)
113  .addReg(FrameReg)
114  .addImm(Offset);
115  break;
116  default:
117  llvm_unreachable("Unhandled opcode.");
118  }
119 
120  // Erase old instruction.
121  MBB.erase(II);
122 }
123 
125 
127  return MF.getMMI().hasDebugInfo() || MF.getFunction().needsUnwindTableEntry();
128 }
129 
130 const MCPhysReg *
132  return CSR_ARC_SaveList;
133 }
134 
136  BitVector Reserved(getNumRegs());
137 
138  Reserved.set(ARC::ILINK);
139  Reserved.set(ARC::SP);
140  Reserved.set(ARC::GP);
141  Reserved.set(ARC::R25);
142  Reserved.set(ARC::BLINK);
143  Reserved.set(ARC::FP);
144  return Reserved;
145 }
146 
148  const MachineFunction &MF) const {
149  return true;
150 }
151 
153  const MachineFunction &MF) const {
154  return true;
155 }
156 
158  return true;
159 }
160 
162  int SPAdj, unsigned FIOperandNum,
163  RegScavenger *RS) const {
164  assert(SPAdj == 0 && "Unexpected");
165  MachineInstr &MI = *II;
166  MachineOperand &FrameOp = MI.getOperand(FIOperandNum);
167  int FrameIndex = FrameOp.getIndex();
168 
169  MachineFunction &MF = *MI.getParent()->getParent();
170  const ARCInstrInfo &TII = *MF.getSubtarget<ARCSubtarget>().getInstrInfo();
171  const ARCFrameLowering *TFI = getFrameLowering(MF);
172  int Offset = MF.getFrameInfo().getObjectOffset(FrameIndex);
173  int ObjSize = MF.getFrameInfo().getObjectSize(FrameIndex);
174  int StackSize = MF.getFrameInfo().getStackSize();
175  int LocalFrameSize = MF.getFrameInfo().getLocalFrameSize();
176 
177  LLVM_DEBUG(dbgs() << "\nFunction : " << MF.getName() << "\n");
178  LLVM_DEBUG(dbgs() << "<--------->\n");
179  LLVM_DEBUG(dbgs() << MI << "\n");
180  LLVM_DEBUG(dbgs() << "FrameIndex : " << FrameIndex << "\n");
181  LLVM_DEBUG(dbgs() << "ObjSize : " << ObjSize << "\n");
182  LLVM_DEBUG(dbgs() << "FrameOffset : " << Offset << "\n");
183  LLVM_DEBUG(dbgs() << "StackSize : " << StackSize << "\n");
184  LLVM_DEBUG(dbgs() << "LocalFrameSize : " << LocalFrameSize << "\n");
185  (void)LocalFrameSize;
186 
187  // Special handling of DBG_VALUE instructions.
188  if (MI.isDebugValue()) {
189  unsigned FrameReg = getFrameRegister(MF);
190  MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
191  MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
192  return;
193  }
194 
195  // fold constant into offset.
196  Offset += MI.getOperand(FIOperandNum + 1).getImm();
197 
198  // TODO: assert based on the load type:
199  // ldb needs no alignment,
200  // ldh needs 2 byte alignment
201  // ld needs 4 byte alignment
202  LLVM_DEBUG(dbgs() << "Offset : " << Offset << "\n"
203  << "<--------->\n");
204 
205  unsigned Reg = MI.getOperand(0).getReg();
206  assert(ARC::GPR32RegClass.contains(Reg) && "Unexpected register operand");
207 
208  if (!TFI->hasFP(MF)) {
209  Offset = StackSize + Offset;
210  if (FrameIndex >= 0)
211  assert((Offset >= 0 && Offset < StackSize) && "SP Offset not in bounds.");
212  } else {
213  if (FrameIndex >= 0) {
214  assert((Offset < 0 && -Offset <= StackSize) &&
215  "FP Offset not in bounds.");
216  }
217  }
218  ReplaceFrameIndex(II, TII, Reg, getFrameRegister(MF), Offset, StackSize,
219  ObjSize, RS, SPAdj);
220 }
221 
223  const ARCFrameLowering *TFI = getFrameLowering(MF);
224  return TFI->hasFP(MF) ? ARC::FP : ARC::SP;
225 }
226 
227 const uint32_t *
229  CallingConv::ID CC) const {
230  return CSR_ARC_RegMask;
231 }
BitVector & set()
Definition: BitVector.h:397
bool hasDebugInfo() const
Returns true if valid debug info is present.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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.
int64_t getLocalFrameSize() const
Get the size of the local object blob.
unsigned Reg
bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const override
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:33
MachineModuleInfo & getMMI() const
return AArch64::GPR64RegClass contains(Reg)
unsigned getFrameRegister(const MachineFunction &MF) const override
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const HexagonInstrInfo * TII
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:408
BitVector getReservedRegs(const MachineFunction &MF) const override
unsigned FindUnusedReg(const TargetRegisterClass *RC) const
Find an unused register of the specified register class.
bool useFPForScavengingIndex(const MachineFunction &MF) const override
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
unsigned getKillRegState(bool B)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
This file declares the machine register scavenger class.
static bool needsFrameMoves(const MachineFunction &MF)
Return whether to emit frame moves.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
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.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:533
bool isDebugValue() const
Definition: MachineInstr.h:996
MachineOperand class - Representation of each machine instruction operand.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID CC) const override
int64_t getImm() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition: Function.h:572
void eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
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.
bool requiresRegisterScavenging(const MachineFunction &MF) const override
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned scavengeRegister(const TargetRegisterClass *RC, MachineBasicBlock::iterator I, int SPAdj, bool AllowSpill=true)
Make a register of the specific register class available and do the appropriate bookkeeping.
void setRegUsed(unsigned Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Tell the scavenger a register is used.
IRTranslator LLVM IR MI
static void ReplaceFrameIndex(MachineBasicBlock::iterator II, const ARCInstrInfo &TII, unsigned Reg, unsigned FrameReg, int Offset, int StackSize, int ObjSize, RegScavenger *RS, int SPAdj)
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:413
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
Code Generation virtual methods...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...