1 //===-- AMDGPUInstrInfo.cpp - Base class for AMD GPU InstrInfo ------------===//
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 //
10 /// \file
11 /// \brief Implementation of the TargetInstrInfo class that is common to all
12 /// AMD GPUs.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "AMDGPUInstrInfo.h"
17 #include "AMDGPURegisterInfo.h"
18 #include "AMDGPUTargetMachine.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22
23 using namespace llvm;
24
25 #define GET_INSTRINFO_CTOR_DTOR
26 #define GET_INSTRINFO_NAMED_OPS
27 #define GET_INSTRMAP_INFO
28 #include "AMDGPUGenInstrInfo.inc"
29
30 // Pin the vtable to this file.
anchor()31 void AMDGPUInstrInfo::anchor() {}
32
AMDGPUInstrInfo(const AMDGPUSubtarget & st)33 AMDGPUInstrInfo::AMDGPUInstrInfo(const AMDGPUSubtarget &st)
34 : AMDGPUGenInstrInfo(-1, -1), ST(st) {}
35
getRegisterInfo() const36 const AMDGPURegisterInfo &AMDGPUInstrInfo::getRegisterInfo() const {
37 return RI;
38 }
39
isCoalescableExtInstr(const MachineInstr & MI,unsigned & SrcReg,unsigned & DstReg,unsigned & SubIdx) const40 bool AMDGPUInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
41 unsigned &SrcReg, unsigned &DstReg,
42 unsigned &SubIdx) const {
43 // TODO: Implement this function
44 return false;
45 }
46
isLoadFromStackSlot(const MachineInstr * MI,int & FrameIndex) const47 unsigned AMDGPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
48 int &FrameIndex) const {
49 // TODO: Implement this function
50 return 0;
51 }
52
isLoadFromStackSlotPostFE(const MachineInstr * MI,int & FrameIndex) const53 unsigned AMDGPUInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
54 int &FrameIndex) const {
55 // TODO: Implement this function
56 return 0;
57 }
58
hasLoadFromStackSlot(const MachineInstr * MI,const MachineMemOperand * & MMO,int & FrameIndex) const59 bool AMDGPUInstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
60 const MachineMemOperand *&MMO,
61 int &FrameIndex) const {
62 // TODO: Implement this function
63 return false;
64 }
isStoreFromStackSlot(const MachineInstr * MI,int & FrameIndex) const65 unsigned AMDGPUInstrInfo::isStoreFromStackSlot(const MachineInstr *MI,
66 int &FrameIndex) const {
67 // TODO: Implement this function
68 return 0;
69 }
isStoreFromStackSlotPostFE(const MachineInstr * MI,int & FrameIndex) const70 unsigned AMDGPUInstrInfo::isStoreFromStackSlotPostFE(const MachineInstr *MI,
71 int &FrameIndex) const {
72 // TODO: Implement this function
73 return 0;
74 }
hasStoreFromStackSlot(const MachineInstr * MI,const MachineMemOperand * & MMO,int & FrameIndex) const75 bool AMDGPUInstrInfo::hasStoreFromStackSlot(const MachineInstr *MI,
76 const MachineMemOperand *&MMO,
77 int &FrameIndex) const {
78 // TODO: Implement this function
79 return false;
80 }
81
82 MachineInstr *
convertToThreeAddress(MachineFunction::iterator & MFI,MachineBasicBlock::iterator & MBBI,LiveVariables * LV) const83 AMDGPUInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
84 MachineBasicBlock::iterator &MBBI,
85 LiveVariables *LV) const {
86 // TODO: Implement this function
87 return nullptr;
88 }
89
90 void
storeRegToStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned SrcReg,bool isKill,int FrameIndex,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const91 AMDGPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator MI,
93 unsigned SrcReg, bool isKill,
94 int FrameIndex,
95 const TargetRegisterClass *RC,
96 const TargetRegisterInfo *TRI) const {
97 llvm_unreachable("Not Implemented");
98 }
99
100 void
loadRegFromStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned DestReg,int FrameIndex,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const101 AMDGPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
102 MachineBasicBlock::iterator MI,
103 unsigned DestReg, int FrameIndex,
104 const TargetRegisterClass *RC,
105 const TargetRegisterInfo *TRI) const {
106 llvm_unreachable("Not Implemented");
107 }
108
expandPostRAPseudo(MachineBasicBlock::iterator MI) const109 bool AMDGPUInstrInfo::expandPostRAPseudo (MachineBasicBlock::iterator MI) const {
110 MachineBasicBlock *MBB = MI->getParent();
111 int OffsetOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
112 AMDGPU::OpName::addr);
113 // addr is a custom operand with multiple MI operands, and only the
114 // first MI operand is given a name.
115 int RegOpIdx = OffsetOpIdx + 1;
116 int ChanOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
117 AMDGPU::OpName::chan);
118 if (isRegisterLoad(*MI)) {
119 int DstOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
120 AMDGPU::OpName::dst);
121 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
122 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
123 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
124 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
125 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
126 buildMovInstr(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
127 getIndirectAddrRegClass()->getRegister(Address));
128 } else {
129 buildIndirectRead(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
130 Address, OffsetReg);
131 }
132 } else if (isRegisterStore(*MI)) {
133 int ValOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
134 AMDGPU::OpName::val);
135 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
136 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
137 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
138 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
139 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
140 buildMovInstr(MBB, MI, getIndirectAddrRegClass()->getRegister(Address),
141 MI->getOperand(ValOpIdx).getReg());
142 } else {
143 buildIndirectWrite(MBB, MI, MI->getOperand(ValOpIdx).getReg(),
144 calculateIndirectAddress(RegIndex, Channel),
145 OffsetReg);
146 }
147 } else {
148 return false;
149 }
150
151 MBB->erase(MI);
152 return true;
153 }
154
foldMemoryOperandImpl(MachineFunction & MF,MachineInstr * MI,ArrayRef<unsigned> Ops,int FrameIndex) const155 MachineInstr *AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
156 MachineInstr *MI,
157 ArrayRef<unsigned> Ops,
158 int FrameIndex) const {
159 // TODO: Implement this function
160 return nullptr;
161 }
162 MachineInstr *
foldMemoryOperandImpl(MachineFunction & MF,MachineInstr * MI,ArrayRef<unsigned> Ops,MachineInstr * LoadMI) const163 AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
164 ArrayRef<unsigned> Ops,
165 MachineInstr *LoadMI) const {
166 // TODO: Implement this function
167 return nullptr;
168 }
canFoldMemoryOperand(const MachineInstr * MI,ArrayRef<unsigned> Ops) const169 bool AMDGPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
170 ArrayRef<unsigned> Ops) const {
171 // TODO: Implement this function
172 return false;
173 }
174 bool
unfoldMemoryOperand(MachineFunction & MF,MachineInstr * MI,unsigned Reg,bool UnfoldLoad,bool UnfoldStore,SmallVectorImpl<MachineInstr * > & NewMIs) const175 AMDGPUInstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
176 unsigned Reg, bool UnfoldLoad,
177 bool UnfoldStore,
178 SmallVectorImpl<MachineInstr*> &NewMIs) const {
179 // TODO: Implement this function
180 return false;
181 }
182
183 bool
unfoldMemoryOperand(SelectionDAG & DAG,SDNode * N,SmallVectorImpl<SDNode * > & NewNodes) const184 AMDGPUInstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
185 SmallVectorImpl<SDNode*> &NewNodes) const {
186 // TODO: Implement this function
187 return false;
188 }
189
190 unsigned
getOpcodeAfterMemoryUnfold(unsigned Opc,bool UnfoldLoad,bool UnfoldStore,unsigned * LoadRegIndex) const191 AMDGPUInstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
192 bool UnfoldLoad, bool UnfoldStore,
193 unsigned *LoadRegIndex) const {
194 // TODO: Implement this function
195 return 0;
196 }
197
enableClusterLoads() const198 bool AMDGPUInstrInfo::enableClusterLoads() const {
199 return true;
200 }
201
202 // FIXME: This behaves strangely. If, for example, you have 32 load + stores,
203 // the first 16 loads will be interleaved with the stores, and the next 16 will
204 // be clustered as expected. It should really split into 2 16 store batches.
205 //
206 // Loads are clustered until this returns false, rather than trying to schedule
207 // groups of stores. This also means we have to deal with saying different
208 // address space loads should be clustered, and ones which might cause bank
209 // conflicts.
210 //
211 // This might be deprecated so it might not be worth that much effort to fix.
shouldScheduleLoadsNear(SDNode * Load0,SDNode * Load1,int64_t Offset0,int64_t Offset1,unsigned NumLoads) const212 bool AMDGPUInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1,
213 int64_t Offset0, int64_t Offset1,
214 unsigned NumLoads) const {
215 assert(Offset1 > Offset0 &&
216 "Second offset should be larger than first offset!");
217 // If we have less than 16 loads in a row, and the offsets are within 64
218 // bytes, then schedule together.
219
220 // A cacheline is 64 bytes (for global memory).
221 return (NumLoads <= 16 && (Offset1 - Offset0) < 64);
222 }
223
224 bool
ReverseBranchCondition(SmallVectorImpl<MachineOperand> & Cond) const225 AMDGPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
226 const {
227 // TODO: Implement this function
228 return true;
229 }
insertNoop(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI) const230 void AMDGPUInstrInfo::insertNoop(MachineBasicBlock &MBB,
231 MachineBasicBlock::iterator MI) const {
232 // TODO: Implement this function
233 }
234
isPredicated(const MachineInstr * MI) const235 bool AMDGPUInstrInfo::isPredicated(const MachineInstr *MI) const {
236 // TODO: Implement this function
237 return false;
238 }
239 bool
SubsumesPredicate(const SmallVectorImpl<MachineOperand> & Pred1,const SmallVectorImpl<MachineOperand> & Pred2) const240 AMDGPUInstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
241 const SmallVectorImpl<MachineOperand> &Pred2)
242 const {
243 // TODO: Implement this function
244 return false;
245 }
246
DefinesPredicate(MachineInstr * MI,std::vector<MachineOperand> & Pred) const247 bool AMDGPUInstrInfo::DefinesPredicate(MachineInstr *MI,
248 std::vector<MachineOperand> &Pred) const {
249 // TODO: Implement this function
250 return false;
251 }
252
isPredicable(MachineInstr * MI) const253 bool AMDGPUInstrInfo::isPredicable(MachineInstr *MI) const {
254 // TODO: Implement this function
255 return MI->getDesc().isPredicable();
256 }
257
258 bool
isSafeToMoveRegClassDefs(const TargetRegisterClass * RC) const259 AMDGPUInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
260 // TODO: Implement this function
261 return true;
262 }
263
isRegisterStore(const MachineInstr & MI) const264 bool AMDGPUInstrInfo::isRegisterStore(const MachineInstr &MI) const {
265 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_STORE;
266 }
267
isRegisterLoad(const MachineInstr & MI) const268 bool AMDGPUInstrInfo::isRegisterLoad(const MachineInstr &MI) const {
269 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_LOAD;
270 }
271
getIndirectIndexBegin(const MachineFunction & MF) const272 int AMDGPUInstrInfo::getIndirectIndexBegin(const MachineFunction &MF) const {
273 const MachineRegisterInfo &MRI = MF.getRegInfo();
274 const MachineFrameInfo *MFI = MF.getFrameInfo();
275 int Offset = -1;
276
277 if (MFI->getNumObjects() == 0) {
278 return -1;
279 }
280
281 if (MRI.livein_empty()) {
282 return 0;
283 }
284
285 const TargetRegisterClass *IndirectRC = getIndirectAddrRegClass();
286 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
287 LE = MRI.livein_end();
288 LI != LE; ++LI) {
289 unsigned Reg = LI->first;
290 if (TargetRegisterInfo::isVirtualRegister(Reg) ||
291 !IndirectRC->contains(Reg))
292 continue;
293
294 unsigned RegIndex;
295 unsigned RegEnd;
296 for (RegIndex = 0, RegEnd = IndirectRC->getNumRegs(); RegIndex != RegEnd;
297 ++RegIndex) {
298 if (IndirectRC->getRegister(RegIndex) == Reg)
299 break;
300 }
301 Offset = std::max(Offset, (int)RegIndex);
302 }
303
304 return Offset + 1;
305 }
306
getIndirectIndexEnd(const MachineFunction & MF) const307 int AMDGPUInstrInfo::getIndirectIndexEnd(const MachineFunction &MF) const {
308 int Offset = 0;
309 const MachineFrameInfo *MFI = MF.getFrameInfo();
310
311 // Variable sized objects are not supported
312 assert(!MFI->hasVarSizedObjects());
313
314 if (MFI->getNumObjects() == 0) {
315 return -1;
316 }
317
318 Offset = MF.getSubtarget().getFrameLowering()->getFrameIndexOffset(MF, -1);
319
320 return getIndirectIndexBegin(MF) + Offset;
321 }
322
getMaskedMIMGOp(uint16_t Opcode,unsigned Channels) const323 int AMDGPUInstrInfo::getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const {
324 switch (Channels) {
325 default: return Opcode;
326 case 1: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_1);
327 case 2: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_2);
328 case 3: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_3);
329 }
330 }
331
332 // Wrapper for Tablegen'd function. enum Subtarget is not defined in any
333 // header files, so we need to wrap it in a function that takes unsigned
334 // instead.
335 namespace llvm {
336 namespace AMDGPU {
getMCOpcode(uint16_t Opcode,unsigned Gen)337 static int getMCOpcode(uint16_t Opcode, unsigned Gen) {
338 return getMCOpcodeGen(Opcode, (enum Subtarget)Gen);
339 }
340 }
341 }
342
343 // This must be kept in sync with the SISubtarget class in SIInstrInfo.td
344 enum SISubtarget {
345 SI = 0,
346 VI = 1
347 };
348
AMDGPUSubtargetToSISubtarget(unsigned Gen)349 static enum SISubtarget AMDGPUSubtargetToSISubtarget(unsigned Gen) {
350 switch (Gen) {
351 default:
352 return SI;
353 case AMDGPUSubtarget::VOLCANIC_ISLANDS:
354 return VI;
355 }
356 }
357
pseudoToMCOpcode(int Opcode) const358 int AMDGPUInstrInfo::pseudoToMCOpcode(int Opcode) const {
359 int MCOp = AMDGPU::getMCOpcode(
360 Opcode, AMDGPUSubtargetToSISubtarget(ST.getGeneration()));
361
362 // -1 means that Opcode is already a native instruction.
363 if (MCOp == -1)
364 return Opcode;
365
366 // (uint16_t)-1 means that Opcode is a pseudo instruction that has
367 // no encoding in the given subtarget generation.
368 if (MCOp == (uint16_t)-1)
369 return -1;
370
371 return MCOp;
372 }
373