LLVM 22.0.0git
AArch64RegisterBankInfo.cpp
Go to the documentation of this file.
1//===- AArch64RegisterBankInfo.cpp ----------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements the targeting of the RegisterBankInfo class for
10/// AArch64.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
15#include "AArch64RegisterInfo.h"
16#include "AArch64Subtarget.h"
18#include "llvm/ADT/STLExtras.h"
33#include "llvm/IR/IntrinsicsAArch64.h"
36#include <cassert>
37
38#define GET_TARGET_REGBANK_IMPL
39#include "AArch64GenRegisterBank.inc"
40
41// This file will be TableGen'ed at some point.
42#include "AArch64GenRegisterBankInfo.def"
43
44using namespace llvm;
45static const unsigned CustomMappingID = 1;
46
48 const TargetRegisterInfo &TRI) {
49 static llvm::once_flag InitializeRegisterBankFlag;
50
51 static auto InitializeRegisterBankOnce = [&]() {
52 // We have only one set of register banks, whatever the subtarget
53 // is. Therefore, the initialization of the RegBanks table should be
54 // done only once. Indeed the table of all register banks
55 // (AArch64::RegBanks) is unique in the compiler. At some point, it
56 // will get tablegen'ed and the whole constructor becomes empty.
57
58 const RegisterBank &RBGPR = getRegBank(AArch64::GPRRegBankID);
59 (void)RBGPR;
60 assert(&AArch64::GPRRegBank == &RBGPR &&
61 "The order in RegBanks is messed up");
62
63 const RegisterBank &RBFPR = getRegBank(AArch64::FPRRegBankID);
64 (void)RBFPR;
65 assert(&AArch64::FPRRegBank == &RBFPR &&
66 "The order in RegBanks is messed up");
67
68 const RegisterBank &RBCCR = getRegBank(AArch64::CCRegBankID);
69 (void)RBCCR;
70 assert(&AArch64::CCRegBank == &RBCCR &&
71 "The order in RegBanks is messed up");
72
73 // The GPR register bank is fully defined by all the registers in
74 // GR64all + its subclasses.
75 assert(RBGPR.covers(*TRI.getRegClass(AArch64::GPR32RegClassID)) &&
76 "Subclass not added?");
77 assert(getMaximumSize(RBGPR.getID()) == 128 &&
78 "GPRs should hold up to 128-bit");
79
80 // The FPR register bank is fully defined by all the registers in
81 // GR64all + its subclasses.
82 assert(RBFPR.covers(*TRI.getRegClass(AArch64::QQRegClassID)) &&
83 "Subclass not added?");
84 assert(RBFPR.covers(*TRI.getRegClass(AArch64::FPR64RegClassID)) &&
85 "Subclass not added?");
86 assert(getMaximumSize(RBFPR.getID()) == 512 &&
87 "FPRs should hold up to 512-bit via QQQQ sequence");
88
89 assert(RBCCR.covers(*TRI.getRegClass(AArch64::CCRRegClassID)) &&
90 "Class not added?");
91 assert(getMaximumSize(RBCCR.getID()) == 32 &&
92 "CCR should hold up to 32-bit");
93
94 // Check that the TableGen'ed like file is in sync we our expectations.
95 // First, the Idx.
98 "PartialMappingIdx's are incorrectly ordered");
102 "PartialMappingIdx's are incorrectly ordered");
103// Now, the content.
104// Check partial mapping.
105#define CHECK_PARTIALMAP(Idx, ValStartIdx, ValLength, RB) \
106 do { \
107 assert( \
108 checkPartialMap(PartialMappingIdx::Idx, ValStartIdx, ValLength, RB) && \
109 #Idx " is incorrectly initialized"); \
110 } while (false)
111
112 CHECK_PARTIALMAP(PMI_GPR32, 0, 32, RBGPR);
113 CHECK_PARTIALMAP(PMI_GPR64, 0, 64, RBGPR);
114 CHECK_PARTIALMAP(PMI_GPR128, 0, 128, RBGPR);
115 CHECK_PARTIALMAP(PMI_FPR16, 0, 16, RBFPR);
116 CHECK_PARTIALMAP(PMI_FPR32, 0, 32, RBFPR);
117 CHECK_PARTIALMAP(PMI_FPR64, 0, 64, RBFPR);
118 CHECK_PARTIALMAP(PMI_FPR128, 0, 128, RBFPR);
119 CHECK_PARTIALMAP(PMI_FPR256, 0, 256, RBFPR);
120 CHECK_PARTIALMAP(PMI_FPR512, 0, 512, RBFPR);
121
122// Check value mapping.
123#define CHECK_VALUEMAP_IMPL(RBName, Size, Offset) \
124 do { \
125 assert(checkValueMapImpl(PartialMappingIdx::PMI_##RBName##Size, \
126 PartialMappingIdx::PMI_First##RBName, Size, \
127 Offset) && \
128 #RBName #Size " " #Offset " is incorrectly initialized"); \
129 } while (false)
130
131#define CHECK_VALUEMAP(RBName, Size) CHECK_VALUEMAP_IMPL(RBName, Size, 0)
132
133 CHECK_VALUEMAP(GPR, 32);
134 CHECK_VALUEMAP(GPR, 64);
135 CHECK_VALUEMAP(GPR, 128);
136 CHECK_VALUEMAP(FPR, 16);
137 CHECK_VALUEMAP(FPR, 32);
138 CHECK_VALUEMAP(FPR, 64);
139 CHECK_VALUEMAP(FPR, 128);
140 CHECK_VALUEMAP(FPR, 256);
141 CHECK_VALUEMAP(FPR, 512);
142
143// Check the value mapping for 3-operands instructions where all the operands
144// map to the same value mapping.
145#define CHECK_VALUEMAP_3OPS(RBName, Size) \
146 do { \
147 CHECK_VALUEMAP_IMPL(RBName, Size, 0); \
148 CHECK_VALUEMAP_IMPL(RBName, Size, 1); \
149 CHECK_VALUEMAP_IMPL(RBName, Size, 2); \
150 } while (false)
151
152 CHECK_VALUEMAP_3OPS(GPR, 32);
153 CHECK_VALUEMAP_3OPS(GPR, 64);
154 CHECK_VALUEMAP_3OPS(GPR, 128);
160
161#define CHECK_VALUEMAP_CROSSREGCPY(RBNameDst, RBNameSrc, Size) \
162 do { \
163 unsigned PartialMapDstIdx = PMI_##RBNameDst##Size - PMI_Min; \
164 unsigned PartialMapSrcIdx = PMI_##RBNameSrc##Size - PMI_Min; \
165 (void)PartialMapDstIdx; \
166 (void)PartialMapSrcIdx; \
167 const ValueMapping *Map = getCopyMapping(AArch64::RBNameDst##RegBankID, \
168 AArch64::RBNameSrc##RegBankID, \
169 TypeSize::getFixed(Size)); \
170 (void)Map; \
171 assert(Map[0].BreakDown == \
172 &AArch64GenRegisterBankInfo::PartMappings[PartialMapDstIdx] && \
173 Map[0].NumBreakDowns == 1 && \
174 #RBNameDst #Size " Dst is incorrectly initialized"); \
175 assert(Map[1].BreakDown == \
176 &AArch64GenRegisterBankInfo::PartMappings[PartialMapSrcIdx] && \
177 Map[1].NumBreakDowns == 1 && \
178 #RBNameSrc #Size " Src is incorrectly initialized"); \
179 \
180 } while (false)
181
182 CHECK_VALUEMAP_CROSSREGCPY(GPR, GPR, 32);
184 CHECK_VALUEMAP_CROSSREGCPY(GPR, GPR, 64);
190
191#define CHECK_VALUEMAP_FPEXT(DstSize, SrcSize) \
192 do { \
193 unsigned PartialMapDstIdx = PMI_FPR##DstSize - PMI_Min; \
194 unsigned PartialMapSrcIdx = PMI_FPR##SrcSize - PMI_Min; \
195 (void)PartialMapDstIdx; \
196 (void)PartialMapSrcIdx; \
197 const ValueMapping *Map = getFPExtMapping(DstSize, SrcSize); \
198 (void)Map; \
199 assert(Map[0].BreakDown == \
200 &AArch64GenRegisterBankInfo::PartMappings[PartialMapDstIdx] && \
201 Map[0].NumBreakDowns == 1 && "FPR" #DstSize \
202 " Dst is incorrectly initialized"); \
203 assert(Map[1].BreakDown == \
204 &AArch64GenRegisterBankInfo::PartMappings[PartialMapSrcIdx] && \
205 Map[1].NumBreakDowns == 1 && "FPR" #SrcSize \
206 " Src is incorrectly initialized"); \
207 \
208 } while (false)
209
210 CHECK_VALUEMAP_FPEXT(32, 16);
211 CHECK_VALUEMAP_FPEXT(64, 16);
212 CHECK_VALUEMAP_FPEXT(64, 32);
213 CHECK_VALUEMAP_FPEXT(128, 64);
214
215 assert(verify(TRI) && "Invalid register bank information");
216 };
217
218 llvm::call_once(InitializeRegisterBankFlag, InitializeRegisterBankOnce);
219}
220
222 const RegisterBank &B,
223 const TypeSize Size) const {
224 // What do we do with different size?
225 // copy are same size.
226 // Will introduce other hooks for different size:
227 // * extract cost.
228 // * build_sequence cost.
229
230 // Copy from (resp. to) GPR to (resp. from) FPR involves FMOV.
231 // FIXME: This should be deduced from the scheduling model.
232 if (&A == &AArch64::GPRRegBank && &B == &AArch64::FPRRegBank)
233 // FMOVXDr or FMOVWSr.
234 return 5;
235 if (&A == &AArch64::FPRRegBank && &B == &AArch64::GPRRegBank)
236 // FMOVDXr or FMOVSWr.
237 return 4;
238
240}
241
242const RegisterBank &
244 LLT Ty) const {
245 switch (RC.getID()) {
246 case AArch64::GPR64sponlyRegClassID:
247 return getRegBank(AArch64::GPRRegBankID);
248 default:
250 }
251}
252
255 const MachineInstr &MI) const {
256 const MachineFunction &MF = *MI.getParent()->getParent();
257 const TargetSubtargetInfo &STI = MF.getSubtarget();
258 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
259 const MachineRegisterInfo &MRI = MF.getRegInfo();
260
261 switch (MI.getOpcode()) {
262 case TargetOpcode::G_OR: {
263 // 32 and 64-bit or can be mapped on either FPR or
264 // GPR for the same cost.
265 TypeSize Size = getSizeInBits(MI.getOperand(0).getReg(), MRI, TRI);
266 if (Size != 32 && Size != 64)
267 break;
268
269 // If the instruction has any implicit-defs or uses,
270 // do not mess with it.
271 if (MI.getNumOperands() != 3)
272 break;
273 InstructionMappings AltMappings;
274 const InstructionMapping &GPRMapping = getInstructionMapping(
275 /*ID*/ 1, /*Cost*/ 1, getValueMapping(PMI_FirstGPR, Size),
276 /*NumOperands*/ 3);
277 const InstructionMapping &FPRMapping = getInstructionMapping(
278 /*ID*/ 2, /*Cost*/ 1, getValueMapping(PMI_FirstFPR, Size),
279 /*NumOperands*/ 3);
280
281 AltMappings.push_back(&GPRMapping);
282 AltMappings.push_back(&FPRMapping);
283 return AltMappings;
284 }
285 case TargetOpcode::G_BITCAST: {
286 TypeSize Size = getSizeInBits(MI.getOperand(0).getReg(), MRI, TRI);
287 if (Size != 32 && Size != 64)
288 break;
289
290 // If the instruction has any implicit-defs or uses,
291 // do not mess with it.
292 if (MI.getNumOperands() != 2)
293 break;
294
295 InstructionMappings AltMappings;
296 const InstructionMapping &GPRMapping = getInstructionMapping(
297 /*ID*/ 1, /*Cost*/ 1,
298 getCopyMapping(AArch64::GPRRegBankID, AArch64::GPRRegBankID, Size),
299 /*NumOperands*/ 2);
300 const InstructionMapping &FPRMapping = getInstructionMapping(
301 /*ID*/ 2, /*Cost*/ 1,
302 getCopyMapping(AArch64::FPRRegBankID, AArch64::FPRRegBankID, Size),
303 /*NumOperands*/ 2);
304 const InstructionMapping &GPRToFPRMapping = getInstructionMapping(
305 /*ID*/ 3,
306 /*Cost*/
307 copyCost(AArch64::GPRRegBank, AArch64::FPRRegBank,
309 getCopyMapping(AArch64::FPRRegBankID, AArch64::GPRRegBankID, Size),
310 /*NumOperands*/ 2);
311 const InstructionMapping &FPRToGPRMapping = getInstructionMapping(
312 /*ID*/ 3,
313 /*Cost*/
314 copyCost(AArch64::GPRRegBank, AArch64::FPRRegBank,
316 getCopyMapping(AArch64::GPRRegBankID, AArch64::FPRRegBankID, Size),
317 /*NumOperands*/ 2);
318
319 AltMappings.push_back(&GPRMapping);
320 AltMappings.push_back(&FPRMapping);
321 AltMappings.push_back(&GPRToFPRMapping);
322 AltMappings.push_back(&FPRToGPRMapping);
323 return AltMappings;
324 }
325 case TargetOpcode::G_LOAD: {
326 TypeSize Size = getSizeInBits(MI.getOperand(0).getReg(), MRI, TRI);
327 if (Size != 64)
328 break;
329
330 // If the instruction has any implicit-defs or uses,
331 // do not mess with it.
332 if (MI.getNumOperands() != 2)
333 break;
334
335 InstructionMappings AltMappings;
336 const InstructionMapping &GPRMapping = getInstructionMapping(
337 /*ID*/ 1, /*Cost*/ 1,
340 // Addresses are GPR 64-bit.
342 /*NumOperands*/ 2);
343 const InstructionMapping &FPRMapping = getInstructionMapping(
344 /*ID*/ 2, /*Cost*/ 1,
347 // Addresses are GPR 64-bit.
349 /*NumOperands*/ 2);
350
351 AltMappings.push_back(&GPRMapping);
352 AltMappings.push_back(&FPRMapping);
353 return AltMappings;
354 }
355 default:
356 break;
357 }
359}
360
361void AArch64RegisterBankInfo::applyMappingImpl(
362 MachineIRBuilder &Builder, const OperandsMapper &OpdMapper) const {
363 MachineInstr &MI = OpdMapper.getMI();
364 MachineRegisterInfo &MRI = OpdMapper.getMRI();
365
366 switch (MI.getOpcode()) {
367 case TargetOpcode::G_OR:
368 case TargetOpcode::G_BITCAST:
369 case TargetOpcode::G_LOAD:
370 // Those ID must match getInstrAlternativeMappings.
371 assert((OpdMapper.getInstrMapping().getID() >= 1 &&
372 OpdMapper.getInstrMapping().getID() <= 4) &&
373 "Don't know how to handle that ID");
374 return applyDefaultMapping(OpdMapper);
375 case TargetOpcode::G_INSERT_VECTOR_ELT: {
376 // Extend smaller gpr operands to 32 bit.
377 Builder.setInsertPt(*MI.getParent(), MI.getIterator());
378 auto Ext = Builder.buildAnyExt(LLT::scalar(32), MI.getOperand(2).getReg());
379 MRI.setRegBank(Ext.getReg(0), getRegBank(AArch64::GPRRegBankID));
380 MI.getOperand(2).setReg(Ext.getReg(0));
381 return applyDefaultMapping(OpdMapper);
382 }
383 case AArch64::G_DUP: {
384 // Extend smaller gpr to 32-bits
385 assert(MRI.getType(MI.getOperand(1).getReg()).getSizeInBits() < 32 &&
386 "Expected sources smaller than 32-bits");
387 Builder.setInsertPt(*MI.getParent(), MI.getIterator());
388
389 Register ConstReg;
390 auto ConstMI = MRI.getVRegDef(MI.getOperand(1).getReg());
391 if (ConstMI->getOpcode() == TargetOpcode::G_CONSTANT) {
392 auto CstVal = ConstMI->getOperand(1).getCImm()->getValue();
393 ConstReg =
394 Builder.buildConstant(LLT::scalar(32), CstVal.sext(32)).getReg(0);
395 } else {
396 ConstReg = Builder.buildAnyExt(LLT::scalar(32), MI.getOperand(1).getReg())
397 .getReg(0);
398 }
399 MRI.setRegBank(ConstReg, getRegBank(AArch64::GPRRegBankID));
400 MI.getOperand(1).setReg(ConstReg);
401 return applyDefaultMapping(OpdMapper);
402 }
403 default:
404 llvm_unreachable("Don't know how to handle that operation");
405 }
406}
407
409AArch64RegisterBankInfo::getSameKindOfOperandsMapping(
410 const MachineInstr &MI) const {
411 const unsigned Opc = MI.getOpcode();
412 const MachineFunction &MF = *MI.getParent()->getParent();
413 const MachineRegisterInfo &MRI = MF.getRegInfo();
414
415 unsigned NumOperands = MI.getNumOperands();
416 assert(NumOperands <= 3 &&
417 "This code is for instructions with 3 or less operands");
418
419 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
422
424
425#ifndef NDEBUG
426 // Make sure all the operands are using similar size and type.
427 // Should probably be checked by the machine verifier.
428 // This code won't catch cases where the number of lanes is
429 // different between the operands.
430 // If we want to go to that level of details, it is probably
431 // best to check that the types are the same, period.
432 // Currently, we just check that the register banks are the same
433 // for each types.
434 for (unsigned Idx = 1; Idx != NumOperands; ++Idx) {
435 LLT OpTy = MRI.getType(MI.getOperand(Idx).getReg());
436 assert(
438 RBIdx, OpTy.getSizeInBits()) ==
440 "Operand has incompatible size");
441 bool OpIsFPR = OpTy.isVector() || isPreISelGenericFloatingPointOpcode(Opc);
442 (void)OpIsFPR;
443 assert(IsFPR == OpIsFPR && "Operand has incompatible type");
444 }
445#endif // End NDEBUG.
446
448 getValueMapping(RBIdx, Size), NumOperands);
449}
450
451/// \returns true if a given intrinsic only uses and defines FPRs.
453 const MachineInstr &MI) {
454 // TODO: Add more intrinsics.
455 switch (cast<GIntrinsic>(MI).getIntrinsicID()) {
456 default:
457 return false;
458 case Intrinsic::aarch64_neon_uaddlv:
459 case Intrinsic::aarch64_neon_uaddv:
460 case Intrinsic::aarch64_neon_saddv:
461 case Intrinsic::aarch64_neon_umaxv:
462 case Intrinsic::aarch64_neon_smaxv:
463 case Intrinsic::aarch64_neon_uminv:
464 case Intrinsic::aarch64_neon_sminv:
465 case Intrinsic::aarch64_neon_faddv:
466 case Intrinsic::aarch64_neon_fmaxv:
467 case Intrinsic::aarch64_neon_fminv:
468 case Intrinsic::aarch64_neon_fmaxnmv:
469 case Intrinsic::aarch64_neon_fminnmv:
470 case Intrinsic::aarch64_neon_fmulx:
471 case Intrinsic::aarch64_neon_frecpe:
472 case Intrinsic::aarch64_neon_frecps:
473 case Intrinsic::aarch64_neon_frecpx:
474 case Intrinsic::aarch64_neon_frsqrte:
475 case Intrinsic::aarch64_neon_frsqrts:
476 case Intrinsic::aarch64_neon_facge:
477 case Intrinsic::aarch64_neon_facgt:
478 case Intrinsic::aarch64_neon_fabd:
479 case Intrinsic::aarch64_sisd_fabd:
480 case Intrinsic::aarch64_neon_sqrdmlah:
481 case Intrinsic::aarch64_neon_sqrdmlsh:
482 case Intrinsic::aarch64_neon_sqrdmulh:
483 case Intrinsic::aarch64_neon_sqadd:
484 case Intrinsic::aarch64_neon_sqsub:
485 return true;
486 case Intrinsic::aarch64_neon_saddlv: {
487 const LLT SrcTy = MRI.getType(MI.getOperand(2).getReg());
488 return SrcTy.getElementType().getSizeInBits() >= 16 &&
489 SrcTy.getElementCount().getFixedValue() >= 4;
490 }
491 }
492}
493
494bool AArch64RegisterBankInfo::isPHIWithFPConstraints(
495 const MachineInstr &MI, const MachineRegisterInfo &MRI,
496 const AArch64RegisterInfo &TRI, const unsigned Depth) const {
497 if (!MI.isPHI() || Depth > MaxFPRSearchDepth)
498 return false;
499
500 return any_of(MRI.use_nodbg_instructions(MI.getOperand(0).getReg()),
501 [&](const MachineInstr &UseMI) {
502 if (onlyUsesFP(UseMI, MRI, TRI, Depth + 1))
503 return true;
504 return isPHIWithFPConstraints(UseMI, MRI, TRI, Depth + 1);
505 });
506}
507
508bool AArch64RegisterBankInfo::hasFPConstraints(const MachineInstr &MI,
511 unsigned Depth) const {
512 unsigned Op = MI.getOpcode();
513 if (Op == TargetOpcode::G_INTRINSIC && isFPIntrinsic(MRI, MI))
514 return true;
515
516 // Do we have an explicit floating point instruction?
518 return true;
519
520 // No. Check if we have a copy-like instruction. If we do, then we could
521 // still be fed by floating point instructions.
522 if (Op != TargetOpcode::COPY && !MI.isPHI() &&
524 return false;
525
526 // Check if we already know the register bank.
527 auto *RB = getRegBank(MI.getOperand(0).getReg(), MRI, TRI);
528 if (RB == &AArch64::FPRRegBank)
529 return true;
530 if (RB == &AArch64::GPRRegBank)
531 return false;
532
533 // We don't know anything.
534 //
535 // If we have a phi, we may be able to infer that it will be assigned a FPR
536 // based off of its inputs.
537 if (!MI.isPHI() || Depth > MaxFPRSearchDepth)
538 return false;
539
540 return any_of(MI.explicit_uses(), [&](const MachineOperand &Op) {
541 return Op.isReg() &&
542 onlyDefinesFP(*MRI.getVRegDef(Op.getReg()), MRI, TRI, Depth + 1);
543 });
544}
545
546bool AArch64RegisterBankInfo::onlyUsesFP(const MachineInstr &MI,
549 unsigned Depth) const {
550 switch (MI.getOpcode()) {
551 case TargetOpcode::G_FPTOSI:
552 case TargetOpcode::G_FPTOUI:
553 case TargetOpcode::G_FPTOSI_SAT:
554 case TargetOpcode::G_FPTOUI_SAT:
555 case TargetOpcode::G_FCMP:
556 case TargetOpcode::G_LROUND:
557 case TargetOpcode::G_LLROUND:
558 return true;
559 case TargetOpcode::G_INTRINSIC:
560 switch (cast<GIntrinsic>(MI).getIntrinsicID()) {
561 case Intrinsic::aarch64_neon_fcvtas:
562 case Intrinsic::aarch64_neon_fcvtau:
563 case Intrinsic::aarch64_neon_fcvtzs:
564 case Intrinsic::aarch64_neon_fcvtzu:
565 case Intrinsic::aarch64_neon_fcvtms:
566 case Intrinsic::aarch64_neon_fcvtmu:
567 case Intrinsic::aarch64_neon_fcvtns:
568 case Intrinsic::aarch64_neon_fcvtnu:
569 case Intrinsic::aarch64_neon_fcvtps:
570 case Intrinsic::aarch64_neon_fcvtpu:
571 // Force FPR register bank for half types, as those types otherwise
572 // don't get legalized correctly resulting in fp16 <-> gpr32 COPY's.
573 return MRI.getType(MI.getOperand(2).getReg()) == LLT::float16();
574 default:
575 break;
576 }
577 break;
578 default:
579 break;
580 }
581 return hasFPConstraints(MI, MRI, TRI, Depth);
582}
583
584bool AArch64RegisterBankInfo::onlyDefinesFP(const MachineInstr &MI,
587 unsigned Depth) const {
588 switch (MI.getOpcode()) {
589 case AArch64::G_DUP:
590 case TargetOpcode::G_SITOFP:
591 case TargetOpcode::G_UITOFP:
592 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
593 case TargetOpcode::G_INSERT_VECTOR_ELT:
594 case TargetOpcode::G_BUILD_VECTOR:
595 case TargetOpcode::G_BUILD_VECTOR_TRUNC:
596 return true;
597 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
598 switch (cast<GIntrinsic>(MI).getIntrinsicID()) {
599 case Intrinsic::aarch64_neon_ld1x2:
600 case Intrinsic::aarch64_neon_ld1x3:
601 case Intrinsic::aarch64_neon_ld1x4:
602 case Intrinsic::aarch64_neon_ld2:
603 case Intrinsic::aarch64_neon_ld2lane:
604 case Intrinsic::aarch64_neon_ld2r:
605 case Intrinsic::aarch64_neon_ld3:
606 case Intrinsic::aarch64_neon_ld3lane:
607 case Intrinsic::aarch64_neon_ld3r:
608 case Intrinsic::aarch64_neon_ld4:
609 case Intrinsic::aarch64_neon_ld4lane:
610 case Intrinsic::aarch64_neon_ld4r:
611 return true;
612 default:
613 break;
614 }
615 break;
616 default:
617 break;
618 }
619 return hasFPConstraints(MI, MRI, TRI, Depth);
620}
621
622bool AArch64RegisterBankInfo::prefersFPUse(const MachineInstr &MI,
625 unsigned Depth) const {
626 switch (MI.getOpcode()) {
627 case TargetOpcode::G_SITOFP:
628 case TargetOpcode::G_UITOFP:
629 return MRI.getType(MI.getOperand(0).getReg()).getSizeInBits() ==
630 MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
631 }
632 return onlyDefinesFP(MI, MRI, TRI, Depth);
633}
634
635bool AArch64RegisterBankInfo::isLoadFromFPType(const MachineInstr &MI) const {
636 // GMemOperation because we also want to match indexed loads.
637 auto *MemOp = cast<GMemOperation>(&MI);
638 const Value *LdVal = MemOp->getMMO().getValue();
639 if (!LdVal)
640 return false;
641
642 Type *EltTy = nullptr;
643 if (const GlobalValue *GV = dyn_cast<GlobalValue>(LdVal)) {
644 EltTy = GV->getValueType();
645 // Look at the first element of the struct to determine the type we are
646 // loading
647 while (StructType *StructEltTy = dyn_cast<StructType>(EltTy)) {
648 if (StructEltTy->getNumElements() == 0)
649 break;
650 EltTy = StructEltTy->getTypeAtIndex(0U);
651 }
652 // Look at the first element of the array to determine its type
653 if (isa<ArrayType>(EltTy))
654 EltTy = EltTy->getArrayElementType();
655 } else if (!isa<Constant>(LdVal)) {
656 // FIXME: grubbing around uses is pretty ugly, but with no more
657 // `getPointerElementType` there's not much else we can do.
658 for (const auto *LdUser : LdVal->users()) {
659 if (isa<LoadInst>(LdUser)) {
660 EltTy = LdUser->getType();
661 break;
662 }
663 if (isa<StoreInst>(LdUser) && LdUser->getOperand(1) == LdVal) {
664 EltTy = LdUser->getOperand(0)->getType();
665 break;
666 }
667 }
668 }
669 return EltTy && EltTy->isFPOrFPVectorTy();
670}
671
674 const unsigned Opc = MI.getOpcode();
675
676 // Try the default logic for non-generic instructions that are either copies
677 // or already have some operands assigned to banks.
678 if ((Opc != TargetOpcode::COPY && !isPreISelGenericOpcode(Opc)) ||
679 Opc == TargetOpcode::G_PHI) {
682 if (Mapping.isValid())
683 return Mapping;
684 }
685
686 const MachineFunction &MF = *MI.getParent()->getParent();
687 const MachineRegisterInfo &MRI = MF.getRegInfo();
690
691 switch (Opc) {
692 // G_{F|S|U}REM are not listed because they are not legal.
693 // Arithmetic ops.
694 case TargetOpcode::G_ADD:
695 case TargetOpcode::G_SUB:
696 case TargetOpcode::G_PTR_ADD:
697 case TargetOpcode::G_MUL:
698 case TargetOpcode::G_SDIV:
699 case TargetOpcode::G_UDIV:
700 // Bitwise ops.
701 case TargetOpcode::G_AND:
702 case TargetOpcode::G_OR:
703 case TargetOpcode::G_XOR:
704 // Floating point ops.
705 case TargetOpcode::G_FADD:
706 case TargetOpcode::G_FSUB:
707 case TargetOpcode::G_FMUL:
708 case TargetOpcode::G_FDIV:
709 case TargetOpcode::G_FMAXIMUM:
710 case TargetOpcode::G_FMINIMUM:
711 return getSameKindOfOperandsMapping(MI);
712 case TargetOpcode::G_FPEXT: {
713 LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
714 LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
716 DefaultMappingID, /*Cost*/ 1,
718 /*NumOperands*/ 2);
719 }
720 // Shifts.
721 case TargetOpcode::G_SHL:
722 case TargetOpcode::G_LSHR:
723 case TargetOpcode::G_ASHR: {
724 LLT ShiftAmtTy = MRI.getType(MI.getOperand(2).getReg());
725 LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
726 if (ShiftAmtTy.getSizeInBits() == 64 && SrcTy.getSizeInBits() == 32)
729 return getSameKindOfOperandsMapping(MI);
730 }
731 case TargetOpcode::COPY: {
732 Register DstReg = MI.getOperand(0).getReg();
733 Register SrcReg = MI.getOperand(1).getReg();
734 // Check if one of the register is not a generic register.
735 if ((DstReg.isPhysical() || !MRI.getType(DstReg).isValid()) ||
736 (SrcReg.isPhysical() || !MRI.getType(SrcReg).isValid())) {
737 const RegisterBank *DstRB = getRegBank(DstReg, MRI, TRI);
738 const RegisterBank *SrcRB = getRegBank(SrcReg, MRI, TRI);
739 if (!DstRB)
740 DstRB = SrcRB;
741 else if (!SrcRB)
742 SrcRB = DstRB;
743 // If both RB are null that means both registers are generic.
744 // We shouldn't be here.
745 assert(DstRB && SrcRB && "Both RegBank were nullptr");
746 TypeSize Size = getSizeInBits(DstReg, MRI, TRI);
748 DefaultMappingID, copyCost(*DstRB, *SrcRB, Size),
749 getCopyMapping(DstRB->getID(), SrcRB->getID(), Size),
750 // We only care about the mapping of the destination.
751 /*NumOperands*/ 1);
752 }
753 // Both registers are generic, use G_BITCAST.
754 [[fallthrough]];
755 }
756 case TargetOpcode::G_BITCAST: {
757 LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
758 LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
759 TypeSize Size = DstTy.getSizeInBits();
760 bool DstIsGPR = !DstTy.isVector() && DstTy.getSizeInBits() <= 64;
761 bool SrcIsGPR = !SrcTy.isVector() && SrcTy.getSizeInBits() <= 64;
762 const RegisterBank &DstRB =
763 DstIsGPR ? AArch64::GPRRegBank : AArch64::FPRRegBank;
764 const RegisterBank &SrcRB =
765 SrcIsGPR ? AArch64::GPRRegBank : AArch64::FPRRegBank;
767 DefaultMappingID, copyCost(DstRB, SrcRB, Size),
768 getCopyMapping(DstRB.getID(), SrcRB.getID(), Size),
769 // We only care about the mapping of the destination for COPY.
770 /*NumOperands*/ Opc == TargetOpcode::G_BITCAST ? 2 : 1);
771 }
772 default:
773 break;
774 }
775
776 unsigned NumOperands = MI.getNumOperands();
777 unsigned MappingID = DefaultMappingID;
778
779 // Track the size and bank of each register. We don't do partial mappings.
780 SmallVector<unsigned, 4> OpSize(NumOperands);
781 SmallVector<PartialMappingIdx, 4> OpRegBankIdx(NumOperands);
782 for (unsigned Idx = 0; Idx < NumOperands; ++Idx) {
783 auto &MO = MI.getOperand(Idx);
784 if (!MO.isReg() || !MO.getReg())
785 continue;
786
787 LLT Ty = MRI.getType(MO.getReg());
788 if (!Ty.isValid())
789 continue;
790 OpSize[Idx] = Ty.getSizeInBits().getKnownMinValue();
791
792 // As a top-level guess, vectors including both scalable and non-scalable
793 // ones go in FPRs, scalars and pointers in GPRs.
794 // For floating-point instructions, scalars go in FPRs.
795 if (Ty.isVector())
796 OpRegBankIdx[Idx] = PMI_FirstFPR;
798 Ty.getSizeInBits() > 64)
799 OpRegBankIdx[Idx] = PMI_FirstFPR;
800 else
801 OpRegBankIdx[Idx] = PMI_FirstGPR;
802 }
803
804 unsigned Cost = 1;
805 // Some of the floating-point instructions have mixed GPR and FPR operands:
806 // fine-tune the computed mapping.
807 switch (Opc) {
808 case AArch64::G_DUP: {
809 Register ScalarReg = MI.getOperand(1).getReg();
810 LLT ScalarTy = MRI.getType(ScalarReg);
811 auto ScalarDef = MRI.getVRegDef(ScalarReg);
812 // We want to select dup(load) into LD1R.
813 if (ScalarDef->getOpcode() == TargetOpcode::G_LOAD)
814 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
815 // s8 is an exception for G_DUP, which we always want on gpr.
816 else if (ScalarTy.getSizeInBits() != 8 &&
817 (getRegBank(ScalarReg, MRI, TRI) == &AArch64::FPRRegBank ||
818 onlyDefinesFP(*ScalarDef, MRI, TRI)))
819 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
820 else {
821 if (ScalarTy.getSizeInBits() < 32 &&
822 getRegBank(ScalarReg, MRI, TRI) == &AArch64::GPRRegBank) {
823 // Calls applyMappingImpl()
824 MappingID = CustomMappingID;
825 }
826 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstGPR};
827 }
828 break;
829 }
830 case TargetOpcode::G_TRUNC: {
831 LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
832 if (!SrcTy.isVector() && SrcTy.getSizeInBits() == 128)
833 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
834 break;
835 }
836 case TargetOpcode::G_SITOFP:
837 case TargetOpcode::G_UITOFP: {
838 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
839 break;
840 // Integer to FP conversions don't necessarily happen between GPR -> FPR
841 // regbanks. They can also be done within an FPR register.
842 Register SrcReg = MI.getOperand(1).getReg();
843 if (getRegBank(SrcReg, MRI, TRI) == &AArch64::FPRRegBank &&
844 MRI.getType(SrcReg).getSizeInBits() ==
845 MRI.getType(MI.getOperand(0).getReg()).getSizeInBits())
846 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
847 else
848 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstGPR};
849 break;
850 }
851 case TargetOpcode::G_FPTOSI_SAT:
852 case TargetOpcode::G_FPTOUI_SAT: {
853 LLT DstType = MRI.getType(MI.getOperand(0).getReg());
854 if (DstType.isVector())
855 break;
856 if (DstType == LLT::scalar(16)) {
857 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
858 break;
859 }
860 OpRegBankIdx = {PMI_FirstGPR, PMI_FirstFPR};
861 break;
862 }
863 case TargetOpcode::G_FPTOSI:
864 case TargetOpcode::G_FPTOUI:
865 case TargetOpcode::G_INTRINSIC_LRINT:
866 case TargetOpcode::G_INTRINSIC_LLRINT:
867 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
868 break;
869 OpRegBankIdx = {PMI_FirstGPR, PMI_FirstFPR};
870 break;
871 case TargetOpcode::G_FCMP: {
872 // If the result is a vector, it must use a FPR.
874 MRI.getType(MI.getOperand(0).getReg()).isVector() ? PMI_FirstFPR
875 : PMI_FirstGPR;
876 OpRegBankIdx = {Idx0,
877 /* Predicate */ PMI_None, PMI_FirstFPR, PMI_FirstFPR};
878 break;
879 }
880 case TargetOpcode::G_BITCAST:
881 // This is going to be a cross register bank copy and this is expensive.
882 if (OpRegBankIdx[0] != OpRegBankIdx[1])
883 Cost = copyCost(
884 *AArch64GenRegisterBankInfo::PartMappings[OpRegBankIdx[0]].RegBank,
885 *AArch64GenRegisterBankInfo::PartMappings[OpRegBankIdx[1]].RegBank,
886 TypeSize::getFixed(OpSize[0]));
887 break;
888 case TargetOpcode::G_LOAD: {
889 // Loading in vector unit is slightly more expensive.
890 // This is actually only true for the LD1R and co instructions,
891 // but anyway for the fast mode this number does not matter and
892 // for the greedy mode the cost of the cross bank copy will
893 // offset this number.
894 // FIXME: Should be derived from the scheduling model.
895 if (OpRegBankIdx[0] != PMI_FirstGPR) {
896 Cost = 2;
897 break;
898 }
899
900 if (cast<GLoad>(MI).isAtomic()) {
901 // Atomics always use GPR destinations. Don't refine any further.
902 OpRegBankIdx[0] = PMI_FirstGPR;
903 break;
904 }
905
906 // Try to guess the type of the load from the MMO.
907 if (isLoadFromFPType(MI)) {
908 OpRegBankIdx[0] = PMI_FirstFPR;
909 break;
910 }
911
912 // Check if that load feeds fp instructions.
913 // In that case, we want the default mapping to be on FPR
914 // instead of blind map every scalar to GPR.
915 if (any_of(MRI.use_nodbg_instructions(MI.getOperand(0).getReg()),
916 [&](const MachineInstr &UseMI) {
917 // If we have at least one direct or indirect use
918 // in a FP instruction,
919 // assume this was a floating point load in the IR. If it was
920 // not, we would have had a bitcast before reaching that
921 // instruction.
922 //
923 // Int->FP conversion operations are also captured in
924 // prefersFPUse().
925
926 if (isPHIWithFPConstraints(UseMI, MRI, TRI))
927 return true;
928
929 return onlyUsesFP(UseMI, MRI, TRI) ||
930 prefersFPUse(UseMI, MRI, TRI);
931 }))
932 OpRegBankIdx[0] = PMI_FirstFPR;
933 break;
934 }
935 case TargetOpcode::G_STORE:
936 // Check if that store is fed by fp instructions.
937 if (OpRegBankIdx[0] == PMI_FirstGPR) {
938 Register VReg = MI.getOperand(0).getReg();
939 if (!VReg)
940 break;
941 MachineInstr *DefMI = MRI.getVRegDef(VReg);
942 if (onlyDefinesFP(*DefMI, MRI, TRI))
943 OpRegBankIdx[0] = PMI_FirstFPR;
944 break;
945 }
946 break;
947 case TargetOpcode::G_INDEXED_STORE:
948 if (OpRegBankIdx[1] == PMI_FirstGPR) {
949 Register VReg = MI.getOperand(1).getReg();
950 if (!VReg)
951 break;
952 MachineInstr *DefMI = MRI.getVRegDef(VReg);
953 if (onlyDefinesFP(*DefMI, MRI, TRI))
954 OpRegBankIdx[1] = PMI_FirstFPR;
955 break;
956 }
957 break;
958 case TargetOpcode::G_INDEXED_SEXTLOAD:
959 case TargetOpcode::G_INDEXED_ZEXTLOAD:
960 // These should always be GPR.
961 OpRegBankIdx[0] = PMI_FirstGPR;
962 break;
963 case TargetOpcode::G_INDEXED_LOAD: {
964 if (isLoadFromFPType(MI))
965 OpRegBankIdx[0] = PMI_FirstFPR;
966 break;
967 }
968 case TargetOpcode::G_SELECT: {
969 // If the destination is FPR, preserve that.
970 if (OpRegBankIdx[0] != PMI_FirstGPR)
971 break;
972
973 // If we're taking in vectors, we have no choice but to put everything on
974 // FPRs, except for the condition. The condition must always be on a GPR.
975 LLT SrcTy = MRI.getType(MI.getOperand(2).getReg());
976 if (SrcTy.isVector()) {
978 break;
979 }
980
981 // Try to minimize the number of copies. If we have more floating point
982 // constrained values than not, then we'll put everything on FPR. Otherwise,
983 // everything has to be on GPR.
984 unsigned NumFP = 0;
985
986 // Check if the uses of the result always produce floating point values.
987 //
988 // For example:
989 //
990 // %z = G_SELECT %cond %x %y
991 // fpr = G_FOO %z ...
992 if (any_of(MRI.use_nodbg_instructions(MI.getOperand(0).getReg()),
993 [&](MachineInstr &MI) { return onlyUsesFP(MI, MRI, TRI); }))
994 ++NumFP;
995
996 // Check if the defs of the source values always produce floating point
997 // values.
998 //
999 // For example:
1000 //
1001 // %x = G_SOMETHING_ALWAYS_FLOAT %a ...
1002 // %z = G_SELECT %cond %x %y
1003 //
1004 // Also check whether or not the sources have already been decided to be
1005 // FPR. Keep track of this.
1006 //
1007 // This doesn't check the condition, since it's just whatever is in NZCV.
1008 // This isn't passed explicitly in a register to fcsel/csel.
1009 for (unsigned Idx = 2; Idx < 4; ++Idx) {
1010 Register VReg = MI.getOperand(Idx).getReg();
1011 MachineInstr *DefMI = MRI.getVRegDef(VReg);
1012 if (getRegBank(VReg, MRI, TRI) == &AArch64::FPRRegBank ||
1013 onlyDefinesFP(*DefMI, MRI, TRI))
1014 ++NumFP;
1015 }
1016
1017 // If we have more FP constraints than not, then move everything over to
1018 // FPR.
1019 if (NumFP >= 2)
1021
1022 break;
1023 }
1024 case TargetOpcode::G_UNMERGE_VALUES: {
1025 // If the first operand belongs to a FPR register bank, then make sure that
1026 // we preserve that.
1027 if (OpRegBankIdx[0] != PMI_FirstGPR)
1028 break;
1029
1030 LLT SrcTy = MRI.getType(MI.getOperand(MI.getNumOperands()-1).getReg());
1031 // UNMERGE into scalars from a vector should always use FPR.
1032 // Likewise if any of the uses are FP instructions.
1033 if (SrcTy.isVector() || SrcTy == LLT::scalar(128) ||
1034 any_of(MRI.use_nodbg_instructions(MI.getOperand(0).getReg()),
1035 [&](MachineInstr &MI) { return onlyUsesFP(MI, MRI, TRI); })) {
1036 // Set the register bank of every operand to FPR.
1037 for (unsigned Idx = 0, NumOperands = MI.getNumOperands();
1038 Idx < NumOperands; ++Idx)
1039 OpRegBankIdx[Idx] = PMI_FirstFPR;
1040 }
1041 break;
1042 }
1043 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
1044 // Destination and source need to be FPRs.
1045 OpRegBankIdx[0] = PMI_FirstFPR;
1046 OpRegBankIdx[1] = PMI_FirstFPR;
1047
1048 // Index needs to be a GPR.
1049 OpRegBankIdx[2] = PMI_FirstGPR;
1050 break;
1051 case TargetOpcode::G_INSERT_VECTOR_ELT:
1052 OpRegBankIdx[0] = PMI_FirstFPR;
1053 OpRegBankIdx[1] = PMI_FirstFPR;
1054
1055 // The element may be either a GPR or FPR. Preserve that behaviour.
1056 if (getRegBank(MI.getOperand(2).getReg(), MRI, TRI) == &AArch64::FPRRegBank)
1057 OpRegBankIdx[2] = PMI_FirstFPR;
1058 else {
1059 // If the type is i8/i16, and the regank will be GPR, then we change the
1060 // type to i32 in applyMappingImpl.
1061 LLT Ty = MRI.getType(MI.getOperand(2).getReg());
1062 if (Ty.getSizeInBits() == 8 || Ty.getSizeInBits() == 16) {
1063 // Calls applyMappingImpl()
1064 MappingID = CustomMappingID;
1065 }
1066 OpRegBankIdx[2] = PMI_FirstGPR;
1067 }
1068
1069 // Index needs to be a GPR.
1070 OpRegBankIdx[3] = PMI_FirstGPR;
1071 break;
1072 case TargetOpcode::G_EXTRACT: {
1073 // For s128 sources we have to use fpr unless we know otherwise.
1074 auto Src = MI.getOperand(1).getReg();
1075 LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
1076 if (SrcTy.getSizeInBits() != 128)
1077 break;
1078 auto Idx = MRI.getRegClassOrNull(Src) == &AArch64::XSeqPairsClassRegClass
1079 ? PMI_FirstGPR
1080 : PMI_FirstFPR;
1081 OpRegBankIdx[0] = Idx;
1082 OpRegBankIdx[1] = Idx;
1083 break;
1084 }
1085 case TargetOpcode::G_BUILD_VECTOR: {
1086 // If the first source operand belongs to a FPR register bank, then make
1087 // sure that we preserve that.
1088 if (OpRegBankIdx[1] != PMI_FirstGPR)
1089 break;
1090 Register VReg = MI.getOperand(1).getReg();
1091 if (!VReg)
1092 break;
1093
1094 // Get the instruction that defined the source operand reg, and check if
1095 // it's a floating point operation. Or, if it's a type like s16 which
1096 // doesn't have a exact size gpr register class. The exception is if the
1097 // build_vector has all constant operands, which may be better to leave as
1098 // gpr without copies, so it can be matched in imported patterns.
1099 MachineInstr *DefMI = MRI.getVRegDef(VReg);
1100 unsigned DefOpc = DefMI->getOpcode();
1101 const LLT SrcTy = MRI.getType(VReg);
1102 if (all_of(MI.operands(), [&](const MachineOperand &Op) {
1103 return Op.isDef() || MRI.getVRegDef(Op.getReg())->getOpcode() ==
1104 TargetOpcode::G_CONSTANT;
1105 }))
1106 break;
1108 SrcTy.getSizeInBits() < 32 ||
1109 getRegBank(VReg, MRI, TRI) == &AArch64::FPRRegBank) {
1110 // Have a floating point op.
1111 // Make sure every operand gets mapped to a FPR register class.
1112 unsigned NumOperands = MI.getNumOperands();
1113 for (unsigned Idx = 0; Idx < NumOperands; ++Idx)
1114 OpRegBankIdx[Idx] = PMI_FirstFPR;
1115 }
1116 break;
1117 }
1118 case TargetOpcode::G_VECREDUCE_FADD:
1119 case TargetOpcode::G_VECREDUCE_FMUL:
1120 case TargetOpcode::G_VECREDUCE_FMAX:
1121 case TargetOpcode::G_VECREDUCE_FMIN:
1122 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1123 case TargetOpcode::G_VECREDUCE_FMINIMUM:
1124 case TargetOpcode::G_VECREDUCE_ADD:
1125 case TargetOpcode::G_VECREDUCE_MUL:
1126 case TargetOpcode::G_VECREDUCE_AND:
1127 case TargetOpcode::G_VECREDUCE_OR:
1128 case TargetOpcode::G_VECREDUCE_XOR:
1129 case TargetOpcode::G_VECREDUCE_SMAX:
1130 case TargetOpcode::G_VECREDUCE_SMIN:
1131 case TargetOpcode::G_VECREDUCE_UMAX:
1132 case TargetOpcode::G_VECREDUCE_UMIN:
1133 // Reductions produce a scalar value from a vector, the scalar should be on
1134 // FPR bank.
1135 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
1136 break;
1137 case TargetOpcode::G_VECREDUCE_SEQ_FADD:
1138 case TargetOpcode::G_VECREDUCE_SEQ_FMUL:
1139 // These reductions also take a scalar accumulator input.
1140 // Assign them FPR for now.
1141 OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR, PMI_FirstFPR};
1142 break;
1143 case TargetOpcode::G_INTRINSIC:
1144 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: {
1145 switch (cast<GIntrinsic>(MI).getIntrinsicID()) {
1146 case Intrinsic::aarch64_neon_vcvtfxs2fp:
1147 case Intrinsic::aarch64_neon_vcvtfxu2fp:
1148 case Intrinsic::aarch64_neon_vcvtfp2fxs:
1149 case Intrinsic::aarch64_neon_vcvtfp2fxu:
1150 // Override these intrinsics, because they would have a partial
1151 // mapping. This is needed for 'half' types, which otherwise don't
1152 // get legalised correctly.
1153 OpRegBankIdx[0] = PMI_FirstFPR;
1154 OpRegBankIdx[2] = PMI_FirstFPR;
1155 // OpRegBankIdx[1] is the intrinsic ID.
1156 // OpRegBankIdx[3] is an integer immediate.
1157 break;
1158 default: {
1159 // Check if we know that the intrinsic has any constraints on its register
1160 // banks. If it does, then update the mapping accordingly.
1161 unsigned Idx = 0;
1162 if (onlyDefinesFP(MI, MRI, TRI))
1163 for (const auto &Op : MI.defs()) {
1164 if (Op.isReg())
1165 OpRegBankIdx[Idx] = PMI_FirstFPR;
1166 ++Idx;
1167 }
1168 else
1169 Idx += MI.getNumExplicitDefs();
1170
1171 if (onlyUsesFP(MI, MRI, TRI))
1172 for (const auto &Op : MI.explicit_uses()) {
1173 if (Op.isReg())
1174 OpRegBankIdx[Idx] = PMI_FirstFPR;
1175 ++Idx;
1176 }
1177 break;
1178 }
1179 }
1180 break;
1181 }
1182 case TargetOpcode::G_LROUND:
1183 case TargetOpcode::G_LLROUND: {
1184 // Source is always floating point and destination is always integer.
1185 OpRegBankIdx = {PMI_FirstGPR, PMI_FirstFPR};
1186 break;
1187 }
1188 }
1189
1190 // Finally construct the computed mapping.
1191 SmallVector<const ValueMapping *, 8> OpdsMapping(NumOperands);
1192 for (unsigned Idx = 0; Idx < NumOperands; ++Idx) {
1193 if (MI.getOperand(Idx).isReg() && MI.getOperand(Idx).getReg()) {
1194 LLT Ty = MRI.getType(MI.getOperand(Idx).getReg());
1195 if (!Ty.isValid())
1196 continue;
1197 auto Mapping =
1198 getValueMapping(OpRegBankIdx[Idx], TypeSize::getFixed(OpSize[Idx]));
1199 if (!Mapping->isValid())
1201
1202 OpdsMapping[Idx] = Mapping;
1203 }
1204 }
1205
1206 return getInstructionMapping(MappingID, Cost, getOperandsMapping(OpdsMapping),
1207 NumOperands);
1208}
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
static unsigned getIntrinsicID(const SDNode *N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define CHECK_VALUEMAP(RBName, Size)
static bool isFPIntrinsic(const MachineRegisterInfo &MRI, const MachineInstr &MI)
#define CHECK_VALUEMAP_3OPS(RBName, Size)
static const unsigned CustomMappingID
#define CHECK_PARTIALMAP(Idx, ValStartIdx, ValLength, RB)
#define CHECK_VALUEMAP_CROSSREGCPY(RBNameDst, RBNameSrc, Size)
#define CHECK_VALUEMAP_FPEXT(DstSize, SrcSize)
This file declares the targeting of the RegisterBankInfo class for AArch64.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
uint64_t Size
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
IRTranslator LLVM IR MI
Implement a low-level type suitable for MachineInstr level instruction selection.
This file declares the MachineIRBuilder class.
Register const TargetRegisterInfo * TRI
ppc ctr loops verify
static const MCPhysReg FPR[]
FPR - The set of FP registers that should be allocated for arguments on Darwin and AIX.
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
static unsigned getRegBankBaseIdxOffset(unsigned RBIdx, TypeSize Size)
static const RegisterBankInfo::ValueMapping * getCopyMapping(unsigned DstBankID, unsigned SrcBankID, TypeSize Size)
Get the pointer to the ValueMapping of the operands of a copy instruction from the SrcBankID register...
static bool checkPartialMappingIdx(PartialMappingIdx FirstAlias, PartialMappingIdx LastAlias, ArrayRef< PartialMappingIdx > Order)
static const RegisterBankInfo::PartialMapping PartMappings[]
static const RegisterBankInfo::ValueMapping * getFPExtMapping(unsigned DstSize, unsigned SrcSize)
Get the instruction mapping for G_FPEXT.
static const RegisterBankInfo::ValueMapping * getValueMapping(PartialMappingIdx RBIdx, TypeSize Size)
Get the pointer to the ValueMapping representing the RegisterBank at RBIdx with a size of Size.
static const RegisterBankInfo::ValueMapping ValMappings[]
InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const override
Get the alternative mappings for MI.
unsigned copyCost(const RegisterBank &A, const RegisterBank &B, TypeSize Size) const override
Get the cost of a copy from B to A, or put differently, get the cost of A = COPY B.
const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC, LLT Ty) const override
Get a register bank that covers RC.
AArch64RegisterBankInfo(const TargetRegisterInfo &TRI)
const InstructionMapping & getInstrMapping(const MachineInstr &MI) const override
Get the mapping of the different operands of MI on the register bank.
const AArch64RegisterInfo * getRegisterInfo() const override
This class represents an Operation in the Expression.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Definition: LowLevelType.h:43
constexpr bool isValid() const
Definition: LowLevelType.h:146
constexpr bool isVector() const
Definition: LowLevelType.h:149
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
Definition: LowLevelType.h:191
constexpr LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
Definition: LowLevelType.h:278
constexpr ElementCount getElementCount() const
Definition: LowLevelType.h:184
static constexpr LLT float16()
Get a 16-bit IEEE half value.
Definition: LowLevelType.h:85
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Helper class to build MachineInstr.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ANYEXT Op0.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
Register getReg(unsigned Idx) const
Get the register for the operand index.
Representation of each machine instruction.
Definition: MachineInstr.h:72
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:587
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Helper class that represents how the value of an instruction may be mapped and what is the related co...
bool isValid() const
Check whether this object is valid.
virtual InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const
Get the alternative mappings for MI.
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
static void applyDefaultMapping(const OperandsMapper &OpdMapper)
Helper method to apply something that is like the default mapping.
const InstructionMapping & getInvalidInstructionMapping() const
Method to get a uniquely generated invalid InstructionMapping.
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
unsigned getMaximumSize(unsigned RegBankID) const
Get the maximum size in bits that fits in the given register bank.
TypeSize getSizeInBits(Register Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI) const
Get the size in bits of Reg.
virtual const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC, LLT Ty) const
Get a register bank that covers RC.
const ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End.
static const unsigned DefaultMappingID
Identifier used when the related instruction mapping instance is generated by target independent code...
virtual unsigned copyCost(const RegisterBank &A, const RegisterBank &B, TypeSize Size) const
Get the cost of a copy from B to A, or put differently, get the cost of A = COPY B.
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
This class implements the register bank concept.
Definition: RegisterBank.h:29
LLVM_ABI bool covers(const TargetRegisterClass &RC) const
Check whether this register bank covers RC.
unsigned getID() const
Get the identifier of this register bank.
Definition: RegisterBank.h:46
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Class to represent struct types.
Definition: DerivedTypes.h:218
unsigned getID() const
Return the register class ID number.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:346
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Type * getArrayElementType() const
Definition: Type.h:408
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:225
LLVM Value Representation.
Definition: Value.h:75
iterator_range< user_iterator > users()
Definition: Value.h:426
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:203
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:169
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1744
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel.
Definition: TargetOpcodes.h:30
bool isPreISelGenericOptimizationHint(unsigned Opcode)
Definition: TargetOpcodes.h:42
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
void call_once(once_flag &flag, Function &&F, Args &&... ArgList)
Execute the function specified as a parameter once.
Definition: Threading.h:86
LLVM_ABI bool isPreISelGenericFloatingPointOpcode(unsigned Opc)
Returns whether opcode Opc is a pre-isel generic floating-point opcode, having only floating-point op...
Definition: Utils.cpp:1742
The llvm::once_flag structure.
Definition: Threading.h:67