blob: 6381c38919a328de60a60e31e93b5c975ff54033 [file] [log] [blame]
Tom Stellardf98f2ce2012-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 Lejeune4fb224e2013-04-30 00:14:44 +000016#include "AMDGPU.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000017#include "AMDGPUSubtarget.h"
Chandler Carruth58a2cbe2013-01-02 10:22:59 +000018#include "AMDGPUTargetMachine.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000019#include "R600Defines.h"
Tom Stellardc0b0c672013-02-06 17:32:29 +000020#include "R600MachineFunctionInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000021#include "R600RegisterInfo.h"
Tom Stellardc0b0c672013-02-06 17:32:29 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Tom Stellardc0b0c672013-02-06 17:32:29 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000025
Alexey Samsonovb21ab432013-11-18 09:31:53 +000026#define GET_INSTRINFO_CTOR
Tom Stellardf98f2ce2012-12-11 21:25:42 +000027#include "AMDGPUGenDFAPacketizer.inc"
28
29using namespace llvm;
30
31R600InstrInfo::R600InstrInfo(AMDGPUTargetMachine &tm)
32 : AMDGPUInstrInfo(tm),
Bill Wendlingb5632b52013-06-07 20:28:55 +000033 RI(tm),
Vincent Lejeune631591e2013-04-30 00:13:39 +000034 ST(tm.getSubtarget<AMDGPUSubtarget>())
Tom Stellardf98f2ce2012-12-11 21:25:42 +000035 { }
36
37const R600RegisterInfo &R600InstrInfo::getRegisterInfo() const {
38 return RI;
39}
40
41bool R600InstrInfo::isTrig(const MachineInstr &MI) const {
42 return get(MI.getOpcode()).TSFlags & R600_InstFlag::TRIG;
43}
44
45bool R600InstrInfo::isVector(const MachineInstr &MI) const {
46 return get(MI.getOpcode()).TSFlags & R600_InstFlag::VECTOR;
47}
48
49void
50R600InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
51 MachineBasicBlock::iterator MI, DebugLoc DL,
52 unsigned DestReg, unsigned SrcReg,
53 bool KillSrc) const {
Tom Stellard692ee102013-08-01 15:23:42 +000054 unsigned VectorComponents = 0;
55 if (AMDGPU::R600_Reg128RegClass.contains(DestReg) &&
56 AMDGPU::R600_Reg128RegClass.contains(SrcReg)) {
57 VectorComponents = 4;
58 } else if(AMDGPU::R600_Reg64RegClass.contains(DestReg) &&
59 AMDGPU::R600_Reg64RegClass.contains(SrcReg)) {
60 VectorComponents = 2;
61 }
62
63 if (VectorComponents > 0) {
64 for (unsigned I = 0; I < VectorComponents; I++) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +000065 unsigned SubRegIndex = RI.getSubRegFromChannel(I);
66 buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
67 RI.getSubReg(DestReg, SubRegIndex),
68 RI.getSubReg(SrcReg, SubRegIndex))
69 .addReg(DestReg,
70 RegState::Define | RegState::Implicit);
71 }
72 } else {
Tom Stellardf98f2ce2012-12-11 21:25:42 +000073 MachineInstr *NewMI = buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
74 DestReg, SrcReg);
Tom Stellard5e48a0e2013-06-25 21:22:18 +000075 NewMI->getOperand(getOperandIdx(*NewMI, AMDGPU::OpName::src0))
Tom Stellardf98f2ce2012-12-11 21:25:42 +000076 .setIsKill(KillSrc);
77 }
78}
79
Tom Stellardf98f2ce2012-12-11 21:25:42 +000080unsigned R600InstrInfo::getIEQOpcode() const {
81 return AMDGPU::SETE_INT;
82}
83
84bool R600InstrInfo::isMov(unsigned Opcode) const {
85
86
87 switch(Opcode) {
88 default: return false;
89 case AMDGPU::MOV:
90 case AMDGPU::MOV_IMM_F32:
91 case AMDGPU::MOV_IMM_I32:
92 return true;
93 }
94}
95
96// Some instructions act as place holders to emulate operations that the GPU
97// hardware does automatically. This function can be used to check if
98// an opcode falls into this category.
99bool R600InstrInfo::isPlaceHolderOpcode(unsigned Opcode) const {
100 switch (Opcode) {
101 default: return false;
102 case AMDGPU::RETURN:
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000103 return true;
104 }
105}
106
107bool R600InstrInfo::isReductionOp(unsigned Opcode) const {
Aaron Ballman80095e52013-07-10 17:19:22 +0000108 return false;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000109}
110
111bool R600InstrInfo::isCubeOp(unsigned Opcode) const {
112 switch(Opcode) {
113 default: return false;
114 case AMDGPU::CUBE_r600_pseudo:
115 case AMDGPU::CUBE_r600_real:
116 case AMDGPU::CUBE_eg_pseudo:
117 case AMDGPU::CUBE_eg_real:
118 return true;
119 }
120}
121
122bool R600InstrInfo::isALUInstr(unsigned Opcode) const {
123 unsigned TargetFlags = get(Opcode).TSFlags;
124
Tom Stellard7e938192013-06-28 15:46:53 +0000125 return (TargetFlags & R600_InstFlag::ALU_INST);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000126}
127
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000128bool R600InstrInfo::hasInstrModifiers(unsigned Opcode) const {
129 unsigned TargetFlags = get(Opcode).TSFlags;
130
131 return ((TargetFlags & R600_InstFlag::OP1) |
132 (TargetFlags & R600_InstFlag::OP2) |
133 (TargetFlags & R600_InstFlag::OP3));
134}
135
136bool R600InstrInfo::isLDSInstr(unsigned Opcode) const {
137 unsigned TargetFlags = get(Opcode).TSFlags;
138
139 return ((TargetFlags & R600_InstFlag::LDS_1A) |
Tom Stellard8e780122013-08-26 15:05:49 +0000140 (TargetFlags & R600_InstFlag::LDS_1A1D) |
141 (TargetFlags & R600_InstFlag::LDS_1A2D));
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000142}
143
Tom Stellard19a99df2013-11-15 00:12:45 +0000144bool R600InstrInfo::isLDSNoRetInstr(unsigned Opcode) const {
145 return isLDSInstr(Opcode) && getOperandIdx(Opcode, AMDGPU::OpName::dst) == -1;
146}
147
148bool R600InstrInfo::isLDSRetInstr(unsigned Opcode) const {
149 return isLDSInstr(Opcode) && getOperandIdx(Opcode, AMDGPU::OpName::dst) != -1;
150}
151
Vincent Lejeunedfef7cb2013-10-01 19:32:58 +0000152bool R600InstrInfo::canBeConsideredALU(const MachineInstr *MI) const {
153 if (isALUInstr(MI->getOpcode()))
154 return true;
155 if (isVector(*MI) || isCubeOp(MI->getOpcode()))
156 return true;
157 switch (MI->getOpcode()) {
158 case AMDGPU::PRED_X:
159 case AMDGPU::INTERP_PAIR_XY:
160 case AMDGPU::INTERP_PAIR_ZW:
161 case AMDGPU::INTERP_VEC_LOAD:
162 case AMDGPU::COPY:
163 case AMDGPU::DOT_4:
164 return true;
165 default:
166 return false;
167 }
168}
169
Vincent Lejeuneabcde262013-04-30 00:14:17 +0000170bool R600InstrInfo::isTransOnly(unsigned Opcode) const {
Vincent Lejeuneb3df27d2013-09-04 19:53:30 +0000171 if (ST.hasCaymanISA())
172 return false;
173 return (get(Opcode).getSchedClass() == AMDGPU::Sched::TransALU);
Vincent Lejeuneabcde262013-04-30 00:14:17 +0000174}
175
176bool R600InstrInfo::isTransOnly(const MachineInstr *MI) const {
177 return isTransOnly(MI->getOpcode());
178}
179
Vincent Lejeuneb3df27d2013-09-04 19:53:30 +0000180bool R600InstrInfo::isVectorOnly(unsigned Opcode) const {
181 return (get(Opcode).getSchedClass() == AMDGPU::Sched::VecALU);
182}
183
184bool R600InstrInfo::isVectorOnly(const MachineInstr *MI) const {
185 return isVectorOnly(MI->getOpcode());
186}
187
Tom Stellarde7ac2ed2013-08-16 01:11:51 +0000188bool R600InstrInfo::isExport(unsigned Opcode) const {
189 return (get(Opcode).TSFlags & R600_InstFlag::IS_EXPORT);
190}
191
Vincent Lejeune631591e2013-04-30 00:13:39 +0000192bool R600InstrInfo::usesVertexCache(unsigned Opcode) const {
Tom Stellard32c76102013-05-06 17:50:57 +0000193 return ST.hasVertexCache() && IS_VTX(get(Opcode));
Vincent Lejeune631591e2013-04-30 00:13:39 +0000194}
195
196bool R600InstrInfo::usesVertexCache(const MachineInstr *MI) const {
Vincent Lejeune4fb224e2013-04-30 00:14:44 +0000197 const R600MachineFunctionInfo *MFI = MI->getParent()->getParent()->getInfo<R600MachineFunctionInfo>();
198 return MFI->ShaderType != ShaderType::COMPUTE && usesVertexCache(MI->getOpcode());
Vincent Lejeune631591e2013-04-30 00:13:39 +0000199}
200
201bool R600InstrInfo::usesTextureCache(unsigned Opcode) const {
Tom Stellard32c76102013-05-06 17:50:57 +0000202 return (!ST.hasVertexCache() && IS_VTX(get(Opcode))) || IS_TEX(get(Opcode));
Vincent Lejeune631591e2013-04-30 00:13:39 +0000203}
204
205bool R600InstrInfo::usesTextureCache(const MachineInstr *MI) const {
Vincent Lejeune4fb224e2013-04-30 00:14:44 +0000206 const R600MachineFunctionInfo *MFI = MI->getParent()->getParent()->getInfo<R600MachineFunctionInfo>();
207 return (MFI->ShaderType == ShaderType::COMPUTE && usesVertexCache(MI->getOpcode())) ||
208 usesTextureCache(MI->getOpcode());
Vincent Lejeune631591e2013-04-30 00:13:39 +0000209}
210
Tom Stellardcedcfee2013-06-28 15:46:59 +0000211bool R600InstrInfo::mustBeLastInClause(unsigned Opcode) const {
212 switch (Opcode) {
213 case AMDGPU::KILLGT:
214 case AMDGPU::GROUP_BARRIER:
215 return true;
216 default:
217 return false;
218 }
219}
220
Tom Stellard04c55952013-10-22 18:19:10 +0000221bool R600InstrInfo::usesAddressRegister(MachineInstr *MI) const {
222 return MI->findRegisterUseOperandIdx(AMDGPU::AR_X) != -1;
223}
224
225bool R600InstrInfo::definesAddressRegister(MachineInstr *MI) const {
226 return MI->findRegisterDefOperandIdx(AMDGPU::AR_X) != -1;
227}
228
Tom Stellardac779b82013-09-12 02:55:06 +0000229bool R600InstrInfo::readsLDSSrcReg(const MachineInstr *MI) const {
230 if (!isALUInstr(MI->getOpcode())) {
231 return false;
232 }
233 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
234 E = MI->operands_end(); I != E; ++I) {
235 if (!I->isReg() || !I->isUse() ||
236 TargetRegisterInfo::isVirtualRegister(I->getReg()))
237 continue;
238
239 if (AMDGPU::R600_LDS_SRC_REGRegClass.contains(I->getReg()))
240 return true;
241 }
242 return false;
243}
244
Tom Stellard58d33352013-07-23 01:48:24 +0000245int R600InstrInfo::getSrcIdx(unsigned Opcode, unsigned SrcNum) const {
246 static const unsigned OpTable[] = {
247 AMDGPU::OpName::src0,
248 AMDGPU::OpName::src1,
249 AMDGPU::OpName::src2
250 };
251
252 assert (SrcNum < 3);
253 return getOperandIdx(Opcode, OpTable[SrcNum]);
254}
255
256#define SRC_SEL_ROWS 11
257int R600InstrInfo::getSelIdx(unsigned Opcode, unsigned SrcIdx) const {
258 static const unsigned SrcSelTable[SRC_SEL_ROWS][2] = {
259 {AMDGPU::OpName::src0, AMDGPU::OpName::src0_sel},
260 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_sel},
261 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_sel},
262 {AMDGPU::OpName::src0_X, AMDGPU::OpName::src0_sel_X},
263 {AMDGPU::OpName::src0_Y, AMDGPU::OpName::src0_sel_Y},
264 {AMDGPU::OpName::src0_Z, AMDGPU::OpName::src0_sel_Z},
265 {AMDGPU::OpName::src0_W, AMDGPU::OpName::src0_sel_W},
266 {AMDGPU::OpName::src1_X, AMDGPU::OpName::src1_sel_X},
267 {AMDGPU::OpName::src1_Y, AMDGPU::OpName::src1_sel_Y},
268 {AMDGPU::OpName::src1_Z, AMDGPU::OpName::src1_sel_Z},
269 {AMDGPU::OpName::src1_W, AMDGPU::OpName::src1_sel_W}
270 };
271
272 for (unsigned i = 0; i < SRC_SEL_ROWS; ++i) {
273 if (getOperandIdx(Opcode, SrcSelTable[i][0]) == (int)SrcIdx) {
274 return getOperandIdx(Opcode, SrcSelTable[i][1]);
275 }
276 }
277 return -1;
278}
279#undef SRC_SEL_ROWS
280
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000281SmallVector<std::pair<MachineOperand *, int64_t>, 3>
282R600InstrInfo::getSrcs(MachineInstr *MI) const {
283 SmallVector<std::pair<MachineOperand *, int64_t>, 3> Result;
284
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000285 if (MI->getOpcode() == AMDGPU::DOT_4) {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000286 static const unsigned OpTable[8][2] = {
287 {AMDGPU::OpName::src0_X, AMDGPU::OpName::src0_sel_X},
288 {AMDGPU::OpName::src0_Y, AMDGPU::OpName::src0_sel_Y},
289 {AMDGPU::OpName::src0_Z, AMDGPU::OpName::src0_sel_Z},
290 {AMDGPU::OpName::src0_W, AMDGPU::OpName::src0_sel_W},
291 {AMDGPU::OpName::src1_X, AMDGPU::OpName::src1_sel_X},
292 {AMDGPU::OpName::src1_Y, AMDGPU::OpName::src1_sel_Y},
293 {AMDGPU::OpName::src1_Z, AMDGPU::OpName::src1_sel_Z},
294 {AMDGPU::OpName::src1_W, AMDGPU::OpName::src1_sel_W},
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000295 };
296
297 for (unsigned j = 0; j < 8; j++) {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000298 MachineOperand &MO = MI->getOperand(getOperandIdx(MI->getOpcode(),
299 OpTable[j][0]));
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000300 unsigned Reg = MO.getReg();
301 if (Reg == AMDGPU::ALU_CONST) {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000302 unsigned Sel = MI->getOperand(getOperandIdx(MI->getOpcode(),
303 OpTable[j][1])).getImm();
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000304 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Sel));
305 continue;
306 }
307
308 }
309 return Result;
310 }
311
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000312 static const unsigned OpTable[3][2] = {
313 {AMDGPU::OpName::src0, AMDGPU::OpName::src0_sel},
314 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_sel},
315 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_sel},
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000316 };
317
318 for (unsigned j = 0; j < 3; j++) {
319 int SrcIdx = getOperandIdx(MI->getOpcode(), OpTable[j][0]);
320 if (SrcIdx < 0)
321 break;
322 MachineOperand &MO = MI->getOperand(SrcIdx);
323 unsigned Reg = MI->getOperand(SrcIdx).getReg();
324 if (Reg == AMDGPU::ALU_CONST) {
325 unsigned Sel = MI->getOperand(
326 getOperandIdx(MI->getOpcode(), OpTable[j][1])).getImm();
327 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Sel));
328 continue;
329 }
330 if (Reg == AMDGPU::ALU_LITERAL_X) {
331 unsigned Imm = MI->getOperand(
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000332 getOperandIdx(MI->getOpcode(), AMDGPU::OpName::literal)).getImm();
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000333 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Imm));
334 continue;
335 }
336 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, 0));
337 }
338 return Result;
339}
340
341std::vector<std::pair<int, unsigned> >
342R600InstrInfo::ExtractSrcs(MachineInstr *MI,
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000343 const DenseMap<unsigned, unsigned> &PV,
344 unsigned &ConstCount) const {
345 ConstCount = 0;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000346 const SmallVector<std::pair<MachineOperand *, int64_t>, 3> Srcs = getSrcs(MI);
347 const std::pair<int, unsigned> DummyPair(-1, 0);
348 std::vector<std::pair<int, unsigned> > Result;
349 unsigned i = 0;
350 for (unsigned n = Srcs.size(); i < n; ++i) {
351 unsigned Reg = Srcs[i].first->getReg();
352 unsigned Index = RI.getEncodingValue(Reg) & 0xff;
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000353 if (Reg == AMDGPU::OQAP) {
354 Result.push_back(std::pair<int, unsigned>(Index, 0));
355 }
Vincent Lejeunef7c9b952013-06-17 20:16:40 +0000356 if (PV.find(Reg) != PV.end()) {
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000357 // 255 is used to tells its a PS/PV reg
358 Result.push_back(std::pair<int, unsigned>(255, 0));
359 continue;
360 }
361 if (Index > 127) {
362 ConstCount++;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000363 Result.push_back(DummyPair);
364 continue;
365 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000366 unsigned Chan = RI.getHWRegChan(Reg);
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000367 Result.push_back(std::pair<int, unsigned>(Index, Chan));
368 }
369 for (; i < 3; ++i)
370 Result.push_back(DummyPair);
371 return Result;
372}
373
374static std::vector<std::pair<int, unsigned> >
375Swizzle(std::vector<std::pair<int, unsigned> > Src,
376 R600InstrInfo::BankSwizzle Swz) {
Vincent Lejeunef94eea92013-09-04 19:53:54 +0000377 if (Src[0] == Src[1])
378 Src[1].first = -1;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000379 switch (Swz) {
Vincent Lejeune7d1a0d42013-06-29 19:32:29 +0000380 case R600InstrInfo::ALU_VEC_012_SCL_210:
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000381 break;
Vincent Lejeune7d1a0d42013-06-29 19:32:29 +0000382 case R600InstrInfo::ALU_VEC_021_SCL_122:
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000383 std::swap(Src[1], Src[2]);
384 break;
Vincent Lejeune7d1a0d42013-06-29 19:32:29 +0000385 case R600InstrInfo::ALU_VEC_102_SCL_221:
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000386 std::swap(Src[0], Src[1]);
387 break;
Vincent Lejeune7d1a0d42013-06-29 19:32:29 +0000388 case R600InstrInfo::ALU_VEC_120_SCL_212:
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000389 std::swap(Src[0], Src[1]);
390 std::swap(Src[0], Src[2]);
391 break;
392 case R600InstrInfo::ALU_VEC_201:
393 std::swap(Src[0], Src[2]);
394 std::swap(Src[0], Src[1]);
395 break;
396 case R600InstrInfo::ALU_VEC_210:
397 std::swap(Src[0], Src[2]);
398 break;
399 }
400 return Src;
401}
402
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000403static unsigned
404getTransSwizzle(R600InstrInfo::BankSwizzle Swz, unsigned Op) {
405 switch (Swz) {
406 case R600InstrInfo::ALU_VEC_012_SCL_210: {
407 unsigned Cycles[3] = { 2, 1, 0};
408 return Cycles[Op];
409 }
410 case R600InstrInfo::ALU_VEC_021_SCL_122: {
411 unsigned Cycles[3] = { 1, 2, 2};
412 return Cycles[Op];
413 }
414 case R600InstrInfo::ALU_VEC_120_SCL_212: {
415 unsigned Cycles[3] = { 2, 1, 2};
416 return Cycles[Op];
417 }
418 case R600InstrInfo::ALU_VEC_102_SCL_221: {
419 unsigned Cycles[3] = { 2, 2, 1};
420 return Cycles[Op];
421 }
422 default:
423 llvm_unreachable("Wrong Swizzle for Trans Slot");
424 return 0;
425 }
426}
427
428/// returns how many MIs (whose inputs are represented by IGSrcs) can be packed
429/// in the same Instruction Group while meeting read port limitations given a
430/// Swz swizzle sequence.
431unsigned R600InstrInfo::isLegalUpTo(
432 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
433 const std::vector<R600InstrInfo::BankSwizzle> &Swz,
434 const std::vector<std::pair<int, unsigned> > &TransSrcs,
435 R600InstrInfo::BankSwizzle TransSwz) const {
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000436 int Vector[4][3];
437 memset(Vector, -1, sizeof(Vector));
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000438 for (unsigned i = 0, e = IGSrcs.size(); i < e; i++) {
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000439 const std::vector<std::pair<int, unsigned> > &Srcs =
440 Swizzle(IGSrcs[i], Swz[i]);
441 for (unsigned j = 0; j < 3; j++) {
442 const std::pair<int, unsigned> &Src = Srcs[j];
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000443 if (Src.first < 0 || Src.first == 255)
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000444 continue;
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000445 if (Src.first == GET_REG_INDEX(RI.getEncodingValue(AMDGPU::OQAP))) {
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000446 if (Swz[i] != R600InstrInfo::ALU_VEC_012_SCL_210 &&
447 Swz[i] != R600InstrInfo::ALU_VEC_021_SCL_122) {
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000448 // The value from output queue A (denoted by register OQAP) can
449 // only be fetched during the first cycle.
450 return false;
451 }
452 // OQAP does not count towards the normal read port restrictions
453 continue;
454 }
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000455 if (Vector[Src.second][j] < 0)
456 Vector[Src.second][j] = Src.first;
457 if (Vector[Src.second][j] != Src.first)
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000458 return i;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000459 }
460 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000461 // Now check Trans Alu
462 for (unsigned i = 0, e = TransSrcs.size(); i < e; ++i) {
463 const std::pair<int, unsigned> &Src = TransSrcs[i];
464 unsigned Cycle = getTransSwizzle(TransSwz, i);
465 if (Src.first < 0)
466 continue;
467 if (Src.first == 255)
468 continue;
469 if (Vector[Src.second][Cycle] < 0)
470 Vector[Src.second][Cycle] = Src.first;
471 if (Vector[Src.second][Cycle] != Src.first)
472 return IGSrcs.size() - 1;
473 }
474 return IGSrcs.size();
475}
476
477/// Given a swizzle sequence SwzCandidate and an index Idx, returns the next
478/// (in lexicographic term) swizzle sequence assuming that all swizzles after
479/// Idx can be skipped
480static bool
481NextPossibleSolution(
482 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
483 unsigned Idx) {
484 assert(Idx < SwzCandidate.size());
485 int ResetIdx = Idx;
486 while (ResetIdx > -1 && SwzCandidate[ResetIdx] == R600InstrInfo::ALU_VEC_210)
487 ResetIdx --;
488 for (unsigned i = ResetIdx + 1, e = SwzCandidate.size(); i < e; i++) {
489 SwzCandidate[i] = R600InstrInfo::ALU_VEC_012_SCL_210;
490 }
491 if (ResetIdx == -1)
492 return false;
Benjamin Kramer97daabf2013-06-29 20:04:19 +0000493 int NextSwizzle = SwzCandidate[ResetIdx] + 1;
494 SwzCandidate[ResetIdx] = (R600InstrInfo::BankSwizzle)NextSwizzle;
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000495 return true;
496}
497
498/// Enumerate all possible Swizzle sequence to find one that can meet all
499/// read port requirements.
500bool R600InstrInfo::FindSwizzleForVectorSlot(
501 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
502 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
503 const std::vector<std::pair<int, unsigned> > &TransSrcs,
504 R600InstrInfo::BankSwizzle TransSwz) const {
505 unsigned ValidUpTo = 0;
506 do {
507 ValidUpTo = isLegalUpTo(IGSrcs, SwzCandidate, TransSrcs, TransSwz);
508 if (ValidUpTo == IGSrcs.size())
509 return true;
510 } while (NextPossibleSolution(SwzCandidate, ValidUpTo));
511 return false;
512}
513
514/// Instructions in Trans slot can't read gpr at cycle 0 if they also read
515/// a const, and can't read a gpr at cycle 1 if they read 2 const.
516static bool
517isConstCompatible(R600InstrInfo::BankSwizzle TransSwz,
518 const std::vector<std::pair<int, unsigned> > &TransOps,
519 unsigned ConstCount) {
Vincent Lejeunebb25a012013-09-04 19:53:46 +0000520 // TransALU can't read 3 constants
521 if (ConstCount > 2)
522 return false;
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000523 for (unsigned i = 0, e = TransOps.size(); i < e; ++i) {
524 const std::pair<int, unsigned> &Src = TransOps[i];
525 unsigned Cycle = getTransSwizzle(TransSwz, i);
526 if (Src.first < 0)
527 continue;
528 if (ConstCount > 0 && Cycle == 0)
529 return false;
530 if (ConstCount > 1 && Cycle == 1)
531 return false;
532 }
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000533 return true;
534}
535
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000536bool
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000537R600InstrInfo::fitsReadPortLimitations(const std::vector<MachineInstr *> &IG,
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000538 const DenseMap<unsigned, unsigned> &PV,
539 std::vector<BankSwizzle> &ValidSwizzle,
540 bool isLastAluTrans)
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000541 const {
542 //Todo : support shared src0 - src1 operand
543
544 std::vector<std::vector<std::pair<int, unsigned> > > IGSrcs;
545 ValidSwizzle.clear();
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000546 unsigned ConstCount;
Vincent Lejeunec36a8d22013-06-30 21:44:06 +0000547 BankSwizzle TransBS = ALU_VEC_012_SCL_210;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000548 for (unsigned i = 0, e = IG.size(); i < e; ++i) {
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000549 IGSrcs.push_back(ExtractSrcs(IG[i], PV, ConstCount));
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000550 unsigned Op = getOperandIdx(IG[i]->getOpcode(),
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000551 AMDGPU::OpName::bank_swizzle);
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000552 ValidSwizzle.push_back( (R600InstrInfo::BankSwizzle)
553 IG[i]->getOperand(Op).getImm());
554 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000555 std::vector<std::pair<int, unsigned> > TransOps;
556 if (!isLastAluTrans)
557 return FindSwizzleForVectorSlot(IGSrcs, ValidSwizzle, TransOps, TransBS);
558
559 TransOps = IGSrcs.back();
560 IGSrcs.pop_back();
561 ValidSwizzle.pop_back();
562
563 static const R600InstrInfo::BankSwizzle TransSwz[] = {
564 ALU_VEC_012_SCL_210,
565 ALU_VEC_021_SCL_122,
566 ALU_VEC_120_SCL_212,
567 ALU_VEC_102_SCL_221
568 };
569 for (unsigned i = 0; i < 4; i++) {
570 TransBS = TransSwz[i];
571 if (!isConstCompatible(TransBS, TransOps, ConstCount))
572 continue;
573 bool Result = FindSwizzleForVectorSlot(IGSrcs, ValidSwizzle, TransOps,
574 TransBS);
575 if (Result) {
576 ValidSwizzle.push_back(TransBS);
577 return true;
578 }
579 }
580
581 return false;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000582}
583
584
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000585bool
586R600InstrInfo::fitsConstReadLimitations(const std::vector<unsigned> &Consts)
587 const {
588 assert (Consts.size() <= 12 && "Too many operands in instructions group");
589 unsigned Pair1 = 0, Pair2 = 0;
590 for (unsigned i = 0, n = Consts.size(); i < n; ++i) {
591 unsigned ReadConstHalf = Consts[i] & 2;
592 unsigned ReadConstIndex = Consts[i] & (~3);
593 unsigned ReadHalfConst = ReadConstIndex | ReadConstHalf;
594 if (!Pair1) {
595 Pair1 = ReadHalfConst;
596 continue;
597 }
598 if (Pair1 == ReadHalfConst)
599 continue;
600 if (!Pair2) {
601 Pair2 = ReadHalfConst;
602 continue;
603 }
604 if (Pair2 != ReadHalfConst)
605 return false;
606 }
607 return true;
608}
609
610bool
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000611R600InstrInfo::fitsConstReadLimitations(const std::vector<MachineInstr *> &MIs)
612 const {
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000613 std::vector<unsigned> Consts;
Vincent Lejeune8e377052013-07-31 19:32:07 +0000614 SmallSet<int64_t, 4> Literals;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000615 for (unsigned i = 0, n = MIs.size(); i < n; i++) {
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000616 MachineInstr *MI = MIs[i];
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000617 if (!isALUInstr(MI->getOpcode()))
618 continue;
619
Craig Toppera0ec3f92013-07-14 04:42:23 +0000620 const SmallVectorImpl<std::pair<MachineOperand *, int64_t> > &Srcs =
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000621 getSrcs(MI);
622
623 for (unsigned j = 0, e = Srcs.size(); j < e; j++) {
624 std::pair<MachineOperand *, unsigned> Src = Srcs[j];
Vincent Lejeune8e377052013-07-31 19:32:07 +0000625 if (Src.first->getReg() == AMDGPU::ALU_LITERAL_X)
626 Literals.insert(Src.second);
627 if (Literals.size() > 4)
628 return false;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000629 if (Src.first->getReg() == AMDGPU::ALU_CONST)
630 Consts.push_back(Src.second);
631 if (AMDGPU::R600_KC0RegClass.contains(Src.first->getReg()) ||
632 AMDGPU::R600_KC1RegClass.contains(Src.first->getReg())) {
633 unsigned Index = RI.getEncodingValue(Src.first->getReg()) & 0xff;
634 unsigned Chan = RI.getHWRegChan(Src.first->getReg());
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000635 Consts.push_back((Index << 2) | Chan);
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000636 }
637 }
638 }
639 return fitsConstReadLimitations(Consts);
640}
641
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000642DFAPacketizer *R600InstrInfo::CreateTargetScheduleState(const TargetMachine *TM,
643 const ScheduleDAG *DAG) const {
644 const InstrItineraryData *II = TM->getInstrItineraryData();
645 return TM->getSubtarget<AMDGPUSubtarget>().createDFAPacketizer(II);
646}
647
648static bool
649isPredicateSetter(unsigned Opcode) {
650 switch (Opcode) {
651 case AMDGPU::PRED_X:
652 return true;
653 default:
654 return false;
655 }
656}
657
658static MachineInstr *
659findFirstPredicateSetterFrom(MachineBasicBlock &MBB,
660 MachineBasicBlock::iterator I) {
661 while (I != MBB.begin()) {
662 --I;
663 MachineInstr *MI = I;
664 if (isPredicateSetter(MI->getOpcode()))
665 return MI;
666 }
667
668 return NULL;
669}
670
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000671static
672bool isJump(unsigned Opcode) {
673 return Opcode == AMDGPU::JUMP || Opcode == AMDGPU::JUMP_COND;
674}
675
Vincent Lejeune5b00e832013-10-01 19:32:38 +0000676static bool isBranch(unsigned Opcode) {
677 return Opcode == AMDGPU::BRANCH || Opcode == AMDGPU::BRANCH_COND_i32 ||
678 Opcode == AMDGPU::BRANCH_COND_f32;
679}
680
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000681bool
682R600InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
683 MachineBasicBlock *&TBB,
684 MachineBasicBlock *&FBB,
685 SmallVectorImpl<MachineOperand> &Cond,
686 bool AllowModify) const {
687 // Most of the following comes from the ARM implementation of AnalyzeBranch
688
689 // If the block has no terminators, it just falls into the block after it.
690 MachineBasicBlock::iterator I = MBB.end();
691 if (I == MBB.begin())
692 return false;
693 --I;
694 while (I->isDebugValue()) {
695 if (I == MBB.begin())
696 return false;
697 --I;
698 }
Vincent Lejeune5b00e832013-10-01 19:32:38 +0000699 // AMDGPU::BRANCH* instructions are only available after isel and are not
700 // handled
701 if (isBranch(I->getOpcode()))
702 return true;
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000703 if (!isJump(static_cast<MachineInstr *>(I)->getOpcode())) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000704 return false;
705 }
706
707 // Get the last instruction in the block.
708 MachineInstr *LastInst = I;
709
710 // If there is only one terminator instruction, process it.
711 unsigned LastOpc = LastInst->getOpcode();
712 if (I == MBB.begin() ||
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000713 !isJump(static_cast<MachineInstr *>(--I)->getOpcode())) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000714 if (LastOpc == AMDGPU::JUMP) {
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000715 TBB = LastInst->getOperand(0).getMBB();
716 return false;
717 } else if (LastOpc == AMDGPU::JUMP_COND) {
718 MachineInstr *predSet = I;
719 while (!isPredicateSetter(predSet->getOpcode())) {
720 predSet = --I;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000721 }
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000722 TBB = LastInst->getOperand(0).getMBB();
723 Cond.push_back(predSet->getOperand(1));
724 Cond.push_back(predSet->getOperand(2));
725 Cond.push_back(MachineOperand::CreateReg(AMDGPU::PRED_SEL_ONE, false));
726 return false;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000727 }
728 return true; // Can't handle indirect branch.
729 }
730
731 // Get the instruction before it if it is a terminator.
732 MachineInstr *SecondLastInst = I;
733 unsigned SecondLastOpc = SecondLastInst->getOpcode();
734
735 // If the block ends with a B and a Bcc, handle it.
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000736 if (SecondLastOpc == AMDGPU::JUMP_COND && LastOpc == AMDGPU::JUMP) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000737 MachineInstr *predSet = --I;
738 while (!isPredicateSetter(predSet->getOpcode())) {
739 predSet = --I;
740 }
741 TBB = SecondLastInst->getOperand(0).getMBB();
742 FBB = LastInst->getOperand(0).getMBB();
743 Cond.push_back(predSet->getOperand(1));
744 Cond.push_back(predSet->getOperand(2));
745 Cond.push_back(MachineOperand::CreateReg(AMDGPU::PRED_SEL_ONE, false));
746 return false;
747 }
748
749 // Otherwise, can't handle this.
750 return true;
751}
752
753int R600InstrInfo::getBranchInstr(const MachineOperand &op) const {
754 const MachineInstr *MI = op.getParent();
755
756 switch (MI->getDesc().OpInfo->RegClass) {
757 default: // FIXME: fallthrough??
758 case AMDGPU::GPRI32RegClassID: return AMDGPU::BRANCH_COND_i32;
759 case AMDGPU::GPRF32RegClassID: return AMDGPU::BRANCH_COND_f32;
760 };
761}
762
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000763static
764MachineBasicBlock::iterator FindLastAluClause(MachineBasicBlock &MBB) {
765 for (MachineBasicBlock::reverse_iterator It = MBB.rbegin(), E = MBB.rend();
766 It != E; ++It) {
767 if (It->getOpcode() == AMDGPU::CF_ALU ||
768 It->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE)
769 return llvm::prior(It.base());
770 }
771 return MBB.end();
772}
773
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000774unsigned
775R600InstrInfo::InsertBranch(MachineBasicBlock &MBB,
776 MachineBasicBlock *TBB,
777 MachineBasicBlock *FBB,
778 const SmallVectorImpl<MachineOperand> &Cond,
779 DebugLoc DL) const {
780 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
781
782 if (FBB == 0) {
783 if (Cond.empty()) {
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000784 BuildMI(&MBB, DL, get(AMDGPU::JUMP)).addMBB(TBB);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000785 return 1;
786 } else {
787 MachineInstr *PredSet = findFirstPredicateSetterFrom(MBB, MBB.end());
788 assert(PredSet && "No previous predicate !");
789 addFlag(PredSet, 0, MO_FLAG_PUSH);
790 PredSet->getOperand(2).setImm(Cond[1].getImm());
791
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000792 BuildMI(&MBB, DL, get(AMDGPU::JUMP_COND))
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000793 .addMBB(TBB)
794 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000795 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
796 if (CfAlu == MBB.end())
797 return 1;
798 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU);
799 CfAlu->setDesc(get(AMDGPU::CF_ALU_PUSH_BEFORE));
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000800 return 1;
801 }
802 } else {
803 MachineInstr *PredSet = findFirstPredicateSetterFrom(MBB, MBB.end());
804 assert(PredSet && "No previous predicate !");
805 addFlag(PredSet, 0, MO_FLAG_PUSH);
806 PredSet->getOperand(2).setImm(Cond[1].getImm());
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000807 BuildMI(&MBB, DL, get(AMDGPU::JUMP_COND))
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000808 .addMBB(TBB)
809 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000810 BuildMI(&MBB, DL, get(AMDGPU::JUMP)).addMBB(FBB);
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000811 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
812 if (CfAlu == MBB.end())
813 return 2;
814 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU);
815 CfAlu->setDesc(get(AMDGPU::CF_ALU_PUSH_BEFORE));
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000816 return 2;
817 }
818}
819
820unsigned
821R600InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
822
823 // Note : we leave PRED* instructions there.
824 // They may be needed when predicating instructions.
825
826 MachineBasicBlock::iterator I = MBB.end();
827
828 if (I == MBB.begin()) {
829 return 0;
830 }
831 --I;
832 switch (I->getOpcode()) {
833 default:
834 return 0;
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000835 case AMDGPU::JUMP_COND: {
836 MachineInstr *predSet = findFirstPredicateSetterFrom(MBB, I);
837 clearFlag(predSet, 0, MO_FLAG_PUSH);
838 I->eraseFromParent();
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000839 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
840 if (CfAlu == MBB.end())
841 break;
842 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE);
843 CfAlu->setDesc(get(AMDGPU::CF_ALU));
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000844 break;
845 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000846 case AMDGPU::JUMP:
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000847 I->eraseFromParent();
848 break;
849 }
850 I = MBB.end();
851
852 if (I == MBB.begin()) {
853 return 1;
854 }
855 --I;
856 switch (I->getOpcode()) {
857 // FIXME: only one case??
858 default:
859 return 1;
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000860 case AMDGPU::JUMP_COND: {
861 MachineInstr *predSet = findFirstPredicateSetterFrom(MBB, I);
862 clearFlag(predSet, 0, MO_FLAG_PUSH);
863 I->eraseFromParent();
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000864 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
865 if (CfAlu == MBB.end())
866 break;
867 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE);
868 CfAlu->setDesc(get(AMDGPU::CF_ALU));
Vincent Lejeunefd49dac2013-03-11 18:15:06 +0000869 break;
870 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000871 case AMDGPU::JUMP:
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000872 I->eraseFromParent();
873 break;
874 }
875 return 2;
876}
877
878bool
879R600InstrInfo::isPredicated(const MachineInstr *MI) const {
880 int idx = MI->findFirstPredOperandIdx();
881 if (idx < 0)
882 return false;
883
884 unsigned Reg = MI->getOperand(idx).getReg();
885 switch (Reg) {
886 default: return false;
887 case AMDGPU::PRED_SEL_ONE:
888 case AMDGPU::PRED_SEL_ZERO:
889 case AMDGPU::PREDICATE_BIT:
890 return true;
891 }
892}
893
894bool
895R600InstrInfo::isPredicable(MachineInstr *MI) const {
896 // XXX: KILL* instructions can be predicated, but they must be the last
897 // instruction in a clause, so this means any instructions after them cannot
898 // be predicated. Until we have proper support for instruction clauses in the
899 // backend, we will mark KILL* instructions as unpredicable.
900
901 if (MI->getOpcode() == AMDGPU::KILLGT) {
902 return false;
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000903 } else if (MI->getOpcode() == AMDGPU::CF_ALU) {
904 // If the clause start in the middle of MBB then the MBB has more
905 // than a single clause, unable to predicate several clauses.
906 if (MI->getParent()->begin() != MachineBasicBlock::iterator(MI))
907 return false;
908 // TODO: We don't support KC merging atm
909 if (MI->getOperand(3).getImm() != 0 || MI->getOperand(4).getImm() != 0)
910 return false;
911 return true;
Vincent Lejeune62b0a9b2013-03-05 19:12:06 +0000912 } else if (isVector(*MI)) {
913 return false;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000914 } else {
915 return AMDGPUInstrInfo::isPredicable(MI);
916 }
917}
918
919
920bool
921R600InstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
922 unsigned NumCyles,
923 unsigned ExtraPredCycles,
924 const BranchProbability &Probability) const{
925 return true;
926}
927
928bool
929R600InstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
930 unsigned NumTCycles,
931 unsigned ExtraTCycles,
932 MachineBasicBlock &FMBB,
933 unsigned NumFCycles,
934 unsigned ExtraFCycles,
935 const BranchProbability &Probability) const {
936 return true;
937}
938
939bool
940R600InstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
941 unsigned NumCyles,
942 const BranchProbability &Probability)
943 const {
944 return true;
945}
946
947bool
948R600InstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
949 MachineBasicBlock &FMBB) const {
950 return false;
951}
952
953
954bool
955R600InstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
956 MachineOperand &MO = Cond[1];
957 switch (MO.getImm()) {
958 case OPCODE_IS_ZERO_INT:
959 MO.setImm(OPCODE_IS_NOT_ZERO_INT);
960 break;
961 case OPCODE_IS_NOT_ZERO_INT:
962 MO.setImm(OPCODE_IS_ZERO_INT);
963 break;
964 case OPCODE_IS_ZERO:
965 MO.setImm(OPCODE_IS_NOT_ZERO);
966 break;
967 case OPCODE_IS_NOT_ZERO:
968 MO.setImm(OPCODE_IS_ZERO);
969 break;
970 default:
971 return true;
972 }
973
974 MachineOperand &MO2 = Cond[2];
975 switch (MO2.getReg()) {
976 case AMDGPU::PRED_SEL_ZERO:
977 MO2.setReg(AMDGPU::PRED_SEL_ONE);
978 break;
979 case AMDGPU::PRED_SEL_ONE:
980 MO2.setReg(AMDGPU::PRED_SEL_ZERO);
981 break;
982 default:
983 return true;
984 }
985 return false;
986}
987
988bool
989R600InstrInfo::DefinesPredicate(MachineInstr *MI,
990 std::vector<MachineOperand> &Pred) const {
991 return isPredicateSetter(MI->getOpcode());
992}
993
994
995bool
996R600InstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
997 const SmallVectorImpl<MachineOperand> &Pred2) const {
998 return false;
999}
1000
1001
1002bool
1003R600InstrInfo::PredicateInstruction(MachineInstr *MI,
1004 const SmallVectorImpl<MachineOperand> &Pred) const {
1005 int PIdx = MI->findFirstPredOperandIdx();
1006
Vincent Lejeunef2cfef82013-07-09 15:03:33 +00001007 if (MI->getOpcode() == AMDGPU::CF_ALU) {
1008 MI->getOperand(8).setImm(0);
1009 return true;
1010 }
1011
Vincent Lejeune41107972013-11-16 16:24:41 +00001012 if (MI->getOpcode() == AMDGPU::DOT_4) {
1013 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_X))
1014 .setReg(Pred[2].getReg());
1015 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_Y))
1016 .setReg(Pred[2].getReg());
1017 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_Z))
1018 .setReg(Pred[2].getReg());
1019 MI->getOperand(getOperandIdx(*MI, AMDGPU::OpName::pred_sel_W))
1020 .setReg(Pred[2].getReg());
1021 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1022 MIB.addReg(AMDGPU::PREDICATE_BIT, RegState::Implicit);
1023 return true;
1024 }
1025
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001026 if (PIdx != -1) {
1027 MachineOperand &PMO = MI->getOperand(PIdx);
1028 PMO.setReg(Pred[2].getReg());
NAKAMURA Takumi6b207d32012-12-20 00:22:11 +00001029 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1030 MIB.addReg(AMDGPU::PREDICATE_BIT, RegState::Implicit);
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001031 return true;
1032 }
1033
1034 return false;
1035}
1036
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +00001037unsigned int R600InstrInfo::getPredicationCost(const MachineInstr *) const {
1038 return 2;
1039}
1040
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001041unsigned int R600InstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1042 const MachineInstr *MI,
1043 unsigned *PredCost) const {
1044 if (PredCost)
1045 *PredCost = 2;
1046 return 2;
1047}
1048
Tom Stellarda2b4eb62013-11-13 23:36:50 +00001049void R600InstrInfo::reserveIndirectRegisters(BitVector &Reserved,
Tom Stellardc0b0c672013-02-06 17:32:29 +00001050 const MachineFunction &MF) const {
1051 const AMDGPUFrameLowering *TFL =
1052 static_cast<const AMDGPUFrameLowering*>(TM.getFrameLowering());
Tom Stellardc0b0c672013-02-06 17:32:29 +00001053
1054 unsigned StackWidth = TFL->getStackWidth(MF);
1055 int End = getIndirectIndexEnd(MF);
1056
Tom Stellarda2b4eb62013-11-13 23:36:50 +00001057 if (End == -1)
1058 return;
Tom Stellardc0b0c672013-02-06 17:32:29 +00001059
1060 for (int Index = getIndirectIndexBegin(MF); Index <= End; ++Index) {
1061 unsigned SuperReg = AMDGPU::R600_Reg128RegClass.getRegister(Index);
Tom Stellarda2b4eb62013-11-13 23:36:50 +00001062 Reserved.set(SuperReg);
Tom Stellardc0b0c672013-02-06 17:32:29 +00001063 for (unsigned Chan = 0; Chan < StackWidth; ++Chan) {
1064 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister((4 * Index) + Chan);
Tom Stellarda2b4eb62013-11-13 23:36:50 +00001065 Reserved.set(Reg);
Tom Stellardc0b0c672013-02-06 17:32:29 +00001066 }
1067 }
Tom Stellardc0b0c672013-02-06 17:32:29 +00001068}
1069
1070unsigned R600InstrInfo::calculateIndirectAddress(unsigned RegIndex,
1071 unsigned Channel) const {
1072 // XXX: Remove when we support a stack width > 2
1073 assert(Channel == 0);
1074 return RegIndex;
1075}
1076
Tom Stellard04c55952013-10-22 18:19:10 +00001077const TargetRegisterClass *R600InstrInfo::getIndirectAddrRegClass() const {
1078 return &AMDGPU::R600_TReg32_XRegClass;
Tom Stellardc0b0c672013-02-06 17:32:29 +00001079}
1080
1081MachineInstrBuilder R600InstrInfo::buildIndirectWrite(MachineBasicBlock *MBB,
1082 MachineBasicBlock::iterator I,
1083 unsigned ValueReg, unsigned Address,
1084 unsigned OffsetReg) const {
1085 unsigned AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address);
1086 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1087 AMDGPU::AR_X, OffsetReg);
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001088 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardc0b0c672013-02-06 17:32:29 +00001089
1090 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1091 AddrReg, ValueReg)
Tom Stellardad7ecc62013-06-05 03:43:06 +00001092 .addReg(AMDGPU::AR_X,
1093 RegState::Implicit | RegState::Kill);
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001094 setImmOperand(Mov, AMDGPU::OpName::dst_rel, 1);
Tom Stellardc0b0c672013-02-06 17:32:29 +00001095 return Mov;
1096}
1097
1098MachineInstrBuilder R600InstrInfo::buildIndirectRead(MachineBasicBlock *MBB,
1099 MachineBasicBlock::iterator I,
1100 unsigned ValueReg, unsigned Address,
1101 unsigned OffsetReg) const {
1102 unsigned AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address);
1103 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1104 AMDGPU::AR_X,
1105 OffsetReg);
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001106 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardc0b0c672013-02-06 17:32:29 +00001107 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1108 ValueReg,
1109 AddrReg)
Tom Stellardad7ecc62013-06-05 03:43:06 +00001110 .addReg(AMDGPU::AR_X,
1111 RegState::Implicit | RegState::Kill);
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001112 setImmOperand(Mov, AMDGPU::OpName::src0_rel, 1);
Tom Stellardc0b0c672013-02-06 17:32:29 +00001113
1114 return Mov;
1115}
1116
Vincent Lejeunedae2a202013-04-03 16:49:34 +00001117unsigned R600InstrInfo::getMaxAlusPerClause() const {
1118 return 115;
1119}
Tom Stellardc0b0c672013-02-06 17:32:29 +00001120
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001121MachineInstrBuilder R600InstrInfo::buildDefaultInstruction(MachineBasicBlock &MBB,
1122 MachineBasicBlock::iterator I,
1123 unsigned Opcode,
1124 unsigned DstReg,
1125 unsigned Src0Reg,
1126 unsigned Src1Reg) const {
1127 MachineInstrBuilder MIB = BuildMI(MBB, I, MBB.findDebugLoc(I), get(Opcode),
1128 DstReg); // $dst
1129
1130 if (Src1Reg) {
1131 MIB.addImm(0) // $update_exec_mask
1132 .addImm(0); // $update_predicate
1133 }
1134 MIB.addImm(1) // $write
1135 .addImm(0) // $omod
1136 .addImm(0) // $dst_rel
1137 .addImm(0) // $dst_clamp
1138 .addReg(Src0Reg) // $src0
1139 .addImm(0) // $src0_neg
1140 .addImm(0) // $src0_rel
Tom Stellard9f7818d2013-01-23 02:09:06 +00001141 .addImm(0) // $src0_abs
1142 .addImm(-1); // $src0_sel
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001143
1144 if (Src1Reg) {
1145 MIB.addReg(Src1Reg) // $src1
1146 .addImm(0) // $src1_neg
1147 .addImm(0) // $src1_rel
Tom Stellard9f7818d2013-01-23 02:09:06 +00001148 .addImm(0) // $src1_abs
1149 .addImm(-1); // $src1_sel
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001150 }
1151
1152 //XXX: The r600g finalizer expects this to be 1, once we've moved the
1153 //scheduling to the backend, we can change the default to 0.
1154 MIB.addImm(1) // $last
1155 .addReg(AMDGPU::PRED_SEL_OFF) // $pred_sel
Vincent Lejeunee332e352013-04-30 00:14:08 +00001156 .addImm(0) // $literal
1157 .addImm(0); // $bank_swizzle
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001158
1159 return MIB;
1160}
1161
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001162#define OPERAND_CASE(Label) \
1163 case Label: { \
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001164 static const unsigned Ops[] = \
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001165 { \
1166 Label##_X, \
1167 Label##_Y, \
1168 Label##_Z, \
1169 Label##_W \
1170 }; \
1171 return Ops[Slot]; \
1172 }
1173
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001174static unsigned getSlotedOps(unsigned Op, unsigned Slot) {
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001175 switch (Op) {
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001176 OPERAND_CASE(AMDGPU::OpName::update_exec_mask)
1177 OPERAND_CASE(AMDGPU::OpName::update_pred)
1178 OPERAND_CASE(AMDGPU::OpName::write)
1179 OPERAND_CASE(AMDGPU::OpName::omod)
1180 OPERAND_CASE(AMDGPU::OpName::dst_rel)
1181 OPERAND_CASE(AMDGPU::OpName::clamp)
1182 OPERAND_CASE(AMDGPU::OpName::src0)
1183 OPERAND_CASE(AMDGPU::OpName::src0_neg)
1184 OPERAND_CASE(AMDGPU::OpName::src0_rel)
1185 OPERAND_CASE(AMDGPU::OpName::src0_abs)
1186 OPERAND_CASE(AMDGPU::OpName::src0_sel)
1187 OPERAND_CASE(AMDGPU::OpName::src1)
1188 OPERAND_CASE(AMDGPU::OpName::src1_neg)
1189 OPERAND_CASE(AMDGPU::OpName::src1_rel)
1190 OPERAND_CASE(AMDGPU::OpName::src1_abs)
1191 OPERAND_CASE(AMDGPU::OpName::src1_sel)
1192 OPERAND_CASE(AMDGPU::OpName::pred_sel)
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001193 default:
1194 llvm_unreachable("Wrong Operand");
1195 }
1196}
1197
1198#undef OPERAND_CASE
1199
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001200MachineInstr *R600InstrInfo::buildSlotOfVectorInstruction(
1201 MachineBasicBlock &MBB, MachineInstr *MI, unsigned Slot, unsigned DstReg)
1202 const {
1203 assert (MI->getOpcode() == AMDGPU::DOT_4 && "Not Implemented");
1204 unsigned Opcode;
1205 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +00001206 if (ST.getGeneration() <= AMDGPUSubtarget::R700)
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001207 Opcode = AMDGPU::DOT4_r600;
1208 else
1209 Opcode = AMDGPU::DOT4_eg;
1210 MachineBasicBlock::iterator I = MI;
1211 MachineOperand &Src0 = MI->getOperand(
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001212 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src0, Slot)));
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001213 MachineOperand &Src1 = MI->getOperand(
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001214 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src1, Slot)));
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001215 MachineInstr *MIB = buildDefaultInstruction(
1216 MBB, I, Opcode, DstReg, Src0.getReg(), Src1.getReg());
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001217 static const unsigned Operands[14] = {
1218 AMDGPU::OpName::update_exec_mask,
1219 AMDGPU::OpName::update_pred,
1220 AMDGPU::OpName::write,
1221 AMDGPU::OpName::omod,
1222 AMDGPU::OpName::dst_rel,
1223 AMDGPU::OpName::clamp,
1224 AMDGPU::OpName::src0_neg,
1225 AMDGPU::OpName::src0_rel,
1226 AMDGPU::OpName::src0_abs,
1227 AMDGPU::OpName::src0_sel,
1228 AMDGPU::OpName::src1_neg,
1229 AMDGPU::OpName::src1_rel,
1230 AMDGPU::OpName::src1_abs,
1231 AMDGPU::OpName::src1_sel,
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001232 };
1233
Vincent Lejeune41107972013-11-16 16:24:41 +00001234 MachineOperand &MO = MI->getOperand(getOperandIdx(MI->getOpcode(),
1235 getSlotedOps(AMDGPU::OpName::pred_sel, Slot)));
1236 MIB->getOperand(getOperandIdx(Opcode, AMDGPU::OpName::pred_sel))
1237 .setReg(MO.getReg());
1238
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001239 for (unsigned i = 0; i < 14; i++) {
1240 MachineOperand &MO = MI->getOperand(
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001241 getOperandIdx(MI->getOpcode(), getSlotedOps(Operands[i], Slot)));
Vincent Lejeune4ed99172013-05-17 16:50:32 +00001242 assert (MO.isImm());
1243 setImmOperand(MIB, Operands[i], MO.getImm());
1244 }
1245 MIB->getOperand(20).setImm(0);
1246 return MIB;
1247}
1248
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001249MachineInstr *R600InstrInfo::buildMovImm(MachineBasicBlock &BB,
1250 MachineBasicBlock::iterator I,
1251 unsigned DstReg,
1252 uint64_t Imm) const {
1253 MachineInstr *MovImm = buildDefaultInstruction(BB, I, AMDGPU::MOV, DstReg,
1254 AMDGPU::ALU_LITERAL_X);
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001255 setImmOperand(MovImm, AMDGPU::OpName::literal, Imm);
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001256 return MovImm;
1257}
1258
Tom Stellard04c55952013-10-22 18:19:10 +00001259MachineInstr *R600InstrInfo::buildMovInstr(MachineBasicBlock *MBB,
1260 MachineBasicBlock::iterator I,
1261 unsigned DstReg, unsigned SrcReg) const {
1262 return buildDefaultInstruction(*MBB, I, AMDGPU::MOV, DstReg, SrcReg);
1263}
1264
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001265int R600InstrInfo::getOperandIdx(const MachineInstr &MI, unsigned Op) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001266 return getOperandIdx(MI.getOpcode(), Op);
1267}
1268
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001269int R600InstrInfo::getOperandIdx(unsigned Opcode, unsigned Op) const {
1270 return AMDGPU::getNamedOperandIdx(Opcode, Op);
Vincent Lejeunee67a4af2013-06-04 23:17:15 +00001271}
1272
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001273void R600InstrInfo::setImmOperand(MachineInstr *MI, unsigned Op,
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001274 int64_t Imm) const {
1275 int Idx = getOperandIdx(*MI, Op);
1276 assert(Idx != -1 && "Operand not supported for this instruction.");
1277 assert(MI->getOperand(Idx).isImm());
1278 MI->getOperand(Idx).setImm(Imm);
1279}
1280
1281//===----------------------------------------------------------------------===//
1282// Instruction flag getters/setters
1283//===----------------------------------------------------------------------===//
1284
1285bool R600InstrInfo::hasFlagOperand(const MachineInstr &MI) const {
1286 return GET_FLAG_OPERAND_IDX(get(MI.getOpcode()).TSFlags) != 0;
1287}
1288
1289MachineOperand &R600InstrInfo::getFlagOp(MachineInstr *MI, unsigned SrcIdx,
1290 unsigned Flag) const {
1291 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1292 int FlagIndex = 0;
1293 if (Flag != 0) {
1294 // If we pass something other than the default value of Flag to this
1295 // function, it means we are want to set a flag on an instruction
1296 // that uses native encoding.
1297 assert(HAS_NATIVE_OPERANDS(TargetFlags));
1298 bool IsOP3 = (TargetFlags & R600_InstFlag::OP3) == R600_InstFlag::OP3;
1299 switch (Flag) {
1300 case MO_FLAG_CLAMP:
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001301 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::clamp);
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001302 break;
1303 case MO_FLAG_MASK:
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001304 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::write);
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001305 break;
1306 case MO_FLAG_NOT_LAST:
1307 case MO_FLAG_LAST:
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001308 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::last);
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001309 break;
1310 case MO_FLAG_NEG:
1311 switch (SrcIdx) {
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001312 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_neg); break;
1313 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_neg); break;
1314 case 2: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src2_neg); break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001315 }
1316 break;
1317
1318 case MO_FLAG_ABS:
1319 assert(!IsOP3 && "Cannot set absolute value modifier for OP3 "
1320 "instructions.");
Tom Stellard08f2d932012-12-13 19:38:52 +00001321 (void)IsOP3;
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001322 switch (SrcIdx) {
Tom Stellard5e48a0e2013-06-25 21:22:18 +00001323 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_abs); break;
1324 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_abs); break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001325 }
1326 break;
1327
1328 default:
1329 FlagIndex = -1;
1330 break;
1331 }
1332 assert(FlagIndex != -1 && "Flag not supported for this instruction");
1333 } else {
1334 FlagIndex = GET_FLAG_OPERAND_IDX(TargetFlags);
1335 assert(FlagIndex != 0 &&
1336 "Instruction flags not supported for this instruction");
1337 }
1338
1339 MachineOperand &FlagOp = MI->getOperand(FlagIndex);
1340 assert(FlagOp.isImm());
1341 return FlagOp;
1342}
1343
1344void R600InstrInfo::addFlag(MachineInstr *MI, unsigned Operand,
1345 unsigned Flag) const {
1346 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1347 if (Flag == 0) {
1348 return;
1349 }
1350 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1351 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1352 if (Flag == MO_FLAG_NOT_LAST) {
1353 clearFlag(MI, Operand, MO_FLAG_LAST);
1354 } else if (Flag == MO_FLAG_MASK) {
1355 clearFlag(MI, Operand, Flag);
1356 } else {
1357 FlagOp.setImm(1);
1358 }
1359 } else {
1360 MachineOperand &FlagOp = getFlagOp(MI, Operand);
1361 FlagOp.setImm(FlagOp.getImm() | (Flag << (NUM_MO_FLAGS * Operand)));
1362 }
1363}
1364
1365void R600InstrInfo::clearFlag(MachineInstr *MI, unsigned Operand,
1366 unsigned Flag) const {
1367 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1368 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1369 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1370 FlagOp.setImm(0);
1371 } else {
1372 MachineOperand &FlagOp = getFlagOp(MI);
1373 unsigned InstFlags = FlagOp.getImm();
1374 InstFlags &= ~(Flag << (NUM_MO_FLAGS * Operand));
1375 FlagOp.setImm(InstFlags);
1376 }
1377}