LLVM  8.0.1
ARCRegisterInfo.cpp
Go to the documentation of this file.
1 //===- ARCRegisterInfo.cpp - ARC Register Information -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the ARC implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ARCRegisterInfo.h"
15 #include "ARC.h"
16 #include "ARCInstrInfo.h"
17 #include "ARCMachineFunctionInfo.h"
18 #include "ARCSubtarget.h"
19 #include "llvm/ADT/BitVector.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/Debug.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "arc-reg-info"
35 
36 #define GET_REGINFO_TARGET_DESC
37 #include "ARCGenRegisterInfo.inc"
38 
40  const ARCInstrInfo &TII, unsigned Reg,
41  unsigned FrameReg, int Offset, int StackSize,
42  int ObjSize, RegScavenger *RS, int SPAdj) {
43  assert(RS && "Need register scavenger.");
44  MachineInstr &MI = *II;
45  MachineBasicBlock &MBB = *MI.getParent();
46  DebugLoc dl = MI.getDebugLoc();
47  unsigned BaseReg = FrameReg;
48  unsigned KillState = 0;
49  if (MI.getOpcode() == ARC::LD_rs9 && (Offset >= 256 || Offset < -256)) {
50  // Loads can always be reached with LD_rlimm.
51  BuildMI(MBB, II, dl, TII.get(ARC::LD_rlimm), Reg)
52  .addReg(BaseReg)
53  .addImm(Offset)
55  MBB.erase(II);
56  return;
57  }
58 
59  if (MI.getOpcode() != ARC::GETFI && (Offset >= 256 || Offset < -256)) {
60  // We need to use a scratch register to reach the far-away frame indexes.
61  BaseReg = RS->FindUnusedReg(&ARC::GPR32RegClass);
62  if (!BaseReg) {
63  // We can be sure that the scavenged-register slot is within the range
64  // of the load offset.
65  const TargetRegisterInfo *TRI =
67  BaseReg = RS->scavengeRegister(&ARC::GPR32RegClass, II, SPAdj);
68  assert(BaseReg && "Register scavenging failed.");
69  LLVM_DEBUG(dbgs() << "Scavenged register " << printReg(BaseReg, TRI)
70  << " for FrameReg=" << printReg(FrameReg, TRI)
71  << "+Offset=" << Offset << "\n");
72  (void)TRI;
73  RS->setRegUsed(BaseReg);
74  }
75  unsigned AddOpc = isUInt<6>(Offset) ? ARC::ADD_rru6 : ARC::ADD_rrlimm;
76  BuildMI(MBB, II, dl, TII.get(AddOpc))
77  .addReg(BaseReg, RegState::Define)
78  .addReg(FrameReg)
79  .addImm(Offset);
80  Offset = 0;
81  KillState = RegState::Kill;
82  }
83  switch (MI.getOpcode()) {
84  case ARC::LD_rs9:
85  assert((Offset % 4 == 0) && "LD needs 4 byte alignment.");
86  case ARC::LDH_rs9:
87  case ARC::LDH_X_rs9:
88  assert((Offset % 2 == 0) && "LDH needs 2 byte alignment.");
89  case ARC::LDB_rs9:
90  case ARC::LDB_X_rs9:
91  LLVM_DEBUG(dbgs() << "Building LDFI\n");
92  BuildMI(MBB, II, dl, TII.get(MI.getOpcode()), Reg)
93  .addReg(BaseReg, KillState)
94  .addImm(Offset)
96  break;
97  case ARC::ST_rs9:
98  assert((Offset % 4 == 0) && "ST needs 4 byte alignment.");
99  case ARC::STH_rs9:
100  assert((Offset % 2 == 0) && "STH needs 2 byte alignment.");
101  case ARC::STB_rs9:
102  LLVM_DEBUG(dbgs() << "Building STFI\n");
103  BuildMI(MBB, II, dl, TII.get(MI.getOpcode()))
104  .addReg(Reg, getKillRegState(MI.getOperand(0).isKill()))
105  .addReg(BaseReg, KillState)
106  .addImm(Offset)
108  break;
109  case ARC::GETFI:
110  LLVM_DEBUG(dbgs() << "Building GETFI\n");
111  BuildMI(MBB, II, dl,
112  TII.get(isUInt<6>(Offset) ? ARC::ADD_rru6 : ARC::ADD_rrlimm))
113  .addReg(Reg, RegState::Define)
114  .addReg(FrameReg)
115  .addImm(Offset);
116  break;
117  default:
118  llvm_unreachable("Unhandled opcode.");
119  }
120 
121  // Erase old instruction.
122  MBB.erase(II);
123 }
124 
126 
128  return MF.getMMI().hasDebugInfo() || MF.getFunction().needsUnwindTableEntry();
129 }
130 
131 const MCPhysReg *
133  return CSR_ARC_SaveList;
134 }
135 
137  BitVector Reserved(getNumRegs());
138 
139  Reserved.set(ARC::ILINK);
140  Reserved.set(ARC::SP);
141  Reserved.set(ARC::GP);
142  Reserved.set(ARC::R25);
143  Reserved.set(ARC::BLINK);
144  Reserved.set(ARC::FP);
145  return Reserved;
146 }
147 
149  const MachineFunction &MF) const {
150  return true;
151 }
152 
154  const MachineFunction &MF) const {
155  return true;
156 }
157 
159  return true;
160 }
161 
163  int SPAdj, unsigned FIOperandNum,
164  RegScavenger *RS) const {
165  assert(SPAdj == 0 && "Unexpected");
166  MachineInstr &MI = *II;
167  MachineOperand &FrameOp = MI.getOperand(FIOperandNum);
168  int FrameIndex = FrameOp.getIndex();
169 
170  MachineFunction &MF = *MI.getParent()->getParent();
171  const ARCInstrInfo &TII = *MF.getSubtarget<ARCSubtarget>().getInstrInfo();
172  const ARCFrameLowering *TFI = getFrameLowering(MF);
173  int Offset = MF.getFrameInfo().getObjectOffset(FrameIndex);
174  int ObjSize = MF.getFrameInfo().getObjectSize(FrameIndex);
175  int StackSize = MF.getFrameInfo().getStackSize();
176  int LocalFrameSize = MF.getFrameInfo().getLocalFrameSize();
177 
178  LLVM_DEBUG(dbgs() << "\nFunction : " << MF.getName() << "\n");
179  LLVM_DEBUG(dbgs() << "<--------->\n");
180  LLVM_DEBUG(dbgs() << MI << "\n");
181  LLVM_DEBUG(dbgs() << "FrameIndex : " << FrameIndex << "\n");
182  LLVM_DEBUG(dbgs() << "ObjSize : " << ObjSize << "\n");
183  LLVM_DEBUG(dbgs() << "FrameOffset : " << Offset << "\n");
184  LLVM_DEBUG(dbgs() << "StackSize : " << StackSize << "\n");
185  LLVM_DEBUG(dbgs() << "LocalFrameSize : " << LocalFrameSize << "\n");
186  (void)LocalFrameSize;
187 
188  // Special handling of DBG_VALUE instructions.
189  if (MI.isDebugValue()) {
190  unsigned FrameReg = getFrameRegister(MF);
191  MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
192  MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
193  return;
194  }
195 
196  // fold constant into offset.
197  Offset += MI.getOperand(FIOperandNum + 1).getImm();
198 
199  // TODO: assert based on the load type:
200  // ldb needs no alignment,
201  // ldh needs 2 byte alignment
202  // ld needs 4 byte alignment
203  LLVM_DEBUG(dbgs() << "Offset : " << Offset << "\n"
204  << "<--------->\n");
205 
206  unsigned Reg = MI.getOperand(0).getReg();
207  assert(ARC::GPR32RegClass.contains(Reg) && "Unexpected register operand");
208 
209  if (!TFI->hasFP(MF)) {
210  Offset = StackSize + Offset;
211  if (FrameIndex >= 0)
212  assert((Offset >= 0 && Offset < StackSize) && "SP Offset not in bounds.");
213  } else {
214  if (FrameIndex >= 0) {
215  assert((Offset < 0 && -Offset <= StackSize) &&
216  "FP Offset not in bounds.");
217  }
218  }
219  ReplaceFrameIndex(II, TII, Reg, getFrameRegister(MF), Offset, StackSize,
220  ObjSize, RS, SPAdj);
221 }
222 
224  const ARCFrameLowering *TFI = getFrameLowering(MF);
225  return TFI->hasFP(MF) ? ARC::FP : ARC::SP;
226 }
227 
228 const uint32_t *
230  CallingConv::ID CC) const {
231  return CSR_ARC_RegMask;
232 }
BitVector & set()
Definition: BitVector.h:398
bool hasDebugInfo() const
Returns true if valid debug info is present.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:383
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:34
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:409
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:534
bool isDebugValue() const
Definition: MachineInstr.h:997
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:133
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition: Function.h:573
void eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:254
Representation of each machine instruction.
Definition: MachineInstr.h:64
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
unsigned scavengeRegister(const TargetRegisterClass *RC, MachineBasicBlock::iterator I, int SPAdj)
Make a register of the specific register class available and do the appropriate bookkeeping.
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())
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:123
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
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...