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/GlobalISel/RegisterBank.h"
17 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.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 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 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}};
92 static bool checkValueMapping(const RegisterBankInfo::ValueMapping
&VM
,
93 RegisterBankInfo::PartialMapping
*BreakDown
) {
94 return VM
.NumBreakDowns
== 1 && VM
.BreakDown
== BreakDown
;
97 static void checkValueMappings() {
98 assert(checkValueMapping(ValueMappings
[GPR3OpsIdx
],
99 &PartMappings
[PMI_GPR
- PMI_Min
]) &&
100 "Wrong value mapping for 3 GPR ops instruction");
101 assert(checkValueMapping(ValueMappings
[GPR3OpsIdx
+ 1],
102 &PartMappings
[PMI_GPR
- PMI_Min
]) &&
103 "Wrong value mapping for 3 GPR ops instruction");
104 assert(checkValueMapping(ValueMappings
[GPR3OpsIdx
+ 2],
105 &PartMappings
[PMI_GPR
- PMI_Min
]) &&
106 "Wrong value mapping for 3 GPR ops instruction");
108 assert(checkValueMapping(ValueMappings
[SPR3OpsIdx
],
109 &PartMappings
[PMI_SPR
- PMI_Min
]) &&
110 "Wrong value mapping for 3 SPR ops instruction");
111 assert(checkValueMapping(ValueMappings
[SPR3OpsIdx
+ 1],
112 &PartMappings
[PMI_SPR
- PMI_Min
]) &&
113 "Wrong value mapping for 3 SPR ops instruction");
114 assert(checkValueMapping(ValueMappings
[SPR3OpsIdx
+ 2],
115 &PartMappings
[PMI_SPR
- PMI_Min
]) &&
116 "Wrong value mapping for 3 SPR ops instruction");
118 assert(checkValueMapping(ValueMappings
[DPR3OpsIdx
],
119 &PartMappings
[PMI_DPR
- PMI_Min
]) &&
120 "Wrong value mapping for 3 DPR ops instruction");
121 assert(checkValueMapping(ValueMappings
[DPR3OpsIdx
+ 1],
122 &PartMappings
[PMI_DPR
- PMI_Min
]) &&
123 "Wrong value mapping for 3 DPR ops instruction");
124 assert(checkValueMapping(ValueMappings
[DPR3OpsIdx
+ 2],
125 &PartMappings
[PMI_DPR
- PMI_Min
]) &&
126 "Wrong value mapping for 3 DPR ops instruction");
129 } // end namespace arm
130 } // end namespace llvm
132 ARMRegisterBankInfo::ARMRegisterBankInfo(const TargetRegisterInfo
&TRI
)
133 : ARMGenRegisterBankInfo() {
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::GPRnoip_and_tcGPRRegClassID
)) &&
160 "Subclass not added?");
161 assert(RBGPR
.covers(*TRI
.getRegClass(
162 ARM::tGPREven_and_GPRnoip_and_tcGPRRegClassID
)) &&
163 "Subclass not added?");
164 assert(RBGPR
.covers(*TRI
.getRegClass(ARM::tGPROdd_and_tcGPRRegClassID
)) &&
165 "Subclass not added?");
166 assert(RBGPR
.getSize() == 32 && "GPRs should hold up to 32-bit");
169 ARM::checkPartialMappings();
170 ARM::checkValueMappings();
174 llvm::call_once(InitializeRegisterBankFlag
, InitializeRegisterBankOnce
);
178 ARMRegisterBankInfo::getRegBankFromRegClass(const TargetRegisterClass
&RC
,
182 switch (RC
.getID()) {
184 case GPRwithAPSRRegClassID
:
185 case GPRnoipRegClassID
:
186 case GPRnopcRegClassID
:
187 case GPRnoip_and_GPRnopcRegClassID
:
189 case GPRspRegClassID
:
190 case GPRnoip_and_tcGPRRegClassID
:
191 case tcGPRRegClassID
:
193 case tGPREvenRegClassID
:
194 case tGPROddRegClassID
:
195 case tGPR_and_tGPREvenRegClassID
:
196 case tGPR_and_tGPROddRegClassID
:
197 case tGPREven_and_tcGPRRegClassID
:
198 case tGPREven_and_GPRnoip_and_tcGPRRegClassID
:
199 case tGPROdd_and_tcGPRRegClassID
:
200 return getRegBank(ARM::GPRRegBankID
);
202 case SPR_8RegClassID
:
204 case DPR_8RegClassID
:
207 return getRegBank(ARM::FPRRegBankID
);
209 llvm_unreachable("Unsupported register kind");
212 llvm_unreachable("Switch should handle all register classes");
215 const RegisterBankInfo::InstructionMapping
&
216 ARMRegisterBankInfo::getInstrMapping(const MachineInstr
&MI
) const {
217 auto Opc
= MI
.getOpcode();
219 // Try the default logic for non-generic instructions that are either copies
220 // or already have some operands assigned to banks.
221 if (!isPreISelGenericOpcode(Opc
) || Opc
== TargetOpcode::G_PHI
) {
222 const InstructionMapping
&Mapping
= getInstrMappingImpl(MI
);
223 if (Mapping
.isValid())
227 using namespace TargetOpcode
;
229 const MachineFunction
&MF
= *MI
.getParent()->getParent();
230 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
231 unsigned NumOperands
= MI
.getNumOperands();
232 const ValueMapping
*OperandsMapping
= &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
237 // Integer operations where the source and destination are in the
238 // same register class.
239 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
240 OperandsMapping
= Ty
.getSizeInBits() == 64
241 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
242 : &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
261 // FIXME: We're abusing the fact that everything lives in a GPR for now; in
262 // the real world we would use different mappings.
263 OperandsMapping
= &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
266 // In some cases we may end up with a G_TRUNC from a 64-bit value to a
267 // 32-bit value. This isn't a real floating point trunc (that would be a
268 // G_FPTRUNC). Instead it is an integer trunc in disguise, which can appear
269 // because the legalizer doesn't distinguish between integer and floating
270 // point values so it may leave some 64-bit integers un-narrowed. Until we
271 // have a more principled solution that doesn't let such things sneak all
272 // the way to this point, just map the source to a DPR and the destination
274 LLT LargeTy
= MRI
.getType(MI
.getOperand(1).getReg());
276 LargeTy
.getSizeInBits() <= 32
277 ? &ARM::ValueMappings
[ARM::GPR3OpsIdx
]
278 : getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
279 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]});
284 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
286 Ty
.getSizeInBits() == 64
287 ? getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
288 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]})
289 : &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
297 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
298 OperandsMapping
=Ty
.getSizeInBits() == 64
299 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
300 : &ARM::ValueMappings
[ARM::SPR3OpsIdx
];
304 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
306 Ty
.getSizeInBits() == 64
307 ? getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
308 &ARM::ValueMappings
[ARM::DPR3OpsIdx
],
309 &ARM::ValueMappings
[ARM::DPR3OpsIdx
],
310 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]})
311 : getOperandsMapping({&ARM::ValueMappings
[ARM::SPR3OpsIdx
],
312 &ARM::ValueMappings
[ARM::SPR3OpsIdx
],
313 &ARM::ValueMappings
[ARM::SPR3OpsIdx
],
314 &ARM::ValueMappings
[ARM::SPR3OpsIdx
]});
318 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
319 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
320 if (ToTy
.getSizeInBits() == 64 && FromTy
.getSizeInBits() == 32)
322 getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
323 &ARM::ValueMappings
[ARM::SPR3OpsIdx
]});
327 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
328 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
329 if (ToTy
.getSizeInBits() == 32 && FromTy
.getSizeInBits() == 64)
331 getOperandsMapping({&ARM::ValueMappings
[ARM::SPR3OpsIdx
],
332 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]});
337 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
338 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
339 if ((FromTy
.getSizeInBits() == 32 || FromTy
.getSizeInBits() == 64) &&
340 ToTy
.getSizeInBits() == 32)
342 FromTy
.getSizeInBits() == 64
343 ? getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
344 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]})
345 : getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
346 &ARM::ValueMappings
[ARM::SPR3OpsIdx
]});
351 LLT ToTy
= MRI
.getType(MI
.getOperand(0).getReg());
352 LLT FromTy
= MRI
.getType(MI
.getOperand(1).getReg());
353 if (FromTy
.getSizeInBits() == 32 &&
354 (ToTy
.getSizeInBits() == 32 || ToTy
.getSizeInBits() == 64))
356 ToTy
.getSizeInBits() == 64
357 ? getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
358 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]})
359 : getOperandsMapping({&ARM::ValueMappings
[ARM::SPR3OpsIdx
],
360 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
364 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
365 OperandsMapping
= getOperandsMapping(
366 {Ty
.getSizeInBits() == 64 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
367 : &ARM::ValueMappings
[ARM::SPR3OpsIdx
],
375 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr});
378 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
380 LLT Ty2
= MRI
.getType(MI
.getOperand(1).getReg());
382 assert(Ty
.getSizeInBits() == 32 && "Unsupported size for G_SELECT");
383 assert(Ty2
.getSizeInBits() == 1 && "Unsupported size for G_SELECT");
385 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
386 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
387 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
388 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
392 LLT Ty2
= MRI
.getType(MI
.getOperand(2).getReg());
394 assert(Ty2
.getSizeInBits() == 32 && "Unsupported size for G_ICMP");
396 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr,
397 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
398 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
402 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
404 LLT Ty1
= MRI
.getType(MI
.getOperand(2).getReg());
405 LLT Ty2
= MRI
.getType(MI
.getOperand(3).getReg());
407 assert(Ty
.getSizeInBits() == 1 && "Unsupported size for G_FCMP");
408 assert(Ty1
.getSizeInBits() == Ty2
.getSizeInBits() &&
409 "Mismatched operand sizes for G_FCMP");
411 unsigned Size
= Ty1
.getSizeInBits();
412 assert((Size
== 32 || Size
== 64) && "Unsupported size for G_FCMP");
414 auto FPRValueMapping
= Size
== 32 ? &ARM::ValueMappings
[ARM::SPR3OpsIdx
]
415 : &ARM::ValueMappings
[ARM::DPR3OpsIdx
];
417 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr,
418 FPRValueMapping
, FPRValueMapping
});
421 case G_MERGE_VALUES
: {
422 // We only support G_MERGE_VALUES for creating a double precision floating
423 // point value out of two GPRs.
424 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
425 LLT Ty1
= MRI
.getType(MI
.getOperand(1).getReg());
426 LLT Ty2
= MRI
.getType(MI
.getOperand(2).getReg());
427 if (Ty
.getSizeInBits() != 64 || Ty1
.getSizeInBits() != 32 ||
428 Ty2
.getSizeInBits() != 32)
429 return getInvalidInstructionMapping();
431 getOperandsMapping({&ARM::ValueMappings
[ARM::DPR3OpsIdx
],
432 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
433 &ARM::ValueMappings
[ARM::GPR3OpsIdx
]});
436 case G_UNMERGE_VALUES
: {
437 // We only support G_UNMERGE_VALUES for splitting a double precision
438 // floating point value into two GPRs.
439 LLT Ty
= MRI
.getType(MI
.getOperand(0).getReg());
440 LLT Ty1
= MRI
.getType(MI
.getOperand(1).getReg());
441 LLT Ty2
= MRI
.getType(MI
.getOperand(2).getReg());
442 if (Ty
.getSizeInBits() != 32 || Ty1
.getSizeInBits() != 32 ||
443 Ty2
.getSizeInBits() != 64)
444 return getInvalidInstructionMapping();
446 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
],
447 &ARM::ValueMappings
[ARM::GPR3OpsIdx
],
448 &ARM::ValueMappings
[ARM::DPR3OpsIdx
]});
452 OperandsMapping
= getOperandsMapping({nullptr});
456 getOperandsMapping({&ARM::ValueMappings
[ARM::GPR3OpsIdx
], nullptr});
459 SmallVector
<const ValueMapping
*, 4> OperandBanks(NumOperands
);
460 const MachineOperand
&MaybeReg
= MI
.getOperand(0);
461 if (MaybeReg
.isReg() && MaybeReg
.getReg()) {
462 unsigned Size
= MRI
.getType(MaybeReg
.getReg()).getSizeInBits();
463 if (Size
> 32 && Size
!= 64)
464 return getInvalidInstructionMapping();
465 OperandBanks
[0] = Size
== 64 ? &ARM::ValueMappings
[ARM::DPR3OpsIdx
]
466 : &ARM::ValueMappings
[ARM::GPR3OpsIdx
];
468 OperandsMapping
= getOperandsMapping(OperandBanks
);
472 return getInvalidInstructionMapping();
476 for (unsigned i
= 0; i
< NumOperands
; i
++) {
477 for (const auto &Mapping
: OperandsMapping
[i
]) {
479 (Mapping
.RegBank
->getID() != ARM::FPRRegBankID
||
480 MF
.getSubtarget
<ARMSubtarget
>().hasVFP2Base()) &&
481 "Trying to use floating point register bank on target without vfp");
486 return getInstructionMapping(DefaultMappingID
, /*Cost=*/1, OperandsMapping
,