blob: 080ff6eea75db9ba609cde67c3f5accb030150f8 [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)
32 : AMDGPUInstrInfo(st),
33 RI(st)
Tom Stellard75aadc22012-12-11 21:25:42 +000034 { }
35
36const R600RegisterInfo &R600InstrInfo::getRegisterInfo() const {
37 return RI;
38}
39
40bool R600InstrInfo::isTrig(const MachineInstr &MI) const {
41 return get(MI.getOpcode()).TSFlags & R600_InstFlag::TRIG;
42}
43
44bool R600InstrInfo::isVector(const MachineInstr &MI) const {
45 return get(MI.getOpcode()).TSFlags & R600_InstFlag::VECTOR;
46}
47
48void
49R600InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
50 MachineBasicBlock::iterator MI, DebugLoc DL,
51 unsigned DestReg, unsigned SrcReg,
52 bool KillSrc) const {
Tom Stellard0344cdf2013-08-01 15:23:42 +000053 unsigned VectorComponents = 0;
54 if (AMDGPU::R600_Reg128RegClass.contains(DestReg) &&
55 AMDGPU::R600_Reg128RegClass.contains(SrcReg)) {
56 VectorComponents = 4;
57 } else if(AMDGPU::R600_Reg64RegClass.contains(DestReg) &&
58 AMDGPU::R600_Reg64RegClass.contains(SrcReg)) {
59 VectorComponents = 2;
60 }
61
62 if (VectorComponents > 0) {
63 for (unsigned I = 0; I < VectorComponents; I++) {
Tom Stellard75aadc22012-12-11 21:25:42 +000064 unsigned SubRegIndex = RI.getSubRegFromChannel(I);
65 buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
66 RI.getSubReg(DestReg, SubRegIndex),
67 RI.getSubReg(SrcReg, SubRegIndex))
68 .addReg(DestReg,
69 RegState::Define | RegState::Implicit);
70 }
71 } else {
Tom Stellard75aadc22012-12-11 21:25:42 +000072 MachineInstr *NewMI = buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
73 DestReg, SrcReg);
Tom Stellard02661d92013-06-25 21:22:18 +000074 NewMI->getOperand(getOperandIdx(*NewMI, AMDGPU::OpName::src0))
Tom Stellard75aadc22012-12-11 21:25:42 +000075 .setIsKill(KillSrc);
76 }
77}
78
Tom Stellardcd6b0a62013-11-22 00:41:08 +000079/// \returns true if \p MBBI can be moved into a new basic.
80bool R600InstrInfo::isLegalToSplitMBBAt(MachineBasicBlock &MBB,
81 MachineBasicBlock::iterator MBBI) const {
82 for (MachineInstr::const_mop_iterator I = MBBI->operands_begin(),
83 E = MBBI->operands_end(); I != E; ++I) {
84 if (I->isReg() && !TargetRegisterInfo::isVirtualRegister(I->getReg()) &&
85 I->isUse() && RI.isPhysRegLiveAcrossClauses(I->getReg()))
86 return false;
87 }
88 return true;
89}
90
Tom Stellard75aadc22012-12-11 21:25:42 +000091unsigned R600InstrInfo::getIEQOpcode() const {
92 return AMDGPU::SETE_INT;
93}
94
95bool R600InstrInfo::isMov(unsigned Opcode) const {
96
97
98 switch(Opcode) {
99 default: return false;
100 case AMDGPU::MOV:
101 case AMDGPU::MOV_IMM_F32:
102 case AMDGPU::MOV_IMM_I32:
103 return true;
104 }
105}
106
107// Some instructions act as place holders to emulate operations that the GPU
108// hardware does automatically. This function can be used to check if
109// an opcode falls into this category.
110bool R600InstrInfo::isPlaceHolderOpcode(unsigned Opcode) const {
111 switch (Opcode) {
112 default: return false;
113 case AMDGPU::RETURN:
Tom Stellard75aadc22012-12-11 21:25:42 +0000114 return true;
115 }
116}
117
118bool R600InstrInfo::isReductionOp(unsigned Opcode) const {
Aaron Ballmanf04bbd82013-07-10 17:19:22 +0000119 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000120}
121
122bool R600InstrInfo::isCubeOp(unsigned Opcode) const {
123 switch(Opcode) {
124 default: return false;
125 case AMDGPU::CUBE_r600_pseudo:
126 case AMDGPU::CUBE_r600_real:
127 case AMDGPU::CUBE_eg_pseudo:
128 case AMDGPU::CUBE_eg_real:
129 return true;
130 }
131}
132
133bool R600InstrInfo::isALUInstr(unsigned Opcode) const {
134 unsigned TargetFlags = get(Opcode).TSFlags;
135
Tom Stellard5eb903d2013-06-28 15:46:53 +0000136 return (TargetFlags & R600_InstFlag::ALU_INST);
Tom Stellard75aadc22012-12-11 21:25:42 +0000137}
138
Tom Stellardc026e8b2013-06-28 15:47:08 +0000139bool R600InstrInfo::hasInstrModifiers(unsigned Opcode) const {
140 unsigned TargetFlags = get(Opcode).TSFlags;
141
142 return ((TargetFlags & R600_InstFlag::OP1) |
143 (TargetFlags & R600_InstFlag::OP2) |
144 (TargetFlags & R600_InstFlag::OP3));
145}
146
147bool R600InstrInfo::isLDSInstr(unsigned Opcode) const {
148 unsigned TargetFlags = get(Opcode).TSFlags;
149
150 return ((TargetFlags & R600_InstFlag::LDS_1A) |
Tom Stellardf3d166a2013-08-26 15:05:49 +0000151 (TargetFlags & R600_InstFlag::LDS_1A1D) |
152 (TargetFlags & R600_InstFlag::LDS_1A2D));
Tom Stellardc026e8b2013-06-28 15:47:08 +0000153}
154
Tom Stellard8f9fc202013-11-15 00:12:45 +0000155bool R600InstrInfo::isLDSNoRetInstr(unsigned Opcode) const {
156 return isLDSInstr(Opcode) && getOperandIdx(Opcode, AMDGPU::OpName::dst) == -1;
157}
158
159bool R600InstrInfo::isLDSRetInstr(unsigned Opcode) const {
160 return isLDSInstr(Opcode) && getOperandIdx(Opcode, AMDGPU::OpName::dst) != -1;
161}
162
Vincent Lejeunea4da6fb2013-10-01 19:32:58 +0000163bool R600InstrInfo::canBeConsideredALU(const MachineInstr *MI) const {
164 if (isALUInstr(MI->getOpcode()))
165 return true;
166 if (isVector(*MI) || isCubeOp(MI->getOpcode()))
167 return true;
168 switch (MI->getOpcode()) {
169 case AMDGPU::PRED_X:
170 case AMDGPU::INTERP_PAIR_XY:
171 case AMDGPU::INTERP_PAIR_ZW:
172 case AMDGPU::INTERP_VEC_LOAD:
173 case AMDGPU::COPY:
174 case AMDGPU::DOT_4:
175 return true;
176 default:
177 return false;
178 }
179}
180
Vincent Lejeune076c0b22013-04-30 00:14:17 +0000181bool R600InstrInfo::isTransOnly(unsigned Opcode) const {
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +0000182 if (ST.hasCaymanISA())
183 return false;
184 return (get(Opcode).getSchedClass() == AMDGPU::Sched::TransALU);
Vincent Lejeune076c0b22013-04-30 00:14:17 +0000185}
186
187bool R600InstrInfo::isTransOnly(const MachineInstr *MI) const {
188 return isTransOnly(MI->getOpcode());
189}
190
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +0000191bool R600InstrInfo::isVectorOnly(unsigned Opcode) const {
192 return (get(Opcode).getSchedClass() == AMDGPU::Sched::VecALU);
193}
194
195bool R600InstrInfo::isVectorOnly(const MachineInstr *MI) const {
196 return isVectorOnly(MI->getOpcode());
197}
198
Tom Stellard676c16d2013-08-16 01:11:51 +0000199bool R600InstrInfo::isExport(unsigned Opcode) const {
200 return (get(Opcode).TSFlags & R600_InstFlag::IS_EXPORT);
201}
202
Vincent Lejeunec2991642013-04-30 00:13:39 +0000203bool R600InstrInfo::usesVertexCache(unsigned Opcode) const {
Tom Stellardd93cede2013-05-06 17:50:57 +0000204 return ST.hasVertexCache() && IS_VTX(get(Opcode));
Vincent Lejeunec2991642013-04-30 00:13:39 +0000205}
206
207bool R600InstrInfo::usesVertexCache(const MachineInstr *MI) const {
Vincent Lejeune3a8d78a2013-04-30 00:14:44 +0000208 const R600MachineFunctionInfo *MFI = MI->getParent()->getParent()->getInfo<R600MachineFunctionInfo>();
209 return MFI->ShaderType != ShaderType::COMPUTE && 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 {
Vincent Lejeune3a8d78a2013-04-30 00:14:44 +0000217 const R600MachineFunctionInfo *MFI = MI->getParent()->getParent()->getInfo<R600MachineFunctionInfo>();
218 return (MFI->ShaderType == ShaderType::COMPUTE && usesVertexCache(MI->getOpcode())) ||
219 usesTextureCache(MI->getOpcode());
Vincent Lejeunec2991642013-04-30 00:13:39 +0000220}
221
Tom Stellardce540332013-06-28 15:46:59 +0000222bool R600InstrInfo::mustBeLastInClause(unsigned Opcode) const {
223 switch (Opcode) {
224 case AMDGPU::KILLGT:
225 case AMDGPU::GROUP_BARRIER:
226 return true;
227 default:
228 return false;
229 }
230}
231
Tom Stellard26a3b672013-10-22 18:19:10 +0000232bool R600InstrInfo::usesAddressRegister(MachineInstr *MI) const {
233 return MI->findRegisterUseOperandIdx(AMDGPU::AR_X) != -1;
234}
235
236bool R600InstrInfo::definesAddressRegister(MachineInstr *MI) const {
237 return MI->findRegisterDefOperandIdx(AMDGPU::AR_X) != -1;
238}
239
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000240bool R600InstrInfo::readsLDSSrcReg(const MachineInstr *MI) const {
241 if (!isALUInstr(MI->getOpcode())) {
242 return false;
243 }
244 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
245 E = MI->operands_end(); I != E; ++I) {
246 if (!I->isReg() || !I->isUse() ||
247 TargetRegisterInfo::isVirtualRegister(I->getReg()))
248 continue;
249
250 if (AMDGPU::R600_LDS_SRC_REGRegClass.contains(I->getReg()))
251 return true;
252 }
253 return false;
254}
255
Tom Stellard84021442013-07-23 01:48:24 +0000256int R600InstrInfo::getSrcIdx(unsigned Opcode, unsigned SrcNum) const {
257 static const unsigned OpTable[] = {
258 AMDGPU::OpName::src0,
259 AMDGPU::OpName::src1,
260 AMDGPU::OpName::src2
261 };
262
263 assert (SrcNum < 3);
264 return getOperandIdx(Opcode, OpTable[SrcNum]);
265}
266
267#define SRC_SEL_ROWS 11
268int R600InstrInfo::getSelIdx(unsigned Opcode, unsigned SrcIdx) const {
269 static const unsigned SrcSelTable[SRC_SEL_ROWS][2] = {
270 {AMDGPU::OpName::src0, AMDGPU::OpName::src0_sel},
271 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_sel},
272 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_sel},
273 {AMDGPU::OpName::src0_X, AMDGPU::OpName::src0_sel_X},
274 {AMDGPU::OpName::src0_Y, AMDGPU::OpName::src0_sel_Y},
275 {AMDGPU::OpName::src0_Z, AMDGPU::OpName::src0_sel_Z},
276 {AMDGPU::OpName::src0_W, AMDGPU::OpName::src0_sel_W},
277 {AMDGPU::OpName::src1_X, AMDGPU::OpName::src1_sel_X},
278 {AMDGPU::OpName::src1_Y, AMDGPU::OpName::src1_sel_Y},
279 {AMDGPU::OpName::src1_Z, AMDGPU::OpName::src1_sel_Z},
280 {AMDGPU::OpName::src1_W, AMDGPU::OpName::src1_sel_W}
281 };
282
283 for (unsigned i = 0; i < SRC_SEL_ROWS; ++i) {
284 if (getOperandIdx(Opcode, SrcSelTable[i][0]) == (int)SrcIdx) {
285 return getOperandIdx(Opcode, SrcSelTable[i][1]);
286 }
287 }
288 return -1;
289}
290#undef SRC_SEL_ROWS
291
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 }
318
319 }
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
570 TransOps = IGSrcs.back();
571 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
Tom Stellard75aadc22012-12-11 21:25:42 +0000653DFAPacketizer *R600InstrInfo::CreateTargetScheduleState(const TargetMachine *TM,
654 const ScheduleDAG *DAG) const {
655 const InstrItineraryData *II = TM->getInstrItineraryData();
656 return TM->getSubtarget<AMDGPUSubtarget>().createDFAPacketizer(II);
657}
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 Stellard81d871d2013-11-13 23:36:50 +00001056void R600InstrInfo::reserveIndirectRegisters(BitVector &Reserved,
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001057 const MachineFunction &MF) const {
1058 const AMDGPUFrameLowering *TFL =
Tom Stellardd881e912014-06-13 01:31:56 +00001059 static_cast<const AMDGPUFrameLowering*>(
1060 MF.getTarget().getFrameLowering());
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001061
1062 unsigned StackWidth = TFL->getStackWidth(MF);
1063 int End = getIndirectIndexEnd(MF);
1064
Tom Stellard81d871d2013-11-13 23:36:50 +00001065 if (End == -1)
1066 return;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001067
1068 for (int Index = getIndirectIndexBegin(MF); Index <= End; ++Index) {
1069 unsigned SuperReg = AMDGPU::R600_Reg128RegClass.getRegister(Index);
Tom Stellard81d871d2013-11-13 23:36:50 +00001070 Reserved.set(SuperReg);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001071 for (unsigned Chan = 0; Chan < StackWidth; ++Chan) {
1072 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister((4 * Index) + Chan);
Tom Stellard81d871d2013-11-13 23:36:50 +00001073 Reserved.set(Reg);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001074 }
1075 }
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001076}
1077
1078unsigned R600InstrInfo::calculateIndirectAddress(unsigned RegIndex,
1079 unsigned Channel) const {
1080 // XXX: Remove when we support a stack width > 2
1081 assert(Channel == 0);
1082 return RegIndex;
1083}
1084
Tom Stellard26a3b672013-10-22 18:19:10 +00001085const TargetRegisterClass *R600InstrInfo::getIndirectAddrRegClass() const {
1086 return &AMDGPU::R600_TReg32_XRegClass;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001087}
1088
1089MachineInstrBuilder R600InstrInfo::buildIndirectWrite(MachineBasicBlock *MBB,
1090 MachineBasicBlock::iterator I,
1091 unsigned ValueReg, unsigned Address,
1092 unsigned OffsetReg) const {
1093 unsigned AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address);
1094 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1095 AMDGPU::AR_X, OffsetReg);
Tom Stellard02661d92013-06-25 21:22:18 +00001096 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001097
1098 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1099 AddrReg, ValueReg)
Tom Stellardaad53762013-06-05 03:43:06 +00001100 .addReg(AMDGPU::AR_X,
1101 RegState::Implicit | RegState::Kill);
Tom Stellard02661d92013-06-25 21:22:18 +00001102 setImmOperand(Mov, AMDGPU::OpName::dst_rel, 1);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001103 return Mov;
1104}
1105
1106MachineInstrBuilder R600InstrInfo::buildIndirectRead(MachineBasicBlock *MBB,
1107 MachineBasicBlock::iterator I,
1108 unsigned ValueReg, unsigned Address,
1109 unsigned OffsetReg) const {
1110 unsigned AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address);
1111 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1112 AMDGPU::AR_X,
1113 OffsetReg);
Tom Stellard02661d92013-06-25 21:22:18 +00001114 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001115 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1116 ValueReg,
1117 AddrReg)
Tom Stellardaad53762013-06-05 03:43:06 +00001118 .addReg(AMDGPU::AR_X,
1119 RegState::Implicit | RegState::Kill);
Tom Stellard02661d92013-06-25 21:22:18 +00001120 setImmOperand(Mov, AMDGPU::OpName::src0_rel, 1);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001121
1122 return Mov;
1123}
1124
Vincent Lejeune80031d9f2013-04-03 16:49:34 +00001125unsigned R600InstrInfo::getMaxAlusPerClause() const {
1126 return 115;
1127}
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001128
Tom Stellard75aadc22012-12-11 21:25:42 +00001129MachineInstrBuilder R600InstrInfo::buildDefaultInstruction(MachineBasicBlock &MBB,
1130 MachineBasicBlock::iterator I,
1131 unsigned Opcode,
1132 unsigned DstReg,
1133 unsigned Src0Reg,
1134 unsigned Src1Reg) const {
1135 MachineInstrBuilder MIB = BuildMI(MBB, I, MBB.findDebugLoc(I), get(Opcode),
1136 DstReg); // $dst
1137
1138 if (Src1Reg) {
1139 MIB.addImm(0) // $update_exec_mask
1140 .addImm(0); // $update_predicate
1141 }
1142 MIB.addImm(1) // $write
1143 .addImm(0) // $omod
1144 .addImm(0) // $dst_rel
1145 .addImm(0) // $dst_clamp
1146 .addReg(Src0Reg) // $src0
1147 .addImm(0) // $src0_neg
1148 .addImm(0) // $src0_rel
Tom Stellard365366f2013-01-23 02:09:06 +00001149 .addImm(0) // $src0_abs
1150 .addImm(-1); // $src0_sel
Tom Stellard75aadc22012-12-11 21:25:42 +00001151
1152 if (Src1Reg) {
1153 MIB.addReg(Src1Reg) // $src1
1154 .addImm(0) // $src1_neg
1155 .addImm(0) // $src1_rel
Tom Stellard365366f2013-01-23 02:09:06 +00001156 .addImm(0) // $src1_abs
1157 .addImm(-1); // $src1_sel
Tom Stellard75aadc22012-12-11 21:25:42 +00001158 }
1159
1160 //XXX: The r600g finalizer expects this to be 1, once we've moved the
1161 //scheduling to the backend, we can change the default to 0.
1162 MIB.addImm(1) // $last
1163 .addReg(AMDGPU::PRED_SEL_OFF) // $pred_sel
Vincent Lejeune22c42482013-04-30 00:14:08 +00001164 .addImm(0) // $literal
1165 .addImm(0); // $bank_swizzle
Tom Stellard75aadc22012-12-11 21:25:42 +00001166
1167 return MIB;
1168}
1169
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001170#define OPERAND_CASE(Label) \
1171 case Label: { \
Tom Stellard02661d92013-06-25 21:22:18 +00001172 static const unsigned Ops[] = \
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001173 { \
1174 Label##_X, \
1175 Label##_Y, \
1176 Label##_Z, \
1177 Label##_W \
1178 }; \
1179 return Ops[Slot]; \
1180 }
1181
Tom Stellard02661d92013-06-25 21:22:18 +00001182static unsigned getSlotedOps(unsigned Op, unsigned Slot) {
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001183 switch (Op) {
Tom Stellard02661d92013-06-25 21:22:18 +00001184 OPERAND_CASE(AMDGPU::OpName::update_exec_mask)
1185 OPERAND_CASE(AMDGPU::OpName::update_pred)
1186 OPERAND_CASE(AMDGPU::OpName::write)
1187 OPERAND_CASE(AMDGPU::OpName::omod)
1188 OPERAND_CASE(AMDGPU::OpName::dst_rel)
1189 OPERAND_CASE(AMDGPU::OpName::clamp)
1190 OPERAND_CASE(AMDGPU::OpName::src0)
1191 OPERAND_CASE(AMDGPU::OpName::src0_neg)
1192 OPERAND_CASE(AMDGPU::OpName::src0_rel)
1193 OPERAND_CASE(AMDGPU::OpName::src0_abs)
1194 OPERAND_CASE(AMDGPU::OpName::src0_sel)
1195 OPERAND_CASE(AMDGPU::OpName::src1)
1196 OPERAND_CASE(AMDGPU::OpName::src1_neg)
1197 OPERAND_CASE(AMDGPU::OpName::src1_rel)
1198 OPERAND_CASE(AMDGPU::OpName::src1_abs)
1199 OPERAND_CASE(AMDGPU::OpName::src1_sel)
1200 OPERAND_CASE(AMDGPU::OpName::pred_sel)
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001201 default:
1202 llvm_unreachable("Wrong Operand");
1203 }
1204}
1205
1206#undef OPERAND_CASE
1207
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001208MachineInstr *R600InstrInfo::buildSlotOfVectorInstruction(
1209 MachineBasicBlock &MBB, MachineInstr *MI, unsigned Slot, unsigned DstReg)
1210 const {
1211 assert (MI->getOpcode() == AMDGPU::DOT_4 && "Not Implemented");
1212 unsigned Opcode;
Tom Stellarda6c6e1b2013-06-07 20:37:48 +00001213 if (ST.getGeneration() <= AMDGPUSubtarget::R700)
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001214 Opcode = AMDGPU::DOT4_r600;
1215 else
1216 Opcode = AMDGPU::DOT4_eg;
1217 MachineBasicBlock::iterator I = MI;
1218 MachineOperand &Src0 = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001219 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src0, Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001220 MachineOperand &Src1 = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001221 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src1, Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001222 MachineInstr *MIB = buildDefaultInstruction(
1223 MBB, I, Opcode, DstReg, Src0.getReg(), Src1.getReg());
Tom Stellard02661d92013-06-25 21:22:18 +00001224 static const unsigned Operands[14] = {
1225 AMDGPU::OpName::update_exec_mask,
1226 AMDGPU::OpName::update_pred,
1227 AMDGPU::OpName::write,
1228 AMDGPU::OpName::omod,
1229 AMDGPU::OpName::dst_rel,
1230 AMDGPU::OpName::clamp,
1231 AMDGPU::OpName::src0_neg,
1232 AMDGPU::OpName::src0_rel,
1233 AMDGPU::OpName::src0_abs,
1234 AMDGPU::OpName::src0_sel,
1235 AMDGPU::OpName::src1_neg,
1236 AMDGPU::OpName::src1_rel,
1237 AMDGPU::OpName::src1_abs,
1238 AMDGPU::OpName::src1_sel,
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001239 };
1240
Vincent Lejeune745d4292013-11-16 16:24:41 +00001241 MachineOperand &MO = MI->getOperand(getOperandIdx(MI->getOpcode(),
1242 getSlotedOps(AMDGPU::OpName::pred_sel, Slot)));
1243 MIB->getOperand(getOperandIdx(Opcode, AMDGPU::OpName::pred_sel))
1244 .setReg(MO.getReg());
1245
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001246 for (unsigned i = 0; i < 14; i++) {
1247 MachineOperand &MO = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001248 getOperandIdx(MI->getOpcode(), getSlotedOps(Operands[i], Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001249 assert (MO.isImm());
1250 setImmOperand(MIB, Operands[i], MO.getImm());
1251 }
1252 MIB->getOperand(20).setImm(0);
1253 return MIB;
1254}
1255
Tom Stellard75aadc22012-12-11 21:25:42 +00001256MachineInstr *R600InstrInfo::buildMovImm(MachineBasicBlock &BB,
1257 MachineBasicBlock::iterator I,
1258 unsigned DstReg,
1259 uint64_t Imm) const {
1260 MachineInstr *MovImm = buildDefaultInstruction(BB, I, AMDGPU::MOV, DstReg,
1261 AMDGPU::ALU_LITERAL_X);
Tom Stellard02661d92013-06-25 21:22:18 +00001262 setImmOperand(MovImm, AMDGPU::OpName::literal, Imm);
Tom Stellard75aadc22012-12-11 21:25:42 +00001263 return MovImm;
1264}
1265
Tom Stellard26a3b672013-10-22 18:19:10 +00001266MachineInstr *R600InstrInfo::buildMovInstr(MachineBasicBlock *MBB,
1267 MachineBasicBlock::iterator I,
1268 unsigned DstReg, unsigned SrcReg) const {
1269 return buildDefaultInstruction(*MBB, I, AMDGPU::MOV, DstReg, SrcReg);
1270}
1271
Tom Stellard02661d92013-06-25 21:22:18 +00001272int R600InstrInfo::getOperandIdx(const MachineInstr &MI, unsigned Op) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001273 return getOperandIdx(MI.getOpcode(), Op);
1274}
1275
Tom Stellard02661d92013-06-25 21:22:18 +00001276int R600InstrInfo::getOperandIdx(unsigned Opcode, unsigned Op) const {
1277 return AMDGPU::getNamedOperandIdx(Opcode, Op);
Vincent Lejeunec6896792013-06-04 23:17:15 +00001278}
1279
Tom Stellard02661d92013-06-25 21:22:18 +00001280void R600InstrInfo::setImmOperand(MachineInstr *MI, unsigned Op,
Tom Stellard75aadc22012-12-11 21:25:42 +00001281 int64_t Imm) const {
1282 int Idx = getOperandIdx(*MI, Op);
1283 assert(Idx != -1 && "Operand not supported for this instruction.");
1284 assert(MI->getOperand(Idx).isImm());
1285 MI->getOperand(Idx).setImm(Imm);
1286}
1287
1288//===----------------------------------------------------------------------===//
1289// Instruction flag getters/setters
1290//===----------------------------------------------------------------------===//
1291
1292bool R600InstrInfo::hasFlagOperand(const MachineInstr &MI) const {
1293 return GET_FLAG_OPERAND_IDX(get(MI.getOpcode()).TSFlags) != 0;
1294}
1295
1296MachineOperand &R600InstrInfo::getFlagOp(MachineInstr *MI, unsigned SrcIdx,
1297 unsigned Flag) const {
1298 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1299 int FlagIndex = 0;
1300 if (Flag != 0) {
1301 // If we pass something other than the default value of Flag to this
1302 // function, it means we are want to set a flag on an instruction
1303 // that uses native encoding.
1304 assert(HAS_NATIVE_OPERANDS(TargetFlags));
1305 bool IsOP3 = (TargetFlags & R600_InstFlag::OP3) == R600_InstFlag::OP3;
1306 switch (Flag) {
1307 case MO_FLAG_CLAMP:
Tom Stellard02661d92013-06-25 21:22:18 +00001308 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::clamp);
Tom Stellard75aadc22012-12-11 21:25:42 +00001309 break;
1310 case MO_FLAG_MASK:
Tom Stellard02661d92013-06-25 21:22:18 +00001311 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::write);
Tom Stellard75aadc22012-12-11 21:25:42 +00001312 break;
1313 case MO_FLAG_NOT_LAST:
1314 case MO_FLAG_LAST:
Tom Stellard02661d92013-06-25 21:22:18 +00001315 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::last);
Tom Stellard75aadc22012-12-11 21:25:42 +00001316 break;
1317 case MO_FLAG_NEG:
1318 switch (SrcIdx) {
Tom Stellard02661d92013-06-25 21:22:18 +00001319 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_neg); break;
1320 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_neg); break;
1321 case 2: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src2_neg); break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001322 }
1323 break;
1324
1325 case MO_FLAG_ABS:
1326 assert(!IsOP3 && "Cannot set absolute value modifier for OP3 "
1327 "instructions.");
Tom Stellard6975d352012-12-13 19:38:52 +00001328 (void)IsOP3;
Tom Stellard75aadc22012-12-11 21:25:42 +00001329 switch (SrcIdx) {
Tom Stellard02661d92013-06-25 21:22:18 +00001330 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_abs); break;
1331 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_abs); break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001332 }
1333 break;
1334
1335 default:
1336 FlagIndex = -1;
1337 break;
1338 }
1339 assert(FlagIndex != -1 && "Flag not supported for this instruction");
1340 } else {
1341 FlagIndex = GET_FLAG_OPERAND_IDX(TargetFlags);
1342 assert(FlagIndex != 0 &&
1343 "Instruction flags not supported for this instruction");
1344 }
1345
1346 MachineOperand &FlagOp = MI->getOperand(FlagIndex);
1347 assert(FlagOp.isImm());
1348 return FlagOp;
1349}
1350
1351void R600InstrInfo::addFlag(MachineInstr *MI, unsigned Operand,
1352 unsigned Flag) const {
1353 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1354 if (Flag == 0) {
1355 return;
1356 }
1357 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1358 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1359 if (Flag == MO_FLAG_NOT_LAST) {
1360 clearFlag(MI, Operand, MO_FLAG_LAST);
1361 } else if (Flag == MO_FLAG_MASK) {
1362 clearFlag(MI, Operand, Flag);
1363 } else {
1364 FlagOp.setImm(1);
1365 }
1366 } else {
1367 MachineOperand &FlagOp = getFlagOp(MI, Operand);
1368 FlagOp.setImm(FlagOp.getImm() | (Flag << (NUM_MO_FLAGS * Operand)));
1369 }
1370}
1371
1372void R600InstrInfo::clearFlag(MachineInstr *MI, unsigned Operand,
1373 unsigned Flag) const {
1374 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1375 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1376 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1377 FlagOp.setImm(0);
1378 } else {
1379 MachineOperand &FlagOp = getFlagOp(MI);
1380 unsigned InstFlags = FlagOp.getImm();
1381 InstFlags &= ~(Flag << (NUM_MO_FLAGS * Operand));
1382 FlagOp.setImm(InstFlags);
1383 }
1384}