1 //===- llvm/CodeGen/GlobalISel/RegisterBankInfo.cpp --------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements the RegisterBankInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/TargetOpcodes.h"
23 #include "llvm/CodeGen/TargetRegisterInfo.h"
24 #include "llvm/CodeGen/TargetSubtargetInfo.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm> // For std::max.
32 #define DEBUG_TYPE "registerbankinfo"
36 STATISTIC(NumPartialMappingsCreated
,
37 "Number of partial mappings dynamically created");
38 STATISTIC(NumPartialMappingsAccessed
,
39 "Number of partial mappings dynamically accessed");
40 STATISTIC(NumValueMappingsCreated
,
41 "Number of value mappings dynamically created");
42 STATISTIC(NumValueMappingsAccessed
,
43 "Number of value mappings dynamically accessed");
44 STATISTIC(NumOperandsMappingsCreated
,
45 "Number of operands mappings dynamically created");
46 STATISTIC(NumOperandsMappingsAccessed
,
47 "Number of operands mappings dynamically accessed");
48 STATISTIC(NumInstructionMappingsCreated
,
49 "Number of instruction mappings dynamically created");
50 STATISTIC(NumInstructionMappingsAccessed
,
51 "Number of instruction mappings dynamically accessed");
53 const unsigned RegisterBankInfo::DefaultMappingID
= UINT_MAX
;
54 const unsigned RegisterBankInfo::InvalidMappingID
= UINT_MAX
- 1;
56 //------------------------------------------------------------------------------
57 // RegisterBankInfo implementation.
58 //------------------------------------------------------------------------------
59 RegisterBankInfo::RegisterBankInfo(RegisterBank
**RegBanks
,
61 : RegBanks(RegBanks
), NumRegBanks(NumRegBanks
) {
63 for (unsigned Idx
= 0, End
= getNumRegBanks(); Idx
!= End
; ++Idx
) {
64 assert(RegBanks
[Idx
] != nullptr && "Invalid RegisterBank");
65 assert(RegBanks
[Idx
]->isValid() && "RegisterBank should be valid");
70 bool RegisterBankInfo::verify(const TargetRegisterInfo
&TRI
) const {
72 for (unsigned Idx
= 0, End
= getNumRegBanks(); Idx
!= End
; ++Idx
) {
73 const RegisterBank
&RegBank
= getRegBank(Idx
);
74 assert(Idx
== RegBank
.getID() &&
75 "ID does not match the index in the array");
76 LLVM_DEBUG(dbgs() << "Verify " << RegBank
<< '\n');
77 assert(RegBank
.verify(TRI
) && "RegBank is invalid");
84 RegisterBankInfo::getRegBank(unsigned Reg
, const MachineRegisterInfo
&MRI
,
85 const TargetRegisterInfo
&TRI
) const {
86 if (TargetRegisterInfo::isPhysicalRegister(Reg
))
87 return &getRegBankFromRegClass(getMinimalPhysRegClass(Reg
, TRI
));
89 assert(Reg
&& "NoRegister does not have a register bank");
90 const RegClassOrRegBank
&RegClassOrBank
= MRI
.getRegClassOrRegBank(Reg
);
91 if (auto *RB
= RegClassOrBank
.dyn_cast
<const RegisterBank
*>())
93 if (auto *RC
= RegClassOrBank
.dyn_cast
<const TargetRegisterClass
*>())
94 return &getRegBankFromRegClass(*RC
);
98 const TargetRegisterClass
&
99 RegisterBankInfo::getMinimalPhysRegClass(unsigned Reg
,
100 const TargetRegisterInfo
&TRI
) const {
101 assert(TargetRegisterInfo::isPhysicalRegister(Reg
) &&
102 "Reg must be a physreg");
103 const auto &RegRCIt
= PhysRegMinimalRCs
.find(Reg
);
104 if (RegRCIt
!= PhysRegMinimalRCs
.end())
105 return *RegRCIt
->second
;
106 const TargetRegisterClass
*PhysRC
= TRI
.getMinimalPhysRegClass(Reg
);
107 PhysRegMinimalRCs
[Reg
] = PhysRC
;
111 const RegisterBank
*RegisterBankInfo::getRegBankFromConstraints(
112 const MachineInstr
&MI
, unsigned OpIdx
, const TargetInstrInfo
&TII
,
113 const TargetRegisterInfo
&TRI
) const {
114 // The mapping of the registers may be available via the
115 // register class constraints.
116 const TargetRegisterClass
*RC
= MI
.getRegClassConstraint(OpIdx
, &TII
, &TRI
);
121 const RegisterBank
&RegBank
= getRegBankFromRegClass(*RC
);
122 // Sanity check that the target properly implemented getRegBankFromRegClass.
123 assert(RegBank
.covers(*RC
) &&
124 "The mapping of the register bank does not make sense");
128 const TargetRegisterClass
*RegisterBankInfo::constrainGenericRegister(
129 unsigned Reg
, const TargetRegisterClass
&RC
, MachineRegisterInfo
&MRI
) {
131 // If the register already has a class, fallback to MRI::constrainRegClass.
132 auto &RegClassOrBank
= MRI
.getRegClassOrRegBank(Reg
);
133 if (RegClassOrBank
.is
<const TargetRegisterClass
*>())
134 return MRI
.constrainRegClass(Reg
, &RC
);
136 const RegisterBank
*RB
= RegClassOrBank
.get
<const RegisterBank
*>();
137 // Otherwise, all we can do is ensure the bank covers the class, and set it.
138 if (RB
&& !RB
->covers(RC
))
141 // If nothing was set or the class is simply compatible, set it.
142 MRI
.setRegClass(Reg
, &RC
);
146 /// Check whether or not \p MI should be treated like a copy
147 /// for the mappings.
148 /// Copy like instruction are special for mapping because
149 /// they don't have actual register constraints. Moreover,
150 /// they sometimes have register classes assigned and we can
151 /// just use that instead of failing to provide a generic mapping.
152 static bool isCopyLike(const MachineInstr
&MI
) {
153 return MI
.isCopy() || MI
.isPHI() ||
154 MI
.getOpcode() == TargetOpcode::REG_SEQUENCE
;
157 const RegisterBankInfo::InstructionMapping
&
158 RegisterBankInfo::getInstrMappingImpl(const MachineInstr
&MI
) const {
159 // For copies we want to walk over the operands and try to find one
160 // that has a register bank since the instruction itself will not get
161 // us any constraint.
162 bool IsCopyLike
= isCopyLike(MI
);
163 // For copy like instruction, only the mapping of the definition
164 // is important. The rest is not constrained.
165 unsigned NumOperandsForMapping
= IsCopyLike
? 1 : MI
.getNumOperands();
167 const MachineFunction
&MF
= *MI
.getMF();
168 const TargetSubtargetInfo
&STI
= MF
.getSubtarget();
169 const TargetRegisterInfo
&TRI
= *STI
.getRegisterInfo();
170 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
171 // We may need to query the instruction encoding to guess the mapping.
172 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
174 // Before doing anything complicated check if the mapping is not
175 // directly available.
176 bool CompleteMapping
= true;
178 SmallVector
<const ValueMapping
*, 8> OperandsMapping(NumOperandsForMapping
);
179 for (unsigned OpIdx
= 0, EndIdx
= MI
.getNumOperands(); OpIdx
!= EndIdx
;
181 const MachineOperand
&MO
= MI
.getOperand(OpIdx
);
184 unsigned Reg
= MO
.getReg();
187 // The register bank of Reg is just a side effect of the current
188 // excution and in particular, there is no reason to believe this
189 // is the best default mapping for the current instruction. Keep
190 // it as an alternative register bank if we cannot figure out
192 const RegisterBank
*AltRegBank
= getRegBank(Reg
, MRI
, TRI
);
193 // For copy-like instruction, we want to reuse the register bank
194 // that is already set on Reg, if any, since those instructions do
195 // not have any constraints.
196 const RegisterBank
*CurRegBank
= IsCopyLike
? AltRegBank
: nullptr;
198 // If this is a target specific instruction, we can deduce
199 // the register bank from the encoding constraints.
200 CurRegBank
= getRegBankFromConstraints(MI
, OpIdx
, TII
, TRI
);
202 // All our attempts failed, give up.
203 CompleteMapping
= false;
206 // MI does not carry enough information to guess the mapping.
207 return getInvalidInstructionMapping();
211 const ValueMapping
*ValMapping
=
212 &getValueMapping(0, getSizeInBits(Reg
, MRI
, TRI
), *CurRegBank
);
214 OperandsMapping
[0] = ValMapping
;
215 CompleteMapping
= true;
218 OperandsMapping
[OpIdx
] = ValMapping
;
221 if (IsCopyLike
&& !CompleteMapping
)
222 // No way to deduce the type from what we have.
223 return getInvalidInstructionMapping();
225 assert(CompleteMapping
&& "Setting an uncomplete mapping");
226 return getInstructionMapping(
227 DefaultMappingID
, /*Cost*/ 1,
228 /*OperandsMapping*/ getOperandsMapping(OperandsMapping
),
229 NumOperandsForMapping
);
232 /// Hashing function for PartialMapping.
233 static hash_code
hashPartialMapping(unsigned StartIdx
, unsigned Length
,
234 const RegisterBank
*RegBank
) {
235 return hash_combine(StartIdx
, Length
, RegBank
? RegBank
->getID() : 0);
238 /// Overloaded version of hash_value for a PartialMapping.
240 llvm::hash_value(const RegisterBankInfo::PartialMapping
&PartMapping
) {
241 return hashPartialMapping(PartMapping
.StartIdx
, PartMapping
.Length
,
242 PartMapping
.RegBank
);
245 const RegisterBankInfo::PartialMapping
&
246 RegisterBankInfo::getPartialMapping(unsigned StartIdx
, unsigned Length
,
247 const RegisterBank
&RegBank
) const {
248 ++NumPartialMappingsAccessed
;
250 hash_code Hash
= hashPartialMapping(StartIdx
, Length
, &RegBank
);
251 const auto &It
= MapOfPartialMappings
.find(Hash
);
252 if (It
!= MapOfPartialMappings
.end())
255 ++NumPartialMappingsCreated
;
257 auto &PartMapping
= MapOfPartialMappings
[Hash
];
258 PartMapping
= llvm::make_unique
<PartialMapping
>(StartIdx
, Length
, RegBank
);
262 const RegisterBankInfo::ValueMapping
&
263 RegisterBankInfo::getValueMapping(unsigned StartIdx
, unsigned Length
,
264 const RegisterBank
&RegBank
) const {
265 return getValueMapping(&getPartialMapping(StartIdx
, Length
, RegBank
), 1);
269 hashValueMapping(const RegisterBankInfo::PartialMapping
*BreakDown
,
270 unsigned NumBreakDowns
) {
271 if (LLVM_LIKELY(NumBreakDowns
== 1))
272 return hash_value(*BreakDown
);
273 SmallVector
<size_t, 8> Hashes(NumBreakDowns
);
274 for (unsigned Idx
= 0; Idx
!= NumBreakDowns
; ++Idx
)
275 Hashes
.push_back(hash_value(BreakDown
[Idx
]));
276 return hash_combine_range(Hashes
.begin(), Hashes
.end());
279 const RegisterBankInfo::ValueMapping
&
280 RegisterBankInfo::getValueMapping(const PartialMapping
*BreakDown
,
281 unsigned NumBreakDowns
) const {
282 ++NumValueMappingsAccessed
;
284 hash_code Hash
= hashValueMapping(BreakDown
, NumBreakDowns
);
285 const auto &It
= MapOfValueMappings
.find(Hash
);
286 if (It
!= MapOfValueMappings
.end())
289 ++NumValueMappingsCreated
;
291 auto &ValMapping
= MapOfValueMappings
[Hash
];
292 ValMapping
= llvm::make_unique
<ValueMapping
>(BreakDown
, NumBreakDowns
);
296 template <typename Iterator
>
297 const RegisterBankInfo::ValueMapping
*
298 RegisterBankInfo::getOperandsMapping(Iterator Begin
, Iterator End
) const {
300 ++NumOperandsMappingsAccessed
;
302 // The addresses of the value mapping are unique.
303 // Therefore, we can use them directly to hash the operand mapping.
304 hash_code Hash
= hash_combine_range(Begin
, End
);
305 auto &Res
= MapOfOperandsMappings
[Hash
];
309 ++NumOperandsMappingsCreated
;
311 // Create the array of ValueMapping.
312 // Note: this array will not hash to this instance of operands
313 // mapping, because we use the pointer of the ValueMapping
314 // to hash and we expect them to uniquely identify an instance
316 Res
= llvm::make_unique
<ValueMapping
[]>(std::distance(Begin
, End
));
318 for (Iterator It
= Begin
; It
!= End
; ++It
, ++Idx
) {
319 const ValueMapping
*ValMap
= *It
;
327 const RegisterBankInfo::ValueMapping
*RegisterBankInfo::getOperandsMapping(
328 const SmallVectorImpl
<const RegisterBankInfo::ValueMapping
*> &OpdsMapping
)
330 return getOperandsMapping(OpdsMapping
.begin(), OpdsMapping
.end());
333 const RegisterBankInfo::ValueMapping
*RegisterBankInfo::getOperandsMapping(
334 std::initializer_list
<const RegisterBankInfo::ValueMapping
*> OpdsMapping
)
336 return getOperandsMapping(OpdsMapping
.begin(), OpdsMapping
.end());
340 hashInstructionMapping(unsigned ID
, unsigned Cost
,
341 const RegisterBankInfo::ValueMapping
*OperandsMapping
,
342 unsigned NumOperands
) {
343 return hash_combine(ID
, Cost
, OperandsMapping
, NumOperands
);
346 const RegisterBankInfo::InstructionMapping
&
347 RegisterBankInfo::getInstructionMappingImpl(
348 bool IsInvalid
, unsigned ID
, unsigned Cost
,
349 const RegisterBankInfo::ValueMapping
*OperandsMapping
,
350 unsigned NumOperands
) const {
351 assert(((IsInvalid
&& ID
== InvalidMappingID
&& Cost
== 0 &&
352 OperandsMapping
== nullptr && NumOperands
== 0) ||
354 "Mismatch argument for invalid input");
355 ++NumInstructionMappingsAccessed
;
358 hashInstructionMapping(ID
, Cost
, OperandsMapping
, NumOperands
);
359 const auto &It
= MapOfInstructionMappings
.find(Hash
);
360 if (It
!= MapOfInstructionMappings
.end())
363 ++NumInstructionMappingsCreated
;
365 auto &InstrMapping
= MapOfInstructionMappings
[Hash
];
367 InstrMapping
= llvm::make_unique
<InstructionMapping
>();
369 InstrMapping
= llvm::make_unique
<InstructionMapping
>(
370 ID
, Cost
, OperandsMapping
, NumOperands
);
371 return *InstrMapping
;
374 const RegisterBankInfo::InstructionMapping
&
375 RegisterBankInfo::getInstrMapping(const MachineInstr
&MI
) const {
376 const RegisterBankInfo::InstructionMapping
&Mapping
= getInstrMappingImpl(MI
);
377 if (Mapping
.isValid())
379 llvm_unreachable("The target must implement this");
382 RegisterBankInfo::InstructionMappings
383 RegisterBankInfo::getInstrPossibleMappings(const MachineInstr
&MI
) const {
384 InstructionMappings PossibleMappings
;
385 // Put the default mapping first.
386 PossibleMappings
.push_back(&getInstrMapping(MI
));
387 // Then the alternative mapping, if any.
388 InstructionMappings AltMappings
= getInstrAlternativeMappings(MI
);
389 for (const InstructionMapping
*AltMapping
: AltMappings
)
390 PossibleMappings
.push_back(AltMapping
);
392 for (const InstructionMapping
*Mapping
: PossibleMappings
)
393 assert(Mapping
->verify(MI
) && "Mapping is invalid");
395 return PossibleMappings
;
398 RegisterBankInfo::InstructionMappings
399 RegisterBankInfo::getInstrAlternativeMappings(const MachineInstr
&MI
) const {
400 // No alternative for MI.
401 return InstructionMappings();
404 void RegisterBankInfo::applyDefaultMapping(const OperandsMapper
&OpdMapper
) {
405 MachineInstr
&MI
= OpdMapper
.getMI();
406 MachineRegisterInfo
&MRI
= OpdMapper
.getMRI();
407 LLVM_DEBUG(dbgs() << "Applying default-like mapping\n");
408 for (unsigned OpIdx
= 0,
409 EndIdx
= OpdMapper
.getInstrMapping().getNumOperands();
410 OpIdx
!= EndIdx
; ++OpIdx
) {
411 LLVM_DEBUG(dbgs() << "OpIdx " << OpIdx
);
412 MachineOperand
&MO
= MI
.getOperand(OpIdx
);
414 LLVM_DEBUG(dbgs() << " is not a register, nothing to be done\n");
418 LLVM_DEBUG(dbgs() << " is %%noreg, nothing to be done\n");
421 assert(OpdMapper
.getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
!=
424 assert(OpdMapper
.getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
==
426 "This mapping is too complex for this function");
427 iterator_range
<SmallVectorImpl
<unsigned>::const_iterator
> NewRegs
=
428 OpdMapper
.getVRegs(OpIdx
);
429 if (NewRegs
.begin() == NewRegs
.end()) {
430 LLVM_DEBUG(dbgs() << " has not been repaired, nothing to be done\n");
433 unsigned OrigReg
= MO
.getReg();
434 unsigned NewReg
= *NewRegs
.begin();
435 LLVM_DEBUG(dbgs() << " changed, replace " << printReg(OrigReg
, nullptr));
437 LLVM_DEBUG(dbgs() << " with " << printReg(NewReg
, nullptr));
439 // The OperandsMapper creates plain scalar, we may have to fix that.
440 // Check if the types match and if not, fix that.
441 LLT OrigTy
= MRI
.getType(OrigReg
);
442 LLT NewTy
= MRI
.getType(NewReg
);
443 if (OrigTy
!= NewTy
) {
444 // The default mapping is not supposed to change the size of
445 // the storage. However, right now we don't necessarily bump all
446 // the types to storage size. For instance, we can consider
447 // s16 G_AND legal whereas the storage size is going to be 32.
448 assert(OrigTy
.getSizeInBits() <= NewTy
.getSizeInBits() &&
449 "Types with difference size cannot be handled by the default "
451 LLVM_DEBUG(dbgs() << "\nChange type of new opd from " << NewTy
<< " to "
453 MRI
.setType(NewReg
, OrigTy
);
455 LLVM_DEBUG(dbgs() << '\n');
459 unsigned RegisterBankInfo::getSizeInBits(unsigned Reg
,
460 const MachineRegisterInfo
&MRI
,
461 const TargetRegisterInfo
&TRI
) const {
462 if (TargetRegisterInfo::isPhysicalRegister(Reg
)) {
463 // The size is not directly available for physical registers.
464 // Instead, we need to access a register class that contains Reg and
465 // get the size of that register class.
466 // Because this is expensive, we'll cache the register class by calling
467 auto *RC
= &getMinimalPhysRegClass(Reg
, TRI
);
468 assert(RC
&& "Expecting Register class");
469 return TRI
.getRegSizeInBits(*RC
);
471 return TRI
.getRegSizeInBits(Reg
, MRI
);
474 //------------------------------------------------------------------------------
475 // Helper classes implementation.
476 //------------------------------------------------------------------------------
477 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
478 LLVM_DUMP_METHOD
void RegisterBankInfo::PartialMapping::dump() const {
484 bool RegisterBankInfo::PartialMapping::verify() const {
485 assert(RegBank
&& "Register bank not set");
486 assert(Length
&& "Empty mapping");
487 assert((StartIdx
<= getHighBitIdx()) && "Overflow, switch to APInt?");
488 // Check if the minimum width fits into RegBank.
489 assert(RegBank
->getSize() >= Length
&& "Register bank too small for Mask");
493 void RegisterBankInfo::PartialMapping::print(raw_ostream
&OS
) const {
494 OS
<< "[" << StartIdx
<< ", " << getHighBitIdx() << "], RegBank = ";
501 bool RegisterBankInfo::ValueMapping::verify(unsigned MeaningfulBitWidth
) const {
502 assert(NumBreakDowns
&& "Value mapped nowhere?!");
503 unsigned OrigValueBitWidth
= 0;
504 for (const RegisterBankInfo::PartialMapping
&PartMap
: *this) {
505 // Check that each register bank is big enough to hold the partial value:
506 // this check is done by PartialMapping::verify
507 assert(PartMap
.verify() && "Partial mapping is invalid");
508 // The original value should completely be mapped.
509 // Thus the maximum accessed index + 1 is the size of the original value.
511 std::max(OrigValueBitWidth
, PartMap
.getHighBitIdx() + 1);
513 assert(OrigValueBitWidth
>= MeaningfulBitWidth
&&
514 "Meaningful bits not covered by the mapping");
515 APInt
ValueMask(OrigValueBitWidth
, 0);
516 for (const RegisterBankInfo::PartialMapping
&PartMap
: *this) {
517 // Check that the union of the partial mappings covers the whole value,
519 // The high bit is exclusive in the APInt API, thus getHighBitIdx + 1.
520 APInt PartMapMask
= APInt::getBitsSet(OrigValueBitWidth
, PartMap
.StartIdx
,
521 PartMap
.getHighBitIdx() + 1);
522 ValueMask
^= PartMapMask
;
523 assert((ValueMask
& PartMapMask
) == PartMapMask
&&
524 "Some partial mappings overlap");
526 assert(ValueMask
.isAllOnesValue() && "Value is not fully mapped");
530 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
531 LLVM_DUMP_METHOD
void RegisterBankInfo::ValueMapping::dump() const {
537 void RegisterBankInfo::ValueMapping::print(raw_ostream
&OS
) const {
538 OS
<< "#BreakDown: " << NumBreakDowns
<< " ";
540 for (const PartialMapping
&PartMap
: *this) {
543 OS
<< '[' << PartMap
<< ']';
548 bool RegisterBankInfo::InstructionMapping::verify(
549 const MachineInstr
&MI
) const {
550 // Check that all the register operands are properly mapped.
551 // Check the constructor invariant.
552 // For PHI, we only care about mapping the definition.
553 assert(NumOperands
== (isCopyLike(MI
) ? 1 : MI
.getNumOperands()) &&
554 "NumOperands must match, see constructor");
555 assert(MI
.getParent() && MI
.getMF() &&
556 "MI must be connected to a MachineFunction");
557 const MachineFunction
&MF
= *MI
.getMF();
558 const RegisterBankInfo
*RBI
= MF
.getSubtarget().getRegBankInfo();
561 for (unsigned Idx
= 0; Idx
< NumOperands
; ++Idx
) {
562 const MachineOperand
&MO
= MI
.getOperand(Idx
);
564 assert(!getOperandMapping(Idx
).isValid() &&
565 "We should not care about non-reg mapping");
568 unsigned Reg
= MO
.getReg();
571 assert(getOperandMapping(Idx
).isValid() &&
572 "We must have a mapping for reg operands");
573 const RegisterBankInfo::ValueMapping
&MOMapping
= getOperandMapping(Idx
);
575 // Register size in bits.
576 // This size must match what the mapping expects.
577 assert(MOMapping
.verify(RBI
->getSizeInBits(
578 Reg
, MF
.getRegInfo(), *MF
.getSubtarget().getRegisterInfo())) &&
579 "Value mapping is invalid");
584 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
585 LLVM_DUMP_METHOD
void RegisterBankInfo::InstructionMapping::dump() const {
591 void RegisterBankInfo::InstructionMapping::print(raw_ostream
&OS
) const {
592 OS
<< "ID: " << getID() << " Cost: " << getCost() << " Mapping: ";
594 for (unsigned OpIdx
= 0; OpIdx
!= NumOperands
; ++OpIdx
) {
595 const ValueMapping
&ValMapping
= getOperandMapping(OpIdx
);
598 OS
<< "{ Idx: " << OpIdx
<< " Map: " << ValMapping
<< '}';
602 const int RegisterBankInfo::OperandsMapper::DontKnowIdx
= -1;
604 RegisterBankInfo::OperandsMapper::OperandsMapper(
605 MachineInstr
&MI
, const InstructionMapping
&InstrMapping
,
606 MachineRegisterInfo
&MRI
)
607 : MRI(MRI
), MI(MI
), InstrMapping(InstrMapping
) {
608 unsigned NumOpds
= InstrMapping
.getNumOperands();
609 OpToNewVRegIdx
.resize(NumOpds
, OperandsMapper::DontKnowIdx
);
610 assert(InstrMapping
.verify(MI
) && "Invalid mapping for MI");
613 iterator_range
<SmallVectorImpl
<unsigned>::iterator
>
614 RegisterBankInfo::OperandsMapper::getVRegsMem(unsigned OpIdx
) {
615 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
616 unsigned NumPartialVal
=
617 getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
;
618 int StartIdx
= OpToNewVRegIdx
[OpIdx
];
620 if (StartIdx
== OperandsMapper::DontKnowIdx
) {
621 // This is the first time we try to access OpIdx.
622 // Create the cells that will hold all the partial values at the
623 // end of the list of NewVReg.
624 StartIdx
= NewVRegs
.size();
625 OpToNewVRegIdx
[OpIdx
] = StartIdx
;
626 for (unsigned i
= 0; i
< NumPartialVal
; ++i
)
627 NewVRegs
.push_back(0);
629 SmallVectorImpl
<unsigned>::iterator End
=
630 getNewVRegsEnd(StartIdx
, NumPartialVal
);
632 return make_range(&NewVRegs
[StartIdx
], End
);
635 SmallVectorImpl
<unsigned>::const_iterator
636 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx
,
637 unsigned NumVal
) const {
638 return const_cast<OperandsMapper
*>(this)->getNewVRegsEnd(StartIdx
, NumVal
);
640 SmallVectorImpl
<unsigned>::iterator
641 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx
,
643 assert((NewVRegs
.size() == StartIdx
+ NumVal
||
644 NewVRegs
.size() > StartIdx
+ NumVal
) &&
645 "NewVRegs too small to contain all the partial mapping");
646 return NewVRegs
.size() <= StartIdx
+ NumVal
? NewVRegs
.end()
647 : &NewVRegs
[StartIdx
+ NumVal
];
650 void RegisterBankInfo::OperandsMapper::createVRegs(unsigned OpIdx
) {
651 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
652 iterator_range
<SmallVectorImpl
<unsigned>::iterator
> NewVRegsForOpIdx
=
654 const ValueMapping
&ValMapping
= getInstrMapping().getOperandMapping(OpIdx
);
655 const PartialMapping
*PartMap
= ValMapping
.begin();
656 for (unsigned &NewVReg
: NewVRegsForOpIdx
) {
657 assert(PartMap
!= ValMapping
.end() && "Out-of-bound access");
658 assert(NewVReg
== 0 && "Register has already been created");
659 // The new registers are always bound to scalar with the right size.
660 // The actual type has to be set when the target does the mapping
661 // of the instruction.
662 // The rationale is that this generic code cannot guess how the
663 // target plans to split the input type.
664 NewVReg
= MRI
.createGenericVirtualRegister(LLT::scalar(PartMap
->Length
));
665 MRI
.setRegBank(NewVReg
, *PartMap
->RegBank
);
670 void RegisterBankInfo::OperandsMapper::setVRegs(unsigned OpIdx
,
671 unsigned PartialMapIdx
,
673 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
674 assert(getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
>
676 "Out-of-bound access for partial mapping");
677 // Make sure the memory is initialized for that operand.
678 (void)getVRegsMem(OpIdx
);
679 assert(NewVRegs
[OpToNewVRegIdx
[OpIdx
] + PartialMapIdx
] == 0 &&
680 "This value is already set");
681 NewVRegs
[OpToNewVRegIdx
[OpIdx
] + PartialMapIdx
] = NewVReg
;
684 iterator_range
<SmallVectorImpl
<unsigned>::const_iterator
>
685 RegisterBankInfo::OperandsMapper::getVRegs(unsigned OpIdx
,
686 bool ForDebug
) const {
688 assert(OpIdx
< getInstrMapping().getNumOperands() && "Out-of-bound access");
689 int StartIdx
= OpToNewVRegIdx
[OpIdx
];
691 if (StartIdx
== OperandsMapper::DontKnowIdx
)
692 return make_range(NewVRegs
.end(), NewVRegs
.end());
694 unsigned PartMapSize
=
695 getInstrMapping().getOperandMapping(OpIdx
).NumBreakDowns
;
696 SmallVectorImpl
<unsigned>::const_iterator End
=
697 getNewVRegsEnd(StartIdx
, PartMapSize
);
698 iterator_range
<SmallVectorImpl
<unsigned>::const_iterator
> Res
=
699 make_range(&NewVRegs
[StartIdx
], End
);
701 for (unsigned VReg
: Res
)
702 assert((VReg
|| ForDebug
) && "Some registers are uninitialized");
707 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
708 LLVM_DUMP_METHOD
void RegisterBankInfo::OperandsMapper::dump() const {
714 void RegisterBankInfo::OperandsMapper::print(raw_ostream
&OS
,
715 bool ForDebug
) const {
716 unsigned NumOpds
= getInstrMapping().getNumOperands();
718 OS
<< "Mapping for " << getMI() << "\nwith " << getInstrMapping() << '\n';
719 // Print out the internal state of the index table.
720 OS
<< "Populated indices (CellNumber, IndexInNewVRegs): ";
722 for (unsigned Idx
= 0; Idx
!= NumOpds
; ++Idx
) {
723 if (OpToNewVRegIdx
[Idx
] != DontKnowIdx
) {
726 OS
<< '(' << Idx
<< ", " << OpToNewVRegIdx
[Idx
] << ')';
732 OS
<< "Mapping ID: " << getInstrMapping().getID() << ' ';
734 OS
<< "Operand Mapping: ";
735 // If we have a function, we can pretty print the name of the registers.
736 // Otherwise we will print the raw numbers.
737 const TargetRegisterInfo
*TRI
=
738 getMI().getParent() && getMI().getMF()
739 ? getMI().getMF()->getSubtarget().getRegisterInfo()
742 for (unsigned Idx
= 0; Idx
!= NumOpds
; ++Idx
) {
743 if (OpToNewVRegIdx
[Idx
] == DontKnowIdx
)
748 OS
<< '(' << printReg(getMI().getOperand(Idx
).getReg(), TRI
) << ", [";
749 bool IsFirstNewVReg
= true;
750 for (unsigned VReg
: getVRegs(Idx
)) {
753 IsFirstNewVReg
= false;
754 OS
<< printReg(VReg
, TRI
);