blob: 5f0bdf348153087114898a6c7ae9977ab3d7e0b0 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- R600InstrInfo.cpp - R600 Instruction Information ------------------===//
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 R600 Implementation of TargetInstrInfo.
12//
13//===----------------------------------------------------------------------===//
14
15#include "R600InstrInfo.h"
Vincent Lejeune3a8d78a2013-04-30 00:14:44 +000016#include "AMDGPU.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000017#include "AMDGPUSubtarget.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000018#include "AMDGPUTargetMachine.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "R600Defines.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000020#include "R600MachineFunctionInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "R600RegisterInfo.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025
Chandler Carruthd174b722014-04-22 02:03:14 +000026using namespace llvm;
27
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000028#define GET_INSTRINFO_CTOR_DTOR
Tom Stellard75aadc22012-12-11 21:25:42 +000029#include "AMDGPUGenDFAPacketizer.inc"
30
Tom Stellard2e59a452014-06-13 01:32:00 +000031R600InstrInfo::R600InstrInfo(const AMDGPUSubtarget &st)
Eric Christopher6c5b5112015-03-11 18:43:21 +000032 : AMDGPUInstrInfo(st), RI() {}
Tom Stellard75aadc22012-12-11 21:25:42 +000033
34const R600RegisterInfo &R600InstrInfo::getRegisterInfo() const {
35 return RI;
36}
37
38bool R600InstrInfo::isTrig(const MachineInstr &MI) const {
39 return get(MI.getOpcode()).TSFlags & R600_InstFlag::TRIG;
40}
41
42bool R600InstrInfo::isVector(const MachineInstr &MI) const {
43 return get(MI.getOpcode()).TSFlags & R600_InstFlag::VECTOR;
44}
45
46void
47R600InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
48 MachineBasicBlock::iterator MI, DebugLoc DL,
49 unsigned DestReg, unsigned SrcReg,
50 bool KillSrc) const {
Tom Stellard0344cdf2013-08-01 15:23:42 +000051 unsigned VectorComponents = 0;
Tom Stellard880a80a2014-06-17 16:53:14 +000052 if ((AMDGPU::R600_Reg128RegClass.contains(DestReg) ||
53 AMDGPU::R600_Reg128VerticalRegClass.contains(DestReg)) &&
54 (AMDGPU::R600_Reg128RegClass.contains(SrcReg) ||
55 AMDGPU::R600_Reg128VerticalRegClass.contains(SrcReg))) {
Tom Stellard0344cdf2013-08-01 15:23:42 +000056 VectorComponents = 4;
Tom Stellard880a80a2014-06-17 16:53:14 +000057 } else if((AMDGPU::R600_Reg64RegClass.contains(DestReg) ||
58 AMDGPU::R600_Reg64VerticalRegClass.contains(DestReg)) &&
59 (AMDGPU::R600_Reg64RegClass.contains(SrcReg) ||
60 AMDGPU::R600_Reg64VerticalRegClass.contains(SrcReg))) {
Tom Stellard0344cdf2013-08-01 15:23:42 +000061 VectorComponents = 2;
62 }
63
64 if (VectorComponents > 0) {
65 for (unsigned I = 0; I < VectorComponents; I++) {
Tom Stellard75aadc22012-12-11 21:25:42 +000066 unsigned SubRegIndex = RI.getSubRegFromChannel(I);
67 buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
68 RI.getSubReg(DestReg, SubRegIndex),
69 RI.getSubReg(SrcReg, SubRegIndex))
70 .addReg(DestReg,
71 RegState::Define | RegState::Implicit);
72 }
73 } else {
Tom Stellard75aadc22012-12-11 21:25:42 +000074 MachineInstr *NewMI = buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
75 DestReg, SrcReg);
Tom Stellard02661d92013-06-25 21:22:18 +000076 NewMI->getOperand(getOperandIdx(*NewMI, AMDGPU::OpName::src0))
Tom Stellard75aadc22012-12-11 21:25:42 +000077 .setIsKill(KillSrc);
78 }
79}
80
Tom Stellardcd6b0a62013-11-22 00:41:08 +000081/// \returns true if \p MBBI can be moved into a new basic.
82bool R600InstrInfo::isLegalToSplitMBBAt(MachineBasicBlock &MBB,
83 MachineBasicBlock::iterator MBBI) const {
84 for (MachineInstr::const_mop_iterator I = MBBI->operands_begin(),
85 E = MBBI->operands_end(); I != E; ++I) {
86 if (I->isReg() && !TargetRegisterInfo::isVirtualRegister(I->getReg()) &&
87 I->isUse() && RI.isPhysRegLiveAcrossClauses(I->getReg()))
88 return false;
89 }
90 return true;
91}
92
Tom Stellard75aadc22012-12-11 21:25:42 +000093bool R600InstrInfo::isMov(unsigned Opcode) const {
94
95
96 switch(Opcode) {
97 default: return false;
98 case AMDGPU::MOV:
99 case AMDGPU::MOV_IMM_F32:
100 case AMDGPU::MOV_IMM_I32:
101 return true;
102 }
103}
104
105// Some instructions act as place holders to emulate operations that the GPU
106// hardware does automatically. This function can be used to check if
107// an opcode falls into this category.
108bool R600InstrInfo::isPlaceHolderOpcode(unsigned Opcode) const {
109 switch (Opcode) {
110 default: return false;
111 case AMDGPU::RETURN:
Tom Stellard75aadc22012-12-11 21:25:42 +0000112 return true;
113 }
114}
115
116bool R600InstrInfo::isReductionOp(unsigned Opcode) const {
Aaron Ballmanf04bbd82013-07-10 17:19:22 +0000117 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000118}
119
120bool R600InstrInfo::isCubeOp(unsigned Opcode) const {
121 switch(Opcode) {
122 default: return false;
123 case AMDGPU::CUBE_r600_pseudo:
124 case AMDGPU::CUBE_r600_real:
125 case AMDGPU::CUBE_eg_pseudo:
126 case AMDGPU::CUBE_eg_real:
127 return true;
128 }
129}
130
131bool R600InstrInfo::isALUInstr(unsigned Opcode) const {
132 unsigned TargetFlags = get(Opcode).TSFlags;
133
Tom Stellard5eb903d2013-06-28 15:46:53 +0000134 return (TargetFlags & R600_InstFlag::ALU_INST);
Tom Stellard75aadc22012-12-11 21:25:42 +0000135}
136
Tom Stellardc026e8b2013-06-28 15:47:08 +0000137bool R600InstrInfo::hasInstrModifiers(unsigned Opcode) const {
138 unsigned TargetFlags = get(Opcode).TSFlags;
139
140 return ((TargetFlags & R600_InstFlag::OP1) |
141 (TargetFlags & R600_InstFlag::OP2) |
142 (TargetFlags & R600_InstFlag::OP3));
143}
144
145bool R600InstrInfo::isLDSInstr(unsigned Opcode) const {
146 unsigned TargetFlags = get(Opcode).TSFlags;
147
148 return ((TargetFlags & R600_InstFlag::LDS_1A) |
Tom Stellardf3d166a2013-08-26 15:05:49 +0000149 (TargetFlags & R600_InstFlag::LDS_1A1D) |
150 (TargetFlags & R600_InstFlag::LDS_1A2D));
Tom Stellardc026e8b2013-06-28 15:47:08 +0000151}
152
Tom Stellard8f9fc202013-11-15 00:12:45 +0000153bool R600InstrInfo::isLDSNoRetInstr(unsigned Opcode) const {
154 return isLDSInstr(Opcode) && getOperandIdx(Opcode, AMDGPU::OpName::dst) == -1;
155}
156
157bool R600InstrInfo::isLDSRetInstr(unsigned Opcode) const {
158 return isLDSInstr(Opcode) && getOperandIdx(Opcode, AMDGPU::OpName::dst) != -1;
159}
160
Vincent Lejeunea4da6fb2013-10-01 19:32:58 +0000161bool R600InstrInfo::canBeConsideredALU(const MachineInstr *MI) const {
162 if (isALUInstr(MI->getOpcode()))
163 return true;
164 if (isVector(*MI) || isCubeOp(MI->getOpcode()))
165 return true;
166 switch (MI->getOpcode()) {
167 case AMDGPU::PRED_X:
168 case AMDGPU::INTERP_PAIR_XY:
169 case AMDGPU::INTERP_PAIR_ZW:
170 case AMDGPU::INTERP_VEC_LOAD:
171 case AMDGPU::COPY:
172 case AMDGPU::DOT_4:
173 return true;
174 default:
175 return false;
176 }
177}
178
Vincent Lejeune076c0b22013-04-30 00:14:17 +0000179bool R600InstrInfo::isTransOnly(unsigned Opcode) const {
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +0000180 if (ST.hasCaymanISA())
181 return false;
182 return (get(Opcode).getSchedClass() == AMDGPU::Sched::TransALU);
Vincent Lejeune076c0b22013-04-30 00:14:17 +0000183}
184
185bool R600InstrInfo::isTransOnly(const MachineInstr *MI) const {
186 return isTransOnly(MI->getOpcode());
187}
188
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +0000189bool R600InstrInfo::isVectorOnly(unsigned Opcode) const {
190 return (get(Opcode).getSchedClass() == AMDGPU::Sched::VecALU);
191}
192
193bool R600InstrInfo::isVectorOnly(const MachineInstr *MI) const {
194 return isVectorOnly(MI->getOpcode());
195}
196
Tom Stellard676c16d2013-08-16 01:11:51 +0000197bool R600InstrInfo::isExport(unsigned Opcode) const {
198 return (get(Opcode).TSFlags & R600_InstFlag::IS_EXPORT);
199}
200
Vincent Lejeunec2991642013-04-30 00:13:39 +0000201bool R600InstrInfo::usesVertexCache(unsigned Opcode) const {
Tom Stellardd93cede2013-05-06 17:50:57 +0000202 return ST.hasVertexCache() && IS_VTX(get(Opcode));
Vincent Lejeunec2991642013-04-30 00:13:39 +0000203}
204
205bool R600InstrInfo::usesVertexCache(const MachineInstr *MI) const {
Matt Arsenault762af962014-07-13 03:06:39 +0000206 const MachineFunction *MF = MI->getParent()->getParent();
207 const R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
208 return MFI->getShaderType() != ShaderType::COMPUTE &&
209 usesVertexCache(MI->getOpcode());
Vincent Lejeunec2991642013-04-30 00:13:39 +0000210}
211
212bool R600InstrInfo::usesTextureCache(unsigned Opcode) const {
Tom Stellardd93cede2013-05-06 17:50:57 +0000213 return (!ST.hasVertexCache() && IS_VTX(get(Opcode))) || IS_TEX(get(Opcode));
Vincent Lejeunec2991642013-04-30 00:13:39 +0000214}
215
216bool R600InstrInfo::usesTextureCache(const MachineInstr *MI) const {
Matt Arsenault762af962014-07-13 03:06:39 +0000217 const MachineFunction *MF = MI->getParent()->getParent();
218 const R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
219 return (MFI->getShaderType() == ShaderType::COMPUTE &&
220 usesVertexCache(MI->getOpcode())) ||
221 usesTextureCache(MI->getOpcode());
Vincent Lejeunec2991642013-04-30 00:13:39 +0000222}
223
Tom Stellardce540332013-06-28 15:46:59 +0000224bool R600InstrInfo::mustBeLastInClause(unsigned Opcode) const {
225 switch (Opcode) {
226 case AMDGPU::KILLGT:
227 case AMDGPU::GROUP_BARRIER:
228 return true;
229 default:
230 return false;
231 }
232}
233
Tom Stellard26a3b672013-10-22 18:19:10 +0000234bool R600InstrInfo::usesAddressRegister(MachineInstr *MI) const {
235 return MI->findRegisterUseOperandIdx(AMDGPU::AR_X) != -1;
236}
237
238bool R600InstrInfo::definesAddressRegister(MachineInstr *MI) const {
239 return MI->findRegisterDefOperandIdx(AMDGPU::AR_X) != -1;
240}
241
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000242bool R600InstrInfo::readsLDSSrcReg(const MachineInstr *MI) const {
243 if (!isALUInstr(MI->getOpcode())) {
244 return false;
245 }
246 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
247 E = MI->operands_end(); I != E; ++I) {
248 if (!I->isReg() || !I->isUse() ||
249 TargetRegisterInfo::isVirtualRegister(I->getReg()))
250 continue;
251
252 if (AMDGPU::R600_LDS_SRC_REGRegClass.contains(I->getReg()))
253 return true;
254 }
255 return false;
256}
257
Tom Stellard84021442013-07-23 01:48:24 +0000258int R600InstrInfo::getSrcIdx(unsigned Opcode, unsigned SrcNum) const {
259 static const unsigned OpTable[] = {
260 AMDGPU::OpName::src0,
261 AMDGPU::OpName::src1,
262 AMDGPU::OpName::src2
263 };
264
265 assert (SrcNum < 3);
266 return getOperandIdx(Opcode, OpTable[SrcNum]);
267}
268
Tom Stellard84021442013-07-23 01:48:24 +0000269int R600InstrInfo::getSelIdx(unsigned Opcode, unsigned SrcIdx) const {
Jan Vesely468e0552015-03-02 18:56:52 +0000270 static const unsigned SrcSelTable[][2] = {
Tom Stellard84021442013-07-23 01:48:24 +0000271 {AMDGPU::OpName::src0, AMDGPU::OpName::src0_sel},
272 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_sel},
273 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_sel},
274 {AMDGPU::OpName::src0_X, AMDGPU::OpName::src0_sel_X},
275 {AMDGPU::OpName::src0_Y, AMDGPU::OpName::src0_sel_Y},
276 {AMDGPU::OpName::src0_Z, AMDGPU::OpName::src0_sel_Z},
277 {AMDGPU::OpName::src0_W, AMDGPU::OpName::src0_sel_W},
278 {AMDGPU::OpName::src1_X, AMDGPU::OpName::src1_sel_X},
279 {AMDGPU::OpName::src1_Y, AMDGPU::OpName::src1_sel_Y},
280 {AMDGPU::OpName::src1_Z, AMDGPU::OpName::src1_sel_Z},
281 {AMDGPU::OpName::src1_W, AMDGPU::OpName::src1_sel_W}
282 };
283
Jan Vesely468e0552015-03-02 18:56:52 +0000284 for (const auto &Row : SrcSelTable) {
285 if (getOperandIdx(Opcode, Row[0]) == (int)SrcIdx) {
286 return getOperandIdx(Opcode, Row[1]);
Tom Stellard84021442013-07-23 01:48:24 +0000287 }
288 }
289 return -1;
290}
Tom Stellard84021442013-07-23 01:48:24 +0000291
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000292SmallVector<std::pair<MachineOperand *, int64_t>, 3>
293R600InstrInfo::getSrcs(MachineInstr *MI) const {
294 SmallVector<std::pair<MachineOperand *, int64_t>, 3> Result;
295
Vincent Lejeunec6896792013-06-04 23:17:15 +0000296 if (MI->getOpcode() == AMDGPU::DOT_4) {
Tom Stellard02661d92013-06-25 21:22:18 +0000297 static const unsigned OpTable[8][2] = {
298 {AMDGPU::OpName::src0_X, AMDGPU::OpName::src0_sel_X},
299 {AMDGPU::OpName::src0_Y, AMDGPU::OpName::src0_sel_Y},
300 {AMDGPU::OpName::src0_Z, AMDGPU::OpName::src0_sel_Z},
301 {AMDGPU::OpName::src0_W, AMDGPU::OpName::src0_sel_W},
302 {AMDGPU::OpName::src1_X, AMDGPU::OpName::src1_sel_X},
303 {AMDGPU::OpName::src1_Y, AMDGPU::OpName::src1_sel_Y},
304 {AMDGPU::OpName::src1_Z, AMDGPU::OpName::src1_sel_Z},
305 {AMDGPU::OpName::src1_W, AMDGPU::OpName::src1_sel_W},
Vincent Lejeunec6896792013-06-04 23:17:15 +0000306 };
307
308 for (unsigned j = 0; j < 8; j++) {
Tom Stellard02661d92013-06-25 21:22:18 +0000309 MachineOperand &MO = MI->getOperand(getOperandIdx(MI->getOpcode(),
310 OpTable[j][0]));
Vincent Lejeunec6896792013-06-04 23:17:15 +0000311 unsigned Reg = MO.getReg();
312 if (Reg == AMDGPU::ALU_CONST) {
Tom Stellard02661d92013-06-25 21:22:18 +0000313 unsigned Sel = MI->getOperand(getOperandIdx(MI->getOpcode(),
314 OpTable[j][1])).getImm();
Vincent Lejeunec6896792013-06-04 23:17:15 +0000315 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Sel));
316 continue;
317 }
Matt Arsenault0163e032014-07-20 06:31:06 +0000318
Vincent Lejeunec6896792013-06-04 23:17:15 +0000319 }
320 return Result;
321 }
322
Tom Stellard02661d92013-06-25 21:22:18 +0000323 static const unsigned OpTable[3][2] = {
324 {AMDGPU::OpName::src0, AMDGPU::OpName::src0_sel},
325 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_sel},
326 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_sel},
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000327 };
328
329 for (unsigned j = 0; j < 3; j++) {
330 int SrcIdx = getOperandIdx(MI->getOpcode(), OpTable[j][0]);
331 if (SrcIdx < 0)
332 break;
333 MachineOperand &MO = MI->getOperand(SrcIdx);
334 unsigned Reg = MI->getOperand(SrcIdx).getReg();
335 if (Reg == AMDGPU::ALU_CONST) {
336 unsigned Sel = MI->getOperand(
337 getOperandIdx(MI->getOpcode(), OpTable[j][1])).getImm();
338 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Sel));
339 continue;
340 }
341 if (Reg == AMDGPU::ALU_LITERAL_X) {
342 unsigned Imm = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +0000343 getOperandIdx(MI->getOpcode(), AMDGPU::OpName::literal)).getImm();
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000344 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Imm));
345 continue;
346 }
347 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, 0));
348 }
349 return Result;
350}
351
352std::vector<std::pair<int, unsigned> >
353R600InstrInfo::ExtractSrcs(MachineInstr *MI,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000354 const DenseMap<unsigned, unsigned> &PV,
355 unsigned &ConstCount) const {
356 ConstCount = 0;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000357 const SmallVector<std::pair<MachineOperand *, int64_t>, 3> Srcs = getSrcs(MI);
358 const std::pair<int, unsigned> DummyPair(-1, 0);
359 std::vector<std::pair<int, unsigned> > Result;
360 unsigned i = 0;
361 for (unsigned n = Srcs.size(); i < n; ++i) {
362 unsigned Reg = Srcs[i].first->getReg();
363 unsigned Index = RI.getEncodingValue(Reg) & 0xff;
Tom Stellardc026e8b2013-06-28 15:47:08 +0000364 if (Reg == AMDGPU::OQAP) {
365 Result.push_back(std::pair<int, unsigned>(Index, 0));
366 }
Vincent Lejeune41d4cf22013-06-17 20:16:40 +0000367 if (PV.find(Reg) != PV.end()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000368 // 255 is used to tells its a PS/PV reg
369 Result.push_back(std::pair<int, unsigned>(255, 0));
370 continue;
371 }
372 if (Index > 127) {
373 ConstCount++;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000374 Result.push_back(DummyPair);
375 continue;
376 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000377 unsigned Chan = RI.getHWRegChan(Reg);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000378 Result.push_back(std::pair<int, unsigned>(Index, Chan));
379 }
380 for (; i < 3; ++i)
381 Result.push_back(DummyPair);
382 return Result;
383}
384
385static std::vector<std::pair<int, unsigned> >
386Swizzle(std::vector<std::pair<int, unsigned> > Src,
387 R600InstrInfo::BankSwizzle Swz) {
Vincent Lejeune744efa42013-09-04 19:53:54 +0000388 if (Src[0] == Src[1])
389 Src[1].first = -1;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000390 switch (Swz) {
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000391 case R600InstrInfo::ALU_VEC_012_SCL_210:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000392 break;
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000393 case R600InstrInfo::ALU_VEC_021_SCL_122:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000394 std::swap(Src[1], Src[2]);
395 break;
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000396 case R600InstrInfo::ALU_VEC_102_SCL_221:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000397 std::swap(Src[0], Src[1]);
398 break;
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000399 case R600InstrInfo::ALU_VEC_120_SCL_212:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000400 std::swap(Src[0], Src[1]);
401 std::swap(Src[0], Src[2]);
402 break;
403 case R600InstrInfo::ALU_VEC_201:
404 std::swap(Src[0], Src[2]);
405 std::swap(Src[0], Src[1]);
406 break;
407 case R600InstrInfo::ALU_VEC_210:
408 std::swap(Src[0], Src[2]);
409 break;
410 }
411 return Src;
412}
413
Vincent Lejeune77a83522013-06-29 19:32:43 +0000414static unsigned
415getTransSwizzle(R600InstrInfo::BankSwizzle Swz, unsigned Op) {
416 switch (Swz) {
417 case R600InstrInfo::ALU_VEC_012_SCL_210: {
418 unsigned Cycles[3] = { 2, 1, 0};
419 return Cycles[Op];
420 }
421 case R600InstrInfo::ALU_VEC_021_SCL_122: {
422 unsigned Cycles[3] = { 1, 2, 2};
423 return Cycles[Op];
424 }
425 case R600InstrInfo::ALU_VEC_120_SCL_212: {
426 unsigned Cycles[3] = { 2, 1, 2};
427 return Cycles[Op];
428 }
429 case R600InstrInfo::ALU_VEC_102_SCL_221: {
430 unsigned Cycles[3] = { 2, 2, 1};
431 return Cycles[Op];
432 }
433 default:
434 llvm_unreachable("Wrong Swizzle for Trans Slot");
435 return 0;
436 }
437}
438
439/// returns how many MIs (whose inputs are represented by IGSrcs) can be packed
440/// in the same Instruction Group while meeting read port limitations given a
441/// Swz swizzle sequence.
442unsigned R600InstrInfo::isLegalUpTo(
443 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
444 const std::vector<R600InstrInfo::BankSwizzle> &Swz,
445 const std::vector<std::pair<int, unsigned> > &TransSrcs,
446 R600InstrInfo::BankSwizzle TransSwz) const {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000447 int Vector[4][3];
448 memset(Vector, -1, sizeof(Vector));
Vincent Lejeune77a83522013-06-29 19:32:43 +0000449 for (unsigned i = 0, e = IGSrcs.size(); i < e; i++) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000450 const std::vector<std::pair<int, unsigned> > &Srcs =
451 Swizzle(IGSrcs[i], Swz[i]);
452 for (unsigned j = 0; j < 3; j++) {
453 const std::pair<int, unsigned> &Src = Srcs[j];
Vincent Lejeune77a83522013-06-29 19:32:43 +0000454 if (Src.first < 0 || Src.first == 255)
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000455 continue;
Tom Stellardc026e8b2013-06-28 15:47:08 +0000456 if (Src.first == GET_REG_INDEX(RI.getEncodingValue(AMDGPU::OQAP))) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000457 if (Swz[i] != R600InstrInfo::ALU_VEC_012_SCL_210 &&
458 Swz[i] != R600InstrInfo::ALU_VEC_021_SCL_122) {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000459 // The value from output queue A (denoted by register OQAP) can
460 // only be fetched during the first cycle.
461 return false;
462 }
463 // OQAP does not count towards the normal read port restrictions
464 continue;
465 }
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000466 if (Vector[Src.second][j] < 0)
467 Vector[Src.second][j] = Src.first;
468 if (Vector[Src.second][j] != Src.first)
Vincent Lejeune77a83522013-06-29 19:32:43 +0000469 return i;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000470 }
471 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000472 // Now check Trans Alu
473 for (unsigned i = 0, e = TransSrcs.size(); i < e; ++i) {
474 const std::pair<int, unsigned> &Src = TransSrcs[i];
475 unsigned Cycle = getTransSwizzle(TransSwz, i);
476 if (Src.first < 0)
477 continue;
478 if (Src.first == 255)
479 continue;
480 if (Vector[Src.second][Cycle] < 0)
481 Vector[Src.second][Cycle] = Src.first;
482 if (Vector[Src.second][Cycle] != Src.first)
483 return IGSrcs.size() - 1;
484 }
485 return IGSrcs.size();
486}
487
488/// Given a swizzle sequence SwzCandidate and an index Idx, returns the next
489/// (in lexicographic term) swizzle sequence assuming that all swizzles after
490/// Idx can be skipped
491static bool
492NextPossibleSolution(
493 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
494 unsigned Idx) {
495 assert(Idx < SwzCandidate.size());
496 int ResetIdx = Idx;
497 while (ResetIdx > -1 && SwzCandidate[ResetIdx] == R600InstrInfo::ALU_VEC_210)
498 ResetIdx --;
499 for (unsigned i = ResetIdx + 1, e = SwzCandidate.size(); i < e; i++) {
500 SwzCandidate[i] = R600InstrInfo::ALU_VEC_012_SCL_210;
501 }
502 if (ResetIdx == -1)
503 return false;
Benjamin Kramer39690642013-06-29 20:04:19 +0000504 int NextSwizzle = SwzCandidate[ResetIdx] + 1;
505 SwzCandidate[ResetIdx] = (R600InstrInfo::BankSwizzle)NextSwizzle;
Vincent Lejeune77a83522013-06-29 19:32:43 +0000506 return true;
507}
508
509/// Enumerate all possible Swizzle sequence to find one that can meet all
510/// read port requirements.
511bool R600InstrInfo::FindSwizzleForVectorSlot(
512 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
513 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
514 const std::vector<std::pair<int, unsigned> > &TransSrcs,
515 R600InstrInfo::BankSwizzle TransSwz) const {
516 unsigned ValidUpTo = 0;
517 do {
518 ValidUpTo = isLegalUpTo(IGSrcs, SwzCandidate, TransSrcs, TransSwz);
519 if (ValidUpTo == IGSrcs.size())
520 return true;
521 } while (NextPossibleSolution(SwzCandidate, ValidUpTo));
522 return false;
523}
524
525/// Instructions in Trans slot can't read gpr at cycle 0 if they also read
526/// a const, and can't read a gpr at cycle 1 if they read 2 const.
527static bool
528isConstCompatible(R600InstrInfo::BankSwizzle TransSwz,
529 const std::vector<std::pair<int, unsigned> > &TransOps,
530 unsigned ConstCount) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000531 // TransALU can't read 3 constants
532 if (ConstCount > 2)
533 return false;
Vincent Lejeune77a83522013-06-29 19:32:43 +0000534 for (unsigned i = 0, e = TransOps.size(); i < e; ++i) {
535 const std::pair<int, unsigned> &Src = TransOps[i];
536 unsigned Cycle = getTransSwizzle(TransSwz, i);
537 if (Src.first < 0)
538 continue;
539 if (ConstCount > 0 && Cycle == 0)
540 return false;
541 if (ConstCount > 1 && Cycle == 1)
542 return false;
543 }
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000544 return true;
545}
546
Tom Stellardc026e8b2013-06-28 15:47:08 +0000547bool
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000548R600InstrInfo::fitsReadPortLimitations(const std::vector<MachineInstr *> &IG,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000549 const DenseMap<unsigned, unsigned> &PV,
550 std::vector<BankSwizzle> &ValidSwizzle,
551 bool isLastAluTrans)
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000552 const {
553 //Todo : support shared src0 - src1 operand
554
555 std::vector<std::vector<std::pair<int, unsigned> > > IGSrcs;
556 ValidSwizzle.clear();
Vincent Lejeune77a83522013-06-29 19:32:43 +0000557 unsigned ConstCount;
Vincent Lejeunea8a50242013-06-30 21:44:06 +0000558 BankSwizzle TransBS = ALU_VEC_012_SCL_210;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000559 for (unsigned i = 0, e = IG.size(); i < e; ++i) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000560 IGSrcs.push_back(ExtractSrcs(IG[i], PV, ConstCount));
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000561 unsigned Op = getOperandIdx(IG[i]->getOpcode(),
Tom Stellard02661d92013-06-25 21:22:18 +0000562 AMDGPU::OpName::bank_swizzle);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000563 ValidSwizzle.push_back( (R600InstrInfo::BankSwizzle)
564 IG[i]->getOperand(Op).getImm());
565 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000566 std::vector<std::pair<int, unsigned> > TransOps;
567 if (!isLastAluTrans)
568 return FindSwizzleForVectorSlot(IGSrcs, ValidSwizzle, TransOps, TransBS);
569
Benjamin Kramere12a6ba2014-10-03 18:33:16 +0000570 TransOps = std::move(IGSrcs.back());
Vincent Lejeune77a83522013-06-29 19:32:43 +0000571 IGSrcs.pop_back();
572 ValidSwizzle.pop_back();
573
574 static const R600InstrInfo::BankSwizzle TransSwz[] = {
575 ALU_VEC_012_SCL_210,
576 ALU_VEC_021_SCL_122,
577 ALU_VEC_120_SCL_212,
578 ALU_VEC_102_SCL_221
579 };
580 for (unsigned i = 0; i < 4; i++) {
581 TransBS = TransSwz[i];
582 if (!isConstCompatible(TransBS, TransOps, ConstCount))
583 continue;
584 bool Result = FindSwizzleForVectorSlot(IGSrcs, ValidSwizzle, TransOps,
585 TransBS);
586 if (Result) {
587 ValidSwizzle.push_back(TransBS);
588 return true;
589 }
590 }
591
592 return false;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000593}
594
595
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000596bool
597R600InstrInfo::fitsConstReadLimitations(const std::vector<unsigned> &Consts)
598 const {
599 assert (Consts.size() <= 12 && "Too many operands in instructions group");
600 unsigned Pair1 = 0, Pair2 = 0;
601 for (unsigned i = 0, n = Consts.size(); i < n; ++i) {
602 unsigned ReadConstHalf = Consts[i] & 2;
603 unsigned ReadConstIndex = Consts[i] & (~3);
604 unsigned ReadHalfConst = ReadConstIndex | ReadConstHalf;
605 if (!Pair1) {
606 Pair1 = ReadHalfConst;
607 continue;
608 }
609 if (Pair1 == ReadHalfConst)
610 continue;
611 if (!Pair2) {
612 Pair2 = ReadHalfConst;
613 continue;
614 }
615 if (Pair2 != ReadHalfConst)
616 return false;
617 }
618 return true;
619}
620
621bool
Vincent Lejeune77a83522013-06-29 19:32:43 +0000622R600InstrInfo::fitsConstReadLimitations(const std::vector<MachineInstr *> &MIs)
623 const {
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000624 std::vector<unsigned> Consts;
Vincent Lejeunebb3f9312013-07-31 19:32:07 +0000625 SmallSet<int64_t, 4> Literals;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000626 for (unsigned i = 0, n = MIs.size(); i < n; i++) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000627 MachineInstr *MI = MIs[i];
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000628 if (!isALUInstr(MI->getOpcode()))
629 continue;
630
Craig Topperb94011f2013-07-14 04:42:23 +0000631 const SmallVectorImpl<std::pair<MachineOperand *, int64_t> > &Srcs =
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000632 getSrcs(MI);
633
634 for (unsigned j = 0, e = Srcs.size(); j < e; j++) {
635 std::pair<MachineOperand *, unsigned> Src = Srcs[j];
Vincent Lejeunebb3f9312013-07-31 19:32:07 +0000636 if (Src.first->getReg() == AMDGPU::ALU_LITERAL_X)
637 Literals.insert(Src.second);
638 if (Literals.size() > 4)
639 return false;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000640 if (Src.first->getReg() == AMDGPU::ALU_CONST)
641 Consts.push_back(Src.second);
642 if (AMDGPU::R600_KC0RegClass.contains(Src.first->getReg()) ||
643 AMDGPU::R600_KC1RegClass.contains(Src.first->getReg())) {
644 unsigned Index = RI.getEncodingValue(Src.first->getReg()) & 0xff;
645 unsigned Chan = RI.getHWRegChan(Src.first->getReg());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000646 Consts.push_back((Index << 2) | Chan);
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000647 }
648 }
649 }
650 return fitsConstReadLimitations(Consts);
651}
652
Eric Christopher143f02c2014-10-09 01:59:35 +0000653DFAPacketizer *
654R600InstrInfo::CreateTargetScheduleState(const TargetSubtargetInfo &STI) const {
655 const InstrItineraryData *II = STI.getInstrItineraryData();
656 return static_cast<const AMDGPUSubtarget &>(STI).createDFAPacketizer(II);
Tom Stellard75aadc22012-12-11 21:25:42 +0000657}
658
659static bool
660isPredicateSetter(unsigned Opcode) {
661 switch (Opcode) {
662 case AMDGPU::PRED_X:
663 return true;
664 default:
665 return false;
666 }
667}
668
669static MachineInstr *
670findFirstPredicateSetterFrom(MachineBasicBlock &MBB,
671 MachineBasicBlock::iterator I) {
672 while (I != MBB.begin()) {
673 --I;
674 MachineInstr *MI = I;
675 if (isPredicateSetter(MI->getOpcode()))
676 return MI;
677 }
678
Craig Topper062a2ba2014-04-25 05:30:21 +0000679 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +0000680}
681
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000682static
683bool isJump(unsigned Opcode) {
684 return Opcode == AMDGPU::JUMP || Opcode == AMDGPU::JUMP_COND;
685}
686
Vincent Lejeune269708b2013-10-01 19:32:38 +0000687static bool isBranch(unsigned Opcode) {
688 return Opcode == AMDGPU::BRANCH || Opcode == AMDGPU::BRANCH_COND_i32 ||
689 Opcode == AMDGPU::BRANCH_COND_f32;
690}
691
Tom Stellard75aadc22012-12-11 21:25:42 +0000692bool
693R600InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
694 MachineBasicBlock *&TBB,
695 MachineBasicBlock *&FBB,
696 SmallVectorImpl<MachineOperand> &Cond,
697 bool AllowModify) const {
698 // Most of the following comes from the ARM implementation of AnalyzeBranch
699
700 // If the block has no terminators, it just falls into the block after it.
701 MachineBasicBlock::iterator I = MBB.end();
702 if (I == MBB.begin())
703 return false;
704 --I;
705 while (I->isDebugValue()) {
706 if (I == MBB.begin())
707 return false;
708 --I;
709 }
Vincent Lejeune269708b2013-10-01 19:32:38 +0000710 // AMDGPU::BRANCH* instructions are only available after isel and are not
711 // handled
712 if (isBranch(I->getOpcode()))
713 return true;
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000714 if (!isJump(static_cast<MachineInstr *>(I)->getOpcode())) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000715 return false;
716 }
717
Tom Stellarda64353e2014-01-23 18:49:34 +0000718 // Remove successive JUMP
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000719 while (I != MBB.begin() && std::prev(I)->getOpcode() == AMDGPU::JUMP) {
720 MachineBasicBlock::iterator PriorI = std::prev(I);
Tom Stellarda64353e2014-01-23 18:49:34 +0000721 if (AllowModify)
722 I->removeFromParent();
723 I = PriorI;
724 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000725 MachineInstr *LastInst = I;
726
727 // If there is only one terminator instruction, process it.
728 unsigned LastOpc = LastInst->getOpcode();
729 if (I == MBB.begin() ||
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000730 !isJump(static_cast<MachineInstr *>(--I)->getOpcode())) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000731 if (LastOpc == AMDGPU::JUMP) {
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000732 TBB = LastInst->getOperand(0).getMBB();
733 return false;
734 } else if (LastOpc == AMDGPU::JUMP_COND) {
735 MachineInstr *predSet = I;
736 while (!isPredicateSetter(predSet->getOpcode())) {
737 predSet = --I;
Tom Stellard75aadc22012-12-11 21:25:42 +0000738 }
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000739 TBB = LastInst->getOperand(0).getMBB();
740 Cond.push_back(predSet->getOperand(1));
741 Cond.push_back(predSet->getOperand(2));
742 Cond.push_back(MachineOperand::CreateReg(AMDGPU::PRED_SEL_ONE, false));
743 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000744 }
745 return true; // Can't handle indirect branch.
746 }
747
748 // Get the instruction before it if it is a terminator.
749 MachineInstr *SecondLastInst = I;
750 unsigned SecondLastOpc = SecondLastInst->getOpcode();
751
752 // If the block ends with a B and a Bcc, handle it.
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000753 if (SecondLastOpc == AMDGPU::JUMP_COND && LastOpc == AMDGPU::JUMP) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000754 MachineInstr *predSet = --I;
755 while (!isPredicateSetter(predSet->getOpcode())) {
756 predSet = --I;
757 }
758 TBB = SecondLastInst->getOperand(0).getMBB();
759 FBB = LastInst->getOperand(0).getMBB();
760 Cond.push_back(predSet->getOperand(1));
761 Cond.push_back(predSet->getOperand(2));
762 Cond.push_back(MachineOperand::CreateReg(AMDGPU::PRED_SEL_ONE, false));
763 return false;
764 }
765
766 // Otherwise, can't handle this.
767 return true;
768}
769
Vincent Lejeunece499742013-07-09 15:03:33 +0000770static
771MachineBasicBlock::iterator FindLastAluClause(MachineBasicBlock &MBB) {
772 for (MachineBasicBlock::reverse_iterator It = MBB.rbegin(), E = MBB.rend();
773 It != E; ++It) {
774 if (It->getOpcode() == AMDGPU::CF_ALU ||
775 It->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE)
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000776 return std::prev(It.base());
Vincent Lejeunece499742013-07-09 15:03:33 +0000777 }
778 return MBB.end();
779}
780
Tom Stellard75aadc22012-12-11 21:25:42 +0000781unsigned
782R600InstrInfo::InsertBranch(MachineBasicBlock &MBB,
783 MachineBasicBlock *TBB,
784 MachineBasicBlock *FBB,
785 const SmallVectorImpl<MachineOperand> &Cond,
786 DebugLoc DL) const {
787 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
788
Craig Topper062a2ba2014-04-25 05:30:21 +0000789 if (!FBB) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000790 if (Cond.empty()) {
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000791 BuildMI(&MBB, DL, get(AMDGPU::JUMP)).addMBB(TBB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000792 return 1;
793 } else {
794 MachineInstr *PredSet = findFirstPredicateSetterFrom(MBB, MBB.end());
795 assert(PredSet && "No previous predicate !");
796 addFlag(PredSet, 0, MO_FLAG_PUSH);
797 PredSet->getOperand(2).setImm(Cond[1].getImm());
798
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000799 BuildMI(&MBB, DL, get(AMDGPU::JUMP_COND))
Tom Stellard75aadc22012-12-11 21:25:42 +0000800 .addMBB(TBB)
801 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Vincent Lejeunece499742013-07-09 15:03:33 +0000802 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
803 if (CfAlu == MBB.end())
804 return 1;
805 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU);
806 CfAlu->setDesc(get(AMDGPU::CF_ALU_PUSH_BEFORE));
Tom Stellard75aadc22012-12-11 21:25:42 +0000807 return 1;
808 }
809 } else {
810 MachineInstr *PredSet = findFirstPredicateSetterFrom(MBB, MBB.end());
811 assert(PredSet && "No previous predicate !");
812 addFlag(PredSet, 0, MO_FLAG_PUSH);
813 PredSet->getOperand(2).setImm(Cond[1].getImm());
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000814 BuildMI(&MBB, DL, get(AMDGPU::JUMP_COND))
Tom Stellard75aadc22012-12-11 21:25:42 +0000815 .addMBB(TBB)
816 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000817 BuildMI(&MBB, DL, get(AMDGPU::JUMP)).addMBB(FBB);
Vincent Lejeunece499742013-07-09 15:03:33 +0000818 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
819 if (CfAlu == MBB.end())
820 return 2;
821 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU);
822 CfAlu->setDesc(get(AMDGPU::CF_ALU_PUSH_BEFORE));
Tom Stellard75aadc22012-12-11 21:25:42 +0000823 return 2;
824 }
825}
826
827unsigned
828R600InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
829
830 // Note : we leave PRED* instructions there.
831 // They may be needed when predicating instructions.
832
833 MachineBasicBlock::iterator I = MBB.end();
834
835 if (I == MBB.begin()) {
836 return 0;
837 }
838 --I;
839 switch (I->getOpcode()) {
840 default:
841 return 0;
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000842 case AMDGPU::JUMP_COND: {
843 MachineInstr *predSet = findFirstPredicateSetterFrom(MBB, I);
844 clearFlag(predSet, 0, MO_FLAG_PUSH);
845 I->eraseFromParent();
Vincent Lejeunece499742013-07-09 15:03:33 +0000846 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
847 if (CfAlu == MBB.end())
848 break;
849 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE);
850 CfAlu->setDesc(get(AMDGPU::CF_ALU));
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000851 break;
852 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000853 case AMDGPU::JUMP:
Tom Stellard75aadc22012-12-11 21:25:42 +0000854 I->eraseFromParent();
855 break;
856 }
857 I = MBB.end();
858
859 if (I == MBB.begin()) {
860 return 1;
861 }
862 --I;
863 switch (I->getOpcode()) {
864 // FIXME: only one case??
865 default:
866 return 1;
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000867 case AMDGPU::JUMP_COND: {
868 MachineInstr *predSet = findFirstPredicateSetterFrom(MBB, I);
869 clearFlag(predSet, 0, MO_FLAG_PUSH);
870 I->eraseFromParent();
Vincent Lejeunece499742013-07-09 15:03:33 +0000871 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
872 if (CfAlu == MBB.end())
873 break;
874 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE);
875 CfAlu->setDesc(get(AMDGPU::CF_ALU));
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000876 break;
877 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000878 case AMDGPU::JUMP:
Tom Stellard75aadc22012-12-11 21:25:42 +0000879 I->eraseFromParent();
880 break;
881 }
882 return 2;
883}
884
885bool
886R600InstrInfo::isPredicated(const MachineInstr *MI) const {
887 int idx = MI->findFirstPredOperandIdx();
888 if (idx < 0)
889 return false;
890
891 unsigned Reg = MI->getOperand(idx).getReg();
892 switch (Reg) {
893 default: return false;
894 case AMDGPU::PRED_SEL_ONE:
895 case AMDGPU::PRED_SEL_ZERO:
896 case AMDGPU::PREDICATE_BIT:
897 return true;
898 }
899}
900
901bool
902R600InstrInfo::isPredicable(MachineInstr *MI) const {
903 // XXX: KILL* instructions can be predicated, but they must be the last
904 // instruction in a clause, so this means any instructions after them cannot
905 // be predicated. Until we have proper support for instruction clauses in the
906 // backend, we will mark KILL* instructions as unpredicable.
907
908 if (MI->getOpcode() == AMDGPU::KILLGT) {
909 return false;
Vincent Lejeunece499742013-07-09 15:03:33 +0000910 } else if (MI->getOpcode() == AMDGPU::CF_ALU) {
911 // If the clause start in the middle of MBB then the MBB has more
912 // than a single clause, unable to predicate several clauses.
913 if (MI->getParent()->begin() != MachineBasicBlock::iterator(MI))
914 return false;
915 // TODO: We don't support KC merging atm
916 if (MI->getOperand(3).getImm() != 0 || MI->getOperand(4).getImm() != 0)
917 return false;
918 return true;
Vincent Lejeunefe32bd82013-03-05 19:12:06 +0000919 } else if (isVector(*MI)) {
920 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000921 } else {
922 return AMDGPUInstrInfo::isPredicable(MI);
923 }
924}
925
926
927bool
928R600InstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
929 unsigned NumCyles,
930 unsigned ExtraPredCycles,
931 const BranchProbability &Probability) const{
932 return true;
933}
934
935bool
936R600InstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
937 unsigned NumTCycles,
938 unsigned ExtraTCycles,
939 MachineBasicBlock &FMBB,
940 unsigned NumFCycles,
941 unsigned ExtraFCycles,
942 const BranchProbability &Probability) const {
943 return true;
944}
945
946bool
947R600InstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
948 unsigned NumCyles,
949 const BranchProbability &Probability)
950 const {
951 return true;
952}
953
954bool
955R600InstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
956 MachineBasicBlock &FMBB) const {
957 return false;
958}
959
960
961bool
962R600InstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
963 MachineOperand &MO = Cond[1];
964 switch (MO.getImm()) {
965 case OPCODE_IS_ZERO_INT:
966 MO.setImm(OPCODE_IS_NOT_ZERO_INT);
967 break;
968 case OPCODE_IS_NOT_ZERO_INT:
969 MO.setImm(OPCODE_IS_ZERO_INT);
970 break;
971 case OPCODE_IS_ZERO:
972 MO.setImm(OPCODE_IS_NOT_ZERO);
973 break;
974 case OPCODE_IS_NOT_ZERO:
975 MO.setImm(OPCODE_IS_ZERO);
976 break;
977 default:
978 return true;
979 }
980
981 MachineOperand &MO2 = Cond[2];
982 switch (MO2.getReg()) {
983 case AMDGPU::PRED_SEL_ZERO:
984 MO2.setReg(AMDGPU::PRED_SEL_ONE);
985 break;
986 case AMDGPU::PRED_SEL_ONE:
987 MO2.setReg(AMDGPU::PRED_SEL_ZERO);
988 break;
989 default:
990 return true;
991 }
992 return false;
993}
994
995bool
996R600InstrInfo::DefinesPredicate(MachineInstr *MI,
997 std::vector<MachineOperand> &Pred) const {
998 return isPredicateSetter(MI->getOpcode());
999}
1000
1001
1002bool
1003R600InstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1004 const SmallVectorImpl<MachineOperand> &Pred2) const {
1005 return false;
1006}
1007
1008
1009bool
1010R600InstrInfo::PredicateInstruction(MachineInstr *MI,
1011 const SmallVectorImpl<MachineOperand> &Pred) const {
1012 int PIdx = MI->findFirstPredOperandIdx();
1013
Vincent Lejeunece499742013-07-09 15:03:33 +00001014 if (MI->getOpcode() == AMDGPU::CF_ALU) {
1015 MI->getOperand(8).setImm(0);
1016 return true;
1017 }
1018
Vincent Lejeune745d4292013-11-16 16:24:41 +00001019 if (MI->getOpcode() == AMDGPU::DOT_4) {
1020 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_X))
1021 .setReg(Pred[2].getReg());
1022 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_Y))
1023 .setReg(Pred[2].getReg());
1024 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_Z))
1025 .setReg(Pred[2].getReg());
1026 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_W))
1027 .setReg(Pred[2].getReg());
1028 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1029 MIB.addReg(AMDGPU::PREDICATE_BIT, RegState::Implicit);
1030 return true;
1031 }
1032
Tom Stellard75aadc22012-12-11 21:25:42 +00001033 if (PIdx != -1) {
1034 MachineOperand &PMO = MI->getOperand(PIdx);
1035 PMO.setReg(Pred[2].getReg());
NAKAMURA Takumi2a0b40f2012-12-20 00:22:11 +00001036 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1037 MIB.addReg(AMDGPU::PREDICATE_BIT, RegState::Implicit);
Tom Stellard75aadc22012-12-11 21:25:42 +00001038 return true;
1039 }
1040
1041 return false;
1042}
1043
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00001044unsigned int R600InstrInfo::getPredicationCost(const MachineInstr *) const {
1045 return 2;
1046}
1047
Tom Stellard75aadc22012-12-11 21:25:42 +00001048unsigned int R600InstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1049 const MachineInstr *MI,
1050 unsigned *PredCost) const {
1051 if (PredCost)
1052 *PredCost = 2;
1053 return 2;
1054}
1055
Tom Stellard880a80a2014-06-17 16:53:14 +00001056bool R600InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1057
1058 switch(MI->getOpcode()) {
1059 default: return AMDGPUInstrInfo::expandPostRAPseudo(MI);
1060 case AMDGPU::R600_EXTRACT_ELT_V2:
1061 case AMDGPU::R600_EXTRACT_ELT_V4:
1062 buildIndirectRead(MI->getParent(), MI, MI->getOperand(0).getReg(),
1063 RI.getHWRegIndex(MI->getOperand(1).getReg()), // Address
1064 MI->getOperand(2).getReg(),
1065 RI.getHWRegChan(MI->getOperand(1).getReg()));
1066 break;
1067 case AMDGPU::R600_INSERT_ELT_V2:
1068 case AMDGPU::R600_INSERT_ELT_V4:
1069 buildIndirectWrite(MI->getParent(), MI, MI->getOperand(2).getReg(), // Value
1070 RI.getHWRegIndex(MI->getOperand(1).getReg()), // Address
1071 MI->getOperand(3).getReg(), // Offset
1072 RI.getHWRegChan(MI->getOperand(1).getReg())); // Channel
1073 break;
1074 }
1075 MI->eraseFromParent();
1076 return true;
1077}
1078
Tom Stellard81d871d2013-11-13 23:36:50 +00001079void R600InstrInfo::reserveIndirectRegisters(BitVector &Reserved,
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001080 const MachineFunction &MF) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001081 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001082 MF.getSubtarget().getFrameLowering());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001083
1084 unsigned StackWidth = TFL->getStackWidth(MF);
1085 int End = getIndirectIndexEnd(MF);
1086
Tom Stellard81d871d2013-11-13 23:36:50 +00001087 if (End == -1)
1088 return;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001089
1090 for (int Index = getIndirectIndexBegin(MF); Index <= End; ++Index) {
1091 unsigned SuperReg = AMDGPU::R600_Reg128RegClass.getRegister(Index);
Tom Stellard81d871d2013-11-13 23:36:50 +00001092 Reserved.set(SuperReg);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001093 for (unsigned Chan = 0; Chan < StackWidth; ++Chan) {
1094 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister((4 * Index) + Chan);
Tom Stellard81d871d2013-11-13 23:36:50 +00001095 Reserved.set(Reg);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001096 }
1097 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001098}
1099
1100unsigned R600InstrInfo::calculateIndirectAddress(unsigned RegIndex,
1101 unsigned Channel) const {
1102 // XXX: Remove when we support a stack width > 2
1103 assert(Channel == 0);
1104 return RegIndex;
1105}
1106
Tom Stellard26a3b672013-10-22 18:19:10 +00001107const TargetRegisterClass *R600InstrInfo::getIndirectAddrRegClass() const {
1108 return &AMDGPU::R600_TReg32_XRegClass;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001109}
1110
1111MachineInstrBuilder R600InstrInfo::buildIndirectWrite(MachineBasicBlock *MBB,
1112 MachineBasicBlock::iterator I,
1113 unsigned ValueReg, unsigned Address,
1114 unsigned OffsetReg) const {
Tom Stellard880a80a2014-06-17 16:53:14 +00001115 return buildIndirectWrite(MBB, I, ValueReg, Address, OffsetReg, 0);
1116}
1117
1118MachineInstrBuilder R600InstrInfo::buildIndirectWrite(MachineBasicBlock *MBB,
1119 MachineBasicBlock::iterator I,
1120 unsigned ValueReg, unsigned Address,
1121 unsigned OffsetReg,
1122 unsigned AddrChan) const {
1123 unsigned AddrReg;
1124 switch (AddrChan) {
1125 default: llvm_unreachable("Invalid Channel");
1126 case 0: AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address); break;
1127 case 1: AddrReg = AMDGPU::R600_Addr_YRegClass.getRegister(Address); break;
1128 case 2: AddrReg = AMDGPU::R600_Addr_ZRegClass.getRegister(Address); break;
1129 case 3: AddrReg = AMDGPU::R600_Addr_WRegClass.getRegister(Address); break;
1130 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001131 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1132 AMDGPU::AR_X, OffsetReg);
Tom Stellard02661d92013-06-25 21:22:18 +00001133 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001134
1135 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1136 AddrReg, ValueReg)
Tom Stellardaad53762013-06-05 03:43:06 +00001137 .addReg(AMDGPU::AR_X,
1138 RegState::Implicit | RegState::Kill);
Tom Stellard02661d92013-06-25 21:22:18 +00001139 setImmOperand(Mov, AMDGPU::OpName::dst_rel, 1);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001140 return Mov;
1141}
1142
1143MachineInstrBuilder R600InstrInfo::buildIndirectRead(MachineBasicBlock *MBB,
1144 MachineBasicBlock::iterator I,
1145 unsigned ValueReg, unsigned Address,
1146 unsigned OffsetReg) const {
Tom Stellard880a80a2014-06-17 16:53:14 +00001147 return buildIndirectRead(MBB, I, ValueReg, Address, OffsetReg, 0);
1148}
1149
1150MachineInstrBuilder R600InstrInfo::buildIndirectRead(MachineBasicBlock *MBB,
1151 MachineBasicBlock::iterator I,
1152 unsigned ValueReg, unsigned Address,
1153 unsigned OffsetReg,
1154 unsigned AddrChan) const {
1155 unsigned AddrReg;
1156 switch (AddrChan) {
1157 default: llvm_unreachable("Invalid Channel");
1158 case 0: AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address); break;
1159 case 1: AddrReg = AMDGPU::R600_Addr_YRegClass.getRegister(Address); break;
1160 case 2: AddrReg = AMDGPU::R600_Addr_ZRegClass.getRegister(Address); break;
1161 case 3: AddrReg = AMDGPU::R600_Addr_WRegClass.getRegister(Address); break;
1162 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001163 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1164 AMDGPU::AR_X,
1165 OffsetReg);
Tom Stellard02661d92013-06-25 21:22:18 +00001166 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001167 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1168 ValueReg,
1169 AddrReg)
Tom Stellardaad53762013-06-05 03:43:06 +00001170 .addReg(AMDGPU::AR_X,
1171 RegState::Implicit | RegState::Kill);
Tom Stellard02661d92013-06-25 21:22:18 +00001172 setImmOperand(Mov, AMDGPU::OpName::src0_rel, 1);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001173
1174 return Mov;
1175}
1176
Vincent Lejeune80031d9f2013-04-03 16:49:34 +00001177unsigned R600InstrInfo::getMaxAlusPerClause() const {
1178 return 115;
1179}
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001180
Tom Stellard75aadc22012-12-11 21:25:42 +00001181MachineInstrBuilder R600InstrInfo::buildDefaultInstruction(MachineBasicBlock &MBB,
1182 MachineBasicBlock::iterator I,
1183 unsigned Opcode,
1184 unsigned DstReg,
1185 unsigned Src0Reg,
1186 unsigned Src1Reg) const {
1187 MachineInstrBuilder MIB = BuildMI(MBB, I, MBB.findDebugLoc(I), get(Opcode),
1188 DstReg); // $dst
1189
1190 if (Src1Reg) {
1191 MIB.addImm(0) // $update_exec_mask
1192 .addImm(0); // $update_predicate
1193 }
1194 MIB.addImm(1) // $write
1195 .addImm(0) // $omod
1196 .addImm(0) // $dst_rel
1197 .addImm(0) // $dst_clamp
1198 .addReg(Src0Reg) // $src0
1199 .addImm(0) // $src0_neg
1200 .addImm(0) // $src0_rel
Tom Stellard365366f2013-01-23 02:09:06 +00001201 .addImm(0) // $src0_abs
1202 .addImm(-1); // $src0_sel
Tom Stellard75aadc22012-12-11 21:25:42 +00001203
1204 if (Src1Reg) {
1205 MIB.addReg(Src1Reg) // $src1
1206 .addImm(0) // $src1_neg
1207 .addImm(0) // $src1_rel
Tom Stellard365366f2013-01-23 02:09:06 +00001208 .addImm(0) // $src1_abs
1209 .addImm(-1); // $src1_sel
Tom Stellard75aadc22012-12-11 21:25:42 +00001210 }
1211
1212 //XXX: The r600g finalizer expects this to be 1, once we've moved the
1213 //scheduling to the backend, we can change the default to 0.
1214 MIB.addImm(1) // $last
1215 .addReg(AMDGPU::PRED_SEL_OFF) // $pred_sel
Vincent Lejeune22c42482013-04-30 00:14:08 +00001216 .addImm(0) // $literal
1217 .addImm(0); // $bank_swizzle
Tom Stellard75aadc22012-12-11 21:25:42 +00001218
1219 return MIB;
1220}
1221
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001222#define OPERAND_CASE(Label) \
1223 case Label: { \
Tom Stellard02661d92013-06-25 21:22:18 +00001224 static const unsigned Ops[] = \
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001225 { \
1226 Label##_X, \
1227 Label##_Y, \
1228 Label##_Z, \
1229 Label##_W \
1230 }; \
1231 return Ops[Slot]; \
1232 }
1233
Tom Stellard02661d92013-06-25 21:22:18 +00001234static unsigned getSlotedOps(unsigned Op, unsigned Slot) {
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001235 switch (Op) {
Tom Stellard02661d92013-06-25 21:22:18 +00001236 OPERAND_CASE(AMDGPU::OpName::update_exec_mask)
1237 OPERAND_CASE(AMDGPU::OpName::update_pred)
1238 OPERAND_CASE(AMDGPU::OpName::write)
1239 OPERAND_CASE(AMDGPU::OpName::omod)
1240 OPERAND_CASE(AMDGPU::OpName::dst_rel)
1241 OPERAND_CASE(AMDGPU::OpName::clamp)
1242 OPERAND_CASE(AMDGPU::OpName::src0)
1243 OPERAND_CASE(AMDGPU::OpName::src0_neg)
1244 OPERAND_CASE(AMDGPU::OpName::src0_rel)
1245 OPERAND_CASE(AMDGPU::OpName::src0_abs)
1246 OPERAND_CASE(AMDGPU::OpName::src0_sel)
1247 OPERAND_CASE(AMDGPU::OpName::src1)
1248 OPERAND_CASE(AMDGPU::OpName::src1_neg)
1249 OPERAND_CASE(AMDGPU::OpName::src1_rel)
1250 OPERAND_CASE(AMDGPU::OpName::src1_abs)
1251 OPERAND_CASE(AMDGPU::OpName::src1_sel)
1252 OPERAND_CASE(AMDGPU::OpName::pred_sel)
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001253 default:
1254 llvm_unreachable("Wrong Operand");
1255 }
1256}
1257
1258#undef OPERAND_CASE
1259
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001260MachineInstr *R600InstrInfo::buildSlotOfVectorInstruction(
1261 MachineBasicBlock &MBB, MachineInstr *MI, unsigned Slot, unsigned DstReg)
1262 const {
1263 assert (MI->getOpcode() == AMDGPU::DOT_4 && "Not Implemented");
1264 unsigned Opcode;
Tom Stellarda6c6e1b2013-06-07 20:37:48 +00001265 if (ST.getGeneration() <= AMDGPUSubtarget::R700)
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001266 Opcode = AMDGPU::DOT4_r600;
1267 else
1268 Opcode = AMDGPU::DOT4_eg;
1269 MachineBasicBlock::iterator I = MI;
1270 MachineOperand &Src0 = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001271 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src0, Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001272 MachineOperand &Src1 = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001273 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src1, Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001274 MachineInstr *MIB = buildDefaultInstruction(
1275 MBB, I, Opcode, DstReg, Src0.getReg(), Src1.getReg());
Tom Stellard02661d92013-06-25 21:22:18 +00001276 static const unsigned Operands[14] = {
1277 AMDGPU::OpName::update_exec_mask,
1278 AMDGPU::OpName::update_pred,
1279 AMDGPU::OpName::write,
1280 AMDGPU::OpName::omod,
1281 AMDGPU::OpName::dst_rel,
1282 AMDGPU::OpName::clamp,
1283 AMDGPU::OpName::src0_neg,
1284 AMDGPU::OpName::src0_rel,
1285 AMDGPU::OpName::src0_abs,
1286 AMDGPU::OpName::src0_sel,
1287 AMDGPU::OpName::src1_neg,
1288 AMDGPU::OpName::src1_rel,
1289 AMDGPU::OpName::src1_abs,
1290 AMDGPU::OpName::src1_sel,
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001291 };
1292
Vincent Lejeune745d4292013-11-16 16:24:41 +00001293 MachineOperand &MO = MI->getOperand(getOperandIdx(MI->getOpcode(),
1294 getSlotedOps(AMDGPU::OpName::pred_sel, Slot)));
1295 MIB->getOperand(getOperandIdx(Opcode, AMDGPU::OpName::pred_sel))
1296 .setReg(MO.getReg());
1297
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001298 for (unsigned i = 0; i < 14; i++) {
1299 MachineOperand &MO = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001300 getOperandIdx(MI->getOpcode(), getSlotedOps(Operands[i], Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001301 assert (MO.isImm());
1302 setImmOperand(MIB, Operands[i], MO.getImm());
1303 }
1304 MIB->getOperand(20).setImm(0);
1305 return MIB;
1306}
1307
Tom Stellard75aadc22012-12-11 21:25:42 +00001308MachineInstr *R600InstrInfo::buildMovImm(MachineBasicBlock &BB,
1309 MachineBasicBlock::iterator I,
1310 unsigned DstReg,
1311 uint64_t Imm) const {
1312 MachineInstr *MovImm = buildDefaultInstruction(BB, I, AMDGPU::MOV, DstReg,
1313 AMDGPU::ALU_LITERAL_X);
Tom Stellard02661d92013-06-25 21:22:18 +00001314 setImmOperand(MovImm, AMDGPU::OpName::literal, Imm);
Tom Stellard75aadc22012-12-11 21:25:42 +00001315 return MovImm;
1316}
1317
Tom Stellard26a3b672013-10-22 18:19:10 +00001318MachineInstr *R600InstrInfo::buildMovInstr(MachineBasicBlock *MBB,
1319 MachineBasicBlock::iterator I,
1320 unsigned DstReg, unsigned SrcReg) const {
1321 return buildDefaultInstruction(*MBB, I, AMDGPU::MOV, DstReg, SrcReg);
1322}
1323
Tom Stellard02661d92013-06-25 21:22:18 +00001324int R600InstrInfo::getOperandIdx(const MachineInstr &MI, unsigned Op) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001325 return getOperandIdx(MI.getOpcode(), Op);
1326}
1327
Tom Stellard02661d92013-06-25 21:22:18 +00001328int R600InstrInfo::getOperandIdx(unsigned Opcode, unsigned Op) const {
1329 return AMDGPU::getNamedOperandIdx(Opcode, Op);
Vincent Lejeunec6896792013-06-04 23:17:15 +00001330}
1331
Tom Stellard02661d92013-06-25 21:22:18 +00001332void R600InstrInfo::setImmOperand(MachineInstr *MI, unsigned Op,
Tom Stellard75aadc22012-12-11 21:25:42 +00001333 int64_t Imm) const {
1334 int Idx = getOperandIdx(*MI, Op);
1335 assert(Idx != -1 && "Operand not supported for this instruction.");
1336 assert(MI->getOperand(Idx).isImm());
1337 MI->getOperand(Idx).setImm(Imm);
1338}
1339
1340//===----------------------------------------------------------------------===//
1341// Instruction flag getters/setters
1342//===----------------------------------------------------------------------===//
1343
1344bool R600InstrInfo::hasFlagOperand(const MachineInstr &MI) const {
1345 return GET_FLAG_OPERAND_IDX(get(MI.getOpcode()).TSFlags) != 0;
1346}
1347
1348MachineOperand &R600InstrInfo::getFlagOp(MachineInstr *MI, unsigned SrcIdx,
1349 unsigned Flag) const {
1350 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1351 int FlagIndex = 0;
1352 if (Flag != 0) {
1353 // If we pass something other than the default value of Flag to this
1354 // function, it means we are want to set a flag on an instruction
1355 // that uses native encoding.
1356 assert(HAS_NATIVE_OPERANDS(TargetFlags));
1357 bool IsOP3 = (TargetFlags & R600_InstFlag::OP3) == R600_InstFlag::OP3;
1358 switch (Flag) {
1359 case MO_FLAG_CLAMP:
Tom Stellard02661d92013-06-25 21:22:18 +00001360 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::clamp);
Tom Stellard75aadc22012-12-11 21:25:42 +00001361 break;
1362 case MO_FLAG_MASK:
Tom Stellard02661d92013-06-25 21:22:18 +00001363 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::write);
Tom Stellard75aadc22012-12-11 21:25:42 +00001364 break;
1365 case MO_FLAG_NOT_LAST:
1366 case MO_FLAG_LAST:
Tom Stellard02661d92013-06-25 21:22:18 +00001367 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::last);
Tom Stellard75aadc22012-12-11 21:25:42 +00001368 break;
1369 case MO_FLAG_NEG:
1370 switch (SrcIdx) {
Tom Stellard02661d92013-06-25 21:22:18 +00001371 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_neg); break;
1372 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_neg); break;
1373 case 2: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src2_neg); break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001374 }
1375 break;
1376
1377 case MO_FLAG_ABS:
1378 assert(!IsOP3 && "Cannot set absolute value modifier for OP3 "
1379 "instructions.");
Tom Stellard6975d352012-12-13 19:38:52 +00001380 (void)IsOP3;
Tom Stellard75aadc22012-12-11 21:25:42 +00001381 switch (SrcIdx) {
Tom Stellard02661d92013-06-25 21:22:18 +00001382 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_abs); break;
1383 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_abs); break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001384 }
1385 break;
1386
1387 default:
1388 FlagIndex = -1;
1389 break;
1390 }
1391 assert(FlagIndex != -1 && "Flag not supported for this instruction");
1392 } else {
1393 FlagIndex = GET_FLAG_OPERAND_IDX(TargetFlags);
1394 assert(FlagIndex != 0 &&
1395 "Instruction flags not supported for this instruction");
1396 }
1397
1398 MachineOperand &FlagOp = MI->getOperand(FlagIndex);
1399 assert(FlagOp.isImm());
1400 return FlagOp;
1401}
1402
1403void R600InstrInfo::addFlag(MachineInstr *MI, unsigned Operand,
1404 unsigned Flag) const {
1405 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1406 if (Flag == 0) {
1407 return;
1408 }
1409 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1410 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1411 if (Flag == MO_FLAG_NOT_LAST) {
1412 clearFlag(MI, Operand, MO_FLAG_LAST);
1413 } else if (Flag == MO_FLAG_MASK) {
1414 clearFlag(MI, Operand, Flag);
1415 } else {
1416 FlagOp.setImm(1);
1417 }
1418 } else {
1419 MachineOperand &FlagOp = getFlagOp(MI, Operand);
1420 FlagOp.setImm(FlagOp.getImm() | (Flag << (NUM_MO_FLAGS * Operand)));
1421 }
1422}
1423
1424void R600InstrInfo::clearFlag(MachineInstr *MI, unsigned Operand,
1425 unsigned Flag) const {
1426 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1427 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1428 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1429 FlagOp.setImm(0);
1430 } else {
1431 MachineOperand &FlagOp = getFlagOp(MI);
1432 unsigned InstFlags = FlagOp.getImm();
1433 InstFlags &= ~(Flag << (NUM_MO_FLAGS * Operand));
1434 FlagOp.setImm(InstFlags);
1435 }
1436}