blob: ef3bdb17e999c85fc519fd8b32b512080037676d [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- 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"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22
Chandler Carruthd174b722014-04-22 02:03:14 +000023using namespace llvm;
24
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000025#define GET_INSTRINFO_CTOR_DTOR
Tom Stellard02661d92013-06-25 21:22:18 +000026#define GET_INSTRINFO_NAMED_OPS
Christian Konigf741fbf2013-02-26 17:52:42 +000027#define GET_INSTRMAP_INFO
Tom Stellard75aadc22012-12-11 21:25:42 +000028#include "AMDGPUGenInstrInfo.inc"
29
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000030// Pin the vtable to this file.
31void AMDGPUInstrInfo::anchor() {}
32
Tom Stellard2e59a452014-06-13 01:32:00 +000033AMDGPUInstrInfo::AMDGPUInstrInfo(const AMDGPUSubtarget &st)
34 : AMDGPUGenInstrInfo(-1,-1), RI(st), ST(st) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000035
36const AMDGPURegisterInfo &AMDGPUInstrInfo::getRegisterInfo() const {
37 return RI;
38}
39
40bool AMDGPUInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
41 unsigned &SrcReg, unsigned &DstReg,
42 unsigned &SubIdx) const {
43// TODO: Implement this function
44 return false;
45}
46
47unsigned AMDGPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
48 int &FrameIndex) const {
49// TODO: Implement this function
50 return 0;
51}
52
53unsigned AMDGPUInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
54 int &FrameIndex) const {
55// TODO: Implement this function
56 return 0;
57}
58
59bool AMDGPUInstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
60 const MachineMemOperand *&MMO,
61 int &FrameIndex) const {
62// TODO: Implement this function
63 return false;
64}
65unsigned AMDGPUInstrInfo::isStoreFromStackSlot(const MachineInstr *MI,
66 int &FrameIndex) const {
67// TODO: Implement this function
68 return 0;
69}
70unsigned AMDGPUInstrInfo::isStoreFromStackSlotPostFE(const MachineInstr *MI,
71 int &FrameIndex) const {
72// TODO: Implement this function
73 return 0;
74}
75bool AMDGPUInstrInfo::hasStoreFromStackSlot(const MachineInstr *MI,
76 const MachineMemOperand *&MMO,
77 int &FrameIndex) const {
78// TODO: Implement this function
79 return false;
80}
81
82MachineInstr *
83AMDGPUInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
84 MachineBasicBlock::iterator &MBBI,
85 LiveVariables *LV) const {
86// TODO: Implement this function
Craig Topper062a2ba2014-04-25 05:30:21 +000087 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +000088}
89bool AMDGPUInstrInfo::getNextBranchInstr(MachineBasicBlock::iterator &iter,
90 MachineBasicBlock &MBB) const {
91 while (iter != MBB.end()) {
92 switch (iter->getOpcode()) {
93 default:
94 break;
95 case AMDGPU::BRANCH_COND_i32:
96 case AMDGPU::BRANCH_COND_f32:
97 case AMDGPU::BRANCH:
98 return true;
99 };
100 ++iter;
101 }
102 return false;
103}
104
Tom Stellard75aadc22012-12-11 21:25:42 +0000105void
106AMDGPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
107 MachineBasicBlock::iterator MI,
108 unsigned SrcReg, bool isKill,
109 int FrameIndex,
110 const TargetRegisterClass *RC,
111 const TargetRegisterInfo *TRI) const {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000112 llvm_unreachable("Not Implemented");
Tom Stellard75aadc22012-12-11 21:25:42 +0000113}
114
115void
116AMDGPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
117 MachineBasicBlock::iterator MI,
118 unsigned DestReg, int FrameIndex,
119 const TargetRegisterClass *RC,
120 const TargetRegisterInfo *TRI) const {
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000121 llvm_unreachable("Not Implemented");
Tom Stellard75aadc22012-12-11 21:25:42 +0000122}
123
Tom Stellard26a3b672013-10-22 18:19:10 +0000124bool AMDGPUInstrInfo::expandPostRAPseudo (MachineBasicBlock::iterator MI) const {
125 MachineBasicBlock *MBB = MI->getParent();
Matt Arsenaulte1f1da32014-03-11 00:01:27 +0000126 int OffsetOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
127 AMDGPU::OpName::addr);
Tom Stellard81d871d2013-11-13 23:36:50 +0000128 // addr is a custom operand with multiple MI operands, and only the
129 // first MI operand is given a name.
130 int RegOpIdx = OffsetOpIdx + 1;
Matt Arsenaulte1f1da32014-03-11 00:01:27 +0000131 int ChanOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
132 AMDGPU::OpName::chan);
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000133 if (isRegisterLoad(*MI)) {
Matt Arsenaulte1f1da32014-03-11 00:01:27 +0000134 int DstOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
135 AMDGPU::OpName::dst);
Tom Stellard81d871d2013-11-13 23:36:50 +0000136 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
137 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000138 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
Tom Stellard81d871d2013-11-13 23:36:50 +0000139 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000140 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000141 buildMovInstr(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000142 getIndirectAddrRegClass()->getRegister(Address));
Tom Stellard26a3b672013-10-22 18:19:10 +0000143 } else {
Tom Stellard81d871d2013-11-13 23:36:50 +0000144 buildIndirectRead(MBB, MI, MI->getOperand(DstOpIdx).getReg(),
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000145 Address, OffsetReg);
Tom Stellard26a3b672013-10-22 18:19:10 +0000146 }
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000147 } else if (isRegisterStore(*MI)) {
Matt Arsenaulte1f1da32014-03-11 00:01:27 +0000148 int ValOpIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
149 AMDGPU::OpName::val);
Tom Stellard81d871d2013-11-13 23:36:50 +0000150 unsigned RegIndex = MI->getOperand(RegOpIdx).getImm();
151 unsigned Channel = MI->getOperand(ChanOpIdx).getImm();
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000152 unsigned Address = calculateIndirectAddress(RegIndex, Channel);
Tom Stellard81d871d2013-11-13 23:36:50 +0000153 unsigned OffsetReg = MI->getOperand(OffsetOpIdx).getReg();
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000154 if (OffsetReg == AMDGPU::INDIRECT_BASE_ADDR) {
155 buildMovInstr(MBB, MI, getIndirectAddrRegClass()->getRegister(Address),
Tom Stellard81d871d2013-11-13 23:36:50 +0000156 MI->getOperand(ValOpIdx).getReg());
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000157 } else {
Tom Stellard81d871d2013-11-13 23:36:50 +0000158 buildIndirectWrite(MBB, MI, MI->getOperand(ValOpIdx).getReg(),
159 calculateIndirectAddress(RegIndex, Channel),
160 OffsetReg);
Aaron Ballman9ab670f2013-10-29 20:40:52 +0000161 }
162 } else {
163 return false;
Tom Stellard26a3b672013-10-22 18:19:10 +0000164 }
165
166 MBB->erase(MI);
167 return true;
168}
169
170
Tom Stellard75aadc22012-12-11 21:25:42 +0000171MachineInstr *
172AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
173 MachineInstr *MI,
174 const SmallVectorImpl<unsigned> &Ops,
175 int FrameIndex) const {
176// TODO: Implement this function
Craig Topper062a2ba2014-04-25 05:30:21 +0000177 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +0000178}
179MachineInstr*
180AMDGPUInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
181 MachineInstr *MI,
182 const SmallVectorImpl<unsigned> &Ops,
183 MachineInstr *LoadMI) const {
184 // TODO: Implement this function
Craig Topper062a2ba2014-04-25 05:30:21 +0000185 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +0000186}
187bool
188AMDGPUInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
189 const SmallVectorImpl<unsigned> &Ops) const {
190 // TODO: Implement this function
191 return false;
192}
193bool
194AMDGPUInstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
195 unsigned Reg, bool UnfoldLoad,
196 bool UnfoldStore,
197 SmallVectorImpl<MachineInstr*> &NewMIs) const {
198 // TODO: Implement this function
199 return false;
200}
201
202bool
203AMDGPUInstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
204 SmallVectorImpl<SDNode*> &NewNodes) const {
205 // TODO: Implement this function
206 return false;
207}
208
209unsigned
210AMDGPUInstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
211 bool UnfoldLoad, bool UnfoldStore,
212 unsigned *LoadRegIndex) const {
213 // TODO: Implement this function
214 return 0;
215}
216
Matt Arsenault034d6662014-07-24 02:10:17 +0000217bool AMDGPUInstrInfo::enableClusterLoads() const {
218 return true;
219}
220
Matt Arsenaultd5f4de22014-08-06 00:29:49 +0000221// FIXME: This behaves strangely. If, for example, you have 32 load + stores,
222// the first 16 loads will be interleaved with the stores, and the next 16 will
223// be clustered as expected. It should really split into 2 16 store batches.
224//
225// Loads are clustered until this returns false, rather than trying to schedule
226// groups of stores. This also means we have to deal with saying different
227// address space loads should be clustered, and ones which might cause bank
228// conflicts.
229//
230// This might be deprecated so it might not be worth that much effort to fix.
231bool AMDGPUInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1,
232 int64_t Offset0, int64_t Offset1,
233 unsigned NumLoads) const {
234 assert(Offset1 > Offset0 &&
235 "Second offset should be larger than first offset!");
236 // If we have less than 16 loads in a row, and the offsets are within 64
237 // bytes, then schedule together.
238
239 // A cacheline is 64 bytes (for global memory).
240 return (NumLoads <= 16 && (Offset1 - Offset0) < 64);
Tom Stellard75aadc22012-12-11 21:25:42 +0000241}
242
243bool
244AMDGPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond)
245 const {
246 // TODO: Implement this function
247 return true;
248}
249void AMDGPUInstrInfo::insertNoop(MachineBasicBlock &MBB,
250 MachineBasicBlock::iterator MI) const {
251 // TODO: Implement this function
252}
253
254bool AMDGPUInstrInfo::isPredicated(const MachineInstr *MI) const {
255 // TODO: Implement this function
256 return false;
257}
258bool
259AMDGPUInstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
260 const SmallVectorImpl<MachineOperand> &Pred2)
261 const {
262 // TODO: Implement this function
263 return false;
264}
265
266bool AMDGPUInstrInfo::DefinesPredicate(MachineInstr *MI,
267 std::vector<MachineOperand> &Pred) const {
268 // TODO: Implement this function
269 return false;
270}
271
272bool AMDGPUInstrInfo::isPredicable(MachineInstr *MI) const {
273 // TODO: Implement this function
274 return MI->getDesc().isPredicable();
275}
276
277bool
278AMDGPUInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
279 // TODO: Implement this function
280 return true;
281}
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000282
283bool AMDGPUInstrInfo::isRegisterStore(const MachineInstr &MI) const {
284 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_STORE;
285}
286
287bool AMDGPUInstrInfo::isRegisterLoad(const MachineInstr &MI) const {
288 return get(MI.getOpcode()).TSFlags & AMDGPU_FLAG_REGISTER_LOAD;
289}
290
Tom Stellard81d871d2013-11-13 23:36:50 +0000291int AMDGPUInstrInfo::getIndirectIndexBegin(const MachineFunction &MF) const {
292 const MachineRegisterInfo &MRI = MF.getRegInfo();
293 const MachineFrameInfo *MFI = MF.getFrameInfo();
294 int Offset = -1;
295
296 if (MFI->getNumObjects() == 0) {
297 return -1;
298 }
299
300 if (MRI.livein_empty()) {
301 return 0;
302 }
303
304 const TargetRegisterClass *IndirectRC = getIndirectAddrRegClass();
305 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
306 LE = MRI.livein_end();
307 LI != LE; ++LI) {
308 unsigned Reg = LI->first;
309 if (TargetRegisterInfo::isVirtualRegister(Reg) ||
310 !IndirectRC->contains(Reg))
311 continue;
312
313 unsigned RegIndex;
314 unsigned RegEnd;
315 for (RegIndex = 0, RegEnd = IndirectRC->getNumRegs(); RegIndex != RegEnd;
316 ++RegIndex) {
317 if (IndirectRC->getRegister(RegIndex) == Reg)
318 break;
319 }
320 Offset = std::max(Offset, (int)RegIndex);
321 }
322
323 return Offset + 1;
324}
325
326int AMDGPUInstrInfo::getIndirectIndexEnd(const MachineFunction &MF) const {
327 int Offset = 0;
328 const MachineFrameInfo *MFI = MF.getFrameInfo();
329
330 // Variable sized objects are not supported
331 assert(!MFI->hasVarSizedObjects());
332
333 if (MFI->getNumObjects() == 0) {
334 return -1;
335 }
336
Eric Christopherd9134482014-08-04 21:25:23 +0000337 Offset = MF.getTarget()
338 .getSubtargetImpl()
339 ->getFrameLowering()
340 ->getFrameIndexOffset(MF, -1);
Tom Stellard81d871d2013-11-13 23:36:50 +0000341
342 return getIndirectIndexBegin(MF) + Offset;
343}
344
Tom Stellard682bfbc2013-10-10 17:11:24 +0000345int AMDGPUInstrInfo::getMaskedMIMGOp(uint16_t Opcode, unsigned Channels) const {
346 switch (Channels) {
347 default: return Opcode;
348 case 1: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_1);
349 case 2: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_2);
350 case 3: return AMDGPU::getMaskedMIMGOp(Opcode, AMDGPU::Channels_3);
351 }
352}
Tom Stellardc721a232014-05-16 20:56:47 +0000353
354// Wrapper for Tablegen'd function. enum Subtarget is not defined in any
355// header files, so we need to wrap it in a function that takes unsigned
356// instead.
357namespace llvm {
358namespace AMDGPU {
359int getMCOpcode(uint16_t Opcode, unsigned Gen) {
360 return getMCOpcode(Opcode);
361}
362}
363}