LLVM  5.0.0
ARMRegisterBankInfo.cpp
Go to the documentation of this file.
1 //===- ARMRegisterBankInfo.cpp -----------------------------------*- 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 /// \file
10 /// This file implements the targeting of the RegisterBankInfo class for ARM.
11 /// \todo This should be generated by TableGen.
12 //===----------------------------------------------------------------------===//
13 
14 #include "ARMRegisterBankInfo.h"
15 #include "ARMInstrInfo.h" // For the register classes
16 #include "ARMSubtarget.h"
21 
22 #define GET_TARGET_REGBANK_IMPL
23 #include "ARMGenRegisterBank.inc"
24 
25 using namespace llvm;
26 
27 #ifndef LLVM_BUILD_GLOBAL_ISEL
28 #error "You shouldn't build this"
29 #endif
30 
31 // FIXME: TableGen this.
32 // If it grows too much and TableGen still isn't ready to do the job, extract it
33 // into an ARMGenRegisterBankInfo.def (similar to AArch64).
34 namespace llvm {
35 namespace ARM {
41 };
42 
44  // GPR Partial Mapping
45  {0, 32, GPRRegBank},
46  // SPR Partial Mapping
47  {0, 32, FPRRegBank},
48  // DPR Partial Mapping
49  {0, 64, FPRRegBank},
50 };
51 
52 #ifndef NDEBUG
54  unsigned Start, unsigned Length,
55  unsigned RegBankID) {
56  return PM.StartIdx == Start && PM.Length == Length &&
57  PM.RegBank->getID() == RegBankID;
58 }
59 
60 static void checkPartialMappings() {
61  assert(
62  checkPartMapping(PartMappings[PMI_GPR - PMI_Min], 0, 32, GPRRegBankID) &&
63  "Wrong mapping for GPR");
64  assert(
65  checkPartMapping(PartMappings[PMI_SPR - PMI_Min], 0, 32, FPRRegBankID) &&
66  "Wrong mapping for SPR");
67  assert(
68  checkPartMapping(PartMappings[PMI_DPR - PMI_Min], 0, 64, FPRRegBankID) &&
69  "Wrong mapping for DPR");
70 }
71 #endif
72 
78 };
79 
81  // invalid
82  {nullptr, 0},
83  // 3 ops in GPRs
84  {&PartMappings[PMI_GPR - PMI_Min], 1},
85  {&PartMappings[PMI_GPR - PMI_Min], 1},
86  {&PartMappings[PMI_GPR - PMI_Min], 1},
87  // 3 ops in SPRs
88  {&PartMappings[PMI_SPR - PMI_Min], 1},
89  {&PartMappings[PMI_SPR - PMI_Min], 1},
90  {&PartMappings[PMI_SPR - PMI_Min], 1},
91  // 3 ops in DPRs
92  {&PartMappings[PMI_DPR - PMI_Min], 1},
93  {&PartMappings[PMI_DPR - PMI_Min], 1},
94  {&PartMappings[PMI_DPR - PMI_Min], 1}};
95 
96 #ifndef NDEBUG
99  return VM.NumBreakDowns == 1 && VM.BreakDown == BreakDown;
100 }
101 
102 static void checkValueMappings() {
105  "Wrong value mapping for 3 GPR ops instruction");
106  assert(checkValueMapping(ValueMappings[GPR3OpsIdx + 1],
108  "Wrong value mapping for 3 GPR ops instruction");
109  assert(checkValueMapping(ValueMappings[GPR3OpsIdx + 2],
111  "Wrong value mapping for 3 GPR ops instruction");
112 
115  "Wrong value mapping for 3 SPR ops instruction");
116  assert(checkValueMapping(ValueMappings[SPR3OpsIdx + 1],
118  "Wrong value mapping for 3 SPR ops instruction");
119  assert(checkValueMapping(ValueMappings[SPR3OpsIdx + 2],
121  "Wrong value mapping for 3 SPR ops instruction");
122 
125  "Wrong value mapping for 3 DPR ops instruction");
126  assert(checkValueMapping(ValueMappings[DPR3OpsIdx + 1],
128  "Wrong value mapping for 3 DPR ops instruction");
129  assert(checkValueMapping(ValueMappings[DPR3OpsIdx + 2],
131  "Wrong value mapping for 3 DPR ops instruction");
132 }
133 #endif
134 } // end namespace arm
135 } // end namespace llvm
136 
139  static bool AlreadyInit = false;
140  // We have only one set of register banks, whatever the subtarget
141  // is. Therefore, the initialization of the RegBanks table should be
142  // done only once. Indeed the table of all register banks
143  // (ARM::RegBanks) is unique in the compiler. At some point, it
144  // will get tablegen'ed and the whole constructor becomes empty.
145  if (AlreadyInit)
146  return;
147  AlreadyInit = true;
148 
149  const RegisterBank &RBGPR = getRegBank(ARM::GPRRegBankID);
150  (void)RBGPR;
151  assert(&ARM::GPRRegBank == &RBGPR && "The order in RegBanks is messed up");
152 
153  // Initialize the GPR bank.
154  assert(RBGPR.covers(*TRI.getRegClass(ARM::GPRRegClassID)) &&
155  "Subclass not added?");
156  assert(RBGPR.covers(*TRI.getRegClass(ARM::GPRwithAPSRRegClassID)) &&
157  "Subclass not added?");
158  assert(RBGPR.covers(*TRI.getRegClass(ARM::GPRnopcRegClassID)) &&
159  "Subclass not added?");
160  assert(RBGPR.covers(*TRI.getRegClass(ARM::rGPRRegClassID)) &&
161  "Subclass not added?");
162  assert(RBGPR.covers(*TRI.getRegClass(ARM::tGPRRegClassID)) &&
163  "Subclass not added?");
164  assert(RBGPR.covers(*TRI.getRegClass(ARM::tcGPRRegClassID)) &&
165  "Subclass not added?");
166  assert(RBGPR.covers(*TRI.getRegClass(ARM::tGPR_and_tcGPRRegClassID)) &&
167  "Subclass not added?");
168  assert(RBGPR.getSize() == 32 && "GPRs should hold up to 32-bit");
169 
170 #ifndef NDEBUG
173 #endif
174 }
175 
177  const TargetRegisterClass &RC) const {
178  using namespace ARM;
179 
180  switch (RC.getID()) {
181  case GPRRegClassID:
182  case GPRnopcRegClassID:
183  case GPRspRegClassID:
184  case tGPR_and_tcGPRRegClassID:
185  case tGPRRegClassID:
186  return getRegBank(ARM::GPRRegBankID);
187  case SPR_8RegClassID:
188  case SPRRegClassID:
189  case DPR_8RegClassID:
190  case DPRRegClassID:
191  return getRegBank(ARM::FPRRegBankID);
192  default:
193  llvm_unreachable("Unsupported register kind");
194  }
195 
196  llvm_unreachable("Switch should handle all register classes");
197 }
198 
201  auto Opc = MI.getOpcode();
202 
203  // Try the default logic for non-generic instructions that are either copies
204  // or already have some operands assigned to banks.
205  if (!isPreISelGenericOpcode(Opc)) {
206  const InstructionMapping &Mapping = getInstrMappingImpl(MI);
207  if (Mapping.isValid())
208  return Mapping;
209  }
210 
211  using namespace TargetOpcode;
212 
213  const MachineFunction &MF = *MI.getParent()->getParent();
214  const MachineRegisterInfo &MRI = MF.getRegInfo();
215  unsigned NumOperands = MI.getNumOperands();
216  const ValueMapping *OperandsMapping = &ARM::ValueMappings[ARM::GPR3OpsIdx];
217 
218  switch (Opc) {
219  case G_ADD:
220  case G_SUB:
221  case G_MUL:
222  case G_AND:
223  case G_OR:
224  case G_XOR:
225  case G_SDIV:
226  case G_UDIV:
227  case G_SEXT:
228  case G_ZEXT:
229  case G_ANYEXT:
230  case G_TRUNC:
231  case G_GEP:
232  // FIXME: We're abusing the fact that everything lives in a GPR for now; in
233  // the real world we would use different mappings.
234  OperandsMapping = &ARM::ValueMappings[ARM::GPR3OpsIdx];
235  break;
236  case G_LOAD:
237  case G_STORE: {
238  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
239  OperandsMapping =
240  Ty.getSizeInBits() == 64
244  break;
245  }
246  case G_FADD: {
247  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
248  assert((Ty.getSizeInBits() == 32 || Ty.getSizeInBits() == 64) &&
249  "Unsupported size for G_FADD");
250  OperandsMapping = Ty.getSizeInBits() == 64
253  break;
254  }
255  case G_CONSTANT:
256  case G_FRAME_INDEX:
257  OperandsMapping =
259  break;
260  case G_SELECT: {
261  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
262  (void)Ty;
263  LLT Ty2 = MRI.getType(MI.getOperand(1).getReg());
264  (void)Ty2;
265  assert(Ty.getSizeInBits() == 32 && "Unsupported size for G_SELECT");
266  assert(Ty2.getSizeInBits() == 1 && "Unsupported size for G_SELECT");
267  OperandsMapping =
270  &ARM::ValueMappings[ARM::GPR3OpsIdx],
271  &ARM::ValueMappings[ARM::GPR3OpsIdx]});
272  break;
273  }
274  case G_ICMP: {
275  LLT Ty2 = MRI.getType(MI.getOperand(2).getReg());
276  (void)Ty2;
277  assert(Ty2.getSizeInBits() == 32 && "Unsupported size for G_ICMP");
278  OperandsMapping =
281  &ARM::ValueMappings[ARM::GPR3OpsIdx]});
282  break;
283  }
284  case G_FCMP: {
285  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
286  (void)Ty;
287  LLT Ty1 = MRI.getType(MI.getOperand(2).getReg());
288  LLT Ty2 = MRI.getType(MI.getOperand(3).getReg());
289  (void)Ty2;
290  assert(Ty.getSizeInBits() == 1 && "Unsupported size for G_FCMP");
291  assert(Ty1.getSizeInBits() == Ty2.getSizeInBits() &&
292  "Mismatched operand sizes for G_FCMP");
293 
294  unsigned Size = Ty1.getSizeInBits();
295  assert((Size == 32 || Size == 64) && "Unsupported size for G_FCMP");
296 
297  auto FPRValueMapping = Size == 32 ? &ARM::ValueMappings[ARM::SPR3OpsIdx]
299  OperandsMapping =
301  FPRValueMapping, FPRValueMapping});
302  break;
303  }
304  case G_MERGE_VALUES: {
305  // We only support G_MERGE_VALUES for creating a double precision floating
306  // point value out of two GPRs.
307  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
308  LLT Ty1 = MRI.getType(MI.getOperand(1).getReg());
309  LLT Ty2 = MRI.getType(MI.getOperand(2).getReg());
310  if (Ty.getSizeInBits() != 64 || Ty1.getSizeInBits() != 32 ||
311  Ty2.getSizeInBits() != 32)
313  OperandsMapping =
316  &ARM::ValueMappings[ARM::GPR3OpsIdx]});
317  break;
318  }
319  case G_UNMERGE_VALUES: {
320  // We only support G_UNMERGE_VALUES for splitting a double precision
321  // floating point value into two GPRs.
322  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
323  LLT Ty1 = MRI.getType(MI.getOperand(1).getReg());
324  LLT Ty2 = MRI.getType(MI.getOperand(2).getReg());
325  if (Ty.getSizeInBits() != 32 || Ty1.getSizeInBits() != 32 ||
326  Ty2.getSizeInBits() != 64)
328  OperandsMapping =
331  &ARM::ValueMappings[ARM::DPR3OpsIdx]});
332  break;
333  }
334  case G_BR:
335  OperandsMapping = getOperandsMapping({nullptr});
336  break;
337  case G_BRCOND:
338  OperandsMapping =
340  break;
341  default:
343  }
344 
345 #ifndef NDEBUG
346  for (unsigned i = 0; i < NumOperands; i++) {
347  for (const auto &Mapping : OperandsMapping[i]) {
348  assert(
349  (Mapping.RegBank->getID() != ARM::FPRRegBankID ||
350  MF.getSubtarget<ARMSubtarget>().hasVFP2()) &&
351  "Trying to use floating point register bank on target without vfp");
352  }
353  }
354 #endif
355 
356  return getInstructionMapping(DefaultMappingID, /*Cost=*/1, OperandsMapping,
357  NumOperands);
358 }
static bool checkValueMapping(const RegisterBankInfo::ValueMapping &VM, RegisterBankInfo::PartialMapping *BreakDown)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const InstructionMapping & getInvalidInstructionMapping() const
Method to get a uniquely generated invalid InstructionMapping.
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
Helper class that represents how the value of an instruction may be mapped and what is the related co...
unsigned getID() const
Return the register class ID number.
setjmp/longjmp based exceptions
static void checkValueMappings()
const PartialMapping * BreakDown
How the value is broken down between the different register banks.
const TargetRegisterClass * getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
unsigned StartIdx
Number of bits at which this partial mapping starts in the original value.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
const ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End...
RegisterBankInfo::PartialMapping static bool checkPartMapping(const RegisterBankInfo::PartialMapping &PM, unsigned Start, unsigned Length, unsigned RegBankID)
const RegisterBank * RegBank
Register bank where the partial value lives.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
unsigned const MachineRegisterInfo * MRI
This file declares the targeting of the RegisterBankInfo class for ARM.
RegisterBankInfo::ValueMapping ValueMappings[]
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
unsigned getID() const
Get the identifier of this register bank.
Definition: RegisterBank.h:48
Helper struct that represents how a value is partially mapped into a register.
unsigned Length
Length of this mapping in bits.
static const unsigned DefaultMappingID
Identifier used when the related instruction mapping instance is generated by target independent code...
RegisterBankInfo::PartialMapping PartMappings[]
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
ARMRegisterBankInfo(const TargetRegisterInfo &TRI)
static void checkPartialMappings()
RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
This class implements the register bank concept.
Definition: RegisterBank.h:29
Helper struct that represents how a value is mapped through different register banks.
static bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel...
Definition: TargetOpcodes.h:31
bool isValid() const
Check whether this object is valid.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:59
const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC) const override
Get a register bank that covers RC.
unsigned getSize() const
Get the maximal size in bits that fits in this register bank.
Definition: RegisterBank.h:55
const InstructionMapping & getInstrMapping(const MachineInstr &MI) const override
Get the mapping of the different operands of MI on the register bank.
bool covers(const TargetRegisterClass &RC) const
Check whether this register bank covers RC.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
unsigned NumBreakDowns
Number of partial mapping to break down this value.
IRTranslator LLVM IR MI
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.