LLVM  5.0.0
AMDGPURegisterBankInfo.cpp
Go to the documentation of this file.
1 //===- AMDGPURegisterBankInfo.cpp -------------------------------*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file implements the targeting of the RegisterBankInfo class for
11 /// AMDGPU.
12 /// \todo This should be generated by TableGen.
13 //===----------------------------------------------------------------------===//
14 
15 #include "AMDGPURegisterBankInfo.h"
16 #include "AMDGPUInstrInfo.h"
17 #include "SIRegisterInfo.h"
20 #include "llvm/IR/Constants.h"
23 
24 #define GET_TARGET_REGBANK_IMPL
25 #include "AMDGPUGenRegisterBank.inc"
26 
27 // This file will be TableGen'ed at some point.
28 #include "AMDGPUGenRegisterBankInfo.def"
29 
30 using namespace llvm;
31 
32 #ifndef LLVM_BUILD_GLOBAL_ISEL
33 #error "You shouldn't build this"
34 #endif
35 
38  TRI(static_cast<const SIRegisterInfo*>(&TRI)) {
39 
40  // HACK: Until this is fully tablegen'd
41  static bool AlreadyInit = false;
42  if (AlreadyInit)
43  return;
44 
45  AlreadyInit = true;
46 
48  (void)RBSGPR;
49  assert(&RBSGPR == &AMDGPU::SGPRRegBank);
50 
52  (void)RBVGPR;
53  assert(&RBVGPR == &AMDGPU::VGPRRegBank);
54 
55 }
56 
58  const RegisterBank &B,
59  unsigned Size) const {
60  return RegisterBankInfo::copyCost(A, B, Size);
61 }
62 
64  const TargetRegisterClass &RC) const {
65 
66  if (TRI->isSGPRClass(&RC))
68 
70 }
71 
74  const MachineInstr &MI) const {
75 
76  const MachineFunction &MF = *MI.getParent()->getParent();
77  const MachineRegisterInfo &MRI = MF.getRegInfo();
78 
79  unsigned Size = getSizeInBits(MI.getOperand(0).getReg(), MRI, *TRI);
80 
81  InstructionMappings AltMappings;
82  switch (MI.getOpcode()) {
83  case TargetOpcode::G_LOAD: {
84  // FIXME: Should we be hard coding the size for these mappings?
85  const InstructionMapping &SSMapping = getInstructionMapping(
86  1, 1, getOperandsMapping(
89  2); // Num Operands
90  AltMappings.push_back(&SSMapping);
91 
92  const InstructionMapping &VVMapping = getInstructionMapping(
93  2, 1, getOperandsMapping(
96  2); // Num Operands
97  AltMappings.push_back(&VVMapping);
98 
99  // FIXME: Should this be the pointer-size (64-bits) or the size of the
100  // register that will hold the bufffer resourc (128-bits).
101  const InstructionMapping &VSMapping = getInstructionMapping(
102  3, 1, getOperandsMapping(
105  2); // Num Operands
106  AltMappings.push_back(&VSMapping);
107 
108  return AltMappings;
109 
110  }
111  default:
112  break;
113  }
115 }
116 
117 void AMDGPURegisterBankInfo::applyMappingImpl(
118  const OperandsMapper &OpdMapper) const {
119  return applyDefaultMapping(OpdMapper);
120 }
121 
122 static bool isInstrUniform(const MachineInstr &MI) {
123  if (!MI.hasOneMemOperand())
124  return false;
125 
126  const MachineMemOperand *MMO = *MI.memoperands_begin();
127  return AMDGPU::isUniformMMO(MMO);
128 }
129 
131 AMDGPURegisterBankInfo::getInstrMappingForLoad(const MachineInstr &MI) const {
132 
133  const MachineFunction &MF = *MI.getParent()->getParent();
134  const MachineRegisterInfo &MRI = MF.getRegInfo();
136  unsigned Size = getSizeInBits(MI.getOperand(0).getReg(), MRI, *TRI);
137  unsigned PtrSize = getSizeInBits(MI.getOperand(1).getReg(), MRI, *TRI);
138 
139  const ValueMapping *ValMapping;
140  const ValueMapping *PtrMapping;
141 
142  if (isInstrUniform(MI)) {
143  // We have a uniform instruction so we want to use an SMRD load
145  PtrMapping = AMDGPU::getValueMapping(AMDGPU::SGPRRegBankID, PtrSize);
146  } else {
148  // FIXME: What would happen if we used SGPRRegBankID here?
149  PtrMapping = AMDGPU::getValueMapping(AMDGPU::VGPRRegBankID, PtrSize);
150  }
151 
152  OpdsMapping[0] = ValMapping;
153  OpdsMapping[1] = PtrMapping;
155  1, 1, getOperandsMapping(OpdsMapping), MI.getNumOperands());
156  return Mapping;
157 
158  // FIXME: Do we want to add a mapping for FLAT load, or should we just
159  // handle that during instruction selection?
160 }
161 
165 
166  if (Mapping.isValid())
167  return Mapping;
168 
169  const MachineFunction &MF = *MI.getParent()->getParent();
170  const MachineRegisterInfo &MRI = MF.getRegInfo();
172 
173  bool IsComplete = true;
174  switch (MI.getOpcode()) {
175  default:
176  IsComplete = false;
177  break;
178  case AMDGPU::G_CONSTANT: {
179  unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
180  OpdsMapping[0] = AMDGPU::getValueMapping(AMDGPU::SGPRRegBankID, Size);
181  break;
182  }
183  case AMDGPU::G_GEP: {
184  for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
185  if (!MI.getOperand(i).isReg())
186  continue;
187 
188  unsigned Size = MRI.getType(MI.getOperand(i).getReg()).getSizeInBits();
189  OpdsMapping[i] = AMDGPU::getValueMapping(AMDGPU::SGPRRegBankID, Size);
190  }
191  break;
192  }
193  case AMDGPU::G_STORE: {
194  assert(MI.getOperand(0).isReg());
195  unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
196  // FIXME: We need to specify a different reg bank once scalar stores
197  // are supported.
198  const ValueMapping *ValMapping =
200  // FIXME: Depending on the type of store, the pointer could be in
201  // the SGPR Reg bank.
202  // FIXME: Pointer size should be based on the address space.
203  const ValueMapping *PtrMapping =
205 
206  OpdsMapping[0] = ValMapping;
207  OpdsMapping[1] = PtrMapping;
208  break;
209  }
210 
211  case AMDGPU::G_LOAD:
212  return getInstrMappingForLoad(MI);
213  }
214 
215  if (!IsComplete) {
216  unsigned BankID = AMDGPU::SGPRRegBankID;
217 
218  unsigned Size = 0;
219  for (unsigned Idx = 0; Idx < MI.getNumOperands(); ++Idx) {
220  // If the operand is not a register default to the size of the previous
221  // operand.
222  // FIXME: Can't we pull the types from the MachineInstr rather than the
223  // operands.
224  if (MI.getOperand(Idx).isReg())
225  Size = getSizeInBits(MI.getOperand(Idx).getReg(), MRI, *TRI);
226  OpdsMapping.push_back(AMDGPU::getValueMapping(BankID, Size));
227  }
228  }
229  return getInstructionMapping(1, 1, getOperandsMapping(OpdsMapping),
230  MI.getNumOperands());
231 }
const InstructionMapping & getInstrMapping(const MachineInstr &MI) const override
Get the mapping of the different operands of MI on the register bank.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
virtual unsigned copyCost(const RegisterBank &A, const RegisterBank &B, unsigned Size) const
Get the cost of a copy from B to A, or put differently, get the cost of A = COPY B.
Interface definition for SIRegisterInfo.
const InstructionMapping & getInstructionMapping(unsigned ID, unsigned Cost, const ValueMapping *OperandsMapping, unsigned NumOperands) const
Method to get a uniquely generated InstructionMapping.
This file declares the targeting of the RegisterBankInfo class for AMDGPU.
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
const ValueMapping & getValueMapping(unsigned StartIdx, unsigned Length, const RegisterBank &RegBank) const
The most common ValueMapping consists of a single PartialMapping.
Helper class that represents how the value of an instruction may be mapped and what is the related co...
bool isSGPRClass(const TargetRegisterClass *RC) const
unsigned copyCost(const RegisterBank &A, const RegisterBank &B, unsigned Size) const override
Get the cost of a copy from B to A, or put differently, get the cost of A = COPY B.
A description of a memory reference used in the backend.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
virtual InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const
Get the alternative mappings for MI.
const ValueMapping * getOperandsMapping(Iterator Begin, Iterator End) const
Get the uniquely generated array of ValueMapping for the elements of between Begin and End...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
static bool isInstrUniform(const MachineInstr &MI)
unsigned const MachineRegisterInfo * MRI
static unsigned getSizeInBits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI)
Get the size in bits of Reg.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
#define A
Definition: LargeTest.cpp:12
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
Definition: MachineInstr.h:378
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Helper class used to get/create the virtual registers that will be used to replace the MachineOperand...
#define B
Definition: LargeTest.cpp:24
RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
static void applyDefaultMapping(const OperandsMapper &OpdMapper)
Helper method to apply something that is like the default mapping.
This class implements the register bank concept.
Definition: RegisterBank.h:29
Helper struct that represents how a value is mapped through different register banks.
AMDGPURegisterBankInfo(const TargetRegisterInfo &TRI)
bool isValid() const
Check whether this object is valid.
InstructionMappings getInstrAlternativeMappings(const MachineInstr &MI) const override
Get the alternative mappings for MI.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Contains the definition of a TargetInstrInfo class that is common to all AMD GPUs.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:59
const RegisterBank & getRegBankFromRegClass(const TargetRegisterClass &RC) const override
Get a register bank that covers RC.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool isUniformMMO(const MachineMemOperand *MMO)
This class provides the information for the target register banks.
Helper struct that represents how a value is mapped through different register banks.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const InstructionMapping & getInstrMappingImpl(const MachineInstr &MI) const
Try to get the mapping of MI.
aarch64 promote const
IRTranslator LLVM IR MI
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:363