25 #include "llvm/Config/llvm-config.h" 32 #define DEBUG_TYPE "registerbankinfo" 37 "Number of partial mappings dynamically created");
39 "Number of partial mappings dynamically accessed");
41 "Number of value mappings dynamically created");
43 "Number of value mappings dynamically accessed");
45 "Number of operands mappings dynamically created");
47 "Number of operands mappings dynamically accessed");
49 "Number of instruction mappings dynamically created");
51 "Number of instruction mappings dynamically accessed");
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");
72 for (
unsigned Idx = 0, End =
getNumRegBanks(); Idx != End; ++Idx) {
75 "ID does not match the index in the array");
89 assert(Reg &&
"NoRegister does not have a register bank");
102 "Reg must be a physreg");
105 return *RegRCIt->second;
124 "The mapping of the register bank does not make sense");
138 if (RB && !RB->
covers(RC))
154 MI.
getOpcode() == TargetOpcode::REG_SEQUENCE;
165 unsigned NumOperandsForMapping = IsCopyLike ? 1 : MI.
getNumOperands();
176 bool CompleteMapping =
true;
179 for (
unsigned OpIdx = 0, EndIdx = MI.
getNumOperands(); OpIdx != EndIdx;
196 const RegisterBank *CurRegBank = IsCopyLike ? AltRegBank :
nullptr;
203 CompleteMapping =
false;
214 OperandsMapping[0] = ValMapping;
215 CompleteMapping =
true;
218 OperandsMapping[OpIdx] = ValMapping;
221 if (IsCopyLike && !CompleteMapping)
225 assert(CompleteMapping &&
"Setting an uncomplete mapping");
229 NumOperandsForMapping);
248 ++NumPartialMappingsAccessed;
255 ++NumPartialMappingsCreated;
258 PartMapping = llvm::make_unique<PartialMapping>(StartIdx, Length, RegBank);
270 unsigned NumBreakDowns) {
274 for (
unsigned Idx = 0; Idx != NumBreakDowns; ++Idx)
281 unsigned NumBreakDowns)
const {
282 ++NumValueMappingsAccessed;
289 ++NumValueMappingsCreated;
292 ValMapping = llvm::make_unique<ValueMapping>(BreakDown, NumBreakDowns);
296 template <
typename Iterator>
300 ++NumOperandsMappingsAccessed;
309 ++NumOperandsMappingsCreated;
316 Res = llvm::make_unique<ValueMapping[]>(std::distance(Begin, End));
318 for (Iterator It = Begin; It != End; ++It, ++Idx) {
334 std::initializer_list<const RegisterBankInfo::ValueMapping *> OpdsMapping)
342 unsigned NumOperands) {
343 return hash_combine(ID, Cost, OperandsMapping, NumOperands);
347 RegisterBankInfo::getInstructionMappingImpl(
348 bool IsInvalid,
unsigned ID,
unsigned Cost,
350 unsigned NumOperands)
const {
352 OperandsMapping ==
nullptr && NumOperands == 0) ||
354 "Mismatch argument for invalid input");
355 ++NumInstructionMappingsAccessed;
363 ++NumInstructionMappingsCreated;
367 InstrMapping = llvm::make_unique<InstructionMapping>();
369 InstrMapping = llvm::make_unique<InstructionMapping>(
370 ID, Cost, OperandsMapping, NumOperands);
371 return *InstrMapping;
383 RegisterBankInfo::getInstrPossibleMappings(
const MachineInstr &
MI)
const {
393 assert(Mapping->verify(MI) &&
"Mapping is invalid");
395 return PossibleMappings;
408 for (
unsigned OpIdx = 0,
410 OpIdx != EndIdx; ++OpIdx) {
426 "This mapping is too complex for this function");
429 if (
empty(NewRegs)) {
430 LLVM_DEBUG(
dbgs() <<
" has not been repaired, nothing to be done\n");
433 unsigned OrigReg = MO.
getReg();
434 unsigned NewReg = *NewRegs.begin();
443 if (OrigTy != NewTy) {
449 "Types with difference size cannot be handled by the default " 451 LLVM_DEBUG(
dbgs() <<
"\nChange type of new opd from " << NewTy <<
" to " 468 assert(RC &&
"Expecting Register class");
477 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 485 assert(RegBank &&
"Register bank not set");
486 assert(Length &&
"Empty mapping");
487 assert((StartIdx <= getHighBitIdx()) &&
"Overflow, switch to APInt?");
489 assert(RegBank->getSize() >= Length &&
"Register bank too small for Mask");
494 OS <<
"[" << StartIdx <<
", " << getHighBitIdx() <<
"], RegBank = ";
502 assert(NumBreakDowns &&
"Value mapped nowhere?!");
503 unsigned OrigValueBitWidth = 0;
507 assert(PartMap.
verify() &&
"Partial mapping is invalid");
513 assert(OrigValueBitWidth >= MeaningfulBitWidth &&
514 "Meaningful bits not covered by the mapping");
515 APInt ValueMask(OrigValueBitWidth, 0);
522 ValueMask ^= PartMapMask;
523 assert((ValueMask & PartMapMask) == PartMapMask &&
524 "Some partial mappings overlap");
530 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 538 OS <<
"#BreakDown: " << NumBreakDowns <<
" ";
543 OS <<
'[' << PartMap <<
']';
554 "NumOperands must match, see constructor");
556 "MI must be connected to a MachineFunction");
561 for (
unsigned Idx = 0; Idx < NumOperands; ++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");
578 Reg, MF.getRegInfo(), *MF.getSubtarget().getRegisterInfo())) &&
579 "Value mapping is invalid");
584 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 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;
607 : MRI(MRI), MI(MI), InstrMapping(InstrMapping) {
609 OpToNewVRegIdx.
resize(NumOpds, OperandsMapper::DontKnowIdx);
610 assert(InstrMapping.
verify(MI) &&
"Invalid mapping for MI");
614 RegisterBankInfo::OperandsMapper::getVRegsMem(
unsigned OpIdx) {
616 unsigned NumPartialVal =
618 int StartIdx = OpToNewVRegIdx[OpIdx];
620 if (StartIdx == OperandsMapper::DontKnowIdx) {
624 StartIdx = NewVRegs.
size();
625 OpToNewVRegIdx[OpIdx] = StartIdx;
626 for (
unsigned i = 0; i < NumPartialVal; ++i)
630 getNewVRegsEnd(StartIdx, NumPartialVal);
636 RegisterBankInfo::OperandsMapper::getNewVRegsEnd(
unsigned StartIdx,
637 unsigned NumVal)
const {
638 return const_cast<OperandsMapper *
>(
this)->getNewVRegsEnd(StartIdx, NumVal);
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];
656 for (
unsigned &NewVReg : NewVRegsForOpIdx) {
657 assert(PartMap != ValMapping.
end() &&
"Out-of-bound access");
658 assert(NewVReg == 0 &&
"Register has already been created");
671 unsigned PartialMapIdx,
676 "Out-of-bound access for partial mapping");
678 (void)getVRegsMem(OpIdx);
679 assert(NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] == 0 &&
680 "This value is already set");
681 NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] = NewVReg;
686 bool ForDebug)
const {
689 int StartIdx = OpToNewVRegIdx[OpIdx];
691 if (StartIdx == OperandsMapper::DontKnowIdx)
694 unsigned PartMapSize =
697 getNewVRegsEnd(StartIdx, PartMapSize);
701 for (
unsigned VReg : Res)
702 assert((VReg || ForDebug) &&
"Some registers are uninitialized");
707 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 715 bool ForDebug)
const {
720 OS <<
"Populated indices (CellNumber, IndexInNewVRegs): ";
722 for (
unsigned Idx = 0; Idx != NumOpds; ++Idx) {
723 if (OpToNewVRegIdx[Idx] != DontKnowIdx) {
726 OS <<
'(' << Idx <<
", " << OpToNewVRegIdx[Idx] <<
')';
734 OS <<
"Operand Mapping: ";
742 for (
unsigned Idx = 0; Idx != NumOpds; ++Idx) {
743 if (OpToNewVRegIdx[Idx] == DontKnowIdx)
749 bool IsFirstNewVReg =
true;
750 for (
unsigned VReg :
getVRegs(Idx)) {
753 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 ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
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;.
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
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.
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)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
unsigned getReg() const
getReg - Returns the register number.
unsigned getNumRegBanks() const
Get the total number of register banks.
MachineRegisterInfo & getMRI() const
The MachineRegisterInfo we used to realize the mapping.
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
void setRegBank(unsigned Reg, const RegisterBank &RegBank)
Set the register bank to RegBank 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.
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.
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
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< unsigned >::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.
static int getID(struct InternalInstruction *insn, const void *miiArg)
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.
virtual const TargetInstrInfo * getInstrInfo() const
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
const TargetRegisterClass & getMinimalPhysRegClass(unsigned Reg, const TargetRegisterInfo &TRI) const
Get the MinimalPhysRegClass for Reg.
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.
void setVRegs(unsigned OpIdx, unsigned PartialMapIdx, unsigned NewVReg)
Set the virtual register of the PartialMapIdx-th partial mapping of the OpIdx-th operand to NewVReg...
bool isAllOnesValue() const
Determine if all bits are set.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
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
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwises returns null...
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.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
DenseMap< unsigned, const TargetRegisterClass * > PhysRegMinimalRCs
Getting the minimal register class of a physreg is expensive.
bool isValid() const
Check whether this object is valid.
unsigned createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
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.
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)
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.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
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.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
static const TargetRegisterClass * constrainGenericRegister(unsigned Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI)
Constrain the (possibly generic) virtual register Reg to RC.
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
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 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.
unsigned getSizeInBits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI) const
Get the size in bits of Reg.
const RegisterBank * getRegBankFromConstraints(const MachineInstr &MI, unsigned OpIdx, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI) const
Get the register bank for the OpIdx-th operand of MI form the encoding constraints, if any.
This class implements an extremely fast bulk output stream that can only output to a stream...
#define LLVM_LIKELY(EXPR)
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.
DenseMap< unsigned, std::unique_ptr< const ValueMapping > > MapOfValueMappings
Keep dynamically allocated ValueMapping in a separate map.
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.
unsigned getID() const
Get the identifier of this register bank.
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
virtual const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC) const
Get a register bank that covers RC.