24 #include "llvm/Config/llvm-config.h" 31 #define DEBUG_TYPE "registerbankinfo" 36 "Number of partial mappings dynamically created");
38 "Number of partial mappings dynamically accessed");
40 "Number of value mappings dynamically created");
42 "Number of value mappings dynamically accessed");
44 "Number of operands mappings dynamically created");
46 "Number of operands mappings dynamically accessed");
48 "Number of instruction mappings dynamically created");
50 "Number of instruction mappings dynamically accessed");
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");
71 for (
unsigned Idx = 0, End =
getNumRegBanks(); Idx != End; ++Idx) {
74 "ID does not match the index in the array");
91 assert(Reg &&
"NoRegister does not have a register bank");
106 return *RegRCIt->second;
128 "The mapping of the register bank does not make sense");
142 if (RB && !RB->
covers(RC))
158 MI.
getOpcode() == TargetOpcode::REG_SEQUENCE;
169 unsigned NumOperandsForMapping = IsCopyLike ? 1 : MI.
getNumOperands();
180 bool CompleteMapping =
true;
183 for (
unsigned OpIdx = 0, EndIdx = MI.
getNumOperands(); OpIdx != EndIdx;
200 const RegisterBank *CurRegBank = IsCopyLike ? AltRegBank :
nullptr;
207 CompleteMapping =
false;
219 if (!OperandsMapping[0]) {
226 OperandsMapping[0] = ValMapping;
233 for (; OpIdx != EndIdx; ++OpIdx) {
247 CompleteMapping =
true;
251 OperandsMapping[OpIdx] = ValMapping;
254 if (IsCopyLike && !CompleteMapping) {
259 assert(CompleteMapping &&
"Setting an uncomplete mapping");
263 NumOperandsForMapping);
282 ++NumPartialMappingsAccessed;
289 ++NumPartialMappingsCreated;
292 PartMapping = std::make_unique<PartialMapping>(StartIdx, Length, RegBank);
304 unsigned NumBreakDowns) {
308 for (
unsigned Idx = 0; Idx != NumBreakDowns; ++Idx)
315 unsigned NumBreakDowns)
const {
316 ++NumValueMappingsAccessed;
323 ++NumValueMappingsCreated;
326 ValMapping = std::make_unique<ValueMapping>(BreakDown, NumBreakDowns);
330 template <
typename Iterator>
334 ++NumOperandsMappingsAccessed;
343 ++NumOperandsMappingsCreated;
350 Res = std::make_unique<ValueMapping[]>(std::distance(Begin, End));
352 for (Iterator It = Begin; It != End; ++It, ++Idx) {
368 std::initializer_list<const RegisterBankInfo::ValueMapping *> OpdsMapping)
376 unsigned NumOperands) {
377 return hash_combine(ID, Cost, OperandsMapping, NumOperands);
381 RegisterBankInfo::getInstructionMappingImpl(
382 bool IsInvalid,
unsigned ID,
unsigned Cost,
384 unsigned NumOperands)
const {
386 OperandsMapping ==
nullptr && NumOperands == 0) ||
388 "Mismatch argument for invalid input");
389 ++NumInstructionMappingsAccessed;
397 ++NumInstructionMappingsCreated;
400 InstrMapping = std::make_unique<InstructionMapping>(
401 ID, Cost, OperandsMapping, NumOperands);
402 return *InstrMapping;
414 RegisterBankInfo::getInstrPossibleMappings(
const MachineInstr &
MI)
const {
417 if (Mapping.isValid()) {
428 assert(Mapping->verify(MI) &&
"Mapping is invalid");
430 return PossibleMappings;
443 for (
unsigned OpIdx = 0,
445 OpIdx != EndIdx; ++OpIdx) {
461 "This mapping is too complex for this function");
464 if (NewRegs.empty()) {
465 LLVM_DEBUG(
dbgs() <<
" has not been repaired, nothing to be done\n");
478 if (OrigTy != NewTy) {
484 "Types with difference size cannot be handled by the default " 486 LLVM_DEBUG(
dbgs() <<
"\nChange type of new opd from " << NewTy <<
" to " 503 assert(RC &&
"Expecting Register class");
512 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 520 assert(RegBank &&
"Register bank not set");
521 assert(Length &&
"Empty mapping");
522 assert((StartIdx <= getHighBitIdx()) &&
"Overflow, switch to APInt?");
524 assert(RegBank->getSize() >= Length &&
"Register bank too small for Mask");
529 OS <<
"[" << StartIdx <<
", " << getHighBitIdx() <<
"], RegBank = ";
537 if (NumBreakDowns < 2)
542 if (Part->Length != First->
Length || Part->RegBank != First->
RegBank)
550 assert(NumBreakDowns &&
"Value mapped nowhere?!");
551 unsigned OrigValueBitWidth = 0;
555 assert(PartMap.
verify() &&
"Partial mapping is invalid");
561 assert(OrigValueBitWidth >= MeaningfulBitWidth &&
562 "Meaningful bits not covered by the mapping");
563 APInt ValueMask(OrigValueBitWidth, 0);
570 ValueMask ^= PartMapMask;
571 assert((ValueMask & PartMapMask) == PartMapMask &&
572 "Some partial mappings overlap");
578 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 586 OS <<
"#BreakDown: " << NumBreakDowns <<
" ";
591 OS <<
'[' << PartMap <<
']';
602 "NumOperands must match, see constructor");
604 "MI must be connected to a MachineFunction");
609 for (
unsigned Idx = 0; Idx < NumOperands; ++Idx) {
612 assert(!getOperandMapping(Idx).isValid() &&
613 "We should not care about non-reg mapping");
619 assert(getOperandMapping(Idx).isValid() &&
620 "We must have a mapping for reg operands");
626 Reg, MF.getRegInfo(), *MF.getSubtarget().getRegisterInfo())) &&
627 "Value mapping is invalid");
632 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 640 OS <<
"ID: " << getID() <<
" Cost: " << getCost() <<
" Mapping: ";
642 for (
unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
643 const ValueMapping &ValMapping = getOperandMapping(OpIdx);
646 OS <<
"{ Idx: " << OpIdx <<
" Map: " << ValMapping <<
'}';
650 const int RegisterBankInfo::OperandsMapper::DontKnowIdx = -1;
655 : MRI(MRI), MI(MI), InstrMapping(InstrMapping) {
657 OpToNewVRegIdx.
resize(NumOpds, OperandsMapper::DontKnowIdx);
658 assert(InstrMapping.
verify(MI) &&
"Invalid mapping for MI");
662 RegisterBankInfo::OperandsMapper::getVRegsMem(
unsigned OpIdx) {
664 unsigned NumPartialVal =
666 int StartIdx = OpToNewVRegIdx[OpIdx];
668 if (StartIdx == OperandsMapper::DontKnowIdx) {
672 StartIdx = NewVRegs.size();
673 OpToNewVRegIdx[OpIdx] = StartIdx;
674 for (
unsigned i = 0; i < NumPartialVal; ++i)
675 NewVRegs.push_back(0);
678 getNewVRegsEnd(StartIdx, NumPartialVal);
684 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(
unsigned StartIdx,
685 unsigned NumVal)
const {
686 return const_cast<OperandsMapper *
>(
this)->getNewVRegsEnd(StartIdx, NumVal);
689 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(
unsigned StartIdx,
691 assert((NewVRegs.size() == StartIdx + NumVal ||
692 NewVRegs.size() > StartIdx + NumVal) &&
693 "NewVRegs too small to contain all the partial mapping");
694 return NewVRegs.size() <= StartIdx + NumVal ? NewVRegs.end()
695 : &NewVRegs[StartIdx + NumVal];
704 for (
Register &NewVReg : NewVRegsForOpIdx) {
705 assert(PartMap != ValMapping.
end() &&
"Out-of-bound access");
706 assert(NewVReg == 0 &&
"Register has already been created");
719 unsigned PartialMapIdx,
724 "Out-of-bound access for partial mapping");
726 (void)getVRegsMem(OpIdx);
727 assert(NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] == 0 &&
728 "This value is already set");
729 NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] = NewVReg;
734 bool ForDebug)
const {
737 int StartIdx = OpToNewVRegIdx[OpIdx];
739 if (StartIdx == OperandsMapper::DontKnowIdx)
740 return make_range(NewVRegs.end(), NewVRegs.end());
742 unsigned PartMapSize =
745 getNewVRegsEnd(StartIdx, PartMapSize);
750 assert((VReg || ForDebug) &&
"Some registers are uninitialized");
755 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 763 bool ForDebug)
const {
768 OS <<
"Populated indices (CellNumber, IndexInNewVRegs): ";
770 for (
unsigned Idx = 0; Idx != NumOpds; ++Idx) {
771 if (OpToNewVRegIdx[Idx] != DontKnowIdx) {
774 OS <<
'(' << Idx <<
", " << OpToNewVRegIdx[Idx] <<
')';
782 OS <<
"Operand Mapping: ";
790 for (
unsigned Idx = 0; Idx != NumOpds; ++Idx) {
791 if (OpToNewVRegIdx[Idx] == DontKnowIdx)
797 bool IsFirstNewVReg =
true;
801 IsFirstNewVReg =
false;
unsigned NumRegBanks
Total number of register banks.
static const unsigned InvalidMappingID
Identifier used when the related instruction mapping instance is generated by the default constructor...
RegisterBank ** RegBanks
Hold the set of supported register banks.
const_iterator end(StringRef path)
Get end iterator over path.
const ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End...
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
typename SuperClass::const_iterator const_iterator
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
This class represents lattice values for constants.
void print(raw_ostream &OS) const
Print this on OS;.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
#define LLVM_LIKELY(EXPR)
void setVRegs(unsigned OpIdx, unsigned PartialMapIdx, Register NewVReg)
Set the virtual register of the PartialMapIdx-th partial mapping of the OpIdx-th operand to NewVReg...
DenseMap< unsigned, std::unique_ptr< const PartialMapping > > MapOfPartialMappings
Keep dynamically allocated PartialMapping in a separate map.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
unsigned getSizeInBits(Register Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI) const
Get the size in bits of Reg.
void dump() const
Print this on dbgs() stream.
Helper class that represents how the value of an instruction may be mapped and what is the related co...
void push_back(const T &Elt)
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
unsigned getNumRegBanks() const
Get the total number of register banks.
MachineRegisterInfo & getMRI() const
The MachineRegisterInfo we used to realize the mapping.
bool isRegSequence() const
LLT getType(unsigned Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register...
Helper class used to get/create the virtual registers that will be used to replace the MachineOperand...
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
void setRegBank(unsigned Reg, const RegisterBank &RegBank)
Set the register bank to RegBank for Reg.
const TargetRegisterClass & getMinimalPhysRegClass(Register Reg, const TargetRegisterInfo &TRI) const
Get the MinimalPhysRegClass for Reg.
static hash_code hashValueMapping(const RegisterBankInfo::PartialMapping *BreakDown, unsigned NumBreakDowns)
void dump() const
Print this partial mapping on dbgs() stream.
bool verify(unsigned MeaningfulBitWidth) const
Verify that this mapping makes sense for a value of MeaningfulBitWidth.
bool covers(const TargetRegisterClass &RC) const
Check whether this register bank covers RC.
bool partsAllUniform() const
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
Holds all the information related to register banks.
const HexagonInstrInfo * TII
const TargetRegisterClass * getRegClassConstraint(unsigned OpIdx, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
Compute the static register class constraint for operand OpIdx.
unsigned getNumOperands() const
Retuns the total number of operands.
const InstructionMapping & getInstrMapping() const
The final mapping of the instruction.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
unsigned StartIdx
Number of bits at which this partial mapping starts in the original value.
void dump() const
Print this operands mapper on dbgs() stream.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
iterator_range< SmallVectorImpl< Register >::const_iterator > getVRegs(unsigned OpIdx, bool ForDebug=false) const
Get all the virtual registers required to map the OpIdx-th operand of the instruction.
bool verify() const
Check that the Mask is compatible with the RegBank.
const RegClassOrRegBank & getRegClassOrRegBank(unsigned Reg) const
Return the register bank or register class of Reg.
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
const PartialMapping & getPartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
Get the uniquely generated PartialMapping for the given arguments.
void setReg(Register Reg)
Change the register this operand corresponds to.
virtual const TargetInstrInfo * getInstrInfo() const
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
const RegisterBank * RegBank
Register bank where the partial value lives.
TargetInstrInfo - Interface to description of machine instruction set.
bool isAllOnesValue() const
Determine if all bits are set.
const TargetRegisterInfo * getTargetRegisterInfo() const
unsigned const MachineRegisterInfo * MRI
const InstructionMapping & getInvalidInstructionMapping() const
Method to get a uniquely generated invalid InstructionMapping.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
virtual const InstructionMapping & getInstrMapping(const MachineInstr &MI) const
Get the mapping of the different operands of MI on the register bank.
Helper struct that represents how a value is partially mapped into a register.
unsigned Length
Length of this mapping in bits.
void setType(unsigned VReg, LLT Ty)
Set the low-level type of VReg to Ty.
static bool isCopyLike(const MachineInstr &MI)
Check whether or not MI should be treated like a copy for the mappings.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static const unsigned DefaultMappingID
Identifier used when the related instruction mapping instance is generated by target independent code...
unsigned getHighBitIdx() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
DenseMap< unsigned, const TargetRegisterClass * > PhysRegMinimalRCs
Getting the minimal register class of a physreg is expensive.
bool isValid() const
Check whether this object is valid.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool verify(const MachineInstr &MI) const
Verifiy that this mapping makes sense for MI.
const PartialMapping * end() const
OperandsMapper(MachineInstr &MI, const InstructionMapping &InstrMapping, MachineRegisterInfo &MRI)
Create an OperandsMapper that will hold the information to apply InstrMapping to MI.
static hash_code hashPartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank *RegBank)
Hashing function for PartialMapping.
Align max(MaybeAlign Lhs, Align Rhs)
RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static void applyDefaultMapping(const OperandsMapper &OpdMapper)
Helper method to apply something that is like the default mapping.
MachineInstr & getMI() const
void print(raw_ostream &OS) const
Print this on OS;.
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
void print(raw_ostream &OS) const
Print this partial mapping on OS;.
This class implements the register bank concept.
Helper struct that represents how a value is mapped through different register banks.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
A range adaptor for a pair of iterators.
Class for arbitrary precision integers.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
typename SuperClass::iterator iterator
static hash_code hashInstructionMapping(unsigned ID, unsigned Cost, const RegisterBankInfo::ValueMapping *OperandsMapping, unsigned NumOperands)
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
An opaque object representing a hash code.
const PartialMapping * begin() const
Iterators through the PartialMappings.
void createVRegs(unsigned OpIdx)
Create as many new virtual registers as needed for the mapping of the OpIdx-th operand.
unsigned getID() const
Get the ID.
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
TargetSubtargetInfo - Generic base class for all target subtargets.
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
virtual InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const
Get the alternative mappings for MI.
DenseMap< unsigned, std::unique_ptr< ValueMapping[]> > MapOfOperandsMappings
Keep dynamically allocated array of ValueMapping in a separate map.
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
const RegisterBank * getRegBankFromConstraints(const MachineInstr &MI, unsigned OpIdx, const TargetInstrInfo &TII, const MachineRegisterInfo &MRI) const
Get the register bank for the OpIdx-th operand of MI form the encoding constraints, if any.
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool cannotCopy(const RegisterBank &Dst, const RegisterBank &Src, unsigned Size) const
bool verify(const TargetRegisterInfo &TRI) const
Check if this register bank is valid.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned NumBreakDowns
Number of partial mapping to break down this value.
virtual const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC, LLT Ty) const
Get a register bank that covers RC.
This class implements an extremely fast bulk output stream that can only output to a stream...
DenseMap< unsigned, std::unique_ptr< const InstructionMapping > > MapOfInstructionMappings
Keep dynamically allocated InstructionMapping in a separate map.
void setRegClass(unsigned Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
RegisterBankInfo()
This constructor is meaningless.
unsigned getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
DenseMap< unsigned, std::unique_ptr< const ValueMapping > > MapOfValueMappings
Keep dynamically allocated ValueMapping in a separate map.
Register getReg() const
getReg - Returns the register number.
void print(raw_ostream &OS, bool ForDebug=false) const
Print this operands mapper on OS stream.
void dump() const
Print this on dbgs() stream.
unsigned getNumOperands() const
Get the number of operands.
const MachineOperand & getOperand(unsigned i) const
const ValueMapping & getValueMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
The most common ValueMapping consists of a single PartialMapping.
SmallVector< const InstructionMapping *, 4 > InstructionMappings
Convenient type to represent the alternatives for mapping an instruction.
static const TargetRegisterClass * constrainGenericRegister(Register Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI)
Constrain the (possibly generic) virtual register Reg to RC.
Wrapper class representing virtual and physical registers.
unsigned getID() const
Get the identifier of this register bank.
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...