23#include "llvm/Config/llvm-config.h"
29#define DEBUG_TYPE "registerbankinfo"
34 "Number of partial mappings dynamically created");
36 "Number of partial mappings dynamically accessed");
38 "Number of value mappings dynamically created");
40 "Number of value mappings dynamically accessed");
42 "Number of operands mappings dynamically created");
44 "Number of operands mappings dynamically accessed");
46 "Number of instruction mappings dynamically created");
48 "Number of instruction mappings dynamically accessed");
57 unsigned NumRegBanks,
const unsigned *Sizes,
59 : RegBanks(RegBanks), NumRegBanks(NumRegBanks), Sizes(Sizes),
65 "RegisterBank ID should match index");
75 "ID does not match the index in the array");
86 if (!Reg.isVirtual()) {
94 if (
auto *RB = dyn_cast_if_present<const RegisterBank *>(RegClassOrBank))
97 dyn_cast_if_present<const TargetRegisterClass *>(RegClassOrBank))
105 assert(Reg.isPhysical() &&
"Reg must be a physreg");
108 RegRCIt->second =
TRI.getMinimalPhysRegClassLLT(Reg,
LLT());
109 return RegRCIt->second;
128 "The mapping of the register bank does not make sense");
136 auto &RegClassOrBank =
MRI.getRegClassOrRegBank(Reg);
137 if (isa<const TargetRegisterClass *>(RegClassOrBank))
138 return MRI.constrainRegClass(Reg, &RC);
140 const RegisterBank *RB = cast<const RegisterBank *>(RegClassOrBank);
142 if (RB && !RB->
covers(RC))
146 MRI.setRegClass(Reg, &RC);
157 return MI.isCopy() ||
MI.isPHI() ||
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;
220 if (!OperandsMapping[0]) {
221 if (
MI.isRegSequence()) {
227 OperandsMapping[0] = ValMapping;
234 for (; OpIdx != EndIdx; ++OpIdx) {
248 CompleteMapping =
true;
252 OperandsMapping[OpIdx] = ValMapping;
255 if (IsCopyLike && !CompleteMapping) {
260 assert(CompleteMapping &&
"Setting an uncomplete mapping");
264 NumOperandsForMapping);
283 ++NumPartialMappingsAccessed;
290 ++NumPartialMappingsCreated;
293 PartMapping = std::make_unique<PartialMapping>(StartIdx,
Length, RegBank);
305 unsigned NumBreakDowns) {
309 for (
unsigned Idx = 0;
Idx != NumBreakDowns; ++
Idx)
316 unsigned NumBreakDowns)
const {
317 ++NumValueMappingsAccessed;
324 ++NumValueMappingsCreated;
327 ValMapping = std::make_unique<ValueMapping>(BreakDown, NumBreakDowns);
331template <
typename Iterator>
335 ++NumOperandsMappingsAccessed;
344 ++NumOperandsMappingsCreated;
351 Res = std::make_unique<ValueMapping[]>(std::distance(Begin,
End));
353 for (Iterator It = Begin; It !=
End; ++It, ++
Idx) {
369 std::initializer_list<const RegisterBankInfo::ValueMapping *> OpdsMapping)
377 unsigned NumOperands) {
382RegisterBankInfo::getInstructionMappingImpl(
383 bool IsInvalid,
unsigned ID,
unsigned Cost,
385 unsigned NumOperands)
const {
387 OperandsMapping ==
nullptr && NumOperands == 0) ||
389 "Mismatch argument for invalid input");
390 ++NumInstructionMappingsAccessed;
398 ++NumInstructionMappingsCreated;
401 InstrMapping = std::make_unique<InstructionMapping>(
402 ID,
Cost, OperandsMapping, NumOperands);
403 return *InstrMapping;
418 if (Mapping.isValid()) {
428 assert(Mapping->verify(
MI) &&
"Mapping is invalid");
430 return PossibleMappings;
443 for (
unsigned OpIdx = 0,
445 OpIdx != EndIdx; ++OpIdx) {
464 "This mapping is too complex for this function");
467 if (NewRegs.
empty()) {
468 LLVM_DEBUG(
dbgs() <<
" has not been repaired, nothing to be done\n");
479 LLT OrigTy =
MRI.getType(OrigReg);
480 LLT NewTy =
MRI.getType(NewReg);
481 if (OrigTy != NewTy) {
488 "Types with difference size cannot be handled by the default "
490 LLVM_DEBUG(
dbgs() <<
"\nChange type of new opd from " << NewTy <<
" to "
492 MRI.setType(NewReg, OrigTy);
501 if (Reg.isPhysical()) {
507 assert(RC &&
"Expecting Register class");
508 return TRI.getRegSizeInBits(*RC);
510 return TRI.getRegSizeInBits(Reg,
MRI);
516#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
525 assert(RegBank &&
"Register bank not set");
527 assert((StartIdx <= getHighBitIdx()) &&
"Overflow, switch to APInt?");
530 "Register bank too small for Mask");
535 OS <<
"[" << StartIdx <<
", " << getHighBitIdx() <<
"], RegBank = ";
543 if (NumBreakDowns < 2)
548 if (Part->Length !=
First->Length || Part->RegBank !=
First->RegBank)
556 TypeSize MeaningfulBitWidth)
const {
557 assert(NumBreakDowns &&
"Value mapped nowhere?!");
558 unsigned OrigValueBitWidth = 0;
562 assert(PartMap.
verify(RBI) &&
"Partial mapping is invalid");
569 OrigValueBitWidth >= MeaningfulBitWidth) &&
570 "Meaningful bits not covered by the mapping");
571 APInt ValueMask(OrigValueBitWidth, 0);
578 ValueMask ^= PartMapMask;
579 assert((ValueMask & PartMapMask) == PartMapMask &&
580 "Some partial mappings overlap");
586#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
594 OS <<
"#BreakDown: " << NumBreakDowns <<
" ";
599 OS <<
'[' << PartMap <<
']';
610 "NumOperands must match, see constructor");
612 "MI must be connected to a MachineFunction");
618 for (
unsigned Idx = 0;
Idx < NumOperands; ++
Idx) {
622 "We should not care about non-reg mapping");
628 LLT Ty =
MRI.getType(Reg);
632 "We must have a mapping for reg operands");
640 "Value mapping is invalid");
645#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
653 OS <<
"ID: " << getID() <<
" Cost: " << getCost() <<
" Mapping: ";
655 for (
unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
656 const ValueMapping &ValMapping = getOperandMapping(OpIdx);
659 OS <<
"{ Idx: " << OpIdx <<
" Map: " << ValMapping <<
'}';
663const int RegisterBankInfo::OperandsMapper::DontKnowIdx = -1;
668 :
MRI(
MRI),
MI(
MI), InstrMapping(InstrMapping) {
670 OpToNewVRegIdx.
resize(NumOpds, OperandsMapper::DontKnowIdx);
675RegisterBankInfo::OperandsMapper::getVRegsMem(
unsigned OpIdx) {
677 unsigned NumPartialVal =
679 int StartIdx = OpToNewVRegIdx[OpIdx];
681 if (StartIdx == OperandsMapper::DontKnowIdx) {
685 StartIdx = NewVRegs.size();
686 OpToNewVRegIdx[OpIdx] = StartIdx;
687 for (
unsigned i = 0; i < NumPartialVal; ++i)
688 NewVRegs.push_back(0);
691 getNewVRegsEnd(StartIdx, NumPartialVal);
697RegisterBankInfo::OperandsMapper::getNewVRegsEnd(
unsigned StartIdx,
698 unsigned NumVal)
const {
699 return const_cast<OperandsMapper *
>(
this)->getNewVRegsEnd(StartIdx, NumVal);
702RegisterBankInfo::OperandsMapper::getNewVRegsEnd(
unsigned StartIdx,
704 assert((NewVRegs.size() == StartIdx + NumVal ||
705 NewVRegs.size() > StartIdx + NumVal) &&
706 "NewVRegs too small to contain all the partial mapping");
707 return NewVRegs.size() <= StartIdx + NumVal ? NewVRegs.end()
708 : &NewVRegs[StartIdx + NumVal];
717 for (
Register &NewVReg : NewVRegsForOpIdx) {
718 assert(PartMap != ValMapping.
end() &&
"Out-of-bound access");
719 assert(NewVReg == 0 &&
"Register has already been created");
732 unsigned PartialMapIdx,
737 "Out-of-bound access for partial mapping");
739 (void)getVRegsMem(OpIdx);
740 assert(NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] == 0 &&
741 "This value is already set");
742 NewVRegs[OpToNewVRegIdx[OpIdx] + PartialMapIdx] = NewVReg;
747 bool ForDebug)
const {
750 int StartIdx = OpToNewVRegIdx[OpIdx];
752 if (StartIdx == OperandsMapper::DontKnowIdx)
753 return make_range(NewVRegs.end(), NewVRegs.end());
755 unsigned PartMapSize =
758 getNewVRegsEnd(StartIdx, PartMapSize);
763 assert((VReg || ForDebug) &&
"Some registers are uninitialized");
768#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
776 bool ForDebug)
const {
781 OS <<
"Populated indices (CellNumber, IndexInNewVRegs): ";
783 for (
unsigned Idx = 0;
Idx != NumOpds; ++
Idx) {
784 if (OpToNewVRegIdx[
Idx] != DontKnowIdx) {
787 OS <<
'(' <<
Idx <<
", " << OpToNewVRegIdx[
Idx] <<
')';
795 OS <<
"Operand Mapping: ";
799 getMI().getParent() && getMI().getMF()
800 ? getMI().getMF()->getSubtarget().getRegisterInfo()
803 for (
unsigned Idx = 0;
Idx != NumOpds; ++
Idx) {
804 if (OpToNewVRegIdx[
Idx] == DontKnowIdx)
810 bool IsFirstNewVReg =
true;
814 IsFirstNewVReg =
false;
unsigned const MachineRegisterInfo * MRI
This file implements a class to represent arbitrary precision integral constant values and operations...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
#define LLVM_LIKELY(EXPR)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
const HexagonInstrInfo * TII
unsigned const TargetRegisterInfo * TRI
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static hash_code hashPartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank *RegBank)
Hashing function for PartialMapping.
static bool isCopyLike(const MachineInstr &MI)
Check whether or not MI should be treated like a copy for the mappings.
static hash_code hashValueMapping(const RegisterBankInfo::PartialMapping *BreakDown, unsigned NumBreakDowns)
static hash_code hashInstructionMapping(unsigned ID, unsigned Cost, const RegisterBankInfo::ValueMapping *OperandsMapping, unsigned NumOperands)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Class for arbitrary precision integers.
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Helper class that represents how the value of an instruction may be mapped and what is the related co...
unsigned getNumOperands() const
Get the number of operands.
unsigned getID() const
Get the ID.
bool verify(const MachineInstr &MI) const
Verifiy that this mapping makes sense for MI.
void dump() const
Print this on dbgs() stream.
void print(raw_ostream &OS) const
Print this on OS;.
bool isValid() const
Check whether this object is valid.
Helper class used to get/create the virtual registers that will be used to replace the MachineOperand...
const InstructionMapping & getInstrMapping() const
The final mapping of the instruction.
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.
void print(raw_ostream &OS, bool ForDebug=false) const
Print this operands mapper on OS stream.
MachineInstr & getMI() const
void createVRegs(unsigned OpIdx)
Create as many new virtual registers as needed for the mapping of the OpIdx-th operand.
MachineRegisterInfo & getMRI() const
The MachineRegisterInfo we used to realize the mapping.
OperandsMapper(MachineInstr &MI, const InstructionMapping &InstrMapping, MachineRegisterInfo &MRI)
Create an OperandsMapper that will hold the information to apply InstrMapping to MI.
void dump() const
Print this operands mapper on dbgs() stream.
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.
Holds all the information related to register banks.
const PartialMapping & getPartialMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
Get the uniquely generated PartialMapping for the given arguments.
virtual InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const
Get the alternative mappings for MI.
static const TargetRegisterClass * constrainGenericRegister(Register Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI)
Constrain the (possibly generic) virtual register Reg to RC.
DenseMap< hash_code, std::unique_ptr< const InstructionMapping > > MapOfInstructionMappings
Keep dynamically allocated InstructionMapping in a separate map.
const RegisterBank ** RegBanks
Hold the set of supported register banks.
RegisterBankInfo()
This constructor is meaningless.
DenseMap< hash_code, std::unique_ptr< const PartialMapping > > MapOfPartialMappings
Keep dynamically allocated PartialMapping in a separate map.
virtual const InstructionMapping & getInstrMapping(const MachineInstr &MI) const
Get the mapping of the different operands of MI on the register bank.
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
static void applyDefaultMapping(const OperandsMapper &OpdMapper)
Helper method to apply something that is like the default mapping.
DenseMap< hash_code, std::unique_ptr< const ValueMapping > > MapOfValueMappings
Keep dynamically allocated ValueMapping in a separate map.
const TargetRegisterClass * getMinimalPhysRegClass(Register Reg, const TargetRegisterInfo &TRI) const
Get the MinimalPhysRegClass for Reg.
const ValueMapping & getValueMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
The most common ValueMapping consists of a single PartialMapping.
const InstructionMapping & getInvalidInstructionMapping() const
Method to get a uniquely generated invalid InstructionMapping.
DenseMap< hash_code, std::unique_ptr< ValueMapping[]> > MapOfOperandsMappings
Keep dynamically allocated array of ValueMapping in a separate map.
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
bool cannotCopy(const RegisterBank &Dst, const RegisterBank &Src, TypeSize Size) const
unsigned getMaximumSize(unsigned RegBankID) const
Get the maximum size in bits that fits in the given register bank.
unsigned getNumRegBanks() const
Get the total number of register banks.
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,...
TypeSize getSizeInBits(Register Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI) const
Get the size in bits of Reg.
virtual const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC, LLT Ty) const
Get a register bank that covers RC.
InstructionMappings getInstrPossibleMappings(const MachineInstr &MI) const
Get the possible mapping for MI.
DenseMap< unsigned, const TargetRegisterClass * > PhysRegMinimalRCs
Getting the minimal register class of a physreg is expensive.
static const unsigned InvalidMappingID
Identifier used when the related instruction mapping instance is generated by the default constructor...
const ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End.
static const unsigned DefaultMappingID
Identifier used when the related instruction mapping instance is generated by target independent code...
SmallVector< const InstructionMapping *, 4 > InstructionMappings
Convenient type to represent the alternatives for mapping an instruction.
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
This class implements the register bank concept.
bool verify(const RegisterBankInfo &RBI, const TargetRegisterInfo &TRI) const
Check if this register bank is valid.
bool covers(const TargetRegisterClass &RC) const
Check whether this register bank covers RC.
unsigned getID() const
Get the identifier of this register bank.
Wrapper class representing virtual and physical registers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
typename SuperClass::const_iterator const_iterator
typename SuperClass::iterator iterator
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const RegisterBankInfo * getRegBankInfo() const
If the information for the register banks is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
An opaque object representing a hash code.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
hash_code hash_value(const FixedPointSemantics &Val)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
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.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Helper struct that represents how a value is partially mapped into a register.
void print(raw_ostream &OS) const
Print this partial mapping on OS;.
unsigned StartIdx
Number of bits at which this partial mapping starts in the original value.
unsigned getHighBitIdx() const
bool verify(const RegisterBankInfo &RBI) const
Check that the Mask is compatible with the RegBank.
void dump() const
Print this partial mapping on dbgs() stream.
const RegisterBank * RegBank
Register bank where the partial value lives.
unsigned Length
Length of this mapping in bits.
Helper struct that represents how a value is mapped through different register banks.
const PartialMapping * begin() const
Iterators through the PartialMappings.
const PartialMapping * end() const
void print(raw_ostream &OS) const
Print this on OS;.
bool verify(const RegisterBankInfo &RBI, TypeSize MeaningfulBitWidth) const
Verify that this mapping makes sense for a value of MeaningfulBitWidth.
bool partsAllUniform() const
unsigned NumBreakDowns
Number of partial mapping to break down this value.
void dump() const
Print this on dbgs() stream.