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/IR/Type.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetInstrInfo.h"
26 #include "llvm/Target/TargetOpcodes.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
28 #include "llvm/Target/TargetSubtargetInfo.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");
49 const unsigned RegisterBankInfo::DefaultMappingID = UINT_MAX;
50 const unsigned RegisterBankInfo::InvalidMappingID = UINT_MAX - 1;
52 //------------------------------------------------------------------------------
53 // RegisterBankInfo implementation.
54 //------------------------------------------------------------------------------
55 RegisterBankInfo::RegisterBankInfo(RegisterBank **RegBanks,
57 : RegBanks(RegBanks), NumRegBanks(NumRegBanks) {
59 for (unsigned Idx = 0, End = getNumRegBanks(); Idx != End; ++Idx)
60 assert(RegBanks[Idx] != nullptr && "Invalid RegisterBank");
64 RegisterBankInfo::~RegisterBankInfo() {
65 for (auto It : MapOfPartialMappings)
67 for (auto It : MapOfValueMappings)
71 bool RegisterBankInfo::verify(const TargetRegisterInfo &TRI) const {
73 for (unsigned Idx = 0, End = getNumRegBanks(); Idx != End; ++Idx) {
74 const RegisterBank &RegBank = getRegBank(Idx);
75 assert(Idx == RegBank.getID() &&
76 "ID does not match the index in the array");
77 dbgs() << "Verify " << RegBank << '\n';
78 assert(RegBank.verify(TRI) && "RegBank is invalid");
84 void RegisterBankInfo::createRegisterBank(unsigned ID, const char *Name) {
85 DEBUG(dbgs() << "Create register bank: " << ID << " with name \"" << Name
87 RegisterBank &RegBank = getRegBank(ID);
88 assert(RegBank.getID() == RegisterBank::InvalidID &&
89 "A register bank should be created only once");
94 void RegisterBankInfo::addRegBankCoverage(unsigned ID, unsigned RCId,
95 const TargetRegisterInfo &TRI) {
96 RegisterBank &RB = getRegBank(ID);
97 unsigned NbOfRegClasses = TRI.getNumRegClasses();
99 DEBUG(dbgs() << "Add coverage for: " << RB << '\n');
101 // Check if RB is underconstruction.
103 RB.ContainedRegClasses.resize(NbOfRegClasses);
104 else if (RB.covers(*TRI.getRegClass(RCId)))
105 // If RB already covers this register class, there is nothing
109 BitVector &Covered = RB.ContainedRegClasses;
110 SmallVector<unsigned, 8> WorkList;
112 WorkList.push_back(RCId);
115 unsigned &MaxSize = RB.Size;
117 unsigned RCId = WorkList.pop_back_val();
119 const TargetRegisterClass &CurRC = *TRI.getRegClass(RCId);
121 DEBUG(dbgs() << "Examine: " << TRI.getRegClassName(&CurRC)
122 << "(Size*8: " << (CurRC.getSize() * 8) << ")\n");
124 // Remember the biggest size in bits.
125 MaxSize = std::max(MaxSize, CurRC.getSize() * 8);
127 // Walk through all sub register classes and push them into the worklist.
129 for (BitMaskClassIterator It(CurRC.getSubClassMask(), TRI); It.isValid();
131 unsigned SubRCId = It.getID();
132 if (!Covered.test(SubRCId)) {
134 DEBUG(dbgs() << " Enqueue sub-class: ");
135 DEBUG(dbgs() << TRI.getRegClassName(TRI.getRegClass(SubRCId)) << ", ");
136 WorkList.push_back(SubRCId);
137 // Remember that we saw the sub class.
138 Covered.set(SubRCId);
143 DEBUG(dbgs() << '\n');
145 // Push also all the register classes that can be accessed via a
146 // subreg index, i.e., its subreg-class (which is different than
149 // Note: It would probably be faster to go the other way around
150 // and have this method add only super classes, since this
151 // information is available in a more efficient way. However, it
152 // feels less natural for the client of this APIs plus we will
153 // TableGen the whole bitset at some point, so compile time for
154 // the initialization is not very important.
156 for (unsigned SubRCId = 0; SubRCId < NbOfRegClasses; ++SubRCId) {
157 if (Covered.test(SubRCId))
160 const TargetRegisterClass *SubRC = TRI.getRegClass(SubRCId);
161 for (SuperRegClassIterator SuperRCIt(SubRC, &TRI); SuperRCIt.isValid();
165 for (BitMaskClassIterator It(SuperRCIt.getMask(), TRI); It.isValid();
167 unsigned SuperRCId = It.getID();
168 if (SuperRCId == RCId) {
170 DEBUG(dbgs() << " Enqueue subreg-class: ");
171 DEBUG(dbgs() << TRI.getRegClassName(SubRC) << ", ");
172 WorkList.push_back(SubRCId);
173 // Remember that we saw the sub class.
174 Covered.set(SubRCId);
183 DEBUG(dbgs() << '\n');
184 } while (!WorkList.empty());
188 RegisterBankInfo::getRegBank(unsigned Reg, const MachineRegisterInfo &MRI,
189 const TargetRegisterInfo &TRI) const {
190 if (TargetRegisterInfo::isPhysicalRegister(Reg))
191 return &getRegBankFromRegClass(*TRI.getMinimalPhysRegClass(Reg));
193 assert(Reg && "NoRegister does not have a register bank");
194 const RegClassOrRegBank &RegClassOrBank = MRI.getRegClassOrRegBank(Reg);
195 if (auto *RB = RegClassOrBank.dyn_cast<const RegisterBank *>())
197 if (auto *RC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>())
198 return &getRegBankFromRegClass(*RC);
202 const RegisterBank *RegisterBankInfo::getRegBankFromConstraints(
203 const MachineInstr &MI, unsigned OpIdx, const TargetInstrInfo &TII,
204 const TargetRegisterInfo &TRI) const {
205 // The mapping of the registers may be available via the
206 // register class constraints.
207 const TargetRegisterClass *RC = MI.getRegClassConstraint(OpIdx, &TII, &TRI);
212 const RegisterBank &RegBank = getRegBankFromRegClass(*RC);
213 // Sanity check that the target properly implemented getRegBankFromRegClass.
214 assert(RegBank.covers(*RC) &&
215 "The mapping of the register bank does not make sense");
219 const TargetRegisterClass *RegisterBankInfo::constrainGenericRegister(
220 unsigned Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI) {
222 // If the register already has a class, fallback to MRI::constrainRegClass.
223 auto &RegClassOrBank = MRI.getRegClassOrRegBank(Reg);
224 if (RegClassOrBank.is<const TargetRegisterClass *>())
225 return MRI.constrainRegClass(Reg, &RC);
227 const RegisterBank *RB = RegClassOrBank.get<const RegisterBank *>();
228 // Otherwise, all we can do is ensure the bank covers the class, and set it.
229 if (RB && !RB->covers(RC))
232 // If nothing was set or the class is simply compatible, set it.
233 MRI.setRegClass(Reg, &RC);
237 RegisterBankInfo::InstructionMapping
238 RegisterBankInfo::getInstrMappingImpl(const MachineInstr &MI) const {
239 // For copies we want to walk over the operands and try to find one
240 // that has a register bank since the instruction itself will not get
241 // us any constraint.
242 bool isCopyLike = MI.isCopy() || MI.isPHI();
243 // For copy like instruction, only the mapping of the definition
244 // is important. The rest is not constrained.
245 unsigned NumOperandsForMapping = isCopyLike ? 1 : MI.getNumOperands();
247 RegisterBankInfo::InstructionMapping Mapping(DefaultMappingID, /*Cost*/ 1,
248 /*OperandsMapping*/ nullptr,
249 NumOperandsForMapping);
250 const MachineFunction &MF = *MI.getParent()->getParent();
251 const TargetSubtargetInfo &STI = MF.getSubtarget();
252 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
253 const MachineRegisterInfo &MRI = MF.getRegInfo();
254 // We may need to query the instruction encoding to guess the mapping.
255 const TargetInstrInfo &TII = *STI.getInstrInfo();
257 // Before doing anything complicated check if the mapping is not
258 // directly available.
259 bool CompleteMapping = true;
261 SmallVector<const ValueMapping *, 8> OperandsMapping(NumOperandsForMapping);
262 for (unsigned OpIdx = 0, EndIdx = MI.getNumOperands(); OpIdx != EndIdx;
264 const MachineOperand &MO = MI.getOperand(OpIdx);
267 unsigned Reg = MO.getReg();
270 // The register bank of Reg is just a side effect of the current
271 // excution and in particular, there is no reason to believe this
272 // is the best default mapping for the current instruction. Keep
273 // it as an alternative register bank if we cannot figure out
275 const RegisterBank *AltRegBank = getRegBank(Reg, MRI, TRI);
276 // For copy-like instruction, we want to reuse the register bank
277 // that is already set on Reg, if any, since those instructions do
278 // not have any constraints.
279 const RegisterBank *CurRegBank = isCopyLike ? AltRegBank : nullptr;
281 // If this is a target specific instruction, we can deduce
282 // the register bank from the encoding constraints.
283 CurRegBank = getRegBankFromConstraints(MI, OpIdx, TII, TRI);
285 // All our attempts failed, give up.
286 CompleteMapping = false;
289 // MI does not carry enough information to guess the mapping.
290 return InstructionMapping();
294 const ValueMapping *ValMapping =
295 &getValueMapping(0, getSizeInBits(Reg, MRI, TRI), *CurRegBank);
297 OperandsMapping[0] = ValMapping;
298 CompleteMapping = true;
301 OperandsMapping[OpIdx] = ValMapping;
304 if (isCopyLike && !CompleteMapping)
305 // No way to deduce the type from what we have.
306 return InstructionMapping();
308 assert(CompleteMapping && "Setting an uncomplete mapping");
309 Mapping.setOperandsMapping(getOperandsMapping(OperandsMapping));
313 /// Hashing function for PartialMapping.
314 static hash_code hashPartialMapping(unsigned StartIdx, unsigned Length,
315 const RegisterBank *RegBank) {
316 return hash_combine(StartIdx, Length, RegBank ? RegBank->getID() : 0);
319 /// Overloaded version of hash_value for a PartialMapping.
321 llvm::hash_value(const RegisterBankInfo::PartialMapping &PartMapping) {
322 return hashPartialMapping(PartMapping.StartIdx, PartMapping.Length,
323 PartMapping.RegBank);
326 const RegisterBankInfo::PartialMapping &
327 RegisterBankInfo::getPartialMapping(unsigned StartIdx, unsigned Length,
328 const RegisterBank &RegBank) const {
329 ++NumPartialMappingsAccessed;
331 hash_code Hash = hashPartialMapping(StartIdx, Length, &RegBank);
332 const auto &It = MapOfPartialMappings.find(Hash);
333 if (It != MapOfPartialMappings.end())
336 ++NumPartialMappingsCreated;
338 const PartialMapping *&PartMapping = MapOfPartialMappings[Hash];
339 PartMapping = new PartialMapping{StartIdx, Length, RegBank};
343 const RegisterBankInfo::ValueMapping &
344 RegisterBankInfo::getValueMapping(unsigned StartIdx, unsigned Length,
345 const RegisterBank &RegBank) const {
346 return getValueMapping(&getPartialMapping(StartIdx, Length, RegBank), 1);
350 hashValueMapping(const RegisterBankInfo::PartialMapping *BreakDown,
351 unsigned NumBreakDowns) {
352 if (LLVM_LIKELY(NumBreakDowns == 1))
353 return hash_value(*BreakDown);
354 SmallVector<size_t, 8> Hashes(NumBreakDowns);
355 for (unsigned Idx = 0; Idx != NumBreakDowns; ++Idx)
356 Hashes.push_back(hash_value(BreakDown[Idx]));
357 return hash_combine_range(Hashes.begin(), Hashes.end());
360 const RegisterBankInfo::ValueMapping &
361 RegisterBankInfo::getValueMapping(const PartialMapping *BreakDown,
362 unsigned NumBreakDowns) const {
363 ++NumValueMappingsAccessed;
365 hash_code Hash = hashValueMapping(BreakDown, NumBreakDowns);
366 const auto &It = MapOfValueMappings.find(Hash);
367 if (It != MapOfValueMappings.end())
370 ++NumValueMappingsCreated;
372 const ValueMapping *&ValMapping = MapOfValueMappings[Hash];
373 ValMapping = new ValueMapping{BreakDown, NumBreakDowns};
377 template <typename Iterator>
378 const RegisterBankInfo::ValueMapping *
379 RegisterBankInfo::getOperandsMapping(Iterator Begin, Iterator End) const {
381 ++NumOperandsMappingsAccessed;
383 // The addresses of the value mapping are unique.
384 // Therefore, we can use them directly to hash the operand mapping.
385 hash_code Hash = hash_combine_range(Begin, End);
386 const auto &It = MapOfOperandsMappings.find(Hash);
387 if (It != MapOfOperandsMappings.end())
390 ++NumOperandsMappingsCreated;
392 // Create the array of ValueMapping.
393 // Note: this array will not hash to this instance of operands
394 // mapping, because we use the pointer of the ValueMapping
395 // to hash and we expect them to uniquely identify an instance
397 ValueMapping *&Res = MapOfOperandsMappings[Hash];
398 Res = new ValueMapping[std::distance(Begin, End)];
400 for (Iterator It = Begin; It != End; ++It, ++Idx) {
401 const ValueMapping *ValMap = *It;
409 const RegisterBankInfo::ValueMapping *RegisterBankInfo::getOperandsMapping(
410 const SmallVectorImpl<const RegisterBankInfo::ValueMapping *> &OpdsMapping)
412 return getOperandsMapping(OpdsMapping.begin(), OpdsMapping.end());
415 const RegisterBankInfo::ValueMapping *RegisterBankInfo::getOperandsMapping(
416 std::initializer_list<const RegisterBankInfo::ValueMapping *> OpdsMapping)
418 return getOperandsMapping(OpdsMapping.begin(), OpdsMapping.end());
421 RegisterBankInfo::InstructionMapping
422 RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
423 RegisterBankInfo::InstructionMapping Mapping = getInstrMappingImpl(MI);
424 if (Mapping.isValid())
426 llvm_unreachable("The target must implement this");
429 RegisterBankInfo::InstructionMappings
430 RegisterBankInfo::getInstrPossibleMappings(const MachineInstr &MI) const {
431 InstructionMappings PossibleMappings;
432 // Put the default mapping first.
433 PossibleMappings.push_back(getInstrMapping(MI));
434 // Then the alternative mapping, if any.
435 InstructionMappings AltMappings = getInstrAlternativeMappings(MI);
436 for (InstructionMapping &AltMapping : AltMappings)
437 PossibleMappings.emplace_back(std::move(AltMapping));
439 for (const InstructionMapping &Mapping : PossibleMappings)
440 assert(Mapping.verify(MI) && "Mapping is invalid");
442 return PossibleMappings;
445 RegisterBankInfo::InstructionMappings
446 RegisterBankInfo::getInstrAlternativeMappings(const MachineInstr &MI) const {
447 // No alternative for MI.
448 return InstructionMappings();
451 void RegisterBankInfo::applyDefaultMapping(const OperandsMapper &OpdMapper) {
452 MachineInstr &MI = OpdMapper.getMI();
453 DEBUG(dbgs() << "Applying default-like mapping\n");
454 for (unsigned OpIdx = 0,
455 EndIdx = OpdMapper.getInstrMapping().getNumOperands();
456 OpIdx != EndIdx; ++OpIdx) {
457 DEBUG(dbgs() << "OpIdx " << OpIdx);
458 MachineOperand &MO = MI.getOperand(OpIdx);
460 DEBUG(dbgs() << " is not a register, nothing to be done\n");
463 assert(OpdMapper.getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns ==
465 "This mapping is too complex for this function");
466 iterator_range<SmallVectorImpl<unsigned>::const_iterator> NewRegs =
467 OpdMapper.getVRegs(OpIdx);
468 if (NewRegs.begin() == NewRegs.end()) {
469 DEBUG(dbgs() << " has not been repaired, nothing to be done\n");
472 DEBUG(dbgs() << " changed, replace " << MO.getReg());
473 MO.setReg(*NewRegs.begin());
474 DEBUG(dbgs() << " with " << MO.getReg());
478 unsigned RegisterBankInfo::getSizeInBits(unsigned Reg,
479 const MachineRegisterInfo &MRI,
480 const TargetRegisterInfo &TRI) {
481 const TargetRegisterClass *RC = nullptr;
482 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
483 // The size is not directly available for physical registers.
484 // Instead, we need to access a register class that contains Reg and
485 // get the size of that register class.
486 RC = TRI.getMinimalPhysRegClass(Reg);
488 LLT Ty = MRI.getType(Reg);
489 unsigned RegSize = Ty.isValid() ? Ty.getSizeInBits() : 0;
490 // If Reg is not a generic register, query the register class to
494 // Since Reg is not a generic register, it must have a register class.
495 RC = MRI.getRegClass(Reg);
497 assert(RC && "Unable to deduce the register class");
498 return RC->getSize() * 8;
501 //------------------------------------------------------------------------------
502 // Helper classes implementation.
503 //------------------------------------------------------------------------------
504 LLVM_DUMP_METHOD void RegisterBankInfo::PartialMapping::dump() const {
509 bool RegisterBankInfo::PartialMapping::verify() const {
510 assert(RegBank && "Register bank not set");
511 assert(Length && "Empty mapping");
512 assert((StartIdx <= getHighBitIdx()) && "Overflow, switch to APInt?");
513 // Check if the minimum width fits into RegBank.
514 assert(RegBank->getSize() >= Length && "Register bank too small for Mask");
518 void RegisterBankInfo::PartialMapping::print(raw_ostream &OS) const {
519 OS << "[" << StartIdx << ", " << getHighBitIdx() << "], RegBank = ";
526 bool RegisterBankInfo::ValueMapping::verify(unsigned MeaningfulBitWidth) const {
527 assert(NumBreakDowns && "Value mapped nowhere?!");
528 unsigned OrigValueBitWidth = 0;
529 for (const RegisterBankInfo::PartialMapping &PartMap : *this) {
530 // Check that each register bank is big enough to hold the partial value:
531 // this check is done by PartialMapping::verify
532 assert(PartMap.verify() && "Partial mapping is invalid");
533 // The original value should completely be mapped.
534 // Thus the maximum accessed index + 1 is the size of the original value.
536 std::max(OrigValueBitWidth, PartMap.getHighBitIdx() + 1);
538 assert(OrigValueBitWidth >= MeaningfulBitWidth &&
539 "Meaningful bits not covered by the mapping");
540 APInt ValueMask(OrigValueBitWidth, 0);
541 for (const RegisterBankInfo::PartialMapping &PartMap : *this) {
542 // Check that the union of the partial mappings covers the whole value,
544 // The high bit is exclusive in the APInt API, thus getHighBitIdx + 1.
545 APInt PartMapMask = APInt::getBitsSet(OrigValueBitWidth, PartMap.StartIdx,
546 PartMap.getHighBitIdx() + 1);
547 ValueMask ^= PartMapMask;
548 assert((ValueMask & PartMapMask) == PartMapMask &&
549 "Some partial mappings overlap");
551 assert(ValueMask.isAllOnesValue() && "Value is not fully mapped");
555 LLVM_DUMP_METHOD void RegisterBankInfo::ValueMapping::dump() const {
560 void RegisterBankInfo::ValueMapping::print(raw_ostream &OS) const {
561 OS << "#BreakDown: " << NumBreakDowns << " ";
563 for (const PartialMapping &PartMap : *this) {
566 OS << '[' << PartMap << ']';
571 bool RegisterBankInfo::InstructionMapping::verify(
572 const MachineInstr &MI) const {
573 // Check that all the register operands are properly mapped.
574 // Check the constructor invariant.
575 // For PHI, we only care about mapping the definition.
576 assert(NumOperands ==
577 ((MI.isCopy() || MI.isPHI()) ? 1 : MI.getNumOperands()) &&
578 "NumOperands must match, see constructor");
579 assert(MI.getParent() && MI.getParent()->getParent() &&
580 "MI must be connected to a MachineFunction");
581 const MachineFunction &MF = *MI.getParent()->getParent();
584 for (unsigned Idx = 0; Idx < NumOperands; ++Idx) {
585 const MachineOperand &MO = MI.getOperand(Idx);
587 assert(!getOperandMapping(Idx).isValid() &&
588 "We should not care about non-reg mapping");
591 unsigned Reg = MO.getReg();
594 assert(getOperandMapping(Idx).isValid() &&
595 "We must have a mapping for reg operands");
596 const RegisterBankInfo::ValueMapping &MOMapping = getOperandMapping(Idx);
598 // Register size in bits.
599 // This size must match what the mapping expects.
600 assert(MOMapping.verify(getSizeInBits(
601 Reg, MF.getRegInfo(), *MF.getSubtarget().getRegisterInfo())) &&
602 "Value mapping is invalid");
607 LLVM_DUMP_METHOD void RegisterBankInfo::InstructionMapping::dump() const {
612 void RegisterBankInfo::InstructionMapping::print(raw_ostream &OS) const {
613 OS << "ID: " << getID() << " Cost: " << getCost() << " Mapping: ";
615 for (unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
616 const ValueMapping &ValMapping = getOperandMapping(OpIdx);
619 OS << "{ Idx: " << OpIdx << " Map: " << ValMapping << '}';
623 const int RegisterBankInfo::OperandsMapper::DontKnowIdx = -1;
625 RegisterBankInfo::OperandsMapper::OperandsMapper(
626 MachineInstr &MI, const InstructionMapping &InstrMapping,
627 MachineRegisterInfo &MRI)
628 : MRI(MRI), MI(MI), InstrMapping(InstrMapping) {
629 unsigned NumOpds = InstrMapping.getNumOperands();
630 OpToNewVRegIdx.resize(NumOpds, OperandsMapper::DontKnowIdx);
631 assert(InstrMapping.verify(MI) && "Invalid mapping for MI");
634 iterator_range<SmallVectorImpl<unsigned>::iterator>
635 RegisterBankInfo::OperandsMapper::getVRegsMem(unsigned OpIdx) {
636 assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
637 unsigned NumPartialVal =
638 getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns;
639 int StartIdx = OpToNewVRegIdx[OpIdx];
641 if (StartIdx == OperandsMapper::DontKnowIdx) {
642 // This is the first time we try to access OpIdx.
643 // Create the cells that will hold all the partial values at the
644 // end of the list of NewVReg.
645 StartIdx = NewVRegs.size();
646 OpToNewVRegIdx[OpIdx] = StartIdx;
647 for (unsigned i = 0; i < NumPartialVal; ++i)
648 NewVRegs.push_back(0);
650 SmallVectorImpl<unsigned>::iterator End =
651 getNewVRegsEnd(StartIdx, NumPartialVal);
653 return make_range(&NewVRegs[StartIdx], End);
656 SmallVectorImpl<unsigned>::const_iterator
657 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx,
658 unsigned NumVal) const {
659 return const_cast<OperandsMapper *>(this)->getNewVRegsEnd(StartIdx, NumVal);
661 SmallVectorImpl<unsigned>::iterator
662 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(unsigned StartIdx,
664 assert((NewVRegs.size() == StartIdx + NumVal ||
665 NewVRegs.size() > StartIdx + NumVal) &&
666 "NewVRegs too small to contain all the partial mapping");
667 return NewVRegs.size() <= StartIdx + NumVal ? NewVRegs.end()
668 : &NewVRegs[StartIdx + NumVal];
671 void RegisterBankInfo::OperandsMapper::createVRegs(unsigned OpIdx) {
672 assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
673 iterator_range<SmallVectorImpl<unsigned>::iterator> NewVRegsForOpIdx =
675 const ValueMapping &ValMapping = getInstrMapping().getOperandMapping(OpIdx);
676 const PartialMapping *PartMap = ValMapping.begin();
677 for (unsigned &NewVReg : NewVRegsForOpIdx) {
678 assert(PartMap != ValMapping.end() && "Out-of-bound access");
679 assert(NewVReg == 0 && "Register has already been created");
680 NewVReg = MRI.createGenericVirtualRegister(LLT::scalar(PartMap->Length));
681 MRI.setRegBank(NewVReg, *PartMap->RegBank);
686 void RegisterBankInfo::OperandsMapper::setVRegs(unsigned OpIdx,
687 unsigned PartialMapIdx,
689 assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
690 assert(getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns >
692 "Out-of-bound access for partial mapping");
693 // Make sure the memory is initialized for that operand.
694 (void)getVRegsMem(OpIdx);
695 assert(NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] == 0 &&
696 "This value is already set");
697 NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] = NewVReg;
700 iterator_range<SmallVectorImpl<unsigned>::const_iterator>
701 RegisterBankInfo::OperandsMapper::getVRegs(unsigned OpIdx,
702 bool ForDebug) const {
704 assert(OpIdx < getInstrMapping().getNumOperands() && "Out-of-bound access");
705 int StartIdx = OpToNewVRegIdx[OpIdx];
707 if (StartIdx == OperandsMapper::DontKnowIdx)
708 return make_range(NewVRegs.end(), NewVRegs.end());
710 unsigned PartMapSize =
711 getInstrMapping().getOperandMapping(OpIdx).NumBreakDowns;
712 SmallVectorImpl<unsigned>::const_iterator End =
713 getNewVRegsEnd(StartIdx, PartMapSize);
714 iterator_range<SmallVectorImpl<unsigned>::const_iterator> Res =
715 make_range(&NewVRegs[StartIdx], End);
717 for (unsigned VReg : Res)
718 assert((VReg || ForDebug) && "Some registers are uninitialized");
723 LLVM_DUMP_METHOD void RegisterBankInfo::OperandsMapper::dump() const {
728 void RegisterBankInfo::OperandsMapper::print(raw_ostream &OS,
729 bool ForDebug) const {
730 unsigned NumOpds = getInstrMapping().getNumOperands();
732 OS << "Mapping for " << getMI() << "\nwith " << getInstrMapping() << '\n';
733 // Print out the internal state of the index table.
734 OS << "Populated indices (CellNumber, IndexInNewVRegs): ";
736 for (unsigned Idx = 0; Idx != NumOpds; ++Idx) {
737 if (OpToNewVRegIdx[Idx] != DontKnowIdx) {
740 OS << '(' << Idx << ", " << OpToNewVRegIdx[Idx] << ')';
746 OS << "Mapping ID: " << getInstrMapping().getID() << ' ';
748 OS << "Operand Mapping: ";
749 // If we have a function, we can pretty print the name of the registers.
750 // Otherwise we will print the raw numbers.
751 const TargetRegisterInfo *TRI =
752 getMI().getParent() && getMI().getParent()->getParent()
753 ? getMI().getParent()->getParent()->getSubtarget().getRegisterInfo()
756 for (unsigned Idx = 0; Idx != NumOpds; ++Idx) {
757 if (OpToNewVRegIdx[Idx] == DontKnowIdx)
762 OS << '(' << PrintReg(getMI().getOperand(Idx).getReg(), TRI) << ", [";
763 bool IsFirstNewVReg = true;
764 for (unsigned VReg : getVRegs(Idx)) {
767 IsFirstNewVReg = false;
768 OS << PrintReg(VReg, TRI);