1 //===- llvm/CodeGen/GlobalISel/RegisterBankInfo.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 RegisterBankInfo class.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/ADT/iterator_range.h"
17 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/TargetOpcodes.h"
22 #include "llvm/CodeGen/TargetRegisterInfo.h"
23 #include "llvm/CodeGen/TargetSubtargetInfo.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm> // For std::max.
31 #define DEBUG_TYPE "registerbankinfo"
35 STATISTIC(NumPartialMappingsCreated
,
36 "Number of partial mappings dynamically created");
37 STATISTIC(NumPartialMappingsAccessed
,
38 "Number of partial mappings dynamically accessed");
39 STATISTIC(NumValueMappingsCreated
,
40 "Number of value mappings dynamically created");
41 STATISTIC(NumValueMappingsAccessed
,
42 "Number of value mappings dynamically accessed");
43 STATISTIC(NumOperandsMappingsCreated
,
44 "Number of operands mappings dynamically created");
45 STATISTIC(NumOperandsMappingsAccessed
,
46 "Number of operands mappings dynamically accessed");
47 STATISTIC(NumInstructionMappingsCreated
,
48 "Number of instruction mappings dynamically created");
49 STATISTIC(NumInstructionMappingsAccessed
,
50 "Number of instruction mappings dynamically accessed");
52 const unsigned RegisterBankInfo::DefaultMappingID
= UINT_MAX
;
53 const unsigned RegisterBankInfo::InvalidMappingID
= UINT_MAX
- 1;
55 //------------------------------------------------------------------------------
56 // RegisterBankInfo implementation.
57 //------------------------------------------------------------------------------
58 RegisterBankInfo::RegisterBankInfo(RegisterBank
**RegBanks
,
60 : RegBanks(RegBanks
), NumRegBanks(NumRegBanks
) {
62 for (unsigned Idx
= 0, End
= getNumRegBanks(); Idx
!= End
; ++Idx
) {
63 assert(RegBanks
[Idx
] != nullptr && "Invalid RegisterBank");
64 assert(RegBanks
[Idx
]->isValid() && "RegisterBank should be valid");
69 bool RegisterBankInfo::verify(const TargetRegisterInfo
&TRI
) const {
71 for (unsigned Idx
= 0, End
= getNumRegBanks(); Idx
!= End
; ++Idx
) {
72 const RegisterBank
&RegBank
= getRegBank(Idx
);
73 assert(Idx
== RegBank
.getID() &&
74 "ID does not match the index in the array");
75 LLVM_DEBUG(dbgs() << "Verify " << RegBank
<< '\n');
76 assert(RegBank
.verify(TRI
) && "RegBank is invalid");
83 RegisterBankInfo::getRegBank(unsigned Reg
, const MachineRegisterInfo
&MRI
,
84 const TargetRegisterInfo
&TRI
) const {
85 if (TargetRegisterInfo::isPhysicalRegister(Reg
))
86 return &getRegBankFromRegClass(getMinimalPhysRegClass(Reg
, TRI
));
88 assert(Reg
&& "NoRegister does not have a register bank");
89 const RegClassOrRegBank
&RegClassOrBank
= MRI
.getRegClassOrRegBank(Reg
);
90 if (auto *RB
= RegClassOrBank
.dyn_cast
<const RegisterBank
*>())
92 if (auto *RC
= RegClassOrBank
.dyn_cast
<const TargetRegisterClass
*>())
93 return &getRegBankFromRegClass(*RC
);
97 const TargetRegisterClass
&
98 RegisterBankInfo::getMinimalPhysRegClass(unsigned Reg
,
99 const TargetRegisterInfo
&TRI
) const {
100 assert(TargetRegisterInfo::isPhysicalRegister(Reg
) &&
101 "Reg must be a physreg");
102 const auto &RegRCIt
= PhysRegMinimalRCs
.find(Reg
);
103 if (RegRCIt
!= PhysRegMinimalRCs
.end())
104 return *RegRCIt
->second
;
105 const TargetRegisterClass
*PhysRC
= TRI
.getMinimalPhysRegClass(Reg
);
106 PhysRegMinimalRCs
[Reg
] = PhysRC
;
110 const RegisterBank
*RegisterBankInfo::getRegBankFromConstraints(
111 const MachineInstr
&MI
, unsigned OpIdx
, const TargetInstrInfo
&TII
,
112 const TargetRegisterInfo
&TRI
) const {
113 // The mapping of the registers may be available via the
114 // register class constraints.
115 const TargetRegisterClass
*RC
= MI
.getRegClassConstraint(OpIdx
, &TII
, &TRI
);
120 const RegisterBank
&RegBank
= getRegBankFromRegClass(*RC
);
121 // Sanity check that the target properly implemented getRegBankFromRegClass.
122 assert(RegBank
.covers(*RC
) &&
123 "The mapping of the register bank does not make sense");
127 const TargetRegisterClass
*RegisterBankInfo::constrainGenericRegister(
128 unsigned Reg
, const TargetRegisterClass
&RC
, MachineRegisterInfo
&MRI
) {
130 // If the register already has a class, fallback to MRI::constrainRegClass.
131 auto &RegClassOrBank
= MRI
.getRegClassOrRegBank(Reg
);
132 if (RegClassOrBank
.is
<const TargetRegisterClass
*>())
133 return MRI
.constrainRegClass(Reg
, &RC
);
135 const RegisterBank
*RB
= RegClassOrBank
.get
<const RegisterBank
*>();
136 // Otherwise, all we can do is ensure the bank covers the class, and set it.
137 if (RB
&& !RB
->covers(RC
))
140 // If nothing was set or the class is simply compatible, set it.
141 MRI
.setRegClass(Reg
, &RC
);
145 /// Check whether or not \p MI should be treated like a copy
146 /// for the mappings.
147 /// Copy like instruction are special for mapping because
148 /// they don't have actual register constraints. Moreover,
149 /// they sometimes have register classes assigned and we can
150 /// just use that instead of failing to provide a generic mapping.
151 static bool isCopyLike(const MachineInstr
&MI
) {
152 return MI
.isCopy() || MI
.isPHI() ||
153 MI
.getOpcode() == TargetOpcode::REG_SEQUENCE
;
156 const RegisterBankInfo::InstructionMapping
&
157 RegisterBankInfo::getInstrMappingImpl(const MachineInstr
&MI
) const {
158 // For copies we want to walk over the operands and try to find one
159 // that has a register bank since the instruction itself will not get
160 // us any constraint.
161 bool IsCopyLike
= isCopyLike(MI
);
162 // For copy like instruction, only the mapping of the definition
163 // is important. The rest is not constrained.
164 unsigned NumOperandsForMapping
= IsCopyLike
? 1 : MI
.getNumOperands();
166 const MachineFunction
&MF
= *MI
.getMF();
167 const TargetSubtargetInfo
&STI
= MF
.getSubtarget();
168 const TargetRegisterInfo
&TRI
= *STI
.getRegisterInfo();
169 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
170 // We may need to query the instruction encoding to guess the mapping.
171 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
173 // Before doing anything complicated check if the mapping is not
174 // directly available.
175 bool CompleteMapping
= true;
177 SmallVector
<const ValueMapping
*, 8> OperandsMapping(NumOperandsForMapping
);
178 for (unsigned OpIdx
= 0, EndIdx
= MI
.getNumOperands(); OpIdx
!= EndIdx
;
180 const MachineOperand
&MO
= MI
.getOperand(OpIdx
);
183 unsigned Reg
= MO
.getReg();
186 // The register bank of Reg is just a side effect of the current
187 // excution and in particular, there is no reason to believe this
188 // is the best default mapping for the current instruction. Keep
189 // it as an alternative register bank if we cannot figure out
191 const RegisterBank
*AltRegBank
= getRegBank(Reg
, MRI
, TRI
);
192 // For copy-like instruction, we want to reuse the register bank
193 // that is already set on Reg, if any, since those instructions do
194 // not have any constraints.
195 const RegisterBank
*CurRegBank
= IsCopyLike
? AltRegBank
: nullptr;
197 // If this is a target specific instruction, we can deduce
198 // the register bank from the encoding constraints.
199 CurRegBank
= getRegBankFromConstraints(MI
, OpIdx
, TII
, TRI
);
201 // All our attempts failed, give up.
202 CompleteMapping
= false;
205 // MI does not carry enough information to guess the mapping.
206 return getInvalidInstructionMapping();
210 const ValueMapping
*ValMapping
=
211 &getValueMapping(0, getSizeInBits(Reg
, MRI
, TRI
), *CurRegBank
);
213 OperandsMapping
[0] = ValMapping
;
214 CompleteMapping
= true;
217 OperandsMapping
[OpIdx
] = ValMapping
;
220 if (IsCopyLike
&& !CompleteMapping
)
221 // No way to deduce the type from what we have.
222 return getInvalidInstructionMapping();
224 assert(CompleteMapping
&& "Setting an uncomplete mapping");
225 return getInstructionMapping(
226 DefaultMappingID
, /*Cost*/ 1,
227 /*OperandsMapping*/ getOperandsMapping(OperandsMapping
),
228 NumOperandsForMapping
);
231 /// Hashing function for PartialMapping.
232 static hash_code
hashPartialMapping(unsigned StartIdx
, unsigned Length
,
233 const RegisterBank
*RegBank
) {
234 return hash_combine(StartIdx
, Length
, RegBank
? RegBank
->getID() : 0);
237 /// Overloaded version of hash_value for a PartialMapping.
239 llvm::hash_value(const RegisterBankInfo::PartialMapping
&PartMapping
) {
240 return hashPartialMapping(PartMapping
.StartIdx
, PartMapping
.Length
,
241 PartMapping
.RegBank
);
244 const RegisterBankInfo::PartialMapping
&
245 RegisterBankInfo::getPartialMapping(unsigned StartIdx
, unsigned Length
,
246 const RegisterBank
&RegBank
) const {
247 ++NumPartialMappingsAccessed
;
249 hash_code Hash
= hashPartialMapping(StartIdx
, Length
, &RegBank
);
250 const auto &It
= MapOfPartialMappings
.find(Hash
);
251 if (It
!= MapOfPartialMappings
.end())
254 ++NumPartialMappingsCreated
;
256 auto &PartMapping
= MapOfPartialMappings
[Hash
];
257 PartMapping
= llvm::make_unique
<PartialMapping
>(StartIdx
, Length
, RegBank
);
261 const RegisterBankInfo::ValueMapping
&
262 RegisterBankInfo::getValueMapping(unsigned StartIdx
, unsigned Length
,
263 const RegisterBank
&RegBank
) const {
264 return getValueMapping(&getPartialMapping(StartIdx
, Length
, RegBank
), 1);
268 hashValueMapping(const RegisterBankInfo::PartialMapping
*BreakDown
,
269 unsigned NumBreakDowns
) {
270 if (LLVM_LIKELY(NumBreakDowns
== 1))
271 return hash_value(*BreakDown
);
272 SmallVector
<size_t, 8> Hashes(NumBreakDowns
);
273 for (unsigned Idx
= 0; Idx
!= NumBreakDowns
; ++Idx
)
274 Hashes
.push_back(hash_value(BreakDown
[Idx
]));
275 return hash_combine_range(Hashes
.begin(), Hashes
.end());
278 const RegisterBankInfo::ValueMapping
&
279 RegisterBankInfo::getValueMapping(const PartialMapping
*BreakDown
,
280 unsigned NumBreakDowns
) const {
281 ++NumValueMappingsAccessed
;
283 hash_code Hash
= hashValueMapping(BreakDown
, NumBreakDowns
);
284 const auto &It
= MapOfValueMappings
.find(Hash
);
285 if (It
!= MapOfValueMappings
.end())
288 ++NumValueMappingsCreated
;
290 auto &ValMapping
= MapOfValueMappings
[Hash
];
291 ValMapping
= llvm::make_unique
<ValueMapping
>(BreakDown
, NumBreakDowns
);
295 template <typename Iterator
>
296 const RegisterBankInfo::ValueMapping
*
297 RegisterBankInfo::getOperandsMapping(Iterator Begin
, Iterator End
) const {
299 ++NumOperandsMappingsAccessed
;
301 // The addresses of the value mapping are unique.
302 // Therefore, we can use them directly to hash the operand mapping.
303 hash_code Hash
= hash_combine_range(Begin
, End
);
304 auto &Res
= MapOfOperandsMappings
[Hash
];
308 ++NumOperandsMappingsCreated
;
310 // Create the array of ValueMapping.
311 // Note: this array will not hash to this instance of operands
312 // mapping, because we use the pointer of the ValueMapping
313 // to hash and we expect them to uniquely identify an instance
315 Res
= llvm::make_unique
<ValueMapping
[]>(std::distance(Begin
, End
));
317 for (Iterator It
= Begin
; It
!= End
; ++It
, ++Idx
) {
318 const ValueMapping
*ValMap
= *It
;
326 const RegisterBankInfo::ValueMapping
*RegisterBankInfo::getOperandsMapping(
327 const SmallVectorImpl
<const RegisterBankInfo::ValueMapping
*> &OpdsMapping
)
329 return getOperandsMapping(OpdsMapping
.begin(), OpdsMapping
.end());
332 const RegisterBankInfo::ValueMapping
*RegisterBankInfo::getOperandsMapping(
333 std::initializer_list
<const RegisterBankInfo::ValueMapping
*> OpdsMapping
)
335 return getOperandsMapping(OpdsMapping
.begin(), OpdsMapping
.end());
339 hashInstructionMapping(unsigned ID
, unsigned Cost
,
340 const RegisterBankInfo::ValueMapping
*OperandsMapping
,
341 unsigned NumOperands
) {
342 return hash_combine(ID
, Cost
, OperandsMapping
, NumOperands
);
345 const RegisterBankInfo::InstructionMapping
&
346 RegisterBankInfo::getInstructionMappingImpl(
347 bool IsInvalid
, unsigned ID
, unsigned Cost
,
348 const RegisterBankInfo::ValueMapping
*OperandsMapping
,
349 unsigned NumOperands
) const {
350 assert(((IsInvalid
&& ID
== InvalidMappingID
&& Cost
== 0 &&
351 OperandsMapping
== nullptr && NumOperands
== 0) ||
353 "Mismatch argument for invalid input");
354 ++NumInstructionMappingsAccessed
;
357 hashInstructionMapping(ID
, Cost
, OperandsMapping
, NumOperands
);
358 const auto &It
= MapOfInstructionMappings
.find(Hash
);
359 if (It
!= MapOfInstructionMappings
.end())
362 ++NumInstructionMappingsCreated
;
364 auto &InstrMapping
= MapOfInstructionMappings
[Hash
];
366 InstrMapping
= llvm::make_unique
<InstructionMapping
>();
368 InstrMapping
= llvm::make_unique
<InstructionMapping
>(
369 ID
, Cost
, OperandsMapping
, NumOperands
);
370 return *InstrMapping
;
373 const RegisterBankInfo::InstructionMapping
&
374 RegisterBankInfo::getInstrMapping(const MachineInstr
&MI
) const {
375 const RegisterBankInfo::InstructionMapping
&Mapping
= getInstrMappingImpl(MI
);
376 if (Mapping
.isValid())
378 llvm_unreachable("The target must implement this");
381 RegisterBankInfo::InstructionMappings
382 RegisterBankInfo::getInstrPossibleMappings(const MachineInstr
&MI
) const {
383 InstructionMappings PossibleMappings
;
384 // Put the default mapping first.
385 PossibleMappings
.push_back(&getInstrMapping(MI
));
386 // Then the alternative mapping, if any.
387 InstructionMappings AltMappings
= getInstrAlternativeMappings(MI
);
388 for (const InstructionMapping
*AltMapping
: AltMappings
)
389 PossibleMappings
.push_back(AltMapping
);
391 for (const InstructionMapping
*Mapping
: PossibleMappings
)
392 assert(Mapping
->verify(MI
) && "Mapping is invalid");
394 return PossibleMappings
;
397 RegisterBankInfo::InstructionMappings
398 RegisterBankInfo::getInstrAlternativeMappings(const MachineInstr
&MI
) const {
399 // No alternative for MI.
400 return InstructionMappings();
403 void RegisterBankInfo::applyDefaultMapping(const OperandsMapper
&OpdMapper
) {
404 MachineInstr
&MI
= OpdMapper
.getMI();
405 MachineRegisterInfo
&MRI
= OpdMapper
.getMRI();
406 LLVM_DEBUG(dbgs() << "Applying default-like mapping\n");
407 for (unsigned OpIdx
= 0,
408 EndIdx
= OpdMapper
.getInstrMapping().getNumOperands();
409 OpIdx
!= EndIdx
; ++OpIdx
) {
410 LLVM_DEBUG(dbgs() << "OpIdx " << OpIdx
);
411 MachineOperand
&MO
= MI
.getOperand(OpIdx
);
413 LLVM_DEBUG(dbgs() << " is not a register, nothing to be done\n");
417 LLVM_DEBUG(dbgs() << " is %%noreg, nothing to be done\n");
420 assert(OpdMapper
.getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
!=
423 assert(OpdMapper
.getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
==
425 "This mapping is too complex for this function");
426 iterator_range
<SmallVectorImpl
<unsigned>::const_iterator
> NewRegs
=
427 OpdMapper
.getVRegs(OpIdx
);
428 if (empty(NewRegs
)) {
429 LLVM_DEBUG(dbgs() << " has not been repaired, nothing to be done\n");
432 unsigned OrigReg
= MO
.getReg();
433 unsigned NewReg
= *NewRegs
.begin();
434 LLVM_DEBUG(dbgs() << " changed, replace " << printReg(OrigReg
, nullptr));
436 LLVM_DEBUG(dbgs() << " with " << printReg(NewReg
, nullptr));
438 // The OperandsMapper creates plain scalar, we may have to fix that.
439 // Check if the types match and if not, fix that.
440 LLT OrigTy
= MRI
.getType(OrigReg
);
441 LLT NewTy
= MRI
.getType(NewReg
);
442 if (OrigTy
!= NewTy
) {
443 // The default mapping is not supposed to change the size of
444 // the storage. However, right now we don't necessarily bump all
445 // the types to storage size. For instance, we can consider
446 // s16 G_AND legal whereas the storage size is going to be 32.
447 assert(OrigTy
.getSizeInBits() <= NewTy
.getSizeInBits() &&
448 "Types with difference size cannot be handled by the default "
450 LLVM_DEBUG(dbgs() << "\nChange type of new opd from " << NewTy
<< " to "
452 MRI
.setType(NewReg
, OrigTy
);
454 LLVM_DEBUG(dbgs() << '\n');
458 unsigned RegisterBankInfo::getSizeInBits(unsigned Reg
,
459 const MachineRegisterInfo
&MRI
,
460 const TargetRegisterInfo
&TRI
) const {
461 if (TargetRegisterInfo::isPhysicalRegister(Reg
)) {
462 // The size is not directly available for physical registers.
463 // Instead, we need to access a register class that contains Reg and
464 // get the size of that register class.
465 // Because this is expensive, we'll cache the register class by calling
466 auto *RC
= &getMinimalPhysRegClass(Reg
, TRI
);
467 assert(RC
&& "Expecting Register class");
468 return TRI
.getRegSizeInBits(*RC
);
470 return TRI
.getRegSizeInBits(Reg
, MRI
);
473 //------------------------------------------------------------------------------
474 // Helper classes implementation.
475 //------------------------------------------------------------------------------
476 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
477 LLVM_DUMP_METHOD
void RegisterBankInfo::PartialMapping::dump() const {
483 bool RegisterBankInfo::PartialMapping::verify() const {
484 assert(RegBank
&& "Register bank not set");
485 assert(Length
&& "Empty mapping");
486 assert((StartIdx
<= getHighBitIdx()) && "Overflow, switch to APInt?");
487 // Check if the minimum width fits into RegBank.
488 assert(RegBank
->getSize() >= Length
&& "Register bank too small for Mask");
492 void RegisterBankInfo::PartialMapping::print(raw_ostream
&OS
) const {
493 OS
<< "[" << StartIdx
<< ", " << getHighBitIdx() << "], RegBank = ";
500 bool RegisterBankInfo::ValueMapping::partsAllUniform() const {
501 if (NumBreakDowns
< 2)
504 const PartialMapping
*First
= begin();
505 for (const PartialMapping
*Part
= First
+ 1; Part
!= end(); ++Part
) {
506 if (Part
->Length
!= First
->Length
|| Part
->RegBank
!= First
->RegBank
)
513 bool RegisterBankInfo::ValueMapping::verify(unsigned MeaningfulBitWidth
) const {
514 assert(NumBreakDowns
&& "Value mapped nowhere?!");
515 unsigned OrigValueBitWidth
= 0;
516 for (const RegisterBankInfo::PartialMapping
&PartMap
: *this) {
517 // Check that each register bank is big enough to hold the partial value:
518 // this check is done by PartialMapping::verify
519 assert(PartMap
.verify() && "Partial mapping is invalid");
520 // The original value should completely be mapped.
521 // Thus the maximum accessed index + 1 is the size of the original value.
523 std::max(OrigValueBitWidth
, PartMap
.getHighBitIdx() + 1);
525 assert(OrigValueBitWidth
>= MeaningfulBitWidth
&&
526 "Meaningful bits not covered by the mapping");
527 APInt
ValueMask(OrigValueBitWidth
, 0);
528 for (const RegisterBankInfo::PartialMapping
&PartMap
: *this) {
529 // Check that the union of the partial mappings covers the whole value,
531 // The high bit is exclusive in the APInt API, thus getHighBitIdx + 1.
532 APInt PartMapMask
= APInt::getBitsSet(OrigValueBitWidth
, PartMap
.StartIdx
,
533 PartMap
.getHighBitIdx() + 1);
534 ValueMask
^= PartMapMask
;
535 assert((ValueMask
& PartMapMask
) == PartMapMask
&&
536 "Some partial mappings overlap");
538 assert(ValueMask
.isAllOnesValue() && "Value is not fully mapped");
542 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
543 LLVM_DUMP_METHOD
void RegisterBankInfo::ValueMapping::dump() const {
549 void RegisterBankInfo::ValueMapping::print(raw_ostream
&OS
) const {
550 OS
<< "#BreakDown: " << NumBreakDowns
<< " ";
552 for (const PartialMapping
&PartMap
: *this) {
555 OS
<< '[' << PartMap
<< ']';
560 bool RegisterBankInfo::InstructionMapping::verify(
561 const MachineInstr
&MI
) const {
562 // Check that all the register operands are properly mapped.
563 // Check the constructor invariant.
564 // For PHI, we only care about mapping the definition.
565 assert(NumOperands
== (isCopyLike(MI
) ? 1 : MI
.getNumOperands()) &&
566 "NumOperands must match, see constructor");
567 assert(MI
.getParent() && MI
.getMF() &&
568 "MI must be connected to a MachineFunction");
569 const MachineFunction
&MF
= *MI
.getMF();
570 const RegisterBankInfo
*RBI
= MF
.getSubtarget().getRegBankInfo();
573 for (unsigned Idx
= 0; Idx
< NumOperands
; ++Idx
) {
574 const MachineOperand
&MO
= MI
.getOperand(Idx
);
576 assert(!getOperandMapping(Idx
).isValid() &&
577 "We should not care about non-reg mapping");
580 unsigned Reg
= MO
.getReg();
583 assert(getOperandMapping(Idx
).isValid() &&
584 "We must have a mapping for reg operands");
585 const RegisterBankInfo::ValueMapping
&MOMapping
= getOperandMapping(Idx
);
587 // Register size in bits.
588 // This size must match what the mapping expects.
589 assert(MOMapping
.verify(RBI
->getSizeInBits(
590 Reg
, MF
.getRegInfo(), *MF
.getSubtarget().getRegisterInfo())) &&
591 "Value mapping is invalid");
596 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
597 LLVM_DUMP_METHOD
void RegisterBankInfo::InstructionMapping::dump() const {
603 void RegisterBankInfo::InstructionMapping::print(raw_ostream
&OS
) const {
604 OS
<< "ID: " << getID() << " Cost: " << getCost() << " Mapping: ";
606 for (unsigned OpIdx
= 0; OpIdx
!= NumOperands
; ++OpIdx
) {
607 const ValueMapping
&ValMapping
= getOperandMapping(OpIdx
);
610 OS
<< "{ Idx: " << OpIdx
<< " Map: " << ValMapping
<< '}';
614 const int RegisterBankInfo::OperandsMapper::DontKnowIdx
= -1;
616 RegisterBankInfo::OperandsMapper::OperandsMapper(
617 MachineInstr
&MI
, const InstructionMapping
&InstrMapping
,
618 MachineRegisterInfo
&MRI
)
619 : MRI(MRI
), MI(MI
), InstrMapping(InstrMapping
) {
620 unsigned NumOpds
= InstrMapping
.getNumOperands();
621 OpToNewVRegIdx
.resize(NumOpds
, OperandsMapper::DontKnowIdx
);
622 assert(InstrMapping
.verify(MI
) && "Invalid mapping for MI");
625 iterator_range
<SmallVectorImpl
<unsigned>::iterator
>
626 RegisterBankInfo::OperandsMapper::getVRegsMem(unsigned OpIdx
) {
627 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
628 unsigned NumPartialVal
=
629 getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
;
630 int StartIdx
= OpToNewVRegIdx
[OpIdx
];
632 if (StartIdx
== OperandsMapper::DontKnowIdx
) {
633 // This is the first time we try to access OpIdx.
634 // Create the cells that will hold all the partial values at the
635 // end of the list of NewVReg.
636 StartIdx
= NewVRegs
.size();
637 OpToNewVRegIdx
[OpIdx
] = StartIdx
;
638 for (unsigned i
= 0; i
< NumPartialVal
; ++i
)
639 NewVRegs
.push_back(0);
641 SmallVectorImpl
<unsigned>::iterator End
=
642 getNewVRegsEnd(StartIdx
, NumPartialVal
);
644 return make_range(&NewVRegs
[StartIdx
], End
);
647 SmallVectorImpl
<unsigned>::const_iterator
648 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx
,
649 unsigned NumVal
) const {
650 return const_cast<OperandsMapper
*>(this)->getNewVRegsEnd(StartIdx
, NumVal
);
652 SmallVectorImpl
<unsigned>::iterator
653 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx
,
655 assert((NewVRegs
.size() == StartIdx
+ NumVal
||
656 NewVRegs
.size() > StartIdx
+ NumVal
) &&
657 "NewVRegs too small to contain all the partial mapping");
658 return NewVRegs
.size() <= StartIdx
+ NumVal
? NewVRegs
.end()
659 : &NewVRegs
[StartIdx
+ NumVal
];
662 void RegisterBankInfo::OperandsMapper::createVRegs(unsigned OpIdx
) {
663 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
664 iterator_range
<SmallVectorImpl
<unsigned>::iterator
> NewVRegsForOpIdx
=
666 const ValueMapping
&ValMapping
= getInstrMapping().getOperandMapping(OpIdx
);
667 const PartialMapping
*PartMap
= ValMapping
.begin();
668 for (unsigned &NewVReg
: NewVRegsForOpIdx
) {
669 assert(PartMap
!= ValMapping
.end() && "Out-of-bound access");
670 assert(NewVReg
== 0 && "Register has already been created");
671 // The new registers are always bound to scalar with the right size.
672 // The actual type has to be set when the target does the mapping
673 // of the instruction.
674 // The rationale is that this generic code cannot guess how the
675 // target plans to split the input type.
676 NewVReg
= MRI
.createGenericVirtualRegister(LLT::scalar(PartMap
->Length
));
677 MRI
.setRegBank(NewVReg
, *PartMap
->RegBank
);
682 void RegisterBankInfo::OperandsMapper::setVRegs(unsigned OpIdx
,
683 unsigned PartialMapIdx
,
685 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
686 assert(getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
>
688 "Out-of-bound access for partial mapping");
689 // Make sure the memory is initialized for that operand.
690 (void)getVRegsMem(OpIdx
);
691 assert(NewVRegs
[OpToNewVRegIdx
[OpIdx
] + PartialMapIdx
] == 0 &&
692 "This value is already set");
693 NewVRegs
[OpToNewVRegIdx
[OpIdx
] + PartialMapIdx
] = NewVReg
;
696 iterator_range
<SmallVectorImpl
<unsigned>::const_iterator
>
697 RegisterBankInfo::OperandsMapper::getVRegs(unsigned OpIdx
,
698 bool ForDebug
) const {
700 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
701 int StartIdx
= OpToNewVRegIdx
[OpIdx
];
703 if (StartIdx
== OperandsMapper::DontKnowIdx
)
704 return make_range(NewVRegs
.end(), NewVRegs
.end());
706 unsigned PartMapSize
=
707 getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
;
708 SmallVectorImpl
<unsigned>::const_iterator End
=
709 getNewVRegsEnd(StartIdx
, PartMapSize
);
710 iterator_range
<SmallVectorImpl
<unsigned>::const_iterator
> Res
=
711 make_range(&NewVRegs
[StartIdx
], End
);
713 for (unsigned VReg
: Res
)
714 assert((VReg
|| ForDebug
) && "Some registers are uninitialized");
719 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
720 LLVM_DUMP_METHOD
void RegisterBankInfo::OperandsMapper::dump() const {
726 void RegisterBankInfo::OperandsMapper::print(raw_ostream
&OS
,
727 bool ForDebug
) const {
728 unsigned NumOpds
= getInstrMapping().getNumOperands();
730 OS
<< "Mapping for " << getMI() << "\nwith " << getInstrMapping() << '\n';
731 // Print out the internal state of the index table.
732 OS
<< "Populated indices (CellNumber, IndexInNewVRegs): ";
734 for (unsigned Idx
= 0; Idx
!= NumOpds
; ++Idx
) {
735 if (OpToNewVRegIdx
[Idx
] != DontKnowIdx
) {
738 OS
<< '(' << Idx
<< ", " << OpToNewVRegIdx
[Idx
] << ')';
744 OS
<< "Mapping ID: " << getInstrMapping().getID() << ' ';
746 OS
<< "Operand Mapping: ";
747 // If we have a function, we can pretty print the name of the registers.
748 // Otherwise we will print the raw numbers.
749 const TargetRegisterInfo
*TRI
=
750 getMI().getParent() && getMI().getMF()
751 ? getMI().getMF()->getSubtarget().getRegisterInfo()
754 for (unsigned Idx
= 0; Idx
!= NumOpds
; ++Idx
) {
755 if (OpToNewVRegIdx
[Idx
] == DontKnowIdx
)
760 OS
<< '(' << printReg(getMI().getOperand(Idx
).getReg(), TRI
) << ", [";
761 bool IsFirstNewVReg
= true;
762 for (unsigned VReg
: getVRegs(Idx
)) {
765 IsFirstNewVReg
= false;
766 OS
<< printReg(VReg
, TRI
);