1 //===- ARMRegisterBankInfo.cpp -----------------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements the targeting of the RegisterBankInfo class for ARM.
11 /// \todo This should be generated by TableGen.
12 //===----------------------------------------------------------------------===//
14 #include "ARMRegisterBankInfo.h"
15 #include "ARMInstrInfo.h" // For the register classes
16 #include "ARMSubtarget.h"
17 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
18 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
22 #define GET_TARGET_REGBANK_IMPL
23 #include "ARMGenRegisterBank.inc"
27 // FIXME: TableGen this.
28 // If it grows too much and TableGen still isn't ready to do the job, extract it
29 // into an ARMGenRegisterBankInfo.def (similar to AArch64).
32 enum PartialMappingIdx {
39 RegisterBankInfo::PartialMapping PartMappings[]{
40 // GPR Partial Mapping
42 // SPR Partial Mapping
44 // DPR Partial Mapping
49 static bool checkPartMapping(const RegisterBankInfo::PartialMapping &PM,
50 unsigned Start, unsigned Length,
52 return PM.StartIdx == Start && PM.Length == Length &&
53 PM.RegBank->getID() == RegBankID;
56 static void checkPartialMappings() {
58 checkPartMapping(PartMappings[PMI_GPR - PMI_Min], 0, 32, GPRRegBankID) &&
59 "Wrong mapping for GPR");
61 checkPartMapping(PartMappings[PMI_SPR - PMI_Min], 0, 32, FPRRegBankID) &&
62 "Wrong mapping for SPR");
64 checkPartMapping(PartMappings[PMI_DPR - PMI_Min], 0, 64, FPRRegBankID) &&
65 "Wrong mapping for DPR");
69 enum ValueMappingIdx {
76 RegisterBankInfo::ValueMapping ValueMappings[] = {
80 {&PartMappings[PMI_GPR - PMI_Min], 1},
81 {&PartMappings[PMI_GPR - PMI_Min], 1},
82 {&PartMappings[PMI_GPR - PMI_Min], 1},
84 {&PartMappings[PMI_SPR - PMI_Min], 1},
85 {&PartMappings[PMI_SPR - PMI_Min], 1},
86 {&PartMappings[PMI_SPR - PMI_Min], 1},
88 {&PartMappings[PMI_DPR - PMI_Min], 1},
89 {&PartMappings[PMI_DPR - PMI_Min], 1},
90 {&PartMappings[PMI_DPR - PMI_Min], 1}};
93 static bool checkValueMapping(const RegisterBankInfo::ValueMapping &VM,
94 RegisterBankInfo::PartialMapping *BreakDown) {
95 return VM.NumBreakDowns == 1 && VM.BreakDown == BreakDown;
98 static void checkValueMappings() {
99 assert(checkValueMapping(ValueMappings[GPR3OpsIdx],
100 &PartMappings[PMI_GPR - PMI_Min]) &&
101 "Wrong value mapping for 3 GPR ops instruction");
102 assert(checkValueMapping(ValueMappings[GPR3OpsIdx + 1],
103 &PartMappings[PMI_GPR - PMI_Min]) &&
104 "Wrong value mapping for 3 GPR ops instruction");
105 assert(checkValueMapping(ValueMappings[GPR3OpsIdx + 2],
106 &PartMappings[PMI_GPR - PMI_Min]) &&
107 "Wrong value mapping for 3 GPR ops instruction");
109 assert(checkValueMapping(ValueMappings[SPR3OpsIdx],
110 &PartMappings[PMI_SPR - PMI_Min]) &&
111 "Wrong value mapping for 3 SPR ops instruction");
112 assert(checkValueMapping(ValueMappings[SPR3OpsIdx + 1],
113 &PartMappings[PMI_SPR - PMI_Min]) &&
114 "Wrong value mapping for 3 SPR ops instruction");
115 assert(checkValueMapping(ValueMappings[SPR3OpsIdx + 2],
116 &PartMappings[PMI_SPR - PMI_Min]) &&
117 "Wrong value mapping for 3 SPR ops instruction");
119 assert(checkValueMapping(ValueMappings[DPR3OpsIdx],
120 &PartMappings[PMI_DPR - PMI_Min]) &&
121 "Wrong value mapping for 3 DPR ops instruction");
122 assert(checkValueMapping(ValueMappings[DPR3OpsIdx + 1],
123 &PartMappings[PMI_DPR - PMI_Min]) &&
124 "Wrong value mapping for 3 DPR ops instruction");
125 assert(checkValueMapping(ValueMappings[DPR3OpsIdx + 2],
126 &PartMappings[PMI_DPR - PMI_Min]) &&
127 "Wrong value mapping for 3 DPR ops instruction");
130 } // end namespace arm
131 } // end namespace llvm
133 ARMRegisterBankInfo::ARMRegisterBankInfo(const TargetRegisterInfo &TRI)
134 : ARMGenRegisterBankInfo() {
135 static bool AlreadyInit = false;
136 // We have only one set of register banks, whatever the subtarget
137 // is. Therefore, the initialization of the RegBanks table should be
138 // done only once. Indeed the table of all register banks
139 // (ARM::RegBanks) is unique in the compiler. At some point, it
140 // will get tablegen'ed and the whole constructor becomes empty.
145 const RegisterBank &RBGPR = getRegBank(ARM::GPRRegBankID);
147 assert(&ARM::GPRRegBank == &RBGPR && "The order in RegBanks is messed up");
149 // Initialize the GPR bank.
150 assert(RBGPR.covers(*TRI.getRegClass(ARM::GPRRegClassID)) &&
151 "Subclass not added?");
152 assert(RBGPR.covers(*TRI.getRegClass(ARM::GPRwithAPSRRegClassID)) &&
153 "Subclass not added?");
154 assert(RBGPR.covers(*TRI.getRegClass(ARM::GPRnopcRegClassID)) &&
155 "Subclass not added?");
156 assert(RBGPR.covers(*TRI.getRegClass(ARM::rGPRRegClassID)) &&
157 "Subclass not added?");
158 assert(RBGPR.covers(*TRI.getRegClass(ARM::tGPRRegClassID)) &&
159 "Subclass not added?");
160 assert(RBGPR.covers(*TRI.getRegClass(ARM::tcGPRRegClassID)) &&
161 "Subclass not added?");
162 assert(RBGPR.covers(*TRI.getRegClass(ARM::tGPR_and_tcGPRRegClassID)) &&
163 "Subclass not added?");
164 assert(RBGPR.getSize() == 32 && "GPRs should hold up to 32-bit");
167 ARM::checkPartialMappings();
168 ARM::checkValueMappings();
172 const RegisterBank &ARMRegisterBankInfo::getRegBankFromRegClass(
173 const TargetRegisterClass &RC) const {
176 switch (RC.getID()) {
178 case GPRwithAPSRRegClassID:
179 case GPRnopcRegClassID:
181 case GPRspRegClassID:
182 case tGPR_and_tcGPRRegClassID:
183 case tcGPRRegClassID:
185 return getRegBank(ARM::GPRRegBankID);
187 case SPR_8RegClassID:
189 case DPR_8RegClassID:
192 return getRegBank(ARM::FPRRegBankID);
194 llvm_unreachable("Unsupported register kind");
197 llvm_unreachable("Switch should handle all register classes");
200 const RegisterBankInfo::InstructionMapping &
201 ARMRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
202 auto Opc = MI.getOpcode();
204 // Try the default logic for non-generic instructions that are either copies
205 // or already have some operands assigned to banks.
206 if (!isPreISelGenericOpcode(Opc) || Opc == TargetOpcode::G_PHI) {
207 const InstructionMapping &Mapping = getInstrMappingImpl(MI);
208 if (Mapping.isValid())
212 using namespace TargetOpcode;
214 const MachineFunction &MF = *MI.getParent()->getParent();
215 const MachineRegisterInfo &MRI = MF.getRegInfo();
216 unsigned NumOperands = MI.getNumOperands();
217 const ValueMapping *OperandsMapping = &ARM::ValueMappings[ARM::GPR3OpsIdx];
237 // FIXME: We're abusing the fact that everything lives in a GPR for now; in
238 // the real world we would use different mappings.
239 OperandsMapping = &ARM::ValueMappings[ARM::GPR3OpsIdx];
242 // In some cases we may end up with a G_TRUNC from a 64-bit value to a
243 // 32-bit value. This isn't a real floating point trunc (that would be a
244 // G_FPTRUNC). Instead it is an integer trunc in disguise, which can appear
245 // because the legalizer doesn't distinguish between integer and floating
246 // point values so it may leave some 64-bit integers un-narrowed. Until we
247 // have a more principled solution that doesn't let such things sneak all
248 // the way to this point, just map the source to a DPR and the destination
250 LLT LargeTy = MRI.getType(MI.getOperand(1).getReg());
252 LargeTy.getSizeInBits() <= 32
253 ? &ARM::ValueMappings[ARM::GPR3OpsIdx]
254 : getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx],
255 &ARM::ValueMappings[ARM::DPR3OpsIdx]});
260 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
262 Ty.getSizeInBits() == 64
263 ? getOperandsMapping({&ARM::ValueMappings[ARM::DPR3OpsIdx],
264 &ARM::ValueMappings[ARM::GPR3OpsIdx]})
265 : &ARM::ValueMappings[ARM::GPR3OpsIdx];
273 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
274 OperandsMapping =Ty.getSizeInBits() == 64
275 ? &ARM::ValueMappings[ARM::DPR3OpsIdx]
276 : &ARM::ValueMappings[ARM::SPR3OpsIdx];
280 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
282 Ty.getSizeInBits() == 64
283 ? getOperandsMapping({&ARM::ValueMappings[ARM::DPR3OpsIdx],
284 &ARM::ValueMappings[ARM::DPR3OpsIdx],
285 &ARM::ValueMappings[ARM::DPR3OpsIdx],
286 &ARM::ValueMappings[ARM::DPR3OpsIdx]})
287 : getOperandsMapping({&ARM::ValueMappings[ARM::SPR3OpsIdx],
288 &ARM::ValueMappings[ARM::SPR3OpsIdx],
289 &ARM::ValueMappings[ARM::SPR3OpsIdx],
290 &ARM::ValueMappings[ARM::SPR3OpsIdx]});
294 LLT ToTy = MRI.getType(MI.getOperand(0).getReg());
295 LLT FromTy = MRI.getType(MI.getOperand(1).getReg());
296 if (ToTy.getSizeInBits() == 64 && FromTy.getSizeInBits() == 32)
298 getOperandsMapping({&ARM::ValueMappings[ARM::DPR3OpsIdx],
299 &ARM::ValueMappings[ARM::SPR3OpsIdx]});
303 LLT ToTy = MRI.getType(MI.getOperand(0).getReg());
304 LLT FromTy = MRI.getType(MI.getOperand(1).getReg());
305 if (ToTy.getSizeInBits() == 32 && FromTy.getSizeInBits() == 64)
307 getOperandsMapping({&ARM::ValueMappings[ARM::SPR3OpsIdx],
308 &ARM::ValueMappings[ARM::DPR3OpsIdx]});
313 LLT ToTy = MRI.getType(MI.getOperand(0).getReg());
314 LLT FromTy = MRI.getType(MI.getOperand(1).getReg());
315 if ((FromTy.getSizeInBits() == 32 || FromTy.getSizeInBits() == 64) &&
316 ToTy.getSizeInBits() == 32)
318 FromTy.getSizeInBits() == 64
319 ? getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx],
320 &ARM::ValueMappings[ARM::DPR3OpsIdx]})
321 : getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx],
322 &ARM::ValueMappings[ARM::SPR3OpsIdx]});
327 LLT ToTy = MRI.getType(MI.getOperand(0).getReg());
328 LLT FromTy = MRI.getType(MI.getOperand(1).getReg());
329 if (FromTy.getSizeInBits() == 32 &&
330 (ToTy.getSizeInBits() == 32 || ToTy.getSizeInBits() == 64))
332 ToTy.getSizeInBits() == 64
333 ? getOperandsMapping({&ARM::ValueMappings[ARM::DPR3OpsIdx],
334 &ARM::ValueMappings[ARM::GPR3OpsIdx]})
335 : getOperandsMapping({&ARM::ValueMappings[ARM::SPR3OpsIdx],
336 &ARM::ValueMappings[ARM::GPR3OpsIdx]});
343 getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx], nullptr});
346 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
348 LLT Ty2 = MRI.getType(MI.getOperand(1).getReg());
350 assert(Ty.getSizeInBits() == 32 && "Unsupported size for G_SELECT");
351 assert(Ty2.getSizeInBits() == 1 && "Unsupported size for G_SELECT");
353 getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx],
354 &ARM::ValueMappings[ARM::GPR3OpsIdx],
355 &ARM::ValueMappings[ARM::GPR3OpsIdx],
356 &ARM::ValueMappings[ARM::GPR3OpsIdx]});
360 LLT Ty2 = MRI.getType(MI.getOperand(2).getReg());
362 assert(Ty2.getSizeInBits() == 32 && "Unsupported size for G_ICMP");
364 getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx], nullptr,
365 &ARM::ValueMappings[ARM::GPR3OpsIdx],
366 &ARM::ValueMappings[ARM::GPR3OpsIdx]});
370 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
372 LLT Ty1 = MRI.getType(MI.getOperand(2).getReg());
373 LLT Ty2 = MRI.getType(MI.getOperand(3).getReg());
375 assert(Ty.getSizeInBits() == 1 && "Unsupported size for G_FCMP");
376 assert(Ty1.getSizeInBits() == Ty2.getSizeInBits() &&
377 "Mismatched operand sizes for G_FCMP");
379 unsigned Size = Ty1.getSizeInBits();
380 assert((Size == 32 || Size == 64) && "Unsupported size for G_FCMP");
382 auto FPRValueMapping = Size == 32 ? &ARM::ValueMappings[ARM::SPR3OpsIdx]
383 : &ARM::ValueMappings[ARM::DPR3OpsIdx];
385 getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx], nullptr,
386 FPRValueMapping, FPRValueMapping});
389 case G_MERGE_VALUES: {
390 // We only support G_MERGE_VALUES for creating a double precision floating
391 // point value out of two GPRs.
392 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
393 LLT Ty1 = MRI.getType(MI.getOperand(1).getReg());
394 LLT Ty2 = MRI.getType(MI.getOperand(2).getReg());
395 if (Ty.getSizeInBits() != 64 || Ty1.getSizeInBits() != 32 ||
396 Ty2.getSizeInBits() != 32)
397 return getInvalidInstructionMapping();
399 getOperandsMapping({&ARM::ValueMappings[ARM::DPR3OpsIdx],
400 &ARM::ValueMappings[ARM::GPR3OpsIdx],
401 &ARM::ValueMappings[ARM::GPR3OpsIdx]});
404 case G_UNMERGE_VALUES: {
405 // We only support G_UNMERGE_VALUES for splitting a double precision
406 // floating point value into two GPRs.
407 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
408 LLT Ty1 = MRI.getType(MI.getOperand(1).getReg());
409 LLT Ty2 = MRI.getType(MI.getOperand(2).getReg());
410 if (Ty.getSizeInBits() != 32 || Ty1.getSizeInBits() != 32 ||
411 Ty2.getSizeInBits() != 64)
412 return getInvalidInstructionMapping();
414 getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx],
415 &ARM::ValueMappings[ARM::GPR3OpsIdx],
416 &ARM::ValueMappings[ARM::DPR3OpsIdx]});
420 OperandsMapping = getOperandsMapping({nullptr});
424 getOperandsMapping({&ARM::ValueMappings[ARM::GPR3OpsIdx], nullptr});
427 return getInvalidInstructionMapping();
431 for (unsigned i = 0; i < NumOperands; i++) {
432 for (const auto &Mapping : OperandsMapping[i]) {
434 (Mapping.RegBank->getID() != ARM::FPRRegBankID ||
435 MF.getSubtarget<ARMSubtarget>().hasVFP2()) &&
436 "Trying to use floating point register bank on target without vfp");
441 return getInstructionMapping(DefaultMappingID, /*Cost=*/1, OperandsMapping,