1 //===- ARMRegisterBankInfo.cpp -----------------------------------*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 /// This file implements the targeting of the RegisterBankInfo class for ARM.
10 /// \todo This should be generated by TableGen.
11 //===----------------------------------------------------------------------===//
13 #include "ARMRegisterBankInfo.h"
14 #include "ARMInstrInfo.h" // For the register classes
15 #include "ARMSubtarget.h"
16 #include "llvm/CodeGen/MachineRegisterInfo.h"
17 #include "llvm/CodeGen/RegisterBank.h"
18 #include "llvm/CodeGen/RegisterBankInfo.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #define GET_TARGET_REGBANK_IMPL
22 #include "ARMGenRegisterBank.inc"
26 // FIXME: TableGen this.
27 // If it grows too much and TableGen still isn't ready to do the job, extract it
28 // into an ARMGenRegisterBankInfo.def (similar to AArch64).
31 enum PartialMappingIdx
{
38 const RegisterBankInfo::PartialMapping PartMappings
[]{
39 // GPR Partial Mapping
41 // SPR Partial Mapping
43 // DPR Partial Mapping
48 static bool checkPartMapping(const RegisterBankInfo::PartialMapping
&PM
,
49 unsigned Start
, unsigned Length
,
51 return PM
.StartIdx
== Start
&& PM
.Length
== Length
&&
52 PM
.RegBank
->getID() == RegBankID
;
55 static void checkPartialMappings() {
57 checkPartMapping(PartMappings
[PMI_GPR
- PMI_Min
], 0, 32, GPRRegBankID
) &&
58 "Wrong mapping for GPR");
60 checkPartMapping(PartMappings
[PMI_SPR
- PMI_Min
], 0, 32, FPRRegBankID
) &&
61 "Wrong mapping for SPR");
63 checkPartMapping(PartMappings
[PMI_DPR
- PMI_Min
], 0, 64, FPRRegBankID
) &&
64 "Wrong mapping for DPR");
68 enum ValueMappingIdx
{
75 const RegisterBankInfo::ValueMapping ValueMappings
[] = {
79 {&PartMappings
[PMI_GPR
- PMI_Min
], 1},
80 {&PartMappings
[PMI_GPR
- PMI_Min
], 1},
81 {&PartMappings
[PMI_GPR
- PMI_Min
], 1},
83 {&PartMappings
[PMI_SPR
- PMI_Min
], 1},
84 {&PartMappings
[PMI_SPR
- PMI_Min
], 1},
85 {&PartMappings
[PMI_SPR
- PMI_Min
], 1},
87 {&PartMappings
[PMI_DPR
- PMI_Min
], 1},
88 {&PartMappings
[PMI_DPR
- PMI_Min
], 1},
89 {&PartMappings
[PMI_DPR
- PMI_Min
], 1}};
93 checkValueMapping(const RegisterBankInfo::ValueMapping
&VM
,
94 const 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 // We have only one set of register banks, whatever the subtarget
135 // is. Therefore, the initialization of the RegBanks table should be
136 // done only once. Indeed the table of all register banks
137 // (ARM::RegBanks) is unique in the compiler. At some point, it
138 // will get tablegen'ed and the whole constructor becomes empty.
139 static llvm::once_flag InitializeRegisterBankFlag
;
141 static auto InitializeRegisterBankOnce
= [&]() {
142 const RegisterBank
&RBGPR
= getRegBank(ARM::GPRRegBankID
);
144 assert(&ARM::GPRRegBank
== &RBGPR
&& "The order in RegBanks is messed up");
146 // Initialize the GPR bank.
147 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::GPRRegClassID
)) &&
148 "Subclass not added?");
149 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::GPRwithAPSRRegClassID
)) &&
150 "Subclass not added?");
151 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::GPRnopcRegClassID
)) &&
152 "Subclass not added?");
153 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::rGPRRegClassID
)) &&
154 "Subclass not added?");
155 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::tGPRRegClassID
)) &&
156 "Subclass not added?");
157 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::tcGPRRegClassID
)) &&
158 "Subclass not added?");
159 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::tGPROdd_and_tcGPRRegClassID
)) &&
160 "Subclass not added?");
161 assert(getMaximumSize(RBGPR
.getID()) == 32 &&
162 "GPRs should hold up to 32-bit");
165 ARM::checkPartialMappings();
166 ARM::checkValueMappings();
170 llvm::call_once(InitializeRegisterBankFlag
, InitializeRegisterBankOnce
);
173 const RegisterBankInfo::InstructionMapping
&
174 ARMRegisterBankInfo::getInstrMapping(const MachineInstr
&MI
) const {
175 auto Opc
= MI
.getOpcode();
177 // Try the default logic for non-generic instructions that are either copies
178 // or already have some operands assigned to banks.
179 if (!isPreISelGenericOpcode(Opc
) || Opc
== TargetOpcode::G_PHI
) {
180 const InstructionMapping
&Mapping
= getInstrMappingImpl(MI
);
181 if (Mapping
.isValid())
185 using namespace TargetOpcode
;
187 const MachineFunction
&MF
= *MI
.getParent()->getParent();
188 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
189 unsigned NumOperands
= MI
.getNumOperands();
190 const ValueMapping
*OperandsMapping
= &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
195 // Integer operations where the source and destination are in the
196 // same register class.
197 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
198 OperandsMapping
= Ty
.getSizeInBits() == 64
199 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
200 : &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
219 // FIXME: We're abusing the fact that everything lives in a GPR for now; in
220 // the real world we would use different mappings.
221 OperandsMapping
= &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
224 // In some cases we may end up with a G_TRUNC from a 64-bit value to a
225 // 32-bit value. This isn't a real floating point trunc (that would be a
226 // G_FPTRUNC). Instead it is an integer trunc in disguise, which can appear
227 // because the legalizer doesn't distinguish between integer and floating
228 // point values so it may leave some 64-bit integers un-narrowed. Until we
229 // have a more principled solution that doesn't let such things sneak all
230 // the way to this point, just map the source to a DPR and the destination
232 LLT LargeTy
= MRI
.getType(MI
.getOperand(1).getReg());
234 LargeTy
.getSizeInBits() <= 32
235 ? &ARM::ValueMappings
[ARM::GPR3OpsIdx
]
236 : getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
237 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]});
242 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
244 Ty
.getSizeInBits() == 64
245 ? getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
246 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]})
247 : &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
255 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
256 OperandsMapping
=Ty
.getSizeInBits() == 64
257 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
258 : &ARM::ValueMappings
[ARM::SPR3OpsIdx
];
262 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
264 Ty
.getSizeInBits() == 64
265 ? getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
266 &ARM::ValueMappings
[ARM::DPR3OpsIdx
],
267 &ARM::ValueMappings
[ARM::DPR3OpsIdx
],
268 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]})
269 : getOperandsMapping({&ARM::ValueMappings
[ARM::SPR3OpsIdx
],
270 &ARM::ValueMappings
[ARM::SPR3OpsIdx
],
271 &ARM::ValueMappings
[ARM::SPR3OpsIdx
],
272 &ARM::ValueMappings
[ARM::SPR3OpsIdx
]});
276 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
277 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
278 if (ToTy
.getSizeInBits() == 64 && FromTy
.getSizeInBits() == 32)
280 getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
281 &ARM::ValueMappings
[ARM::SPR3OpsIdx
]});
285 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
286 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
287 if (ToTy
.getSizeInBits() == 32 && FromTy
.getSizeInBits() == 64)
289 getOperandsMapping({&ARM::ValueMappings
[ARM::SPR3OpsIdx
],
290 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]});
295 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
296 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
297 if ((FromTy
.getSizeInBits() == 32 || FromTy
.getSizeInBits() == 64) &&
298 ToTy
.getSizeInBits() == 32)
300 FromTy
.getSizeInBits() == 64
301 ? getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
302 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]})
303 : getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
304 &ARM::ValueMappings
[ARM::SPR3OpsIdx
]});
309 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
310 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
311 if (FromTy
.getSizeInBits() == 32 &&
312 (ToTy
.getSizeInBits() == 32 || ToTy
.getSizeInBits() == 64))
314 ToTy
.getSizeInBits() == 64
315 ? getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
316 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]})
317 : getOperandsMapping({&ARM::ValueMappings
[ARM::SPR3OpsIdx
],
318 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
322 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
323 OperandsMapping
= getOperandsMapping(
324 {Ty
.getSizeInBits() == 64 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
325 : &ARM::ValueMappings
[ARM::SPR3OpsIdx
],
332 case G_CONSTANT_POOL
:
334 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr});
337 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
339 LLT Ty2
= MRI
.getType(MI
.getOperand(1).getReg());
341 assert(Ty
.getSizeInBits() == 32 && "Unsupported size for G_SELECT");
342 assert(Ty2
.getSizeInBits() == 1 && "Unsupported size for G_SELECT");
344 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
345 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
346 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
347 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
351 LLT Ty2
= MRI
.getType(MI
.getOperand(2).getReg());
353 assert(Ty2
.getSizeInBits() == 32 && "Unsupported size for G_ICMP");
355 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr,
356 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
357 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
361 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
363 LLT Ty1
= MRI
.getType(MI
.getOperand(2).getReg());
364 LLT Ty2
= MRI
.getType(MI
.getOperand(3).getReg());
366 assert(Ty
.getSizeInBits() == 1 && "Unsupported size for G_FCMP");
367 assert(Ty1
.getSizeInBits() == Ty2
.getSizeInBits() &&
368 "Mismatched operand sizes for G_FCMP");
370 unsigned Size
= Ty1
.getSizeInBits();
371 assert((Size
== 32 || Size
== 64) && "Unsupported size for G_FCMP");
373 auto FPRValueMapping
= Size
== 32 ? &ARM::ValueMappings
[ARM::SPR3OpsIdx
]
374 : &ARM::ValueMappings
[ARM::DPR3OpsIdx
];
376 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr,
377 FPRValueMapping
, FPRValueMapping
});
380 case G_MERGE_VALUES
: {
381 // We only support G_MERGE_VALUES for creating a double precision floating
382 // point value out of two GPRs.
383 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
384 LLT Ty1
= MRI
.getType(MI
.getOperand(1).getReg());
385 LLT Ty2
= MRI
.getType(MI
.getOperand(2).getReg());
386 if (Ty
.getSizeInBits() != 64 || Ty1
.getSizeInBits() != 32 ||
387 Ty2
.getSizeInBits() != 32)
388 return getInvalidInstructionMapping();
390 getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
391 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
392 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
395 case G_UNMERGE_VALUES
: {
396 // We only support G_UNMERGE_VALUES for splitting a double precision
397 // floating point value into two GPRs.
398 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
399 LLT Ty1
= MRI
.getType(MI
.getOperand(1).getReg());
400 LLT Ty2
= MRI
.getType(MI
.getOperand(2).getReg());
401 if (Ty
.getSizeInBits() != 32 || Ty1
.getSizeInBits() != 32 ||
402 Ty2
.getSizeInBits() != 64)
403 return getInvalidInstructionMapping();
405 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
406 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
407 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]});
411 OperandsMapping
= getOperandsMapping({nullptr});
415 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr});
418 SmallVector
<const ValueMapping
*, 4> OperandBanks(NumOperands
);
419 const MachineOperand
&MaybeReg
= MI
.getOperand(0);
420 if (MaybeReg
.isReg() && MaybeReg
.getReg()) {
421 unsigned Size
= MRI
.getType(MaybeReg
.getReg()).getSizeInBits();
422 if (Size
> 32 && Size
!= 64)
423 return getInvalidInstructionMapping();
424 OperandBanks
[0] = Size
== 64 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
425 : &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
427 OperandsMapping
= getOperandsMapping(OperandBanks
);
434 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr});
437 OperandsMapping
= getOperandsMapping({nullptr});
440 return getInvalidInstructionMapping();
444 for (unsigned i
= 0; i
< NumOperands
; i
++) {
445 for (const auto &Mapping
: OperandsMapping
[i
]) {
447 (Mapping
.RegBank
->getID() != ARM::FPRRegBankID
||
448 MF
.getSubtarget
<ARMSubtarget
>().hasVFP2Base()) &&
449 "Trying to use floating point register bank on target without vfp");
454 return getInstructionMapping(DefaultMappingID
, /*Cost=*/1, OperandsMapping
,